Skip to content

Commit

Permalink
initial PGML version of niceTables
Browse files Browse the repository at this point in the history
  • Loading branch information
drdrew42 committed Mar 11, 2024
1 parent a9d9280 commit 4ed0c7c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions macros/ui/PGMLTables.pl
Original file line number Diff line number Diff line change
@@ -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/'/\\'/g}
return "['" . join("','", @$array) . "']";
}

sub PGMLopts {
my $opts = shift;
for (keys %$opts) {
if (ref($opts->{$_}) eq 'ARRAY') {
$opts->{$_} = arrayString($opts->{$_});
} else {
$opts->{$_} =~ s/'/\\'/g;
$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;
}

0 comments on commit 4ed0c7c

Please sign in to comment.