<project name="help-runtime" default="build" basedir=".">

  <!--
    - encoding used in XSL transform defaults to utf-8. Override via
    - command line with -Dencoding=<encoding> to use other encodings.
    - TocMerge.java overrides this property with the encoding detected in
    - toc-master.xml.
    -->
  <property file="build.properties" prefix="props" />

  <target name="build" depends="xform_toc_conditional" />

  <!-- update the TOC if subsidiary TOC files are newer than tocTab.js -->

  <target name="xform_toc_conditional">
    <uptodate property="toc-js-uptodate" targetfile="tocTab.js">
      <srcfiles dir="." includes="toc.xml, toc.xsl" />
    </uptodate>
    <antcall target="xform_toc_check" />
  </target>

  <target name="xform_toc_check" unless="toc-js-uptodate">
    <antcall target="xform_toc" />
  </target>

  <!-- run XSLT on the TOC to produce tocTab.js -->
  <target name="xform_toc">
  
    <!--
      -  delete tocTab.js since it will be regenerated
      -->
    <delete file="tocTab.js"/>
    
    <echo message="encoding: ${props.encoding}" />

    <xslt in="toc.xml"
          out="tocTab.js"
          style="toc.xsl">
      <outputproperty name="method" value="text"/>
      <outputproperty name="standalone" value="no"/>
      <outputproperty name="encoding" value="${props.encoding}"/>
      <outputproperty name="indent" value="no"/>
    </xslt>

    <!-- clean up after XSLT's braindead <xsl:number> formatting
      -  that outputs "1,000" instead of "1000" in array indices.
      -  Actually remove any non-digit that is embedded in the
      -  digits to account for non-English grouping separators.
      -->
    <replaceregexp file="tocTab.js"
                   match="\[([0-9]*)[^0-9]([0-9]*)\]"
                   replace="[\1\2]"
                   byline="true" />
  </target>

</project>
