<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../etc/phing-grammar.rng" 
    type="application/xml" 
    schematypens="http://relaxng.org/ns/structure/1.0"?>
<!-- 
 * ==============================================================================
 * Main Phing build file for DocBook5 version of the Phing manual
 *
 * Public targets that uses xsltproc processing
 * ____________________________________________
 * html (default)    Produce single page HTML output
 * htmlfancy         Alternative styling for single page HTML output
 * chunk             Produce chunked HTML output
 * pdf               Produce PDF output
 * epub              Experimental support for EPUB
 * webhelp           Produce webhelp output (with searchable index)
 * validate          Validate all DocBook5 sources (requires Jing to be installed)
 * clean             Remove all generated files
 * 
 * Public targets that needs Saxon 6.5.5 for transformation
 * ________________________________________________________
 * The following two targets produces versions of the manual that applies
 * syntax highlightning to all source listings (XML and PHP). However, this
 * requres that Saxon 6.5.5 xsl processor is installed as well as the extra
 * jar file xslthl-2.x.x.jar (in the CLASSPATH). 
 *
 * PLEASE NOTE that these two targets uses a utility script "hlsaxon" which
 * needs a full path to the DocBook5 stylesheets in the script. This varies
 * between setups and needs to be adjusted. Mutatis mutandis.
 *
 * hlhtml	     Create HTML version using syntax highlighting
 * hlpdf         Create PDF version using syntax highlighting
 *   
 * Created: 7 feb 2012
 * Author: Johan Persson <johan162@gmail.com>
 * ==============================================================================    
-->  

