| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Pre::BibTeX | # | ||
| 3 | # | Implements BibTeX for LaTeXML | # | ||
| 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::Pre::BibTeX; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Global; | ||
| 16 | use LaTeXML::Common::Error; | ||
| 17 | use LaTeXML::Util::Pathname; | ||
| 18 | use LaTeXML::Pre::BibTeX::Entry; | ||
| 19 | use Text::Balanced qw(extract_delimited extract_bracketed); | ||
| 20 | |||
| 21 | #********************************************************************** | ||
| 22 | # LaTeXML::Pre::BibTeX is responsible for the lowest level parsing of | ||
| 23 | # BibTeX files, collecting and storing a set of entries | ||
| 24 | # (along with any TeX code in @PREAMBLE). | ||
| 25 | # It doesn't analyze the entries or fields. | ||
| 26 | # See BibTeX.pool.ltxml for the processing. | ||
| 27 | # | ||
| 28 | # NOTE: This should be run from within a binding of $STATE. | ||
| 29 | # | ||
| 30 | # NOTE: It may be desirable to save the line (& column) numbers | ||
| 31 | # for the TeX fragments to assist in debugging message later. | ||
| 32 | # Column number is currently kinda flubbed up. | ||
| 33 | #********************************************************************** | ||
| 34 | my %default_macros = ( # [CONSTANT] | ||
| 35 | jan => "January", feb => "February", mar => "March", apr => "April", | ||
| 36 | may => "May", jun => "June", jul => "July", aug => "August", | ||
| 37 | sep => "September", oct => "October", nov => "November", dec => "December", | ||
| 38 | acmcs => "ACM Computing Surveys", | ||
| 39 | acta => "Acta Informatica", | ||
| 40 | cacm => "Communications of the ACM", | ||
| 41 | ibmjrd => "IBM Journal of Research and Development", | ||
| 42 | ibmsj => "IBM Systems Journal", | ||
| 43 | ieeese => "IEEE Transactions on Software Engineering", | ||
| 44 | ieeetc => "IEEE Transactions on Computers", | ||
| 45 | ieeetcad => "IEEE Transactions on Computer-Aided Design of Integrated Circuits", | ||
| 46 | ipl => "Information Processing Letters", | ||
| 47 | jacm => "Journal of the ACM", | ||
| 48 | jcss => "Journal of Computer and System Sciences", | ||
| 49 | scp => "Science of Computer Programming", | ||
| 50 | sicomp => "SIAM Journal on Computing", | ||
| 51 | tocs => "ACM Transactions on Computer Systems", | ||
| 52 | tods => "ACM Transactions on Database Systems", | ||
| 53 | tog => "ACM Transactions on Graphics", | ||
| 54 | toms => "ACM Transactions on Mathematical Software", | ||
| 55 | toois => "ACM Transactions on Office Information Systems", | ||
| 56 | toplas => "ACM Transactions on Programming Languages and Systems", | ||
| 57 | tcs => "Theoretical Computer Science"); | ||
| 58 | #====================================================================== | ||
| 59 | |||
| 60 | sub newFromFile { | ||
| 61 | my ($class, $bibname) = @_; | ||
| 62 | my $self = { source => $bibname, preamble => [], entries => [], macros => {%default_macros} }; | ||
| 63 | bless $self, $class; | ||
| 64 | my $paths = $STATE->lookupValue('SEARCHPATHS'); | ||
| 65 | my $file = pathname_find($bibname, types => ['bib'], paths => $paths); | ||
| 66 | Fatal('missing_file', $bibname, undef, "Can't find BibTeX file $bibname", | ||
| 67 | "SEACHPATHS is " . join(', ', @$paths)) unless $file; | ||
| 68 | my $BIB; | ||
| 69 | $$self{file} = $bibname; | ||
| 70 | open($BIB, '<', $file) or Fatal('I/O', $file, undef, "Can't open BibTeX $file for reading", $!); | ||
| 71 | $$self{lines} = [<$BIB>]; | ||
| 72 | close($BIB); | ||
| 73 | $$self{line} = shift(@{ $$self{lines} }) || ''; | ||
| 74 | $$self{lineno} = 1; | ||
| 75 | return $self; } | ||
| 76 | |||
| 77 | sub newFromString { | ||
| 78 | my ($class, $string) = @_; | ||
| 79 | my $self = { source => undef, preamble => [], entries => [], macros => {%default_macros} }; | ||
| 80 | bless $self, $class; | ||
| 81 | |||
| 82 | $$self{file} = "<anonymous>"; | ||
| 83 | $$self{lines} = [split(/\n/, $string)]; # Uh, what about CR's? | ||
| 84 | $$self{line} = shift(@{ $$self{lines} }); | ||
| 85 | $$self{lineno} = 1; | ||
| 86 | return $self; } | ||
| 87 | |||
| 88 | # Read all available lines from available Mouth's in the Gullet. | ||
| 89 | sub newFromGullet { | ||
| 90 | my ($class, $name, $gullet) = @_; | ||
| 91 | my $self = { source => $name, preamble => [], entries => [], macros => {%default_macros} }; | ||
| 92 | bless $self, $class; | ||
| 93 | |||
| 94 | my @lines = (); | ||
| 95 | while ($gullet->getMouth->hasMoreInput) { | ||
| 96 | while (defined(my $line = $gullet->readRawLine)) { | ||
| 97 | push(@lines, $line . "\n"); } | ||
| 98 | $gullet->closeMouth; } | ||
| 99 | |||
| 100 | $$self{file} = $name; | ||
| 101 | $$self{lines} = [@lines]; | ||
| 102 | $$self{line} = shift(@{ $$self{lines} }); | ||
| 103 | $$self{lineno} = 1; | ||
| 104 | return $self; } | ||
| 105 | |||
| 106 | sub toString { | ||
| 107 | my ($self) = @_; | ||
| 108 | return 'Bibliography[' . ($$self{source} || '<Unknown>') . ']'; } | ||
| 109 | |||
| 110 | sub toTeX { | ||
| 111 | my ($self) = @_; | ||
| 112 | $self->parseTopLevel unless $$self{parsed}; | ||
| 113 | # Store all entries into $STATE under BIBKEY@$key. | ||
| 114 | # NOTE: The case in the key has been preserved, but we'll store it under the lowercase! | ||
| 115 | foreach my $entry (@{ $$self{entries} }) { | ||
| 116 | $STATE->assignValue('BIBENTRY@' . lc($entry->getKey) => $entry); } | ||
| 117 | |||
| 118 | return join("\n", | ||
| 119 | @{ $$self{preamble} }, | ||
| 120 | '\begin{bibtex@bibliography}', | ||
| 121 | (map { '\ProcessBibTeXEntry{' . $_->getKey . '}' } @{ $$self{entries} }), | ||
| 122 | '\end{bibtex@bibliography}'); } | ||
| 123 | |||
| 124 | # This lets Bib support formatted error messages. | ||
| 125 | sub getLocator { | ||
| 126 | my ($self) = @_; | ||
| 127 | # TODO: The content of $$self{line} is not used here anymore | ||
| 128 | # do we really need it? | ||
| 129 | return LaTeXML::Common::Locator->new($$self{source}, $$self{lineno}); } | ||
| 130 | |||
| 131 | #====================================================================== | ||
| 132 | # Greg Ward has a pretty good description of the BibTeX data format | ||
| 133 | # as part of his btparser package (See CPAN) | ||
| 134 | #====================================================================== | ||
| 135 | # Parse the Bibliography at the Top level | ||
| 136 | # consists of skipping till we find an @name | ||
| 137 | # and parse the body according to the type. | ||
| 138 | sub parseTopLevel { | ||
| 139 | my ($self) = @_; | ||
| 140 | my $note = "Preparsing Bibliography " . ($$self{source} || "<Unknown>"); | ||
| 141 | ProgressSpinup($note); | ||
| 142 | while ($self->skipJunk) { | ||
| 143 | my $type = $self->parseEntryType; | ||
| 144 | if ($type eq 'preamble') { $self->parsePreamble; } | ||
| 145 | elsif ($type eq 'string') { $self->parseMacro; } | ||
| 146 | elsif ($type eq 'comment') { $self->parseComment; } | ||
| 147 | else { $self->parseEntry($type); } | ||
| 148 | } | ||
| 149 | ProgressSpindown($note); | ||
| 150 | $$self{parsed} = 1; | ||
| 151 | return; } | ||
| 152 | |||
| 153 | #============================== | ||
| 154 | my %CLOSE = ("{" => "}", "(" => ")"); # [CONSTANT] | ||
| 155 | |||
| 156 | # @preamble open "rawtex" close | ||
| 157 | # open = { or ( close is balancing } or ) | ||
| 158 | sub parsePreamble { | ||
| 159 | my ($self) = @_; | ||
| 160 | my $open = $self->parseMatch("({"); | ||
| 161 | my ($value, $rawvalue) = $self->parseValue(); | ||
| 162 | push(@{ $$self{preamble} }, $value); | ||
| 163 | $self->parseMatch($CLOSE{$open}); | ||
| 164 | return; } | ||
| 165 | |||
| 166 | # @string open [name = value]* close | ||
| 167 | # open = { or ( close is balancing } or ) | ||
| 168 | sub parseMacro { | ||
| 169 | my ($self) = @_; | ||
| 170 | my $open = $self->parseMatch("({"); | ||
| 171 | my ($fields, $rawfields) = $self->parseFields('@string', $open); | ||
| 172 | foreach my $macro (@$fields) { | ||
| 173 | $$self{macros}{ $$macro[0] } = $$macro[1]; } | ||
| 174 | return; } | ||
| 175 | |||
| 176 | # @comment string | ||
| 177 | sub parseComment { | ||
| 178 | my ($self) = @_; | ||
| 179 | my $comment = $self->parseString(); # Supposedly should accept () delimited strings, too? | ||
| 180 | # store it? | ||
| 181 | return; } | ||
| 182 | |||
| 183 | # @entryname open name, [name = value]* close | ||
| 184 | # open = { or ( close is balancing } or ) | ||
| 185 | sub parseEntry { | ||
| 186 | my ($self, $type) = @_; | ||
| 187 | my $open = $self->parseMatch("({"); | ||
| 188 | my $key = $self->parseEntryName(); | ||
| 189 | $self->parseMatch(','); | ||
| 190 | # NOTE: actually, the entry should be ignored if there already is one for $key! | ||
| 191 | my ($fields, $rawfields) = $self->parseFields('@string', $open); | ||
| 192 | push(@{ $$self{entries} }, LaTeXML::Pre::BibTeX::Entry->new($type, $key, $fields, $rawfields)); | ||
| 193 | return; } | ||
| 194 | |||
| 195 | sub parseFields { | ||
| 196 | my ($self, $for, $open) = @_; | ||
| 197 | my @fields = (); | ||
| 198 | my @rawfields = (); | ||
| 199 | do { | ||
| 200 | my $name = $self->parseFieldName; | ||
| 201 | $self->parseMatch('='); | ||
| 202 | my ($value, $rawvalue) = $self->parseValue; | ||
| 203 | push(@fields, [$name, $value]); | ||
| 204 | push(@rawfields, [$name, $rawvalue]); | ||
| 205 | $self->skipWhite; | ||
| 206 | } while (($$self{line} =~ s/^,//) # like parseMatch, but we just end parsing fields if missing | ||
| 207 | && $self->skipWhite && ($$self{line} !~ /^\Q$CLOSE{$open}\E/)); | ||
| 208 | $self->parseMatch($CLOSE{$open}); | ||
| 209 | return ([@fields], [@rawfields]); } | ||
| 210 | |||
| 211 | #============================== | ||
| 212 | # Low level parsing | ||
| 213 | |||
| 214 | # There are several kinds of names here, and they allow different stuff. | ||
| 215 | # Most of the odd stuff eventually will cause problems processing by LaTeX, | ||
| 216 | # Especially "\", which BibTeX allows, but it throws us off (semiverbatim vs verbatim) | ||
| 217 | # when we store the bibentries before digesting the key! | ||
| 218 | |||
| 219 | # Use strings for these, since they're character classes | ||
| 220 | # & inserted into qr/.../ which otherwise (sometimes) mangles them. | ||
| 221 | my $BIBNAME_re = q/a-zA-Z0-9/; # [CONSTANT] | ||
| 222 | my $BIBNOISE_re = q/\.\+\-\*\/\^\_\:\;\@\`\?\!\~\|\<\>\$\[\]/; # [CONSTANT] | ||
| 223 | |||
| 224 | sub parseEntryType { | ||
| 225 | my ($self) = @_; | ||
| 226 | $self->skipWhite; | ||
| 227 | return ($$self{line} =~ s/^([$BIBNAME_re$BIBNOISE_re]*)//x ? lc($1) : undef); } | ||
| 228 | |||
| 229 | sub parseEntryName { | ||
| 230 | my ($self) = @_; | ||
| 231 | $self->skipWhite; | ||
| 232 | return ( # Preserve case (at this point!) | ||
| 233 | $$self{line} =~ s/^([\"\#\%\&\'\(\)\=\{$BIBNAME_re$BIBNOISE_re]*)//x ? $1 : undef); } | ||
| 234 | |||
| 235 | sub parseFieldName { | ||
| 236 | my ($self) = @_; | ||
| 237 | $self->skipWhite; | ||
| 238 | return ($$self{line} =~ s/^([\&$BIBNAME_re$BIBNOISE_re]*)//x ? lc($1) : undef); } | ||
| 239 | |||
| 240 | sub parseMatch { | ||
| 241 | my ($self, $delims) = @_; | ||
| 242 | $self->skipWhite; | ||
| 243 | if (!$delims) { | ||
| 244 | return; } | ||
| 245 | elsif ($$self{line} =~ s/^([\Q$delims\E])//) { | ||
| 246 | return $1; } | ||
| 247 | else { | ||
| 248 | Error('expected', $delims, undef, "Expected one of " . join(' ', split(//, $delims))); | ||
| 249 | return; } } | ||
| 250 | |||
| 251 | # A string is delimited with balanced {}, or "" | ||
| 252 | sub parseString { | ||
| 253 | my ($self) = @_; | ||
| 254 | $self->skipWhite; | ||
| 255 | my $string; | ||
| 256 | if ($$self{line} =~ s/^\"//) { # If opening " (and remove it!) | ||
| 257 | # Note that BibTeX doesn't see a " if it is enclosed with {}, so can't use extract_delmited | ||
| 258 | while ($$self{line} !~ s/^\"//) { # Until we've found the closing " | ||
| 259 | if (!$$self{line}) { $self->extendLine; } | ||
| 260 | elsif ($$self{line} =~ /^\{/) { # Starts with a brace! extract balanced {} | ||
| 261 | $string .= $self->parseBalancedBraces; } | ||
| 262 | elsif ($$self{line} =~ s/^([^"\{]*)//) { # else pull off everything except a brace or " | ||
| 263 | $string .= $1; } } | ||
| 264 | } | ||
| 265 | elsif ($$self{line} =~ /^\{/) { | ||
| 266 | $string = $self->parseBalancedBraces; | ||
| 267 | $string =~ s/^.//; # Remove the delimiters. | ||
| 268 | $string =~ s/.$//; } | ||
| 269 | else { | ||
| 270 | Error('expected', '<delimitedstring>', undef, | ||
| 271 | "Expected a string delimited by \"..\", (..) or {..}"); } | ||
| 272 | $string =~ s/^\s+//; # and trim | ||
| 273 | $string =~ s/\s+$//; | ||
| 274 | return $string; } | ||
| 275 | |||
| 276 | sub parseBalancedBraces { | ||
| 277 | my ($self) = @_; | ||
| 278 | my $string; | ||
| 279 | # minor optimization: make sure there's at least one closing } | ||
| 280 | while (($$self{line} !~ /\}/) && $self->extendLine) { } | ||
| 281 | # Now try to parse balanced {}, extending until we do get a balanced pair. | ||
| 282 | while ((!defined($string = extract_bracketed($$self{line}, '{}'))) && $self->extendLine) { } | ||
| 283 | return $string; } | ||
| 284 | |||
| 285 | sub extendLine { | ||
| 286 | my ($self) = @_; | ||
| 287 | my $nextline = shift(@{ $$self{lines} }); | ||
| 288 | if (defined $nextline) { | ||
| 289 | $$self{line} .= $nextline; | ||
| 290 | $$self{lineno}++; | ||
| 291 | return 1; } | ||
| 292 | else { | ||
| 293 | Error('unexpected', '<EOF>', undef, | ||
| 294 | "Input ended while parsing string"); | ||
| 295 | return; } } | ||
| 296 | |||
| 297 | # value : simple_value ( HASH simple_value)* | ||
| 298 | # simple_value : string | NAME | ||
| 299 | sub parseValue { | ||
| 300 | my ($self) = @_; | ||
| 301 | my $value = ""; | ||
| 302 | do { | ||
| 303 | $self->skipWhite; | ||
| 304 | if ($$self{line} =~ /^[\"\{]/) { | ||
| 305 | $value .= $self->parseString; } | ||
| 306 | elsif (my $name = $self->parseFieldName) { | ||
| 307 | my $macro = ($name =~ /^\d+$/ ? $name : $$self{macros}{$name}); | ||
| 308 | if (!defined $macro) { | ||
| 309 | Error('unexpected', $name, undef, | ||
| 310 | "The BibTeX macro '$name' is not defined"); | ||
| 311 | $macro = $name; } # Default error handling is leave the text in? | ||
| 312 | $value .= $macro; } | ||
| 313 | else { | ||
| 314 | Error('expected', '<value>', undef, | ||
| 315 | "Expected a BibTeX value"); } | ||
| 316 | $self->skipWhite; | ||
| 317 | } while ($$self{line} =~ s/^#//); | ||
| 318 | return $value; } | ||
| 319 | |||
| 320 | sub skipWhite { | ||
| 321 | my ($self) = @_; | ||
| 322 | my $nextline; | ||
| 323 | do { | ||
| 324 | $$self{line} =~ s/^(\s+)//s; | ||
| 325 | return 1 if $$self{line}; | ||
| 326 | $nextline = shift(@{ $$self{lines} }); | ||
| 327 | $$self{line} = $nextline || ""; | ||
| 328 | $$self{lineno}++; | ||
| 329 | } while defined $nextline; | ||
| 330 | return; } | ||
| 331 | |||
| 332 | # Although % officially starts comments, apparently BibTeX accepts | ||
| 333 | # anything until @ as an "implied comment" | ||
| 334 | # So, until an @ is encountered, pretty much skip anything | ||
| 335 | sub skipJunk { | ||
| 336 | my ($self) = @_; | ||
| 337 | $$self{line} ||= ""; | ||
| 338 | while (1) { | ||
| 339 | $$self{line} =~ s/^[^@%]*//; # Skip till comment or @ | ||
| 340 | return '@' if $$self{line} =~ s/^@//; # Found @ | ||
| 341 | # else line is empty, or a comment, so get next | ||
| 342 | my $nextline = shift(@{ $$self{lines} }); | ||
| 343 | $$self{line} = $nextline || ""; | ||
| 344 | $$self{lineno}++; | ||
| 345 | return unless defined $nextline; } | ||
| 346 | return; } | ||
| 347 | |||
| 348 | #====================================================================== | ||
| 349 | 1; | ||
| 350 | |||
| 351 | __END__ |