From: <pau...@us...> - 2012-09-30 05:24:07
|
Revision: 7518 http://docutils.svn.sourceforge.net/docutils/?rev=7518&view=rev Author: paultremblay Date: 2012-09-30 05:24:00 +0000 (Sun, 30 Sep 2012) Log Message: ----------- Adding tests for the new script. Also, what appeared to be a bug in root.xsl was not a bug, and so I am taking out that code. Modified Paths: -------------- trunk/sandbox/docbook/scripts/docutils_to_xml.py trunk/sandbox/docbook/xsl/root.xsl Added Paths: ----------- trunk/sandbox/docbook/scripts/validate_docbook.py trunk/sandbox/docbook/test/ trunk/sandbox/docbook/test/data/ trunk/sandbox/docbook/test/data/invalid_docbook.xml trunk/sandbox/docbook/test/data/math.rst trunk/sandbox/docbook/test/data/sampler.rst trunk/sandbox/docbook/test/data/simple.rst trunk/sandbox/docbook/test/data/simple_docbook.xml trunk/sandbox/docbook/test/data/simple_docbook_res.xml trunk/sandbox/docbook/test/data/simple_raw.xml trunk/sandbox/docbook/test/data/utf16.rst trunk/sandbox/docbook/test/data/utf8.rst trunk/sandbox/docbook/test/out trunk/sandbox/docbook/test/test_all.py Modified: trunk/sandbox/docbook/scripts/docutils_to_xml.py =================================================================== --- trunk/sandbox/docbook/scripts/docutils_to_xml.py 2012-09-29 22:52:50 UTC (rev 7517) +++ trunk/sandbox/docbook/scripts/docutils_to_xml.py 2012-09-30 05:24:00 UTC (rev 7518) @@ -2,6 +2,7 @@ import xml.etree.cElementTree as etree import asciitomathml.asciitomathml from xml.etree.ElementTree import Element, tostring +import validate_docbook try: import locale @@ -18,9 +19,14 @@ """ - def __init__(self): + def __init__(self, in_file, in_encoding='utf8', to_docbook=True, + validate_docbook = True, convert_to_fo = True): self.path_id = '__rst__' self._transform_num = 0 + self.in_file = in_file + self.in_encoding = in_encoding + self.validate_docbook = validate_docbook + self.convert_to_fo = convert_to_fo self.make_logging() def make_logging(self, ch_level=logging.ERROR, fh_level=logging.INFO): @@ -88,6 +94,8 @@ return '{0}{1}docbook.xml'.format(filename, self.path_id ) elif the_type == 'rst': return '{0}{1}raw.xml'.format(filename, self.path_id ) + elif the_type == 'fo': + return '{0}{1}docbook.fo'.format(filename, self.path_id ) def to_docbook(self, raw_path, xsl_files = []): doc_home = os.environ.get('DOCBOOK_XSL') @@ -113,7 +121,23 @@ raise NoRunException('Cannot do xsl') in_files.append(out_file) counter += 1 + return in_files[-1] + def to_fo(self, docbook_path, xsl_file = None): + doc_home = os.environ.get('DOCBOOK_OFF') + if xsl_file == None: + xsl_file = os.path.join(doc_home, 'fo', 'docbook.xsl') + if not os.path.isfile(xsl_file): + raise IOError('cannot find "{0}'.format(xsl_file)) + out_file = self._make_temp(the_type='fo') + command_list = ['xsltproc', '--nonet', '--novalid', '--output', out_file, xsl_file, docbook_path] + self.logger.debug(command_list) + exit_status = subprocess.call(command_list) + if exit_status: + raise NoRunException('Cannot do xsl') + return out_file + + def clean(self, the_dir): pattern = os.path.join(the_dir, '*{0}docbook.xml'.format(self.path_id)) print(pattern) @@ -127,24 +151,21 @@ os.remove(f) - def convert(self, in_file, in_encoding='utf8', to_docbook=True): - self.in_file = in_file - xml_string = self.rst_to_xml(in_file=in_file, in_encoding=in_encoding) + def convert(self): + xml_string = self.rst_to_xml(in_file=self.in_file, in_encoding=self.in_encoding) xml_string = self.insert_math_elements(xml_string) raw_path = self._make_temp(the_type = 'rst') with open(raw_path, 'w') as write_obj: write_obj.write(xml_string) - self.to_docbook(raw_path) - doc_home = os.environ.get('DOCBOOK_XSL') - x_main = os.path.join(doc_home, 'docutils_to_docbook.xsl') - xsl_t = '/Users/cejohnsonlouisville/Dropbox/programming/xsl/examples/complete_copy.xsl' - self.to_docbook(raw_path, xsl_files = [xsl_t, x_main]) + docbook_file = self.to_docbook(raw_path) + if self.validate_docbook: + valid_obj = validate_docbook.ValidateDocbook() + valid = valid_obj.is_valid(in_files = docbook_file) + if self.convert_to_fo: + self.to_fo(docbook_file) if __name__ == '__main__': - to_xml_obj = ToXml() - to_xml_obj.make_logging(ch_level = logging.DEBUG) - t = to_xml_obj.convert(in_file = 'test/data/simple.rst') - to_xml_obj.clean(the_dir = 'test/data') - # to_xml_obj.convert(in_file = 'temp2.rst', in_encoding='utf16') + to_xml_obj = ToXml(sys.argv[1]) + to_xml_obj.convert() Added: trunk/sandbox/docbook/scripts/validate_docbook.py =================================================================== --- trunk/sandbox/docbook/scripts/validate_docbook.py (rev 0) +++ trunk/sandbox/docbook/scripts/validate_docbook.py 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,36 @@ +import sys, os, subprocess, glob + +class ValidateDocbook(): + + def __init__(self): + jing_dir = os.environ.get('JING_DIR') + if jing_dir == None: + raise IOError('You need to set the variable "JING_DIR"') + jing_jar = os.path.join(jing_dir, 'bin', 'jing.jar') + if not os.path.isfile(jing_jar): + raise IOError('You need download the jing.jar') + self.jing_jar = jing_jar + valid_home = os.environ.get('VALIDATE_HOME') + if valid_home == None: + raise IOError('You need to set the variable "VALIDATE_HOME"') + docbook_rng = os.path.join(valid_home, 'relax', 'docbook.rng') + if not os.path.isfile(docbook_rng): + raise IOError('You need download docbook.rng') + self.docbook_rng = docbook_rng + + def is_valid(self, in_files): + command_list = ['java', '-jar', self.jing_jar, self.docbook_rng] + if isinstance(in_files, list): + for f in in_files: + command_list.append(f) + else: + command_list.append(in_files) + exit_status = subprocess.call(command_list) + if exit_status: + return False + return True + +if __name__ == '__main__': + in_files = sys.argv[1:] + valid_obj = ValidateDocbook() + valid = valid_obj.is_valid(in_files = in_files) Added: trunk/sandbox/docbook/test/data/invalid_docbook.xml =================================================================== --- trunk/sandbox/docbook/test/data/invalid_docbook.xml (rev 0) +++ trunk/sandbox/docbook/test/data/invalid_docbook.xml 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<article xmlns="http://docbook.org/ns/docbook"> + <info> + <title>Executive Report</title> + <author> + <personname>Paul Tremblay</personname> + </author> + </info> + <bogus>just a line</bogus> +</article> Added: trunk/sandbox/docbook/test/data/math.rst =================================================================== --- trunk/sandbox/docbook/test/data/math.rst (rev 0) +++ trunk/sandbox/docbook/test/data/math.rst 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,5 @@ +Document tests math directive. + +.. math:: + + quad quad x = (-b +- sqrt(b^2-4ac))/(2a) Added: trunk/sandbox/docbook/test/data/sampler.rst =================================================================== --- trunk/sandbox/docbook/test/data/sampler.rst (rev 0) +++ trunk/sandbox/docbook/test/data/sampler.rst 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,78 @@ +The rst2pdf Sampler +=================== + +-------------------- +Or, what does it do? +-------------------- + +.. footer:: Page ###Page### + +.. header:: ###Title### + + +.. raw:: pdf + + PageBreak twoColumn + +This document tries to show you some of the nifty things rst2pdf can do. Each one of these should be +explained in The Friendly Manual :sup:`TM` For example, you are now +reading this in a two-column layout. However, the previous page had a one-column layout. + +That's because rst2pdf lets you change the page layout as many times as you want, and define +layouts almost arbitrarily complex (as long as you like rectangular frames full of text, that is). + +Did you notice there is a hyphen in the previous paragraph? And that it's aligned *justified*? + +On the top and bottom of this page (but not of the previous one), you can see a header and a footer +showing you the current page and document name. They could also show a section name and number, but +this document is too short for that. + +Let's go back to one-column now, but first some filler, so you can see the pretty columns. + +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse pede. Nam auctor. Donec ac urna. Quisque tempus, dui sit amet cursus euismod, leo arcu ullamcorper ligula, a elementum elit augue eu ipsum. Donec sem. Aliquam adipiscing nunc ut ante. Praesent consectetuer lacinia nulla. Pellentesque ut augue nec ante gravida vestibulum. Nunc dignissim odio ut elit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse malesuada porta sapien. Suspendisse congue. Morbi eget nulla at purus auctor molestie. Nulla nec orci. Duis condimentum luctus sem. Etiam elementum, turpis non blandit molestie, magna felis faucibus risus, eu ultrices risus lacus at ante. Nullam sed dui nec eros iaculis facilisis. + +Morbi massa. Pellentesque metus sem, tincidunt at, hendrerit et, faucibus nec, arcu. Aenean non arcu. Sed enim odio, adipiscing at, pretium ac, porttitor ac, enim. Vestibulum sollicitudin porttitor leo. Quisque ut augue sed magna sagittis aliquam. Vestibulum lobortis. Aenean at sem a risus molestie cursus. Fusce commodo pharetra orci. Pellentesque eleifend. Sed suscipit, erat sed vestibulum feugiat, eros purus pretium quam, ut molestie pede odio sit amet ipsum. Sed eu pede. Phasellus molestie. Cras nec nulla et diam lacinia viverra. Duis libero. Aliquam tempor ligula quis leo. + +Nullam ac sem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti. Nunc a libero vitae enim cursus elementum. Nullam sit amet pede eget sapien mattis dapibus. Suspendisse nec mi eu elit pharetra ultricies. Proin mollis mattis metus. Duis viverra, tortor in suscipit imperdiet, lorem sapien dapibus mi, vehicula ultricies dolor dolor ut libero. Sed dapibus, arcu vitae eleifend pharetra, nulla nibh dignissim quam, vitae lacinia leo ipsum ut turpis. Nulla justo justo, pretium blandit, mollis ac, accumsan ac, massa. Quisque lacinia quam. Cras molestie elit eu lorem. Donec vitae lorem. Vivamus iaculis, ante ullamcorper dignissim posuere, tortor sem vestibulum nunc, vel pulvinar nisl quam sit amet erat. Cras pulvinar neque eu mi. Maecenas hendrerit dapibus elit. + +.. raw:: pdf + + PageBreak oneColumn + +One of the things I am pretty proud of in rst2pdf is our sidebars. ReST has a sidebar directive that +lets you go "outside the flow of text" for the document. Like this: + +.. sidebar:: Outside the flow + + This sidebar is outside the flow of the text. + +And now we are back in the boring old text flow. You can make arbitrary fragments of code go "float" using the +sidebar *class*, too. This doesn't yet work for code block, but it does work for anything else. +There is a bug about starting a sidebar while we are *besides* a sidebar, so I will jump to the next page now. + +.. raw:: pdf + + PageBreak + +.. class:: sidebar + +This is **not** a sidebar. Really. It just looks like one. If it were a sidebar it would have a title! + +And back to the old text flow. + +Of course, since the authors are programmers, we want our code to look pretty: + +.. code-block:: python + + print ("Hello world") + +.. class:: palatino + +You can also embed fonts easily. This paragraph is in font Palatino, which is not a standard PDF font. Or rather Palladio, because Palatino costs money. True Type and Type1 fonts are supported. There are **thousands** of free fonts. + +.. role:: redpalatino + +You can even switch fonts and colors :redpalatino:`in the middle` of the text. + +More things will be added to this sampler as time allows it. + Added: trunk/sandbox/docbook/test/data/simple.rst =================================================================== --- trunk/sandbox/docbook/test/data/simple.rst (rev 0) +++ trunk/sandbox/docbook/test/data/simple.rst 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,41 @@ +MARLEY was dead: to begin with. There is no doubt +whatever about that. The register of his burial was +signed by the clergyman, the clerk, the undertaker, +and the chief mourner. Scrooge signed it: and +Scrooge's name was good upon 'Change, for anything he +chose to put his hand to. Old Marley was as dead as a +door-nail. + +Mind! I don't mean to say that I know, of my +own knowledge, what there is particularly dead about +a door-nail. I might have been inclined, myself, to +regard a coffin-nail as the deadest piece of ironmongery +in the trade. But the wisdom of our ancestors +is in the simile; and my unhallowed hands +shall not disturb it, or the Country's done for. You +will therefore permit me to repeat, emphatically, that +Marley was as dead as a door-nail. + +Scrooge knew he was dead? Of course he did. +How could it be otherwise? Scrooge and he were +partners for I don't know how many years. Scrooge +was his sole executor, his sole administrator, his sole +assign, his sole residuary legatee, his sole friend, and +sole mourner. And even Scrooge was not so dreadfully +cut up by the sad event, but that he was an excellent +man of business on the very day of the funeral, +and solemnised it with an undoubted bargain. + +The mention of Marley's funeral brings me back to +the point I started from. There is no doubt that Marley +was dead. This must be distinctly understood, or +nothing wonderful can come of the story I am going +to relate. If we were not perfectly convinced that +Hamlet's Father died before the play began, there +would be nothing more remarkable in his taking a +stroll at night, in an easterly wind, upon his own ramparts, +than there would be in any other middle-aged +gentleman rashly turning out after dark in a breezy +spot--say Saint Paul's Churchyard for instance-- +literally to astonish his son's weak mind. + Added: trunk/sandbox/docbook/test/data/simple_docbook.xml =================================================================== --- trunk/sandbox/docbook/test/data/simple_docbook.xml (rev 0) +++ trunk/sandbox/docbook/test/data/simple_docbook.xml 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<article xmlns="http://docbook.org/ns/docbook"> + <info> + <title>Executive Report</title> + <author> + <personname>Paul Tremblay</personname> + </author> + </info> + <para>just a line</para> +</article> Added: trunk/sandbox/docbook/test/data/simple_docbook_res.xml =================================================================== --- trunk/sandbox/docbook/test/data/simple_docbook_res.xml (rev 0) +++ trunk/sandbox/docbook/test/data/simple_docbook_res.xml 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<d:article xmlns:d="http://docbook.org/ns/docbook"><d:info><d:title>test/data/simple.rst</d:title></d:info><d:para>MARLEY was dead: to begin with. There is no doubt +whatever about that. The register of his burial was +signed by the clergyman, the clerk, the undertaker, +and the chief mourner. Scrooge signed it: and +Scrooge's name was good upon 'Change, for anything he +chose to put his hand to. Old Marley was as dead as a +door-nail.</d:para><d:para>Mind! I don't mean to say that I know, of my +own knowledge, what there is particularly dead about +a door-nail. I might have been inclined, myself, to +regard a coffin-nail as the deadest piece of ironmongery +in the trade. But the wisdom of our ancestors +is in the simile; and my unhallowed hands +shall not disturb it, or the Country's done for. You +will therefore permit me to repeat, emphatically, that +Marley was as dead as a door-nail.</d:para><d:para>Scrooge knew he was dead? Of course he did. +How could it be otherwise? Scrooge and he were +partners for I don't know how many years. Scrooge +was his sole executor, his sole administrator, his sole +assign, his sole residuary legatee, his sole friend, and +sole mourner. And even Scrooge was not so dreadfully +cut up by the sad event, but that he was an excellent +man of business on the very day of the funeral, +and solemnised it with an undoubted bargain.</d:para><d:para>The mention of Marley's funeral brings me back to +the point I started from. There is no doubt that Marley +was dead. This must be distinctly understood, or +nothing wonderful can come of the story I am going +to relate. If we were not perfectly convinced that +Hamlet's Father died before the play began, there +would be nothing more remarkable in his taking a +stroll at night, in an easterly wind, upon his own ramparts, +than there would be in any other middle-aged +gentleman rashly turning out after dark in a breezy +spot--say Saint Paul's Churchyard for instance-- +literally to astonish his son's weak mind.</d:para></d:article> + Added: trunk/sandbox/docbook/test/data/simple_raw.xml =================================================================== --- trunk/sandbox/docbook/test/data/simple_raw.xml (rev 0) +++ trunk/sandbox/docbook/test/data/simple_raw.xml 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,35 @@ +<?xml version='1.0' encoding='ASCII'?> +<document source="test/data/simple.rst"><paragraph>MARLEY was dead: to begin with. There is no doubt +whatever about that. The register of his burial was +signed by the clergyman, the clerk, the undertaker, +and the chief mourner. Scrooge signed it: and +Scrooge's name was good upon 'Change, for anything he +chose to put his hand to. Old Marley was as dead as a +door-nail.</paragraph><paragraph>Mind! I don't mean to say that I know, of my +own knowledge, what there is particularly dead about +a door-nail. I might have been inclined, myself, to +regard a coffin-nail as the deadest piece of ironmongery +in the trade. But the wisdom of our ancestors +is in the simile; and my unhallowed hands +shall not disturb it, or the Country's done for. You +will therefore permit me to repeat, emphatically, that +Marley was as dead as a door-nail.</paragraph><paragraph>Scrooge knew he was dead? Of course he did. +How could it be otherwise? Scrooge and he were +partners for I don't know how many years. Scrooge +was his sole executor, his sole administrator, his sole +assign, his sole residuary legatee, his sole friend, and +sole mourner. And even Scrooge was not so dreadfully +cut up by the sad event, but that he was an excellent +man of business on the very day of the funeral, +and solemnised it with an undoubted bargain.</paragraph><paragraph>The mention of Marley's funeral brings me back to +the point I started from. There is no doubt that Marley +was dead. This must be distinctly understood, or +nothing wonderful can come of the story I am going +to relate. If we were not perfectly convinced that +Hamlet's Father died before the play began, there +would be nothing more remarkable in his taking a +stroll at night, in an easterly wind, upon his own ramparts, +than there would be in any other middle-aged +gentleman rashly turning out after dark in a breezy +spot--say Saint Paul's Churchyard for instance-- +literally to astonish his son's weak mind.</paragraph></document> \ No newline at end of file Added: trunk/sandbox/docbook/test/data/utf16.rst =================================================================== (Binary files differ) Property changes on: trunk/sandbox/docbook/test/data/utf16.rst ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/sandbox/docbook/test/data/utf8.rst =================================================================== --- trunk/sandbox/docbook/test/data/utf8.rst (rev 0) +++ trunk/sandbox/docbook/test/data/utf8.rst 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1 @@ +LATIN CAPITAL LETTER O WITH DIAERESIS: Ö Added: trunk/sandbox/docbook/test/out =================================================================== --- trunk/sandbox/docbook/test/out (rev 0) +++ trunk/sandbox/docbook/test/out 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8"?> +<d:article xmlns:d="http://docbook.org/ns/docbook"><d:info><d:title>test/data/simple.rst</d:title></d:info><d:para>MARLEY was dead: to begin with. There is no doubt +whatever about that. The register of his burial was +signed by the clergyman, the clerk, the undertaker, +and the chief mourner. Scrooge signed it: and +Scrooge's name was good upon 'Change, for anything he +chose to put his hand to. Old Marley was as dead as a +door-nail.</d:para><d:para>Mind! I don't mean to say that I know, of my +own knowledge, what there is particularly dead about +a door-nail. I might have been inclined, myself, to +regard a coffin-nail as the deadest piece of ironmongery +in the trade. But the wisdom of our ancestors +is in the simile; and my unhallowed hands +shall not disturb it, or the Country's done for. You +will therefore permit me to repeat, emphatically, that +Marley was as dead as a door-nail.</d:para><d:para>Scrooge knew he was dead? Of course he did. +How could it be otherwise? Scrooge and he were +partners for I don't know how many years. Scrooge +was his sole executor, his sole administrator, his sole +assign, his sole residuary legatee, his sole friend, and +sole mourner. And even Scrooge was not so dreadfully +cut up by the sad event, but that he was an excellent +man of business on the very day of the funeral, +and solemnised it with an undoubted bargain.</d:para><d:para>The mention of Marley's funeral brings me back to +the point I started from. There is no doubt that Marley +was dead. This must be distinctly understood, or +nothing wonderful can come of the story I am going +to relate. If we were not perfectly convinced that +Hamlet's Father died before the play began, there +would be nothing more remarkable in his taking a +stroll at night, in an easterly wind, upon his own ramparts, +than there would be in any other middle-aged +gentleman rashly turning out after dark in a breezy +spot--say Saint Paul's Churchyard for instance-- +literally to astonish his son's weak mind.</d:para></d:article> + Added: trunk/sandbox/docbook/test/test_all.py =================================================================== --- trunk/sandbox/docbook/test/test_all.py (rev 0) +++ trunk/sandbox/docbook/test/test_all.py 2012-09-30 05:24:00 UTC (rev 7518) @@ -0,0 +1,109 @@ +import unittest, sys, os +cur_dir = os.path.dirname(os.path.abspath(sys.argv[0])) +lib_dir = os.path.abspath(os.path.join(cur_dir, '..', 'scripts')) +sys.path.append(lib_dir) +import docutils_to_xml +import validate_docbook +from xml.dom import minidom + +class TestDocutilsToXml(unittest.TestCase): + + def setUp(self): + pass + + def isEqualXML(self, a, b): + da, db= minidom.parseString(a), minidom.parseString(b) + return self.isEqualElement(da.documentElement, db.documentElement) + + def isEqualElement(self, a, b): + if a.tagName!=b.tagName: + return False + if sorted(a.attributes.items())!=sorted(b.attributes.items()): + return False + if len(a.childNodes)!=len(b.childNodes): + return False + for ac, bc in zip(a.childNodes, b.childNodes): + if ac.nodeType!=bc.nodeType: + return False + if ac.nodeType==ac.TEXT_NODE and ac.data!=bc.data: + return False + if ac.nodeType==ac.ELEMENT_NODE and not self.isEqualElement(ac, bc): + return False + return True + + def _get_file(self, name): + in_file = os.path.join(os.path.dirname(sys.argv[0]), 'data', name) + if not os.path.isfile(in_file): + raise IOError('file "{0}"does not exist'.format(in_file)) + return in_file + + def _file_to_string(self, in_file): + if not os.path.isfile(in_file): + raise IOError('file "{0}"does not exist'.format(in_file)) + with open(in_file, 'r') as read_obj: + lines = ''.join(read_obj.readlines()) + return lines + + def _fix_line(self, line): + return line + + def test_utf8(self): + convert_obj = docutils_to_xml.ToXml(in_file = None) + in_file = self._get_file('utf8.rst') + needed = """<?xml version="1.0" encoding="ascii"?>\n<!-- Generated by Docutils 0.9 -->\n<document source="data/utf8.rst"><paragraph>LATIN CAPITAL LETTER O WITH DIAERESIS: Ö</paragraph></document>""" + return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf8') + return_string = self._fix_line(return_string) + self.assertEqual(return_string, needed) + + def test_utf16(self): + convert_obj = docutils_to_xml.ToXml(in_file = None) + in_file = self._get_file('utf16.rst') + needed = """<?xml version="1.0" encoding="ascii"?>\n<!-- Generated by Docutils 0.9 -->\n<document source="data/utf16.rst"><paragraph>LATIN CAPITAL LETTER O WITH DIAERESIS: Ö</paragraph></document>""" + return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf16') + return_string = self._fix_line(return_string) + self.assertEqual(return_string, needed) + + def test_math_no_conversion(self): + convert_obj = docutils_to_xml.ToXml(in_file = None) + in_file = self._get_file('math.rst') + needed="""<?xml version="1.0" encoding="ascii"?> +<!-- Generated by Docutils 0.9 --> +<document source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve">quad quad x = (-b +- sqrt(b^2-4ac))/(2a)</math_block></document>""" + return_string = convert_obj.rst_to_xml(in_file = in_file, in_encoding='utf8') + return_string = self._fix_line(return_string) + self.assertEqual(return_string, needed) + + def test_math_conversion(self): + convert_obj = docutils_to_xml.ToXml(in_file = None) + in_string = """<?xml version="1.0" encoding="ascii"?> +<!-- Generated by Docutils 0.9 --> +<document source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve">quad quad x = (-b +- sqrt(b^2-4ac))/(2a)</math_block></document>""" + needed = """<?xml version='1.0' encoding='ASCII'?>\n<document xmlns:ns0="http://www.w3.org/1998/Math/MathML" source="data/math.rst"><paragraph>Document tests math directive.</paragraph><math_block xml:space="preserve"><ns0:math><ns0:mstyle displaystyle="true"><ns0:mo>  </ns0:mo><ns0:mo>  </ns0:mo><ns0:mi>x</ns0:mi><ns0:mo>=</ns0:mo><ns0:mfrac><ns0:mrow class="nominator"><ns0:mo>-</ns0:mo><ns0:mi>b</ns0:mi><ns0:mo>±</ns0:mo><ns0:msqrt><ns0:mrow class="radical"><ns0:msup><ns0:mi>b</ns0:mi><ns0:mn>2</ns0:mn></ns0:msup><ns0:mo>-</ns0:mo><ns0:mn>4</ns0:mn><ns0:mi>a</ns0:mi><ns0:mi>c</ns0:mi></ns0:mrow></ns0:msqrt></ns0:mrow><ns0:mrow class="denominator"><ns0:mn>2</ns0:mn><ns0:mi>a</ns0:mi></ns0:mrow></ns0:mfrac></ns0:mstyle></ns0:math></math_block></document>""" + return_string = convert_obj.insert_math_elements(xml_string = in_string) + same = self.isEqualXML(return_string, needed) + self.assertTrue(same) + + def test_to_docbook_simple(self): + in_file = os.path.join('data', 'simple_docbook_res.xml') + convert_obj = docutils_to_xml.ToXml(in_file = in_file ) + raw_path = os.path.join('data', 'simple_raw.xml') + result_file = convert_obj.to_docbook(raw_path = raw_path) + return_string = self._file_to_string(result_file) + needed = self._file_to_string(in_file) + same = self.isEqualXML(return_string, needed) + self.assertTrue(same) + + def test_validate_docbook(self): + valid_obj = validate_docbook.ValidateDocbook() + in_files = os.path.join('data', 'simple_docbook.xml') + valid = valid_obj.is_valid(in_files = in_files) + self.assertTrue(valid) + + def test_validate_invalid_docbook(self): + valid_obj = validate_docbook.ValidateDocbook() + in_files = os.path.join('data', 'invalid_docbook.xml') + valid = valid_obj.is_valid(in_files = in_files) + self.assertFalse(valid) + +if __name__ == '__main__': + unittest.main() Modified: trunk/sandbox/docbook/xsl/root.xsl =================================================================== --- trunk/sandbox/docbook/xsl/root.xsl 2012-09-29 22:52:50 UTC (rev 7517) +++ trunk/sandbox/docbook/xsl/root.xsl 2012-09-30 05:24:00 UTC (rev 7518) @@ -8,19 +8,7 @@ <xsl:when test="$docbook-type = 'book'"> <d:book> <xsl:call-template name="make-info"/> - <xsl:variable name="section-exists"> - <xsl:for-each select="/"> - <xsl:if test="descendant::section">true</xsl:if> - </xsl:for-each> - </xsl:variable> - <xsl:choose> - <xsl:when test="$section-exists != 'true'"> - <xsl:call-template name = "make-section"/> - </xsl:when> - <xsl:otherwise> - <xsl:apply-templates/> - </xsl:otherwise> - </xsl:choose> + <xsl:apply-templates/> <xsl:call-template name="make-appendix"/> </d:book> </xsl:when> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |