lib/LaTeXML/Core/Alignment/Template.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 203 CC 16 SCAR 29.3
CC
16
Cov
0%
new
42.0
addColumn
42.0
column
20.0
CRAP 18.7
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::Token;
19 use LaTeXML::Core::Tokens;
20  
21 sub new {
22 my ($class, %data) = @_;
23 $data{columns} = [] unless $data{columns};
24 $data{repeating} = 1 if $data{repeating} || $data{repeated};
25 $data{repeated} = [] unless $data{repeated};
26 $data{non_repeating} = scalar(@{ $data{columns} });
27 $data{save_before} = [] unless $data{save_before};
28 $data{save_between} = [] unless $data{save_between}; # between comes before before!
29  
30 map { $$_{empty} = 1 } @{ $data{columns} };
31 map { $$_{empty} = 1 } @{ $data{repeated} };
32 return bless {%data}, $class; }
33  
34 sub revert {
35 my ($self) = @_;
36 return @{ $$self{tokens} }; }
37  
38 # Methods for constructing a template.
39  
40 sub setReversion {
41 my ($self, @tokens) = @_;
42 $$self{tokens} = [@tokens];
43 return; }
44  
45 sub setRepeating {
46 my ($self) = @_;
47 $$self{repeating} = 1;
48 return; }
49  
50 # These add material before & after the current column
51 sub addBeforeColumn {
52 my ($self, @tokens) = @_;
53 unshift(@{ $$self{save_before} }, @tokens); # NOTE: goes all the way to front!
54 return; }
55  
56 # NOTE: \@@eat@space should ONLY be added to LaTeX tabular style templates!!!!
57 # NOT \halign style templates!
58 sub addAfterColumn {
59 my ($self, @tokens) = @_;
60 $$self{current_column}{after} = Tokens(T_CS('\@@eat@space'),
61 @tokens, @{ $$self{current_column}{after} });
62 return; }
63  
64 # Or between this column & next...
65 sub addBetweenColumn {
66 my ($self, @tokens) = @_;
67 my @cols = @{ $$self{columns} };
68 if ($$self{current_column}) {
69 $$self{current_column}{after} = Tokens(@{ $$self{current_column}{after} }, @tokens); }
70 else {
71 push(@{ $$self{save_between} }, @tokens); }
72 return; }
73  
74 sub addColumn {
75 my ($self, %properties) = @_;
76 my $col = {%properties};
77 my @before = ();
78 push(@before, @{ $$self{save_between} }) if $$self{save_between};
79 push(@before, $properties{before}->unlist) if $properties{before};
80 push(@before, @{ $$self{save_before} }) if $$self{save_before};
81 $$col{before} = Tokens(@before);
82 my @after = ();
83 push(@after, T_CS('\@@eat@space'));
84 push(@after, $properties{after}->unlist) if $properties{after};
85 $$col{after} = Tokens(@after);
86 ### $$col{after} = Tokens() unless $properties{after};
87 $$col{thead} = $properties{thead};
88 $$col{empty} = 1;
89 $$self{save_between} = [];
90 $$self{save_before} = [];
91 $$self{current_column} = $col;
92  
93 if ($$self{repeating}) {
94 $$self{non_repeating} = scalar(@{ $$self{columns} });
95 push(@{ $$self{repeated} }, $col); }
96 else {
97 push(@{ $$self{columns} }, $col); }
98 return; }
99  
100 # Methods for using a template.
101 sub clone {
102 my ($self) = @_;
103 my @dup = ();
104 foreach my $cell (@{ $$self{columns} }) {
105 push(@dup, {%$cell}); }
106 return bless { columns => [@dup],
107 repeated => $$self{repeated}, non_repeating => $$self{non_repeating},
108 repeating => $$self{repeating} }, ref $self; }
109  
110 sub show {
111 my ($self) = @_;
112 my @strings = ();
113 push(@strings, "\nColumns:\n");
114 foreach my $col (@{ $$self{columns} }) {
115 push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); }
116 if ($$self{repeating}) {
117 push(@strings, "\nRepeated Columns:\n");
118 foreach my $col (@{ $$self{repeated} }) {
119 push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); } }
120 return join(', ', @strings); }
121  
122 sub column {
123 my ($self, $n) = @_;
124 my $N = scalar(@{ $$self{columns} });
125 if (($n > $N) && $$self{repeating}) {
126 my @rep = @{ $$self{repeated} };
127 if (my $m = scalar(@rep)) {
128 for (my $i = $N ; $i < $n ; $i++) {
129 my %dup = %{ $rep[($i - $$self{non_repeating}) % $m] };
130 push(@{ $$self{columns} }, {%dup}); } } }
131 return ($n > 0 ? $$self{columns}->[$n - 1] : undef); }
132  
133 sub columns {
134 my ($self) = @_;
135 return @{ $$self{columns} }; }
136  
137 #======================================================================
138 1;