/usr/share/perl5/LaTeX/TOM/Tree.pm is in liblatex-tom-perl 1.00-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | ###############################################################################
#
# LaTeX::TOM::Tree
#
# This package defines a TOM Tree object.
#
###############################################################################
package LaTeX::TOM::Tree;
use strict;
use Carp qw(croak);
our $VERSION = '0.01';
# "constructor"
#
sub new {
my $class = shift;
my $nodes = shift || []; # empty array for tree structure
my $parser = shift;
my $opts = $parser->{config}{MATHBRACKETS};
my $self = {
config => { MATHBRACKETS => $opts },
nodes => $nodes,
};
return bless $self, ref($class) || $class;
}
# make a copy of a tree, recursively
#
sub copy {
my $tree = shift; # input tree
my @output; # output array (to become tree)
foreach my $node (@{$tree->{nodes}}) {
# make a copy of the node's hash definition
#
my $nodecopy = $node->copy();
# grab a copy of children, if any exist
#
if ($node->{children}) {
my $children = $node->{children}->copy();
$nodecopy->{children} = $children;
}
# add hashref to new node to array for this level
push @output, $nodecopy;
}
# each subtree is a tree
return bless [@output];
}
# Print out the LaTeX "TOM" tree. Good for debugging our parser.
#
sub print {
my $tree = shift;
my $level = shift || 0;
foreach my $node (@{$tree->{nodes}}) {
my $spacer = ' ' x ($level*2);
print $spacer;
# print grouping/command info
if ($node->{type} eq 'COMMAND') {
if ($node->{opts}) {
print "(COMMAND) \\$node->{command} [$node->{opts}] @ [$node->{start}, $node->{end}]";
} else {
print "(COMMAND) \\$node->{command} @ [$node->{start}, $node->{end}]";
}
}
elsif ($node->{type} eq 'GROUP') {
print "(GROUP) [$node->{start}, $node->{end}]";
}
elsif ($node->{type} eq 'ENVIRONMENT') {
print "(ENVIRONMENT) $node->{class} @ inner [$node->{start}, $node->{end}] outer [$node->{ostart}, $node->{oend}]";
}
elsif ($node->{type} eq 'TEXT' || $node->{type} eq 'COMMENT') {
my $spaceout = "$spacer $node->{type} |";
$spaceout =~ s/[A-Z]/ /go;
my $printtext = $node->{content};
my $maxlen = 80 - length($spaceout);
$printtext =~ s/^(.{0,$maxlen}).*$/$1/gm;
$printtext =~ s/\n/\n$spaceout/gs;
print "(".$node->{type}.") |$printtext\"";
}
if ($node->{math}) {
print " ** math mode **";
}
if ($node->{plaintext}) {
print " ** plaintext **";
}
print "\n";
# recur
if (defined $node->{children}) {
$node->{children}->print($level+1);
}
}
}
# pull out the plain text (non-math) TEXT nodes. returns an array of strings.
#
sub plainText {
my $tree = shift;
my $stringlist = [];
foreach my $node (@{$tree->{nodes}}) {
if ($node->{type} eq 'TEXT' && $node->{plaintext}) {
push @$stringlist, $node->{content};
}
if ($node->{children}) {
push @$stringlist, @{$node->{children}->plainText()};
}
}
return $stringlist;
}
# Get the plaintext of a LaTeX DOM and whittle it down into a word list
# suitable for indexing.
#
sub indexableText {
my $tree = shift;
my $pt = $tree->plainText();
my $text = join (' ', @$pt);
# kill leftover commands
$text =~ s/\\\w+\*?//gso;
# kill nonpunctuation
$text =~ s/[^\w\-0-9\s]//gso;
# kill non-intraword hyphens
$text =~ s/(\W)\-+(\W)/$1 $2/gso;
$text =~ s/(\w)\-+(\W)/$1 $2/gso;
$text =~ s/(\W)\-+(\w)/$1 $2/gso;
# kill small words
$text =~ s/\b[^\s]{1,2}\b//gso;
# kill purely numerical "words"
$text =~ s/\b[0-9]+\b//gso;
# compress whitespace
$text =~ s/\s+/ /gso;
return $text;
}
# Convert tree to LaTeX. If our output doesn't compile to the same final
# document, something is amiss (we don't, however, guarantee that the output
# TeX will be identical to the input, due to certain normalizations.)
#
sub toLaTeX {
my $tree = shift;
my $parent = shift;
my $str = "";
foreach my $node (@{$tree->{nodes}}) {
if ($node->{type} eq 'TEXT' ||
$node->{type} eq 'COMMENT') {
$str .= $node->{content};
}
elsif ($node->{type} eq 'GROUP') {
$str .= '{' . $node->{children}->toLaTeX($node) . '}';
}
elsif ($node->{type} eq 'COMMAND') {
if ($node->{position} eq 'outer') {
$str .= "\\$node->{command}" . '{' . $node->{children}->toLaTeX($node) . '}';
}
elsif ($node->{position} eq 'inner') {
if (defined $parent && # dont add superfluous braces
$parent->{start} == $node->{start} &&
$parent->{end} == $node->{end}) {
$str .= "\\$node->{command}" . ' ' . $node->{children}->toLaTeX($node);
} else {
$str .= '{' . "\\$node->{command}" . $node->{children}->toLaTeX($node) . '}';
}
}
elsif ($node->{braces} == 0) {
$str .= "\\$node->{command}" . ' ' . $node->{children}->toLaTeX($node);
}
}
elsif ($node->{type} eq 'ENVIRONMENT') {
# handle special math mode envs
if (defined $tree->{config}{MATHBRACKETS}->{$node->{class}}) {
# print with left and lookup right brace.
$str .= $node->{class} . $node->{children}->toLaTeX($node) . $tree->{config}{MATHBRACKETS}->{$node->{class}};
}
# standard \begin/\end envs
else {
$str .= "\\begin{$node->{class}}" . $node->{children}->toLaTeX($node) . "\\end{$node->{class}}";
}
}
}
return $str;
}
# Augment the nodes in the tree with pointers to all neighboring nodes, so
# traversal is easier for the user who is given a lone node. This is a hack,
# we should really be maintaining this all along.
#
# Note that child pointers are already taken care of.
#
sub listify {
my $tree = shift;
my $parent = shift;
for (my $i = 0; $i < scalar @{$tree->{nodes}}; $i++) {
my $prev = undef;
my $next = undef;
$prev = $tree->{nodes}[$i - 1] if ($i > 0);
$next = $tree->{nodes}[$i + 1] if ($i + 1 < scalar @{$tree->{nodes}});
$tree->{nodes}[$i]->{'prev'} = $prev;
$tree->{nodes}[$i]->{'next'} = $next;
$tree->{nodes}[$i]->{'parent'} = $parent;
# recur, with parent info
if ($tree->{nodes}[$i]->{children}) {
$tree->{nodes}[$i]->{children}->listify($tree->{nodes}[$i]);
}
}
}
###############################################################################
# "Tree walking" methods.
#
sub getTopLevelNodes {
my $tree = shift;
return @{$tree->{nodes}};
}
sub getAllNodes {
my $tree = shift;
my @nodelist;
foreach my $node (@{$tree->{nodes}}) {
push @nodelist, $node;
if ($node->{children}) {
push @nodelist, @{$node->{children}->getAllNodes()};
}
}
return [@nodelist];
}
sub getNodesByCondition {
my $tree = shift;
my $condition = shift;
# XXX rt #48551 - string eval no longer supported (12/08/2009)
unless (ref $condition eq 'CODE') {
croak 'getNodesByCondition(): code reference expected';
}
my @nodelist;
foreach my $node (@{$tree->{nodes}}) {
# evaluate the perl code condition and if the result evaluates to true,
# push this node
#
if ($condition->($node)) {
push @nodelist, $node;
}
if ($node->{children}) {
push @nodelist, @{$node->{children}->getNodesByCondition($condition)};
}
}
return [@nodelist];
}
sub getCommandNodesByName {
my $tree = shift;
my $name = shift;
return $tree->getNodesByCondition(
sub { my $node = shift; return ($node->{type} eq 'COMMAND' && $node->{command} eq $name); }
);
}
sub getEnvironmentsByName {
my $tree = shift;
my $name = shift;
return $tree->getNodesByCondition(
sub { my $node = shift; return ($node->{type} eq 'ENVIRONMENT' && $node->{class} eq $name); }
);
}
sub getFirstNode {
my $tree = shift;
return $tree->{nodes}[0];
}
1;
|