(New script for referencing on this web)
 
m (bib2wiki)
 
(9 intermediate revisions by one user not shown)
Line 1: Line 1:
 
= bib2wiki =
 
= bib2wiki =
  
'''''bib2wiki''''' is a [[perl]] script to generate a [[template]] to cite scientific refeferences on this [[laussy.org|very website]] from its [[bibTeX|$\mathrm{B{\scriptstyle{IB}}\TeX}$]] entry.
+
'''''bib2wiki''''' is a [[perl]] script to generate a [[template]] to cite scientific references on this [[laussy.org|very website]] from its [[bibTeX|$\mathrm{B{\scriptstyle{IB}}\TeX}$]] entry.
  
The rationale is explained [[Blog:Hacks/A_simple_way_to_quote_from_bibTeX_in_MediaWiki|in this blog post]].
+
This is, for instance, an old paper of ours: {{delvalle07a}}
 +
 
 +
The rationale is explained [[Blog:Hacks/A_simple_way_to_quote_from_bibTeX_in_MediaWiki|in this blog post]] and '''[[Blog:Hacks/bib2wiki_or_parsing_bibTeX_into_mediawiki_templates|examples of its use are in this other blog post]]'''. Undefined references can be fixed with '''[[keys2wiki]]'''.
  
 
It is based on [https://metacpan.org/pod/BibTeX::Parser BibTeX::Parser].
 
It is based on [https://metacpan.org/pod/BibTeX::Parser BibTeX::Parser].
Line 11: Line 13:
 
<pre>
 
<pre>
 
# Usage:  
 
# Usage:  
# To print everything:
 
#  bib2wiki -keys=all
 
#
 
 
# To print one key:
 
# To print one key:
#  bib2wiki -keys=laussy04a
+
#  bib2wiki laussy04a
 
#
 
#
 
# To print several keys:
 
# To print several keys:
 
#  bib2wiki -keys=laussy04a,laussy05a,laussy06a
 
#  bib2wiki -keys=laussy04a,laussy05a,laussy06a
 +
#
 +
# To print everything:
 +
#  bib2wiki -keys=all
 
</pre>
 
</pre>
  
== Get the keys from Mediawiki ==
+
== Script ==
  
To extract the keys needed on a page, in edit mode, look for the list of templates at the end. It could look something like this:
+
* 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).
 +
* [[Blog:Trifles/v0°7 of bib2wiki|v0°7]] was written on {{thisday|31|December|2023}}.
  
<wz tip="References not yet uploaded to the wiki.">[[File:Screenshot_20230716_164012.png|250px]]</wz>
+
<pre>
 +
#! /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
 +
# v0°7 Sun 31 Dec 2023 - http://laussy.org/wiki/Blog:Trifles/v0%C2%B07_of_bib2wiki
 +
#
 +
# Usage:
 +
# To print one key:
 +
#  bib2wiki laussy04a
 +
#
 +
# To print several keys:
 +
#  bib2wiki -keys=laussy04a,laussy05a,laussy06a
 +
#
 +
# To print everything:
 +
#  bib2wiki -keys=all
  
Copy/past in a file (say <tt>templates</tt>) on which you run:
+
use BibTeX::Parser;
 +
use IO::File;
  
<pre>
+
# clear screen
cat templates | perl -pe 's/Template:([a-zA-Z]+[0-9]{2}[a-z])\s\(edit\)\.*/\1/p' | sed '/Template/d' | awk -vORS=, '{ print $1 }' | sed 's/,$/\n/'
+
import os;
</pre>
+
#os.system('clear');
  
That will output on one line the list of templates needed:
+
# This is to remove warnings from the bibTeX parser:
 +
use File::Spec;
 +
open STDERR, '>', File::Spec->devnull();
  
<pre>
+
# default file
Kirkwood35a,Kirkwood39a,Kirkwood42a,Kirkwood50a,Lopezcarreno18b,Percus58a,Salsburg53a,Sells53a,Thiele63a,Wertheim63a,Zerniker37a
+
my $fh  = IO::File->new("/home/laussy/Dropbox/Fabrice/bib/sci.bib");
</pre>
+
  
You can then pass this to bib2wiki
+
my $parser = BibTeX::Parser->new($fh);
  
<pre>
+
# the map lc converts to lowercases
bib2wiki -keys=Kirkwood35a,Kirkwood39a,Kirkwood42a,Kirkwood50a,Lopezcarreno18b,Percus58a,Salsburg53a,Sells53a,Thiele63a,Wertheim63a,Zerniker37a
+
my @mykeys = map { lc } split(/,/, $keys);
</pre>
+
  
which will return the list you have to upload:
+
push(@mykeys,$ARGV[0]);
  
<pre>
+
while (my $entry = $parser->next ) {
kirkwood35a
+
    if ($entry->parse_ok) {
<u>[[Statistical Mechanics of Fluid Mixtures]]</u>. [[J. G. Kirkwood]] in [[J. Chem. Phys.]] [http://dx.doi.org/10.1063/1.1749657 '''3''':300] ([[1935]]).
+
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 $output = "<u>[[" . $bibkey . "|" . $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;
 +
    }
  
kirkwood39a
+
    # print last author
<u>[[Molecular Distribution in Liquids]]</u>. [[J. G. Kirkwood]] in [[J. Chem. Phys.]] [http://dx.doi.org/10.1063/1.1750344 '''7''':919] ([[1939]]).
+
    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";
  
kirkwood42a
+
    $output =~ s/, and/ and/g;
<u>[[The Radial Distribution Function in Liquids]]</u>. [[J. G. Kirkwood]] and [[E. Monroe]] in [[J. Chem. Phys.]] [http://dx.doi.org/10.1063/1.1723737 '''10''':394] ([[1942]]).
+
    $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;
  
etc.
+
    # my macros
 +
    $output =~ s/\\Imamoglu/İmamoğlu/g;
 +
    $output =~ s/\\Vuckovic/Vučković/g;
 +
    print $output;
 +
}
 +
    } else {
 +
warn "Error parsing file: " . $entry->error;
 +
    }
 +
}
 
</pre>
 
</pre>

Latest revision as of 19:18, 16 February 2024

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. Undefined references can be fixed with keys2wiki.

It is based on BibTeX::Parser.

Usage

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

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
# v0°7 Sun 31 Dec 2023 - http://laussy.org/wiki/Blog:Trifles/v0%C2%B07_of_bib2wiki
#
# Usage: 
# To print one key:
#   bib2wiki laussy04a
#
# To print several keys:
#   bib2wiki -keys=laussy04a,laussy05a,laussy06a
#
# To print everything:
#   bib2wiki -keys=all

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);

push(@mykeys,$ARGV[0]);

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 $output = "<u>[[" . $bibkey . "|" . $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;
    }
}