hdoc_to_epub2.ant 5.75 KB
Newer Older
1
<?xml version="1.0" encoding="UTF-8"?>
cliclac's avatar
cliclac committed
2 3 4
<project name="hdoc_to_epub" basedir="." default="convert">

  <taskdef resource="net/sf/antcontrib/antlib.xml"/>
cliclac's avatar
cliclac committed
5
  <taskdef name="jing" classname="com.thaiopensource.relaxng.util.JingTask"/>
cliclac's avatar
cliclac committed
6 7 8 9

  <property name="in" location="${basedir}/input"/>
  <property name="out" location="${basedir}/output"/>
  <property name="tmp" location="${basedir}/tmp"/>
cliclac's avatar
cliclac committed
10
  <property name="xsl" location="${basedir}/xsl"/>
Asus-PC's avatar
Asus-PC committed
11
  <property name="file" location="${basedir}/file"/>
cliclac's avatar
cliclac committed
12
  <property name="schema" location="${basedir}/schema"/>
cliclac's avatar
cliclac committed
13
  <property name="lib" location="${basedir}/lib"/>
14 15
  <property name="tmpRetour" location="${basedir}/tmp/retour"/>
  
cliclac's avatar
cliclac committed
16 17 18 19 20 21 22 23
  <target name="convert">
    <delete dir="${tmp}" failonerror="false"/>
    <sleep seconds="1"/>
    <mkdir dir="${tmp}"/>

    <delete dir="${out}" failonerror="false"/>
    <sleep seconds="1"/>
    <mkdir dir="${out}"/>
24 25
  
    <mkdir dir="${tmpRetour}"/>
cliclac's avatar
cliclac committed
26 27 28 29 30 31 32 33 34

    <for param="inputFile">
      <path>
        <fileset dir="${in}" includes="**/*.hdoc"/>
      </path>

      <sequential>
        <local name="filename"/>
        <basename property="filename" file="@{inputFile}"/>
35
        
cliclac's avatar
cliclac committed
36 37 38 39 40 41 42
        <antcall target="UnzipHdocFile">
          <param name="filename" value="${filename}"/>
        </antcall>

        <antcall target="ValidateInput">
          <param name="filename" value="${filename}"/>
        </antcall>
Asus-PC's avatar
Asus-PC committed
43

44 45 46
        <antcall target="MakeOCF">
          <param name="filename" value="${filename}"/>
        </antcall>
Asus-PC's avatar
Asus-PC committed
47

cliclac's avatar
cliclac committed
48 49 50
        <antcall target="MakeOPF">
          <param name="filename" value="${filename}"/>
        </antcall>
Asus-PC's avatar
Asus-PC committed
51

cliclac's avatar
cliclac committed
52 53 54
        <antcall target="MakeOPS">
          <param name="filename" value="${filename}"/>
        </antcall>
cliclac's avatar
cliclac committed
55 56 57
        
        <propertyregex property="properFilename" input="${filename}" regexp=".hdoc" replace="" casesensitive="false" override="true" />
        
58
        <antcall target="ZipEpubFile">
cliclac's avatar
cliclac committed
59
          <param name="properFilename" value="${properFilename}"/>
60 61 62
        </antcall>

        <antcall target="ValidateOutput">
cliclac's avatar
cliclac committed
63
          <param name="properFilename" value="${properFilename}"/>
64 65 66
        </antcall>
        
        <antcall target="CleanTmp"/>
cliclac's avatar
cliclac committed
67 68 69
      </sequential>
    </for>
  </target>
cliclac's avatar
cliclac committed
70
  
cliclac's avatar
cliclac committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  <target name="UnzipHdocFile">
    <!-- Unzip the input hdoc file. Decompressed folder is named "decompressedHdoc" : this name is the only one which
    refers to the hdoc file furthermore in this project. -->
    <unzip src="${in}/${filename}" dest="${tmp}/${filename}/decompressedHdoc"/>
    <chmod dir="${tmp}/${filename}/decompressedHdoc" perm="777"/>
  </target>

  <!-- Validating the XML container file -->
	<target name="ValidateInput">
    <trycatch property="foo" reference="bar">
      <try>
        <jing file="${tmp}/${filename}/decompressedHdoc/META-INF/container.xml" rngfile="${schema}/hdoc1-container.rng"></jing>
      </try>
      <catch>
        <echo>Validation failed</echo>
      </catch>
    </trycatch>
	</target>

