Debian Codegraph @

texmf-dist/source/bibtex/bibtools/bib2html

raw
1 #!/usr/bin/perl
2 #
3 # bib2html - make an html file that is equivalent to the bib files,
4 # using one of the html bib styles
5 #
6 # usage:
7 $usage = "usage: $0 {alpha|index|long|longp|long-pario|short|short-pario|cv} [-o <outfile>] file.bib...\
8 OR \
9 $0 [ {alpha|index|long|longp|long-pario|short|short-pario|cv} ] [-o <outfile>] -a file.aux";
10 # Output is to bib.html.
11 # alpha makes a bibliography like bibstyle{alpha}
12 # long makes a bibliography like bibstyle{abstract}
13 # short makes a bibliography like bibstyle{alpha}, but with citekey for tags
14 # cv lists title first, does not print key, does not sort
15 # (useful for listing your papers on your home page)
16 #
17 # David Kotz 7/94
18 # [email protected]
19
20 # updated Dan Gildea 1/2003:
21 # . now handles nested \bf \em etc correctly
22 # . added some more accents
23 # . new -a file.aux usage to use existing .aux file, useful to
24 # list only papers cited by a given latex document
25 # http://www.cs.rochester.edu/~gildea/
26
27 # updated 2/2004, thanks to Juergen Vollmer
28 # a) allowing \emph{..}, \texttt{...} and \textbf{..}
29 # b) setting the environment variable BSTINPUTS to directory from $0
30 # c) if run on existing .aux file, bibstyle is now optional on the command line.
31 # if not bib style nor specified on command line, uses the html version
32 # of bib style from original document.
33
34 use File::Basename;
35 use File::Spec::Functions;
36
37 $tmp = "/tmp/bib2html$$";
38
39 my $cmd_dir = catfile (dirname ($0),
40 dirname ((defined readlink($0)) ? readlink($0) : $0));
41
42 $ENV{BSTINPUTS} .= ":$cmd_dir";
43
44 if ( scalar(@ARGV) < 2 ) {
45 die "$usage\n";
46 }
47
48
49 ## pick up the args
50 my %Opts; # will hold the option values
51
52 if ($ARGV[0] !~ /^-/) {
53 $Opts{style} = shift @ARGV;
54 }
55 while ($ARGV[0] =~ /^-/) {
56 $tmpArg = shift @ARGV;
57 if ($tmpArg eq "-o" ) {
58 $Opts{outfile} = shift @ARGV;
59 } elsif ($tmpArg eq "-a" ) {
60 $Opts{auxfile} = shift @ARGV;
61 }
62 }
63
64 $Opts{outfile} = "bib.html" if ( !defined ($Opts{outfile}) );
65
66 $SIG{INT} = \&cleanup;
67
68 print "Creating $tmp.aux for $files\n";
69 open AUX, ">$tmp.aux";
70
71 if ($Opts{auxfile}) {
72 system "ls";
73 open(AUXIN, $Opts{auxfile}) || die "could not open $Opts{auxfile}";
74 while (<AUXIN>) {
75 # if style not specified on command line, use html version
76 # of style specified in .aux file
77 s/^\\bibstyle\{([^\}]*)\}/"\\bibstyle{html-".($Opts{style}||$1)."}"/e;
78 print AUX;
79 }
80 } else {
81 if (!defined $Opts{style}) {
82 die "bib style not defined";
83 }
84 ## get all the remaining file names
85 my $files = killSuffix ( shift @ARGV );
86 foreach $file ( @ARGV ) {
87 $files .= ','; # make sure the list is comma delimited, bibtex needs it
88 $files .= killSuffix ($file);
89 }
90
91
92 ## need double slashes here to go around slash interpretation
93 ## only one is printed
94 print AUX <<EOF;
95 \\relax
96 \\citation{*}
97 \\bibstyle{html-$Opts{style}}
98 \\bibdata{$files}
99 EOF
100 }
101 close AUX;
102
103 unlink <$tmp.{bbl,blg}>;
104
105 if (-r "html-split.bst.gz" ) {
106 system ( "gunzip html-split.bst.gz" );
107 }
108
109 print "bibtex $tmp\n";
110
111 system "bibtex $tmp";
112
113 print "Bibtex done\n\n";
114
115
116 ## -------------------------------------------------------------------
117 ## the cleanup operation
118 open BBL, "$tmp.bbl";
119 open OUT, ">$Opts{outfile}";
120
121 ## stack of formatted text types that were started and did not finish on teh same line
122 ## elements are em, tt or b or i and when a candidate for a format-closing brace is found
123 ## this stack will be popped to see which format we have to close
124 my @formatsToClose;
125
126 while ( <BBL> ) {
127
128
129 ## accent handlers
130 ## these are quite definite about their replacemnets and so should
131 ## come before the more generic operations later
132 ## all braces in the latex constructs are optional
133 ## info on the html codes can be found at http://www.w3.org/TR/REC-html40/
134 ## \\? is to handle latex \'\i which you do to put acute on i without dot
135 s/\\ \` (?: \{ )? \\? ([aeiouAEIOU]) (?: \} )?
136 /&$1grave;/gx; # grave accent
137 s/ \\ \' (?: \{ )? \\? ([aeiouAEIOU]) (?: \} )?
138 /&$1acute;/gx; # 'acute accent
139 s/ \\ \^ (?: \{ )? \\? ([aeiouAEIOU]) (?: \} )?
140 /&$1circ;/gx; # circumflex
141 s/ \\ \" (?: \{ )? \\? ([aeiouyAEIOUY]) (?: \} )?
142 /&$1uml;/gx; # "umlaut
143 s/ \\ \~ (?: \{ )? \\? ([anoANO]) (?: \} )?
144 /&$1tilde;/gx; # tilde
145 s/ \\ c (?: \{ )? ([cC]) (?: \} )?
146 /&$1cedil;/gx; # cedilla
147 s/ \\ [v\'] (?: \{ )? ([cC]) (?: \} )?
148 /$1/gx; # no html code for czech hook
149 s/ \{? \\[s\"]s \}?
150 /&szlig;/gx; # german Scharf-S
151
152 ## several weird symbols
153 s/ \\copyright
154 /&copy;/gx;
155 s/ \\pounds
156 /&pound;/gx;
157
158 ## weirder symbols
159 s/ \\ (ae|AE)
160 /&$1lig;/gx;
161 s/ \\ (o|O)
162 /&$1slash;/gx;
163 s/ \\ss
164 /&szlig;/gx;
165
166 ## greek letters, case insensitive matching, but upper case in latex and
167 ## html have the first letter of the english word capitalized
168 s/ \\ (?: var )? (alpha|beta|gamma|delta|epsilon|theta|lambda|pi|rho|sigma|omega)
169 /&$1;/gxi;
170
171 ## remove any \/ space-increasing symbols
172 s+ ([^\\]) \\ \/
173 +$1+gx;
174
175 s+ \\ \ + +gx;
176
177 ## deal with \cite stuff, change it to a link to a record the same html file
178 s+ ([^\\]) \\cite\{ (.*?) \}
179 +$1<a href="#$2">$2</a>+xg;
180
181 ## These rules are to deal with my (DFK) macros
182 s/\\ie/i.e./g;
183 s/\\eg/e.g./g;
184 s/\\etc/etc./g;
185 s+\\vs\\+<EM>vs.</EM>+g;
186 s/\\usec/usec/g;
187 s/\\mbox //g;
188 s/\\par / <P> /g;
189 s/\\par$/ <P>/g;
190
191
192 s/\\\&/\&amp;/g; # ampersand
193
194 s/-{2,3}/-/g; # multiple dashes
195
196 # a few rules are needed to compensate for BibTeXs way of splitting
197 # long words over two lines by sticking a % (TeX comment character) at
198 # the end of the line. This works when one word (usually a URL) is
199 # split over more than one line.
200 ## if we have an unescaped % at the end of the line, remove it and the newline and
201 ## join the next line on
202 ## example straight out of the camel book, pg 204. amazing
203 if ( s/ ([^\\]) \% \n$ /$1/x and $nextline = <BBL> ) {
204 $_ .= $nextline;
205 redo; # back to the top
206 }
207
208 # hyphenation characters should be removed
209 s+\\-++g;
210
211 # tildes -
212 # tilde not preceded by \ or / is a nbsp
213 # \~{} is ~ (likely in a URL)
214 # all other tildes left alone, notably /~ (URL)
215 s+ ([^\\/]) ~ +$1&nbsp;+xg; # normal standalone tilde - nbsp
216 s/ \\~ \{\} /~/xg; # \~{} to ~ - do before removing braces below
217
218 ## --------------------------------------------------------
219 ## deal with em and tt and bf text surrounded by braces
220 ## deal with \emph{..}, \texttt{...} and \textbf{..}
221
222 ## the fancy groupings around the em etc are because in html bold font is not 'bf'
223 ## but just b and italic is i not 'it' so we have to pick out only a part of those
224 ## latex tags
225 ## final |\w+ is to skip over \rm or any other commands we don't handle
226 ## also removes braces with no command
227
228 while ( /(text(b)f|text(tt)|(em)ph)?([\{\}])/ ) {
229 if (defined $1) {
230 my $cmd = "$1";
231 my $fmt = $2 || $3 || $4;
232 s/\\$cmd\{/<$fmt>/;
233 push ( @formatsToClose, $fmt )
234 } elsif ($5 eq '{') {
235 ## beginning of format
236 s/ \{ (?: \\ (?: (em)|(b)f|(tt)|(i)t|\w+ ) )? \s*
237 / ( $format = $1 or $2 or $3 or $4 ) ? "<$format>" : '' /ex ;
238 ## push the format to be closed onto the stack (may be nothing)
239 push ( @formatsToClose, $format )
240 } else {
241 ## pop format to close from stack
242 s+ \s*\}
243 + ( $format = pop (@formatsToClose) ) ? "</$format>" : '' +ex ;
244 }
245 }
246
247 ## --------------------------------------------------------
248
249 ##retrieve symbols escaped by backslashes
250 my $escapedChars = quotemeta ( '#$%&_{}' );
251 s/ ([^\\]) \\ ([$escapedChars])
252 /$1$2/gxo;
253
254 print OUT $_;
255 }
256
257
258 print "\n";
259 print "\noutput is in $Opts{outfile}\n";
260
261 cleanup();
262
263 sub killSuffix {
264 $file = shift();
265 ( $name, $path ) = fileparse ( $file, '\.[^.]*$' ); # the pattern indicates what a suffix looks like
266 return ($path . $name);
267 }
268
269 sub cleanup {
270 unlink ( glob ("$tmp.{aux,bbl,blg}") );
271 }