Exemplo n.º 1
0
 def start_new_table(self, html_tag, tag_style=None):
     self.current_table = Table(html_tag, tag_style)
     self.tables.append(self.current_table)
Exemplo n.º 2
0
 def start_new_table(self, html_tag, tag_style=None):
     self.current_table = Table(self.namespace, html_tag, tag_style)
     self.tables.append(self.current_table)
Exemplo n.º 3
0
class Blocks(object):

    def __init__(self, styles_manager):
        self.styles_manager = styles_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
        self.current_block = None

    def start_new_block(self, html_block, style, is_table_cell=False):
        self.end_current_block()
        self.current_block = Block(self.styles_manager, html_block, style, is_table_cell=is_table_cell)
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, *args):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            block = self.all_blocks[self.pos]
            del self.all_blocks[self.pos]
            del self.items[self.block_map.pop(block)]
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
Exemplo n.º 4
0
class Blocks(object):

    def __init__(self, namespace, styles_manager):
        self.namespace = namespace
        self.styles_manager = styles_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
        self.current_block = None

    def start_new_block(self, html_block, style, is_table_cell=False):
        self.end_current_block()
        self.current_block = Block(self.namespace, self.styles_manager, html_block, style, is_table_cell=is_table_cell)
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(self.namespace, html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, *args):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            block = self.all_blocks[self.pos]
            del self.all_blocks[self.pos]
            del self.items[self.block_map.pop(block)]
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
Exemplo n.º 5
0
class Blocks:
    def __init__(self, namespace, styles_manager, links_manager):
        self.top_bookmark = None
        self.namespace = namespace
        self.styles_manager = styles_manager
        self.links_manager = links_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()
        self.html_tag_start_blocks = {}

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None and self.current_table.current_row is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
                self.current_block.parent_items = self.items
        self.current_block = None

    def start_new_block(self,
                        html_block,
                        style,
                        is_table_cell=False,
                        float_spec=None,
                        is_list_item=False):
        parent_bg = None
        if html_block is not None:
            p = html_block.getparent()
            b = self.html_tag_start_blocks.get(p)
            if b is not None:
                ps = self.styles_manager.styles_for_html_blocks.get(p)
                if ps is not None and ps.background_color is not None:
                    parent_bg = ps.background_color
        self.end_current_block()
        self.current_block = Block(self.namespace,
                                   self.styles_manager,
                                   self.links_manager,
                                   html_block,
                                   style,
                                   is_table_cell=is_table_cell,
                                   float_spec=float_spec,
                                   is_list_item=is_list_item,
                                   parent_bg=parent_bg)
        self.html_tag_start_blocks[html_block] = self.current_block
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(self.namespace, html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            start_block = self.html_tag_start_blocks.get(html_tag)
            if start_block is not None and start_block.html_style[
                    'page-break-after'] == 'always':
                self.current_block.page_break_after = True
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def delete_block_at(self, pos=None):
        pos = self.pos if pos is None else pos
        block = self.all_blocks[pos]
        del self.all_blocks[pos]
        bpos = self.block_map.pop(block, None)
        if bpos is not None:
            del self.items[bpos]
        else:
            items = self.items if block.parent_items is None else block.parent_items
            items.remove(block)
        block.parent_items = None
        if block.float_spec is not None:
            block.float_spec.blocks.remove(block)
        try:
            next_block = self.all_blocks[pos]
            next_block.bookmarks.update(block.bookmarks)
            for attr in 'page_break_after page_break_before'.split():
                setattr(next_block, attr, getattr(block, attr))
        except (IndexError, KeyError):
            pass

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, etype, value, traceback):
        if value is not None:
            return  # Since there was an exception, the data structures are not in a consistent state
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[
                self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            self.delete_block_at(self.pos)
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
            if self.top_bookmark is not None:
                self.all_blocks[self.pos].bookmarks.add(self.top_bookmark)
        self.top_bookmark = None
        self.block_map = {}

    def apply_page_break_after(self):
        for i, block in enumerate(self.all_blocks):
            if block.page_break_after and i < len(self.all_blocks) - 1:
                next_block = self.all_blocks[i + 1]
                if next_block.parent_items is block.parent_items and block.parent_items is self.items:
                    next_block.page_break_before = True

    def resolve_language(self):
        default_lang = self.styles_manager.document_lang
        for block in self.all_blocks:
            count = Counter()
            for run in block.runs:
                count[run.lang] += 1
            if count:
                block.block_lang = bl = count.most_common(1)[0][0]
                for run in block.runs:
                    if run.lang == bl:
                        run.lang = None
                if bl == default_lang:
                    block.block_lang = None

    def __repr__(self):
        return 'Block(%r)' % self.runs
Exemplo n.º 6
0
class Blocks(object):

    def __init__(self, namespace, styles_manager, links_manager):
        self.namespace = namespace
        self.styles_manager = styles_manager
        self.links_manager = links_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
                self.current_block.parent_items = self.items
        self.current_block = None

    def start_new_block(self, html_block, style, is_table_cell=False, float_spec=None, is_list_item=False):
        self.end_current_block()
        self.current_block = Block(
            self.namespace, self.styles_manager, self.links_manager, html_block, style,
            is_table_cell=is_table_cell, float_spec=float_spec, is_list_item=is_list_item)
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(self.namespace, html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def delete_block_at(self, pos=None):
        pos = self.pos if pos is None else pos
        block = self.all_blocks[pos]
        del self.all_blocks[pos]
        if self.block_map:
            del self.items[self.block_map.pop(block)]
        else:
            items = self.items if block.parent_items is None else block.parent_items
            items.remove(block)
        block.parent_items = None
        if block.float_spec is not None:
            block.float_spec.blocks.remove(block)
        try:
            self.all_blocks[pos].bookmarks.update(block.bookmarks)
        except (IndexError, KeyError):
            pass

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, etype, value, traceback):
        if value is not None:
            return  # Since there was an exception, the data structures are not in a consistent state
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            self.delete_block_at(self.pos)
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
        self.block_map = {}
Exemplo n.º 7
0
class Blocks(object):

    def __init__(self, namespace, styles_manager, links_manager):
        self.top_bookmark = None
        self.namespace = namespace
        self.styles_manager = styles_manager
        self.links_manager = links_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()
        self.html_tag_start_blocks = {}

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None and self.current_table.current_row is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
                self.current_block.parent_items = self.items
        self.current_block = None

    def start_new_block(self, html_block, style, is_table_cell=False, float_spec=None, is_list_item=False):
        parent_bg = None
        if html_block is not None:
            p = html_block.getparent()
            b = self.html_tag_start_blocks.get(p)
            if b is not None:
                ps = self.styles_manager.styles_for_html_blocks.get(p)
                if ps is not None and ps.background_color is not None:
                    parent_bg = ps.background_color
        self.end_current_block()
        self.current_block = Block(
            self.namespace, self.styles_manager, self.links_manager, html_block, style,
            is_table_cell=is_table_cell, float_spec=float_spec, is_list_item=is_list_item,
            parent_bg=parent_bg)
        self.html_tag_start_blocks[html_block] = self.current_block
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(self.namespace, html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            start_block = self.html_tag_start_blocks.get(html_tag)
            if start_block is not None and start_block.html_style['page-break-after'] == 'always':
                self.current_block.page_break_after = True
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def delete_block_at(self, pos=None):
        pos = self.pos if pos is None else pos
        block = self.all_blocks[pos]
        del self.all_blocks[pos]
        bpos = self.block_map.pop(block, None)
        if bpos is not None:
            del self.items[bpos]
        else:
            items = self.items if block.parent_items is None else block.parent_items
            items.remove(block)
        block.parent_items = None
        if block.float_spec is not None:
            block.float_spec.blocks.remove(block)
        try:
            next_block = self.all_blocks[pos]
            next_block.bookmarks.update(block.bookmarks)
            for attr in 'page_break_after page_break_before'.split():
                setattr(next_block, attr, getattr(block, attr))
        except (IndexError, KeyError):
            pass

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, etype, value, traceback):
        if value is not None:
            return  # Since there was an exception, the data structures are not in a consistent state
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            self.delete_block_at(self.pos)
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
            if self.top_bookmark is not None:
                self.all_blocks[self.pos].bookmarks.add(self.top_bookmark)
        self.top_bookmark = None
        self.block_map = {}

    def apply_page_break_after(self):
        for i, block in enumerate(self.all_blocks):
            if block.page_break_after and i < len(self.all_blocks) - 1:
                next_block = self.all_blocks[i + 1]
                if next_block.parent_items is block.parent_items and block.parent_items is self.items:
                    next_block.page_break_before = True

    def resolve_language(self):
        default_lang = self.styles_manager.document_lang
        for block in self.all_blocks:
            count = Counter()
            for run in block.runs:
                count[run.lang] += 1
            if count:
                block.block_lang = bl = count.most_common(1)[0][0]
                for run in block.runs:
                    if run.lang == bl:
                        run.lang = None
                if bl == default_lang:
                    block.block_lang = None

    def __repr__(self):
        return 'Block(%r)' % self.runs
Exemplo n.º 8
0
class Blocks(object):
    def __init__(self, namespace, styles_manager, links_manager):
        self.namespace = namespace
        self.styles_manager = styles_manager
        self.links_manager = links_manager
        self.all_blocks = []
        self.pos = 0
        self.current_block = None
        self.items = []
        self.tables = []
        self.current_table = None
        self.open_html_blocks = set()

    def current_or_new_block(self, html_tag, tag_style):
        return self.current_block or self.start_new_block(html_tag, tag_style)

    def end_current_block(self):
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
            if self.current_table is not None:
                self.current_table.add_block(self.current_block)
            else:
                self.block_map[self.current_block] = len(self.items)
                self.items.append(self.current_block)
                self.current_block.parent_items = self.items
        self.current_block = None

    def start_new_block(self,
                        html_block,
                        style,
                        is_table_cell=False,
                        float_spec=None,
                        is_list_item=False):
        self.end_current_block()
        self.current_block = Block(self.namespace,
                                   self.styles_manager,
                                   self.links_manager,
                                   html_block,
                                   style,
                                   is_table_cell=is_table_cell,
                                   float_spec=float_spec,
                                   is_list_item=is_list_item)
        self.open_html_blocks.add(html_block)
        return self.current_block

    def start_new_table(self, html_tag, tag_style=None):
        self.current_table = Table(self.namespace, html_tag, tag_style)
        self.tables.append(self.current_table)

    def start_new_row(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_row(html_tag, tag_style)

    def start_new_cell(self, html_tag, tag_style):
        if self.current_table is None:
            self.start_new_table(html_tag)
        self.current_table.start_new_cell(html_tag, tag_style)

    def finish_tag(self, html_tag):
        if self.current_block is not None and html_tag in self.open_html_blocks:
            self.end_current_block()
            self.open_html_blocks.discard(html_tag)

        if self.current_table is not None:
            table_finished = self.current_table.finish_tag(html_tag)
            if table_finished:
                table = self.tables[-1]
                del self.tables[-1]
                if self.tables:
                    self.current_table = self.tables[-1]
                    self.current_table.add_table(table)
                else:
                    self.current_table = None
                    self.block_map[table] = len(self.items)
                    self.items.append(table)

    def serialize(self, body):
        for item in self.items:
            item.serialize(body)

    def delete_block_at(self, pos=None):
        pos = self.pos if pos is None else pos
        block = self.all_blocks[pos]
        del self.all_blocks[pos]
        if self.block_map:
            del self.items[self.block_map.pop(block)]
        else:
            items = self.items if block.parent_items is None else block.parent_items
            items.remove(block)
        block.parent_items = None
        if block.float_spec is not None:
            block.float_spec.blocks.remove(block)
        try:
            self.all_blocks[pos].bookmarks.update(block.bookmarks)
        except (IndexError, KeyError):
            pass

    def __enter__(self):
        self.pos = len(self.all_blocks)
        self.block_map = {}

    def __exit__(self, etype, value, traceback):
        if value is not None:
            return  # Since there was an exception, the data structures are not in a consistent state
        if self.current_block is not None:
            self.all_blocks.append(self.current_block)
        self.current_block = None
        if len(self.all_blocks) > self.pos and self.all_blocks[
                self.pos].is_empty():
            # Delete the empty block corresponding to the <body> tag when the
            # body tag has no inline content before its first sub-block
            self.delete_block_at(self.pos)
        if self.pos > 0 and self.pos < len(self.all_blocks):
            # Insert a page break corresponding to the start of the html file
            self.all_blocks[self.pos].page_break_before = True
        self.block_map = {}