<?xml version="1.0" encoding="UTF-8"?>
<project default="generate.all"
         basedir="."
         name="org.yakindu.sct.doc.user"
         xmlns:if="ant:if"
         xmlns:unless="ant:unless"
         xmlns:artifact="antlib:org.eclipse.aether.ant">

    <!-- Runtime properties:
         website.local.files: Generates website as local files
    -->

    <!-- Include platform-specific or machine-specific properties, if any. -->
    <property file="local.properties" />

    <!-- The documentation source files located beneath this directory.
         They may be structured into folders of arbitrary depth. -->
    <property name="src.dir" value="src" />

    <!-- CSS files are located here. -->
    <property name="css.dir" value="css" />

    <!-- Root CSS file to be referenced by the generated HTML files. -->
    <property name="css.root.file" value="style.css" />

    <!-- Image files reside in subdirectories with the following name: -->
    <property name="img.dir" value="images" />

    <!-- Target directories for various output formats: -->
    <property name="help.dir" value="help" />
    <property name="website.dir" value="website" />
    <property name="pdf.dir" value="pdf" />
    <property name="epub.dir" value="epub" />

    <!-- Libraries used by various Ant tasks are located here: -->
    <property name="lib.dir" location="${basedir}/lib" />

    <!-- Aether Ant Tasks configuration -->
    <property name="aether.jar.version" value="1.0.0.v20140518" />
    <property name="aether.jar.checksum"
              value="9008c0c96390eacc0f7fdde542dacbd921f942dcd5c139465e21f8b583942edb995304025bb436cfeeea0b89698ef9dfccf8a027da68cd079669adea8cca381f" />
    <property name="aether.jar.src"
              value="http://search.maven.org/remotecontent?filepath=org/eclipse/aether/aether-ant-tasks/${aether.jar.version}/aether-ant-tasks-${aether.jar.version}-uber.jar" />
    <property name="aether.jar.file" value="${lib.dir}/aether-ant-tasks-uber.jar" />

    <!-- Wikitext download configuration -->
    <property name="wikitext.zip.version" value="1.0.0.v20140518" />
    <property name="wikitext.zip.checksum"
              value="00d8bf79a9d7baf20e46d2435dd4b6a9963317a2737c731d11029c96aa86f64e8540639b6eefa6f04b8103860f47ff74e998c71b57445179dc20ad7c2847417c" />
    <!--
    <property name="wikitext.zip.src"
              value="http://www.eclipse.org/downloads/download.php?file=/mylyn/snapshots/nightly/docs/wikitext-standalone-latest.zip" />
    -->
    <property name="wikitext.zip.src" value="file:///${basedir}/externals/wikitext.zip" />
    <property name="wikitext.zip.file" value="${lib.dir}/wikitext.zip" />
    <property name="wikitext.basename" value="wikitext-standalone" />
    <property name="wikitext.version" value="2.8.0-SNAPSHOT" />



    <!-- Remove generated stuff: -->
    <target name="clean">
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${basedir}" defaultexcludes="false">
                <include name="${help.dir}/**" />
                <include name="${website.dir}/**" />
                <include name="${pdf.dir}/**" />
                <include name="${epub.dir}/**" />
            </fileset>
            <fileset dir="${src-merged.dir}" />
        </delete>
    </target>


    <!-- Download and unpack the wikitext standalone distribution. Unfortunately it is currently available as a nightly build only. -->
    <target name="wikitext.download" description="Download and unpack the wikitext standalone distribution.">
        <condition property="wikitext.zip.file.exists">
            <and>
                <available file="${wikitext.zip.file}" />
                <checksum file="${wikitext.zip.file}"
                          algorithm="SHA-512"
                          property="${wikitext.zip.checksum}"
                          verifyproperty="checksum.matches" />
            </and>
        </condition>
        <sequential unless:set="wikitext.zip.file.exists">
            <mkdir dir="${lib.dir}" />
            <get src="${wikitext.zip.src}"
                 dest="${wikitext.zip.file}"
                 skipexisting="false"
                 usetimestamp="true"
                 verbose="true" />
            <fail message="Checksum mismatch for ${wikitext.zip.file}. Please delete it and rerun ant to redownload.">
                <condition>
                    <not>
                        <checksum file="${wikitext.zip.file}"
                                  algorithm="SHA-512"
                                  property="${wikitext.zip.checksum}"
                                  verifyproperty="checksum.matches" />
                    </not>
                </condition>
            </fail>
            <unzip dest="${lib.dir}" src="${wikitext.zip.file}" />
        </sequential>
    </target>


    <!-- Resolves the necessary dependencies: -->
    <target name="init.wikitext"
            description="Resolves the necessary wikitext dependencies."
            depends="wikitext.download">
        <property name="wikitext.dir" location="${lib.dir}/${wikitext.basename}-${wikitext.version}" />
        <path id="wikitext.classpath">
            <fileset dir="${wikitext.dir}">
                <include name="*.jar" />
            </fileset>
        </path>
    </target>


    <!-- Generates the documentation in all supported formats. -->
    <target name="generate.all"
            description="Generates the documentation in all supported formats."
            depends="generate.eclipsehelp, generate.website" />


    <!-- Generates the documentation as Eclipse help. -->
    <target name="generate.eclipsehelp"
            description="Generates the documentation as separate Eclipse help (HTML) files from their respective textile (wikitext) sources."
            depends="init.wikitext">

        <sequential>
            <taskdef classpathref="wikitext.classpath"
                     resource="org/eclipse/mylyn/wikitext/core/ant/tasks.properties" />

            <!-- Textile source files to turn into HTML: -->
            <fileset id="fileset.eclipsehelp" dir="${src.dir}">
                <include name="installation/installation.textile" />
                <include name="tutorials/tutorials.textile" />
                <include name="user-guide/overview.textile" />
                <include name="user-guide/editing_statecharts.textile" />
                <include name="user-guide/simulating_statecharts.textile" />
                <include name="user-guide/advanced_simulation.textile" />
                <include name="user-guide/c-domain.textile" />
                <include name="user-guide/statechart_language.textile" />
                <include name="user-guide/generating_code.textile" />
                <include name="user-guide/generating_code_headless.textile" />
                <include name="user-guide/sctunit.textile" />
            </fileset>

            <!-- Check whether we have all the expected textile source files – no more, no less: -->
            <fail message="Number of textile source file(s) doesn't match.">
                <condition>
                    <resourcecount refid="fileset.eclipsehelp" when="ne" count="11" />
                </condition>
            </fail>

            <!-- Convert textile source files to HTML: -->
            <wikitext-to-eclipse-help markupLanguage="Textile"
                                      multipleOutputFiles="false"
                                      navigationImages="true"
                                      formatoutput="true"
                                      validate="true"
                                      failOnValidationError="true"
                                      failOnValidationWarning="true"
                                      sourceEncoding="UTF-8"
                                      title="YAKINDU Statechart Tools"
                                      helpPrefix="${help.dir}">
                <fileset refid="fileset.eclipsehelp" />
                <stylesheet url="../${css.dir}/${css.root.file}" />
            </wikitext-to-eclipse-help>

            <!-- Files have been generated in the source directory, which is bad practice. Move them to the destination
                 directory: -->
            <move todir="${help.dir}" flatten="no" verbose="no">
                <fileset dir="${src.dir}">
                    <include name="**/*.html" />
                    <include name="**/*-toc.xml" />
                </fileset>
            </move>

            <!-- Add HTML META element for Internet Explorer :-( -->
            <replaceregexp byline="false"
                           match='(\s*)(&lt;title&gt;)'
                           replace='\1&lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&gt;\1\2'>
                <fileset dir="${help.dir}">
                    <include name="**/*.html" />
                </fileset>
            </replaceregexp>

            <!-- Add CSS classes "eclipsehelp" and "body-container" to BODY element -->
            <replaceregexp byline="true" match='&lt;body&gt;' replace='&lt;body class="eclipsehelp body-container"&gt;'>
                <fileset dir="${help.dir}">
                    <include name="**/*.html" />
                </fileset>
            </replaceregexp>

            <!-- Fix DIV elements -->
            <replaceregexp byline="false"
                           flags="g"
                           match='&lt;p&gt;&lt;div(.*)&gt;&lt;/p&gt;'
                           replace='&lt;div\1&gt;'>
                <fileset dir="${help.dir}">
                    <include name="**/*.html" />
                </fileset>
            </replaceregexp>
            <replaceregexp byline="false" flags="g" match='&lt;p&gt;&lt;/div&gt;&lt;/p&gt;' replace='&lt;/div&gt;'>
                <fileset dir="${help.dir}">
                    <include name="**/*.html" />
                </fileset>
            </replaceregexp>

            <!-- Copy the CSS files to the destination directory: -->
            <copy todir="${help.dir}/${css.dir}">
                <fileset dir="${css.dir}">
                    <include name="*" />
                </fileset>
            </copy>

            <!-- Copy the image files to the destination directory: -->
            <copy todir="${help.dir}">
                <fileset dir="src">
                    <include name="**/${img.dir}/*" />
                    <exclude name="**/${img.dir}/*.xcf" />
                </fileset>
            </copy>
        </sequential>
    </target>

    <!-- Generates the documentation as various HTML files, each being composed of selected sections. Each part has a
         name and a title, and consists of a set of source files. The generated documentation structure looks like this:

         website
             YCST-installation.html
             YCST-user-guide.html
             YCST-tutorials.html
             css/style.css
                 …
             images/…
    -->

    <target name="generate.website"
            description="Generates the documentation as various HTML files for the website, each being composed of selected sections. Use -Dwebsite.local.files=true to generate the website as local files for preview."
            depends="init.wikitext">

        <sequential>
            <taskdef classpathref="wikitext.classpath"
                     resource="org/eclipse/mylyn/wikitext/core/ant/tasks.properties" />

            <!-- Installation Guide -->
            <create-website-part name="YSCT-installation" title="YAKINDU Statechart Tools Installation Guide">
                <textile-files>
                    <filelist dir="${src.dir}">
                        <file name="installation/installation.textile" />
                    </filelist>
                </textile-files>
                <image-files>
                    <fileset dir="${src.dir}">
                        <include name="installation/images/**/*" />
                    </fileset>
                </image-files>
            </create-website-part>

            <!-- Tutorials -->
            <create-website-part name="YSCT-tutorials" title="YAKINDU Statechart Tools Tutorials">
                <textile-files>
                    <filelist dir="${src.dir}">
                        <file name="tutorials/tutorials.textile" />
                    </filelist>
                </textile-files>
                <image-files>
                    <fileset dir="${src.dir}">
                        <include name="tutorials/images/**/*" />
                    </fileset>
                </image-files>
            </create-website-part>

            <!-- User Guide -->
            <create-website-part name="YSCT-user-guide" title="YAKINDU Statechart Tools User Guide">
                <textile-files>
                    <filelist dir="${src.dir}">
                        <file name="user-guide/overview.textile" />
                        <file name="user-guide/editing_statecharts.textile" />
                        <file name="user-guide/simulating_statecharts.textile" />
                        <file name="user-guide/advanced_simulation.textile" />
                        <file name="user-guide/c-domain.textile" />
                        <file name="user-guide/statechart_language.textile" />
                        <file name="user-guide/generating_code.textile" />
                        <file name="user-guide/generating_code_headless.textile" />
                        <!-- <file name="user-guide/sctunit.textile" /> -->
                    </filelist>
                </textile-files>
                <image-files>
                    <fileset dir="${src.dir}">
                        <include name="user-guide/images/**/*" />
                    </fileset>
                </image-files>
            </create-website-part>

        </sequential>
    </target>



    <!-- Creates a certain part of the HTML documentation for the website. -->
    <macrodef name="create-website-part"
              description="Creates a certain part of the HTML documentation for the website.">
        <attribute name="name" />
        <attribute name="title" />
        <element name="textile-files" />
        <element name="image-files" />

        <sequential>

            <!-- The directory for this particular part of the HTML documentation: -->
            <local name="work.dir" />
            <property name="work.dir" location="${website.dir}" />

            <!-- The names of the concatenated documentation files: -->
            <local name="index.textile" />
            <property name="index.textile" value="@{name}.textile" />
            <local name="index.html" />
            <property name="index.html" value="@{name}.html" />

            <!-- Append those source files that make up the particular piece of documentation: -->
            <mkdir dir="${work.dir}" />
            <concat destfile="${work.dir}/${index.textile}"
                    append="false"
                    overwrite="true"
                    encoding="UTF-8"
                    outputencoding="UTF-8"
                    fixlastline="true"
                    binary="false"
                    ignoreempty="true">
                <textile-files />
            </concat>

            <!-- Convert textile source files to HTML: -->
            <wikitext-to-html markupLanguage="Textile"
                              multipleOutputFiles="false"
                              navigationImages="true"
                              formatoutput="true"
                              validate="true"
                              xhtmlStrict="true"
                              overwrite="true"
                              failOnValidationError="true"
                              failOnValidationWarning="false"
                              sourceEncoding="UTF-8"
                              title="@{title}">
                <fileset dir="${work.dir}">
                    <include name="${index.textile}" />
                </fileset>
                <stylesheet url="${css.dir}/${css.root.file}" />
            </wikitext-to-html>

            <!-- Load website fragments: -->
            <sequential unless:set="website.local.files">
                <loadfile property="website.header" srcFile="${src.dir}/website/header.htmlf" encoding="UTF-8" />
                <loadfile property="website.footer" srcFile="${src.dir}/website/footer.htmlf" encoding="UTF-8" />
                <loadfile property="website.calltoaction.download"
                          srcFile="${src.dir}/website/calltoaction.download.htmlf"
                          encoding="UTF-8" />
            </sequential>

            <!-- Load fragments for generating local files which should look simular to the website: -->
            <sequential if:set="website.local.files">
                <loadfile property="website.header"
                          srcFile="${src.dir}/website/header.local.files.htmlf"
                          encoding="UTF-8" />
                <loadfile property="website.footer"
                          srcFile="${src.dir}/website/footer.local.files.htmlf"
                          encoding="UTF-8" />
                <loadfile property="website.calltoaction.download"
                          srcFile="${src.dir}/website/calltoaction.download.local.files.htmlf"
                          encoding="UTF-8" />
            </sequential>

            <!-- Replace "<pre><code>" durch "<pre class="prettyprint"><code>": -->
            <replaceregexp byline="false"
                           flags="g"
                           match='&lt;pre&gt;&lt;code&gt;'
                           replace='&lt;pre class="prettyprint"&gt;&lt;code&gt;'>
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- Replace relative image links by absolute ones for the website, unless we are generating local files: -->
            <replaceregexp byline="false"
                           flags="g"
                           match='src="images/'
                           replace='src="http://info.itemis.com/hubfs/Yakindu/Statechart_Tools/Documentation/images/'
                           unless:set="website.local.files">
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- Replace everything up to and including "<body>" by website-specific header: -->
            <replaceregexp byline="false" flags="s" replace="${website.header}">
                <regexp pattern="^(.*)&lt;body&gt;" />
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- Replace everything from and including "</body>" by website-specific footer: -->
            <replaceregexp byline="false" flags="s" replace="${website.footer}">
                <regexp pattern="&lt;/body&gt;(.*)$" />
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- If the generated code contains a table with the ID "updaterepositories", replace it by a
                 "call-to-action" button on the website: -->
            <replaceregexp byline="false" flags="s" replace="${website.calltoaction.download}">
                <regexp pattern='&lt;table id="updaterepositories"&gt;.*&lt;/table&gt;' />
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- Fix links: -->
            <adjust-website-document-links document.dir.name="tutorials"
                                           work.dir="${work.dir}"
                                           html.file="${index.html}" />
            <adjust-website-document-links document.dir.name="user-guide"
                                           work.dir="${work.dir}"
                                           html.file="${index.html}" />
            <adjust-website-document-links document.dir.name="installation"
                                           work.dir="${work.dir}"
                                           html.file="${index.html}" />

            <!-- Fix "pro-feature" elements -->
            <replaceregexp byline="false"
                           flags="g"
                           match='&lt;p&gt;&lt;div class="pro-feature"&gt;&lt;/p&gt;'
                           replace='&lt;div class="pro-feature"&gt;'>
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>
            <replaceregexp byline="false" flags="g" match='&lt;p&gt;&lt;/div&gt;&lt;/p&gt;' replace='&lt;/div&gt;'>
                <fileset dir="${work.dir}">
                    <include name="${index.html}" />
                </fileset>
            </replaceregexp>

            <!-- Copy the CSS files to the destination directory: -->
            <copy todir="${work.dir}/${css.dir}">
                <fileset dir="${css.dir}">
                    <include name="*" />
                </fileset>
            </copy>

            <!-- Copy the image files to the destination directory: -->
            <copy todir="${work.dir}/${img.dir}" flatten="yes">
                <image-files />
            </copy>

            <!-- The textile source file is no longer needed in the website directory. Remove it: -->
            <delete includeemptydirs="true" failonerror="false">
                <fileset dir="${website.dir}" defaultexcludes="false">
                    <include name="${index.textile}" />
                </fileset>
            </delete>
        </sequential>

    </macrodef>

    <!-- Adjusts links in the generated HTML document for either the website or a local equivalent. -->
    <macrodef name="adjust-website-document-links"
              description="Adjusts links in the generated HTML document for either the website or a local equivalent.">
        <attribute name="work.dir" />
        <attribute name="html.file" />
        <attribute name="document.dir.name" />

        <sequential>
            <replaceregexp byline="false"
                           flags="g"
                           match='&lt;a href="(\.\./@{document.dir.name})/.+\.html(.*)"&gt;'
                           replace='&lt;a href="\1/\2"&gt;'
                           unless:set="website.local.files">
                <fileset dir="@{work.dir}">
                    <include name="@{html.file}" />
                </fileset>
            </replaceregexp>
            <replaceregexp byline="false"
                           flags="g"
                           match='&lt;a href="\.\.(/@{document.dir.name})/.+\.html(.*)"&gt;'
                           replace='&lt;a href="YSCT-@{document.dir.name}.html\2"&gt;'
                           if:set="website.local.files">
                <fileset dir="@{work.dir}">
                    <include name="@{html.file}" />
                </fileset>
            </replaceregexp>

        </sequential>
    </macrodef>

</project>
