diff options
author | Karl Berry <karl@freefriends.org> | 2020-08-14 20:28:12 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2020-08-14 20:28:12 +0000 |
commit | 1c1b1c09b9631a7f0be0aedd24c6f37f7658e9ee (patch) | |
tree | d0f9d4d5425086b8f5b2c66460fb43af0775f006 /Master/texmf-dist | |
parent | 5362413c4d74031afac3641a2d24f6861d526603 (diff) |
luaprogtable (14aug20)
git-svn-id: svn://tug.org/texlive/trunk@56113 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist')
8 files changed, 1724 insertions, 0 deletions
diff --git a/Master/texmf-dist/doc/lualatex/luaprogtable/README.md b/Master/texmf-dist/doc/lualatex/luaprogtable/README.md new file mode 100644 index 00000000000..fb2827a6793 --- /dev/null +++ b/Master/texmf-dist/doc/lualatex/luaprogtable/README.md @@ -0,0 +1,13 @@ +# The `luaprogtable` package: programmatic table interface for `LuaLaTeX` + +The LaTeX3 project provides LaTeX users with a handful of macros to interpret and manipulate various types of objects (e.g. integers, floating point numbers, token lists, sequences, ...) However, there is few existing function that allows users to interact with tables in a programmatic fashion. For example, if a user needs to modify the content of the cell on 20th row and 8th column, he/she needs to navigate the correct cell location among a pile of `&`'s and `\\`'s, which is very inefficient and error-prone. It is very difficult for someone to modify a cell based on the content within it using LaTeX macros. + +`luaprogtable` aims to tackle these problems by providing a series of *programmatic* interface for tables. The `\LPTGetCellData` and `\LPTSetCell` commands allow one to access and alter the content of a single cell. The `lptview` environment allows one to modify a small sub-view of a larger table. + +**For more information, please refer to the [documentation](./luaprogtable-doc.pdf)**. + + + +## License + +This package adopts MIT license.
\ No newline at end of file diff --git a/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.pdf b/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.pdf Binary files differnew file mode 100644 index 00000000000..9839f607957 --- /dev/null +++ b/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.pdf diff --git a/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.tex b/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.tex new file mode 100644 index 00000000000..6c072d5cfe9 --- /dev/null +++ b/Master/texmf-dist/doc/lualatex/luaprogtable/luaprogtable-doc.tex @@ -0,0 +1,616 @@ +\documentclass{l3doc} +\usepackage[T1]{fontenc} +\usepackage[english]{babel} +\usepackage{metalogo} +\usepackage{tikz} +\usepackage[normalem]{ulem} +\usepackage{tcolorbox} +\usepackage{booktabs} +\usepackage{multirow} +\usepackage{colortbl} +\usepackage{scrextend} +\usepackage{graphicx} +\usepackage{luaprogtable} + +\tcbuselibrary{breakable,listings, skins} + +\newcommand{\thispkg}{\pkg{luaprogtable}} + +\title{The \href{https://github.com/xziyue/luaprogtable}{\thispkg}\ package: programmatic table interface for \LuaLaTeX} +\author{\href{https://www.alanshawn.com}{Ziyue ``Alan'' Xiang}\\ +{\scriptsize\ttfamily ziyue.alan.xiang@gmail.com}} +\date{\today} + +\NewDocumentEnvironment{optiondetail}{m}{% +\par\bigskip\noindent \textbf{Description of #1} \medskip% +}{\bigskip} + +\NewDocumentEnvironment{authornote}{}{ +\paragraph{Author's note} +}{\medskip} + +\ExplSyntaxOn + +\dim_new:N \g_enum_indent_dim + +\NewDocumentEnvironment{optionitem}{mm}{ +\par\noindent\texttt{#1~=~#2}\smallskip +\dim_gset:Nn \g_enum_indent_dim {\l__codedoc_trial_width_dim} +\begin{addmargin}[2em]{0em} +}{\end{addmargin}\medskip} + +\newlist{optionlst}{enumerate}{10} +\setlist[optionlst]{label={}, font=\ttfamily, left=\g_enum_indent_dim} + +\newcommand{\optiondf}[1]{\uline{#1}} + +\ExplSyntaxOff + +\newtcblisting{tablesample}{enhanced jigsaw, breakable, colback=white, colframe=black!20} + + +\begin{document} + +\pagenumbering{Roman} + +\maketitle +\tableofcontents + +\clearpage + +\pagenumbering{arabic} + + +\begin{documentation} + +\section{Introduction} + +The \LaTeX3 project provides \LaTeX\ users with a handful of macros to interpret and manipulate various types of objects (e.g. integers, floating point numbers, token lists, sequences, \ldots) However, there is few existing function that allows users to interact with tables in a programmatic fashion. For example, if a user needs to modify the content of the cell on 20\textsuperscript{th} row and 8\textsuperscript{th} column, he/she needs to navigate the correct cell location among a pile of |&|'s and |\\|'s, which is very inefficient and error-prone. It is very difficult for someone to modify a cell based on the content within it using \LaTeX\ macros. + +\thispkg\ aims to tackle these problems by providing a series of \emph{programmatic} interface for tables. The \cs{LPTGetCellData} and \cs{LPTSetCell} commands allow one to access and alter the content of a single cell. The \env{lptview} environment allows one to modify a small sub-view of a larger table. + +\section{Basic Usage} + +\subsection{Concepts} + +\begin{itemize} +\item Coordinate system + +This package uses a 1-indexed row-column-order coordinate system. + +\begin{center} + \ExplSyntaxOn + \int_new:N \l_tmpc_int + + \begin{tikzpicture} + \int_set:Nn \l_tmpa_int {1} + + \int_do_while:nNnn { \l_tmpa_int } < { 5 } { + \int_set:Nn \l_tmpb_int {0} + \int_set:Nn \l_tmpc_int { \l_tmpa_int } + \int_do_while:nNnn {\l_tmpb_int} < {\l_tmpa_int} { + \node[minimum~width=1.5cm, + minimum~height=0.6cm, + outer~sep=0cm, draw=black] at + (\fp_eval:n {1.5 * \l_tmpb_int}cm, \fp_eval:n {0.6 * \l_tmpa_int }cm) + {\small (\int_eval:n {5 - \l_tmpa_int},~ \int_eval:n {\l_tmpb_int + 1}) }; + \int_incr:N \l_tmpb_int + } + \int_incr:N \l_tmpa_int + } + \end{tikzpicture} + \ExplSyntaxOff +\end{center} + +\item \hypertarget{index-expr}{Index expressions} + +This package adopts a \emph{Pythonic} indexing convention: each \emph{component} is delimited by `|,|'; within each component, a \emph{range} is separated by a colon (`|:|'). For example, the expression |1,2,3:5| has three components, where the first and second components contain a single index 1, 2, respectively; the third component holds the range $[3, 5]$. + +To comply with \LaTeX\ conventions, both ends of a range are \emph{inclusive}. Therefore, the range |3:5| is made up of three indices, namely 3, 4 and 5. Negative indices indicate reverse access. For example, |-1| refers to the last element; the range |3:-1| contains all indices between 3 and the last element (both ends included). + + +\item \hypertarget{view-expr}{View expressions} + +In \LaTeX , tables are usually constructed with `|&|' and `|\\|'. However, it is very difficult to write a simple parser for this syntax, because these special symbols can appear in other environments with different meanings. For simplicity, \thispkg\ builds table using \emph{view expressions}. In view expressions, each column is enclosed with braces (|{}|); rows are broken with |\\|. For example, the view expression on the right is equivalent to traditional \LaTeX\ table notation on the left. Note that \textbf{one cannot abbreviate outermost braces in view expressions}. + +\begin{center} +\begin{BVerbatim}[gobble=0] +a & b & c & d \\ +e & f & g & h \\ +i & j & k & l +\end{BVerbatim} +\hspace{1cm} +\begin{BVerbatim}[gobble=0] +{a} {b} {c} {d} \\ +{e} {f} {g} {h} \\ +{i} {j} {k} {l} +\end{BVerbatim} +\end{center} + +Sometimes table cells can span across multiple rows/columns. Because \thispkg\ cannot parse \LaTeX\ content, one needs to specify the shape of the cell explicitly for these scenarios. The shape of a cell can be altered by adding |[]| after the closing brace. If a cell spans $n$ columns, one can type |[-n]|; if a cell spans $n$ rows, one can type \verb`[|n]`. For example, view expression (2) is equivalent to traditional \LaTeX\ table (1). + +\begin{enumerate}[label=(\arabic*)] +\item +\begin{BVerbatim}[baseline=t, gobble=0] +\multirow{3}{*}{a} & b & c & d \\ + & \multicolumn{3}{c}{e} \\ + & f & g & h +\end{BVerbatim} + + +\item +\begin{BVerbatim}[baseline=t, gobble=0] +{\multirow{3}{*}{a}}[|3] {b} {c} {d} \\ + {\multicolumn{3}{c}{e}}[-3] \\ + {f} {g} {h} +\end{BVerbatim} + +\end{enumerate} + + +\end{itemize} + +\subsection{Creating a new table} + +% \LPTNewTable{table1}{5}{|c|c|c|c|c|}[nrows=5, input method=file] +\begin{function}{\LPTNewTable} + \begin{syntax} + \cs{LPTNewTable} \Arg{table name} \Arg{num cols} \Arg{table preamble} \oarg{table options} + \end{syntax} + + Creates a new table named after \meta{table name}. The name of the new table must not be the same as existing ones. The number of columns specified in \meta{num cols} needs to match \meta{table preamble} for the table to work correctly. In general, \meta{table name} should not contain comma and back slash. Starting and trailing white spaces in \meta{table name} will be ignored. + + \begin{optiondetail}{\meta{table options}} + \begin{optionitem}{backend}{\optiondf{\{tabular\}}} + Specifies the table environment to be used for this table. Apart from |tabular|, one can also use |longtable|, |tabu| and so on. However, the corresponding package must be loaded manually. + \end{optionitem} + \begin{optionitem}{default before line}{\optiondf{\{\}}} + Specifies the default line style before each row. + \end{optionitem} + \begin{optionitem}{default after line}{\optiondf{\{\}}} + Specifies the default line style after each row. + \end{optionitem} + \begin{optionitem}{default after spacing}{\optiondf{\{\}}} + Specifies the default additional spacing after each row. This is achieved by appending |[<dim expr>]| to each row. + \end{optionitem} + \begin{optionitem}{input method}{file,\optiondf{stringbuffer}} + Specifies how \LaTeX\ will read the table source generated by \thispkg. + \begin{optionlst} + \item[file] The constructed table source will be saved to file system as: + \begin{verbatim} + \jobname_<table name>.table + \end{verbatim} + + It is then read into \LaTeX\ by |\input| macro. This is ideal for debug purposes because the source is visible to the user. + \item[stringbuffer] The constructed table source will be fed into \LaTeX\ directly, without the need of file system operations. On \LaTeX\ side, this is still achieved by calling |\input|. However, the corresponding file callback functions on Lua side are changed, which allows \LaTeX\ to read from Lua string buffers directly. + \end{optionlst} + \end{optionitem} + \begin{optionitem}{nrows}{\optiondf{\{0\}}} + Specifies the number of rows in the table. + \end{optionitem} + \end{optiondetail} + +\end{function} + +\subsection{Selecting current table} + +\begin{function}{\LPTSetCurrentTable} + \begin{syntax} + \cs{LPTSetCurrentTable} \Arg{table name} + \end{syntax} + + For many subsequent commands, there is no need to specify \meta{table name} repeatedly: they fetch this information from a global variable. This macro sets the global variable for current table. +\end{function} + +\begin{function}{\LPTGetCurrentTable} + \begin{syntax} + \cs{LPTGetCurrentTable} + \end{syntax} + + Get the Lua-escaped name of current table. +\end{function} + +\subsection{Modifying table rows} + +\begin{function}{\LPTAddRow} + + \begin{syntax} + \cs{LPTAddRow} \oarg{row options} + \end{syntax} + + This command appends one more row to the current table. If an option is not specified in \meta{row options}, the default value is taken from \meta{table options} of the table. + + \begin{optiondetail}{\meta{row options}} + \begin{optionitem}{before line}{\optiondf{\{\}}} + Specifies the line before this row. + \end{optionitem} + \begin{optionitem}{after line}{\optiondf{\{\}}} + Specifies the line after this row. + \end{optionitem} + \begin{optionitem}{after spacing}{\optiondf{\{\}}} + Specifies the extra spacing after this row. + \end{optionitem} + \end{optiondetail} + +\end{function} + + +\begin{function}{\LPTSetRowProp} + + \begin{syntax} + \cs{LPTSetRowProp} \Arg{index expr} \Arg{row options} + \end{syntax} + + This command modifies the properties of rows specified in \meta{index expr}. In this case, \meta{index expr} can point to multiple rows. For example, the index expression |:3,4,6| will trigger the modification of 5 rows, namely row 1, 2, 3, 4 and 6. + + \begin{optiondetail}{\meta{row options}} + \begin{optionitem}{before line}{\optiondf{\{\}}} + Specifies the line before this row. + \end{optionitem} + \begin{optionitem}{after line}{\optiondf{\{\}}} + Specifies the line after this row. + \end{optionitem} + \begin{optionitem}{after spacing}{\optiondf{\{\}}} + Specifies the extra spacing after this row. + \end{optionitem} + \end{optiondetail} + +\end{function} + +\subsection{Modifying table contents} + +\begin{environment}{lptview} + +\begin{syntax} +|\begin{lptview}| \Arg{index expr} +\Arg{view expr} +|\end{lptview}| +\end{syntax} + +This environment creates a sub-view of current table. The sub-view region is specified by \meta{index expr}, and the content of this sub-view is specified by \meta{view expr}. The index expression should always consist of two components, where the first defines the range of rows and the second defines the range of columns. +\end{environment} + + +\begin{environment}{lptfill} + +\begin{syntax} +|\begin{lptfill}| \Arg{index expr} +\Arg{content} +|\end{lptfill}| +\end{syntax} + +This environment fills table region specified by \meta{index expr} with \meta{content}. When \meta{index expr} is empty, the entire table is filled. Even when \meta{index expr} is empty, the braces surrounding it cannot be abbreviated. + +\end{environment} + +\subsection{Using table source} + +\begin{function}{\LPTUseTable} + +\begin{syntax} +\cs{LPTUseTable} +\end{syntax} + +Reads the source of current table into input stream. + +\end{function} + +\subsection{Deleting existing tables} + +\begin{function}{\LPTDeleteTable} + +\begin{syntax} +\cs{LPTDeleteTable} \Arg{table name} +\end{syntax} + +Remove a table from Lua storage to save memory. + +\end{function} + +\section{Advanced Usage} + +\subsection{Internal design} + +In Lua, each cell is a \emph{class} with three attributes: |data|, |shape| and |parent|. Apparently, |data| holds the content of the cell. By default, the |shape| of all cells is |{1,1}|; the parent of all cells is |nil|. When there is a cell that spans multiple rows/columns, the top-left cell will be considered \emph{parent cell}, and the remaining cells in the region would have |shape| set to |nil| and |parent| set to the coordinates of parent cell. + +\subsection{Programmatic interface} + +The following macros allow one to access and modify table cells programmatically. + +\begin{function}{\LPTSetCell} + +\begin{syntax} +\cs{LPTSetCell} \Arg{index expr} \oarg{shape} \Arg{content} +\end{syntax} + +Set the content of the cell specified in \meta{index expr} to \meta{content}. The index expression should always consist of two components, where each component only points to one integer. The shape is given by |<row span>, <column span>|. By default, the shape of a cell is |1,1|. When a cell occupies more then one cell space, the shape of its children cells will be set to |nil| automatically. + +\begin{authornote} +\cs{LPTSetCell} is not completely identical to \env{lptview}. More concretely, \env{lptview} and \env{lptfill} override \LuaLaTeX 's |process_input_buffer| callback, which allows Lua side to receive verbatim content as is. However, the \meta{content} of \cs{LPTSetCell} needs to be processed by \cs{tl_to_str:n} before being passed to Lua. While the outcome is the same most of the time, \cs{tl_to_str:n} does append an empty space after macros, which stops verbatim commands from working properly. +\end{authornote} + +\end{function} + +\begin{function}{\LPTFill} +\begin{syntax} +\cs{LPTFill} \Arg{index expr} \Arg{content} +\end{syntax} + +Fills table region specified by \meta{index expr} with \meta{content}. When \meta{index expr} is empty, the entire table is filled. + +\end{function} + +\begin{function}{\LPTGetTableNames} +\begin{syntax} +\cs{LPTGetTableNames} +\end{syntax} + +Returns a comma-separated string containing the names of all tables. +\end{function} + +\begin{function}{\LPTGetTableShape} +\begin{syntax} +\cs{LPTGetTableShape} +\end{syntax} + +Returns the shape of the current table as a token list string. The number of rows is stored in first group; the number of columns is stored in second group. +\end{function} + +\begin{function}{\LPTGetCellData} +\begin{syntax} +\cs{LPTGetCellData} \Arg{index expr} +\end{syntax} + +Returns the data of the cell specified by \meta{index expr}. +\end{function} + +\begin{function}{\LPTGetCellShape} +\begin{syntax} +\cs{LPTGetCellShape} \Arg{index expr} +\end{syntax} + +Returns the shape of the cell specified by \meta{index expr} as a token list string. The number of rows is stored in first group; the number of columns is stored in second group. When the shape is |nil|, the macro returns |\c_novalue_tl|. +\end{function} + +\begin{function}{\LPTGetCellParent} +\begin{syntax} +\cs{LPTGetCellParent} \Arg{index expr} +\end{syntax} + +Returns the coordinates of the parent of the cell specified by \meta{index expr} as a token list string. The row index is stored in first group; the column index is stored in second group. When the parent is |nil|, the macro returns |\c_novalue_tl|. +\end{function} + +\section{Examples} + +\subsection{Creating and filling a table} + +\begin{tablesample} +\LPTNewTable{oruVVAVhbMD0}{3}{|c|c|c|}[ + default after line=\hline, + nrows=3] +\LPTSetCurrentTable{oruVVAVhbMD0} +\LPTSetRowProp{1}{before line=\hline} +\begin{lptfill}{} +\verb|#&_^| +\end{lptfill} +\LPTUseTable +\end{tablesample} + + +\subsection{Changing sub-table} + +\begin{tablesample} +\LPTNewTable{IAs5OwqBcv0R}{4}{|c|c|c|c|}[ + default after line=\cline{2-4}, + nrows=4] +\LPTSetCurrentTable{IAs5OwqBcv0R} +\LPTFill{:-2,:-2}{Lorem} +\LPTFill{-1,2:-2}{Sit} +\LPTFill{:,-1}{Dolor} +\LPTSetRowProp{1}{before line=\hline} +\LPTSetRowProp{-1}{after line=\hline} +\begin{lptview}{:, 1} +{ \multirow{4}{*}{\rotatebox{90}{Ipsum}} }[|4] \\ \\ \\ +\end{lptview} +\LPTUseTable +\end{tablesample} + +\begin{tablesample} +\LPTNewTable{4Fz0h0ES2zU9}{6}{|c|c|c|c|c|c|}[ + default after line=\hline, + nrows=6] +\LPTSetCurrentTable{4Fz0h0ES2zU9} +\begin{lptfill}{} +\verb|Lorem| +\end{lptfill} +\LPTSetRowProp{1}{before line=\hline} +\LPTSetRowProp{3}{after line=\cline{1-2}\cline{5-6}} +\begin{lptview}{3:4, 3:4} +{ \multicolumn{2}{c|}{\multirow{2}{*}{Ipsum}} }[-2]\\ +{ \multicolumn{2}{c|}{} }[-2] +\end{lptview} +\LPTUseTable +\end{tablesample} + +\subsection{Modifying row spacing} + +\begin{tablesample} +\LPTNewTable{KSDJ4C6wUgXL}{4}{|c|c|c|c|}[ + default after line=\hline, + nrows=4] +\LPTSetCurrentTable{KSDJ4C6wUgXL} +\LPTFill{}{$\int_a^b f(x) dx$} +\LPTSetRowProp{1}{before line=\hline} +\LPTSetRowProp{-2:-1}{after spacing=1em} +\LPTUseTable +\end{tablesample} + +\subsection{Sequentially constructing a table} + +\begin{tablesample} +\LPTNewTable{yTvLL6PEYgoE}{3}{ccc} +\LPTSetCurrentTable{yTvLL6PEYgoE} +\LPTAddRow[before line=\toprule, after line=\midrule] +\begin{lptview}{-1,:} +{Field 1} {Field 2} {Field 3} +\end{lptview} +\LPTAddRow +\begin{lptview}{-1,:} +{Data 1} {Data 2} {Data 3} +\end{lptview} +\LPTAddRow[after line=\bottomrule] +\begin{lptview}{-1,:} +{Data 4} {\multicolumn{2}{c}{Data 5}}[-2] +\end{lptview} +\LPTUseTable +\end{tablesample} + +\subsection{Change the color of cells based on value} + +\begin{tablesample} +\LPTNewTable{hJXHnablQ14y}{8}{cccccccc}[nrows=8] +\LPTSetCurrentTable{hJXHnablQ14y} +\begin{lptview}{:,:} +{20} {90} {43} {36} {73} {72} {77} {68} \\ +{60} {48} {41} {52} {39} {31} {90} {65} \\ +{81} {47} {58} {62} {67} {35} {49} {51} \\ +{85} {41} {59} {69} {46} {77} {46} {39} \\ +{24} {64} {69} {64} {89} {90} {64} {67} \\ +{27} {75} {47} {40} {43} {63} {29} {27} \\ +{86} {21} {40} {79} {55} {40} {36} {40} \\ +{71} {63} {65} {53} {74} {58} {75} {63} +\end{lptview} +\ExplSyntaxOn +\int_step_inline:nn {8} { + \int_step_inline:nn {8} { + \str_set:Nx \l_tmpa_str {\LPTGetCellData{#1,##1}} + \tl_set:Nx \l_tmpa_tl {\int_eval:n {100 - \l_tmpa_str}} + \str_set:Nx \l_tmpb_str {\exp_not:N\cellcolor{black!\l_tmpa_str} + \exp_not:N\color{blue!\l_tmpa_tl}\l_tmpa_str} + \exp_args:Nno \LPTSetCell {#1,##1} {\l_tmpb_str} + } +} +\ExplSyntaxOff +\LPTUseTable +\end{tablesample} + +\subsection{Listing the name and shape of all tables} + + +\begin{tablesample} +\ExplSyntaxOn +\clist_set:Nx \l_tmpa_clist {\LPTGetTableNames} +\LPTNewTable{oOnXsQcb7f8j}{2}{cc} +\LPTSetCurrentTable{oOnXsQcb7f8j} +\LPTAddRow +\begin{lptview}{1,:} +{Table~Name} {Shape} +\end{lptview} +\clist_map_inline:Nn \l_tmpa_clist { + \LPTAddRow + \LPTSetCell{-1,1}{\texttt{#1}} + \LPTSetCurrentTable{#1} + \tl_set:Nx \l_tmpa_tl {\LPTGetTableShape} + \LPTSetCurrentTable{oOnXsQcb7f8j} + \tl_set:Nx \l_tmpb_tl {$(\tl_item:Nn \l_tmpa_tl {1}, + \tl_item:Nn \l_tmpa_tl {2})$} + \exp_args:Nno \LPTSetCell {-1,2} {\l_tmpb_tl} +} +\LPTSetRowProp{1}{before~line=\toprule, after~line=\midrule} +\LPTSetRowProp{-1}{after~line=\bottomrule} +\LPTUseTable +\ExplSyntaxOff +\end{tablesample} + +\section{Test cases} + +\subsection*{Testing robustness of table modification} + +\begin{tablesample} +\LPTNewTable{mnEfCpDkN3OL}{6}{|c|c|c|c|c|c|}[ + default after line=\hline, + nrows=6] +\LPTSetCurrentTable{mnEfCpDkN3OL} +\begin{lptfill}{} +\verb|Lorem| +\end{lptfill} +\LPTSetRowProp{1}{before line=\hline} +\LPTSetRowProp{3}{after line=\cline{1-2}\cline{5-6}} +\begin{lptview}{3:4, 3:4} +{ \multicolumn{2}{c|}{\multirow{2}{*}{Ipsum}} }[-2]\\ +{ \multicolumn{2}{c|}{} }[-2] +\end{lptview} +\LPTUseTable\par\vspace*{1em} +\begin{lptview}{3:4, 3:4} +{ \multicolumn{2}{c|}{Change} }[-2]\\ +{ \multicolumn{2}{c|}{Change} }[-2] +\end{lptview} +\LPTUseTable\par\vspace*{1em} +\begin{lptview}{3:4, 3:4} +{A} {B}\\ +{C} {D} +\end{lptview} +\LPTUseTable +\end{tablesample} + + +\begin{tablesample} +\LPTNewTable{869CviFSrnEy}{6}{|c|c|c|c|c|c|}[ + default after line=\hline, + nrows=6] +\LPTSetCurrentTable{869CviFSrnEy} +\begin{lptfill}{} +\verb|Lorem| +\end{lptfill} +\LPTSetRowProp{1}{before line=\hline} +\LPTSetRowProp{3}{after line=\cline{1-2}\cline{5-6}} +\LPTSetCell{3,3}[1,2]{\multicolumn{2}{c|}{\multirow{2}{*}{Ipsum}}} +\LPTSetCell{4,3}[1,2]{\multicolumn{2}{c|}{}} +\LPTUseTable\par\vspace*{1em} +\LPTSetCell{3,3}[1,2]{\multicolumn{2}{c|}{Change}} +\LPTSetCell{4,3}[1,2]{\multicolumn{2}{c|}{Change}} +\LPTUseTable\par\vspace*{1em} +Notice how \verb|Lorem| appears because we haven't changed +the values of cell $(3,4)$ and $(4,4)$.\par +\LPTSetCell{3,3}{A} +\LPTSetCell{4,3}{C} +\LPTUseTable\par\vspace*{1em} +\end{tablesample} + +\subsection*{Testing other functions} + +\begin{tablesample} +\LPTNewTable{kCYKq42SyQtf}{5}{|c|c|c|c|c|}[ + default after line=\hline, + nrows=6] +\LPTSetCurrentTable{kCYKq42SyQtf} +\LPTSetRowProp{1}{before line=\hline} +\ExplSyntaxOn +\int_step_inline:nn {6} { + \int_step_inline:nn {5} { + \tl_set:Nx \l_tmpa_tl {\LPTGetCellShape{#1,##1}} + \tl_set:Nx \l_tmpb_tl {shape=\tl_item:Nn \l_tmpa_tl {1}, + \tl_item:Nn \l_tmpa_tl {2}} + \exp_args:Nno \LPTSetCell {#1,##1} {\l_tmpb_tl} + } +} +\ExplSyntaxOff +\LPTUseTable\par\vspace*{1em} +\ExplSyntaxOn +\int_step_inline:nn {6} { + \int_step_inline:nn {5} { + \tl_set:Nx \l_tmpa_tl {\LPTGetCellParent{#1,##1}} + \tl_if_eq:NNTF \l_tmpa_tl \c_novalue_tl { + \LPTSetCell {#1,##1} {NoP} + }{ + \LPTSetCell {#1,##1} {HasP} + } + } +} +\LPTUseTable +\ExplSyntaxOff +\end{tablesample} + +\end{documentation} + +\end{document}
\ No newline at end of file diff --git a/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-parse.lua b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-parse.lua new file mode 100644 index 00000000000..8947535a227 --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-parse.lua @@ -0,0 +1,116 @@ +require "luaprogtable-utility.lua" + +local _std_shape = {1, 1} + +function _lpt_parse_new_cell() + local cur_cell = {data={}, shape=_std_shape} + return cur_cell +end + +function _lpt_parse_table_view(str) + local str = _lpt_strip(str) + + local level = 0 + local pos = 1 + + local cur_cell = nil + local cur_row = {} + local ret = {cur_row} + + function _add_str(s) + if level > 0 then + table.insert(cur_cell["data"], s) + end + end + + while pos <= str:len() do + local chr = str:sub(pos, pos) + local next_pos = pos + 1 + + if chr == "\\" then + local next_chr = str:sub(pos + 1, pos + 1) + if next_chr == nil then + -- do nothing + elseif next_chr == "\\" then + if level == 0 then + cur_row = {} + -- change of row + table.insert(ret, cur_row) + end + elseif next_chr == "{" or next_chr == "}" then + next_pos = pos + 2 + _add_str(str:sub(pos, pos + 1)) + else + _add_str(chr) + end + elseif chr == "{" then + if level == 0 then + cur_cell = _lpt_parse_new_cell() + else + _add_str(chr) + end + level = level + 1 + elseif chr == "}" then + level = level - 1 + if level < 0 then + raise_error("extra '}' found when parsing table view expression") + elseif level == 0 then + local data_str = table.concat(cur_cell["data"], "") + cur_cell["data"] = data_str + table.insert(cur_row, cur_cell) + cur_cell = nil + else + _add_str(chr) + end + elseif chr == "[" then + if level == 0 then + local close = string.find(str, "]", pos + 1) + + if close == nil then + raise_error("unable to find closing ']'") + end + + local size_str = str:sub(pos + 1, close - 1) + size_str = _lpt_strip(size_str) + + local size_1 = size_str:sub(1, 1) + local shape = nil + + if size_1 == "|" then + local nrow = _lpt_tonumber(size_str:sub(2)) + shape = {nrow, 1} + elseif size_1 == "-" then + local ncol = _lpt_tonumber(size_str:sub(2)) + shape = {1, ncol} + else + raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str)) + end + + if shape[1] < 1 or shape[2] < 1 then + raise_error(string.format("invalid size '[%s]' in table view, size_str", size_str)) + end + + local n_cell = #cur_row + local cell = cur_row[n_cell] + if cell == nil then + raise_error(string.format("no suitable cell for size [%s]", size_str)) + end + cell["shape"] = shape + + pos = close + 1 + else + _add_str(chr) + end + else + _add_str(chr) + end + + pos = next_pos + end + + if level ~= 0 then + raise_error("extra '{' found when parsing table view expression") + end + + return ret +end
\ No newline at end of file diff --git a/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-stringbuffer.lua b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-stringbuffer.lua new file mode 100644 index 00000000000..80ba990776e --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-stringbuffer.lua @@ -0,0 +1,74 @@ +TeXStringBuffer = {content={}, finished=false} + +function TeXStringBuffer:new() + o = {} + setmetatable(o, self) + self.__index = self + return o +end + +function TeXStringBuffer:clear() + while #self.content ~= 0 do rawset(self.content, #self.content, nil) end +end + +function TeXStringBuffer:content_to_string() + return table.concat(self.content, "") +end + +function TeXStringBuffer:show() + tex.write(self:content_to_string()) +end + +function TeXStringBuffer:append(data) + table.insert(self.content, data) +end + +function TeXStringBuffer:append_carriage_return(data) + self:append("\r") +end + +function _tex_buffer_remove_callback(name, description) + for k, v in pairs(luatexbase.callback_descriptions(name)) do + if v == description then + texio.write("\nsafely removing callback " .. name .. " : " .. description) + luatexbase.remove_from_callback(name, description) + end + end +end + +function tex_buffer_remove_callback() + _tex_buffer_remove_callback("find_read_file", "tex_file_buffer_find") + _tex_buffer_remove_callback("open_read_file", "tex_file_buffer_read") +end + +function tex_file_buffer_reader(env) + local ret = nil + if not env["finished"] then + ret = env["content"] + env["finished"] = true + -- remove callback immediately + tex_buffer_remove_callback() + end + return ret +end + +function tex_file_buffer_find(id_number, asked_name) + -- arguments and return value doesn't matter + texio.write("\nTeXStringBuffer tries to find ".. asked_name .. " id=" .. id_number) + return asked_name +end + +function TeXStringBuffer:register_callback() + tex_file_buffer_read = function (filename) + local env = {} + texio.write("\nTeXStringBuffer opens ".. filename) + env["finished"] = false + env["content"] = self:content_to_string() + env["reader"] = tex_file_buffer_reader + return env + end + + -- register callback + luatexbase.add_to_callback("find_read_file", tex_file_buffer_find, "tex_file_buffer_find") + luatexbase.add_to_callback("open_read_file", tex_file_buffer_read, "tex_file_buffer_read") +end diff --git a/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-utility.lua b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-utility.lua new file mode 100644 index 00000000000..4ac10f5fd50 --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable-utility.lua @@ -0,0 +1,109 @@ +inspect = require "inspect.lua" + +function raise_error(...) + local arg = {...} + local description = "a luaprogtable package exception occured:\n" .. table.concat(arg, ", ") + tex.error(description) + error(description) +end + +local _spacer = lpeg.S(" \t\f\v\n\r") +local _non_spacer = 1 - _spacer +local _stripper = _spacer^0 * lpeg.C((_spacer^0 * _non_spacer^1)^0) + + +function _lpt_strip(s) + return s and lpeg.match(_stripper,s) or "" +end + +function _lpt_split(s, sep) + sep = lpeg.P(sep) + local elem = lpeg.C((1 - sep)^0) + local p = lpeg.Ct(elem * (sep * elem)^0) -- make a table capture + return lpeg.match(p, s) +end + + +function _lpt_tonumber(s) + local n = tonumber(s) + if n == nil then + raise_error(string.format("'{%s}' is not a valid number expression", s)) + end + return n +end + + +function _lpt_parse_index_expr(expr) + local expr = _lpt_strip(expr) + local parts = _lpt_split(expr, ",") + local ret = {} + + for i=1,#parts do + local subexpr = _lpt_strip(parts[i]) + + if subexpr:len() == 0 then + raise_error(string.format("empty field in index expression '%s'", expr)) + end + + local colon_pos = subexpr:find(":") + local str_l = "" + local str_r = "" + + if colon_pos == nil then + str_l = subexpr + str_r = subexpr + else + local subsubexpr = _lpt_split(subexpr, ":") + str_l = _lpt_strip(subsubexpr[1]) + str_r = _lpt_strip(subsubexpr[2]) + if str_l:len() == 0 then + str_l = "1" + end + if str_r:len() == 0 then + str_r = "-1" + end + end + + local n_l = _lpt_tonumber(str_l) + local n_r = _lpt_tonumber(str_r) + + table.insert(ret, {n_l, n_r}) + end + + return ret +end + +function _lpt_parse_shape(shape_expr) + local shape_expr = _lpt_strip(shape_expr) + local parts = _lpt_split(shape_expr, ",") + if #parts ~= 2 then + raise_error(string.format("invalid shape: '%s'", shape_expr)) + end + local shape1 = _lpt_tonumber(parts[1]) + local shape2 = _lpt_tonumber(parts[2]) + if shape1 < 1 or shape2 < 1 then + raise_error(string.format("invalid shape: '%s'", shape_expr)) + end + return {shape1, shape2} +end + +function table.slice(tbl, first, last, step) + local sliced = {} + + for i = first or 1, last or #tbl, step or 1 do + sliced[#sliced+1] = tbl[i] + end + + return sliced +end + +function table.find(tbl, item, index) + for i=index or 1, #tbl do + local tbl_item = tbl[i] + if tbl_item == item then + return i + end + end + return nil +end + diff --git a/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.lua b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.lua new file mode 100644 index 00000000000..1e3c2a76570 --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.lua @@ -0,0 +1,559 @@ +require "luaprogtable-parse.lua" +require "luaprogtable-stringbuffer.lua" + +TeXTable = {} +TeXTable.__index = TeXTable + +function TeXTable:new() + local ret = { + rows = {} + } + setmetatable(ret, TeXTable) + return ret +end + +TeXTableCell = {} +TeXTableCell.__index = TeXTableCell + +local _std_shape = {1, 1} + +function TeXTableCell:new(default_val) + local ret = { + data=default_val, + shape=_std_shape, + parent=nil + } + setmetatable(ret, TeXTableCell) + return ret +end + + +TeXTableRow = {} +TeXTableRow.__index = TeXTableRow + +function TeXTableRow:new(tex_table) + + local ncols = tex_table.ncols + local default_val = rawget(tex_table, "default value") + + local cells = {} + + for i=1,ncols do + table.insert(cells, TeXTableCell:new(default_val)) + end + + local ret = { + ["before line"] = "", + ["after line"] = rawget(tex_table, "default after line"), + ["after spacing"] = rawget(tex_table, "default after spacing"), + ["cells"] = cells + } + + setmetatable(ret, TeXTableRow) + return ret +end + + +function TeXTable:resize(nrows, ncols) + local now_nrows = #self.rows + local now_ncols = self.ncols + + if nrows < now_nrows or ncols < now_ncols then + raise_error("'TeXTable:resize' doesn not support table truncation") + end + + local nrows_delta = nrows - now_nrows + local ncols_delta = ncols - now_ncols + + -- change column for existing rows + self.ncols = ncols + for i=1,now_nrows do + local row = self.rows[i] + for j=1,ncols_delta do + table.insert(row.cells, TeXTableCell:new(rawget(self, "default value"))) + end + end + + -- add new rows + for i=1,nrows_delta do + table.insert(self.rows, TeXTableRow:new(self)) + end +end + +function TeXTable:to_string(no_linebreak) + local ret = {} + local no_linebreak = no_linebreak or false + local lb = "\n" + if no_linebreak then + lb = " " + end + + table.insert(ret, string.format([[\begin{%s}{%s}]], self.backend, rawget(self, "table preamble"))) + + local nrows = #self.rows + for i=1,nrows do + + local row = self.rows[i] + + local cell_data = {} + local j = 1 + + while j <= self.ncols do + local cell = row.cells[j] + + local next_j = nil + + if cell.shape ~= nil then + table.insert(cell_data, cell.data) + next_j = j + cell.shape[2] + else + local parent_row = cell.parent[1] + local parent_col = cell.parent[2] + local parent_cell = self:get_cell(parent_row, parent_col) + table.insert(cell_data, "") + next_j = j + parent_cell.shape[2] + end + + j = next_j + end + + local row_str = "" + + if string.len(rawget(row, "before line")) > 0 then + row_str = rawget(row, "before line") .. lb .. row_str + end + + row_str = row_str .. table.concat(cell_data, " & ") .. [[ \\]] + + if string.len(rawget(row, "after spacing")) > 0 then + row_str = row_str .. "[" .. rawget(row, "after spacing") .. "]" + end + + row_str = row_str .. rawget(row, "after line") + + table.insert(ret, row_str) + end + + table.insert(ret, string.format([[\end{%s}]], self.backend)) + + return table.concat(ret, lb) + +end + + +function TeXTable:get_cell(row_id, cell_id) + local row = self.rows[row_id] + if row == nil then + raise_error(string.format("row id '%d' is invalid for table '%s'", row_id, rawget(self, "table name"))) + end + local cell = row.cells[cell_id] + if cell == nil then + raise_error(string.format("cell location '(%d,%d)' is invalid for table '%s'", row_id, cell_id, rawget(self, "table name"))) + end + return cell +end + +function TeXTable:set_cell(row_id, cell_id, data, shape) + local cell = self:get_cell(row_id, cell_id) + + -- check if we can modify this cell + if cell.parent ~= nil then + local parent_1 = cell.parent[1] + local parent_2 = cell.parent[2] + raise_error(string.format("cell location '(%d,%d)' of table '%s' is not acceesible because it is overlapped by cell '(%d,%d)'", row_id, cell_id, rawget(self, "table name"), parent_1, parent_2)) + end + + + -- check cell boundary + local max_row = row_id + shape[1] - 1 + local max_col = cell_id + shape[2] - 1 + if max_row > #self.rows or max_col > self.ncols then + raise_error(string.format("cell '(%d,%d)' is too large for table '%s'", row_id, col_id, rawget(self, "table name"))) + end + + + local old_shape = cell.shape + + local std_shape = {1, 1} + -- clear control info for children + for i=1, old_shape[1] do + local row_id = row_id + i - 1 + for j = 1, old_shape[2] do + local col_id = cell_id + j - 1 + local other_cell = self:get_cell(row_id, col_id) + other_cell.parent = nil + other_cell.shape = std_shape + end + end + + local parent_t = {row_id, cell_id} + -- adjust control info based on new shape + for i=1, shape[1] do + local row_id = row_id + i - 1 + for j = 1, shape[2] do + local col_id = cell_id + j - 1 + local other_cell = self:get_cell(row_id, col_id) + other_cell.parent = parent_t + other_cell.shape = nil + end + end + + -- adjust control info for this cell + cell.parent = nil + cell.shape = shape + cell.data = data +end + +function TeXTable:fill(data) + local nrows = #self.rows + for i=1,nrows do + for j=1,self.ncols do + local cell = self:get_cell(i, j) + cell.shape = _std_shape + cell.parent = nil + cell.data = data + end + end +end + +function TeXTable:fix_row_index(index_pair) + local ret = {index_pair[1], index_pair[2]} + + if ret[1] < 0 then + ret[1] = #self.rows + ret[1] + 1 + end + if ret[2] < 0 then + ret[2] = #self.rows + ret[2] + 1 + end + + if ret[2] < ret[1] then + raise_error("invalid index expression: second element must not be smaller than the first") + end + + if ret[1] <= 0 then + raise_error("invalid index expression: index must be greater than zero after conversion") + end + + return ret +end + +function TeXTable:fix_col_index(index_pair) + local ret = {index_pair[1], index_pair[2]} + + if ret[1] < 0 then + ret[1] = self.ncols + ret[1] + 1 + end + if ret[2] < 0 then + ret[2] = self.ncols + ret[2] + 1 + end + + if ret[2] < ret[1] then + raise_error("invalid index expression: second element must not be smaller than the first") + end + + if ret[1] <= 0 then + raise_error("invalid index expression: index must be greater than zero after conversion") + end + + return ret +end + +-- fix the index of a single cell +function TeXTable:fix_cell_index(index_expr) + local the_table = self + local index_parse = _lpt_parse_index_expr(index_expr) + if #index_parse ~= 2 then + raise_error(string.format("invalid index expression for cell: '%s'", index_expr)) + end + + local row_expr = the_table:fix_row_index(index_parse[1]) + local col_expr = the_table:fix_col_index(index_parse[2]) + + if row_expr[1] ~= row_expr[2] then + raise_error(string.format("invalid row index expression for cell: '%s'", index_expr)) + end + if col_expr[1] ~= col_expr[2] then + raise_error(string.format("invalid column index expression for cell: '%s'", index_expr)) + end + + return {row_expr[1], col_expr[1]} +end + +-- make sure table views do not cut through multicol/multirow +function TeXTable:check_view_validity(start_row, start_col, row_span, col_span) + local max_row = start_row + row_span - 1 + local max_col = start_col + col_span - 1 + + for i = 1, row_span do + local row_id = start_row + i - 1 + for j=1,col_span do + local col_id = start_col + j - 1 + local cell = self:get_cell(row_id, col_id) + if cell.shape ~= nil then + local boundary_1 = row_id + cell.shape[1] - 1 + local boundary_2 = col_id + cell.shape[2] - 1 + if boundary_1 > max_row or boundary_2 > max_col then + raise_error(string.format("invalid table view: cell '(%d, %d)' has shape '(%d, %d)', which does not fit into viewed region", row_id, col_id, cell.shape[1], cell.shape[2])) + end + end + end + end +end + + +lpt_info = {} + +function _lpt_get_table(table_name) + local table_name = _lpt_strip(table_name) + local the_table = lpt_info[table_name] + if the_table == nil then + raise_error(string.format("table '%s' does not exist", table_name)) + end + return the_table +end + + +_verb_lines = {} +_verb_end_env = "" + +function _lpt_store_verbatim_lines(str) + if string.find(str , _verb_end_env) then + luatexbase.remove_from_callback("process_input_buffer" , "lpt_store_verbatim_lines") + return _verb_end_env + else + if str ~= nil then + table.insert(_verb_lines, str) + end + return "" + end +end + +function _lpt_register_verbatim(end_env) + for k in next, _verb_lines do rawset(_verb_lines, k, nil) end + _verb_end_env = end_env + luatexbase.add_to_callback("process_input_buffer", _lpt_store_verbatim_lines, "lpt_store_verbatim_lines") +end + +local _lpt_ignored_fields = {nrows=true} +local _lpt_numeric_fields = {ncols=true} + + + +function lpt_new_table(param) + local new_table = TeXTable:new() + + for key, val in pairs(param) do + if _lpt_numeric_fields[key] then + rawset(new_table, key, _lpt_tonumber(val)) + elseif _lpt_ignored_fields[key] then + -- do nothing + else + rawset(new_table, key, val) + end + end + + local table_name = new_table["table name"] + + local table_fetch = lpt_info[table_name] + if table_fetch ~= nil then + raise_error(string.format("table '%s' already exists", table_name)) + end + + local nrows = _lpt_tonumber(param["nrows"]) + + -- integrity check + if nrows < 0 then + raise_error(string.format("'nrows' (%d) of table '%s' is invalid", nrows, table_name)) + end + if new_table.ncols <= 0 then + raise_error(string.format("'ncols' (%d) of table '%s' is invalid", new_table.ncols, table_name)) + end + + -- create table + new_table:resize(nrows, new_table.ncols) + + lpt_info[table_name] = new_table +end + +function lpt_table_fill(table_name, index_expr, env_content) + local the_table = _lpt_get_table(table_name) + local env_content = env_content or table.concat(_verb_lines, " ") + + if index_expr:len() == 0 then + the_table:fill(env_content) + else + local index_parse = _lpt_parse_index_expr(index_expr) + if #index_parse ~= 2 then + raise_error(string.format("invalid index expr '%s' for table '%s'", index_expr, table_name)) + end + + local row_expr = the_table:fix_row_index(index_parse[1]) + local col_expr = the_table:fix_col_index(index_parse[2]) + + local row_span = row_expr[2] - row_expr[1] + 1 + local col_span = col_expr[2] - col_expr[1] + 1 + + the_table:check_view_validity(row_expr[1], col_expr[1], row_span, col_span) + + for i=1,row_span do + local row_id = i + row_expr[1] - 1 + for j=1,col_span do + local col_id = j + col_expr[1] - 1 + the_table:set_cell(row_id, col_id, env_content, _std_shape) + end + end + end +end + + +function lpt_table_view(table_name, index_expr) + local env_content = table.concat(_verb_lines, "\n") + local the_table = _lpt_get_table(table_name) + + local index_parse = _lpt_parse_index_expr(index_expr) + if #index_parse ~= 2 then + raise_error(string.format("invalid index expr '%s' for table '%s'", index_expr, table_name)) + end + + local row_expr = the_table:fix_row_index(index_parse[1]) + local col_expr = the_table:fix_col_index(index_parse[2]) + + local row_span = row_expr[2] - row_expr[1] + 1 + local col_span = col_expr[2] - col_expr[1] + 1 + + the_table:check_view_validity(row_expr[1], col_expr[1], row_span, col_span) + + local parsed_table = _lpt_parse_table_view(env_content) + + if #parsed_table ~= row_span then + raise_error(string.format("lpttableview: expecting '%d' rows, but get '%d' instead for table '%s'", row_span, #parsed_table, table_name)) + end + + for i=1,row_span do + local row_id = i + row_expr[1] - 1 + local cols = parsed_table[i] + + -- sum the number of total columns in this row + local n_table_cols = 0 + local max_row_size = 0 + for _, val in ipairs(cols) do + n_table_cols = n_table_cols + val["shape"][2] + max_row_size = math.max(max_row_size, val["shape"][1]) + end + + local max_row = row_id + max_row_size - 1 + if max_row > row_expr[2] then + raise_error("lpttableview: the height of table is greater than table view") + end + + + -- count the numbers of columns needs to be filled in this row + local pending_table_cols = 0 + for j=1,col_span do + local col_id = j + col_expr[1] - 1 + local cell = the_table:get_cell(row_id, col_id) + if cell.shape ~= nil then + pending_table_cols = pending_table_cols + 1 + else + local parent_row = cell.parent[1] + -- if the parent is in the same row, + -- then this space needs to be filled in the same row + if parent_row == row_id then + pending_table_cols = pending_table_cols + 1 + end + end + end + + if n_table_cols ~= pending_table_cols then + raise_error(string.format("lpttableview: expecting '%d' cols for row '%d', but get '%d' instead for table '%s'", pending_table_cols, i, n_table_cols, table_name)) + end + + + --[[ + if n_table_cols ~= col_span then + raise_error(string.format("lpttableview: expecting '%d' cols for row '%d', but get '%d' instead for table '%s'", col_span, i, n_table_cols, table_name)) + end + --]] + + local k = 1 + for j=1,col_span do + local col_id = j + col_expr[1] - 1 + local cell = the_table:get_cell(row_id, col_id) + if cell.shape ~= nil then + local col_data = cols[k]["data"] + local col_shape = cols[k]["shape"] + the_table:set_cell(row_id, col_id, col_data, col_shape) + k = k + 1 + end + end + + end + + +end + +local _s_buf = TeXStringBuffer:new() + +function lpt_use_table(table_name) + local the_table = _lpt_get_table(table_name) + local input_method = rawget(the_table, "input method") + + if input_method == "stringbuffer" then + local table_str = the_table:to_string(true) + _s_buf:clear() + _s_buf:append(table_str) + _s_buf:register_callback() + tex.print([[\input{stringbuffer}]]) + elseif input_method == "file" then + local table_str = the_table:to_string() + local filename = tex.jobname .. "-" .. table_name .. ".table" + local outfile = io.open(filename, "w") + outfile:write(table_str) + outfile:close() + tex.print(string.format([[\input{%s}]], filename)) + else + raise_error(string.format("invalid input method '%s' for table '%s'", input_method, table_name)) + end + +end + +function lpt_set_row_prop(table_name, index_expr, param) + + local the_table = _lpt_get_table(table_name) + local index_parse = _lpt_parse_index_expr(index_expr) + for key, val in ipairs(index_parse) do + local fixed_val = the_table:fix_row_index(val) + + local l = fixed_val[1] + local r = fixed_val[2] + + for i=l,r do + local row = the_table.rows[i] + if row == nil then + raise_error(string.format("lpt_set_row_prop: invalid index expression '%s'", index_expr)) + end + for kkey, vval in pairs(param) do + if rawget(row, kkey) == nil then + raise_error(string.format("row '%d' of table '%s' does not have attribute '%s'", i, table_name, kkey)) + end + rawset(row, kkey, vval) + end + end + end +end + + +function lpt_add_row(table_name, param) + local the_table = _lpt_get_table(table_name) + local nrows = #the_table.rows + local ncols = the_table.ncols + the_table:resize(nrows+1, ncols) + lpt_set_row_prop(table_name, "-1", param) +end + +function lpt_get_table_shape(table_name) + local the_table = _lpt_get_table(table_name) + local nrows = #the_table.rows + local ncols = the_table.ncols + tex.print(string.format("{%d}{%d}", nrows, ncols)) +end
\ No newline at end of file diff --git a/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.sty b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.sty new file mode 100644 index 00000000000..d05a633c218 --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/luaprogtable/luaprogtable.sty @@ -0,0 +1,237 @@ +\RequirePackage{expl3} +\RequirePackage{xparse} +\RequirePackage{luatexbase} +\RequirePackage{iftex} + +\RequireLuaTeX + +\ProvidesExplPackage {luaprogtable}{08/06/2020}{1.0}{Programmatic table interface} + +\makeatletter +\savecatcodetable 20996 + +\directlua{ + require~"luaprogtable.lua" +} + +\tl_new:N \g__luaprogtable_tmpa_tl +\tl_new:N \g__luaprogtable_tmpb_tl +\tl_new:N \g__luaprogtable_tmpc_tl + +\str_new:N \g__luaprogtable_tmpa_str +\str_new:N \g__luaprogtable_tmpb_str +\str_new:N \g__luaprogtable_tmpc_str + +\int_new:N \g__luaprogtable_tmpa_int +\int_new:N \g__luaprogtable_tmpb_int +\int_new:N \g__luaprogtable_tmpc_int + +\cs_set:Npn \__luaprogtable_lua_table_item:nn #1#2 { + ["#1"]="#2" +} + +\seq_new:N \g__luaprogtable_prop_item_seq + +\cs_set:Npn \__luaprogtable_add_lua_table_item:nn #1#2 { + \str_gset:Nx \g__luaprogtable_tmpa_str {\luaescapestring{\tl_to_str:n {#1}}} + \str_gset:Nx \g__luaprogtable_tmpb_str {\luaescapestring{\tl_to_str:n {#2}}} + \seq_gput_right:Nx \g__luaprogtable_prop_item_seq { + \exp_args:NVV \__luaprogtable_lua_table_item:nn \g__luaprogtable_tmpa_str \g__luaprogtable_tmpb_str + } +} + +\str_new:N \g__luaprogtable_lua_table_str + +\cs_set:Npn \__luaprogtable_prop_to_lua_table:N #1 { + \seq_gclear:N \g__luaprogtable_prop_item_seq + \prop_map_function:NN #1 \__luaprogtable_add_lua_table_item:nn + \str_gclear:N \g__luaprogtable_lua_table_str + \str_gput_right:NV \g__luaprogtable_lua_table_str \c_left_brace_str + \str_gput_right:Nx \g__luaprogtable_lua_table_str {\seq_use:Nn \g__luaprogtable_prop_item_seq {,~}} + \str_gput_right:NV \g__luaprogtable_lua_table_str \c_right_brace_str +} + +\prop_new:N \g__luaprogtable_new_prop +\keys_define:nn { __luaprogtable_new } +{ + backend .prop_gput:N = \g__luaprogtable_new_prop, + ncols .prop_gput:N = \g__luaprogtable_new_prop, + nrows .prop_gput:N = \g__luaprogtable_new_prop, + default~before~line .prop_gput:N = \g__luaprogtable_new_prop, + default~after~line .prop_gput:N = \g__luaprogtable_new_prop, + default~after~spacing .prop_gput:N = \g__luaprogtable_new_prop, + input~method .prop_gput:N = \g__luaprogtable_new_prop +} + +\str_new:N \g__luaprogtable_lua_cur_table_str +\DeclareDocumentCommand{\LPTSetCurrentTable}{m}{ + \directlua{ + local _test_table = _lpt_get_table("\luaescapestring{#1}") + } + \str_gset:Nx \g__luaprogtable_lua_cur_table_str {\luaescapestring{#1}} +} + +\newcommand{\LPTGetCurrentTable}{ + \str_use:N \g__luaprogtable_lua_cur_table_str +} + +\DeclareDocumentCommand{\LPTNewTable}{mmmO{}}{ + \prop_gclear:N \g__luaprogtable_new_prop + \prop_gput:Nnn \g__luaprogtable_new_prop {backend} {tabular} + \prop_gput:Nnn \g__luaprogtable_new_prop {nrows} {0} + \prop_gput:Nnn \g__luaprogtable_new_prop {default~before~line} {} + \prop_gput:Nnn \g__luaprogtable_new_prop {default~after~line} {} + \prop_gput:Nnn \g__luaprogtable_new_prop {default~after~spacing} {} + \prop_gput:Nnn \g__luaprogtable_new_prop {input~method} {stringbuffer} + + \keys_set:nn { __luaprogtable_new } {#4} + + \prop_gput:Nnn \g__luaprogtable_new_prop {table~name} {#1} + \prop_gput:Nnn \g__luaprogtable_new_prop {ncols} {#2} + \prop_gput:Nnn \g__luaprogtable_new_prop {table~preamble} {#3} + \prop_gput:Nnn \g__luaprogtable_new_prop {default~value} {} + + \__luaprogtable_prop_to_lua_table:N \g__luaprogtable_new_prop + + \directlua{ + local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; + lpt_new_table(_tmp_table); + } +} + +\newcommand{\LPTUseTable}{ + \directlua{ + lpt_use_table("\LPTGetCurrentTable"); + } +} + +\DeclareDocumentEnvironment{lptview}{m}{ + \directlua{ + _lpt_register_verbatim([[\string\end{lptview}]]) + } +}{ + % now that verbatim material is passed to lua, call corresponding handler + \directlua{ + lpt_table_view("\LPTGetCurrentTable", "\luaescapestring{#1}") + } +} + +\DeclareDocumentEnvironment{lptfill}{m}{ + \directlua{ + _lpt_register_verbatim([[\string\end{lptfill}]]) + } +}{ + \directlua{ + lpt_table_fill("\LPTGetCurrentTable", "\luaescapestring{#1}") + } +} + +\DeclareDocumentCommand{\LPTFill}{mm}{ + \directlua{ + lpt_table_fill("\LPTGetCurrentTable", "\luaescapestring{#1}", "\luaescapestring{\tl_to_str:n {#2}}") + } +} + +\prop_new:N \g__luaprogtable_row_prop +\keys_define:nn { __luaprogtable_row } +{ + before~line .prop_gput:N = \g__luaprogtable_row_prop, + after~line .prop_gput:N = \g__luaprogtable_row_prop, + after~spacing .prop_gput:N = \g__luaprogtable_row_prop +} + +\DeclareDocumentCommand{\LPTSetRowProp}{mm}{ + \prop_gclear:N \g__luaprogtable_row_prop + \keys_set:nn { __luaprogtable_row } {#2} + + \__luaprogtable_prop_to_lua_table:N \g__luaprogtable_row_prop + \directlua{ + local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; + lpt_set_row_prop("\LPTGetCurrentTable", "\luaescapestring{#1}", _tmp_table) + } +} + +\DeclareDocumentCommand{\LPTAddRow}{O{}}{ + \prop_gclear:N \g__luaprogtable_row_prop + \keys_set:nn { __luaprogtable_row } {#1} + + \__luaprogtable_prop_to_lua_table:N \g__luaprogtable_row_prop + \directlua{ + local~_tmp_table = \str_use:N \g__luaprogtable_lua_table_str; + lpt_add_row("\LPTGetCurrentTable", _tmp_table) + } +} + +\DeclareDocumentCommand{\LPTSetCell}{mO{1,1}m}{ + \directlua{ + local~_table = _lpt_get_table("\LPTGetCurrentTable"); + local~_index = _table:fix_cell_index("\luaescapestring{#1}"); + local~_shape = _lpt_parse_shape("\luaescapestring{#2}"); + _table:set_cell(_index[1], _index[2], "\luaescapestring{ \tl_to_str:n {#3} }", _shape) + } +} + +\newcommand{\LPTGetTableShape}{ + \directlua{ + lpt_get_table_shape("\LPTGetCurrentTable") + } +} + +\ExplSyntaxOff + +\newcommand{\LPTGetTableNames}{% + \directlua{ + local _names = {} + for key, val in pairs(lpt_info) do + table.insert(_names, key) + end + table.sort(_names) + local _ret = table.concat(_names, ",") + tex.print(_ret) + }% +} + +\newcommand{\LPTGetCellData}[1]{% + \directlua{ + local _table = _lpt_get_table("\LPTGetCurrentTable"); + local _index = _table:fix_cell_index("\luaescapestring{#1}"); + local _cell = _table:get_cell(_index[1], _index[2]); + tex.print([[\string\unexpanded{]] .. _cell.data .. "}") + }% +} + +\newcommand{\LPTGetCellMetaIndex}[2]{% + \directlua{ + local _table = _lpt_get_table("\LPTGetCurrentTable"); + local _index = _table:fix_cell_index("\luaescapestring{#2}"); + local _cell = _table:get_cell(_index[1], _index[2]); + local _field = "\luaescapestring{#1}" + local _ret = rawget(_cell, _field) + if _ret == nil then + tex.print(20996, [[\string\c_novalue_tl]]) + else + tex.print("{".. tostring(_ret[1]) .. "}{".. tostring(_ret[2]) .. "}") + end + }% +} + +\newcommand{\LPTGetCellShape}[1]{% + \LPTGetCellMetaIndex{shape}{#1}% +} + +\newcommand{\LPTGetCellParent}[1]{% + \LPTGetCellMetaIndex{parent}{#1}% +} + +\newcommand{\LPTDeleteTable}[1]{ + \directlua{ + local _name = "\luaescapestring{#1}" + local _table = _lpt_get_table(_name) + lpt_info[_name] = nil + } +} + + +\makeatother + +\endinput
\ No newline at end of file |