Esempio n. 1
0
def table_matrix_to_pf(matrix, doc):
    options = matrix[0]
    t_kwargs = options
    caption = matrix[1]
    table = matrix[2]
    footnotes = [i for i in list_to_elems([matrix[3]])]

    row_cnt = len(table)

    rows = []
    new_col_cnt = 0
    old_col_cnt = None

    for r, row in enumerate(table):
        cells = []
        r_kwargs = row[1]

        for c, cell in enumerate(row[0]):
            if isinstance(cell, tuple):
                c_args = cell[0]
                c_kwargs = cell[1]

                col_span = utils.check_type(c_kwargs.get("col_span", 1), int)

                repeat = 1

                if "repeat" in c_kwargs:
                    repeat = utils.check_type(c_kwargs.get("repeat", 1), int)
                    del c_kwargs["repeat"]

                for _ in range(repeat):
                    new_col_cnt += 1
                    cells.append(TableCell(*list_to_elems(c_args), **c_kwargs))

                    for i in range(1, col_span):
                        new_col_cnt += 1
                        cells.append(TableCell(pf.Null(), covered=True))
            else:
                new_col_cnt += 1
                cells.append(TableCell(*list_to_elems([cell])))

        if old_col_cnt is None:
            old_col_cnt = new_col_cnt

        if new_col_cnt != old_col_cnt:
            raise IndexError(f"Expected {old_col_cnt} columns "
                             f"but got {new_col_cnt} in {row}")

        new_col_cnt = 0

        rows.append(TableRow(*cells, **r_kwargs))

    if caption:
        t_kwargs["caption"] = [pf.Span(pf.Str(caption))]

    return pf.Div(
        Table(*rows, col_cnt=old_col_cnt, row_cnt=row_cnt, **t_kwargs),
        *footnotes,
        classes=["custom_table"],
    )
Esempio n. 2
0
    def __init__(self,
                 *args,
                 col_cnt=None,
                 row_cnt=None,
                 total_width=0.8,
                 **kwargs):
        super().__init__(*args, **kwargs)

        self._col_cnt = utils.check_type(col_cnt, int)
        self._row_cnt = utils.check_type(row_cnt, int)
        self.total_width = utils.check_type(total_width, float)
Esempio n. 3
0
    def __init__(self,
                 *args,
                 underlines=None,
                 top_space=False,
                 btm_space=False,
                 **kwargs):

        super().__init__(*args, **kwargs)

        self.underlines = ([] if underlines is None else pf.utils.check_type(
            underlines, list))
        self.top_space = utils.check_type(top_space, bool)
        self.btm_space = utils.check_type(btm_space, bool)
Esempio n. 4
0
def xml_to_table_matrix(xml_input):
    table_soup = BeautifulSoup(xml_input, "lxml")
    table_base = table_soup.html.body
    options = {}

    if table_base.options:
        if len(table_base.find_all("options")) > 1:
            raise ValueError("There should only be one set of options.")
        options = table_base.options.attrs
        width = options.get("width", None)

        if width is not None:
            width = [float(w) for w in width.split(",")]
        options["width"] = width

    caption = ""

    if table_base.caption:
        if len(table_base.find_all("caption")) > 1:
            raise ValueError("There should only be one caption.")
        caption = "".join(str(c) for c in table_base.caption.contents)

    rows = []

    for row in table_base.find_all("row"):
        row_attributes = row.attrs
        row_attributes["underlines"] = [
            tuple(utils.check_type(i, int) for i in u.strip().split("-"))
            for u in row_attributes.get("underlines", "").split(",") if u
        ]

        cells = []

        for cell in row.find_all("cell"):
            cell_attributes = cell.attrs
            cell_content = "".join(str(c) for c in cell.contents)

            if cell_attributes:
                cells.append(((cell_content, ), cell_attributes))
            else:
                cells.append(cell_content)

        rows.append([tuple(cells), row_attributes])

    footnotes = ""

    if table_base.footnotes:
        if len(table_base.find_all("footnotes")) > 1:
            raise ValueError("There should only be one footnote definition.")

        footnotes = "".join(str(c) for c in table_base.footnotes.contents)

    return [options, caption, rows, footnotes]
Esempio n. 5
0
    def __init__(
        self,
        *args,
        col_span=1,
        row_span=1,
        covered=False,
        rm_horizontal_margins=False,
        vertical=False,
        heading=0,
        margin_left=False,
        **kwargs,
    ):
        super().__init__(*args, **kwargs)

        self.col_span = utils.check_type(col_span, int)
        self.row_span = utils.check_type(row_span, int)
        self.covered = utils.check_type(covered, bool)
        self.rm_horizontal_margins = utils.check_type(rm_horizontal_margins,
                                                      bool)
        self.vertical = utils.check_type(vertical, bool)
        self.heading = utils.check_type(heading, int)
        self.margin_left = utils.check_type(margin_left, bool)