示例#1
0
    def wrap(self):
        if DEBUG:
            print("Wrapping list item...")

        p_style, list_level, list_type = get_current_li(self.section.extra)

        self.cell_object.add_paragraph(style=p_style)

        numbered = False
        if list_type == ORDERED_LIST_NAME:
            numbered = True

        utils.list_number(self.cell_object.cell,
                          self.cell_object.paragraph,
                          level=list_level,
                          num=numbered)

        if isinstance(self.section.contents, str):
            insert_text(self.cell_object, self.section.contents)
            return

        temp_section = MarkdownSection('markdown', self.section.contents, {},
                                       {})
        temp_section.propagate_extra('inline', True)

        markdown.invoke(self.cell_object,
                        temp_section,
                        invoked_from_wrapper=True)
def test_collapse_attrs_not_all_collapsable():
    input_dict = [{
        "type":
        "span",
        "attrs": [],
        "layout": {},
        "extra": {},
        "contents": [{
            "type":
            "strong",
            "attrs": [],
            "layout": {},
            "extra": {},
            "contents": [{
                "type": "sometag",
                "attrs": [],
                "layout": {},
                "extra": {},
                "contents": "test"
            }]
        }]
    }]

    res = collapse_attrs(input_dict)
    expected = [
        MarkdownSection("span", [MarkdownSection("sometag", "test", {}, {})],
                        {}, {}, ["bold"])
    ]
    assert res[0].get_dict() == expected[0].get_dict()
def test_collapse_attrs_inner_nesting():
    input_dict = [{
        "type":
        "span",
        "attrs": [],
        "layout": {},
        "extra": {},
        "contents": [{
            "type":
            "strong",
            "attrs": [],
            "layout": {},
            "extra": {},
            "contents": [{
                "type":
                "em",
                "attrs": [],
                "layout": {},
                "extra": {},
                "contents": [{
                    "type": "strong",
                    "attrs": [],
                    "layout": {},
                    "extra": {},
                    "contents": "test"
                }]
            }]
        }]
    }]

    res = collapse_attrs(input_dict)
    expected = [MarkdownSection("span", "test", {}, {}, ["bold", "italic"])]
    assert res[0].get_dict() == expected[0].get_dict()
示例#4
0
    def wrap(self):
        if DEBUG:
            print('Wrapping quote...')

        self.cell_object.add_paragraph()
        cell = self.cell_object.cell.add_table(1, 1).cell(0, 0)

        quote_color = name_to_hex("cornsilk")
        new_cell = insert_cell_background(cell, quote_color)
        self.cell_object = CellObject(new_cell)

        contents = self.section.contents
        if isinstance(contents, str):
            temp_section = MarkdownSection(
                'markdown', [MarkdownSection('span', contents, {}, {})], {},
                {})
        else:
            temp_section = MarkdownSection('markdown', contents, {}, {})

        markdown.invoke(self.cell_object,
                        temp_section,
                        invoked_from_wrapper=True)
示例#5
0
    def wrap(self):
        if DEBUG:
            print("Wrapping code...")

        if 'inline' not in self.section.extra:
            self.cell_object.add_paragraph()
            # TODO: remove newlines from OXML

        cell = self.cell_object.cell.add_table(1, 1).cell(0, 0)
        code_color = name_to_hex("whitesmoke")
        new_cell = insert_cell_background(cell, code_color)
        self.cell_object = CellObject(new_cell)

        contents = self.section.contents
        if isinstance(contents, str):
            temp_section = MarkdownSection(
                'markdown', [MarkdownSection('span', contents, {}, {})], {},
                {})
        else:
            temp_section = MarkdownSection('markdown', contents, {}, {})

        markdown.invoke(self.cell_object,
                        temp_section,
                        invoked_from_wrapper=True)
示例#6
0
def collapse_attrs(section_list: List[Union[Section, dict]]) -> List[Section]:
    """ Collapse all of the sections
    (moving em as attributes or removing redundant elements like <p>) """
    from sane_doc_reports.transform.markdown.MarkdownSection import \
        MarkdownSection
    ret = []
    for section in section_list:
        if isinstance(section, MarkdownSection):
            s = MarkdownSection(section.type, section.contents, section.layout,
                                section.extra, section.attrs)
        else:
            s = MarkdownSection(section['type'], section['contents'],
                                section['layout'], section['extra'],
                                section['attrs'])
        s.collapse(False)
        ret.append(s)
    return ret
