Commit 4e162988 authored by User expired's avatar User expired
Browse files

Text interface to ris2bib online converter added

parent e2c7c8ab
Pipeline #42798 passed with stage
in 2 minutes and 22 seconds
......@@ -57,4 +57,10 @@ Notes on the BibTeX format
- New BibTeX files may enclose contents in quotation marks, but string
concatenation with ``#`` is not supported.
Related
=======
- The ``bin/ris2bib`` is a command line interface to convert RIS files to
BibTeX format using the `ris2bib`_ online converter by Nicolas Bruot.
.. _bibtools.pdf: https://bop.uber.space/files/bibtools/bibtools.pdf
.. _ris2bib: https://www.bruot.org/ris2bib/
#!/usr/bin/perl -w
# Use online ris2bib to convert one ris (RIS) file to bib (BIBTEX) file
use strict;
use Encode;
package MyParser;
use base qw(HTML::Parser);
my $is_bib = 0;
my $bib = '';
sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if ($tagname eq 'textarea') {
if ($attr->{ name } eq 'bib') {
$is_bib = 1;
}
}
}
sub text {
if ($is_bib eq 1) {
my ($self, $text) = @_;
$bib = $text;
$is_bib = 0;
}
}
# Get ris filename from first argument.
my $num_args = $#ARGV + 1;
if ($num_args < 1) {
print "Usage: ris2bib filename.ris [files...]\n";
exit;
}
for (my $i=0; $i < $num_args; $i++) {
my $risfile = $ARGV[$i];
my $bibfile = $risfile;
$bibfile =~ s/\.ris/.bib/;
print "Convert '", $risfile, "' to '", $bibfile, "'.\n";
# Load input ris file.
my $ris = '';
open RISFILE, '<', $risfile or die "\nCannot open input file `$risfile': $!\n";
while (my $line = <RISFILE>) {
chomp $line;
$ris = $ris.$line."\n";
}
# Online conversion to bibtex.
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use LWP::Protocol::https;
my $ua = LWP::UserAgent->new;
my $req = POST "https://www.bruot.org/ris2bib/",
[ ris => $ris ];
my $result = Encode::decode('utf8', $ua->request($req)->as_string);
# Extract bibtext record from html.
$is_bib = 0;
$bib = '';
my $parser = MyParser->new;
$parser->parse( $result );
# Write bibtex result to file.
open(FH, '>', $bibfile) or die $!;
print FH Encode::encode('utf8', $bib);
close(FH);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment