Esempio n. 1
0
def _render_standard_list(text_renderer: text.TextRenderer,
                          paragraph_item_renderer: ParagraphItemRenderer,
                          parent: Element,
                          list_: lists.HeaderContentList) -> Element:
    """
    :return: The added element.
    """
    list_root = list_element_for(parent, list_.list_format)
    for item in list_.items:
        li = SubElement(list_root, 'li')
        text_renderer.apply(li, li, Position.INSIDE, item.header)
        for paragraph_item in item.content_paragraph_items:
            paragraph_item_renderer.apply(li, paragraph_item)
    return list_root
Esempio n. 2
0
def _render_standard_list(text_renderer: text.TextRenderer,
                          paragraph_item_renderer: ParagraphItemRenderer,
                          parent: Element,
                          list_: lists.HeaderContentList) -> Element:
    """
    :return: The added element.
    """
    list_root = list_element_for(parent, list_.list_format)
    for item in list_.items:
        li = SubElement(list_root, 'li')
        text_renderer.apply(li, li, Position.INSIDE, item.header)
        for paragraph_item in item.content_paragraph_items:
            paragraph_item_renderer.apply(li, paragraph_item)
    return list_root
Esempio n. 3
0
 def test_single_anchor_with_cross_reference_as_anchored_text(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.AnchorText(
             core.CrossReferenceText(
                 StringText('cross ref title'),
                 CrossReferenceTargetTestImpl('cross ref target'),
                 target_is_id_in_same_document=True),
             CrossReferenceTargetTestImpl('anchor target')),
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                          root,
                          para,
                          skip_surrounding_p_element=True)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<span id="anchor target">'
         '<a href="#cross ref target">cross ref title</a>'
         '</span>'
         '</root>', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 4
0
def _render_definition_list(text_renderer: text.TextRenderer,
                            paragraph_item_renderer: ParagraphItemRenderer,
                            parent: Element,
                            list_: lists.HeaderContentList) -> Element:
    """
    :return: The added element.
    """
    list_root = SubElement(parent, 'dl')
    for item in list_.items:
        assert isinstance(item, lists.HeaderContentListItem)  # Type info for IDE
        dt = SubElement(list_root, 'dt', )
        text_renderer.apply(dt, dt, Position.INSIDE, item.header)
        content_paragraph_items = item.content_paragraph_items
        if content_paragraph_items:
            dd = SubElement(list_root, 'dd')
            for paragraph_item in content_paragraph_items:
                paragraph_item_renderer.apply(dd, paragraph_item)
    return list_root
