]> git.immae.eu Git - perso/Immae/Projets/Scripts/Public.git/blame - parse_bibtex_html
Update save-gif script
[perso/Immae/Projets/Scripts/Public.git] / parse_bibtex_html
CommitLineData
5172ecf8
IB
1#!/usr/bin/env perl
2# The MIT License (MIT)
3#
4# Copyright (c) 2011-2015 Ismaël Bouya http://www.normalesup.org/~bouya/
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23
24
2d8938f6
IB
25# Feel like tipping/donating? https://www.immae.eu/licenses_and_tipping
26
27
5172ecf8
IB
28use BibTeX::Parser;
29use IO::File;
30use utf8;
31use strict;
32use open ':utf8';
33
34sub parse_config_file {
35 my ($config_line, $Name, $Value, $Config);
36 (my $File, $Config) = @_;
37 if (!open (CONFIG, "$File")) {
38 print "ERROR: Config file not found : $File";
39 exit(0);
40 }
41 my $multiline = 0;
42
43 while (<CONFIG>) {
44 $config_line=$_;
45 chop ($config_line);
46 $config_line =~ s/^\s*//;
47 $config_line =~ s/\s*$//;
48 if ( ($config_line !~ /^#/) && ($config_line ne "") ){
49 if ($multiline) {
50 $$Config{$Name} =~ s/\\$//;
51 $$Config{$Name} .= $config_line;
52 } else {
53 ($Name, $Value) = split (/\s*=\s*/, $config_line);
54 $Value =~ s/^~/$ENV{"HOME"}/;
55 $$Config{$Name} = $Value;
56 }
57 $multiline = ($$Config{$Name} =~ /\\$/);
58 }
59 }
60 close(CONFIG);
61}
62
63my %Config;
64&parse_config_file ($ENV{"HOME"}."/.parse_bibtex_html.rc", \%Config);
65
66my $biblio = $Config{"biblio"};
67my $entete = $Config{"entete"};
68my $avant = $Config{"avant"};
69my $milieu = $Config{"milieu"};
70my $apres = $Config{"apres"};
71my $html = $Config{"html"};
72
73my $dossier = $Config{"dossier"};
74my $dossierweb = $Config{"dossier_web"};
75
76# http://webdesign.about.com/library/bl_htmlcodes.htm
77sub echap {
78 my $t = shift or return;
79 $t =~ s/&/&amp;/g;
80 $t =~ s/</&lt;/g;
81 $t =~ s/>/&gt;/g;
82
83 $t =~ s/--/&mdash;/g;
84
85 $t =~ s/{?\\'a}?/&aacute;/g;
86 $t =~ s/{?\\`a}?/&agrave;/g;
87 $t =~ s/{?\\"a}?/&auml;/g;
88
89 $t =~ s/{?\\r A}?/&Aring;/g;
90
91 $t =~ s/{?\\'e}?/&eacute;/g;
92 $t =~ s/{?\\`e}?/&egrave;/g;
93 $t =~ s/{?\\'E}?/&Eacute;/g;
94 $t =~ s/{?\\"e}?/&euml;/g;
95
96 $t =~ s/{?\\\^i}?/&icirc;/g;
97 $t =~ s/{?\\"i}?/&iuml;/g;
98
99 $t =~ s/{?\\"o}?/&ouml;/g;
100 $t =~ s/{?\\"o}?/&ouml;/g;
101 $t =~ s/{?\\=o}?/&#333;/g;
102 $t =~ s/{?\\o}?/&oslash;/g;
103
104 $t =~ s/{?\\"u}?/&uuml;/g;
105 $t =~ s/{?\\'u}?/&uacute;/g;
106
107 $t =~ s/{?\\~n}?/&ntilde;/g;
108
719d2758
IB
109 $t =~ s/{?\\c\{?c\}?}? ?/&ccedil;/g;
110 $t =~ s/{?\\'\{?c\}?}? ?/&#263;/g;
5172ecf8 111
719d2758 112 $t =~ s/{?\\v\{? ?s\}?}? ?/&#353;/g;
5172ecf8
IB
113
114 $t =~ s/{?\^({[^}]+}|.)}?/<sup>$1<\/sup>/g;
115 $t =~ s/{(.*)}/$1/g;
116 return $t;
117}
118
119open F, ">".$milieu;
120opendir(DIR, $dossier);
121my @FILES = readdir(DIR);
122my $fh = IO::File->new($biblio);
123my $parser = BibTeX::Parser->new($fh);
124print F "\t<ul>\n";
125my %liste = ();
126
127while (my $entry = $parser->next ) {
128 if ($entry->parse_ok) {
129 my $type = $entry->type;
130 my $title = $entry->field("title");
131 my $key = $entry->key;
132 my @authors = $entry->author;
133# my @editors = $entry->editor;
134 my $auth = "";
135 my @authors_sort = ();
136 foreach my $author (@authors) {
137 $auth .= (($author->first)?$author->first. " ":"") .(($author->von)?$author->von." ":"") . (($author->last)?$author->last:"") . ", ";
138 push(@authors_sort,$author->last);
139 }
140 @authors_sort = sort {lc $a cmp lc $b} @authors_sort;
141 my $cle_sort = shift(@authors_sort);
142 $auth = substr $auth, 0 , -2;
143 my $suffix = '(\.|_)';
144 my @match = grep(/^$key$suffix/,@FILES);
145 my $i = 1;
146 my $chaine = "\t\t<li>";
147 $auth = echap $auth;
148 $title = echap $title;
149 if($auth =~ m/\\/ || $title =~ m/\\/) {
150 warn "Unparsed item : $auth, $title";
151 }
152 $chaine .= "<span class='biblio_titre'>".$title."</span><br />".$auth."<br />\n";
153 @match = sort {lc $a cmp lc $b} @match;
154 foreach my $item (@match) {
155 $chaine .= "\t\t\t<a href='".$dossierweb.$item."'>fichier ".$i++."</a> \n";
156 }
157 $chaine .="\t\t\t<a id='".$key."' href='#".$key."' class='bibtex'>BibTeX</a>\n";
158 my $raw = $entry->raw_bibtex;
159 $raw =~ s/&/&amp;/g;
160 $chaine .= "\t\t\t<pre class='bibtex'>".$raw."</pre>\n";
161 $chaine .= "\t\t\t</li>\n";
162 $liste{$cle_sort." ".$key} = $chaine;
163 } else {
164 warn "Error parsing file: " . $entry->error;
165 }
166 }
167
168foreach my $key (sort keys %liste) {
169 print F $liste{$key};
170}
171
172print F "\t\t</ul>";
173close F;
174
175exec "cat $entete $avant $milieu $apres 1> $html" or die "$!\n";