ipypublish.preprocessors.latextags_to_html module

class ipypublish.preprocessors.latextags_to_html.DefaultFormatter(default='')[source]

Bases: string.Formatter

get_value(key, args, kwds)[source]
class ipypublish.preprocessors.latextags_to_html.LatexTagsToHTML(*args, **kwargs)[source]

Bases: nbconvert.preprocessors.base.Preprocessor

a preprocessor to find latex tags (like \cite{abc} or \todo[color]{stuff}) and:

  1. attempt to process them into a html friendly format

  2. remove them entirely if this is not possible

for \ref or \cref, attempts to use resources.refmap to map labels to reference names for labels not found in resources.refmap the reference name is ‘<name> <number>’, where:

  • <name> is either ref of, if labelbycolon is True and the label has a colon, all text before the colon

  • <number> iterate by order of first appearance of a particular label

NB: should be applied after LatexDocHTML, if you want resources.refmap to be available

Examples

>>> from nbformat import NotebookNode
>>> from jsonextended.utils import MockPath
>>> processor = LatexTagsToHTML()
>>> bibfile = MockPath(is_file=True,content='''
... @article{bibkey,
... title = {the title},
... doi = {10.1134/S0018143916050209},
... author = {Surname, A. Name},
... date = {2016-09-01},
... }
... ''')
>>> resources = NotebookNode(
...    {'bibliopath':bibfile, 'refmap':{"label":"label_name"}})
>>> cell = NotebookNode({
... "cell_type":"markdown",
... "metadata":{},
... "source":"test"
... })
>>> nb = NotebookNode({"cells":[cell]})
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
test
>>> cell.source = "\\unknown{test}"
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
>>> cell.source = "\\ref{label}\\unknown{test}"
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
<a href="{id_home_prefix}label">label_name</a>
>>> cell.source = "\\label{test}"
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
<a id="test" class="anchor-link" name="#test">&#182;</a>
>>> cell.source = "\\cite{bibkey}"
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
[<a href="https://doi.org/10.1134/S0018143916050209">Surname <em>et al</em>, 2016.</a>]
>>> cell.source = "\\begin{equation}x=a+b\\end{equation}"
>>> nb, _ = processor.preprocess(nb,resources)
>>> print(nb.cells[0].source)
\begin{equation}x=a+b\end{equation}
bibformat

A trait for unicode strings.

convert(source, resources)[source]

convert a a string with tags in

Example

>>> source = r'''
... References to \cref{fig:example}, \cref{tbl:example}, \cref{eqn:example_sympy} and \cref{code:example_mpl}.
...
... Referencing multiple items: \cref{fig:example,fig:example_h,fig:example_v}.
...
... An unknown latex tag.\unknown{zelenyak_molecular_2016}
... '''
>>> processor = LatexTagsToHTML()
>>> print(processor.convert(source,{}))

References to <a href="{id_home_prefix}fig:example">fig. 1</a>, <a href="{id_home_prefix}tbl:example">tbl. 1</a>, <a href="{id_home_prefix}eqn:example_sympy">eqn. 1</a> and <a href="{id_home_prefix}code:example_mpl">code. 1</a>.

Referencing multiple items: <a href="{id_home_prefix}fig:example">fig. 1</a>, <a href="{id_home_prefix}fig:example_h">fig. 2</a> and <a href="{id_home_prefix}fig:example_v">fig. 3</a>.

An unknown latex tag.
labelbycolon

A boolean (True, False) trait.

preprocess(nb, resources)[source]

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

process_bib_entry(entry)[source]

work out the best way to represent the bib entry

static read_bibliography(path)[source]

read a bibliography

regex

A trait for unicode strings.

replace_reflabel(name, resources)[source]

find a suitable html replacement for a reference label

the links are left with a format hook in them: {id_home_prefix}, so that an nbconvert filter can later replace it this is particularly useful for slides, which require a prefix #/<slide_number><label>

rreplace(source, target, replacement, replacements=1)[source]

replace in string, from right-to-left

ipypublish.preprocessors.latextags_to_html.safe_str(obj)[source]