2 |
|
|
3 |
use XML::Twig; |
use XML::Twig; |
4 |
use Getopt::Std; |
use Getopt::Std; |
5 |
|
use File::Basename; |
6 |
use strict; |
use strict; |
7 |
|
|
8 |
my %opt; |
my %opt; |
45 |
print STDERR "Input encoding: $inputenc\n" if $DEBUG; |
print STDERR "Input encoding: $inputenc\n" if $DEBUG; |
46 |
|
|
47 |
# Get the filename. |
# Get the filename. |
48 |
my $filename = shift @ARGV; |
my $input_filename = shift @ARGV; |
49 |
$filename or die "Usage: $0 filename"; |
$input_filename or die "Usage: $0 filename"; |
50 |
$filename =~ s/\.bib$//; |
|
51 |
print STDERR "File name: $filename\n" if $DEBUG; |
my ($basename, $path) = fileparse($input_filename, '\.bib'); |
52 |
|
my $input_basename = $path . $basename; |
53 |
|
my $output_filename = $basename . '.xml'; |
54 |
|
print STDERR "Input file name: $input_filename\n" if $DEBUG; |
55 |
|
print STDERR "Input file name without suffix: $input_basename\n" if $DEBUG; |
56 |
|
print STDERR "Output file name: $output_filename\n" if $DEBUG; |
57 |
|
|
58 |
# Don't bother dealing with existing files -- exit |
# Don't bother dealing with existing files -- exit |
59 |
# and let the user sort it out: |
# and let the user sort it out: |
60 |
-e "$filename.xml" and die "I won't overwrite $filename.xml."; |
-e $output_filename and die "I won't overwrite $output_filename."; |
61 |
|
|
62 |
# Create a LaTeX-style aux file to instruct BibTeX: |
# Create a LaTeX-style aux file to instruct BibTeX: |
63 |
open (AUX, ">tmp$$.aux") or die "Cannot write fake LaTeX aux file tmp$$.aux: $!."; |
open (AUX, ">tmp$$.aux") or die "Cannot write fake LaTeX aux file tmp$$.aux: $!."; |
64 |
print AUX <<"EOF;"; |
print AUX <<"EOF;"; |
65 |
\\citation{*} |
\\citation{*} |
66 |
\\bibstyle{bib2xml} |
\\bibstyle{bib2xml} |
67 |
\\bibdata{$filename} |
\\bibdata{$input_basename} |
68 |
EOF; |
EOF; |
69 |
close AUX; |
close AUX; |
70 |
|
|
446 |
close XML; |
close XML; |
447 |
|
|
448 |
# Now prettyprint it |
# Now prettyprint it |
449 |
open (XML, ">$filename.xml") or die "Cannot create XML file $filename.xml: $!."; |
open (XML, ">$output_filename") or die "Cannot create XML file $output_filename: $!."; |
450 |
my $bib = new XML::Twig(pretty_print => 'indented', |
my $bib = new XML::Twig(pretty_print => 'indented', |
451 |
output_filter => $filter); |
output_filter => $filter); |
452 |
$bib->parsefile("tmp$$.xml"); |
$bib->parsefile("tmp$$.xml"); |