<?xml version="1.0" encoding="UTF-8"?>
<project name="org.eclipse.m2m.atl.doc" default="all">
	<description>
            Generate Eclipse help content from ATL wiki entries. 
		WARNING: this project cannot be built without 
		- Mylyn wikitext installed. update site:
			download.eclipse.org/tools/mylyn/update/incubator
		- the following projects in the same workspace:
				org.eclipse.mylyn.help.ui
				org.eclipse.mylyn.wikitext.core
				org.eclipse.mylyn.wikitext.mediawiki.core
			Those projects are available here: /cvsroot/tools/org.eclipse.mylyn/
		REASON: special characters + images
    </description>
		
	<property name="wiki.concepts.page.name" value="Concepts"/>
	<property name="wiki.user.page.name" value="User_Guide"/>
	<property name="wiki.developer.page.name" value="Developer_Guide"/>
	
	<property name="wikiurl" value="http://wiki.eclipse.org" />
	<property name="imageFolder" value="images" />

	<path id="tasks.classpath" path="bin" />

	<path id="wikitext.tasks.classpath">
		<pathelement location="../org.eclipse.mylyn.wikitext.core/@dot" />
		<pathelement location="../org.eclipse.mylyn.wikitext.core/bin" />
		<pathelement location="../org.eclipse.mylyn.wikitext.mediawiki.core/@dot" />
		<pathelement location="../org.eclipse.mylyn.wikitext.mediawiki.core/bin" />
	</path>

	<taskdef classpathref="tasks.classpath" resource="org/eclipse/mylyn/internal/help/ui/anttask/tasks.properties" />
	<taskdef classpathref="wikitext.tasks.classpath" resource="org/eclipse/mylyn/wikitext/core/util/anttask/tasks.properties" />

	<target name="all" description="Generate Eclipse help content for the ATL Documentation">
		<eclipse-wiki-to-help 
			help.doc.filenamenoextension="ATL Developer Guide" 
			help.doc.url.html="${wikiurl}/ATL/${wiki.developer.page.name}" 
			help.doc.url.xml="${wikiurl}/Special:Export/ATL/${wiki.developer.page.name}" 
			wiki.url.base="${wikiurl}/ATL" 
			targetfolder="guide/developer" />
		<eclipse-wiki-to-help 
			help.doc.filenamenoextension="ATL Concepts" 
			help.doc.url.html="${wikiurl}/ATL/${wiki.concepts.page.name}" 
			help.doc.url.xml="${wikiurl}/Special:Export/ATL/${wiki.concepts.page.name}" 
			wiki.url.base="${wikiurl}/ATL" 
			targetfolder="guide/concepts" />
		<eclipse-wiki-to-help 
			help.doc.filenamenoextension="ATL User Guide" 
			help.doc.url.html="${wikiurl}/ATL/${wiki.user.page.name}" 
			help.doc.url.xml="${wikiurl}/Special:Export/ATL/${wiki.user.page.name}" 
			wiki.url.base="${wikiurl}/ATL" 
			targetfolder="guide/user" />
	</target>

	<macrodef name="eclipse-wiki-to-help">
		<attribute name="help.doc.url.html" />
		<attribute name="help.doc.url.xml" />
		<attribute name="help.doc.filenameNoExtension" />
		<attribute name="wiki.url.base" />
		<attribute name="targetFolder" />
		<sequential>
			<mkdir dir="tmp" />
			<get dest="tmp/help.doc.xml" src="@{help.doc.url.xml}" />
			<get dest="tmp/help.doc.html" src="@{help.doc.url.html}" />

			<copy todir="tmp">
				<fileset dir="images" />
			</copy>
			<mediawiki-fetch-images src="tmp/help.doc.html" dest="tmp" base="${wikiurl}" />

			<xslt style="extract-markup.xsl" in="tmp/help.doc.xml" out="tmp/@{help.doc.filenameNoExtension}.mediawiki" />
			<echo append="true" file="tmp/@{help.doc.filenameNoExtension}.mediawiki">

= Updating This Document =

This document is maintained in a collaborative wiki.  If you wish to update or modify this document please visit 
@{help.doc.url.html}
			</echo>

			<wikitext-to-eclipse-help markupLanguage="org.eclipse.mylyn.wikitext.mediawiki.core.MediaWikiLanguage" multipleOutputFiles="true" navigationImages="true" helpPrefix="@{targetFolder}" internallinkpattern="@{wiki.url.base}/{0}" validate="true" failonvalidationerror="true" prependImagePrefix="${imageFolder}" formatoutput="true">
				<fileset dir="tmp" includes="**/*.mediawiki" />
				<stylesheet url="book.css" />
			</wikitext-to-eclipse-help>

			<mkdir dir="@{targetFolder}/${imageFolder}" />
			<copy todir="@{targetFolder}/${imageFolder}" overwrite="true">
				<fileset dir="tmp">
					<include name="*.gif" />
					<include name="*.PNG" />
					<include name="*.png" />
					<include name="*.JPG" />
					<include name="*.jpg" />
				</fileset>
			</copy>
			<copy todir="@{targetFolder}" overwrite="true">
				<fileset dir="tmp">
					<include name="*.html" />
					<include name="*toc.xml" />
					<exclude name="help.doc.html" />
				</fileset>
			</copy>
			<antcall target="test" />
			<delete includeemptydirs="true" failonerror="false">
				<fileset dir="tmp" />
			</delete>
		</sequential>

	</macrodef>


	<target name="test" description="verify that all of the HTML files are well-formed XML">
		<echo level="info">
Validating help content XML and HTML files: The Eclipse help system expects well-formed XML
			
If validation fails it is because either:
			
* the html source code is poorly formed, or
* the WikiText MediaWiki parser has a bug
			
Problems with html source are usually caused by improper use of HTML markup in the MediaWiki source,
or inadvertently starting a line with a space character (in MediaWiki this starts a preformatted block)
		</echo>

		<!-- 
		Don't bother with DTD validation: we only care if the files are well-formed.
		We therefore provide an empty DTD 
		-->
		<echo file="tmp/__empty.dtd" message="" />
		<xmlvalidate lenient="true">
			<fileset dir="guide">
				<include name="**/*.xml" />
			</fileset>
			<fileset dir="guide">
				<include name="**/*.html" />
			</fileset>
			<dtd publicid="-//W3C//DTD XHTML 1.0 Transitional//EN" location="${basedir}/tmp/__empty.dtd" />
		</xmlvalidate>
	</target>
</project>
