# Copyright (C) 2008 Eric Hsu # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc. # 59 Temple Place, Suite 330 # Boston, MA 02111-1307 USA $ModulesDescription .= '

$Id: bibtex-bfc4.pl,v 0.3b 2008/07/12 23:20:09 Eric Hsu Exp $

'; # ============== # = bibtex-bfc = # ============== push( @MyRules, \&BibtexRule ); $RuleOrder{\&BibtexRule}=-300; # First parse the data. Then output to a nice table, css-stylable. # We need our data in a special format. # - The whole chunk begins with @TYPE{ or @TYPE( # - Every field name is \w+ followed by = followed by something delimited with "" or {} # - If you must put newlines in a field, don't make the first characters be WORD = , or we'll parse it as a new field. # - Please remove newlines from a field... it will make in-field searches work better. On display, we are kind to \item and \\. # We do parse some elementary latex. So, we'll trim away all \begin{*} and \end{*}. # We will TRY to respect {enumerate} and {itemize}. SOMEDAY. sub BibtexRule { # We just match the stuff from the first leading @ to the following ---- # which denotes the tags/comment start return undef if ($PassToHtmlEditor); # Leave as markup for HTML WYSIWYG editor my @fieldorder = qw(AUTHOR TITLE JOURNAL BOOKTITLE CHAPTER VOLUME NUMBER PAGES MONTH YEAR EDITOR SERIES PUBLISHER ORGANIZATION INSTITUTION SCHOOL ADDRESS CITY COUNTRY EDITION ISBN ISSN LEVEL SOURCE _UNSORTED ABSTRACT ANNOTE NOTE TYPE); my @fieldbanned = qw( KEYWORDS KEY TOPIC FOCUS ); if (m/\G<pre[^&]*>\s*(\@(.*\n)*.*?)<\/pre>/gc) { my $text = $1; my $bibtextable = MakeBibtex($text, \@fieldorder, \@fieldbanned); return WrapInBibtexMinilinksTable( $bibtextable, GetId() ); } else { return undef; } } sub WrapInBibtexMinilinksTable { my ($item, $id) = @_; my $scripturl = ScriptUrl("action=browsetrim;raw=1;id=$id"); #*** make popup. my $biblink = qq( •bibtex); my $tablewithlinks = "
$item$biblink
"; return $tablewithlinks; } # DoBrowseTrim is the action that extracts from a page the first BibTeX entry and emits it as plain text. $Action{"browsetrim"}=\&DoBrowseTrim; sub DoBrowseTrim { my $html; open my $newout, '>', \$html; my $oldout = select; select $newout; BrowseResolvedPage(@_); select $oldout; $html =~ /^(.*?)(Content-Type:.*?\n.*?)<.*?pre[^>]*>(\@.*?)<\/pre>.*/s; print "$1$2$3"; } # In @$fieldorder put _UNSORTED whenever you want to show all the unsorted ones. # This allows you to force some fields to be at the very end. # If you don't put _UNSORTED, then we won't show them at all. sub MakeBibtex { my ($text, $fieldorder, $fieldbanned) = @_; my $result; use HTML::Entities; $text=decode_entities($text); my $value_of = BibtexParse($text); my @fieldorder = @$fieldorder ; my @fieldbanned = @$fieldbanned; foreach $field (@fieldorder) { if ($field eq "_UNSORTED") { $result .= "_UNSORTED"; } else { $result .= BibtexFieldRow($field, $value_of->{$field}); } delete $value_of->{$field}; } my $unsortedresult; foreach $field (@fieldbanned) { delete $value_of->{$field}; } foreach $field (sort keys %$value_of) { $unsortedresult .= BibtexFieldRow($field, $value_of->{$field}); } $result =~ s/_UNSORTED/$unsortedresult/; return BibtexFieldTable($result); } sub BibtexFieldTable { my $table = shift; return "" . $table . "
"; } sub BibtexFieldRow { my ($a, $b)=@_; return unless (defined($b)); my $acss = "bibtex-field"; my $bcss = "bibtex-value-" . lc($a); my $ret = "$a$b"; return $ret; } # pass this a block of $text with some well-formed bibtex entry in $text. It returns the fields and values in a hashref. sub BibtexParse { my $text = shift; # grab the bibtex header line... we check the label, save the type $text =~ s/^(.*)([\{\(]).*\n//; my $type = $1; my $outerdelimiter = $2; $text = reverse $text; if ($outerdelimiter eq "\{") { $text =~ s/(.*?)\}//s; } elsif ($outerdelimiter eq "\(") { $text =~ s/(.*?)\)//s; } else { return "[ BibTeX error: outer delimiters must be ( ) or { } ]"; } $text = reverse $text; my $value; my $currentname; my $field; my $innerdelimiter; # %$field. keys are fieldnames (we translate _'s to spaces) # it points to data. foreach $line (split(/\n\s*/, $text)) { if ($line =~ /^\s*(\w+)\s*\=\s*(.*)/) { $currentname = uc($1); $value=$2; if ($value =~ /^[\"\{]/) { $value =~ s/^(.)//; $innerdelimiter->{"$currentname"}=$1; # otherwise there's no delimiter. And that's okay. # we'll clean up the ending delimiter (which we've left behind) # in one big pass at the end. } $field->{"$currentname"}=$value; } else { # must be part of the last value. $field->{"$currentname"} .= "
" . $line; } } # clean up all the remaining delimiters. foreach $name (keys %$field) { my $tobestripped = reverse $field->{"$name"}; my $delimiter = $innerdelimiter->{"$name"}; $delimiter = "\}" if ($delimiter eq "\{"); $tobestripped =~ s/(.*?)$delimiter//; my $cleanvalue = reverse $tobestripped; $field->{"$name"} = FilterBibtex($cleanvalue); } $field->{"TYPE"}=$type; return ($field); } sub FilterBibtex { my $text = shift; $text =~ s/\\\w+(\{(.*?)\})?//g; $text =~ s/[\t ]+\n/\n/g; $text =~ s/\n\n\n+/\n\n/g; $text =~ s/^\s*\//s; return $text; } # requires mysearchresults push @MyPrintSearchResults, \&BibtexSearchResult; sub BibtexSearchResult { my ($name, $regex, $text, $type) = @_; my $bibtextext; if ($text =~ /]*>\s*(\@.*?)\<\/pre>/s) { $bibtextext=$1; } else { return 0; } my $title = GetPageLink($name, undef, undef); my @fieldorder = qw(PAGETITLE TITLE AUTHOR JOURNAL PUBLISHER BOOKTITLE ABSTRACT ANNOTE); my @fieldbanned = (); my $table = MakeBibtex($bibtextext, \@fieldorder, \@fieldbanned); my $row = BibtexFieldRow("PAGETITLE", $title); $table =~ s/ tags with attributes (
).

(0.3a) Fixed browsetrim regexp to parse HTML without  or  or other non-pre initial tags.

(0.3) For BFC4. Now matches surrounding 
 tags. We assume BibTeX entries look like 
@paper{...}
. Also correctly emits browsetrim pure bibtex. (0.2a) Will not fire if this is creating HTML to pass to an HTML editor like fckeditor in fckeditor-bfc.pl. (0.2) Fixed bug matching entries with nonleading @ Allow multiple entries in a page. (0.1) Adds a Rule to pretty-prints bibtex entries. Uses mysearchresults to pretty print search result of bibtex format.