blib/lib/LaTeX/Parser.pm

filter: Statementstmt 95.5% 65 / 68 Branchbran 78.5% 22 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 50.0% 2 / 4 totaltotal 86.4% 96 / 111 CC 16 SCAR 17.6
CC
16
Cov
89%
latex
9.4
load
4.0
matching
4.0
CRAP 5.8
↑ Summary
1 package LaTeX::Parser;
2  
3 =head1 NAME
4  
5 LaTeX::Parser - Perl extension to parse LaTeX files
6  
7 =head1 SYNOPSIS
8  
9 use LaTeX::Parser;
10 my $l = new LaTeX::Parser 'file' => 'file.tex';
11 my $p = $l->latex; # $p now hold a reference to an array of
12 # file.tex parsed
13  
14 Or use it to break up LaTeX in a variable:
15  
16 my $l = new LaTeX::Parser 'content' =>
17 '\textit{Three Lives} by Gertrude Stein.';
18  
19 Contents of nested braces are extracted as a single element. Another
20 C<LaTeX::Parser> will have to be created to parse nested braces.
21  
22 This is a very early version of C<LaTeX::Parser>, there are many bugs.
23 I think this will work fine with plain TeX files but I do not plan on
24 ever support that.
25  
26 =head1 DESCRIPTION
27  
28 For now, only simple descriptions of the modules functions.
29  
30 =cut
31  
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 =over 4
37  
38 =item LaTeX::Parser->new %hash
39  
40 Creates a LaTeX::Parser object. All values in C<%hash> are initialize
41 to the values in the object. The only two tested values to set are
42 `C<file>' and `C<content>'. `C<file>' is the name of the file to load
43 the LaTeX file from, and it get copied into `C<content>'. If content
44 is set by then C<%hash> then `C<file>' will never be called.
45  
46 =cut
47  
48 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
49 1 my $this = shift;
50 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
51 1 my %hash = @_;
52 1 my $self = \%hash;
53 1 bless $self, $class;
54 1 return $self;
55 }
56  
57 # Function to be considered private that loads the LaTeX file and
58 # throws out comments.
59 1 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
60 1 my $self = shift;
61  
62 1 $self->{'content'} = '';
63 1 ▶ open(FILE, $self->{'file'}) || die "Can't load `$self->{file}', $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $self->{"file"} or die qq[Can't load `$self->{"file"}', $!\n] 1 0 1
64
65 LINE:
66 1 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 26 1 27
67 26 ▶ if (m/^%/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^%/) 1 25 26
68 1 next LINE;
69 }
70 25 s/%.*$//;
71 25 $self->{'content'} .= $_;
72 }
73 1 close(FILE);
74 1 return $self;
75 }
76  
77 =item LaTeX::Parser->latex
78  
79 No arguments. Actualy does all the work. Loads the LaTeX file if not
80 content was specified, and returns a reference to all parsed
81 information.
82  
83 =cut
84 1 ▶sub latex {
Subroutine latex: called CC=9 SCAR=22.4
Pod documented
85 1 my $self = shift;
86  
87 1 ▶ if (!defined $self->{'content'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"content"}) 1 0 1
88 1 $self->load;
89 }
90  
91 1 my $content = $self->{'content'};
92
93 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$content ne "" and do { do { if ($content =~ /^(.*?)([\\\{])/s) { my $prematch = $1; my $match = $2; if ($prematch ne "") { push @{$$self{"parsed"};}, $prematch; $prematch = quotemeta $prematch; $content =~ s/^$prematch//s; } ; if ($match eq "{") { $match = &matching("{", "}", $content); push @{$$self{"parsed"};}, $match; $match = quotemeta $match; $content =~ s/^$match//s; } elsif ($match eq "\\") { if ($content =~ /^(\\[\w\\]+)/) { $match = $1; } elsif ($content =~ /^(\\.)/) { $match = $1; } else { die "A \\ Command I don't understand"; } ; push @{$$self{"parsed"};}, $match; $match = quotemeta $match; $content =~ s/^$match//s; } else { die "Found `${match}' where only `{' of `\\' should be"; } ; } else { push @{$$self{"parsed"};}, $content; $content = ""; } } } 24 1 25
94 25 ▶ if ($content =~ m/^(.*?)([\\\{])/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^(.*?)([\\\{])/s) { } 24 1 25
95 24 my $prematch = $1;
96 24 my $match = $2;
97  
98 24 ▶ if ($prematch ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prematch ne "") 11 13 24
99 11 push @{$self->{'parsed'}}, $prematch;
100 11 $prematch = quotemeta($prematch);
101 11 $content =~ s/^$prematch//s;
102 }
103
104 24 ▶ if ($match eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($match eq '{') { } 13 11 24
elsif ($match eq '\\') { } 11 0 11
105 13 $match = &matching('{', '}', $content);
106 13 push @{$self->{'parsed'}}, $match;
107 13 $match = quotemeta($match);
108 13 $content =~ s/^$match//s;
109 } elsif ($match eq '\\') {
110 11 ▶ if ($content =~ m/^(\\[\w\\]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /^(\\[\w\\]+)/) { } 11 0 11
elsif ($content =~ /^(\\.)/) { } 0 0 0
111 11 $match = $1;
112 } elsif ($content =~ m/^(\\.)/) {
113 0 $match = $1;
114 } else {
115 0 die "A \\ Command I don't understand";
116 }
117 11 push @{$self->{'parsed'}}, $match;
118 11 $match = quotemeta($match);
119 11 $content =~ s/^$match//s;
120 } else {
121 0 die "Found `$match' where only `{' of `\\' should be";
122 }
123  
124 } else {
125 1 push @{$self->{'parsed'}}, $content;
126 1 $content = '';
127 }
128 } while ($content ne '');
129 1 return $self->{'parsed'};
130 }
131  
132  
133  
134 ##############
135  
136 # Just a little utility program to match nested, single character
137 # delimited quotes. Should make it so one can backslach the
138 # delimiter.
139  
140 13 ▶sub matching {
Subroutine matching: called CC=4 SCAR=13.9
Pod undocumented
141 13 my $begin = shift;
142 13 my $end = shift;
143 13 my $text = shift;
144  
145 13 my $loop = 1;
146 13 my $deep = 1;
147  
148 13 ▶ until ($deep == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($deep == 0) 272 13 285
149 272 my $c = substr($text, $loop, 1);
150 272 ▶ if ($c eq $begin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $begin) 2 270 272
151 2 $deep++;
152 }
153 272 ▶ if ($c eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $end) 15 257 272
154 15 $deep--;
155 }
156 272 $loop++;
157 }
158 13 return substr($text, 0, $loop);
159 }
160  
161 1 1;
162 __END__
↑ Summary