Asus-PC's avatar
Asus-PC committed
90
  <target name="MakeOCF">
91 92 93 94
    <mkdir dir="${tmpRetour}/META-INF"/>
    <mkdir dir="${tmpRetour}/oebps"/>
    <copy file="${file}/mimetype" todir="${tmpRetour}"/>
    <copy file="${file}/container.xml" todir="${tmpRetour}/META-INF"/>
Asus-PC's avatar
Asus-PC committed
95 96 97
  </target>

  <target name="MakeOPF">
cliclac's avatar
cliclac committed
98 99
    <!-- Récupération du nom des images dans my.images séparé par des ','-->
    <path id="pathImage">
Asus-PC's avatar
Asus-PC committed
100
      <fileset dir="${tmp}/${filename}/decompressedHdoc/re" erroronmissingdir="false">
cliclac's avatar
cliclac committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        <include name="**/*.jpeg"/>
        <include name="**/*.jpg"/>
        <include name="**/*.svg"/>
        <include name="**/*.png"/>
      </fileset>
    </path>
    <!-- Récupération que du nom de l'image et non de tout le chemin -->
    <pathconvert pathsep="," property="my.images" refid="pathImage">
      <chainedmapper>
        <flattenmapper/>
        <globmapper from="*" to="*"/>
      </chainedmapper>
    </pathconvert>
    
    <xslt in="${tmp}/${filename}/decompressedHdoc/content.xml" out="${tmpRetour}/oebps/content.opf" classpath="${lib}/saxon9he.jar" style="${xsl}/content.xsl">
      <param name="images" expression="${my.images}"/>
    </xslt>
Asus-PC's avatar
Asus-PC committed
118 119 120
  </target>

  <target name="MakeOPS">
cliclac's avatar
cliclac committed
121 122
    <mkdir dir="${tmpRetour}/oebps/styles"/>
    <copy file="${file}/stylesheet.css" todir="${tmpRetour}/oebps/styles"/>
123
    <mkdir dir="${tmpRetour}/oebps/images"/>
cliclac's avatar
cliclac committed
124
    <copy todir="${tmpRetour}/oebps/images" failonerror="false">
cliclac's avatar
cliclac committed
125 126 127 128 129 130
      <fileset dir="${tmp}/${filename}/decompressedHdoc/re">
        <include name="**/*.jpeg"/>
        <include name="**/*.jpg"/>
        <include name="**/*.svg"/>
        <include name="**/*.png"/>
      </fileset>
cliclac's avatar
cliclac committed
131
    </copy>
132 133 134 135
    <mkdir dir="${tmpRetour}/oebps/chapitres"/>
    <xslt in="${tmp}/${filename}/decompressedHdoc/content.xml" out="${tmpRetour}/oebps/tableDesMatieres.ncx" classpath="${lib}/saxon9he.jar" style="${xsl}/tdmncx.xsl"/>
    <xslt destdir="${tmpRetour}/oebps/chapitres" basedir="${tmp}/${filename}/decompressedHdoc" includes="content.xml" classpath="${lib}/saxon9he.jar" style="${xsl}/chapitre.xsl"/>
    <delete file="${tmpRetour}/oebps/chapitres/content.html"/>
Asus-PC's avatar
Asus-PC committed
136 137
  </target>

138 139 140 141 142 143 144 145 146 147 148 149 150 151
  <target name="ZipEpubFile">
    <zip destfile="${out}/${properFilename}.epub" update="true" encoding="UTF-8">
      <zipfileset dir="${tmpRetour}/META-INF"
        includes="*" prefix="META-INF"/>
      <zipfileset dir="${tmpRetour}/oebps"
        includes="**" prefix="oebps"/>
    </zip>
    <zip destfile="${out}/${properFilename}.epub" update="true" compress="false" encoding="UTF-8" keepcompression="true" createunicodeextrafields="never">
      <zipfileset dir="${tmpRetour}"
        includes="mimetype"/>
    </zip>
  </target>
  
  <target name="ValidateOutput">
cliclac's avatar
cliclac committed
152 153
    <java jar="./epubcheck-4.0.2/epubcheck.jar" fork="true">  
      <arg value="${out}/${properFilename}.epub"/>
Asus-PC's avatar
Asus-PC committed
154
    </java>
155 156 157
  </target>
  
  <target name="CleanTmp">
158
    <delete dir="${tmp}" failonerror="false"/>
159 160
  </target>
  
161
</project>