hdoc_to_epub.ant 4.69 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
test  
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
test  
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>
Asus-PC's avatar
Asus-PC committed
55

56 57 58 59 60 61 62 63 64
        <antcall target="ZipEpubFile">
          <param name="filename" value="${filename}"/>
        </antcall>

        <antcall target="ValidateOutput">
          <param name="filename" value="${filename}"/>
        </antcall>
        
        <antcall target="CleanTmp"/>
cliclac's avatar
cliclac committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      </sequential>
    </for>
  </target>

  <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
88
  <target name="MakeOCF">
89 90 91 92
    <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
93 94 95
  </target>

  <target name="MakeOPF">
96
    <xslt in="${tmp}/${filename}/decompressedHdoc/content.xml" out="${tmpRetour}/oebps/content.opf" classpath="${lib}/saxon9he.jar" style="${xsl}/content.xsl"/>
Asus-PC's avatar
Asus-PC committed
97 98 99
  </target>

  <target name="MakeOPS">
100 101
    <mkdir dir="${tmpRetour}/oebps/images"/>
    <copy todir="${tmpRetour}/oebps/images">
cliclac's avatar
cliclac committed
102 103
      <fileset dir="${tmp}/${filename}/decompressedHdoc/re" includes="**"/>
    </copy>
104 105 106 107 108
    <mkdir dir="${tmpRetour}/oebps/styles"/>
    <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
109 110
  </target>

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  <target name="ZipEpubFile">
    <propertyregex property="properFilename" input="${filename}" regexp=".hdoc" replace="" casesensitive="false" override="true" />
    <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>
    <!-- zip64Mode="never" -->
    <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">
    
  </target>
  
  <target name="CleanTmp">
    <delete dir="${tmp}" failonerror="false"/>
  </target>
  
134
</project>