Skip to content

Commit

Permalink
Handle RST quotes that don't map to quote slides. (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyergler committed Mar 28, 2015
1 parent eb4c479 commit e37970b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 25 deletions.
65 changes: 65 additions & 0 deletions src/hieroglyph/tests/test_translator.py
Expand Up @@ -374,3 +374,68 @@ def test_rst_quote_makes_quote_slide(self):
'\n\n\n\n\n</article>',
],
)

def test_unattributed_rst_quote_makes_quote_slide(self):
document = util.make_document(
'quoted',
"""\
.. slide:: Quotes
:level: 2
reStructuredText quotes are automatically converted
""",
)
translator = SlideTranslator(
self.builder,
document,
)

document.walkabout(translator)

self.assertEqual(
translator.body,
[
u'\n<article class="admonition-quotes slide level-2">\n\n'
'<h2>Quotes</h2>\n\n'
'<q>\n'
'reStructuredText quotes are automatically converted</q>\n'
'\n\n\n\n</article>',
],
)

def test_rst_quote_processes_normally_with_extra_content(self):
document = util.make_document(
'quoted',
"""\
.. slide:: Indented RST
:level: 2
This text is over indented.
As is this text.
They look like quotes but they're not.
""",
)
translator = SlideTranslator(
self.builder,
document,
)

document.walkabout(translator)

self.assertEqual(
translator.body,
[
u'\n<article class="admonition-indented-rst slide level-2">\n\n'
'<h2>Indented RST</h2>\n\n'
'<blockquote>\n'
'<div><p>This text is over indented.</p>\n'
'<p>As is this text.</p>\n'
'<p>They look like quotes but they\'re not.</p>\n'
'</div></blockquote>\n'
'\n\n\n\n</article>',
],
)
54 changes: 29 additions & 25 deletions src/hieroglyph/writer.py
Expand Up @@ -264,33 +264,37 @@ def depart_title(self, node):
self.body.append(title)

def visit_block_quote(self, node):
# first child must be a paragraph, process it as a <q> element
p = node.children[0]
if p.tagname != 'paragraph':
raise ValueError("The first child of a quote must be a paragraph")
self.body.append(self.starttag(node, 'q'))
for text_item in p:
text_item.walkabout(self)
self.body.append('</q>\n')

if len(node.children) > 2:
raise ValueError("A quote can only have 2 children, the quote text"
" and an attribution")
# optional second child must be an attribution, processing as a <div>
# following the <q>
if len(node.children) > 1:
attr = node.children[1]
if attr.tagname != 'attribution':
raise ValueError("The second child of a quote must be"
" an attribution")

self.body.append(self.starttag(attr, 'div', CLASS="author"))
for text_item in attr:
quote_slide_tags = ['paragraph', 'attribution']

# see if this looks like a quote slide
if (len(node.children) <= 2 and
[c.tagname for c in node.children] == quote_slide_tags[:len(node.children)]):

# process this as a quote slide

# first child must be a paragraph, process it as a <q> element
p = node.children[0]
self.body.append(self.starttag(node, 'q'))
for text_item in p:
text_item.walkabout(self)
self.body.append('</div>\n')
self.body.append('</q>\n')

# optional second child must be an attribution, processing as a <div>
# following the <q>
if len(node.children) > 1:
attr = node.children[1]

self.body.append(self.starttag(attr, 'div', CLASS="author"))
for text_item in attr:
text_item.walkabout(self)
self.body.append('</div>\n')

# skip all normal processing
raise nodes.SkipNode

else:
return HTMLTranslator.visit_block_quote(self, node)

# skip all normal processing
raise nodes.SkipNode

class SlideTranslator(BaseSlideTranslator):

Expand Down

0 comments on commit e37970b

Please sign in to comment.