Example #1
0
def builtin2meta(val):
    if isinstance(val, bool):
        return pf.MetaBool(val)
    elif isinstance(val, (float, int)):
        return pf.MetaString(str(val))
    elif isinstance(val, string_types):
        return pf.MetaString(val)
    elif isinstance(val, list):
        return pf.MetaList(*[builtin2meta(x) for x in val])
    elif isinstance(val, dict):
        return pf.MetaMap(*[(k, builtin2meta(v)) for k, v in val.items()])
    elif isinstance(val, pf.Block):
        return pf.MetaBlocks(val)
    elif isinstance(val, pf.Inline):
        return pf.MetaInlines(val)
    elif isinstance(
            val,
        (
            pf.MetaBool,
            pf.MetaString,
            pf.MetaValue,
            pf.MetaList,
            pf.MetaMap,
            pf.MetaBlocks,
            pf.MetaInlines,
        ),
    ):
        return val

    raise TypeError("unknown type: {} (type: {})".format(val, type(val)))
Example #2
0
def resolve_tables(element, doc):
    # type: (Table, Doc) -> None
    if not isinstance(element, (pf.Table)):
        return None

    ref_type = REFTYPE_TABLE

    attributes = None
    if element.caption:  # type: Inline
        # attributes = _find_attribute(element.caption[0],
        #                              allow_any=True, delete_preceding=False)
        attributes = find_attributes(
            element.caption[-1], search_left=True, include_element=True
        )

    if not attributes:
        return None

    # update count
    doc.refcount[ref_type] += 1
    # add to metadata
    doc.metadata["$$references"][attributes["id"]] = pf.MetaMap(
        **{"type": pf.MetaString(ref_type), "number": doc.refcount[ref_type]}
    )
    # remove attribute from caption
    element.caption = [el for el in element.caption if el not in attributes["elements"]]

    # wrap in a div
    return pf.Div(
        element,
        classes=["labelled-{}".format(ref_type)] + attributes["classes"],
        attributes=attributes["attributes"],
        identifier=attributes["id"],
    )
Example #3
0
def test_get_variable():

    doc = pf.Doc(metadata={
        "a": pf.MetaString("x"),
        "b": pf.MetaMap(c=pf.MetaString("y"))
    })

    assert pf.get_option(default="a") == "a"
    assert pf.get_option({"a": 1}, "a") == 1
    assert pf.get_option({"a": None}, "a", default=2) == 2
    assert pf.get_option({"a": None}, "a", doc, "a") == "x"
    assert pf.get_option(doc=doc, doc_tag="b.c") == "y"
Example #4
0
def resolve_equations_images(element, doc):
    # type: (Element, Doc) -> None

    # attribute equations in table captions / definition items?
    if not isinstance(element, get_panflute_containers(pf.Math)):
        return None

    if not element.content:
        return None

    to_delete = set()
    to_wrap = dict()

    subel = element.content[0]

    while subel:  # type: Element

        ref_type = None
        if isinstance(subel, pf.Math):
            ref_type = REFTYPE_MATH
        # elif isinstance(subel, pf.Table):
        #     ref_type = "Table"
        elif isinstance(subel, pf.Image):
            ref_type = REFTYPE_IMAGE
        else:
            subel = subel.next
            continue

        if isinstance(subel, pf.Image) and compare_version('1.16', '>='):
            # pandoc >= 1.16 already supports this
            # TODO for pandoc < 1.16 also look for attributes attached,
            # to the image path, as occurs with image references
            # see https://github.com/tomduck/pandoc-fignos/issues/14
            attributes = {
                "id": subel.identifier,
                # "classes": subel.classes,
                # "attributes": subel.attributes,
                "elements": []
            }

        else:
            attributes = find_attributes(subel)
            if attributes:
                to_wrap[subel] = attributes
                for _ in attributes["elements"]:
                    subel = subel.next

        if attributes and attributes["id"]:
            # update count
            doc.refcount[ref_type] += 1
            # add to metadata
            doc.metadata["$$references"][attributes["id"]] = pf.MetaMap(
                **{
                    "type": pf.MetaString(ref_type),
                    "number": doc.refcount[ref_type]
                })

            to_delete.update(attributes["elements"])

        subel = subel.next

    new_content = [
        pf.Span(el,
                classes=["labelled-{}".format(ref_type)] +
                to_wrap[el]["classes"],
                attributes=to_wrap[el]["attributes"],
                identifier=to_wrap[el]["id"]) if el in to_wrap else el
        for el in element.content if el not in to_delete
    ]

    # if isinstance(element, pf.Plain):
    #     return pf.Plain(*new_content)
    # else:
    #     return pf.Para(*new_content)
    element.content = new_content
    return element
Example #5
0
def prepare(doc):
    # type: (Doc) -> None
    doc.refcount = {"Table": 0, "Image": 0, "Math": 0}
    doc.metadata["$$references"] = pf.MetaMap()