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)))
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"
def finalize(doc): """Function called at the end of the filter""" doc.metadata["compiletime"] = pf.MetaString( time.strftime("%m/%d/%Y %H:%M:%S")) if doc.format == "html": dumpyaml(doc.toc, "toc.yaml") writetoc(doc, tochtml(doc.toc, doc.get_metadata("booktitle", ""))) if doc.get_metadata("indexpage", False): indexhtml(doc) file = doc.get_metadata("filename", "") title = doc.get_metadata("title", "") searchindex = doc.get_metadata("searchindex", "") logstring("Search index " + file + "," + title + "," + searchindex, doc) if file and title and searchindex: t = re.sub(r"\\\w+", " ", doc.searchtext.lower()) text = re.sub("[^a-zA-Z -]", "", t) updateindex(searchindex, file + ".html", title, text, doc) if doc.format == "html" and doc.footnotecontents: logstring("inserting " + str(doc.footnotecontents), doc) L = ([pf.RawBlock("<ol>", format="html")] + doc.footnotecontents + [pf.RawBlock("</ol>", format="html")]) footnotes = pf.Div(*L, identifier="footnotediv", classes=["footnotes"]) doc.content.append(footnotes) doc.logfile.close()
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"], )
def handle_msetsubject(self, cmd_args, elem): r"""Handle ``\MSetSubject{}`` command. Command defines the category. """ elem.doc.metadata["subject"] = pf.MetaString( MINTMOD_SUBJECTS[cmd_args[0]]) return []
def handle_msubject(self, cmd_args, elem): r"""Handle ``\MSubject{title}`` command. Command defines the document title. """ if not hasattr(elem.doc.metadata, "title"): elem.doc.metadata["title"] = pf.MetaString(cmd_args[0]) return create_header(cmd_args[0], level=1, doc=elem.doc)
def pdflink(doc): """Link to the PDF version of the document""" pdfbase = doc.get_metadata("binarybaseurl", "") file = pdfbase + "/" + doc.get_metadata("filename", "nofile") + ".pdf" HTML = ( "\n" + fr"""<div><p style="color:#871640;">★ See also the <a id="pdflink" href='{file}'><b>PDF version of this chapter</b></a> (better formatting/references) ★</p></div>""" + "\n") doc.metadata["pdffile"] = pf.MetaString(file) doc.metadata["pdflink"] = pf.MetaBlocks(pf.RawBlock(HTML, format="html"))
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