def _add_html_listitem(self, html_parent, list_item: ListItem) -> None: html_list_item = ET.SubElement(html_parent, 'li') inline_elements: TList[InlineElement] = list_item.inline_elements if list_item.symbol != '-': if len(inline_elements) > 0 and isinstance(inline_elements[0], NormalText): inline_elements[0] = NormalText(list_item.symbol + ' ' + inline_elements[0].text) else: inline_elements = [NormalText(list_item.symbol + ' ') ] + inline_elements # if list_item.preformatted: # html_cur_item = ET.SubElement(html_list_item, 'pre') # else: # html_cur_item = html_list_item # self._add_html_inline_elements(html_cur_item, inline_elements) if list_item.preformatted: self._add_preformatted_html_inline_elements( html_list_item, inline_elements) else: self._add_html_inline_elements(html_list_item, inline_elements) for sub_item in list_item.sub_items: # html_list = ET.SubElement(html_cur_item, 'ul') html_list = ET.SubElement(html_list_item, 'ul') self._add_html_listitem(html_list, sub_item)
def _iter_inline_elements(self, xml_element) -> Iterator[InlineElement]: """ <a>a.text<b>b.text</b>b.tail</a>""" if xml_element.text: yield NormalText(xml_element.text) for xml_child in xml_element: inline_elem = self._create_inline_element(xml_child) if inline_elem is not None: yield inline_elem if xml_child.tail: yield NormalText(xml_child.tail)
def test_list_with_sublist(self): self._test_one_element( List([ ListItem(inline_elements=[ NormalText('aaa'), BoldText('bbb'), NormalText('ccc') ], sub_items=[ListItem([NormalText('ddd')])]) ]), '<list>' '<item>aaa<bold>bbb</bold>ccc' '<item>ddd</item>' '</item>' '</list>')
def test_nested_list(self): self._test_one_element( List([ ListItem([NormalText('aaa')], sub_items=[ListItem([NormalText('bbb')])]) ]), '<ul>' '<li>aaa' '<ul>' '<li>bbb</li>' '</ul>' '</li>' '</ul>' )
def test_table(self): self._test_one_element( Table(columns=[Column(halign=HAlign.LEFT, text='A'), Column(halign=HAlign.RIGHT, text='B')], rows=[Row([Cell([NormalText('aaa')]), Cell([NormalText('bbb')])])]), '<table>' '<thead>' '<tr>' '<th align="left">A</th>' '<th align="right">B</th>' '</tr>' '</thead>' '<tbody>' '<tr>' '<td>aaa</td>' '<td>bbb</td>' '</tr>' '</tbody>' '</table>' )
def test_table(self): self._test_one_element( Table( columns=[ Column(halign=HAlign.LEFT, text='A'), Column(halign=HAlign.RIGHT, text='B') ], rows=[ Row([Cell([NormalText('aaa')]), Cell([NormalText('bbb')])]) ]), '<table>' '<column halign="left">A</column>' '<column halign="right">B</column>' '<row><cell>aaa</cell><cell>bbb</cell></row>' '</table>')
def create(self) -> ET.XML: html_page: ET.XML = ET.fromstring('<html></html>') if self._title: self._add_html_header( html_page, Header(level=0, inline_elements=[NormalText(self._title)])) for block_element in self._page.block_elements: self._add_html_block_element(html_page, block_element) return html_page
def _extend_page_with_task_cache(self, page: Page, task: Task) -> None: task_cache = task.cache if task_cache: if task_cache.readme: page.block_elements.append( Header(level=2, inline_elements=[NormalText('readme:')])) page.block_elements.append( Paragraph([NormalText(task_cache.readme)], preformatted=True)) if task_cache.file_names: page.block_elements.append( Header(level=2, inline_elements=[NormalText('files:')])) file_tree = FilebufSplitter(task_cache.file_names).split() new_list = List() task_path = task.get_path(self._task_model.tasks_root) for name in sorted(file_tree.keys()): new_link = Link(text=name, uri=str(task_path / name)) new_item = ListItem(inline_elements=[new_link]) self._build_file_list_recursive(new_item, file_tree[name], task_path / name) new_list.items.append(new_item) page.block_elements.append(new_list)
def test_preformatted_list(self): self._test_one_element( List([ListItem([NormalText(' aaa\n bbb')], preformatted=True)]), '<ul><li><pre> aaa</pre><pre> bbb</pre></li></ul>' )
def test_list_with_arrow(self): self._test_one_element( List([ListItem([NormalText('aaa')]), ListItem([NormalText('bbb')], symbol='=>')]), '<ul><li>aaa</li><li>=> bbb</li></ul>' )
def test_simple_list(self): self._test_one_element(List([ListItem([NormalText('aaa')])]), '<list><item>aaa</item></list>')
def test_list(self): self._test_one_element( List([ListItem([NormalText('aaa')])]), '<ul><li>aaa</li></ul>' )
def test_bold(self): self._test_one_element( Paragraph([NormalText('aaa'), BoldText('bbb'), NormalText('ccc')]), '<p>aaa<b>bbb</b>ccc</p>' )
def test_preformatted_paragraph(self): self._test_one_element( Paragraph([NormalText(' aaa\n bbb')], preformatted=True), '<pre> aaa\n bbb</pre>' )
def test_preformatted_paragraph(self): self._test_one_element( Paragraph([NormalText('aaa')], preformatted=True), '<paragraph preformatted="true">aaa</paragraph>')
def test_preformatted_list_item(self): self._test_one_element( List([ListItem([NormalText('aaa')], preformatted=True)]), '<list><item preformatted="true">aaa</item></list>')
def test_list_with_non_default_symbol(self): self._test_one_element( List([ListItem([NormalText('aaa')], symbol='a.')]), '<list><item symbol="a.">aaa</item></list>')
def test_header(self): self._test_one_element( Header(level=1, inline_elements=[NormalText('aaa')]), '<h2>aaa</h2>' )
def test_simple_paragraph(self): self._test_one_element( Paragraph([NormalText('aaa')]), '<p>aaa</p>' )
def _process_gap_text(self, gap_text: str) -> None: if len(gap_text) > 0: normal_text = NormalText(gap_text) self._inline_elements.append(normal_text)
def test_paragraph(self): self._test_one_element(Paragraph([NormalText('aaa')]), '<paragraph>aaa</paragraph>')