Merge luamml-pdf.sty into luamml.dtx

This commit is contained in:
Marcel Fabian Krüger 2021-06-26 20:23:14 +02:00
parent 903480a3f9
commit c78ac2e779
2 changed files with 183 additions and 127 deletions

View File

@ -1,120 +0,0 @@
\ProvidesExplPackage {luamml-pdf} {2021-05-31} {0.0.1-alpha}
{MathML generation for L̶u̶a̶pdfLaTeX}
\cs_if_exist:NTF \showstream {
\iow_new:N \l__luamml_pdf_stream
\iow_open:Nn \l__luamml_pdf_stream { \jobname .tml }
\cs_new_protected:Npn \__luamml_pdf_showlists: {
\group_begin:
\int_set:Nn \tex_showboxdepth:D { \c_max_int }
\int_set:Nn \tex_showboxbreadth:D { \c_max_int }
\showstream = \l__luamml_pdf_stream
\tex_showlists:D
\group_end:
}
} {
\cs_set_eq:NN \l__luamml_pdf_stream \c_log_iow
\cs_set_eq:NN \__luamml_pdf_set_showstream: \scan_stop:
\cs_new_protected:Npn \__luamml_pdf_showlists: {
\group_begin:
\int_set:Nn \l_tmpa_int { \tex_interactionmode:D }
\int_set:Nn \tex_interactionmode:D { 0 }
\int_set:Nn \tex_showboxdepth:D { \c_max_int }
\int_set:Nn \tex_showboxbreadth:D { \c_max_int }
\tex_showlists:D
\int_set:Nn \tex_interactionmode:D { \l_tmpa_int }
\group_end:
}
}
\int_new:N \g__luamml_formula_id_int
\cs_new_protected:Npn \luamml_pdf_write: {
\int_gincr:N \g__luamml_formula_id_int
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_FORMULA_BEGIN:
\int_use:N \g__luamml_formula_id_int
:3:mrow
}
\__luamml_pdf_showlists:
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_FORMULA_END
}
}
\cs_new:Npn \luamml_pdf_last_formula: {
\int_use:N \g__luamml_formula_id_int
}
\cs_generate_variant:Nn \tl_to_str:n { e }
% annotate parameters
% #1 number of top level nodes to be annotated
% #2 annotation
% #3 nodes to be annotated
\int_new:N \g__luamml_annotation_id_int
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = \int_eval:n {#1},
#2
}
\exp_not:N \iow_newline:
LUAMML_MARK_END
}
#3
}
% annotate parameters
% #1 annotation
% #2 nodes to be annotated
% THIS VERSION IS SIGNIFICANTLY SLOWER
\cs_new_protected:Npn \luamml_annotate:en #1#2 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = data.count[\int_use:N \g__luamml_annotation_id_int],
#1
}
\exp_not:N \iow_newline:
LUAMML_MARK_END
}
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_COUNT:
\int_use:N \g__luamml_annotation_id_int
}
\__luamml_pdf_showlists:
#2
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_COUNT_END
}
\__luamml_pdf_showlists:
}
\NewDocumentCommand \RegisterFamilyMapping {m m} {
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_INSTRUCTION:REGISTER_MAPPING: \int_use:N #1 : #2
}
}
\endinput
\cs_new:Npn \__luamml_patch_package:nn #1 #2 {
\@ifpackageloaded {#1} {#2} {
\hook_gput_code:nnn {package/after/#1} {luamml} {#2}
}
}
\cs_new:Npn \__luamml_patch_package:n #1 {
\__luamml_patch_package:nn {#1} {
\RequirePackage { luamml-patches-#1 }
}
}
\RequirePackage { luamml-patches-kernel }
\__luamml_patch_package:n {amsmath}
\__luamml_patch_package:n {array}

View File

