示例#1
0
 def p_no_break(self, t):
     """higher_element : NO_BREAK lower_elements"""
     prev = self._previous_paragraph
     if prev is None:
         paragraph = Paragraph(t[2])
     else:
         paragraph = Paragraph(t[2],
                               layout=LeftAligned(
                                   LeftAligned.FirstLineIndent.none),
                               embedded=prev.embedded,
                               introductory=prev.introductory,
                               poetic=prev.poetic,
                               continuation=True)
     self._previous_paragraph = paragraph
     t[0] = [paragraph]
示例#2
0
 def test_paragraph(self):
     bools = (False, True)
     for embedded, poetic, introductory, continuation \
             in itertools.product(bools, bools, bools, bools):
         word = test_utils.word()
         text = Text(word)
         paragraph = Paragraph([text],
                               embedded=embedded,
                               poetic=poetic,
                               introductory=introductory,
                               continuation=continuation)
         rendered = self.render_elements(paragraph)
         self.assertIn(word, rendered)
         if embedded:
             self.assertIn("embedded", rendered)  # should appear as a class
         else:
             self.assertNotIn("embedded", rendered)
         if poetic:
             self.assertIn("poetic", rendered)
         else:
             self.assertNotIn("poetic", rendered)
         if introductory:
             self.assertIn("introductory", rendered)
         else:
             self.assertNotIn("introductory", rendered)
         if continuation:
             self.assertIn("continuation", rendered)
         else:
             self.assertNotIn("continuation", rendered)
示例#3
0
 def p_chapter_label_after(self, t):
     """higher_element : CHAPTER CHAPTER_LABEL lower_elements"""
     chapter = ChapterNumber(ChapterNumber.Kind.standard,
                             [Text(t[2].value)])
     if len(t[3]) == 0:
         t[0] = [chapter]
     else:
         t[0] = [chapter, Paragraph(t[3])]
示例#4
0
 def test_heading(self):
     word = test_utils.word()
     heading = test_utils.word()
     elements = [Paragraph([Text(word)])]
     document = Document(elements, heading=heading)
     rendered = self.render(document)
     self.assertIn(word, rendered)
     self.assertIn(heading, rendered)
示例#5
0
 def test_footnotes(self):
     for kind in list(Footnote.Kind):
         word = test_utils.word()
         footnote = Footnote(kind, [Text(word)], AutomaticFootnoteLabel())
         paragraph = Paragraph([footnote])
         rendered = self.render_elements(paragraph)
         self.assertIn(kind.name, rendered)
         self.assertIn(word, rendered)
     for kind in list(Footnote.Kind):
         word = test_utils.word()
         label = test_utils.word(allow_empty=False)
         footnote = Footnote(kind, [Text(word)], CustomFootnoteLabel(label))
         paragraph = Paragraph([footnote])
         rendered = self.render_elements(paragraph)
         self.assertIn(kind.name, rendered)
         self.assertIn(word, rendered)
         self.assertIn(label, rendered)
示例#6
0
 def p_chapter_label_before(self, t):
     """higher_element : CHAPTER_LABEL CHAPTER lower_elements"""
     self.relative_chapter_label = t[1].value
     text = self.relative_chapter_label + " " + t[2].value
     chapter = ChapterNumber(ChapterNumber.Kind.standard, [Text(text)])
     if len(t[3]) == 0:
         t[0] = [chapter]
     else:
         t[0] = [chapter, Paragraph(t[3])]
示例#7
0
 def p_chapter(self, t):
     """higher_element : CHAPTER lower_elements"""
     if self.relative_chapter_label is not None:
         text = self.relative_chapter_label + " " + t[1].value
     else:
         text = t[1].value
     chapter = ChapterNumber(ChapterNumber.Kind.standard, [Text(text)])
     if len(t[2]) == 0:
         t[0] = [chapter]
     else:
         t[0] = [chapter, Paragraph(t[2])]
示例#8
0
 def higher_rule_inner(self, t):
     lower_rule(self, t)
     t[0] = [t[0]]
     if len(t[lower_elements_index]) > 0:
         t[0].append(Paragraph(t[lower_elements_index]))
示例#9
0
string literal that is used inside USFM files, and constructor is a function for
constructing the corresponding Element.

In general, a constructor of None means that a particular flag has special functionality,
and needs to be handled separately during parsing
"""

from usfm_utils.elements.element_impls import Paragraph, FormattedText, \
    ChapterNumber, OtherText, Text, Heading, Whitespace, Footnote
from usfm_utils.elements.paragraph_utils import Centered, LeftAligned, \
    RightAligned


# TOKEN_NAME: (flag, callable[children -> Paragraph])
paragraphs = {
    "CENTERED_PARAGRAPH": ("pc", Paragraph.Builder(layout=Centered())),
    "PARAGRAPH": ("p", Paragraph.Builder()),
    "FLUSH_PARAGRAPH": ("m", Paragraph.Builder(layout=LeftAligned(LeftAligned.FirstLineIndent.none))),
    "EMBEDDED_OPENING": ("pmo", Paragraph.Builder(layout=LeftAligned(LeftAligned.FirstLineIndent.none),
                                                  embedded=True)),
    "EMBEDDED_PARAGRAPH": ("pm", Paragraph.Builder(embedded=True)),
    "EMBEDDED_CLOSING": ("pmc", Paragraph.Builder(layout=LeftAligned(LeftAligned.FirstLineIndent.none),
                                                  embedded=True)),
    "EMBEDDED_REFRAIN": ("pmr", Paragraph.Builder(embedded=True,
                                                  layout=RightAligned())),
    "NO_BREAK": ("nb", None),
    "POETIC_RIGHT_ALIGNED": ("qr", Paragraph.Builder(layout=RightAligned(),
                                                     poetic=True)),
    "POETIC_CENTERED": ("qc", Paragraph.Builder(layout=Centered(), poetic=True)),

    # introductory paragraphs