def finalize(doc): """Adds the pgfplots and caption packages to the header-includes if needed. """ if doc.plot_found: pgfplots_inline = pf.MetaInlines(pf.RawInline( r'''% \makeatletter \@ifpackageloaded{pgfplots}{}{\usepackage{pgfplots}} \makeatother \usepgfplotslibrary{groupplots} ''', format='tex')) try: doc.metadata['header-includes'].append(pgfplots_inline) except KeyError: doc.metadata['header-includes'] = pf.MetaList(pgfplots_inline) if doc.caption_found: caption_inline = pf.MetaInlines(pf.RawInline( r'''% \makeatletter \@ifpackageloaded{caption}{}{\usepackage{caption}} \@ifpackageloaded{cleveref}{}{\usepackage{cleveref}} \@ifundefined{codelisting}{% \DeclareCaptionType{codelisting}[Code Listing][List of Code Listings] \crefname{codelisting}{code listing}{code listings} \Crefname{codelisting}{Code Listing}{Code Listings} \captionsetup[codelisting]{position=bottom} }{} \makeatother ''', format='tex')) try: doc.metadata['header-includes'].append(caption_inline) except KeyError: doc.metadata['header-includes'] = pf.MetaList(caption_inline)
def _finalize(doc): logging.debug("Finalize doc!") hdr_inc = "header-includes" # Add header-includes if necessary if "header-includes" not in doc.metadata: if doc.get_metadata("output.beamer_presentation.includes") is None: logging.debug("No 'header-includes' nor `includes` ? Created 'header-includes'!") doc.metadata[hdr_inc] = pf.MetaList() else: logging.ERROR("Found 'includes'! SAD THINK") exit(1) # Convert header-includes to MetaList if necessary logging.debug("Append background packages to `header-includes`") if not isinstance(doc.metadata[hdr_inc], pf.MetaList): logging.debug("The '" + hdr_inc + "' is not a list? Converted!") doc.metadata[hdr_inc] = pf.MetaList(doc.metadata[hdr_inc]) frmt = doc.format if doc.format in ("latex", "beamer"): frmt = "latex" if doc.format in ("tex", "latex", "beamer"): doc.metadata[hdr_inc].append( pf.MetaInlines(pf.RawInline("\\usepackage{xspace}", frmt)) ) doc.metadata[hdr_inc].append( pf.MetaInlines(pf.RawInline("\\usepackage{trimclip}", frmt)) )
def finalize(doc): # Add header-includes if necessary if 'header-includes' not in doc.metadata: doc.metadata['header-includes'] = pf.MetaList() # Convert header-includes to MetaList if necessary elif not isinstance(doc.metadata['header-includes'], pf.MetaList): doc.metadata['header-includes'] = pf.MetaList( doc.metadata['header-includes']) # Add usefull LaTexPackage doc.metadata['header-includes'].append( pf.MetaInlines(pf.RawInline('\\usepackage{pstricks}', 'tex'))) doc.metadata['header-includes'].append( pf.MetaInlines(pf.RawInline('\\usepackage{pst-barcode}', 'tex')))
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 _finalize(doc): logging.debug("Finalize doc!") hdr_inc = "header-includes" # Add header-includes if necessary if "header-includes" not in doc.metadata: if doc.get_metadata("output.beamer_presentation.includes") is None: logging.debug( "No 'header-includes' nor `includes` ? Created 'header-includes'!" ) doc.metadata["header-includes"] = pf.MetaList() else: logging.ERROR("Found 'includes'! SAD THING!") exit(1) # Convert header-includes to MetaList if necessary logging.debug("Append background packages to `header-includes`") if not isinstance(doc.metadata[hdr_inc], pf.MetaList): logging.debug("The '" + hdr_inc + "' is not a list? Converted!") doc.metadata[hdr_inc] = pf.MetaList(doc.metadata[hdr_inc]) doc.metadata[hdr_inc].append( pf.MetaInlines( pf.RawInline( "\\usepackage[pages=some,placement=center,scale=3,angle=45,color=red!55]{background}", "latex")))
def str_to_metainline(raw_str): tokens=raw_str.split(" ") mi=pf.MetaInlines(pf.Str(tokens[0])) i=0 for i in range(1,len(tokens)): mi.content.append(pf.Space) mi.content.append(pf.Str(tokens[i])) return mi
def finalize(doc): if doc.format == 'latex': # tex = [r'\usepackage[toc]{glossaries}', '\makeglossaries'] tex = [] for acronym, definition in doc.acronyms.items(): tex_acronym = TEMPLATE_NEWACRONYM.safe_substitute(acronym=acronym, definition=definition) tex.append(tex_acronym) tex = [pf.MetaInlines(pf.RawInline(line, format='latex')) for line in tex] tex = pf.MetaList(*tex) doc.metadata['acronyms'] = tex
def finalize(doc): if doc.format == 'latex': tex = [r'\makeglossaries'] for acronym, definition in doc.acronyms.items(): tex_acronym = TEMPLATE_NEWACRONYM.safe_substitute( acronym=acronym, definition=definition) tex.append(tex_acronym) tex = [ pf.MetaInlines(pf.RawInline(line, format='latex')) for line in tex ] tex = pf.MetaList(*tex) if 'header-includes' in doc.metadata: doc.metadata['header-includes'].content.extend(tex) else: doc.metadata['header-includes'] = tex
def finalize(doc): if doc.format == 'latex': tex = [r'\makeglossaries'] for _, values in doc.abbrs.items(): tex_acronym = DEFINE_ABBREVIATION.render(**values) tex.append(tex_acronym) for _, values in doc.glsentries.items(): tex_acronym = DEFINE_GLOSSARY_ENTRY.render(**values) tex.append(tex_acronym) tex = [ pf.MetaInlines(pf.RawInline(line, format='latex')) for line in tex ] tex = pf.MetaList(*tex) if 'header-includes' in doc.metadata: doc.metadata['header-includes'].content.extend(tex) else: doc.metadata['header-includes'] = tex
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 prepare(doc): doc.exercisecount = 0 ## Added attribute!! doc.inside_exercise = False doc.questioncount = 0 ## Added attribute!! doc.inside_question = False doc.column_count = 0 doc.columns_width = Dimension("600pt") doc.columns_sep = Dimension("0cm") doc.columns_to_patch = [] doc.prev_column = None doc.enable_traditional_tables = False doc.disable_columns = True doc.custom_counters = {} #print >> sys.stderr, doc.format if doc.api_version == (1, 17, 0, 4): doc.disable_columns = False doc.pandoc_columns = doc.get_metadata('pandoc_columns', default=False, builtin=True) tables = doc.get_metadata('traditional-tables', default=False, builtin=True) doc.autounderlined = doc.get_metadata( 'autounderlined', default=False, builtin=True) and latex_format( doc.format) framed_on = doc.get_metadata('includeframed', default=True, builtin=True) and latex_format(doc.format) doc.embed_pdfnotes = doc.get_metadata( 'embed_pdfnotes', default=False, builtin=True) and doc.format == "beamer" doc.note_counter = 1 if tables: doc.enable_traditional_tables = tables if doc.format == "latex" or doc.format == "beamer": if 'header-includes' in doc.metadata: includes = doc.metadata['header-includes'] else: includes = pf.MetaList([]) doc.metadata['header-includes'] = includes cont = includes.content ## TIKZ cont.append(pf.MetaInlines(pf.RawInline('\n\\usepackage{tikz}', 'tex'))) cont.append( pf.MetaInlines( pf.RawInline( '\n\\usetikzlibrary{calc,backgrounds,arrows,shapes,matrix,fit,patterns,trees,positioning,decorations.pathreplacing,automata}', 'tex'))) cont.append( pf.MetaInlines(pf.RawInline('\n\\usepackage{standalone}', 'tex'))) cont.append( pf.MetaInlines(pf.RawInline('\n\\usepackage{color}', 'tex'))) #Shaded enviroments if framed_on: cont.append( pf.MetaInlines( pf.RawInline('\n\\usepackage{framed,color}', 'tex'))) cont.append( pf.MetaInlines( pf.RawInline('\n\\definecolor{shadecolor}{gray}{0.9}', 'tex'))) cont.append( pf.MetaInlines( pf.RawInline('\n\\definecolor{gray}{rgb}{0.5,0.5,0.5}', 'tex'))) cont.append( pf.MetaInlines(pf.RawInline('\n\\usepackage{framed}', 'tex')))
def _finalize(doc): doc.metadata["custom-automatic-styles"] = pf.MetaInlines( pf.RawInline("\n".join(doc.auto_styles), format="opendocument")) doc.metadata["sequence-decls"] = pf.MetaInlines( pf.RawInline("\n".join(doc.sequence_decls), format="opendocument"))
def __append_header_includes(rawstr, frmt): logging.debug(f"Append line '{rawstr}' to `header-includes`") if not rawstr in doc.get_metadata("header-includes"): doc.metadata[hdr_inc].append( pf.MetaInlines(pf.RawInline(rawstr, frmt)))
def prepare(doc): ##Load defaults lang_dict={} lang_dict["lang1"]="SP" lang_dict["lang2"]="EN" lang_avail=["SP","EN"] lang_id=1 ## Fits lang1=doc.get_metadata('lang1') lang2=doc.get_metadata('lang2') ## See if the user specified languages other than default if lang1!=None: lang_dict["lang1"]=lang1 lang_avail[0]=lang1 if lang2!=None: lang_dict["lang2"]=lang2 lang_avail[1]=lang2 ## Check user's choice lang_enabled=doc.get_metadata('lang_enabled') if lang_enabled!=None: if lang_enabled=="1": doc.lang_str=lang_dict["lang1"] doc.lang_id=1 elif lang_enabled=="2": doc.lang_str=lang_dict["lang2"] doc.lang_id=2 else: try: idx=lang_avail.index(lang_enabled) except: print(lang_enabled,"key not found in lang list", file=sys.stderr) exit(1) doc.lang_str=lang_enabled doc.lang_id=idx+1 else: doc.lang_str=lang_avail[0] doc.lang_id=1 ## Tags to include/exclude stuff doc.include_begin="BEGIN-"+doc.lang_str doc.include_end="END-"+doc.lang_str if doc.lang_id==1: tag=lang_dict["lang2"] else: tag=lang_dict["lang1"] ## Define some variables automatically (pandoc-crossref bridge code) default_vars(doc) ## Deal with variable translation dual_vars(doc) doc.exclude_begin="BEGIN-"+tag doc.exclude_end="END-"+tag ## Internal parameter doc.remove_component=False ## Include for tex ### IMPORTANT NOTICE: if the -H option of pandoc is used in the ### Command line, then this will not take any effect whatsoever if doc.format=="latex" or doc.format=="beamer": if 'header-includes' in doc.metadata: includes=doc.metadata['header-includes'] #pf.debug(type(includes)) else: includes=pf.MetaList([]) doc.metadata['header-includes']=includes cont=includes.content #for c in includes.content: # pf.debug(c) #return cont.append(pf.MetaInlines(pf.RawInline('\n\\usepackage{comment}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\includecomment{in'+doc.lang_str+'}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\excludecomment{in'+tag+'}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\newcommand{\\dtext}[2]{#'+str(doc.lang_id)+'}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\newcommand{\\dcode}[2]{\\textcolor{NavyBlue}{#'+str(doc.lang_id)+'}}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\usepackage{tikz}','tex'))) cont.append(pf.MetaInlines(pf.RawInline('\n\\usetikzlibrary{calc,backgrounds,arrows,shapes,matrix,fit,patterns,trees,positioning,decorations.pathreplacing,automata}','tex')))