@ -20,7 +20,10 @@
%</gobble>
\input docstrip.tex
\keepsilent
\generate{\file{luamml.sty}{\from{luamml.dtx}{package}}}
\generate{
\file{luamml.sty}{\from{luamml.dtx}{package,luatex}}
\file{luamml-pdf.sty}{\from{luamml.dtx}{package,pdftex}}
}
\endbatchfile
%</batch>
%<*gobble>
@ -56,13 +59,20 @@
%
% \begin{implementation}
% \section{Package Implementation}
% \subsection{Initialization}
% \iffalse
%<*package>
% \fi
% \begin{macrocode}
%<@@=luamml>
%<*luatex>
\ProvidesExplPackage {luamml} {2021-04-23} {0.0.1-alpha}
{Automatically generate presentational MathML from LuaTeX math expressions}
%</luatex>
%<*pdftex>
\ProvidesExplPackage {luamml-pdf} {2021-05-31} {0.0.1-alpha}
{MathML generation for L̶u̶a̶pdfLaTeX}
%</pdftex>
% \end{macrocode}
%
% \subsection{Initialization}
@ -75,15 +85,18 @@
% them later.
% \begin{macrocode}
\int_new:N \l__luamml_flag_int
\tl_new:N \l__luamml_filename_tl
%<luatex>\tl_new:N \l__luamml_filename_tl
\tl_new:N \l__luamml_root_tl
\tl_set:Nn \l__luamml_root_tl { mrow }
\int_new:N \tracingmathml
%<pdftex>\int_new:N \g__luamml_formula_id_int
%<luatex>\int_new:N \tracingmathml
% \end{macrocode}
%
% Now we can load the Lua module which defines the callback.
% Of course until pdf\TeX starts implementing \cs{directlua} this is only
% done in Lua\TeX.
% \begin{macrocode}
\lua_now:n { require'luamml-tex' }
%<luatex>\lua_now:n { require'luamml-tex' }
% \end{macrocode}
%
% \subsection{Flags}
@ -158,12 +171,14 @@
%
% \begin{macro}{\luamml_flag_structelem:}
% Like \cs{luamml_flag_process:}, but additionally add PDF structure
% elements. This only works if \pkg{tagpdf} has been loaded \emph{before}
% \texttt{luamml}.
% elements. This only works in Lua\TeX\ and requires that the \pkg{tagpdf} package
% has been loaded \emph{before} \texttt{luamml}.
% \begin{macrocode}
%<*luatex>
\cs_new_protected:Npn \luamml_flag_structelem: {
\int_set:Nn \l__luamml_flag_int { 11 }
}
%</luatex>
% \end{macrocode}
% \end{macro}
%
@ -176,10 +191,15 @@
%
% Only complete formulas get written into files (so formulas where
% \cs{luamml_flag_process:} or \cs{luamml_flag_structelem:} are in effect).
%
% Only implemented in Lua\TeX, in pdf\TeX\ the arguments for \texttt{pdfmml}
% determine the output location.
% \begin{macrocode}
%<*luatex>
\cs_new_protected:Npn \luamml_set_filename:n {
\tl_set:Nn \l__luamml_filename_tl
}
%</luatex>
% \end{macrocode}
% \end{macro}
%
@ -189,11 +209,17 @@
% \end{macrocode}
%
% \subsection{Annotations}
% These are implemented very differently depending on the engine, but the interface
% should be the same.
% \subsubsection{Lua\TeX}
% \begin{macrocode}
%<*luatex>
% \end{macrocode}
% \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
% A simple annotation scheme: The first argument is the number of top level
% noads to be annotated, the second parameter the annotation and the third
% parameter the actual list of math tokens. The first argument can be omitted to
% let Lua\TeX detrmine the number itself.
% let Lua\TeX determine the number itself.
%
% Passing the first parameter explicitly is useful for any annotations which
% should be compatible with fututre pdf\TeX versions of this functionality.
@ -212,6 +238,153 @@
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%</luatex>
% \end{macrocode}
% \subsubsection{pdf\TeX}
% \begin{macrocode}
%<*pdftex>
% \end{macrocode}
% \begin{macro}{\__luamml_pdf_showlists:}
% Here and in many other locations the \pdfTeX{} implementation is based on \cs{showlists},
% so we define a internal wrapper which sets all relevant parameters.
% \begin{macrocode}
\cs_if_exist:NTF \showstream {
\iow_new:N \l__luamml_pdf_stream
\iow_open:Nn \l__luamml_pdf_stream { \jobname .tml }
\cs_new_protected:Npn \__luamml_pdf_showlists: {
\group_begin:
\int_set:Nn \tex_showboxdepth:D { \c_max_int }
\int_set:Nn \tex_showboxbreadth:D { \c_max_int }
\showstream = \l__luamml_pdf_stream
\tex_showlists:D
\group_end:
}
} {
\cs_set_eq:NN \l__luamml_pdf_stream \c_log_iow
\cs_set_eq:NN \__luamml_pdf_set_showstream: \scan_stop:
\cs_new_protected:Npn \__luamml_pdf_showlists: {
\group_begin:
\int_set:Nn \l_tmpa_int { \tex_interactionmode:D }
\int_set:Nn \tex_interactionmode:D { 0 }
\int_set:Nn \tex_showboxdepth:D { \c_max_int }
\int_set:Nn \tex_showboxbreadth:D { \c_max_int }
\tex_showlists:D
\int_set:Nn \tex_interactionmode:D { \l_tmpa_int }
\group_end:
}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
% Now we can define the annotation commands for pdf\TeX.
% \begin{macrocode}
\cs_generate_variant:Nn \tl_to_str:n { e }
\int_new:N \g__luamml_annotation_id_int
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = \int_eval:n {#1},
#2
}
\exp_not:N \iow_newline:
LUAMML_MARK_END
}
#3
}
\cs_new_protected:Npn \luamml_annotate:en #1#2 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = data.count[\int_use:N \g__luamml_annotation_id_int],
#1
}
\exp_not:N \iow_newline:
LUAMML_MARK_END
}
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_COUNT:
\int_use:N \g__luamml_annotation_id_int
}
\__luamml_pdf_showlists:
#2
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_COUNT_END
}
\__luamml_pdf_showlists:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%</pdftex>
% \end{macrocode}
%
% \subsection{Trigger for specific formula}
% This only applies for pdf\TeX\ since in Lua\TeX\ everything is controlled by the callback.
%
% \begin{macrocode}
%<*pdftex>
% \end{macrocode}
%
% \begin{macro}{\luamml_pdf_write:}
% We could accept parameters for the flag and tag here, but for compatibility
% with Lua\TeX they are passed in macros instead.
% \begin{macrocode}
\cs_new_protected:Npn \luamml_pdf_write: {
\int_gincr:N \g__luamml_formula_id_int
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_FORMULA_BEGIN:
\int_use:N \g__luamml_formula_id_int
:3:mrow
}
\__luamml_pdf_showlists:
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_FORMULA_END
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\luamml_pdf_last_formula:}
% A unique identifier for the last formula written with \cs{luamml_pdf_write:}.
% Some form of this will probably be added to the Lua\TeX insterface at some point.
% \begin{macrocode}
\cs_new:Npn \luamml_pdf_last_formula: {
\int_use:N \g__luamml_formula_id_int
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%</pdftex>
% \end{macrocode}
%
% \subsection{Further helpers}
%
% \begin{macro}{\RegisterFamilyMapping}
% The Lua version of this is defined in the Lua module.
% \begin{macrocode}
%<*pdftex>
\NewDocumentCommand \RegisterFamilyMapping {m m} {
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_INSTRUCTION:REGISTER_MAPPING: \int_use:N #1 : #2
}
}
%</pdftex>
% \end{macrocode}
%
%
% \subsection{Patching}
% For some packages, we ship with patches to make them more compatible and to
% demonstrate how other code can be patched to work with \texttt{luamml}.
@ -237,10 +410,13 @@
% \end{macrocode}
% \end{macro}
%
%
% \begin{macrocode}
%<*luatex>
\RequirePackage { luamml-patches-kernel }
\__luamml_patch_package:n {amsmath}
\__luamml_patch_package:n {array}
%</luatex>
% \end{macrocode}
% \iffalse