| 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
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | LINE: | ||||||||||||||||||||
| 66 | 1 | ▶ | while (<FILE>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 26 | ▶ | if (m/^%/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 88 | 1 | $self->load; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | my $content = $self->{'content'}; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 1 | ▶ | do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 25 | ▶ | if ($content =~ m/^(.*?)([\\\{])/s) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 24 | my $prematch = $1; | |||||||||||||||||||
| 96 | 24 | my $match = $2; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 24 | ▶ | if ($prematch ne '') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 149 | 272 | my $c = substr($text, $loop, 1); | |||||||||||||||||||
| 150 | 272 | ▶ | if ($c eq $begin) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 2 | $deep++; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | 272 | ▶ | if ($c eq $end) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 15 | $deep--; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | 272 | $loop++; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | 13 | return substr($text, 0, $loop); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||