File: Template.pm

package info (click to toggle)
latexml 0.8.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,920 kB
  • sloc: xml: 109,048; perl: 30,224; sh: 179; javascript: 28; makefile: 13
file content (158 lines) | stat: -rw-r--r-- 5,602 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# /=====================================================================\ #
# |  LaTeXML::Core::Alignment                                           | #
# | Support for tabular/array environments                              | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Core::Alignment::Template;
use strict;
use warnings;
use base qw(LaTeXML::Common::Object);
use LaTeXML::Global;
use LaTeXML::Common::Object;
use LaTeXML::Core::Token;
use LaTeXML::Core::Tokens;

sub new {
  my ($class, %data) = @_;
  $data{columns}       = [] unless $data{columns};
  $data{repeating}     = 1 if $data{repeating} || $data{repeated};
  $data{repeated}      = [] unless $data{repeated};
  $data{non_repeating} = scalar(@{ $data{columns} });
  $data{save_before}   = [] unless $data{save_before};
  $data{save_between}  = [] unless $data{save_between};    # between comes before before!

  map { $$_{empty} = 1 } @{ $data{columns} };
  map { $$_{empty} = 1 } @{ $data{repeated} };
  return bless {%data}, $class; }

sub revert {
  my ($self) = @_;
  return @{ $$self{tokens} }; }

# Methods for constructing a template.

sub setReversion {
  my ($self, @tokens) = @_;
  $$self{tokens} = [@tokens];
  return; }

sub setRepeating {
  my ($self) = @_;
  $$self{repeating} = 1;
  return; }

# These add material before & after the current column
sub addBeforeColumn {
  my ($self, @tokens) = @_;
  unshift(@{ $$self{save_before} }, @tokens);    # NOTE: goes all the way to front!
  return; }

# NOTE: \lx@column@trimright should ONLY be added to LaTeX tabular style templates!!!!
# NOT \halign style templates!
sub addAfterColumn {
  my ($self, @tokens) = @_;
  $$self{current_column}{after} = Tokens(T_CS('\lx@column@trimright'),
    @tokens, @{ $$self{current_column}{after} });
  return; }

# Or between this column & next...
sub addBetweenColumn {
  my ($self, @tokens) = @_;
  my @cols = @{ $$self{columns} };
  if (my $col = $$self{current_column}) {
    if (!$$self{disabled_intercolumn}) {
      unshift(@tokens, T_CS('\lx@intercol')); }
    $$self{current_column}{after} = Tokens(@{ $$self{current_column}{after} }, @tokens); }
  else {
    push(@{ $$self{save_between} }, @tokens); }
  return; }

sub disableIntercolumn {
  my ($self) = @_;
  if (my $col = $$self{current_column}) {
    $$self{disabled_intercolumn} = 1; }
  return; }

sub addColumn {
  my ($self, %properties) = @_;
  my $col = {%properties};
  if (my $prev = $$self{current_column}) {
    $$prev{after} = Tokens(($$prev{after} || ()), T_CS('\lx@intercol'))
      unless $$self{disabled_intercolumn}; }
  my @before = ();
  push(@before, @{ $$self{save_between} }) if $$self{save_between};
  push(@before, T_CS('\lx@intercol')) unless $$self{disabled_intercolumn};
  delete $$self{disabled_intercolumn};

  push(@before, $properties{before}->unlist) if $properties{before};
  push(@before, @{ $$self{save_before} })    if $$self{save_before};
  $$col{before} = Tokens(@before);
  my @after = ();
  push(@after, T_CS('\lx@column@trimright'));
  push(@after, $properties{after}->unlist) if $properties{after};
  $$col{after}           = Tokens(@after);
  $$col{thead}           = $properties{thead};
  $$col{empty}           = 1;
  $$self{save_between}   = [];
  $$self{save_before}    = [];
  $$self{current_column} = $col;

  if ($$self{repeating}) {
    $$self{non_repeating} = scalar(@{ $$self{columns} });
    push(@{ $$self{repeated} }, $col); }
  else {
    push(@{ $$self{columns} }, $col); }
  return; }

sub finish {
  my ($self) = @_;
  if (my $prev = $$self{current_column}) {
    $$prev{after} = Tokens(($$prev{after} || ()), T_CS('\lx@intercol'))
      unless $$self{disabled_intercolumn}; }
  return; }

# Methods for using a template.
sub clone {
  my ($self) = @_;
  my @dup = ();
  foreach my $cell (@{ $$self{columns} }) {
    push(@dup, {%$cell}); }
  return bless { columns => [@dup],
    repeated  => $$self{repeated}, non_repeating => $$self{non_repeating},
    repeating => $$self{repeating} }, ref $self; }

sub show {
  my ($self) = @_;
  my @strings = ();
  push(@strings, "\nColumns:\n");
  foreach my $col (@{ $$self{columns} }) {
    push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); }
  if ($$self{repeating}) {
    push(@strings, "\nRepeated Columns:\n");
    foreach my $col (@{ $$self{repeated} }) {
      push(@strings, "\n{" . join(', ', map { "$_=>" . Stringify($$col{$_}) } sort keys %$col) . '}'); } }
  return join(', ', @strings); }

sub column {
  my ($self, $n) = @_;
  my $N = scalar(@{ $$self{columns} });
  if (($n > $N) && $$self{repeating}) {
    my @rep = @{ $$self{repeated} };
    if (my $m = scalar(@rep)) {
      for (my $i = $N ; $i < $n ; $i++) {
        my %dup = %{ $rep[($i - $$self{non_repeating}) % $m] };
        push(@{ $$self{columns} }, {%dup}); } } }
  return ($n > 0 ? $$self{columns}->[$n - 1] : undef); }

sub columns {
  my ($self) = @_;
  return @{ $$self{columns} }; }

#======================================================================
1;