Ejemplo n.º 1
0
    def insert(self):
        if DEBUG:
            print("Adding list...")

        list_data = self.section.contents
        if isinstance(list_data, dict) and len(list_data) != 1:
            list_data = [list_data]

        if isinstance(list_data, dict) and len(list_data) == 1:
            # Create the parent title
            wrapper_table = self.cell_object.cell.add_table(rows=2, cols=1)
            title_cell = wrapper_table.cell(0, 0)
            title_text = list(list_data.keys())[0]
            insert_text(title_cell, title_text,
                        self.style['title'])

            # Create a list in a list because this is a grouped list
            co = CellObject(wrapper_table.cell(1, 0))

            table_data = list_data[title_text]
            invoke(co,
                   Section('list', table_data, self.section.layout,
                           {}))
        else:
            table.invoke(self.cell_object,
                         Section('table', list_data,
                                 self.section.layout,
                                 {'list_style': True}))
Ejemplo n.º 2
0
    def wrap(self, invoked_from_wrapper=False):
        # Handle called from another wrapper.
        items = self.section.contents

        if not isinstance(items, list):
            raise ValueError('ItemsSection does not have valid contents ' +
                             '(must be a list)')

        table_width = max(items, key=lambda x: x['endCol']).get('endCol')
        row_count = max(items,
                        key=lambda x: x.get('index', 0)).get('index') + 1

        title_offset = 0
        has_title = 'title' in self.section.extra
        if has_title:
            title_offset += 1

        item_table = self.cell_object.cell.add_table(rows=row_count +
                                                     title_offset,
                                                     cols=table_width)
        if DEBUG:
            item_table.style = DEFAULT_TABLE_STYLE

        if has_title:
            section_title = self.section.extra['title']
            insert_text(item_table.cell(0, 0), section_title + '\n',
                        self.style['title'])

        for item in items:
            row, col, col_to_merge = item.get(
                'index',
                0 + title_offset), item.get('startCol'), item.get('endCol')
            current_cell = item_table.cell(row + title_offset, col)
            current_cell.merge(
                item_table.cell(row + title_offset, col_to_merge - 1))

            field_name = item.get("fieldName", "")
            field_type = item.get("fieldType", "shortText")
            field_name = field_name[0].upper() + field_name[1:]
            field_name = f'{field_name}: '

            if item.get('displayType', self.display_types['ROW']) == \
                    self.display_types['CARD']:
                field_name += '\n'

            data = item.get("data", "")
            insert_text(current_cell, field_name, self.style['key'])

            if field_type == 'grid':
                table.invoke(
                    self.cell_object,
                    SectionObject('table', data, self.section.layout, {}))
            else:
                insert_text(current_cell, data, self.style['value'])
Ejemplo n.º 3
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}')