From 14aa0cb82e9ba66e0da94b881ce0c5e4181543b5 Mon Sep 17 00:00:00 2001 From: "K. Andrew Parker" Date: Sat, 9 Mar 2024 17:25:21 -0500 Subject: [PATCH] initial PGML version of niceTables --- macros/ui/PGMLTables.pl | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 macros/ui/PGMLTables.pl diff --git a/macros/ui/PGMLTables.pl b/macros/ui/PGMLTables.pl new file mode 100644 index 0000000000..3e8664ee71 --- /dev/null +++ b/macros/ui/PGMLTables.pl @@ -0,0 +1,59 @@ +sub _PGMLTables_init { + main::PG_restricted_eval('sub PGMLTable { PGMLTables::PGMLTable(@_) }'); +} + +loadMacros('PGML.pl'); + +package PGMLTables; + +sub wrapTeX { return '[`' . shift . '`]' } + +sub arrayString { + my $array = shift; + for (@$array) {s/'/\\'/} + return "['" . join("','", @$array) . "']"; +} + +sub PGMLopts { + my $opts = shift; + for (keys %$opts) { + if (ref($opts->{$_}) eq 'ARRAY') { + $opts->{$_} = arrayString($opts->{$_}); + } else { + $opts->{$_} =~ s/'/\\'/; + $opts->{$_} = "'$opts->{$_}'"; + } + } + return '{' . join(', ', map {"$_ => $opts->{$_}"} keys %$opts) . '}'; +} + +sub wrapCell { + my $cell = shift; + my $opts; + if (ref $cell eq 'ARRAY') { + my $data = shift @$cell; + $opts = {@$cell}; + $cell = $data; + } + my $string = "[.$cell.]"; + $string .= PGMLopts($opts) if $opts; + return $string; +} + +sub convertRow { + my $row = shift; + return join('', map { wrapCell($_) } @$row) . '* '; +} + +sub PGMLTable { + my $rows = shift; + my %opts = @_; + my $table = '[#' . join(' ', map { convertRow($_) } @$rows) . '#]'; + if ($opts{layout}) { + $table .= '*'; + delete $opts{layout}; + } + $table .= PGMLopts(\%opts) if %opts; + return $table; +} +