BibTeX PARSE

version 1
package wikindx4\core\importexport
author Mark Grimshaw

 Methods

BIBTEXPARSE

__construct() 

For a quick command-line test (php -f PARSEENTRIES.php) after installation, uncomment these lines:

Parse a file $parse = NEW PARSEENTRIES(); $parse->expandMacro = TRUE; // $array = array("RMP" =>"Rev., Mod. Phys."); // $parse->loadStringMacro($array); // $parse->removeDelimit = FALSE; // $parse->fieldExtract = FALSE; $parse->openBib("bib.bib"); $parse->extractEntries(); $parse->closeBib(); list($preamble, $strings, $entries, $undefinedStrings) = $parse->returnArrays(); print_r($preamble); print "\n"; print_r($strings); print "\n"; print_r($entries); print "\n\n";

// Parse a bibtex PHP string $bibtex_data = <<< END

\@STRING{three = "THREE"} \@STRING{two = "TWO"} \@string{JRNL23 = {NatLA 23 } # " " # two # " " # three}

\@article{klitzing.1, author = "v. Klitzing and Dorda and Pepper", title = "New method for high mark@sirfragalot.com accuracy determination of fine structure constant based on quantized hall resistance", volume = "45", journal = {Journal of } # JRNL23, pages = "494", citeulike-article-id = {12222 } , ignoreMe = {blah}, }

\@article { klitzing.2, author = "Klaus von Klitzing", title = "The Quantized Hall Effect", volume = "58", journal = two, pages = "519", }

END;

$parse = NEW PARSEENTRIES(); $parse->expandMacro = TRUE; // $parse->removeDelimit = FALSE; // $parse->fieldExtract = FALSE; $array = array("RMP" =>"Rev., Mod. Phys."); $parse->loadStringMacro($array); $parse->loadBibtexString($bibtex_data); $parse->extractEntries(); list($preamble, $strings, $entries, $undefinedStrings) = $parse->returnArrays(); print_r($preamble); print "\n"; print_r($strings); print "\n"; print_r($entries); print "\n\n";

Close bib file

closeBib() 

This function receives a string and a closing delimiter '}' or ')'

closingDelimiter(string $val, string $delimitEnd) : int

and looks for the position of the closing delimiter taking into account the following Bibtex rules: Inside the braces, there can arbitrarily nested pairs of braces, but braces must also be balanced inside quotes! Inside quotes, to place the " character it is not sufficient to simply escape with \": Quotes must be placed inside braces.

Parameters

$val

string

$delimitEnd

string

Returns

int

This function works like explode('#',$val) but has to take into account whether

explodeString(string $val) : array

the character # is part of a string (i.e., is enclosed into "..." or {...} ) or defines a string concatenation as in @string{ "x # x" # ss # {xx{x}x} }

Parameters

$val

string

Returns

array

This function extract entries taking into account how comments are defined in BibTeX.

extractEntries() 

BibTeX splits the file in two areas: inside an entry and outside an entry, the delimitation being indicated by the presence of a @ sign. When this character is met, BibTex expects to find an entry. Before that sign, and after an entry, everything is considered a comment!

Extract value part of @string field enclosed by double-quotes or braces.

extractStringValue(string $string) : string

The string may be expanded with previously-defined strings

Parameters

$string

string

Returns

string

Extract a field

fieldSplit(string $seg) : array

Parameters

$seg

string

Returns

array

Start splitting a bibtex entry into component fields.

fullSplit(string $entry) 

Store the entry type and citation.

Parameters

$entry

string

Get a non-empty line from the bib file or from the bibtexString

getLine() : string

Returns

string

Load a bibtex string to parse it

loadBibtexString(string $bibtex_string) 

Parameters

$bibtex_string

string

Set strings macro

loadStringMacro(array $macro_array) 

Parameters

$macro_array

array

Open bib file

openBib(string $file) 

Parameters

$file

string

Grab a complete bibtex entry

parseEntry(string $entry) 

Parameters

$entry

string

Extract and format fields

reduceFields(string $oldString) 

Parameters

$oldString

string

Remove delimiters from a string

removeDelimiters(string $string) : string

Parameters

$string

string

Returns

string

Remove enclosures around entry field values.

removeDelimitersAndExpand(string $string, boolean $inpreamble) : string

Additionally, expand macros if flag set.

Parameters

$string

string

$inpreamble

boolean

Default is FALSE

Returns

string

Return arrays of entries etc.

returnArrays() : array

to the calling process.

Returns

array(preamble, strings, entry, undefinedStrings)