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

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 255 CC 23 SCAR 33.8
CC
23
Cov
0%
addColumn
90.0
new
42.0
column
20.0
CRAP 29.4
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: \lx@column@trimright 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('\lx@column@trimright'),
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 (my $col = $$self{current_column}) {
69 if (!$$self{disabled_intercolumn}) {
70 unshift(@tokens, T_CS('\lx@intercol')); }
71 $$self{current_column}{after} = Tokens(@{ $$self{current_column}{after} }, @tokens); }
72 else {
73 push(@{ $$self{save_between} }, @tokens); }
74 return; }
75  
76 sub disableIntercolumn {
77 my ($self) = @_;
78 if (my $col = $$self{current_column}) {
79 $$self{disabled_intercolumn} = 1; }
80 return; }
81  
82 sub addColumn {
83 my ($self, %properties) = @_;
84 my $col = {%properties};
85 if (my $prev = $$self{current_column}) {
86 $$prev{after} = Tokens(($$prev{after} || ()), T_CS('\lx@intercol'))
87 unless $$self{disabled_intercolumn}; }
88 my @before = ();
89 push(@before, @{ $$self{save_between} }) if $$self{save_between};
90 push(@before, T_CS('\lx@intercol')) unless $$self{disabled_intercolumn};
91 delete $$self{disabled_intercolumn};
92  
93 push(@before, $properties{before}->unlist) if $properties{before};
94 push(@before, @{ $$self{save_before} }) if $$self{save_before};
95 $$col{before} = Tokens(@before);
96 my @after = ();
97 push(@after, T_CS('\lx@column@trimright'));
98 push(@after, $properties{after}->unlist) if $properties{after};
99 $$col{after} = Tokens(@after);
100 $$col{thead} = $properties{thead};
101 $$col{empty} = 1;
102 $$self{save_between} = [];
103 $$self{save_before} = [];
104 $$self{current_column} = $col;
105  
106 if ($$self{repeating}) {
107 $$self{non_repeating} = scalar(@{ $$self{columns} });
108 push(@{ $$self{repeated} }, $col); }
109 else {
110 push(@{ $$self{columns} }, $col); }
111 return; }
112  
113 sub finish {
114 my ($self) = @_;
115 if (my $prev = $$self{current_column}) {
116 $$prev{after} = Tokens(($$prev{after} || ()), T_CS('\lx@intercol'))
117 unless $$self{disabled_intercolumn}; }
118 return; }
119  
120 # Methods for using a template.
121 sub clone {
122 my ($self) = @_;
123 my @dup = ();
124 foreach my $cell (@{ $$self{columns} }) {
125 push(@dup, {%$cell}); }
126 return bless { columns => [@dup],
127 repeated => $$self{repeated}, non_repeating => $$self{non_repeating},
128 repeating => $$self{repeating} }, ref $self; }
129  
130 sub show {
131 my ($self) = @_;
132 my @strings = ();
133 push(@strings, "\nColumns:\n");
134 foreach my $col (@{ $$self{columns} }) {
135 push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); }
136 if ($$self{repeating}) {
137 push(@strings, "\nRepeated Columns:\n");
138 foreach my $col (@{ $$self{repeated} }) {
139 push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); } }
140 return join(', ', @strings); }
141  
142 sub column {
143 my ($self, $n) = @_;
144 my $N = scalar(@{ $$self{columns} });
145 if (($n > $N) && $$self{repeating}) {
146 my @rep = @{ $$self{repeated} };
147 if (my $m = scalar(@rep)) {
148 for (my $i = $N ; $i < $n ; $i++) {
149 my %dup = %{ $rep[($i - $$self{non_repeating}) % $m] };
150 push(@{ $$self{columns} }, {%dup}); } } }
151 return ($n > 0 ? $$self{columns}->[$n - 1] : undef); }
152  
153 sub columns {
154 my ($self) = @_;
155 return @{ $$self{columns} }; }
156  
157 #======================================================================
158 1;