1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
\section{Plugins}
\label{sec:plugins}
As explained in Chapter~\ref{sec:command-line}, one can use command line
options of configuration files to use extra packages or templates or even a
full custom renderer.
However this is not convenient if you want use the same extras in several
projects, or want to distribute them. In order to do that, you can create a
\plasTeX\ plugin. Such a plugin is simply a python package that contains
one or several subpackages named \file{templates}, \file{Packages} or
\file{Renderers}.
In this section, we describe some minimalistic plugins demonstrating the
possibilities offered. For clarity we will separate the three kinds of plugin content
into three plugins but of course a sophisticated plugin can combine several kinds.
Any kind of python packaging can be used as long as it allows to put the plugin code into
the Python search path and can bundle Python code with some other files, typically template files.
In this documentation we will use \texttt{setuptools} and put all configuration into
a \file{pyproject.toml} file which is essentially the same for all three examples,
differing only in the name and description metadata.
\begin{lstlisting}
[project]
name = "my_wonderful_plastex_plugin"
version = "1.0.0"
description = "A nice plasTeX plugin."
requires-python = ">=3.7"
dependencies = ["plastex>=3.1"]
[tool.setuptools.package-data]
plastex_markdown_renderer = ["**/*.jinja2", "**/*.jinja2s"]
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
\end{lstlisting}
Note in particular the line about packaging Jinja files together with python files. Of course
you can add CSS file of javascript files or any other kind of required file.
Note also that you will need a bit more metadata if you want to upload your
plugin to pypi for instance.
You can then install this python package by running \verb+pip install .+ from the folder
containing that \file{pyproject.toml}. Then you can compile a file \file{text.tex} using your plugin by running
\begin{verbatim}
plastex --plugins my_wonderful_plastex_plugin -- test.tex
\end{verbatim}
In the above command, the double-dash before the file name is needed because
the \longprogramopt{plugins} options takes a list of space separated plugins.
\subsection{Templates plugin}%
\label{sub:templates-plugin}
The simplest kind of plugin is useful when you need to reuse custom templates. Say you do not like the
HTML5 template for quotes and often want to use the same custom template. You can create a folder with the
following structure.
\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[my_templates_plugin
[pyproject.toml]
[src
[plastex_div_quote
[__init__.py]
[templates
[Quote.jinja2]
]
]
]
]
\end{forest}
The \file{pyproject.toml} was already explained and the \file{__init__.py} can be empty in this case.
In this case the plugin name is \texttt{plastex_div_quote}. The content of \file{Quote.jinja2} could be:
\begin{lstlisting}[language=HTML]
name: quote
<div class="quote">{{ obj }}</div>
\end{lstlisting}
\subsection{Package plugin}%
\label{sub:package-plugin}
For a slighly more complicated example, assume that you have implemented a
python version of some package that is not natively supported by \plasTeX, or
you don't like the available implementation and, for some reason, contributing
your implementation to \plasTeX is not possible. You can use the
\longprogramopt{packages-dir} option to use your implementation, but this is
not convenient if you want to distribute it. So you can create a folder with
the following structure
\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[my_package_plugin
[pyproject.toml]
[src
[plastex_foo_package
[__init__.py]
[Packages
[foo.py]
]
[templates
[Foo.jinja2]
]
]
]
]
\end{forest}
The \file{pyproject.toml} was already explained and the \file{__init__.py} can be empty in this case.
In this case the plugin name is \texttt{plastex_foo_package}. The content of \file{foo.py} could be:
\begin{lstlisting}[language=python]
from plasTeX import Command
class foo(Command):
pass
\end{lstlisting}
and the content of \file{Foo.jinja2} could be:
\begin{lstlisting}[language=HTML]
name: foo
Foo!
\end{lstlisting}
When compiling a file using this plugin, you can put \verb+\usepackage{foo}+ in the preamble and
then use the \verb+\foo+ command that will be rendered as ``Foo!'' with any template based renderer
(for instance the default HTML5 renderer).
\subsection{Renderer plugin}%
\label{sub:renderer-plugin}
For our final example, we build a plugin which brings a new renderer. We will use as an
example a renderer targetting Markdown. This is slightly silly since markdown itself is a source code
format, and much cruder that \LaTeX, but this is only an example.
\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[my_renderer_plugin
[pyproject.toml]
[src
[plastex_markdown_renderer
[__init__.py]
[Renderers
[Markdown
[__init__.py]
[Math.jinja2s]
[Sectioning.jinja2s]
[Text.jinja2s]
[Themes
[default
[default-layout.jinja2]
]
]
]
]]]]
\end{forest}
The \file{__init__.py} in \file{plastex_markdown_renderer} is empty but the one in
\file{Markdown} contains
\begin{lstlisting}[language=python]
from plasTeX.Renderers.PageTemplate import Renderer as PTRenderer
class Renderer(PTRenderer):
"""Renderer targetting Markdown using templates."""
fileExtension = '.md'
\end{lstlisting}
which simply declares a page template based renderer which will produce files with extension \file{.md}.
The various files with \file{.jinja2s} extension contain the following code split into three files for convenience:
\begin{lstlisting}
name: math displaymath
{{ obj.mathjax_source }}
name: document
{{ obj }}
name: section
# {{ obj.fullTitle }}
{{ obj }}
name: subsection
## {{ obj.fullTitle }}
{{ obj }}
name: par
{{ obj }}
name: emph em
*{{ obj }}*
name: itshape textit
*{{ obj }}*
name: bfseries textbf
**{{ obj }}**
name: ttfamily texttt
`{{ obj }}`
\end{lstlisting}
and the file \file{default-layout.jinja2} simply contains one line saying
\verb+{{ obj }}+. This will allow to run
\begin{verbatim}
plastex --plugins plastex_markdown_renderer --renderer Markdown test.tex
\end{verbatim}
to render a tiny subset of \LaTeX\ to markdown.
|