diff options
author | Vivien Kraus <vivien@planete-kraus.eu> | 2021-04-15 14:43:18 +0200 |
---|---|---|
committer | Vivien Kraus <vivien@planete-kraus.eu> | 2021-04-15 14:43:18 +0200 |
commit | d3d710e3525853de849a5b14984da53a3925109e (patch) | |
tree | f26632bd1afd5a4857e06b5a041032ab0a870bb0 /latex |
First commit1.0.0
Diffstat (limited to 'latex')
-rw-r--r-- | latex/algorithm.xsl | 59 | ||||
-rw-r--r-- | latex/escape.xsl | 158 | ||||
-rw-r--r-- | latex/markup.xsl | 337 |
3 files changed, 554 insertions, 0 deletions
diff --git a/latex/algorithm.xsl b/latex/algorithm.xsl new file mode 100644 index 0000000..92f08ae --- /dev/null +++ b/latex/algorithm.xsl @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="utf-8"?> +<xsl:stylesheet + version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:alg="http://h4sp.planete-kraus.eu/ns/algorithm"> + + <xsl:import href="escape.xsl" /> + <xsl:import href="markup.xsl" /> + + <xsl:output method="text"/> + + <xsl:template match="alg:algorithmic"> + <xsl:text>\begin{algorithmic}
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{algorithmic}
</xsl:text> + </xsl:template> + + <xsl:template match="alg:donnée"> + <xsl:text>\State \textbf{Donnée~:} </xsl:text> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> + + <xsl:template match="alg:hyperparamètre"> + <xsl:text>\State \textbf{Hyperparamètre~:} </xsl:text> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> + + <xsl:template match="alg:modèle"> + <xsl:text>\State \textbf{Modèle~:} </xsl:text> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> + + <xsl:template match="alg:résultat"> + <xsl:text>\State \textbf{Résultat~:} </xsl:text> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> + + <xsl:template match="alg:state[count(ancestor::alg:state) = 0]" mode="state-index"> + <xsl:value-of select="1 + count(preceding-sibling::alg:state)" /> + </xsl:template> + + <xsl:template match="alg:state[count(ancestor::alg:state) = 1]" mode="state-index"> + <xsl:apply-templates mode="state-index" select="ancestor::alg:state" /> + <xsl:text>.</xsl:text> + <xsl:value-of select="substring('abcdefghijklmnopqrstuvwxyz', 1 + count(preceding-sibling::alg:state), 1)" /> + </xsl:template> + + <xsl:template match="alg:state"> + <xsl:text>\State </xsl:text> + <xsl:apply-templates mode="state-index" select="." /> + <xsl:text>. </xsl:text> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> +</xsl:stylesheet> diff --git a/latex/escape.xsl b/latex/escape.xsl new file mode 100644 index 0000000..309a393 --- /dev/null +++ b/latex/escape.xsl @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="utf-8"?> +<xsl:stylesheet + version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> + + <xsl:output method="text"/> + + <!-- In this table, the odd-numbered elements are the unicode + characters that need to be replaced, and the even-numbered + elements are the replacement strings. No space is allowed + within the replacement string! --> + <xsl:param name="patterns-table"> + & \& + % \% + \ \textbackslash{} + è \`{e} + é \'{e} + É \'{E} + à \`{a} + î \^{i} + ’ ' + ~ <!-- non-breaking space --> + « \og{} + » \fg{} + ν $\nu$ + </xsl:param> + + <!-- Set to 'true' to print the trace of replacements --> + <xsl:param name="trace-escape" select="'false'" /> + + <xsl:template name="escape-latex-char-try-one"> + <xsl:param name="char" /> + <xsl:param name="patterns" /> + <xsl:if test="$trace-escape = 'true'"> + <xsl:message> + <xsl:text> Try to reduce '</xsl:text> + <xsl:value-of select="$char" /> + <xsl:text>': '</xsl:text> + <xsl:value-of select="$patterns" /> + <xsl:text>'...
</xsl:text> + </xsl:message> + </xsl:if> + <xsl:choose> + <xsl:when test="$patterns = ''"> + <xsl:if test="$trace-escape = 'true'"> + <xsl:message> + <xsl:text> No more patterns to try.
</xsl:text> + </xsl:message> + </xsl:if> + <xsl:value-of select="$char" /> + </xsl:when> + <xsl:otherwise> + <xsl:variable name="pattern" + select="substring-before($patterns, ' ')" /> + <xsl:variable name="replacement" + select="substring-before(substring-after($patterns, ' '), ' ')" /> + <xsl:variable name="other-patterns" + select="substring-after(substring-after($patterns, ' '), ' ')" /> + <xsl:choose> + <xsl:when test="$char = $pattern"> + <xsl:if test="$trace-escape = 'true'"> + <xsl:message> + <xsl:text> Matching pattern found, producing '</xsl:text> + <xsl:value-of select="$replacement" /> + <xsl:text>'
</xsl:text> + </xsl:message> + </xsl:if> + <xsl:value-of select="$replacement" /> + </xsl:when> + <xsl:otherwise> + <xsl:call-template name="escape-latex-char-try-one"> + <xsl:with-param name="char" select="$char" /> + <xsl:with-param name="patterns" select="$other-patterns" /> + </xsl:call-template> + </xsl:otherwise> + </xsl:choose> + </xsl:otherwise> + </xsl:choose> + </xsl:template> + + <xsl:template name="escape-latex-char"> + <xsl:param name="char" /> + <xsl:variable name="patterns"> + <xsl:value-of select="normalize-space($patterns-table)" /> + <xsl:text> </xsl:text> <!-- Otherwise, the last pattern won’t produce anything --> + </xsl:variable> + <xsl:if test="$trace-escape = 'true'"> + <xsl:message> + <xsl:text>Escaping character '</xsl:text> + <xsl:value-of select="$char" /> + <xsl:text>'...
</xsl:text> + </xsl:message> + </xsl:if> + <xsl:call-template name="escape-latex-char-try-one"> + <xsl:with-param name="char" select="$char" /> + <xsl:with-param name="patterns" select="$patterns" /> + </xsl:call-template> + </xsl:template> + + <xsl:template name="do-escape-latex"> + <xsl:param name="str" /> + <xsl:if test="$str != ''"> + <xsl:call-template name="escape-latex-char"> + <xsl:with-param name="char" select="substring($str, 1, 1)" /> + </xsl:call-template> + <xsl:call-template name="do-escape-latex"> + <xsl:with-param name="str" select="substring($str, 2)" /> + </xsl:call-template> + </xsl:if> + </xsl:template> + + <xsl:template name="escape-latex"> + <xsl:param name="str" /> + <xsl:call-template name="do-escape-latex"> + <xsl:with-param name="str" select="normalize-space($str)" /> + </xsl:call-template> + </xsl:template> + + <xsl:template match="text()"> + <xsl:variable name="final"> + <xsl:call-template name="escape-latex"> + <xsl:with-param name="str" select="." /> + </xsl:call-template> + </xsl:variable> + <!-- If the node is not first and it starts with space, we need to + keep the first space (eg, <emph>x</emph> = y should map to + \textit{x} = y and not \textit{x}= y) --> + <xsl:variable name="first-char"> + <xsl:value-of select="substring(., 1, 1)" /> + </xsl:variable> + <xsl:variable name="last-char"> + <xsl:value-of select="substring(., string-length(.))" /> + </xsl:variable> + <xsl:variable name="first-char-space"> + <xsl:if test="$first-char = ' ' or $first-char = '	' or $first-char = '
'"> + <xsl:text> </xsl:text> + </xsl:if> + </xsl:variable> + <xsl:variable name="last-char-space"> + <xsl:if test="$last-char = ' ' or $last-char = '	' or $last-char = '
'"> + <xsl:text> </xsl:text> + </xsl:if> + </xsl:variable> + <xsl:variable name="space-left"> + <xsl:if test="count(preceding-sibling::*) >= 1 "> + <xsl:value-of select="$first-char-space" /> + </xsl:if> + </xsl:variable> + <xsl:variable name="space-right"> + <xsl:if test="count(following-sibling::*) >= 1 "> + <xsl:value-of select="$last-char-space" /> + </xsl:if> + </xsl:variable> + <xsl:value-of select="$space-left" /> + <xsl:value-of select="$final" /> + <xsl:value-of select="$space-right" /> + </xsl:template> +</xsl:stylesheet> diff --git a/latex/markup.xsl b/latex/markup.xsl new file mode 100644 index 0000000..46181f1 --- /dev/null +++ b/latex/markup.xsl @@ -0,0 +1,337 @@ +<?xml version="1.0" encoding="utf-8"?> +<xsl:stylesheet + version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:html="http://www.w3.org/1999/xhtml" + xmlns:h4sp="http://h4sp.planete-kraus.eu/ns/h4sp"> + + <xsl:import href="escape.xsl" /> + + <xsl:output method="text"/> + + <xsl:template match="h4sp:small-caps"> + <xsl:text>\textsc{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="html:br"> + <xsl:text>\\
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:tableofcontents"> + <xsl:text>\tableofcontents
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:listoffigures"> + <xsl:text>\listoffigures
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:listoftables"> + <xsl:text>\listoftables
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:listofalgorithms"> + <xsl:text>\listofalgorithms
</xsl:text> + </xsl:template> + + <xsl:template name="label"> + <xsl:if test="@id"> + <xsl:text>\label{</xsl:text> + <xsl:value-of select="@id" /> + <xsl:text>}</xsl:text> + </xsl:if> + </xsl:template> + + <xsl:template name="chaptermark"> + <xsl:if test="@short"> + <xsl:text>\chaptermark{\textit{</xsl:text> + <xsl:call-template name="escape-latex"> + <xsl:with-param name="str" select="@short" /> + </xsl:call-template> + <xsl:text>}}{}
</xsl:text> + </xsl:if> + </xsl:template> + + <xsl:template match="html:h1"> + <xsl:text>\chapter{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + <xsl:call-template name="label" /> + <xsl:call-template name="chaptermark" /> + </xsl:template> + + <xsl:template match="html:h2"> + <xsl:text>\section{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + <xsl:call-template name="label" /> + </xsl:template> + + <xsl:template match="html:h3"> + <xsl:text>\subsection{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + <xsl:call-template name="label" /> + </xsl:template> + + <xsl:template match="html:h4"> + <xsl:text>\subsubsection{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + <xsl:call-template name="label" /> + </xsl:template> + + <xsl:template match="html:h5"> + <xsl:text>\paragraph{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + <xsl:call-template name="label" /> + </xsl:template> + + <xsl:template match="html:p"> + <xsl:apply-templates /> + <xsl:text>

</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:eq"> + <xsl:variable name="inside"> + <xsl:apply-templates mode="verbatim-math" /> + </xsl:variable> + <xsl:text>$</xsl:text> + <xsl:value-of select="normalize-space($inside)" /> + <xsl:text>$</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:equation-x"> + <xsl:variable name="inside"> + <xsl:apply-templates mode="verbatim-math" /> + </xsl:variable> + <xsl:text>\begin{equation*}
</xsl:text> + <xsl:value-of select="normalize-space($inside)" /> + <xsl:text>
\end{equation*}

</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:equation"> + <xsl:variable name="inside"> + <xsl:apply-templates mode="verbatim-math" /> + </xsl:variable> + <xsl:text>\begin{equation}
</xsl:text> + <xsl:value-of select="normalize-space($inside)" /> + <xsl:call-template name="label" /> + <xsl:text>
\end{equation}

</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:mini"> + <xsl:text>\begin{mini}
</xsl:text> + <xsl:text>{</xsl:text> + <xsl:apply-templates select="h4sp:variables" mode="verbatim-math" /> + <xsl:text>}
</xsl:text> + <xsl:text>{</xsl:text> + <xsl:apply-templates select="h4sp:objective" mode="verbatim-math" /> + <xsl:call-template name="label" /> + <xsl:text>}
</xsl:text> + <xsl:text>{}
</xsl:text> + <xsl:text>{}
</xsl:text> + <xsl:apply-templates select="h4sp:add-constraint | h4sp:break-objective" /> + <xsl:text>\end{mini}

</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:add-constraint"> + <xsl:text>\addConstraint{</xsl:text> + <xsl:apply-templates select="h4sp:left" mode="verbatim-math" /> + <xsl:text>}{</xsl:text> + <xsl:apply-templates select="h4sp:right" mode="verbatim-math" /> + <xsl:text>}
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:break-objective"> + <xsl:text>\breakObjective{</xsl:text> + <xsl:apply-templates mode="verbatim-math" /> + <xsl:text>}
</xsl:text> + </xsl:template> + + <xsl:template match="html:emph"> + <xsl:text>\emph{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="html:strong"> + <xsl:text>\textbf{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:fn"> + <xsl:text>\footnote{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:cite"> + <xsl:text>\cite{</xsl:text> + <xsl:value-of select="@href" /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:bibliography"> + <xsl:text>\bibliographystyle{</xsl:text> + <xsl:value-of select="@style" /> + <xsl:text>}
</xsl:text> + <xsl:text>\bibliography{manuscrit}
</xsl:text> + </xsl:template> + + <xsl:template match="html:ul"> + <xsl:text>\begin{itemize}
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{itemize}

</xsl:text> + </xsl:template> + + <xsl:template match="html:ol"> + <xsl:text>\begin{enumerate}
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{enumerate}

</xsl:text> + </xsl:template> + + <xsl:template match="html:li"> + <xsl:text>\item </xsl:text> + <xsl:call-template name="label" /> + <xsl:apply-templates /> + <xsl:text>
</xsl:text> + </xsl:template> + + <xsl:template match="html:sup"> + <xsl:text>\textsuperscript{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:résumé-chapitre"> + <xsl:text>\begin{quote}
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{quote}
</xsl:text> + <xsl:text>\newpage
</xsl:text> + </xsl:template> + + <xsl:template match="html:figure"> + <xsl:text>\begin{figure}
</xsl:text> + <xsl:text>\centering
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{figure}

</xsl:text> + </xsl:template> + + <xsl:template match="html:img[substring(@src, string-length(@src) - 3) = '.svg']"> + <xsl:text>\includesvg{</xsl:text> + <xsl:value-of select="@src" /> + <xsl:text>}
</xsl:text> + </xsl:template> + + <xsl:template match="html:img[substring(@src, string-length(@src) - 3) = '.png']"> + <xsl:text>\includegraphics[width=.9\linewidth]{</xsl:text> + <xsl:value-of select="@src" /> + <xsl:text>}
</xsl:text> + </xsl:template> + + <xsl:template match="html:figcaption|html:caption"> + <xsl:text>\caption{</xsl:text> + <xsl:call-template name="label" /> + <xsl:apply-templates /> + <xsl:text>}
</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:ref"> + <xsl:text>\ref{</xsl:text> + <xsl:value-of select="substring-after(@href, '#')" /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:eqref"> + <xsl:text>\eqref{</xsl:text> + <xsl:value-of select="substring-after(@href, '#')" /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="html:blockquote"> + <xsl:text>\begin{quote}
</xsl:text> + <xsl:apply-templates /> + <xsl:text>\end{quote}

</xsl:text> + </xsl:template> + + <xsl:template match="html:a"> + <xsl:text>\href{</xsl:text> + <xsl:value-of select="@href" /> + <xsl:text>}{</xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> + + <xsl:template match="h4sp:algorithm"> + <xsl:text>\begin{algorithm}
</xsl:text> + <xsl:apply-templates /> + <xsl:call-template name="label" /> + <xsl:text>\end{algorithm}

</xsl:text> + </xsl:template> + + <xsl:template match="html:table"> + <xsl:text>\begin{</xsl:text> + <xsl:value-of select="@sideways" /> + <xsl:text>table}
</xsl:text> + <xsl:apply-templates select="html:caption" /> + <xsl:text>\centering
</xsl:text> + <xsl:text>\begin{tabular}</xsl:text> + <xsl:if test="@latex-align"> + <xsl:text>{</xsl:text> + <xsl:value-of select="@latex-align" /> + <xsl:text>}</xsl:text> + </xsl:if> + <xsl:text>
</xsl:text> + <xsl:if test="count(html:thead) != 0"> + <xsl:apply-templates select="html:thead" /> + <xsl:text>\hline
</xsl:text> + </xsl:if> + <xsl:if test="count(html:tbody) = 0"> + <xsl:message terminate="yes"> + <xsl:text>I can only work with tables with an optional <thead> and required <tbody>.
</xsl:text> + </xsl:message> + </xsl:if> + <xsl:apply-templates select="html:tbody" /> + <xsl:if test="count(html:tfoot) != 0"> + <xsl:text>\hline
</xsl:text> + <xsl:apply-templates select="html:tfoot" /> + </xsl:if> + <xsl:text>\end{tabular}
</xsl:text> + <xsl:text>\end{</xsl:text> + <xsl:value-of select="@sideways" /> + <xsl:text>table}

</xsl:text> + </xsl:template> + + <xsl:template match="html:thead|html:tbody|html:tfoot"> + <xsl:apply-templates /> + </xsl:template> + + <xsl:template match="html:tr"> + <xsl:apply-templates /> + <xsl:text>\\
</xsl:text> + </xsl:template> + + <xsl:template match="html:td"> + <xsl:if test="count(preceding-sibling::html:td) != 0"> + <xsl:text> & </xsl:text> + </xsl:if> + <xsl:apply-templates /> + </xsl:template> + + <xsl:template match="html:th"> + <xsl:if test="count(preceding-sibling::html:th) != 0"> + <xsl:text> & </xsl:text> + </xsl:if> + <xsl:apply-templates /> + </xsl:template> + + <xsl:template match="small-font-size"> + <xsl:text>{\small </xsl:text> + <xsl:apply-templates /> + <xsl:text>}</xsl:text> + </xsl:template> +</xsl:stylesheet> |