ipypublish.preprocessors.latextags_to_html module¶
Bases:
string.Formatter
Bases:
nbconvert.preprocessors.base.Preprocessor
a preprocessor to find latex tags (like
\cite{abc}
or\todo[color]{stuff}
) and:attempt to process them into a html friendly format
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">¶</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}
A trait for unicode strings.
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.
A boolean (True, False) trait.
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 convertedresources (
dictionary
) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.
work out the best way to represent the bib entry
read a bibliography
A trait for unicode strings.
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>
replace in string, from right-to-left