def add_eq_number_and_id(fmt, value, num, labelName): num = str(num) pre = pf.RawInline('markdown', "\n\n") post = pf.RawInline('markdown', "\n\n") val = re.sub(r'\\label\{(.*?)\}', "", value[1]) val = re.sub(r'\{split\}', "{aligned}", val) equation = pf.RawInline('markdown', f"$$\n {val} \\tag{{{num}}} \n$$") return [pre, equation, post]
def convert_internal_refs(self, key, value, format, metadata): """Convert all internal links from '#blah' into format specified in self.replacements. """ if key != 'Cite': return None citations, inlines = value if len(citations) > 1: ''' Note: Need to check that *all* of the citations in a multicitation are in the reference list. If not, the citation is bibliographic, and we want LaTeX to handle it, so just return unmodified. ''' for citation in citations: if citation['citationId'] not in self.references: return return self.convert_multiref(key, value, format, metadata) else: citation = citations[0] prefix = pf.stringify(citation['citationPrefix']) suffix = pf.stringify(citation['citationSuffix']) if prefix: prefix += ' ' label = citation['citationId'] if label not in self.references: return rtype = self.references[label]['type'] n = self.references[label]['id'] text = self.replacements[rtype].format(n) if format == 'latex' and self.autoref: link = u'{pre}\\autoref{{{label}}}{post}'.format(pre=prefix, label=label, post=suffix) return pf.RawInline('latex', link) elif format == 'latex' and not self.autoref: link = u'{pre}\\ref{{{label}}}{post}'.format(pre=prefix, label=label, post=suffix) return pf.RawInline('latex', link) else: link_text = '{}{}{}'.format(prefix, text, suffix) link = pf.Link([pf.Str(link_text)], ('#' + label, '')) return link
def html_dashes(key, value, fmt, meta): def fix_dash(s): s = re.sub('([^ ])--([^ ])', '\\1–\\2', s) s = s.replace('--', '—') return s if key == 'Str' and fmt == 'html': return pf.RawInline(fmt, fix_dash(value)) elif key == 'RawInline' and fmt == 'html': return pf.RawInline(value[0], fix_dash(value[1])) else: return None
def convert_internal_refs(self, key, value, format, metadata): """Convert all internal links from '#blah' into format specified in self.replacements. """ if key != 'Cite': return None citations, inlines = value if len(citations) > 1: ''' Note: Need to check that *all* of the citations in a multicitation are in the reference list. If not, the citation is bibliographic, and we want LaTeX to handle it, so just return unmodified. ''' for citation in citations: if citation['citationId'] not in self.references: return return self.convert_multiref(key, value, format, metadata) else: citation = citations[0] prefix = citation['citationPrefix'] suffix = citation['citationSuffix'] label = citation['citationId'] if label not in self.references: return if prefix: prefix += [pf.Space()] rtype = self.references[label]['type'] n = self.references[label]['id'] text = self.replacements[rtype].format(n) if format in ['latex', 'beamer'] and self.autoref: link = pf.RawInline('latex', '\\cref{{{label}}}'.format(label=label)) return prefix + [link] + suffix elif format in ['latex', 'beamer'] and not self.autoref: link = pf.RawInline('latex', '\\ref{{{label}}}'.format(label=label)) return prefix + [link] + suffix else: link = pf.Link(["", [], []], [pf.Str(text)], ('#' + label, '')) return prefix + [link] + suffix
def add_eq_number_and_id(fmt, value, num, labelName): num = str(num) outer = pf.RawInline( 'html', f'<span id="{labelName}" style="display: inline-block; position: relative; width: 100%;">' ) inner = pf.RawInline( 'html', '<span style="position: absolute; right: 0em; top: 50%; line-height: 0; ">' ) eqno = pf.Str('(%s)' % num) endtags = pf.RawInline('html', '</span></span>') return [outer, elt('Math', 2)(*value), inner, eqno, endtags]
def RawInline(format, string): """Overwrite pandocfilters RawInline so that html5 and html raw output both use the html writer. """ if format == 'html5': format = 'html' return pf.RawInline(format, string)
def suit_symbols(key, value, fmt, meta): if key == 'Str': abbrevs = ['!C', '!D', '!H', '!S', '!N'] orig_value = value if fmt != 'latex': suit_html = ['<font color=green>♣</font>', '<font color=orange>♦</font>', '<font color=red>♥</font>', '<font color=blue>♠</font>', 'N'] suit_other = ['C', 'D', 'H', 'S', 'N'] suit_symbols = suit_html if fmt == 'html' else suit_other for (abbrev, symbol) in zip(abbrevs, suit_symbols): value = value.replace(abbrev, symbol) else: # LaTeX needs special handling (for mbox) value = value.replace('!N', 'N') suit = ['\\clubs{\\1}{\\2}', '\\diamonds{\\1}{\\2}', '\\hearts{\\1}{\\2}', '\\spades{\\1}{\\2}'] for (abbrev, symbol) in zip(abbrevs, suit): value = re.sub('([a-zA-Z0-9]*)%s([a-zA-Z0-9]*)' % abbrev, symbol, value) if (fmt == 'html' or fmt == 'latex') and value != orig_value: return pf.RawInline(fmt, value) return pf.Str(value) else: return None
def formatCode(key, value, format, meta): style = 'default' if key == 'CodeBlock': if value[0][1]: return pf.Plain([ pf.RawInline( "html", highlightCode(value[1], style=style, language=value[0][1][0])) ]) else: return pf.Plain([ pf.RawInline( "html", highlightCode(value[1], style=style, language='text')) ])
def applyLabelMapToText(key, value, format, meta): global labelMap if key == 'Link': eprint(value) if value[0][2] != []: if value[0][2][1][1] in labelMap: return [ pf.RawInline('markdown', str(labelMap[value[0][2][1][1]])) ]
def raw_equations(key, value, format, metadata): """Pass latex contained in $$...$$ to latex writer as raw latex *if* the latex starts with 'begin'. """ if format == 'latex' and key == 'Math' and value[0]['t'] == 'DisplayMath': mathtype, content = value if content.strip().startswith('\\begin{'): # n.b, has to be rawinline as the element we are replacing # is inline return pf.RawInline('latex', content)
def handle_pdf(key, value, format, meta): assert format == 'html' if key == 'Link': attr, inlines, target = value url, title = target src, _, ratio = url.rpartition('::') _, ext = splitext(src) if ext == '.pdf' and ratio in ('16by9', '4by3'): src = media(src) return pf.RawInline( format, dedent(f'''\ <div class="embed embed-{ratio}"> <iframe src="{src}" type="application/pdf" allowfullscreen> <a href="{src}">{src}</a> </iframe> </div> '''))
def katex(key, value, format, meta): if key != "Math": return None fmt, code = value if not isinstance(fmt, dict) and 't' not in fmt: return None display = fmt['t'] == 'DisplayMath' call = subprocess.run( [ 'npx', 'katex', '--no-throw-on-error', '--display-mode' if display else '' ], input=code, text=True, capture_output=True, check=True, ) return pandocfilters.RawInline( 'html', f'<span class="math {"display" if display else "inline"}" ' f'data-latex-input="{html.escape(code)}">{call.stdout}</span>', )
def li(s): return pf.RawInline('latex', s)
def inlatex(s): return pf.RawInline('latex', s)
def latex_inline(s): return pandocfilters.RawInline('latex', s)
def inlatex(s): return pf.RawInline("latex", s)