m (bib2wiki)
m (Script)
Line 25: Line 25:
 
== Script ==
 
== Script ==
  
All versions till v0°5 were written on {{thisday|16|July|2023}}.
+
* All versions till v0°5 were written on {{thisday|16|July|2023}}.
 +
* v0°6 was written on {{thisday|22|October|2023}} to interface with [[keys2wiki]] (clearing the screen).
  
 
<pre>
 
<pre>
Line 35: Line 36:
 
#|_.__/|_|_.__/_____| \_/\_/ |_|_|\_\_|
 
#|_.__/|_|_.__/_____| \_/\_/ |_|_|\_\_|
 
# F.P. Laussy - fabrice.laussy@gmailcom
 
# F.P. Laussy - fabrice.laussy@gmailcom
# v0°5 Sun 16 Jul CEST 2023
+
# v0°5 Sun 16 Jul 2023
 +
# v0°6 Sun 22 Oct 2023
 
#
 
#
 
# Usage:  
 
# Usage:  
Line 49: Line 51:
 
use BibTeX::Parser;
 
use BibTeX::Parser;
 
use IO::File;
 
use IO::File;
 +
 +
# clear screen
 +
import os;
 +
os.system('clear');
  
 
# This is to remove warnings from the bibTeX parser:
 
# This is to remove warnings from the bibTeX parser:

Revision as of 14:55, 22 October 2023

bib2wiki

bib2wiki is a perl script to generate a template to cite scientific references on this very website from its $\mathrm{B{\scriptstyle{IB}}\TeX}$ entry.

This is, for instance, an old paper of ours: Electrostatic control of quantum dot entanglement induced by coupling to external reservoirs. E. del Valle, F. P. Laussy and C. Tejedor in Europhys. Lett. 80:57001 (2007). Pdf-48px.png

The rationale is explained in this blog post and examples of its use are in this other blog post.

It is based on BibTeX::Parser.

Usage

# Usage: 
# To print everything:
#   bib2wiki -keys=all
#
# To print one key:
#   bib2wiki -keys=laussy04a
#
# To print several keys:
#   bib2wiki -keys=laussy04a,laussy05a,laussy06a

Script

#! /usr/bin/perl -X -s 
# _     _ _    ____           _ _    _ 
#| |__ (_) |__|___ \__      _(_) | _(_)
#| '_ \| | '_ \ __) \ \ /\ / / | |/ / |
#| |_) | | |_) / __/ \ V  V /| |   <| |
#|_.__/|_|_.__/_____| \_/\_/ |_|_|\_\_|
# F.P. Laussy - fabrice.laussy@gmailcom
# v0°5 Sun 16 Jul 2023
# v0°6 Sun 22 Oct 2023
#
# Usage: 
# To print everything:
#   bib2wiki -keys=all
#
# To print one key:
#   bib2wiki -keys=laussy04a
#
# To print several keys:
#   bib2wiki -keys=laussy04a,laussy05a,laussy06a

use BibTeX::Parser;
use IO::File;

# clear screen
import os;
os.system('clear');

# This is to remove warnings from the bibTeX parser:
use File::Spec;
open STDERR, '>', File::Spec->devnull();

# default file
my $fh  = IO::File->new("/home/laussy/Dropbox/Fabrice/bib/sci.bib");

my $parser = BibTeX::Parser->new($fh);

# the map lc converts to lowercases
my @mykeys =  map { lc } split(/,/, $keys);

while (my $entry = $parser->next ) {
    if ($entry->parse_ok) {
	my $bibkey  = $entry->key;
	
	if (($keys eq "all") or ($bibkey ~~ @mykeys)) {
	    my $type    = $entry->type;
	    my $title   = $entry->field("title");
	    my $url   = $entry->field("url");
	    $url =~ s/doi:/http:\/\/dx.doi.org\//;
	    
	    my $journal   = $entry->field("journal");
	    my $volume   = $entry->field("volume");
	    my $pages   = $entry->field("pages");
	    my $year   = $entry->field("year");
	    
#	    print $type . " -- " . $bibkey . "\n";
	    print $bibkey . "\n";
#	    my $output = "'''[" . $url . " " . $title . "]'''. ";
	    my $output = "<u>[[" . $title . "]]</u>. ";
	    
	    my @authors = $entry->author;
	    # or:
	    # my @editors = $entry->editor;
	    
	    # keep all but last authors
	    my @notlast = @authors;
	    pop @notlast;

	    foreach my $author (@notlast) {
		my $initials = $author->first;
		$initials =~ s/~/ /g;
		
		$output .= "[[" . $initials . " "
		    . $author->von . " "
		    . $author->last . "]], "
		    ##                            . $author->jr;
	    }

	    # print last author
	    my $initials = $authors[-1]->first;
	    $initials =~ s/~//g;
	    if (@notlast) { $output .= "and " };
	    $output .= "[[" . $initials . " " . $authors[-1]->von . " " . $authors[-1]->last . "]] in ";
	    
#	    my $output = "'''[" . $url . " " . $title . "]'''. ";
	    $output .= "[[" . $journal . "]] [" . $url . " '''" . $volume ."''':" . $pages . "] ([[" . $year . "]]).\n\n";

	    $output =~ s/, and/ and/g;
	    $output =~ s/ +/ /g;
	    $output =~ s/--/-/g;
	    $output =~ s/{//g;
	    $output =~ s/}//g;
	    $output =~ s/\\'a/á/g;
	    $output =~ s/\\'e/é/g;
	    $output =~ s/\\`e/è/g;
	    $output =~ s/\\'o/ó/g;
	    $output =~ s/\\"a/ä/g;
	    $output =~ s/\\"o/ö/g;
	    $output =~ s/\\"u/ü/g;
	    $output =~ s/\\~n/ñ/g;
	    $output =~ s/\\'E/É/g;

	    # my macros
	    $output =~ s/\\Imamoglu/İmamoğlu/g; 
	    $output =~ s/\\Vuckovic/Vučković/g;
	    print $output;
	}
    } else {
	warn "Error parsing file: " . $entry->error;
    }
}