Esempio n. 5
0
 def test_two_string_text(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([core.StringText('_1_'), core.StringText('_2_')])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual('<root>' '<p>_1_<br />_2_</p>' '</root>', xml_string)
     self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
Esempio n. 6
0
 def test_empty(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual('<root />', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 7
0
 def test_two_string_text(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([core.StringText('_1_'), core.StringText('_2_')])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                          root,
                          para,
                          skip_surrounding_p_element=True)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual('<root>' '_1_<br />_2_' '</root>', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 8
0
def _render_definition_list(text_renderer: text.TextRenderer,
                            paragraph_item_renderer: ParagraphItemRenderer,
                            parent: Element,
                            list_: lists.HeaderContentList) -> Element:
    """
    :return: The added element.
    """
    list_root = SubElement(parent, 'dl')
    for item in list_.items:
        assert isinstance(item,
                          lists.HeaderContentListItem)  # Type info for IDE
        dt = SubElement(
            list_root,
            'dt',
        )
        text_renderer.apply(dt, dt, Position.INSIDE, item.header)
        content_paragraph_items = item.content_paragraph_items
        if content_paragraph_items:
            dd = SubElement(list_root, 'dd')
            for paragraph_item in content_paragraph_items:
                paragraph_item_renderer.apply(dd, paragraph_item)
    return list_root
Esempio n. 9
0
def render(text_renderer: TextRenderer,
           parent: Element,
           paragraph: Paragraph,
           skip_surrounding_p_element=False) -> Element:
    """
    :param para:
    :return: parent, if the paragraph is empty. Otherwise the added p element.
    """
    start_on_new_line_blocks = paragraph.start_on_new_line_blocks
    if not start_on_new_line_blocks:
        return parent
    if skip_surrounding_p_element:
        element_container = parent
    else:
        element_container = SubElement(parent, 'p')

    text_renderer.apply(element_container, element_container, Position.INSIDE,
                        start_on_new_line_blocks[0])
    for text in start_on_new_line_blocks[1:]:
        br = SubElement(element_container, 'br')
        text_renderer.apply(element_container, br, Position.AFTER, text)
    return element_container
Esempio n. 10
0
 def test_empty(self):
     # ARRANGE #
     for list_type in ListType:
         with self.subTest(list_type=list_type):
             root = Element('root')
             the_list = HeaderContentList([], Format(list_type))
             # ACT #
             ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                                  ConstantPRenderer('para text'), root,
                                  the_list)
             # ASSERT #
             xml_string = as_unicode_str(root)
             self.assertEqual('<root />', xml_string)
             self.assertIs(root, ret_val)
Esempio n. 11
0
 def test_single_anchor_with_string_as_anchored_text(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.AnchorText(core.StringText('concrete string'),
                         CrossReferenceTargetTestImpl('target')),
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<p>'
         '<span id="target">concrete string</span>'
         '</p>'
         '</root>', xml_string)
     self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
Esempio n. 12
0
 def test_single_cross_reference_to_not_id_in_same_document(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.CrossReferenceText(StringText('title'),
                                 CrossReferenceTargetTestImpl('target'),
                                 target_is_id_in_same_document=False),
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<p>'
         '<a href="target">title</a>'
         '</p>'
         '</root>', xml_string)
     self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
Esempio n. 13
0
 def test_single_anchor_with_string_as_anchored_text(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.AnchorText(core.StringText('concrete string'),
                         CrossReferenceTargetTestImpl('target')),
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                          root,
                          para,
                          skip_surrounding_p_element=True)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<span id="target">concrete string</span>'
         '</root>', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 14
0
 def test_cross_reference_and_string(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.CrossReferenceText(StringText('title'),
                                 CrossReferenceTargetTestImpl('target'),
                                 target_is_id_in_same_document=True),
         core.StringText('string')
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                          root,
                          para,
                          skip_surrounding_p_element=True)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<a href="#target">title</a><br />string'
         '</root>', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 15
0
 def test_two_cross_reference(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.CrossReferenceText(StringText('title 1'),
                                 CrossReferenceTargetTestImpl('target 1')),
         core.CrossReferenceText(StringText('title 2'),
                                 CrossReferenceTargetTestImpl('target 2'))
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<p>'
         '<a href="#target 1">'
         'title 1</a><br /><a href="#target 2">title 2'
         '</a>'
         '</p>'
         '</root>', xml_string)
     self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
Esempio n. 16
0
 def test(self):
     cases = [
         Case('test_singleton_element_without_contents',
              items=[HeaderContentListItem(text('header'))],
              expected='<root>'
              '<dl>'
              '<dt>header</dt>'
              '</dl>'
              '</root>'),
         Case('test_singleton_element_with_multiple_content_paragraphs',
              items=[
                  HeaderContentListItem(
                      text('header'),
                      [para('ignored'), para('ignored')])
              ],
              expected='<root>'
              '<dl>'
              '<dt>header</dt>'
              '<dd>'
              '<p>every para text</p>'
              '<p>every para text</p>'
              '</dd>'
              '</dl>'
              '</root>'),
     ]
     for case in cases:
         with self.subTest(case.name):
             root = Element('root')
             the_list = HeaderContentList(case.items,
                                          Format(ListType.VARIABLE_LIST))
             # ACT #
             ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                                  ConstantPRenderer('every para text'),
                                  root, the_list)
             # ASSERT #
             xml_string = as_unicode_str(root)
             self.assertEqual(case.expected, xml_string)
             self.assertIs(list(root)[0], ret_val)
Esempio n. 17
0
 def test_two_cross_reference(self):
     # ARRANGE #
     root = Element('root')
     para = Paragraph([
         core.CrossReferenceText(StringText('title 1'),
                                 CrossReferenceTargetTestImpl('target 1')),
         core.CrossReferenceText(StringText('title 2'),
                                 CrossReferenceTargetTestImpl('target 2'))
     ])
     # ACT #
     ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                          root,
                          para,
                          skip_surrounding_p_element=True)
     # ASSERT #
     xml_string = as_unicode_str(root)
     self.assertEqual(
         '<root>'
         '<a href="#target 1">'
         'title 1</a><br /><a href="#target 2">title 2'
         '</a>'
         '</root>', xml_string)
     self.assertIs(root, ret_val)
Esempio n. 18
0
                    '</body>'
                    '</html>')
        self.assertEqual(expected, actual)


class SingleParaPopulator(utils.ElementPopulator):
    def __init__(self, para_text: str):
        self.para_text = para_text

    def apply(self, parent: Element):
        SubElement(parent, 'p').text = self.para_text


class HeadStylePopulator(utils.ElementPopulator):
    def __init__(self, style: str):
        self.style = style

    def apply(self, parent: Element):
        SubElement(parent, 'style').text = self.style


