m (bib2wiki)
m (bib2wiki)
 
(7 intermediate revisions by one user not shown)
Line 5: Line 5:
 
This is, for instance, an old paper of ours: {{delvalle07a}}
 
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]]'''.
+
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 13: 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>
  
 
== 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).
 +
* [[Blog:Trifles/v0°7 of bib2wiki|v0°7]] was written on {{thisday|31|December|2023}}.
  
 
<pre>
 
<pre>
Line 35: Line 37:
 
#|_.__/|_|_.__/_____| \_/\_/ |_|_|\_\_|
 
#|_.__/|_|_.__/_____| \_/\_/ |_|_|\_\_|
 
# 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
 +
# v0°7 Sun 31 Dec 2023 - http://laussy.org/wiki/Blog:Trifles/v0%C2%B07_of_bib2wiki
 
#
 
#
 
# 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
  
 
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:
Line 61: Line 69:
 
# the map lc converts to lowercases
 
# the map lc converts to lowercases
 
my @mykeys =  map { lc } split(/,/, $keys);
 
my @mykeys =  map { lc } split(/,/, $keys);
 +
 +
push(@mykeys,$ARGV[0]);
  
 
while (my $entry = $parser->next ) {
 
while (my $entry = $parser->next ) {
Line 80: Line 90:
 
    print $bibkey . "\n";
 
    print $bibkey . "\n";
 
#     my $output = "'''[" . $url . " " . $title . "]'''. ";
 
#     my $output = "'''[" . $url . " " . $title . "]'''. ";
    my $output = "<u>[[" . $title . "]]</u>. ";
+
#     my $output = "<u>[[" . $title . "]]</u>. ";
 +
    my $output = "<u>[[" . $bibkey . "|" . $title . "]]</u>. ";
 
     
 
     
 
    my @authors = $entry->author;
 
    my @authors = $entry->author;

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