From 8389f0bb8e6af2c5dcbfe3cdad746a9c170d1157 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Sun, 7 Nov 2021 03:01:05 +0000 Subject: CTAN sync 202111070301 --- macros/luatex/generic/luakeys/README.md | 22 + macros/luatex/generic/luakeys/luakeys-debug.sty | 6 +- macros/luatex/generic/luakeys/luakeys-debug.tex | 4 +- macros/luatex/generic/luakeys/luakeys-doc.pdf | Bin 0 -> 157648 bytes macros/luatex/generic/luakeys/luakeys-doc.tex | 726 +++++++++++++++++++++ macros/luatex/generic/luakeys/luakeys.lua | 19 +- macros/luatex/generic/luakeys/luakeys.pdf | Bin 153582 -> 0 bytes macros/luatex/generic/luakeys/luakeys.sty | 21 + macros/luatex/generic/luakeys/luakeys.tex | 660 +------------------ macros/luatex/latex/pyluatex/README.md | 10 +- .../luatex/latex/pyluatex/pyluatex-interpreter.py | 37 +- macros/luatex/latex/pyluatex/pyluatex.lua | 73 ++- macros/luatex/latex/pyluatex/pyluatex.pdf | Bin 79524 -> 82190 bytes macros/luatex/latex/pyluatex/pyluatex.sty | 29 +- macros/luatex/latex/pyluatex/pyluatex.tex | 47 +- 15 files changed, 952 insertions(+), 702 deletions(-) create mode 100644 macros/luatex/generic/luakeys/luakeys-doc.pdf create mode 100644 macros/luatex/generic/luakeys/luakeys-doc.tex delete mode 100644 macros/luatex/generic/luakeys/luakeys.pdf create mode 100644 macros/luatex/generic/luakeys/luakeys.sty (limited to 'macros/luatex') diff --git a/macros/luatex/generic/luakeys/README.md b/macros/luatex/generic/luakeys/README.md index cb4c429d4c..8c2e5cf8db 100644 --- a/macros/luatex/generic/luakeys/README.md +++ b/macros/luatex/generic/luakeys/README.md @@ -50,3 +50,25 @@ or ``` make test ``` + +### Release a new version + +Update copyright in: + +* LICENSE +* luakeys-debug.sty +* luakeys-debug.tex +* luakeys.lua +* luakeys.sty +* luakeys.tex +* README.md + +Update version in: + +* luakeys-doc.tex +* luakeys-debug.sty +* luakeys.sty + +Summarize the changes in the luakeys-doc.tex as changes. + +Create a new git tag `git tag -sa v0.3`. Prefix the version with “v”. diff --git a/macros/luatex/generic/luakeys/luakeys-debug.sty b/macros/luatex/generic/luakeys/luakeys-debug.sty index a96d2d51c6..ca800bfad7 100644 --- a/macros/luatex/generic/luakeys/luakeys-debug.sty +++ b/macros/luatex/generic/luakeys/luakeys-debug.sty @@ -13,10 +13,10 @@ % % The Current Maintainer of this work is Josef Friedrich. % -% This work consists of the files luakeys.lua, luakeys-debug.sty -% and luakeys-debug.tex. +% This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex +% luakeys-debug.sty and luakeys-debug.tex. \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{luakeys-debug}[2021/09/19 Debug package for luakeys.] +\ProvidesPackage{luakeys-debug}[2021/11/05 v0.3 Debug package for luakeys.] \input luakeys-debug.tex diff --git a/macros/luatex/generic/luakeys/luakeys-debug.tex b/macros/luatex/generic/luakeys/luakeys-debug.tex index b63e67e050..a6cd7c85c2 100644 --- a/macros/luatex/generic/luakeys/luakeys-debug.tex +++ b/macros/luatex/generic/luakeys/luakeys-debug.tex @@ -13,8 +13,8 @@ % % The Current Maintainer of this work is Josef Friedrich. % -% This work consists of the files luakeys.lua, luakeys-debug.sty -% and luakeys-debug.tex. +% This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex +% luakeys-debug.sty and luakeys-debug.tex. \directlua{ luakeys = require('luakeys') diff --git a/macros/luatex/generic/luakeys/luakeys-doc.pdf b/macros/luatex/generic/luakeys/luakeys-doc.pdf new file mode 100644 index 0000000000..3c28176a7d Binary files /dev/null and b/macros/luatex/generic/luakeys/luakeys-doc.pdf differ diff --git a/macros/luatex/generic/luakeys/luakeys-doc.tex b/macros/luatex/generic/luakeys/luakeys-doc.tex new file mode 100644 index 0000000000..b7e525fc4b --- /dev/null +++ b/macros/luatex/generic/luakeys/luakeys-doc.tex @@ -0,0 +1,726 @@ +\documentclass{ltxdoc} + +\usepackage{hyperref} +\EnableCrossrefs +\CodelineIndex +\RecordChanges + +\usepackage{mdframed} +\usepackage{minted} +\usepackage{luakeys-debug} +\usepackage{multicol} +\usepackage{luacode} +\usepackage{syntax} +\usemintedstyle{friendly} +\BeforeBeginEnvironment{minted}{\begin{mdframed}} +\AfterEndEnvironment{minted}{\end{mdframed}} +\setminted{ + breaklines=true, + fontsize=\footnotesize, +} +\def\lua#1{\mintinline{lua}|#1|} + +\begin{document} + +\providecommand*{\url}{\texttt} + +\title{The \textsf{luakeys} package} +\author{% + Josef Friedrich\\% + \url{josef@friedrich.rocks}\\% + \href{https://github.com/Josef-Friedrich/luakeys}{github.com/Josef-Friedrich/luakeys}% +} +\date{v0.3 from 2021/11/05} + +\maketitle + +\vfill + +%\luakeysdebug{level1={level2={level3={dim=1cm,bool=true,num=-1e-03,str=lua}}}} + +\begin{minted}{lua} +local luakeys = require('luakeys') +local kv = luakeys.parse('level1={level2={level3={dim=1cm,bool=true,num=-1e-03,str=lua}}}') +luakeys.print(kv) +\end{minted} + +\noindent +Result: + +\begin{center} +\begin{minted}{lua} +{ + ['level1'] = { + ['level2'] = { + ['level3'] = { + ['dim'] = 1864679, + ['bool'] = true, + ['num'] = -0.001 + ['str'] = 'lua', + } + } + } +} +\end{minted} +\end{center} + +\vfill + +\strut + +\newpage + +\tableofcontents + +\newpage + +\section{Introduction} + +\noindent +|luakeys| is a Lua module that can parse key-value options like the +\TeX{} packages \href{https://www.ctan.org/pkg/keyval}{keyval}, +\href{https://www.ctan.org/pkg/kvsetkeys}{kvsetkeys}, +\href{https://www.ctan.org/pkg/kvoptions}{kvoptions}, +\href{https://www.ctan.org/pkg/xkeyval}{xkeyval}, +\href{https://www.ctan.org/pkg/pgfkeys}{pgfkeys} etc. do. |luakeys|, +however, accompilshes this task entirely, by using the Lua language and +doesn’t rely on \TeX{}. Therefore this package can only be used with the +\TeX{} engine Lua\TeX{}. Since |luakeys| uses +\href{http://www.inf.puc-rio.br/~roberto/lpeg/}{LPeg}, the parsing +mechanism should be pretty robust. + +The TUGboat article +\href{http://www.tug.org/tugboat/tb30-1/tb94wright-keyval.pdf} +{“Implementing key–value input: An introduction” (Volume 30 (2009), No. 1)} +by Joseph Wright and Christian Feuersänger gives a good overview of the +available key-value packages. + +This package would not be possible without the article +\href{https://tug.org/TUGboat/tb40-2/tb125menke-lpeg.pdf} +{Parsing complex data formats in LuaTEX with LPEG (Volume 40 (2019), No. 2)}. + +%----------------------------------------------------------------------- +% +%----------------------------------------------------------------------- + +\clearpage + +\section{Usage} + +%% +% +%% + +\subsection{Using the Lua module \texttt{luakeys.lua}} + +The core functionality of this package is realized in Lua. So you can +use \texttt{luakeys} without using the wrapper \TeX{} files +\texttt{luakeys.sty} and \texttt{luakeys.tex}. + +\begin{minted}{latex} +\documentclass{article} +\directlua{ + luakeys = require('luakeys') +} + +\newcommand{\helloworld}[2][]{ + \directlua{ + local keys = luakeys.parse('\luaescapestring{\unexpanded{#1}}') + luakeys.print(keys) + local marg = '#2' + tex.print(keys.greeting .. ', ' .. marg .. keys.punctuation) + } +} +\begin{document} +\helloworld[greeting=hello,punctuation=!]{world} +\end{document} +\end{minted} + +%% +% +%% + +\subsection{Using the Lua\LaTeX{} wrapper \texttt{luakeys.sty}} + +The supplied Lua\LaTeX{} file is quite small: + +\begin{minted}{latex} +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{luakeys} +\directlua{luakeys = require('luakeys')} +\end{minted} + +\noindent +It loads the Lua module into the global variable \texttt{luakeys}. + +\begin{minted}{latex} +\documentclass{article} +\usepackage{luakeys} + +\begin{document} + \directlua{ + local keys = luakeys.parse('one,two,three') + tex.print(keys[1]) + tex.print(keys[2]) + tex.print(keys[3]) + } +\end{document} +\end{minted} + +%% +% +%% + +\subsection{Using the plain Lua\TeX{} wrapper \texttt{luakeys.tex}} + +Even smaller is the file \texttt{luakeys.tex}. It consists of only one +line: + +\begin{minted}{latex} +\directlua{luakeys = require('luakeys')} +\end{minted} + +\noindent +It does the same as the Lua\LaTeX{} wrapper and loads the Lua module +\texttt{luakeys.lua} into the global variable \texttt{luakeys}. + +\begin{minted}{latex} +\input luakeys.tex + +\directlua{ + local keys = luakeys.parse('one,two,three') + tex.print(keys[1]) + tex.print(keys[2]) + tex.print(keys[3]) +} +\bye +\end{minted} + +\section{Syntax of the recognized key-value format} + +%% +% +%% + +\subsection{A attempt to put the syntax into words} + +A key-value pair is definied by an equal sign (\texttt{key=value}). +Several key-value pairs or values without keys are lined up with commas +(\texttt{key=value,value}) and build a key-value list. Curly brackets +can be used to create a recursive data structure of nested key-value +lists (\texttt{level1=\{level2=\{key=value,value\}\}}). + +%% +% +%% + +\subsection{An (incomplete) attempt to put the syntax into the Extended Backus-Naur Form} + +\begin{grammar} + ::= | + + ::= ( | ) [ `,' ] + + ::== `{' `}' + + ::= + \alt + \alt + \alt + \alt + + ::= `-' | `+' + + ::= `0' | `1' | `2' | `3' | `4' | `5' | `6' | `7' | `8' | `9' + + ::= `bp' | `BP' + \alt `cc' | `CC' + \alt `cm' | `CM' + \alt `dd' | `DD' + \alt `em' | `EM' + \alt `ex' | `EX' + \alt `in' | `IN' + \alt `mm' | `MM' + \alt `nc' | `NC' + \alt `nd' | `ND' + \alt `pc' | `PC' + \alt `pt' | `PT' + \alt `sp' | `SP' + +\end{grammar} + +... to be continued + +%% +% +%% + +\subsection{Recognized data types} + +\subsubsection{boolean} + +The strings \texttt{true}, \texttt{TRUE} and \texttt{True} are converted +into Lua’s boolean type \lua{true}, the strings \texttt{false}, +\texttt{FALSE} and \texttt{False} into \lua{false}. + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{ + lower case true = true, + upper case true = TRUE, + title case true = True + lower case false = false, + upper case false = FALSE, + title case false = False, +} +\end{minted} +\begin{minted}{lua} +{ + ['lower case true'] = true, + ['upper case true'] = true, + ['title case true'] = true, + ['lower case false'] = false, + ['upper case false'] = false + ['title case false'] = false, +} +\end{minted} +\end{multicols} + +%% +% +%% + +\clearpage + +\subsubsection{number} + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{ + num1 = 4, + num2 = -4, + num3 = 0.4, + num4 = 4.57e-3, + num5 = 0.3e12, + num6 = 5e+20 +} +\end{minted} +\begin{minted}{lua} +{ + ['num1'] = 4, + ['num2'] = -4, + ['num3'] = 0.4, + ['num4'] = 0.00457, + ['num5'] = 300000000000.0, + ['num6'] = 5e+20 +} +\end{minted} +\end{multicols} + +%% +% +%% + +\clearpage + +\subsubsection{dimension} + +|luakeys| detects \TeX{} dimensions and automatically converts the +dimensions into scaled points using the function \lua{tex.sp(dim)}. Use +the option \lua{convert_dimensions} of the function +\lua{parse(kv_string, options)} to disalbe the automatic conversion. + +\begin{minted}{lua} +local result = parse('dim=1cm', { + convert_dimensions = false, +}) +\end{minted} + +\noindent +If you want to convert a scale point into a unit string you can use the module +\href{https://raw.githubusercontent.com/latex3/lualibs/master/lualibs-util-dim.lua}{lualibs-util-dim.lua}. + +\begin{minted}{latex} +\begin{luacode} +require('lualibs') +tex.print(number.todimen(tex.sp('1cm'), 'cm', '%0.0F%s')) +\end{luacode} +\end{minted} + +\begin{center} +\begin{tabular}{rl} +\textbf{Unit name} & \textbf{Description} \\\hline +bp & big point \\ +cc & cicero \\ +cm & centimeter \\ +dd & didot \\ +em & horizontal measure of \emph{M} \\ +ex & vertical measure of \emph{x} \\ +in & inch \\ +mm & milimeter \\ +nc & new cicero \\ +nd & new didot \\ +pc & pica \\ +pt & point \\ +sp & scaledpoint \\ +\end{tabular} +\end{center} + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{ + bp = 1bp, + cc = 1cc, + cm = 1cm, + dd = 1dd, + em = 1em, + ex = 1ex, + in = 1in, + mm = 1mm, + nc = 1nc, + nd = 1nd, + pc = 1pc, + pt = 1pt, + sp = 1sp, +} +\end{minted} +\begin{minted}{lua} +{ + ['bp'] = 65781, + ['cc'] = 841489, + ['cm'] = 1864679, + ['dd'] = 70124, + ['em'] = 655360, + ['ex'] = 282460, + ['in'] = 4736286, + ['mm'] = 186467, + ['nc'] = 839105, + ['nd'] = 69925, + ['pc'] = 786432, + ['pt'] = 65536, + ['sp'] = 1, +} +\end{minted} +\end{multicols} + +%% +% +%% + +\subsubsection{string} + +There are two ways to specify strings: With or without quotes. If the +text have to contain commas or equal signs, then double quotation +marks must be used. + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{ + without quotes = no commas and equal signs are allowed, + with double quotes = ", and = are allowed", +} +\end{minted} +\begin{minted}{lua} +{ + ['without quotes'] = 'no commas and equal signs are allowed', + ['with double quotes'] = ', and = are allowed', +} +\end{minted} +\end{multicols} + +\subsubsection{Standalone values} + +Standalone values are values without a key. They are converted into an +array. In Lua an array is a table with numeric indexes (The first index +is 1). + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{one,two,three} +\end{minted} +\columnbreak +\begin{minted}{lua} +{ 'one', 'two', 'three' } +\end{minted} + +\noindent +is equivalent to + +\begin{minted}{lua} +{ + [1] = 'one', + [2] = 'two', + [3] = 'three', +} +\end{minted} +\end{multicols} + +\noindent +All recognized data types can be used as standalone values. + +\begin{multicols}{2} +\begin{minted}{latex} +\luakeysdebug{one,2,3cm} +\end{minted} +\columnbreak +\begin{minted}{lua} +{ 'one', 2, 5594039 } +\end{minted} +\end{multicols} + +%----------------------------------------------------------------------- +% +%----------------------------------------------------------------------- + +\clearpage + +\section{Exported functions of the Lua module \texttt{luakeys.lua}} + +To learn more about the individual functions (local functions), please +read the \href{https://josef-friedrich.github.io/luakeys/}{source code +documentation}, which was created with +\href{http://stevedonovan.github.io/ldoc/}{LDoc}. The Lua module exports +this functions: + +\begin{minted}{lua} +local luakeys = require('luakeys') +local parse = luakeys.parse +local render = luakeys.render +--local print = luakeys.print -- That would overwrite the built-in Lua function +local save = luakeys.save +local get = luakeys.get +\end{minted} + +%% +% +%% + +\subsection{\texttt{parse(kv\_string, options)}: table} + +The function \lua{parse(input_string, options)} is the main method of +this module. It parses a key-value string into a Lua table. + +\begin{minted}{latex} +\newcommand{\mykeyvalcmd}[1][]{ + \directlua{ + result = luakeys.parse('#1') + luakeys.print(result) + } + #2 +} +\mykeyvalcmd[one=1]{test} +\end{minted} + +\noindent +In plain \TeX: + +\begin{minted}{latex} +\def\mykeyvalcommand#1{ + \directlua{ + result = luakeys.parse('#1') + luakeys.print(result) + } +} +\mykeyvalcmd{one=1} +\end{minted} + +\noindent +The function can be called with a options table. This two options are +supported. + +\begin{minted}{lua} +local result = parse('one,two,three', { + convert_dimensions = false, + unpack_single_array_value = false +}) +\end{minted} + +%% +% +%% + +\subsection{\texttt{render(tbl)}: string} + +The function \lua{render(tbl)} reverses the function +\lua{parse(kv_string)}. It takes a Lua table and converts this table +into a key-value string. The resulting string usually has a different +order as the input table. + +\begin{minted}{lua} +result = luakeys.parse('one=1,two=2,tree=3,') +print(luakeys.render(result)) +--- one=1,two=2,tree=3, +--- or: +--- two=2,one=1,tree=3, +--- or: +--- ... +\end{minted} + +\noindent +In Lua only tables with 1-based consecutive integer keys (a.k.a. array +tables) can be parsed in order. + +\begin{minted}{lua} +result = luakeys.parse('one,two,three') +print(luakeys.render(result)) +--- one,two,three, (always) +\end{minted} + +%% +% +%% + +\subsection{\texttt{print(tbl): void}} + +The function \lua{print(tbl)} pretty prints a Lua table to standard +output (stdout). It is a utility function that can be used to debug and +inspect the resulting Lua table of the function \lua{parse}. You have to +compile your \TeX{} document in a console to see the terminal output. + +%\luakeysdebug{level1={level2={key=value}}} + +\begin{minted}{lua} +result = luakeys.parse('level1={level2={key=value}}') +luakeys.print(result) +\end{minted} + +\noindent +The output should look like this: + +\begin{minted}{md} +{ + ['level1'] = { + ['level2'] = { + ['key'] = 'value', + }, +} +\end{minted} + +%% +% +%% + +\subsection{\texttt{save(identifier, result): void}} + +The function \lua{save(identifier, result)} saves a result (a +table from a previous run of \lua{parse}) under an identifier. +Therefore, it is not necessary to pollute the global namespace to +store results for the later usage. + +%% +% +%% + +\subsection{\texttt{get(identifier): table}} + +The function \lua{get(identifier)} retrieves a saved result from the +result store. + +%----------------------------------------------------------------------- +% +%----------------------------------------------------------------------- + +\clearpage + +\section{Debug packages} + +Two small debug packages are included in |luakeys|. One debug package +can be used in \LaTeX{} (luakeys-debug.sty) and one can be used in plain +\TeX{} (luakeys-debug.tex). Both packages provide only one command: +|\luakeysdebug{kv-string}| + +\begin{minted}{latex} +\luakeysdebug{one,two,three} +\end{minted} + +\noindent +Then the following output should appear in the document: +\bigskip + +\luakeysdebug{one,two,three} + +%% +% +%% + +\subsection{For plain \TeX: luakeys-debug.tex} + +An example of how to use the command in plain \TeX: + +\begin{minted}{latex} +\input luakeys-debug.tex +\luakeysdebug{one,two,three} +\bye +\end{minted} + +%% +% +%% + +\subsection{For \LaTeX: luakeys-debug.sty} + +An example of how to use the command in \LaTeX: + +\begin{minted}{latex} +\documentclass{article} +\usepackage{luakeys-debug} +\begin{document} +\luakeysdebug[ + unpack single array values=false, + convert dimensions=false +]{one,two,three} +\end{document} +\end{minted} + +%----------------------------------------------------------------------- +% +%----------------------------------------------------------------------- + +\clearpage + +\section{Implementation} + +%% +% +%% + +\subsection{luakeys.lua} + +\inputminted[linenos=true]{lua}{luakeys.lua} + +%% +% +%% + +\clearpage + +\subsection{luakeys-debug.tex} + +\inputminted[linenos=true]{latex}{luakeys-debug.tex} + +%% +% +%% + +\clearpage + +\subsection{luakeys-debug.sty} + +\inputminted[linenos=true]{latex}{luakeys-debug.sty} + +\changes{v0.1}{2021/01/18}{Inital release} +\changes{v0.2}{2021/09/19}{ +* Allow all recognized data types as keys +* Allow TeX macros in the values +* New public Lua functions: save(identifier, result), get(identifier) +} +\changes{v0.3}{2021/11/05}{ +* Add a LuaLaTeX wrapper “luakeys.sty” +* Add a plain LuaTeX wrapper “luakeys.tex” +* Rename the previous documentation file “luakeys.tex” to luakeys-doc.tex” +} +\pagebreak +\PrintChanges +\pagebreak +\PrintIndex +\end{document} diff --git a/macros/luatex/generic/luakeys/luakeys.lua b/macros/luatex/generic/luakeys/luakeys.lua index b026413800..05b3d94e1f 100644 --- a/macros/luatex/generic/luakeys/luakeys.lua +++ b/macros/luatex/generic/luakeys/luakeys.lua @@ -1,4 +1,4 @@ --- luakeys-debug.tex +-- luakeys.lua -- Copyright 2021 Josef Friedrich -- -- This work may be distributed and/or modified under the @@ -13,8 +13,8 @@ -- -- The Current Maintainer of this work is Josef Friedrich. -- --- This work consists of the files luakeys.lua, luakeys-debug.sty --- and luakeys-debug.tex. +-- This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex +-- luakeys-debug.sty and luakeys-debug.tex. --- A key-value parser written with Lpeg. -- @@ -167,7 +167,11 @@ local function generate_parser(options) lpeg.V('dimension') + lpeg.V('number') + lpeg.V('string_quoted') + - lpeg.V('string_unquoted'), + lpeg.V('string_unquoted') + + lpeg.V('array'), + + array = + ws('{') * lpeg.Ct((lpeg.V('value') * ws(',')^-1)^0) * ws('}'), boolean = boolean_true * lpeg.Cc(true) + @@ -175,6 +179,9 @@ local function generate_parser(options) dimension = build_dimension_pattern(), + number = + white_space^0 * (number / tonumber) * white_space^0, + string_quoted = white_space^0 * lpeg.P('"') * lpeg.C((lpeg.P('\\"') + 1 - lpeg.P('"'))^0) * @@ -188,10 +195,6 @@ local function generate_parser(options) white_space^0, word_unquoted = (1 - white_space - lpeg.S('{},='))^1; - - number = - white_space^0 * (number / tonumber) * white_space^0, - }) end diff --git a/macros/luatex/generic/luakeys/luakeys.pdf b/macros/luatex/generic/luakeys/luakeys.pdf deleted file mode 100644 index c65765d5b9..0000000000 Binary files a/macros/luatex/generic/luakeys/luakeys.pdf and /dev/null differ diff --git a/macros/luatex/generic/luakeys/luakeys.sty b/macros/luatex/generic/luakeys/luakeys.sty new file mode 100644 index 0000000000..6717b85b57 --- /dev/null +++ b/macros/luatex/generic/luakeys/luakeys.sty @@ -0,0 +1,21 @@ +%% luakeys.sty +%% Copyright 2021 Josef Friedrich +% +% This work may be distributed and/or modified under the +% conditions of the LaTeX Project Public License, either version 1.3c +% of this license or (at your option) any later version. +% The latest version of this license is in +% http://www.latex-project.org/lppl.txt +% and version 1.3c or later is part of all distributions of LaTeX +% version 2008/05/04 or later. +% +% This work has the LPPL maintenance status `maintained'. +% +% The Current Maintainer of this work is Josef Friedrich. +% +% This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex +% luakeys-debug.sty and luakeys-debug.tex. + +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{luakeys}[2021/11/05 v0.3 Parsing key-value options using Lua.] +\directlua{luakeys = require('luakeys')} diff --git a/macros/luatex/generic/luakeys/luakeys.tex b/macros/luatex/generic/luakeys/luakeys.tex index fc465f46ae..68fc92d8ee 100644 --- a/macros/luatex/generic/luakeys/luakeys.tex +++ b/macros/luatex/generic/luakeys/luakeys.tex @@ -1,651 +1,19 @@ -\documentclass{ltxdoc} - -\usepackage{hyperref} -\EnableCrossrefs -\CodelineIndex -\RecordChanges - -\usepackage{mdframed} -\usepackage{minted} -\usepackage{luakeys-debug} -\usepackage{multicol} -\usepackage{luacode} -\usepackage{syntax} -\usemintedstyle{friendly} -\BeforeBeginEnvironment{minted}{\begin{mdframed}} -\AfterEndEnvironment{minted}{\end{mdframed}} -\setminted{ - breaklines=true, - fontsize=\footnotesize, -} -\def\lua#1{\mintinline{lua}|#1|} - -\begin{document} - -\providecommand*{\url}{\texttt} - -\title{The \textsf{luakeys} package} -\author{% - Josef Friedrich\\% - \url{josef@friedrich.rocks}\\% - \href{https://github.com/Josef-Friedrich/luakeys}{github.com/Josef-Friedrich/luakeys}% -} -\date{v0.2 from 2021/09/19} - -\maketitle - -\vfill - -%\luakeysdebug{level1={level2={level3={dim=1cm,bool=true,num=-1e-03,str=lua}}}} - -\begin{minted}{lua} -local luakeys = require('luakeys') -local kv = luakeys.parse('level1={level2={level3={dim=1cm,bool=true,num=-1e-03,str=lua}}}') -luakeys.print(kv) -\end{minted} - -\noindent -Result: - -\begin{center} -\begin{minted}{lua} -{ - ['level1'] = { - ['level2'] = { - ['level3'] = { - ['dim'] = 1864679, - ['bool'] = true, - ['num'] = -0.001 - ['str'] = 'lua', - } - } - } -} -\end{minted} -\end{center} - -\vfill - -\strut - -\newpage - -\tableofcontents - -\newpage - -\section{Introduction} - -\noindent -|luakeys| is a Lua module that can parse key-value options like the -\TeX{} packages \href{https://www.ctan.org/pkg/keyval}{keyval}, -\href{https://www.ctan.org/pkg/kvsetkeys}{kvsetkeys}, -\href{https://www.ctan.org/pkg/kvoptions}{kvoptions}, -\href{https://www.ctan.org/pkg/xkeyval}{xkeyval}, -\href{https://www.ctan.org/pkg/pgfkeys}{pgfkeys} etc. do. |luakeys|, -however, accompilshes this task entirely, by using the Lua language and -doesn’t rely on \TeX{}. Therefore this package can only be used with the -\TeX{} engine Lua\TeX{}. Since |luakeys| uses -\href{http://www.inf.puc-rio.br/~roberto/lpeg/}{LPeg}, the parsing -mechanism should be pretty robust. - -The TUGboat article -\href{http://www.tug.org/tugboat/tb30-1/tb94wright-keyval.pdf} -{“Implementing key–value input: An introduction” (Volume 30 (2009), No. 1)} -by Joseph Wright and Christian Feuersänger gives a good overview of the -available key-value packages. - -This package would not be possible without the article -\href{https://tug.org/TUGboat/tb40-2/tb125menke-lpeg.pdf} -{Parsing complex data formats in LuaTEX with LPEG (Volume 40 (2019), No. 2)}. - -%----------------------------------------------------------------------- -% -%----------------------------------------------------------------------- - -\clearpage - -\section{Usage} - -\begin{minted}{latex} -\documentclass{article} -\directlua{ - luakeys = require('luakeys') -} - -\newcommand{\helloworld}[2][]{ - \directlua{ - local keys = luakeys.parse('\luaescapestring{\unexpanded{#1}}') - luakeys.print(keys) - local marg = '#2' - tex.print(keys.greeting .. ', ' .. marg .. keys.punctuation) - } -} -\begin{document} -\helloworld[greeting=hello,punctuation=!]{world} -\end{document} -\end{minted} - -\section{Syntax of the recognized key-value format} - -%% -% -%% - -\subsection{A attempt to put the syntax into words} - -A key-value pair is definied by an equal sign (\texttt{key=value}). -Several key-value pairs or values without keys are lined up with commas -(\texttt{key=value,value}) and build a key-value list. Curly brackets -can be used to create a recursive data structure of nested key-value -lists (\texttt{level1=\{level2=\{key=value,value\}\}}). - -%% -% -%% - -\subsection{An (incomplete) attempt to put the syntax into the Extended Backus-Naur Form} - -\begin{grammar} - ::= | - - ::= ( | ) [ `,' ] - - ::== `{' `}' - - ::= - \alt - \alt - \alt - \alt - - ::= `-' | `+' - - ::= `0' | `1' | `2' | `3' | `4' | `5' | `6' | `7' | `8' | `9' - - ::= `bp' | `BP' - \alt `cc' | `CC' - \alt `cm' | `CM' - \alt `dd' | `DD' - \alt `em' | `EM' - \alt `ex' | `EX' - \alt `in' | `IN' - \alt `mm' | `MM' - \alt `nc' | `NC' - \alt `nd' | `ND' - \alt `pc' | `PC' - \alt `pt' | `PT' - \alt `sp' | `SP' - -\end{grammar} - -... to be continued - -%% -% -%% - -\subsection{Recognized data types} - -\subsubsection{boolean} - -The strings \texttt{true}, \texttt{TRUE} and \texttt{True} are converted -into Lua’s boolean type \lua{true}, the strings \texttt{false}, -\texttt{FALSE} and \texttt{False} into \lua{false}. - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{ - lower case true = true, - upper case true = TRUE, - title case true = True - lower case false = false, - upper case false = FALSE, - title case false = False, -} -\end{minted} -\begin{minted}{lua} -{ - ['lower case true'] = true, - ['upper case true'] = true, - ['title case true'] = true, - ['lower case false'] = false, - ['upper case false'] = false - ['title case false'] = false, -} -\end{minted} -\end{multicols} - -%% -% -%% - -\clearpage - -\subsubsection{number} - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{ - num1 = 4, - num2 = -4, - num3 = 0.4, - num4 = 4.57e-3, - num5 = 0.3e12, - num6 = 5e+20 -} -\end{minted} -\begin{minted}{lua} -{ - ['num1'] = 4, - ['num2'] = -4, - ['num3'] = 0.4, - ['num4'] = 0.00457, - ['num5'] = 300000000000.0, - ['num6'] = 5e+20 -} -\end{minted} -\end{multicols} - -%% -% -%% - -\clearpage - -\subsubsection{dimension} - -|luakeys| detects \TeX{} dimensions and automatically converts the -dimensions into scaled points using the function \lua{tex.sp(dim)}. Use -the option \lua{convert_dimensions} of the function -\lua{parse(kv_string, options)} to disalbe the automatic conversion. - -\begin{minted}{lua} -local result = parse('dim=1cm', { - convert_dimensions = false, -}) -\end{minted} - -\noindent -If you want to convert a scale point into a unit string you can use the module -\href{https://raw.githubusercontent.com/latex3/lualibs/master/lualibs-util-dim.lua}{lualibs-util-dim.lua}. - -\begin{minted}{latex} -\begin{luacode} -require('lualibs') -tex.print(number.todimen(tex.sp('1cm'), 'cm', '%0.0F%s')) -\end{luacode} -\end{minted} - -\begin{center} -\begin{tabular}{rl} -\textbf{Unit name} & \textbf{Description} \\\hline -bp & big point \\ -cc & cicero \\ -cm & centimeter \\ -dd & didot \\ -em & horizontal measure of \emph{M} \\ -ex & vertical measure of \emph{x} \\ -in & inch \\ -mm & milimeter \\ -nc & new cicero \\ -nd & new didot \\ -pc & pica \\ -pt & point \\ -sp & scaledpoint \\ -\end{tabular} -\end{center} - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{ - bp = 1bp, - cc = 1cc, - cm = 1cm, - dd = 1dd, - em = 1em, - ex = 1ex, - in = 1in, - mm = 1mm, - nc = 1nc, - nd = 1nd, - pc = 1pc, - pt = 1pt, - sp = 1sp, -} -\end{minted} -\begin{minted}{lua} -{ - ['bp'] = 65781, - ['cc'] = 841489, - ['cm'] = 1864679, - ['dd'] = 70124, - ['em'] = 655360, - ['ex'] = 282460, - ['in'] = 4736286, - ['mm'] = 186467, - ['nc'] = 839105, - ['nd'] = 69925, - ['pc'] = 786432, - ['pt'] = 65536, - ['sp'] = 1, -} -\end{minted} -\end{multicols} - -%% -% -%% - -\subsubsection{string} - -There are two ways to specify strings: With or without quotes. If the -text have to contain commas or equal signs, then double quotation -marks must be used. - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{ - without quotes = no commas and equal signs are allowed, - with double quotes = ", and = are allowed", -} -\end{minted} -\begin{minted}{lua} -{ - ['without quotes'] = 'no commas and equal signs are allowed', - ['with double quotes'] = ', and = are allowed', -} -\end{minted} -\end{multicols} - -\subsubsection{Standalone values} - -Standalone values are values without a key. They are converted into an -array. In Lua an array is a table with numeric indexes (The first index -is 1). - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{one,two,three} -\end{minted} -\columnbreak -\begin{minted}{lua} -{ 'one', 'two', 'three' } -\end{minted} - -\noindent -is equivalent to - -\begin{minted}{lua} -{ - [1] = 'one', - [2] = 'two', - [3] = 'three', -} -\end{minted} -\end{multicols} - -\noindent -All recognized data types can be used as standalone values. - -\begin{multicols}{2} -\begin{minted}{latex} -\luakeysdebug{one,2,3cm} -\end{minted} -\columnbreak -\begin{minted}{lua} -{ 'one', 2, 5594039 } -\end{minted} -\end{multicols} - -%----------------------------------------------------------------------- +%% luakeys.tex +%% Copyright 2021 Josef Friedrich % -%----------------------------------------------------------------------- - -\clearpage - -\section{Exported functions of the Lua module \texttt{luakeys.lua}} - -To learn more about the individual functions (local functions), please -read the \href{https://josef-friedrich.github.io/luakeys/}{source code -documentation}, which was created with -\href{http://stevedonovan.github.io/ldoc/}{LDoc}. The Lua module exports -this functions: - -\begin{minted}{lua} -local luakeys = require('luakeys') -local parse = luakeys.parse -local render = luakeys.render ---local print = luakeys.print -- That would overwrite the built-in Lua function -local save = luakeys.save -local get = luakeys.get -\end{minted} - -%% +% This work may be distributed and/or modified under the +% conditions of the LaTeX Project Public License, either version 1.3c +% of this license or (at your option) any later version. +% The latest version of this license is in +% http://www.latex-project.org/lppl.txt +% and version 1.3c or later is part of all distributions of LaTeX +% version 2008/05/04 or later. % -%% - -\subsection{\texttt{parse(kv\_string, options)}: table} - -The function \lua{parse(input_string, options)} is the main method of -this module. It parses a key-value string into a Lua table. - -\begin{minted}{latex} -\newcommand{\mykeyvalcmd}[1][]{ - \directlua{ - result = luakeys.parse('#1') - luakeys.print(result) - } - #2 -} -\mykeyvalcmd[one=1]{test} -\end{minted} - -\noindent -In plain \TeX: - -\begin{minted}{latex} -\def\mykeyvalcommand#1{ - \directlua{ - result = luakeys.parse('#1') - luakeys.print(result) - } -} -\mykeyvalcmd{one=1} -\end{minted} - -\noindent -The function can be called with a options table. This two options are -supported. - -\begin{minted}{lua} -local result = parse('one,two,three', { - convert_dimensions = false, - unpack_single_array_value = false -}) -\end{minted} - -%% +% This work has the LPPL maintenance status `maintained'. % -%% - -\subsection{\texttt{render(tbl)}: string} - -The function \lua{render(tbl)} reverses the function -\lua{parse(kv_string)}. It takes a Lua table and converts this table -into a key-value string. The resulting string usually has a different -order as the input table. - -\begin{minted}{lua} -result = luakeys.parse('one=1,two=2,tree=3,') -print(luakeys.render(result)) ---- one=1,two=2,tree=3, ---- or: ---- two=2,one=1,tree=3, ---- or: ---- ... -\end{minted} - -\noindent -In Lua only tables with 1-based consecutive integer keys (a.k.a. array -tables) can be parsed in order. - -\begin{minted}{lua} -result = luakeys.parse('one,two,three') -print(luakeys.render(result)) ---- one,two,three, (always) -\end{minted} - -%% -% -%% - -\subsection{\texttt{print(tbl): void}} - -The function \lua{print(tbl)} pretty prints a Lua table to standard -output (stdout). It is a utility function that can be used to debug and -inspect the resulting Lua table of the function \lua{parse}. You have to -compile your \TeX{} document in a console to see the terminal output. - -%\luakeysdebug{level1={level2={key=value}}} - -\begin{minted}{lua} -result = luakeys.parse('level1={level2={key=value}}') -luakeys.print(result) -\end{minted} - -\noindent -The output should look like this: - -\begin{minted}{md} -{ - ['level1'] = { - ['level2'] = { - ['key'] = 'value', - }, -} -\end{minted} - -%% +% The Current Maintainer of this work is Josef Friedrich. % -%% - -\subsection{\texttt{save(identifier, result): void}} - -The function \lua{save(identifier, result)} saves a result (a -table from a previous run of \lua{parse}) under an identifier. -Therefore, it is not necessary to pollute the global namespace to -store results for the later usage. - -%% -% -%% - -\subsection{\texttt{get(identifier): table}} - -The function \lua{get(identifier)} retrieves a saved result from the -result store. - -%----------------------------------------------------------------------- -% -%----------------------------------------------------------------------- - -\clearpage - -\section{Debug packages} - -Two small debug packages are included in |luakeys|. One debug package -can be used in \LaTeX{} (luakeys-debug.sty) and one can be used in plain -\TeX{} (luakeys-debug.tex). Both packages provide only one command: -|\luakeysdebug{kv-string}| - -\begin{minted}{latex} -\luakeysdebug{one,two,three} -\end{minted} - -\noindent -Then the following output should appear in the document: -\bigskip - -\luakeysdebug{one,two,three} - -%% -% -%% - -\subsection{For plain \TeX: luakeys-debug.tex} - -An example of how to use the command in plain \TeX: - -\begin{minted}{latex} -\input luakeys-debug.tex -\luakeysdebug{one,two,three} -\bye -\end{minted} - -%% -% -%% - -\subsection{For \LaTeX: luakeys-debug.sty} - -An example of how to use the command in \LaTeX: - -\begin{minted}{latex} -\documentclass{article} -\usepackage{luakeys-debug} -\begin{document} -\luakeysdebug[ - unpack single array values=false, - convert dimensions=false -]{one,two,three} -\end{document} -\end{minted} - -%----------------------------------------------------------------------- -% -%----------------------------------------------------------------------- - -\clearpage - -\section{Implementation} - -%% -% -%% - -\subsection{luakeys.lua} - -\inputminted[linenos=true]{lua}{luakeys.lua} - -%% -% -%% - -\clearpage - -\subsection{luakeys-debug.tex} - -\inputminted[linenos=true]{latex}{luakeys-debug.tex} - -%% -% -%% - -\clearpage - -\subsection{luakeys-debug.sty} - -\inputminted[linenos=true]{latex}{luakeys-debug.sty} +% This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex +% luakeys-debug.sty and luakeys-debug.tex. -\changes{v0.1}{2021/01/18}{Inital release} -\changes{v0.2}{2021/09/19}{ -* Allow all recognized data types as keys -* Allow TeX macros in the values -* New public Lua functions: save(identifier, result), get(identifier) -} -\pagebreak -\PrintChanges -\pagebreak -\PrintIndex -\end{document} +\directlua{luakeys = require('luakeys')} diff --git a/macros/luatex/latex/pyluatex/README.md b/macros/luatex/latex/pyluatex/README.md index 3165301bd8..feb77a04bc 100644 --- a/macros/luatex/latex/pyluatex/README.md +++ b/macros/luatex/latex/pyluatex/README.md @@ -8,10 +8,6 @@ LaTeX documents must be compiled with LuaLaTeX for this to work. ## Example 1\. LaTeX document `example.tex` -**Note:** PyLuaTeX starts Python 3 using the command `python3` by default. -If `python3` does not start Python 3 on your system, find the correct command -and replace `\usepackage{pyluatex}` with `\usepackage[executable={your python command}]{pyluatex}`. -For example, `\usepackage[executable=python.exe]{pyluatex}`. ```latex \documentclass{article} @@ -36,11 +32,15 @@ $\sqrt{371} = \py{math.sqrt(371)}$ \randint{2}{5} \end{document} ``` +**Note:** PyLuaTeX starts Python 3 using the command `python3` by default. +If `python3` does not start Python 3 on your system, find the correct command +and replace `\usepackage{pyluatex}` with `\usepackage[executable={your python command}]{pyluatex}`. +For example, `\usepackage[executable=python.exe]{pyluatex}`. + 2\. Compile using LuaLaTeX (shell escape is required) ``` lualatex -shell-escape example.tex ``` - **Note:** Running LaTeX with the shell escape option enabled allows arbitrary code to be executed. For this reason, it is recommended to compile trusted documents only. diff --git a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py index 4a17b5f5b0..0e8b32035f 100644 --- a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py +++ b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py @@ -30,8 +30,36 @@ import socketserver import json import textwrap from collections import defaultdict +import re class Interpreter(InteractiveInterpreter): + def execute_repl(self, code, ignore_errors): + self.success = True + output = '' + incomplete = False + for line in re.split('\r?\n', code): + output += ('... ' if incomplete else '>>> ') + line + '\n' + if incomplete: + buffer += '\n' + line + else: + buffer = line + with StringIO() as out, redirect_stdout(out), redirect_stderr(out): + try: + code_obj = compile_command(buffer) + if code_obj is not None: + incomplete = False + self.runcode(code_obj) + else: + incomplete = True + except: + incomplete = False + traceback.print_exc(limit=0) + self.success = False + output += out.getvalue() + if not ignore_errors and not self.success: + return False, output + return self.success, output + def execute(self, code): with StringIO() as out, redirect_stdout(out), redirect_stderr(out): self.success = True @@ -61,7 +89,14 @@ class Handler(socketserver.StreamRequestHandler): data = json.loads(data) interpreter = interpreters[data['session']] - success, output = interpreter.execute(textwrap.dedent(data['code'])) + code = textwrap.dedent(data['code']) + if data['repl_mode']: + success, output = interpreter.execute_repl( + code, + data['ignore_errors'] + ) + else: + success, output = interpreter.execute(code) response = { 'success': success, 'output': output } self.wfile.write((json.dumps(response) + '\n').encode('utf-8')) diff --git a/macros/luatex/latex/pyluatex/pyluatex.lua b/macros/luatex/latex/pyluatex/pyluatex.lua index d7e27bc6cb..6e9c6abb63 100644 --- a/macros/luatex/latex/pyluatex/pyluatex.lua +++ b/macros/luatex/latex/pyluatex/pyluatex.lua @@ -26,6 +26,7 @@ local json = require("pyluatex-json") local socket = require("socket") pyluatex = pyluatex or { + ignore_errors = false, verbose = false, session = "default" } @@ -38,10 +39,15 @@ local tcp = nil local env_end = nil local env_lines = nil local parent_env = nil +local env_repl_mode = false local last_code = nil local last_output = nil +local function trim(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + local function err_cmd(message) return "\\PackageError{PyLuaTeX}{" .. message .. "}{}" end @@ -94,7 +100,7 @@ local function split_lines(str) return t end -function pyluatex.execute(code, auto_print, write) +function pyluatex.execute(code, auto_print, write, repl_mode) local full_code if auto_print then full_code = "print(str(" .. code .. "), end='')" @@ -104,12 +110,19 @@ function pyluatex.execute(code, auto_print, write) if pyluatex.verbose then log_input(full_code) end - local success, output = request({ session = pyluatex.session, code = full_code }) + local success, output = request( + { + session = pyluatex.session, + code = full_code, + repl_mode = repl_mode, + ignore_errors = pyluatex.ignore_errors + } + ) last_code = split_lines(code) last_output = split_lines(output) - if success then - if pyluatex.verbose then log_output(output) end + if success or pyluatex.ignore_errors then + if pyluatex.verbose or not success then log_output(output) end if write then tex.print(last_output) @@ -135,10 +148,14 @@ local function record_line(line) local s, e = line:find(env_end, 1, true) if s ~= nil then luatexbase.remove_from_callback("process_input_buffer", "pyluatex_record_line") - table.insert(env_lines, line:sub(1, s - 1)) + local code_in_line = line:sub(1, s - 1) + if trim(code_in_line):len() > 0 then + -- only include this line if it contains non-whitespace characters + table.insert(env_lines, code_in_line) + end local code = table.concat(env_lines, "\n") - local success = pyluatex.execute(code, false, false) - if success then + local success = pyluatex.execute(code, false, false, env_repl_mode) + if success or pyluatex.ignore_errors then return line:sub(s) else return env_end .. err_cmd("Python error (see above)") .. line:sub(e + 1) @@ -149,20 +166,14 @@ local function record_line(line) end end -function pyluatex.record_env(quiet) - local name +function pyluatex.record_env(name, repl_mode) if parent_env ~= nil then name = parent_env parent_env = nil - else - if quiet then - name = "pythonq" - else - name = "python" - end end env_end = "\\end{" .. name .. "}" env_lines = {} + env_repl_mode = repl_mode luatexbase.add_to_callback("process_input_buffer", record_line, "pyluatex_record_line") end @@ -172,12 +183,18 @@ function pyluatex.set_parent_env(name) end end -function pyluatex.run_file(path, write) +function pyluatex.run_file(path, write, repl_mode) local f = io.open(path, "r") if f then local code = f:read("*a") f:close() - pyluatex.execute(code, false, write) + -- ignore trailing new line if present + if code:sub(-2) == "\r\n" then + code = code:sub(0, -3) + elseif code:sub(-1) == "\n" then + code = code:sub(0, -2) + end + pyluatex.execute(code, false, write, repl_mode) else tex.sprint(err_cmd("File not found: " .. path)) end @@ -191,4 +208,26 @@ function pyluatex.get_last_output() return last_output end +local function parse_bool(name, value) + if value == "true" then + return true + elseif value == "false" then + return false + else + tex.sprint( + err_cmd("Invalid value '" .. value .. "' for option " .. name) + ) + end +end + +function pyluatex.set_option(name, value) + if name == "ignoreerrors" then + pyluatex.ignore_errors = parse_bool(name, value) + elseif name == "verbose" then + pyluatex.verbose = parse_bool(name, value) + else + tex.sprint(err_cmd("Unknown option '" .. name .. "'")) + end +end + return pyluatex diff --git a/macros/luatex/latex/pyluatex/pyluatex.pdf b/macros/luatex/latex/pyluatex/pyluatex.pdf index 49fee5afac..0318eda2cb 100644 Binary files a/macros/luatex/latex/pyluatex/pyluatex.pdf and b/macros/luatex/latex/pyluatex/pyluatex.pdf differ diff --git a/macros/luatex/latex/pyluatex/pyluatex.sty b/macros/luatex/latex/pyluatex/pyluatex.sty index 81fa87ea82..ab015d9638 100644 --- a/macros/luatex/latex/pyluatex/pyluatex.sty +++ b/macros/luatex/latex/pyluatex/pyluatex.sty @@ -9,7 +9,7 @@ %% version 2005/12/01 or later. \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{pyluatex}[2021/08/07 v0.3.0 Execute Python code on the fly] +\ProvidesPackage{pyluatex}[2021/11/05 v0.4.0 Execute Python code on the fly] \RequirePackage{expl3} \ExplSyntaxOn @@ -25,19 +25,32 @@ \RequirePackage{kvoptions} \DeclareStringOption[python3]{executable} -\DeclareVoidOption{verbose}{\directlua{pyluatex.verbose = true}} +\DeclareBoolOption{ignoreerrors} +\DeclareBoolOption{verbose} \ProcessKeyvalOptions* +\ifpyluatex@ignoreerrors + \directlua{pyluatex.ignore_errors = true} +\else + \directlua{pyluatex.ignore_errors = false} +\fi +\ifpyluatex@verbose + \directlua{pyluatex.verbose = true} +\else + \directlua{pyluatex.verbose = false} +\fi \directlua{pyluatex.start([==[\pyluatex@executable]==])} \newcommand*{\PyLTVerbatimEnv}{\directlua{pyluatex.set_parent_env([==[\@currenvir]==])}} -\newenvironment{python}{\directlua{pyluatex.record_env(false)}}% +\newenvironment{python}{\directlua{pyluatex.record_env("python", false)}}% {\directlua{pyluatex.print_env()}} -\newenvironment{pythonq}{\directlua{pyluatex.record_env(true)}}{} +\newenvironment{pythonq}{\directlua{pyluatex.record_env("pythonq", false)}}{} -\newcommand*{\pyluatex@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3)}} +\newenvironment{pythonrepl}{\directlua{pyluatex.record_env("pythonrepl", true)}}{} + +\newcommand*{\pyluatex@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3, false)}} \newcommand*{\py}[1]{\pyluatex@inline{#1}{true}{true}} \newcommand*{\pyq}[1]{\pyluatex@inline{#1}{true}{false}} @@ -46,8 +59,10 @@ \newcommand*{\pycq}[1]{\pyluatex@inline{#1}{false}{false}} \newcommand*{\pysession}[1]{\directlua{pyluatex.session = [==[#1]==]}} +\newcommand*{\pyoption}[2]{\directlua{pyluatex.set_option([==[#1]==], [==[#2]==])}} -\newcommand*{\pyfile}[1]{\directlua{pyluatex.run_file([==[#1]==], true)}} -\newcommand*{\pyfileq}[1]{\directlua{pyluatex.run_file([==[#1]==], false)}} +\newcommand*{\pyfile}[1]{\directlua{pyluatex.run_file([==[#1]==], true, false)}} +\newcommand*{\pyfileq}[1]{\directlua{pyluatex.run_file([==[#1]==], false, false)}} +\newcommand*{\pyfilerepl}[1]{\directlua{pyluatex.run_file([==[#1]==], false, true)}} \endinput diff --git a/macros/luatex/latex/pyluatex/pyluatex.tex b/macros/luatex/latex/pyluatex/pyluatex.tex index d6c0325350..86374249af 100644 --- a/macros/luatex/latex/pyluatex/pyluatex.tex +++ b/macros/luatex/latex/pyluatex/pyluatex.tex @@ -15,7 +15,7 @@ \usepackage{url} \title{The \emph{pyluatex} package} \author{Tobias Enderle\\\url{https://github.com/tndrle/PyLuaTeX}} -\date{v0.3.0 (2021/08/07)} +\date{v0.4.0 (2021/11/05)} \begin{document} \maketitle \raggedright @@ -29,10 +29,6 @@ PyLuaTeX allows you to execute Python code and to include the resulting output i \section{Example} \begin{enumerate} \item \LaTeX{} document \inlcode|example.tex|\\[0.5ex] -\textbf{Note:} PyLuaTeX starts Python 3 using the command \inlcode|python3| by default. -If \inlcode|python3| does not start Python 3 on your system, find the correct command -and replace \inlcode|\usepackage{pyluatex}| with \inlcode|\usepackage[executable={your python command}]{pyluatex}|. -For example, \inlcode|\usepackage[executable=python.exe]{pyluatex}|. \begin{tcblisting}{breakable,listing only, size=fbox,colframe=black!8,boxrule=3pt,colback=black!8} \documentclass{article} @@ -58,28 +54,35 @@ $\sqrt{371} = \py{math.sqrt(371)}$ \randint{2}{5} \end{document} \end{tcblisting} +\textbf{Note:} PyLuaTeX starts Python 3 using the command \inlcode|python3| by default. +If \inlcode|python3| does not start Python 3 on your system, find the correct command +and replace \inlcode|\usepackage{pyluatex}| with \inlcode|\usepackage[executable={your python command}]{pyluatex}|. +For example, \inlcode|\usepackage[executable=python.exe]{pyluatex}|. \item Compile using Lua\LaTeX{} (shell escape is required) \begin{tcblisting}{breakable,listing only, size=fbox,colframe=black!8,boxrule=3pt,colback=black!8} lualatex -shell-escape example.tex \end{tcblisting} -\end{enumerate} \textbf{Note:} Running \LaTeX{} with the shell escape option enabled allows arbitrary code to be executed. For this reason, it is recommended to compile trusted documents only. - +\end{enumerate} \subsection{Further Examples} The folder \inlcode|example| contains additional example documents: \begin{itemize} -\item \inlcode|readme-example.tex|\\[0.5ex] - The example above -\item \inlcode|sessions.tex|\\[0.5ex] - Demonstrates the use of different Python sessions in a document +\item \inlcode|beamer.tex|\\[0.5ex] + Demonstrates the use of PyLuaTeX environments and typesetting in \textit{BEAMER} presentations. In particular, the \inlcode|fragile| option for frames is highlighted. \item \inlcode|data-visualization.tex|\\[0.5ex] Demonstrates the visualization of data using \textit{pgfplots} and \textit{pandas} \item \inlcode|matplotlib-external.tex|\\[0.5ex] Demonstrates how \textit{matplotlib} plots can be generated and included in a document \item \inlcode|matplotlib-pgf.tex|\\[0.5ex] Demonstrates how \textit{matplotlib} plots can be generated and included in a document using \textit{PGF} +\item \inlcode|readme-example.tex|\\[0.5ex] + The example above +\item \inlcode|repl.tex|\\[0.5ex] + Demonstrates how a Python console/REPL can be run and typeset +\item \inlcode|sessions.tex|\\[0.5ex] + Demonstrates the use of different Python sessions in a document \item \inlcode|typesetting-example.tex|\\[0.5ex] The code typesetting example below \item \inlcode|typesetting-listings.tex|\\[0.5ex] @@ -127,12 +130,19 @@ For an example, see the Typesetting Code section. \subsection{Package Options} \begin{itemize} \item \inlcode|verbose|\\[0.5ex] - If this option is enabled, Python input and output is written to the log file.\\[0.5ex] + If this option is set, Python input and output is written to the \LaTeX{} log file.\\[0.5ex] \textit{Example:} \inlcode|\usepackage[verbose]{pyluatex}| \item \inlcode|executable|\\[0.5ex] Specifies the path to the Python executable. (default: \inlcode|python3|)\\[0.5ex] \textit{Example:} \inlcode|\usepackage[executable=/usr/local/bin/python3]{pyluatex}| +\item \inlcode|ignoreerrors|\\[0.5ex] + By default, PyLuaTeX aborts the compilation process when Python reports an error. + If the \inlcode|ignoreerrors| option is set, the compilation process is not aborted.\\[0.5ex] + \textit{Example:} \inlcode|\usepackage[ignoreerrors]{pyluatex}| \end{itemize} +Package options (except for \inlcode|executable|) can be changed in the document with the +\inlcode|\pyoption| command, e.g. \inlcode|\pyoption{verbose}{true}| or \inlcode|\pyoption{ignoreerrors}{false}|. + \subsection{Macros} \begin{itemize} \item \inlcode|\py{code}|\\[0.5ex] @@ -157,6 +167,10 @@ For an example, see the Typesetting Code section. Selects \inlcode|session| as Python session for subsequent Python code.\\[0.5ex] The session that is active at the beginning is \inlcode|default|.\\[0.5ex] \textit{Example:} \inlcode|\pysession{main}| +\item \inlcode|\pyoption{option}{value}|\\[0.5ex] + Assigns \inlcode|value| to the package option \inlcode|option| anywhere in the document. For more information consider + the Package Options section.\\[0.5ex] + \textit{Example:} \inlcode|\pyoption{verbose}{true}| \end{itemize} \subsection{Environments} \begin{itemize} @@ -175,8 +189,13 @@ For an example, see the Typesetting Code section. \end{tcblisting} \item \inlcode|pythonq|\\[0.5ex] Same as the \inlcode|python| environment, but any output is suppressed. +\item \inlcode|pythonrepl|\\[0.5ex] + Executes the provided block of Python code in an interactive console/REPL. Code and output are + stored together in the output buffer and can be typeset as explained in section + Typesetting Code or as shown in the example \inlcode|repl.tex| in the folder + \inlcode|example|. \end{itemize} -You can create your own environments based on the \inlcode|python| and \inlcode|pythonq| environments. +You can create your own environments based on the \inlcode|python|, \inlcode|pythonq| and \inlcode|pythonrepl| environments. However, since they are verbatim environments, you have to use the macro \inlcode|\PyLTVerbatimEnv| in your environment definition, e.g. \begin{tcblisting}{breakable,listing only, @@ -249,6 +268,8 @@ Using a different code listings package like \textit{minted}, or typesetting inl You can also define your own environments that combine Python code and typesetting. See the \inlcode|typesetting-*.tex| examples in the \inlcode|example| folder. +To emulate an interactive Python console/REPL, the \inlcode|pythonrepl| environment can be used. + \section{How It Works} PyLuaTeX runs a Python \inlcode|InteractiveInterpreter|\footnote{\url{https://docs.python.org/3/library/code.html#code.InteractiveInterpreter}} (actually several if you use different sessions) in the background for on the fly code execution. Python code from your \LaTeX{} file is sent to the background interpreter through a TCP socket. -- cgit v1.2.3