| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Alignment | # | ||
| 3 | # | Support for tabular/array environments | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Core::Alignment::Template; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use base qw(LaTeXML::Common::Object); | ||
| 16 | use LaTeXML::Global; | ||
| 17 | use LaTeXML::Common::Object; | ||
| 18 | use LaTeXML::Core::Tokens; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ($class, %data) = @_; | ||
| 22 | $data{columns} = [] unless $data{columns}; | ||
| 23 | $data{repeating} = 1 if $data{repeating} || $data{repeated}; | ||
| 24 | $data{repeated} = [] unless $data{repeated}; | ||
| 25 | $data{non_repeating} = scalar(@{ $data{columns} }); | ||
| 26 | $data{save_before} = [] unless $data{save_before}; | ||
| 27 | $data{save_between} = [] unless $data{save_between}; # between comes before before! | ||
| 28 | |||
| 29 | map { $$_{empty} = 1 } @{ $data{columns} }; | ||
| 30 | map { $$_{empty} = 1 } @{ $data{repeated} }; | ||
| 31 | return bless {%data}, $class; } | ||
| 32 | |||
| 33 | sub revert { | ||
| 34 | my ($self) = @_; | ||
| 35 | return @{ $$self{tokens} }; } | ||
| 36 | |||
| 37 | # Methods for constructing a template. | ||
| 38 | |||
| 39 | sub setReversion { | ||
| 40 | my ($self, @tokens) = @_; | ||
| 41 | $$self{tokens} = [@tokens]; | ||
| 42 | return; } | ||
| 43 | |||
| 44 | sub setRepeating { | ||
| 45 | my ($self) = @_; | ||
| 46 | $$self{repeating} = 1; | ||
| 47 | return; } | ||
| 48 | |||
| 49 | # These add material before & after the current column | ||
| 50 | sub addBeforeColumn { | ||
| 51 | my ($self, @tokens) = @_; | ||
| 52 | unshift(@{ $$self{save_before} }, @tokens); # NOTE: goes all the way to front! | ||
| 53 | return; } | ||
| 54 | |||
| 55 | sub addAfterColumn { | ||
| 56 | my ($self, @tokens) = @_; | ||
| 57 | $$self{current_column}{after} = Tokens(@tokens, @{ $$self{current_column}{after} }); | ||
| 58 | return; } | ||
| 59 | |||
| 60 | # Or between this column & next... | ||
| 61 | sub addBetweenColumn { | ||
| 62 | my ($self, @tokens) = @_; | ||
| 63 | my @cols = @{ $$self{columns} }; | ||
| 64 | if ($$self{current_column}) { | ||
| 65 | $$self{current_column}{after} = Tokens(@{ $$self{current_column}{after} }, @tokens); } | ||
| 66 | else { | ||
| 67 | push(@{ $$self{save_between} }, @tokens); } | ||
| 68 | return; } | ||
| 69 | |||
| 70 | sub addColumn { | ||
| 71 | my ($self, %properties) = @_; | ||
| 72 | my $col = {%properties}; | ||
| 73 | my @before = (); | ||
| 74 | push(@before, @{ $$self{save_between} }) if $$self{save_between}; | ||
| 75 | push(@before, $properties{before}->unlist) if $properties{before}; | ||
| 76 | push(@before, @{ $$self{save_before} }) if $$self{save_before}; | ||
| 77 | $$col{before} = Tokens(@before); | ||
| 78 | $$col{after} = Tokens() unless $properties{after}; | ||
| 79 | $$col{thead} = $properties{thead}; | ||
| 80 | $$col{empty} = 1; | ||
| 81 | $$self{save_between} = []; | ||
| 82 | $$self{save_before} = []; | ||
| 83 | $$self{current_column} = $col; | ||
| 84 | if ($$self{repeating}) { | ||
| 85 | $$self{non_repeating} = scalar(@{ $$self{columns} }); | ||
| 86 | push(@{ $$self{repeated} }, $col); } | ||
| 87 | else { | ||
| 88 | push(@{ $$self{columns} }, $col); } | ||
| 89 | return; } | ||
| 90 | |||
| 91 | # Methods for using a template. | ||
| 92 | sub clone { | ||
| 93 | my ($self) = @_; | ||
| 94 | my @dup = (); | ||
| 95 | foreach my $cell (@{ $$self{columns} }) { | ||
| 96 | push(@dup, {%$cell}); } | ||
| 97 | return bless { columns => [@dup], | ||
| 98 | repeated => $$self{repeated}, non_repeating => $$self{non_repeating}, | ||
| 99 | repeating => $$self{repeating} }, ref $self; } | ||
| 100 | |||
| 101 | sub show { | ||
| 102 | my ($self) = @_; | ||
| 103 | my @strings = (); | ||
| 104 | push(@strings, "\nColumns:\n"); | ||
| 105 | foreach my $col (@{ $$self{columns} }) { | ||
| 106 | push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } keys %$col) . '}'); } | ||
| 107 | if ($$self{repeating}) { | ||
| 108 | push(@strings, "\nRepeated Columns:\n"); | ||
| 109 | foreach my $col (@{ $$self{repeated} }) { | ||
| 110 | push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } keys %$col) . '}'); } } | ||
| 111 | return join(', ', @strings); } | ||
| 112 | |||
| 113 | sub column { | ||
| 114 | my ($self, $n) = @_; | ||
| 115 | my $N = scalar(@{ $$self{columns} }); | ||
| 116 | if (($n > $N) && $$self{repeating}) { | ||
| 117 | my @rep = @{ $$self{repeated} }; | ||
| 118 | if (my $m = scalar(@rep)) { | ||
| 119 | for (my $i = $N ; $i < $n ; $i++) { | ||
| 120 | my %dup = %{ $rep[($i - $$self{non_repeating}) % $m] }; | ||
| 121 | push(@{ $$self{columns} }, {%dup}); } } } | ||
| 122 | return $$self{columns}->[$n - 1]; } | ||
| 123 | |||
| 124 | sub columns { | ||
| 125 | my ($self) = @_; | ||
| 126 | return @{ $$self{columns} }; } | ||
| 127 | |||
| 128 | #====================================================================== | ||
| 129 | 1; |