<project name="PhingDocBookGuide" default="all" basedir="."
    description="Build script to produce Phing documentation from DocBook5 sources">

    <!--
        ========================================================================================
        START OF System paths setting. The follwing properties defines capabilities and paths 
        for the system this build is running on.
        ========================================================================================
    -->
    <!-- DocBook Grammar. Used when running validation script -->
    <property name="docbook.relaxngc" value="/usr/share/xml/docbook/schema/rng/5.0/docbookxi.rnc"/>
    <property name="docbook.relaxng" value="/usr/share/xml/docbook/schema/rng/5.0/docbookxi.rng"/>


    <!-- Paths to executables needded -->
    <property name="xsltproc" value="/usr/bin/xsltproc"/>
    <property name="xmllint" value="/usr/bin/xmllint"/>
    <property name="fop" value="/usr/bin/fop"/>
    <!-- DocBook script to help produce ePub output format -->
    <property name="dbtoepub"
        value="/usr/share/xml/docbook/stylesheet/nwalsh5/current/epub/bin/dbtoepub"/>

    <!-- 
        Run the "tidy" program to clean up the produced html.
        This is mostly useful when tweaking the styling when the output produced by the
        XSL sheets must be studied. Running tidy makes it easier to read the resulting (X)HTML.        
    -->
    <property name="flag.tidy" value="0"/>
    <property name="tidy" value="/usr/bin/tidy"/>

    <!--
        ========================================================================================
        END OF System paths settings.
        Noramlly there should be no need for changes below this line.
        ========================================================================================
    -->

    <!-- Name of the master XML file -->
    <property name="master.document" value="source/master.xml"/>

    <!-- Name of the master XML file without file suffix-->
    <property name="master.document.strip" value="source/master"/>

    <!-- Base directory for XSL stylesheets processed by xsltproc. No trailing '/' -->
    <property name="dbxsl.dir" value="stylesheets/xsl"/>

    <!-- Base directory for css stylesheets. No trailing '/' -->
    <property name="dbcss.dir" value="stylesheets/css"/>


    <!-- 
        Possible CSS stylesheet for use with the HTML output.
        The "book-fancyterm.css" variant uses a more grphic illustration of screen
        (or terminal) output in the image of a real terminal window.
        
        NOTE: If you change the name you must also update the corresponding
        xsl stylesheet since this name is set as a parameter there to tell which
        css file to use
    -->
    <property name="css.style" value="${dbcss.dir}/book.css"/>
    <property name="css.highlight" value="${dbcss.dir}/highlight.css"/>

    <!--
        Alternate CSS file for some more "fancy" styling. As for now this only
        includes a more graphic illustration of the screen element to make it
        resemble a terminal window.
    -->
    <property name="css.style.fancy" value="${dbcss.dir}/book-fancyterm.css"/>

    <!-- Canonical name used to reference the chosen CSS stylesheet from the generated (X)HTML -->
    <property name="css.name" value="book.css"/>

    <!-- The temporary build directory -->
    <property name="tmp.dir" value="./tmp/"/>

    <!-- 
	 Temporary locations for admonition figures used in FO build. MUST be absolute path! 
	 Also note that this path must match the path given in stylesheets/xsl/fo-common.xsl 
    -->
    <property name="tmp.xslimg.dir" value="/tmp/_tmpxslimg" />

    <!-- All source files needed -->
    <fileset dir="source" id="sources">
        <include name="chapters/*.xml"/>
        <include name="appendixes/*.xml"/>
        <include name="*.xml"/>
        <exclude name="template_for_tasks.xml"/>
    </fileset>

    <!--
        ================================================================================
        TARGET: Clean 
        ================================================================================
    -->
    <target name="clean">
        <delete quiet="yes" dir="output/"/>
	<phingcall target="_clean_fo_images"/>
    </target>

    <!--
        ================================================================================
        TARGET: validate
        Use Jing to validate book against DocBook Relax NG
	    ================================================================================
    -->
    <target name="validate">
        <xmllint schema="${docbook.relaxng}" useRNG="true">
            <fileset refid="sources"/>
        </xmllint>
    </target>

    <!-- 
        ================================================================== 
        Create necessary output directories for
        Parameters:
        ${dir}  String  Subdirectory.One of html,hlhtml
        ================================================================== 
     -->
    <target name="_prepare.output">
        <mkdir dir="output/${dir}"/>
        <mkdir dir="output/${dir}/img"/>

        <!-- Copy all figures used in the documentation -->
        <available file="source/figures" property="figures.exist"/>
        <if>
            <equals arg1="figures.exist" arg2="true"/>
            <then>
                <copy todir="output/${dir}/figures">
                    <fileset dir="source/figures">
                        <include name="*.png"/>
                        <include name="*.jpg"/>
                    </fileset>
                </copy>
            </then>
        </if>

        <copy todir="output/${dir}/img">
            <fileset dir="${dbcss.dir}/img">
                <include name="*"/>
            </fileset>
        </copy>
    </target>

    <!--
        ================================================================================
	For PDF creation FO needs the included admonition images to be available at an
	absolute path while the PDF is created. For this we use a temporary dir that
	we clean after usage.
        ================================================================================
    -->
    <target name="_copy_fo_images">
      <mkdir dir="${tmp.xslimg.dir}" />
      <mkdir dir="${tmp.xslimg.dir}/images" />
        <!-- Copy all admonitions used in FO output -->
	<copy todir="${tmp.xslimg.dir}/images">
	  <fileset dir="stylesheets/xsl/images">
	    <include name="*.png"/>
	    <include name="*.svg"/>
	  </fileset>
	</copy>
    </target>

    <target name="_clean_fo_images">
      <delete quiet="yes" dir="${tmp.xslimg.dir}" />
    </target>

    <!--
        ================================================================================
        Target: _html 
        Help target for the actual HTML and HTMLFANCY targets
        ================================================================================
    -->
    <target name="_html">
        <uptodate property="target.uptodate" targetfile="output/${outdir}/index.html">
            <fileset refid="sources"/>
            <fileset dir="${dbxsl.dir}">
                <include name="html.xsl"/>
                <include name="chunk.xsl"/>
                <include name="common.xsl"/>
            </fileset>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
                <phingcall target="_prepare.output">
                    <property name="dir" value="${outdir}"/>
                </phingcall>
                <copy todir="output/${outdir}/img">
                    <fileset dir="${dbcss.dir}/img">
                        <include name="*"/>
                    </fileset>
                </copy>
                <exec
                    command="${xsltproc} --xinclude -o 'output/${outdir}/' ${dbxsl.dir}/${xslformat}.xsl ${master.document}"
                    checkreturn="true" passthru="true" dir="./"/>
                <if>
                    <equals arg1="${flag.tidy}" arg2="1"/>
                    <then>
                        <exec command="${tidy} -m -utf8 output/${outdir}/index.html"/>
                    </then>
                </if>
            </then>
        </if>
        <copy file="${cssfile}" tofile="output/${outdir}/${css.name}"/>
        <touch file="output/${outdir}/highlight.css"/>
    </target>

    <target name="html">
        <phingcall target="_html">
            <property name="outdir" value="html"/>
            <property name="xslformat" value="html"/>
            <property name="cssfile" value="${css.style}"/>
        </phingcall>
    </target>

    <target name="htmlfancy">
        <phingcall target="_html">
            <property name="outdir" value="htmlfancy"/>
            <property name="xslformat" value="html.fancycmd"/>
            <property name="cssfile" value="${css.style.fancy}"/>
        </phingcall>
        <copy file="${css.style}" tofile="output/htmlfancy/${css.name}"/>
    </target>

    <target name="chunk">
        <phingcall target="_html">
            <property name="outdir" value="chunkhtml"/>
            <property name="xslformat" value="chunk"/>
            <property name="cssfile" value="${css.style}"/>
        </phingcall>
    </target>

    <!-- 
        ================================================================================
        Target: hlhtml Highlighted HTML
        Note: This target will only work with the Saxon 6.5.5 xslt processor 
        ================================================================================        
    -->
    <target name="hlhtml">
        <uptodate property="target.uptodate" targetfile="output/hlhtml/index.html">
            <fileset refid="sources"/>
            <fileset dir="${dbxsl.dir}">
                <include name="hl-html.xsl"/>
                <include name="html-highlight.xsl"/>
                <include name="common.xsl"/>
            </fileset>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
                <phingcall target="_prepare.output">
                    <property name="dir" value="hlhtml"/>
                </phingcall>
                <!-- We first need to flatten the master file since Saxon doesn't handle XInclude -->
                <exec command="${xmllint} --xinclude ${master.document} > output/hlhtml/_flatten.xml"/>

                <exec dir="output/hlhtml/"
                    command="../../scripts/hlsaxon _flatten.xml ../../${dbxsl.dir}/hl-html.xsl"
                    checkreturn="true"/>
                <delete file="output/hlhtml/_flatten.xml"/> 
            </then>
        </if>
        <copy file="${css.style}" todir="output/hlhtml"/>
        <copy file="${css.highlight}" todir="output/hlhtml"/>
    </target>


    <!--
        ================================================================================
        Target: PDF  &&  HLPDF     
        ================================================================================
    -->
    <target name="pdf">
        <uptodate property="target.uptodate" targetfile="output/pdf/manual.pdf">
            <fileset refid="sources"/>
            <fileset dir="${dbxsl.dir}">
                <include name="fo.xsl"/>
                <include name="fo-common.xsl"/>
                <include name="common.xsl"/>
            </fileset>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
	       <phingcall target="_copy_fo_images"/>
                <mkdir dir="output/pdf"/>
                <exec
                    command="${xsltproc} --xinclude --output output/pdf/manual.fo ${dbxsl.dir}/fo.xsl ${master.document}"
                    checkreturn="true" passthru="true" dir="./"/>
                <exec command="${fop} manual.fo -pdf manual.pdf" dir="./output/pdf/"
                    checkreturn="true" passthru="true"/>
                <delete file="output/pdf/manual.fo"/>
	       <phingcall target="_clean_fo_images"/>
            </then>
        </if>
    </target>

    <target name="hlpdf">
        <uptodate property="target.uptodate" targetfile="output/hlpdf/manual.pdf">
            <fileset refid="sources"/>
            <fileset dir="${dbxsl.dir}">
                <include name="hl-fo.xsl"/>
                <include name="fo-highlight.xsl"/>
                <include name="fo-common.xsl"/>
                <include name="common.xsl"/>
            </fileset>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
	       <phingcall target="_copy_fo_images"/>
                <mkdir dir="output/hlpdf"/>
                <exec command="${xmllint} --xinclude ${master.document} > output/hlpdf/_flatten.xml"/>
                <exec dir="output/hlpdf/"
                    command="../../scripts/hlsaxon -o manual.fo _flatten.xml ../../${dbxsl.dir}/hl-fo.xsl"
                    checkreturn="true"/>
                <exec dir="output/hlpdf/" command="${fop} manual.fo -pdf manual.pdf"
                    checkreturn="true"/>
                <delete file="output/hlpdf/_flatten.xml"/>
                <delete file="output/hlpdf/manual.fo"/>
		<phingcall target="_clean_fo_images"/>
            </then>
        </if>
    </target>

    <!--
        ================================================================================
        Target: EPUB  
        Note: Experimental support for ePub format. (Basically a zipped HTML with
        a XML index file.)
        ================================================================================
    -->
    <target name="epub">
        <uptodate property="target.uptodate" targetfile="output/epub/manual.epub">
            <fileset refid="sources"/>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
                <mkdir dir="output/epub"/>
                <exec
                    command="${dbtoepub} -o 'output/epub/manual.epub' -s ${dbxsl.dir}/epub.xsl -c ${css.style} ${master.document} "
                    checkreturn="true"
                />
            </then>
        </if>
    </target>
       
    <!--
        ================================================================================
        Target: WEBHELP  
        Note: !! This requires 'ant' to be installed !!
        ================================================================================
    -->
    <target name="webhelp">
        <echo>Running webhelp, please wait ...</echo>
        <uptodate property="target.uptodate" targetfile="output/webhelp/index.html">
            <fileset refid="sources"/>
        </uptodate>
        <if>
            <not>
                <isset property="target.uptodate"/>
            </not>
            <then>
                <exec command="ant -f ant-build-webhelp.xml webhelp" dir="." checkreturn="true" />            
            </then>
        </if>        

    </target>

    <!--
	    ================================================================================
        Target: ALL
        Build all possible versions of the manual (incl. highlighted versions)
        ================================================================================

    -->
    <target name="all">
<!--        <phingcall target="pdf"/> -->
        <phingcall target="hlpdf"/>
<!--        <phingcall target="htmlfancy"/>
        <phingcall target="html"/> -->
        <phingcall target="hlhtml"/>
        <phingcall target="chunk"/>
        <phingcall target="epub"/>
<!--        <phingcall target="webhelp"/> -->
    </target>

</project>
