From 6b3b491e254afaa3b6e4891bef30c5b304ecd83b Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 28 Jul 2009 18:09:30 +1000 Subject: [PATCH] Use transforms in info_translator tests so we can test title and subtitle hoisting. --- samples/subsections.rst | 32 +++++++++++++++------- texinfo/info_translator.py | 4 +++ texinfo/info_writer.py | 2 +- texinfo/rst_test_utils.py | 4 +-- texinfo/test_info_translator.py | 59 ++++++++++++----------------------------- texinfo/test_info_writer.py | 7 ++++- 6 files changed, 52 insertions(+), 56 deletions(-) diff --git a/samples/subsections.rst b/samples/subsections.rst index 7ab4056..41c7ecf 100644 --- a/samples/subsections.rst +++ b/samples/subsections.rst @@ -1,14 +1,26 @@ -Chapter 1 Title -=============== +====== +Title! +====== -Section 1.1 Title ------------------ +------------ +Or, Subtitle +------------ -Subsection 1.1.1 Title -~~~~~~~~~~~~~~~~~~~~~~ +Chapter 1 +========= -Section 1.2 Title ------------------ +Section 1 +--------- + +Subsection 1 +~~~~~~~~~~~~ + +Something else +^^^^^^^^^^^^^^ + +Subsection 2 +~~~~~~~~~~~~ + +Section 2 +--------- -Chapter 2 Title -=============== diff --git a/texinfo/info_translator.py b/texinfo/info_translator.py index 09d1b62..55b4e33 100644 --- a/texinfo/info_translator.py +++ b/texinfo/info_translator.py @@ -58,6 +58,10 @@ class InfoTranslator(nodes.NodeVisitor): f(node.astext()) raise nodes.SkipNode + def visit_subtitle(self, node): + self.body.append('@majorheading %s' % node.astext()) + raise nodes.SkipNode + def visit_paragraph(self, node): self.body.append(node.astext()) raise nodes.SkipNode diff --git a/texinfo/info_writer.py b/texinfo/info_writer.py index 208f980..0646bd6 100644 --- a/texinfo/info_writer.py +++ b/texinfo/info_writer.py @@ -9,7 +9,7 @@ class InfoWriter(writers.Writer): self.translator_class = info_translator.InfoTranslator def translate(self): - # print "translate:%s" % self.document.asdom().childNodes[0].toprettyxml(' ','\n') + #print "writer.translate(), document=\n%s" % self.document.asdom().childNodes[0].toprettyxml(' ','\n') self.visitor = visitor = self.translator_class(self.document) self.document.walkabout(visitor) self.output = self.visitor.astext() diff --git a/texinfo/rst_test_utils.py b/texinfo/rst_test_utils.py index 26c7ead..a039227 100644 --- a/texinfo/rst_test_utils.py +++ b/texinfo/rst_test_utils.py @@ -8,7 +8,7 @@ def basic_test_document(text=''): parser_name = 'restructuredtext' reader_class = readers.get_reader_class(reader_name) - reader = reader_class(None, parser_name) + reader = reader_class(parser_name=parser_name) parser = reader.parser options = frontend.OptionParser(components=(parser,reader)) @@ -17,7 +17,7 @@ def basic_test_document(text=''): parser.parse(text, document) - #print 'document:\n%s' % document.asdom().childNodes[0].toprettyxml(' ','\n') + #print 'parser.parse(), document=\n%s' % document.asdom().childNodes[0].toprettyxml(' ','\n') return document class TestCase(unittest.TestCase): diff --git a/texinfo/test_info_translator.py b/texinfo/test_info_translator.py index 2e25d30..28039cc 100644 --- a/texinfo/test_info_translator.py +++ b/texinfo/test_info_translator.py @@ -1,12 +1,17 @@ import rst_test_utils from info_translator import InfoTranslator +from docutils.transforms import frontmatter class T(rst_test_utils.TestCase): - def given_input(self, input): + def given_input(self, input, transforms = []): super(T, self).given_input(input) self.visitor = InfoTranslator(self.document) - self.visitor.section_level = -1 #HACK: core.publish_* start at 0, but that makes these tests fail - maybe missing some attribute set on the new document? + + for t in transforms: + self.document.transformer.add_transform(t) + self.document.transformer.apply_transforms() + self.document.walkabout(self.visitor) def setUp(self): @@ -19,44 +24,25 @@ class T(rst_test_utils.TestCase): self.given_input(''' Hello, world! ================ -''') +''', transforms = [frontmatter.DocTitle]) self.assertEqual(['@node Top', '@top Hello, world!'], self.visitor.body) - def test_chapter(self): + def test_subtitle(self): self.given_input(''' ====== Title! ====== -Chapter 1 -========= -''') - self.assertEqual(['@node Top', '@top Title!', '@chapter Chapter 1'], - self.visitor.body) - - def test_section(self): - self.given_input(''' -====== -Title! -====== - -Chapter 1 -========= - -Section 1 ---------- -''') +-------- +Subtitle +-------- +''', transforms = [frontmatter.DocTitle]) self.assertEqual(['@node Top', '@top Title!', - '@chapter Chapter 1', - '@section Section 1'], + '@majorheading Subtitle'], self.visitor.body) def test_subsection(self): self.given_input(''' -====== -Title! -====== - Chapter 1 ========= @@ -66,18 +52,13 @@ Section 1 Subsection 1 ~~~~~~~~~~~~ ''') - self.assertEqual(['@node Top', '@top Title!', - '@chapter Chapter 1', + self.assertEqual(['@chapter Chapter 1', '@section Section 1', '@subsection Subsection 1'], self.visitor.body) def test_section_layering(self): self.given_input(""" -=================== -A Plan for the Moon -=================== - The Problem =========== @@ -93,8 +74,7 @@ Cows A Modest Solution ================= """) - self.assertEqual(['@node Top', '@top A Plan for the Moon', - '@chapter The Problem', + self.assertEqual(['@chapter The Problem', '@section Cheese', '@subsection Lunar mold', '@section Cows', @@ -103,10 +83,6 @@ A Modest Solution def test_beyond_subsections(self): self.given_input(''' -====== -Title! -====== - Chapter 1 ========= @@ -126,8 +102,7 @@ Section 2 --------- ''') - self.assertEqual(['@node Top', '@top Title!', - '@chapter Chapter 1', + self.assertEqual(['@chapter Chapter 1', '@section Section 1', '@subsection Subsection 1', '@subsection Something else', diff --git a/texinfo/test_info_writer.py b/texinfo/test_info_writer.py index ca4cbb2..c95a5e2 100644 --- a/texinfo/test_info_writer.py +++ b/texinfo/test_info_writer.py @@ -13,12 +13,16 @@ class T(unittest.TestCase): def test_writer_supports_texinfo(self): self.assertTrue(self.writer.supports('texinfo')) - def notest_translate_alice(self): + def test_translate_alice(self): self.given_input(""" ******************************** Alice In Wonderland ******************************** +================================ +Foo +================================ + CHAPTER I. Down the Rabbit-Hole =============================== @@ -33,6 +37,7 @@ conversation?' @node Top @top Alice In Wonderland +@majorheading Foo @chapter CHAPTER I. Down the Rabbit-Hole Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the -- 2.11.4.GIT