From: <pau...@us...> - 2011-10-19 04:48:39
|
Author: paultremblay Date: 2011-10-19 06:48:29 +0200 (Wed, 19 Oct 2011) New Revision: 7189 Added: trunk/sandbox/paultremblay/test_files/substitution.rst Modified: trunk/sandbox/paultremblay/ trunk/sandbox/paultremblay/scripts/docutils_to_fo.sh trunk/sandbox/paultremblay/scripts/rstxml2mathml.py trunk/sandbox/paultremblay/test.sh trunk/sandbox/paultremblay/xsl_fo/body_directives.xsl trunk/sandbox/paultremblay/xsl_fo/body_elements.xsl trunk/sandbox/paultremblay/xsl_fo/error.xsl trunk/sandbox/paultremblay/xsl_fo/footnote.xsl trunk/sandbox/paultremblay/xsl_fo/image_figure.xsl trunk/sandbox/paultremblay/xsl_fo/inline.xsl trunk/sandbox/paultremblay/xsl_fo/root.xsl trunk/sandbox/paultremblay/xsl_fo/toc.xsl Log: Fixing bugs found by running test files from docutils. Property changes on: trunk/sandbox/paultremblay ___________________________________________________________________ Modified: svn:ignore - build test_output svn_config foo_bar temp.fo temp.xml temp.pdf build dist test.xsl MANIFEST + build test_output svn_config foo_bar temp.fo temp.xml temp.pdf build dist test.xsl MANIFEST docutils_test Modified: trunk/sandbox/paultremblay/scripts/docutils_to_fo.sh =================================================================== --- trunk/sandbox/paultremblay/scripts/docutils_to_fo.sh 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/scripts/docutils_to_fo.sh 2011-10-19 04:48:29 UTC (rev 7189) @@ -1,5 +1,6 @@ XSLFO_HOME=/Users/cejohnsonlouisville/Documents/docutils/paultremblay/xsl_fo RSTML=/Users/cejohnsonlouisville/Documents/docutils/paultremblay/scripts/rstxml2mathml.py +FOPCONF=/Users/cejohnsonlouisville/Documents/docutils/paultremblay/fop.conf if [ "$XSLFO_PDF" != "" ]; then PDF='true' else @@ -15,6 +16,7 @@ echo --pdf: create a PDF document echo --valid: validate the FO document echo --strict: quit when a template does not match \(or other error\) + echo --latexml: convert latex in the math element to mathml echo "-s | --stylesheet <stylesheet> : the stylesheet to use" echo "-o | --out: file to output to" } @@ -26,7 +28,8 @@ STYLESHEET= OUT='' STRICT='' -MATHML='true' +LATEXML='false' +ASCIIML='false' while [ $# -gt 0 ] do case "$1" in @@ -36,12 +39,14 @@ --help) Usage;exit 0;; --verbose) VERBOSE='true';; --format) FORMAT='true';; - --test) CLEAN='false';FORMAT='true';VALID='true';PDF='true';TEST='true';STRICT='true';; + --test) CLEAN='false';FORMAT='true';VALID='true';TEST='true';STRICT='true';; --testq) CLEAN='false';VALID='true';FORMAT='true';; --noclean) CLEAN='false';; --pdf) PDF='true';; --valid) VALID='true';; --strict) STRICT='true';; + --latexml) LATEXML='true';; + --asciiml) ASCIIML='true';; --out) shift;OUT=$1;; -o) shift;OUT=$1;; --stylesheet) shift;STYLESHEET=$1;; @@ -94,9 +99,12 @@ FO_FILE=${DIRNAME}/${BASENAME}.fo fi -if [ "$MATHML" == 'true' ]; then +if [ "$LATEXML" == 'true' ]; then rst2xml.py --strip-comments --trim-footnote-reference-space --no-doctype $1\ - | python3 $RSTML > $RAW_XML + | python3 $RSTML --mathml latex > $RAW_XML +elif [ "$ASCIIML" == 'true' ]; then + rst2xml.py --strip-comments --trim-footnote-reference-space --no-doctype $1\ + | python3 $RSTML --mathml ascii > $RAW_XML else rst2xml.py --strip-comments --trim-footnote-reference-space --no-doctype $1 $RAW_XML fi @@ -122,7 +130,7 @@ if [ "$PDF" == 'true' ]; then PDF_FILE=${DIRNAME}/${BASENAME}.pdf - fop -fo $FO_FILE -pdf ${PDF_FILE} + fop -c $FOPCONF -fo $FO_FILE -pdf ${PDF_FILE} fi if [ "$CLEAN" == "true" ]; then Modified: trunk/sandbox/paultremblay/scripts/rstxml2mathml.py =================================================================== --- trunk/sandbox/paultremblay/scripts/rstxml2mathml.py 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/scripts/rstxml2mathml.py 2011-10-19 04:48:29 UTC (rev 7189) @@ -7,35 +7,23 @@ from io import StringIO import asciimathml from xml.etree.ElementTree import Element, tostring +import xml.etree.cElementTree as etree import tempfile, subprocess, os +import docutils.math.latex2mathml -try: - from lxml import etree -except ImportError: - try: - import xml.etree.cElementTree as etree - except ImportError: - try: - import xml.etree.ElementTree as etree - except ImportError: - try: - import cElementTree as etree - except ImportError: - try: - import elementtree.ElementTree as etree - except ImportError: - print("Failed to import ElementTree from any known place") - sys.exit(1) +if sys.version_info < (3,): + sys.stderr.write('Only run with pyton 3\n') + sys.stderr.write('Script now quiting\n') + sys.exit(1) class CopyTree(xml.sax.ContentHandler): - def __init__(self): + def __init__(self, mathml): self.__characters = '' - self.__ascii_math = False - self.__latex_math = False + self.__mathml = mathml self.__ns_dict = {'http://www.w3.org/XML/1998/namespace': "xml"} @@ -44,12 +32,9 @@ def startElementNS(self, name, qname, attrs): + self.__write_text() ns = name[0] el_name = name[1] - if el_name == 'math_block' and attrs.get((None, 'classes')) == 'asciimath': - self.__ascii_math= True - if el_name == 'math_block' and attrs.get((None, 'classes')) == 'latex': - self.__latex_math= True sys.stdout.write('<') if ns: sys.stdout.write('ns1:%s' % el_name) @@ -92,22 +77,24 @@ def endElementNS(self, name, qname): ns = name[0] el_name = name[1] - if el_name == 'math_block' and self.__ascii_math == True: - self.__ascii_math= False + if (el_name == 'math_block' and self.__mathml == 'ascii') or (el_name == 'math' and self.__mathml == 'ascii'): raw_tree = asciimathml.parse(self.__characters)[0] math_tree = Element('math', title="%s" % self.__characters, xmlns="http://www.w3.org/1998/Math/MathML") math_tree.append(raw_tree) string_tree = tostring(math_tree, encoding="utf-8").decode() sys.stdout.write(string_tree) self.__characters = '' - elif el_name == 'math_block' and self.__latex_math == True: - raw_tree = self.__tralics()[0] - math_tree = Element('math', title="%s" % self.__characters, xmlns="http://www.w3.org/1998/Math/MathML") - math_tree.append(raw_tree) - string_tree = tostring(math_tree, encoding="utf-8").decode() - sys.stdout.write(string_tree) - self.__characters = '' - self.__latex_math = False + elif (el_name == 'math_block' and self.__mathml == 'latex') or (el_name == 'math' and self.__mathml == 'latex'): + raw_tree = self.__tralics() + if raw_tree == None: + self.__write_text() + else: + raw_tree = raw_tree[0] + math_tree = Element('math', title="%s" % self.__characters, xmlns="http://www.w3.org/1998/Math/MathML") + math_tree.append(raw_tree) + string_tree = tostring(math_tree, encoding="utf-8").decode() + sys.stdout.write(string_tree) + self.__characters = '' else: self.__write_text() if ns: @@ -117,14 +104,28 @@ else: sys.stdout.write('</%s>' % el_name) + def __python_latex_math(self): + """ + Python code seriously broken + + """ + try: + mathml_tree = docutils.math.latex2mathml.parse_latex_math(self.__characters) + except SyntaxError: + return self.__characters + math_code = ''.join(mathml_tree.xml()) + return math_code + def __tralics(self): num, tex_file = tempfile.mkstemp(suffix='.tex') write_obj = open(tex_file, 'w') + write_obj.write('$') write_obj.write(self.__characters) + write_obj.write('$') write_obj.close() num, bogus_out = tempfile.mkstemp() bogus_out = open(bogus_out, 'w') - p = subprocess.call(['tralics', '-silent', '-utf8output', tex_file], stdout=bogus_out) + p = subprocess.call(['tralics', '-silent', '-utf8output', '-noentnames', tex_file], stdout=bogus_out) bogus_out.close() dir_name = os.path.dirname(tex_file) filename, ext = os.path.splitext(tex_file) @@ -136,6 +137,7 @@ sys.stderr.write('Bug, program now quiting\n') sys.exit(1) tree = etree.ElementTree() + read_obj = open(xml_file, 'r') xml_tree = tree.parse(xml_file) found = None while not found: @@ -143,7 +145,13 @@ if child.tag == '{http://www.w3.org/1998/Math/MathML}math': found = 1 break - xml_tree = xml_tree[0] + try: + xml_tree = xml_tree[0] + except IndexError: + sys.stderr.write('Could not find any latex math\n') + break + if not found: + return None return xml_tree """ line_to_read = 1 @@ -174,7 +182,7 @@ Or, in one pass: rst2xml.py <infile> | python3 rstxml2mathml.py """ parser = argparse.ArgumentParser(description=desc) - # parser.add_argument('--type', nargs=1 ) # much better--demand an arg; the option is still optional + parser.add_argument('--mathml', choices = ['latex', 'ascii'], nargs=1 ) # much better--demand an arg; the option is still optional parser.add_argument('in_file', default = sys.stdin, nargs='?', help = 'the file to input; default is standard in') args = parser.parse_args() @@ -185,6 +193,9 @@ args = self.__parse_args() standard_in = False in_file = args.in_file + mathml = args.mathml + if mathml: + mathml = mathml[0] if isinstance(in_file, io.TextIOWrapper): standard_in = True the_string = sys.stdin.read() @@ -192,7 +203,7 @@ read_obj = StringIO(the_string) else: read_obj = open(in_file, 'r') - the_handle=CopyTree() + the_handle=CopyTree(mathml) parser = xml.sax.make_parser() parser.setFeature(feature_namespaces, 1) parser.setContentHandler(the_handle) Modified: trunk/sandbox/paultremblay/test.sh =================================================================== --- trunk/sandbox/paultremblay/test.sh 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/test.sh 2011-10-19 04:48:29 UTC (rev 7189) @@ -3,8 +3,27 @@ # RST_COMMAND="rst2xml.py --strip-comments --trim-footnote-reference-space" # TEST_COMMAND="docutils_to_fo.sh --noclean --format --valid --pdf --test --strict" +files=`find docutils_test \! -name 'standard.txt' \ + \! -name 'standalone_rst_html4css1.txt'\ + -name '*txt'` +for the_file in $files +do + echo $the_file + DIR=`dirname $the_file` + if [ "$DIR" == "$the_file" ]; then + DIRNAME="." + else + DIRNAME=$DIR + fi + PARENT_DIR=`basename $DIRNAME` + + if [ "$PARENT_DIR" != "custom" ]; then + docutils_to_fo.sh --test $the_file + fi +done + files=`find test_files -name '*rst'` for the_file in $files do @@ -25,3 +44,5 @@ docutils_to_fo.sh --test -s test_files/xsl/endnotes.xsl test_files/custom/endnotes.rst docutils_to_fo.sh --test -s test_files/xsl/footnotes_traditional.xsl test_files/footnotes.rst docutils_to_fo.sh --test -s test_files/xsl/endnotes_traditional.xsl test_files/custom/endnotes.rst + + Added: trunk/sandbox/paultremblay/test_files/substitution.rst =================================================================== --- trunk/sandbox/paultremblay/test_files/substitution.rst (rev 0) +++ trunk/sandbox/paultremblay/test_files/substitution.rst 2011-10-19 04:48:29 UTC (rev 7189) @@ -0,0 +1,6 @@ +An example of substition. + + +I recommend you try |Bergonzi|. + +.. |Bergonzi| replace:: Bergonzi, Joe: *Some Book*. Modified: trunk/sandbox/paultremblay/xsl_fo/body_directives.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/body_directives.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/body_directives.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -303,30 +303,24 @@ <xsl:apply-templates/> </xsl:template> - - <xsl:template match="math_block[not(@classes='asciimath')]"> - <fo:block xsl:use-attribute-sets="literal-block"> - <xsl:apply-templates/> - </fo:block> + <xsl:template match="math_block"> + <xsl:choose> + <xsl:when test="descendant::ml:math"> + <fo:block xsl:use-attribute-sets="mathml-block"> + <fo:instream-foreign-object> + <xsl:copy-of select="ml:math"/> + </fo:instream-foreign-object> + </fo:block> + </xsl:when> + <xsl:otherwise> + <fo:block xsl:use-attribute-sets="literal-block"> + <xsl:apply-templates/> + </fo:block> + </xsl:otherwise> + </xsl:choose> + </xsl:template> + - <xsl:template match="math_block[@classes='asciimath']|math_block[@classes='latex']"> - <xsl:if test="not(ml:math)"> - <xsl:message terminate="yes"> - <xsl:text>You have used the math directive with the classes set to asciimath
</xsl:text> - <xsl:text>But you have no MathML present
</xsl:text> - <xsl:text>It would appear you have not processed the file using rstxml2mathml.py
</xsl:text> - <xsl:text>Stylesheets now quiting.
</xsl:text> - </xsl:message> - </xsl:if> - <fo:block xsl:use-attribute-sets="mathml-block"> - <fo:instream-foreign-object> - <xsl:copy-of select="ml:math"/> - </fo:instream-foreign-object> - </fo:block> - </xsl:template> - - - </xsl:stylesheet> Modified: trunk/sandbox/paultremblay/xsl_fo/body_elements.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/body_elements.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/body_elements.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -191,6 +191,7 @@ </fo:block> </xsl:template> - <xsl:template match="comment"/> + <xsl:template match="comment|substitution_definition"/> + </xsl:stylesheet> Modified: trunk/sandbox/paultremblay/xsl_fo/error.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/error.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/error.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -5,6 +5,8 @@ > <!-- $Id: error.xsl 7131 2011-09-26 19:27:15Z paultremblay $ --> + + <xsl:template name="test-params"> <xsl:if test ="$number-verse != '' and string($number-verse + 1 ) = 'NaN'"> @@ -145,6 +147,31 @@ <xsl:template match="system_message[@type='ERROR']/paragraph| system_message[@type='ERROR']/literal_block" priority="2"/> + <xsl:template match="problematic|system_message|system_message/paragraph"> + <fo:block> + <xsl:apply-templates/> + </fo:block> + </xsl:template> + + <xsl:template match="split-space"> + <xsl:param name="string"/> + <xsl:if test="$string != ''"> + + </xsl:if> + </xsl:template> + + <xsl:template match="raw[@format]"> + <xsl:variable name="msg"> + <xsl:text>Raw HMLT not allowed in an XML document</xsl:text> + </xsl:variable> + <!-- + <xsl:call-template name="quit-message"> + <xsl:with-param name="msg" select="$msg"/> + </xsl:call-template> + --> + </xsl:template> + + <xsl:template name="quit-message"> <xsl:param name="msg"/> <xsl:message terminate="yes"> @@ -170,5 +197,6 @@ </xsl:choose> </xsl:template> + </xsl:stylesheet> Modified: trunk/sandbox/paultremblay/xsl_fo/footnote.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/footnote.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/footnote.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -58,7 +58,11 @@ <xsl:template match="footnote_reference"> - <xsl:apply-templates select="key('footnote', @refid)" mode="footnote"/> + <!--need to check that there are no ancestors in a footnote reference, or + you could get infinite recursion--> + <xsl:if test="not(ancestor::footnote)"> + <xsl:apply-templates select="key('footnote', @refid)" mode="footnote"/> + </xsl:if> </xsl:template> <xsl:template match="footnote" mode="footnote"> Modified: trunk/sandbox/paultremblay/xsl_fo/image_figure.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/image_figure.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/image_figure.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -112,7 +112,7 @@ </xsl:template> <xsl:template match="figure/legend"> - <fo:block role="legend" xsl:use-attribute-sets="figure-legend-block" id="generate-id()"> + <fo:block role="legend" xsl:use-attribute-sets="figure-legend-block" id="{generate-id()}"> <xsl:apply-templates/> </fo:block> </xsl:template> Modified: trunk/sandbox/paultremblay/xsl_fo/inline.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/inline.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/inline.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -1,6 +1,7 @@ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" + xmlns:ml = "http://www.w3.org/1998/Math/MathML" version="1.1"> <!-- $Id: inline.xsl 7131 2011-09-26 19:27:15Z paultremblay $ --> @@ -29,6 +30,18 @@ <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:attribute-set> + <xsl:attribute-set name="subscript"> + <xsl:attribute name="vertical-align">sub</xsl:attribute> + </xsl:attribute-set> + + <xsl:attribute-set name="superscript"> + <!-- + <xsl:atribute name="baseline-shift">super</xsl:atribute> + --> + <xsl:attribute name="vertical-align">super</xsl:attribute> + </xsl:attribute-set> + + <xsl:template match="strong"> <fo:inline xsl:use-attribute-sets="strong-inline"> <xsl:apply-templates/> @@ -41,8 +54,7 @@ </fo:inline> </xsl:template> - <!--internal links--> - <xsl:template match="reference[@refid]"> + <xsl:template name="reference-refid"> <xsl:choose> <xsl:when test="$internal-link-type = 'link'"> <fo:inline> @@ -62,9 +74,24 @@ </fo:inline> </xsl:when> </xsl:choose> + </xsl:template> + <!--internal links--> + <xsl:template match="reference[@refid]"> + <xsl:choose> + <xsl:when test="ancestor::paragraph"> + <xsl:call-template name="reference-refid"/> + </xsl:when> + <xsl:otherwise> + <fo:block> + <xsl:call-template name="reference-refid"/> + </fo:block> + </xsl:otherwise> + </xsl:choose> + </xsl:template> + <!--this template creates clickable links; you may want to give the option to turn this off--> <xsl:template match= "reference[@refuri]"> <fo:basic-link xsl:use-attribute-sets="basic-link-inline" external-destination="url('{@refuri}')"> @@ -91,6 +118,49 @@ <xsl:apply-templates/> </fo:inline> </xsl:template> + + <xsl:template match="inline[@classes]"> + <xsl:variable name="msg"> + <xsl:text>Don't know what to do with inline with classes "</xsl:text> + <xsl:value-of select="@classes"/> + <xsl:text>"</xsl:text> + </xsl:variable> + <xsl:call-template name="error-message"> + <xsl:with-param name="text" select="$msg"/> + </xsl:call-template> + + </xsl:template> + + <xsl:template match="subscript"> + <fo:inline role="subscript" xsl:use-attribute-sets = "subscript"> + <xsl:apply-templates/> + </fo:inline> + </xsl:template> + + <xsl:template match="superscript"> + <fo:inline role="superscript" xsl:use-attribute-sets = "superscript"> + <xsl:apply-templates/> + </fo:inline> + </xsl:template> + + <!--Change this if there is MathML--> + <xsl:template match="paragraph/math"> + <xsl:choose> + <xsl:when test="descendant::ml:math"> + <fo:inline > + <fo:instream-foreign-object> + <xsl:copy-of select="ml:math"/> + </fo:instream-foreign-object> + </fo:inline> + </xsl:when> + <xsl:otherwise> + <fo:inline xsl:use-attribute-sets="literal-inline"> + <xsl:apply-templates/> + </fo:inline> + </xsl:otherwise> + </xsl:choose> + </xsl:template> + Modified: trunk/sandbox/paultremblay/xsl_fo/root.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/root.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/root.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -58,6 +58,7 @@ <xsl:template match="/"> <xsl:element name="fo:root"> + <xsl:attribute name="font-family">STIX</xsl:attribute> <xsl:call-template name="make-pages"/> <xsl:apply-templates/> </xsl:element> Modified: trunk/sandbox/paultremblay/xsl_fo/toc.xsl =================================================================== --- trunk/sandbox/paultremblay/xsl_fo/toc.xsl 2011-10-18 04:40:00 UTC (rev 7188) +++ trunk/sandbox/paultremblay/xsl_fo/toc.xsl 2011-10-19 04:48:29 UTC (rev 7189) @@ -38,7 +38,7 @@ <!--END OF ATTRIBUTE SETS--> - <xsl:template match="topic[@classes='contents']"> + <xsl:template match="topic[@classes='contents']|topic[@classes='contents local']"> <xsl:apply-templates/> </xsl:template> @@ -170,7 +170,7 @@ </fo:block> </xsl:template> - <xsl:template match="generated[@classes='sectnum'][ancestor::topic[@classes='contents']]"> + <xsl:template match="generated[@classes='sectnum'][ancestor::topic[@classes='contents']]|generated[@classes='sectnum'][ancestor::topic[@classes='contents local']]"> <!--not sure about this no-break space--> <xsl:variable name="num" select="concat(substring-before(., ' '), '.')"/> <xsl:call-template name="format-number"> |