Skip to content

Commit

Permalink
fix: keep braces when extracting table commands (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
muzimuzhi committed Nov 23, 2024
1 parent 1c8b861 commit bc1556d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tabularray/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- `text` as hline/vline was fully expanded (lvjr/tabularray#303)
- No extra braces are stripped when splitting table into cells.
- No braces are stripped from cells (lvjr/tabularray#501, #4)
Now to protect non-number `S`-column cells, only one pair of braces
is needed, rather than three pairs as before. (lvjr/tabularray#501)
is needed, rather than three pairs as before.

### Security

Expand Down
48 changes: 38 additions & 10 deletions tabularray/tabularray.sty
Original file line number Diff line number Diff line change
Expand Up @@ -2823,7 +2823,9 @@
{
\tl_clear:N \l__tblr_saved_table_commands_before_cell_text_tl
\tl_clear:N \l__tblr_saved_cell_text_after_table_commands_tl
\exp_last_unbraced:NV \__tblr_extract_table_commands_next:n #1 \q_stop
% \beginunravel
\exp_last_unbraced:No \__tblr_extract_table_commands_next: #1 \q_stop
% \endunravel
\tl_if_empty:NF \l__tblr_saved_table_commands_before_cell_text_tl
{
\__tblr_prop_gput:neV { command }
Expand All @@ -2834,19 +2836,45 @@
}
%% #1 maybe a single token or multiple tokens from a pair of braces
\cs_new_protected:Npn \__tblr_extract_table_commands_next:n #1
\cs_new_protected:Npn \__tblr_extract_table_commands_next:
{
\tl_if_single_token:nTF {#1}
\peek_after:Nw \__tblr_extract_table_commands_aux:
}
\cs_new_protected:Npn \__tblr_extract_table_commands_aux:
{
% if \q_stop, stop
% if space, skip
% if macro: if table command, extract; otherwise stop and collect
% otherwise, stop and collect the remaining as real cell text
\token_case_meaning:NnF \l_peek_token
{
\__tblr_if_table_command:NTF #1
{ \__tblr_extract_one_table_command:N #1 }
\q_stop
{ \__tblr_save_real_cell_text_aux:w \prg_do_nothing: }
\c_space_token
% similar to \peek_remove_spaces:n, especially \__peek_true_remove:w
% but quicker
{
\token_if_eq_meaning:NNF #1 \q_stop
{ \__tblr_save_real_cell_text:w #1 }
\tex_afterassignment:D \__tblr_extract_table_commands_next:
\cs_set_eq:NN \__tblr_unused:w
}
}
% prefix #1 with \prg_do_nothing: to prevent brace stripping
{ \__tblr_save_real_cell_text_aux:w \prg_do_nothing: {#1} }
{
\token_if_macro:NTF \l_peek_token
{ \__tblr_extract_table_commands_auxi:N }
{ \__tblr_save_real_cell_text_aux:w \prg_do_nothing: }
}
}
\cs_new_protected:Npn \__tblr_extract_table_commands_auxi:N #1
{
\__tblr_if_table_command:NTF #1
{
\__tblr_extract_one_table_command:N #1
}
{
\__tblr_save_real_cell_text_aux:w \prg_do_nothing: #1
}
}
\cs_new_protected:Npn \__tblr_extract_one_table_command:N #1
Expand Down Expand Up @@ -2883,7 +2911,7 @@
\int_decr:N \l__tblr_a_int
\__tblr_extract_table_command_arg_m:n
}
{ \__tblr_extract_table_commands_next:n }
{ \__tblr_extract_table_commands_next: }
}
%% The outermost set of braces of cell text #1 will be removed
Expand Down
43 changes: 43 additions & 0 deletions tabularray/testfiles/table-split.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
\input{regression-test}
\documentclass{article}

\ExplSyntaxOn
\debug_on:n { check-declarations , check-expressions , deprecation }
\ExplSyntaxOff

\usepackage{tabularray}

\begin{document}

\SetTblrTracing{+text}

\START

% https://github.com/lvjr/tabularray/issues/501
\BEGINTEST{keep braces when splitting table into cells}
\begin{tblr}{l}
a \\
{b} \\
{{c}} \\
{{{d}}} \\
{{{{e}}}}
\end{tblr}

\begin{tblr}{l}
1 & a \\
2 & {b} \\
3 & {{c}} \\
4 & {{{d}}}
\end{tblr}
\ENDTEST

% https://github.com/muzimuzhi/tabularray-dev/issues/4
\BEGINTEST{keep braces when extracting table commands}
\begin{tblr}{}
a & {a} & {a}a \\
\end{tblr}
\ENDTEST

\end{document}

\END
41 changes: 41 additions & 0 deletions tabularray/testfiles/table-split.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: keep braces when splitting table into cells
============================================================
> The spec list text_1 contains the pairs:.
> {[1][1]} => {a}.
> {[2][1]} => {{b}}.
> {[3][1]} => {{{c}}}.
> {[4][1]} => {{{{d}}}}.
> {[5][1]} => {{{{{e}}}}}.
> The spec list text_1 contains the pairs:.
> {[1][1]} => {1}.
> {[2][1]} => {2}.
> {[3][1]} => {3}.
> {[4][1]} => {4}.
> {[5][1]} => {}.
> {[1][2]} => {a}.
> {[2][2]} => {{b}}.
> {[3][2]} => {{{c}}}.
> {[4][2]} => {{{{d}}}}.
============================================================
============================================================
TEST 2: keep braces when extracting table commands
============================================================
> The spec list text_1 contains the pairs:.
> {[1][1]} => {a}.
> {[2][1]} => {}.
> {[3][1]} => {}.
> {[4][1]} => {}.
> {[5][1]} => {}.
> {[1][2]} => {{a}}.
> {[2][2]} => {}.
> {[3][2]} => {}.
> {[4][2]} => {}.
> {[1][3]} => {{a}a}.
============================================================
[1
] (table-split.aux)
***************
[test] 0 error(s) encountered.

0 comments on commit bc1556d

Please sign in to comment.