def prepare(doc: panflute.Doc) -> None: """Prepare the document If ``acronyms`` map is in the metadata, generate the LaTeX definitions of the acronyms and count the number of uses of the acronyms in the document. These details are to be used by the writer or the main filter. """ if "acronyms" not in doc.metadata: return # Store the acronym information as an attribute of the document doc.acronyms = PandocAcro(doc.get_metadata("acronyms")) # Prepare the LaTeX details. header = doc.metadata["header-includes"] \ if "header-includes" in doc.metadata else [] LaTeX = lambda l: panflute.MetaInlines( # noqa E731 I just want a short name panflute.RawInline(l, format="latex")) header.append(LaTeX(r"\usepackage{acro}")) if doc.acronyms.options: header.append(LaTeX(options.acsetup(doc.acronyms.options))) for key, values in doc.acronyms.items(): header.append(LaTeX(fr"\DeclareAcronym{{{key}}}{{")) # The short key *must be first*! header.append(LaTeX(f"short = {values['short']},\n")) header.append( LaTeX(",\n".join(f"{k} = {v}" for k, v in sorted(values.items()) if k != "short"))) header.append(LaTeX("}")) doc.acronyms[key]["count"] = 0 doc.acronyms[key]["total"] = 0 doc.acronyms[key]["list"] = False doc.metadata["header-includes"] = header # For other outputs, we'll need to tally use of the acronyms doc.walk(keys.count) return
def process_citations(doc: pf.Doc) -> None: """ Apply citation-by-identifier to a Python object representation of Pandoc's Abstract Syntax Tree. """ # process metadata.manubot-bibliography-cache bib_cache = doc.get_metadata(key="manubot-bibliography-cache") if not (bib_cache is None or isinstance(bib_cache, str)): logging.warning( f"Expected metadata.manubot-bibliography-cache to be a string or null (None), " f"but received a {bib_cache.__class__.__name__}. Setting to None.") bib_cache = None doc.manubot["bibliography_cache"] = bib_cache # process metadata.citekey-aliases citekey_aliases = doc.get_metadata("citekey-aliases", default={}) if not isinstance(citekey_aliases, dict): logging.warning( f"Expected metadata.citekey-aliases to be a dict, " f"but received a {citekey_aliases.__class__.__name__}. Disregarding." ) citekey_aliases = dict() doc.manubot["citekey_aliases"] = citekey_aliases doc.walk(_get_reference_link_citekey_aliases) doc.walk(_get_citekeys_action) manuscript_citekeys = doc.manubot["manuscript_citekeys"] citations = Citations(input_ids=manuscript_citekeys, aliases=citekey_aliases) citations.csl_item_failure_log_level = "ERROR" requests_cache_path = doc.get_metadata("manubot-requests-cache-path") if requests_cache_path: from manubot.process.requests_cache import RequestsCache req_cache = RequestsCache(requests_cache_path) req_cache.mkdir() req_cache.install() if doc.get_metadata("manubot-clear-requests-cache", default=False): req_cache.clear() citations.filter_pandoc_xnos() citations.load_manual_references(**_get_load_manual_references_kwargs(doc)) citations.inspect(log_level="WARNING") citations.get_csl_items() doc.manubot["citekey_shortener"] = citations.input_to_csl_id doc.walk(_citation_to_id_action) if requests_cache_path: req_cache.close() citations.write_citekeys_tsv( path=doc.get_metadata("manubot-output-citekeys")) citations.write_csl_items( path=doc.get_metadata("manubot-output-bibliography")) citations.write_csl_items(path=doc.manubot["bibliography_cache"]) # Update pandoc metadata with fields that this filter # has either consumed, created, or modified. doc.metadata["bibliography"] = [] doc.metadata["references"] = citations.csl_items doc.metadata["citekey_aliases"] = citekey_aliases