示例#7
0
    def wrap(self):
        if DEBUG:
            print("Wrapping Ul...")

        temp_section = MarkdownSection('markdown', self.section.contents, {},
                                       {})

        p_style, list_level, list_type = get_current_li(
            self.section.extra, UNORDERED_LIST_NAME)
        temp_section.propagate_extra('list_level',
                                     list_level,
                                     only_multiple_children=False)
        temp_section.propagate_extra('list_type',
                                     list_type,
                                     only_multiple_children=False)

        markdown.invoke(self.cell_object,
                        temp_section,
                        invoked_from_wrapper=True)
示例#8
0
    def wrap(self, invoked_from_wrapper=False):
        # Handle called from another wrapper.
        md_section_list = None
        if isinstance(self.section.contents, list):
            md_section_list = self.section.contents

        elif invoked_from_wrapper and \
                isinstance(self.section.contents.contents, str):
            md_section_list = [self.section.contents]

        if not isinstance(md_section_list, list):
            raise ValueError('Markdown section does not have valid contents ' +
                             '(must be a list)')

        for section in md_section_list:
            # === Start wrappers ===
            if section.type == MD_TYPE_DIV:
                temp_section = MarkdownSection('markdown', section.contents,
                                               {}, {})
                invoke(self.cell_object, temp_section)
                continue

            if section.type == MD_TYPE_CODE:
                md_code.invoke(self.cell_object, section)
                self.cell_object.update_paragraph()
                continue

            if section.type == MD_TYPE_QUOTE:
                md_blockquote.invoke(self.cell_object, section)
                self.cell_object.update_paragraph()
                continue

            if section.type == MD_TYPE_UNORDERED_LIST:
                md_ul.invoke(self.cell_object, section)
                self.cell_object.update_paragraph()
                continue

            if section.type == MD_TYPE_ORDERED_LIST:
                md_ol.invoke(self.cell_object, section)
                self.cell_object.update_paragraph()
                continue

            if section.type == MD_TYPE_LIST_ITEM:
                md_li.invoke(self.cell_object, section)
                continue

            if section.type == MD_TYPE_TABLE:
                table_html = section.extra['original_html']
                t = PyQuery(table_html)
                headers = [i.find('th') for i in t.find('tr').items()][0]
                headers = [c.text() for c in headers.items()]

                rows = [
                    i.find('td') for i in t.find('tr').items() if i.find('td')
                ]
                data = []
                for row in rows:
                    r = {
                        headers[i]: c.text()
                        for i, c in enumerate(row.items())
                    }
                    data.append(r)
                s = Section("table", data, {"tableColumns": headers}, {})
                table.invoke(self.cell_object, s)
                continue

            # Fix wrapped:
            #   (Some times there are elements which contain other elements,
            #    but are not considered one of the declared wrappers)
            if isinstance(section.contents, list):
                is_inside_wrapper = False

                if 'inline' in section.extra:
                    is_inside_wrapper = True

                if section.type == 'span':
                    section.propagate_extra('check_newline',
                                            True,
                                            only_multiple_children=False)

                # TODO: Fix problem with H1 no newline even if in span.
                temp_section = MarkdownSection('markdown', section.contents,
                                               {}, section.extra,
                                               section.attrs)
                invoke(self.cell_object,
                       temp_section,
                       invoked_from_wrapper=is_inside_wrapper)
                continue

            # === Elements ===

            if section.type in SHOULD_NEW_LINE and section.get_extra(
                    'check_newline'):
                self.cell_object.add_paragraph()

            if section.type == MD_TYPE_HORIZONTAL_LINE:
                md_hr.invoke(self.cell_object, section)
                continue

            # Add a block (newline) if not called from a wrapper
            #  (Should come after hr)
            if not invoked_from_wrapper:
                self.cell_object.add_paragraph()

            if section.type in MD_TYPES_HEADERS:
                # We want to keep the h{1...6} for styling
                insert_header(self.cell_object,
                              section.contents,
                              header=section.type)

                continue

            if section.type in [MD_TYPE_TEXT, MD_TYPE_INLINE_TEXT]:
                if invoked_from_wrapper:
                    self.cell_object.add_run()

                insert_text(self.cell_object, section)
                continue

            if section.type == MD_TYPE_LINK:
                md_link.invoke(self.cell_object, section)
                continue

            if section.type == MD_TYPE_IMAGE:
                md_image.invoke(self.cell_object, section)
                continue

            raise ValueError(f'Section type is not defined: {section.type}')