TEST_SECTION_RENDERER = sut.SectionItemRenderer(
    TARGET_RENDERER_AS_NAME,
    HnSectionHeaderRenderer(TextRenderer(TARGET_RENDERER_AS_NAME)),
    ParaWithSingleStrTextRenderer())

DOCUMENT_RENDERER = sut.DocumentRenderer(TEST_SECTION_RENDERER)

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())
Esempio n. 19
0
 def test_non_empty(self):
     # ARRANGE #
     cases = [
         Case(
             'test_singleton_element_without_contents',
             items=[HeaderContentListItem(text('header'))],
             expected='<root>'
             '<{L}><li>header</li></{L}>'
             '</root>',
         ),
         Case(
             'test_multiple_element_without_contents',
             items=[
                 HeaderContentListItem(text('header 1')),
                 HeaderContentListItem(text('header 2'))
             ],
             expected='<root>'
             '<{L}>'
             '<li>header 1</li>'
             '<li>header 2</li>'
             '</{L}>'
             '</root>',
         ),
         Case(
             'test_single_element_with_single_para_contents',
             items=[
                 HeaderContentListItem(text('header'), paras('ignored'))
             ],
             expected='<root>'
             '<{L}>'
             '<li>header<p>every para text</p></li>'
             '</{L}>'
             '</root>',
         ),
         Case(
             'test_single_element_with_multiple_para_contents',
             items=[
                 HeaderContentListItem(
                     text('header'),
                     [para('ignored'), para('ignored')])
             ],
             expected='<root>'
             '<{L}>'
             '<li>header'
             '<p>every para text</p>'
             '<p>every para text</p>'
             '</li>'
             '</{L}>'
             '</root>',
         ),
     ]
     for list_type, element in [(ListType.ITEMIZED_LIST, 'ul'),
                                (ListType.ORDERED_LIST, 'ol')]:
         for case in cases:
             with self.subTest(list_type=list_type, case=case.name):
                 root = Element('root')
                 the_list = HeaderContentList(case.items, Format(list_type))
                 # ACT #
                 ret_val = sut.render(TextRenderer(TARGET_RENDERER),
                                      ConstantPRenderer('every para text'),
                                      root, the_list)
                 # ASSERT #
                 expected = case.expected.format(L=element)
                 xml_string = as_unicode_str(root)
                 self.assertEqual(expected, xml_string)
                 self.assertIs(list(root)[0], ret_val)
Esempio n. 20
0
class TestHnSectionHeaderRenderer(unittest.TestCase):
    HN_SECTION_HEADER_RENDERER = HnSectionHeaderRenderer(
        TextRenderer(TARGET_RENDERER_AS_NAME))

    def test_level_0(self):
        # ARRANGE #
        root = Element('root')
        header_text = 'text'

        expected_element = element(root.tag,
                                   children=[element('h1', text=header_text)])
        # ACT #
        ret_val = self.HN_SECTION_HEADER_RENDERER.apply(
            sut.Environment(0), root, StringText(header_text), {})
        # ASSERT #
        assert_contents_and_that_last_child_is_returned(
            expected_element, root, ret_val, self)

    def test_with_attributes(self):
        # ARRANGE #
        root_to_mutate = Element('root')
        # ACT #
        ret_val = self.HN_SECTION_HEADER_RENDERER.apply(
            sut.Environment(3), root_to_mutate, StringText('text'), {
                'attr1': 'attr1-value',
                'attr2': 'attr2-value'
            })
        expected_element = element(root_to_mutate.tag,
                                   children=[
                                       element('h4',
                                               attributes={
                                                   'attr1': 'attr1-value',
                                                   'attr2': 'attr2-value'
                                               },
                                               text='text')
                                   ])
        # ASSERT #
        assert_contents_and_that_last_child_is_returned(
            expected_element, root_to_mutate, ret_val, self)

    def test_highest_h_level(self):
        # ARRANGE #
        root = Element('root')
        header_text = 'text'

        expected_element = element(root.tag,
                                   children=[element('h6', text=header_text)])
        # ACT #
        ret_val = self.HN_SECTION_HEADER_RENDERER.apply(
            sut.Environment(5), root, StringText(header_text), {})
        # ASSERT #
        assert_contents_and_that_last_child_is_returned(
            expected_element, root, ret_val, self)

    def test_level_greater_than_highest_h_level(self):
        # ARRANGE #
        root = Element('root')
        header_text = 'text'
        expected_element = element(root.tag,
                                   children=[element('h6', text=header_text)])
        # ACT #
        ret_val = self.HN_SECTION_HEADER_RENDERER.apply(
            sut.Environment(6), root, StringText(header_text), {})
        # ASSERT #
        assert_contents_and_that_last_child_is_returned(
            expected_element, root, ret_val, self)