<commit_before>/*--
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.stream.*
import solidbase.xml.elements.*

def reader = XMLInputFactory.newInstance().createXMLStreamReader( new InputStreamReader( new FileInputStream( "doc/manual.xml" ), "UTF-8" ) )
assert reader.hasNext()
reader.next()
assert reader.hasNext()
reader.next()

def book = StaxNodeReader.readNode( reader )
assert book.name == "book"

new File( "../solidbase-wiki/UsersManual.wiki" ).withPrintWriter
{
	def info = book.findElement("info")
	assert info
	it.println "=${info.findElement("title").text}="
	it.println "==${info.findElement("subtitle").text}=="
	it.println "===${info.findElement("author").findElement("personname").text}==="
	it.println "----"
	
	def chapters = book.children
	for( chapter in chapters )
	{
		if( chapter.name == "chapter" )
		{
			it.println "=${chapter.findElement("title").text}="
			chapterorsection( it, chapter, 0 )
		}
		else
			assert chapter.name == "info" : "Unexpected child of book: " + chapter.name
	}
}

def chapterorsection( out, node, int sectiondepth )
{
	for( child in node.children )
	{
		if( child.name == "section" )
		{
			out.println "==${titleToWiki(child.findElement("title"))}=="
			chapterorsection( out, child, sectiondepth + 1 )
		}
		else if( child.name == "para" )
		{
			para( out, child )
		}
		else if( child.name == "itemizedlist" )
		{
			out.println "itemizedlist"
		}
		else if( child.name == "example" )
		{
			out.println "example"
		}
		else if( child.name == "screen" )
		{
			out.println "screen"
		}
		else if( child.name == "table" )
		{
			out.println "table"
		}
		else if( child.name == "section" )
		{
			out.println "section"
		}
		else if( child.name == "programlisting" )
		{
			out.println "programlisting"
		}
		else if( child.name == "note" )
		{
			out.println "note"
		}
		else
			assert child.name == "title" : "Unexpected child ${child.name} of parent ${node.name}"
	}
}

def para( out, element )
{
	for( child in element.children )
	{
		if( child instanceof String )
			out.print child
		else
		{
			assert child instanceof Element
			if( child.name == "code" || child.name == "programlisting" || child.name == "computeroutput" )
			{
				out.print "{{{${child.text}}}}"
			}
			else if( child.name == "xref" )
				out.print "TODO: XREF"
			else
				assert false : "got ${child.name}"
		}
	}
	out.println()
}

def titleToWiki( def element )
{
	def result = new StringBuilder()
	for( child in element.children )
	{
		if( child instanceof String )
			result << child
		else
		{
			assert child instanceof Element
			assert child.name == "code"
			result << "{{{"
			result << child.text
			result << "}}}"
		}
	}
	return result
}<commit_msg>Working on Docbook to Google Wiki transformer.<commit_after>/*--
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.stream.*
import solidbase.xml.elements.*

def reader = XMLInputFactory.newInstance().createXMLStreamReader( new InputStreamReader( new FileInputStream( "doc/manual.xml" ), "UTF-8" ) )
assert reader.hasNext()
reader.next()
assert reader.hasNext()
reader.next()

def book = StaxNodeReader.readNode( reader )
assert book.name == "book"

new File( "../solidbase-wiki/UsersManual.wiki" ).withPrintWriter
{
	def info = book.findElement("info")
	assert info
	it.println "=${info.findElement("title").text}="
	it.println "==${info.findElement("subtitle").text}=="
	it.println "===${info.findElement("author").findElement("personname").text}==="
	it.println "----"
	
	def chapters = book.children
	def chapternumber = 1
	for( chapter in chapters )
	{
		if( chapter.name == "chapter" )
		{
			it.println "=Chapter ${chapternumber++}. ${chapter.findElement("title").text}="
			chapterorsection( it, chapter, 0 )
		}
		else
			assert chapter.name == "info" : "Unexpected child of book: " + chapter.name
	}
}

def chapterorsection( out, node, int sectiondepth )
{
	for( child in node.children )
	{
		if( child.name == "section" )
		{
			out.println "==${titleToWiki(child.findElement("title"))}=="
			chapterorsection( out, child, sectiondepth + 1 )
		}
		else if( child.name == "para" )
		{
			def out2 = new StringWriter()
			para( out2, child )
			out.println "\n" + parastrip( out2.toString() )
		}
		else if( child.name == "itemizedlist" )
		{
			itemizedlist( out, child )
		}
		else if( child.name == "example" )
		{
			out.println "example"
		}
		else if( child.name == "screen" )
		{
			out.println "screen"
		}
		else if( child.name == "table" )
		{
			out.println "table"
		}
		else if( child.name == "section" )
		{
			out.println "section"
		}
		else if( child.name == "programlisting" )
		{
			out.println "programlisting"
		}
		else if( child.name == "note" )
		{
			out.println "note"
		}
		else
			assert child.name == "title" : "Unexpected child ${child.name} of parent ${node.name}"
	}
}

def para( out, element )
{
	for( child in element.children )
	{
		if( child instanceof String )
			out.print child
		else
		{
			assert child instanceof Element
			if( child.name == "code" )
				out.print "{{{${child.text}}}}"
			else if( child.name == "programlisting" || child.name == "computeroutput" )
				out.print "{{{\n${child.text}\n}}}"
			else if( child.name == "xref" )
				out.print "TODO: XREF"
			else if( child.name == "note" )
				out.print "TODO: NOTE"
			else
				assert false : "got ${child.name}"
		}
	}
	out.println()
}

def titleToWiki( def element )
{
	def result = new StringBuilder()
	for( child in element.children )
	{
		if( child instanceof String )
			result << child
		else
		{
			assert child instanceof Element
			assert child.name == "code"
			result << "{{{"
			result << child.text
			result << "}}}"
		}
	}
	return result
}

def itemizedlist( out, element )
{
	for( child in element.children )
	{
		assert child instanceof Element
		if( child.name == "listitem" )
		{
			for( child2 in child.children )
			{
				if( child2.name == "para" )
				{
					def out2 = new StringWriter()
					para( out2, child2 )
					out.println " * " + parabr( out2.toString() )
				}
				else if( child2.name == "programlisting" )
					out.println " * " + programlistingbr( child2 )
				else
					assert fail : "Got ${child2.name}"
			}
		}
		else
			assert child.name == "title" : "got ${child.name}" // TODO title
	}
}

def parabr( para )
{
	para = para.replaceAll( "^\\s+", "" )
	para = para.replaceAll( '\\s+$', "" )
	return para.replaceAll( "\\n", "<br/>" )
}

def parastrip( para )
{
	para = para.replaceAll( "^\\s+", "" )
	para = para.replaceAll( '\\s+$', "" )
	return para.replaceAll( '\\s{2,}', " " )
}

def programlistingbr( element )
{
	def text = element.text
	return "{{{" + text.replaceAll( "\n", "}}}<br/>{{{" ) + "}}}"
}
{'fraction_non_alphanumeric': 0.15716308405200155, 'fraction_numerical': 0.004633801003990218, 'mean_word_length': 3.2715777899945024, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1635014', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2589, 'n_words': 720}