File: get_references.pl

package info (click to toggle)
libxc 5.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196,988 kB
  • sloc: ansic: 31,506; f90: 3,369; perl: 1,392; python: 966; makefile: 425; sh: 318
file content (186 lines) | stat: -rwxr-xr-x 4,584 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env perl

# Copyright (C) 2006-2007 M.A.L. Marques
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

if(@ARGV < 1) {
    print STDERR "Usage: get_references.pl BIBFILE\n";
    exit(1);
}

$bibfile   = shift;

use Cwd;

# initialize stuff
@reference = ();
%bibtex    = {};
%doi       = {};
%bibitem   = {};

# first we read the libxc.bib file and get the references
open(BIB, "<", $bibfile);
while($_=<BIB>){
  if($_ =~ /@.*\{(.*),/){
    $ref = $1;
    push(@reference, $ref);
    $bibtex{$ref} = $_;

    while(($_=<BIB>) && !($_=~/^\s*\}/)){
      $bibtex{$ref} .= $_;

      if($_ =~ /doi\s*=\s*[{\"]([^}\"]*)[}\"]*/){
        $doi{$ref} = $1;
        $doi{$ref} =~ s/http:\/\/doi.org\///;
      }
    }
    $bibtex{$ref} .= "}";
    $bibtex{$ref} =~ s/\\/\\\\/g; # protect backslashes
    $bibtex{$ref} =~ s/\"/\\\"/g; # protect quotation marks
    $bibtex{$ref} =~ s/\n/\\n/g; # protect new lines
  }
}
close(BIB);

# now we have to make the reference. For that we will run latex
my $cwd = getcwd();

$dir = "/tmp";

open(TEX, ">$dir/$$.tex");
print TEX "\\documentclass[aps,prl]{revtex4-1}
\\usepackage[utf8]{inputenc}
\\begin{document}
\\nocite{*}
\\bibliography{$cwd/../libxc}
\\end{document}
";
close(TEX);

# run latex and bibtex
system "cd $dir && latex $$.tex && bibtex $$.aux && latex $$.tex && latex $$.tex && /bin/mv -f $$.dvi libxc.dvi";

# now we parse the bbl file
open(BBL, "<$dir/$$.bbl");
$item = "";
while($_=<BBL>){
  if($item ne ""){
    if($_ =~ /^\s\s/){
      $_ =~ s/\{NoStop\}%//;
      $item .= $_;
    }else{
      # we now clean and parse the bibitem
      $item =~ s/\n//gm;
      $item =~ s/%//gm;

      # remove accents
      $item =~ s/\\o({})?/o/g;
      $item =~ s/\\l({})?/l/g;
      $item =~ s/\\["'`~ov]{(.?)}/$1/g;
      $item =~ s/\\["'`~]//g;

      $item =~ s/\{\\natexlab\{.\}(.*?)\}/$1/g;

      $item =~ s/^\\bibitem\s*\[.*\]\{(.*?)\}//;
      $label = $1;

      $item =~ s/\\BibitemOpen//;
      $item =~ s/\\BibitemShut.*//;

      $item =~ s/\\bibf*namefont\s*\{(.*?)\}/$1/g;
      $item =~ s/\\bibinfo\s*\{.*?\}\s*\{(.*?)\}/$1/g;
      $item =~ s/\\bibinfo\s*\{editor\s+\{(.*?)\}(.*?)\}/$1$2/g; # result of nested bibinfos for book with editor
      $item =~ s/\\bibfield\s*\{.*?\}\s*\{(.*?)\}/$1/g;

      $item =~ s/\\textbf\s*\{(.*?)\}/$1/g;
      $item =~ s/\\emph\s*\{(.*?)\}/$1/g;
      $item =~ s/\\enquote\s*\{(.*?)\}/$1/g;

      $item =~ s/\\href.*?\{.*?\}\s*\{(.*?)\}/$1/g;
      $item =~ s/,\\ \\Eprint.*?\{.*?\}\s*\{(http:\/\/.*?)\}\s*//g; # wipe URL that is not arxiv
      $item =~ s/\\Eprint.*?\{.*?\}\s*\{(.*?)\}\s*/$1/g; # arxiv
      $item =~ s/\\v\{(.)(.*?)\}/\\v{$1}$2/g; # special rule for haceks \v{ } in names

      $item =~ s/\\ / /g;
      $item =~ s/~/ /g;
      $item =~ s/^\s*//;
      $item =~ s/\s+/ /g;

      # double up remaining backslashes so they print and don't try to escape the next character
      $item =~ s/\\/\\\\/g;

      # check if things seem ok
      if($item =~ /\\/) {
        print STDERR "WARNING: backslashes remain.\n";
        print STDERR $item . "\n";
      }
      if($item =~ /\{/ || $item =~ /\}/) {
        print STDERR "WARNING: braces remain.\n";
        print STDERR $item . "\n";
      }

      # remove remaining braces and backslashes
      $item =~ s/{(.*?)}/$1/g;
      $item =~ s/\\//g;

      $bibitem{$label} = $item;
      $item = "";
    }
  }

  if($_ =~ /^\\bibitem/){
    $item = $_;
  }

  while($item =~ /\%/){
    $item =~ s/\%//g;
    $item .= <BBL>;
  }
};
close(BBL);

# delete garbage
system "cd $dir && /bin/rm -f $$.tex $$.aux $$.bbl $$.blg $$.log $$Notes.bib $$.pol $$.pol_ref $$.unpol $$.unpol_ref";

# now we make a nice output
open(OUT_C, ">references.c");
open(OUT_H, ">references.h");
$str = "/*
   This file is autogenerated. Please do not change it directly. Run instead
   ./get_references.pl
*/

/*
 Copyright (C) 2006-2007 M.A.L. Marques

 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0. If a copy of the MPL was not distributed with this
 file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <xc.h>

";

print OUT_C $str;
print OUT_H $str;

foreach $r (@reference){
  $r2 = $r;
  $r2 =~ s/[^a-zA-Z0-9]/_/g;

  print OUT_C "func_reference_type xc_ref_$r2 = {
  \"$bibitem{$r}\",
  \"$doi{$r}\",
  \"$bibtex{$r}\"
};
\n\n";

  print OUT_H "extern func_reference_type xc_ref_$r2;\n";
}

close(OUT_C);
close(OUT_H);