Commit initial
[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
25use BibTeX::Parser;
26use IO::File;
27use utf8;
28use strict;
29use open ':utf8';
30
31sub parse_config_file {
32 my ($config_line, $Name, $Value, $Config);
33 (my $File, $Config) = @_;
34 if (!open (CONFIG, "$File")) {
35 print "ERROR: Config file not found : $File";
36 exit(0);
37 }
38 my $multiline = 0;
39
40 while (<CONFIG>) {
41 $config_line=$_;
42 chop ($config_line);
43 $config_line =~ s/^\s*//;
44 $config_line =~ s/\s*$//;
45 if ( ($config_line !~ /^#/) && ($config_line ne "") ){
46 if ($multiline) {
47 $$Config{$Name} =~ s/\\$//;
48 $$Config{$Name} .= $config_line;
49 } else {
50 ($Name, $Value) = split (/\s*=\s*/, $config_line);
51 $Value =~ s/^~/$ENV{"HOME"}/;
52 $$Config{$Name} = $Value;
53 }
54 $multiline = ($$Config{$Name} =~ /\\$/);
55 }
56 }
57 close(CONFIG);
58}
59
60my %Config;
61&parse_config_file ($ENV{"HOME"}."/.parse_bibtex_html.rc", \%Config);
62
63my $biblio = $Config{"biblio"};
64my $entete = $Config{"entete"};
65my $avant = $Config{"avant"};
66my $milieu = $Config{"milieu"};
67my $apres = $Config{"apres"};
68my $html = $Config{"html"};
69
70my $dossier = $Config{"dossier"};
71my $dossierweb = $Config{"dossier_web"};
72
73# http://webdesign.about.com/library/bl_htmlcodes.htm
74sub echap {
75 my $t = shift or return;
76 $t =~ s/&/&amp;/g;
77 $t =~ s/</&lt;/g;
78 $t =~ s/>/&gt;/g;
79
80 $t =~ s/--/&mdash;/g;
81
82 $t =~ s/{?\\'a}?/&aacute;/g;
83 $t =~ s/{?\\`a}?/&agrave;/g;
84 $t =~ s/{?\\"a}?/&auml;/g;
85
86 $t =~ s/{?\\r A}?/&Aring;/g;
87
88 $t =~ s/{?\\'e}?/&eacute;/g;
89 $t =~ s/{?\\`e}?/&egrave;/g;
90 $t =~ s/{?\\'E}?/&Eacute;/g;
91 $t =~ s/{?\\"e}?/&euml;/g;
92
93 $t =~ s/{?\\\^i}?/&icirc;/g;
94 $t =~ s/{?\\"i}?/&iuml;/g;
95
96 $t =~ s/{?\\"o}?/&ouml;/g;
97 $t =~ s/{?\\"o}?/&ouml;/g;
98 $t =~ s/{?\\=o}?/&#333;/g;
99 $t =~ s/{?\\o}?/&oslash;/g;
100
101 $t =~ s/{?\\"u}?/&uuml;/g;
102 $t =~ s/{?\\'u}?/&uacute;/g;
103
104 $t =~ s/{?\\~n}?/&ntilde;/g;
105
106 $t =~ s/{?\\c{?c}?}? ?/&ccedil;/g;
107 $t =~ s/{?\\'{?c}?}? ?/&#263;/g;
108
109 $t =~ s/{?\\v{? ?s}?}? ?/&#353;/g;
110
111 $t =~ s/{?\^({[^}]+}|.)}?/<sup>$1<\/sup>/g;
112 $t =~ s/{(.*)}/$1/g;
113 return $t;
114}
115
116open F, ">".$milieu;
117opendir(DIR, $dossier);
118my @FILES = readdir(DIR);
119my $fh = IO::File->new($biblio);
120my $parser = BibTeX::Parser->new($fh);
121print F "\t<ul>\n";
122my %liste = ();
123
124while (my $entry = $parser->next ) {
125 if ($entry->parse_ok) {
126 my $type = $entry->type;
127 my $title = $entry->field("title");
128 my $key = $entry->key;
129 my @authors = $entry->author;
130# my @editors = $entry->editor;
131 my $auth = "";
132 my @authors_sort = ();
133 foreach my $author (@authors) {
134 $auth .= (($author->first)?$author->first. " ":"") .(($author->von)?$author->von." ":"") . (($author->last)?$author->last:"") . ", ";
135 push(@authors_sort,$author->last);
136 }
137 @authors_sort = sort {lc $a cmp lc $b} @authors_sort;
138 my $cle_sort = shift(@authors_sort);
139 $auth = substr $auth, 0 , -2;
140 my $suffix = '(\.|_)';
141 my @match = grep(/^$key$suffix/,@FILES);
142 my $i = 1;
143 my $chaine = "\t\t<li>";
144 $auth = echap $auth;
145 $title = echap $title;
146 if($auth =~ m/\\/ || $title =~ m/\\/) {
147 warn "Unparsed item : $auth, $title";
148 }
149 $chaine .= "<span class='biblio_titre'>".$title."</span><br />".$auth."<br />\n";
150 @match = sort {lc $a cmp lc $b} @match;
151 foreach my $item (@match) {
152 $chaine .= "\t\t\t<a href='".$dossierweb.$item."'>fichier ".$i++."</a> \n";
153 }
154 $chaine .="\t\t\t<a id='".$key."' href='#".$key."' class='bibtex'>BibTeX</a>\n";
155 my $raw = $entry->raw_bibtex;
156 $raw =~ s/&/&amp;/g;
157 $chaine .= "\t\t\t<pre class='bibtex'>".$raw."</pre>\n";
158 $chaine .= "\t\t\t</li>\n";
159 $liste{$cle_sort." ".$key} = $chaine;
160 } else {
161 warn "Error parsing file: " . $entry->error;
162 }
163 }
164
165foreach my $key (sort keys %liste) {
166 print F $liste{$key};
167}
168
169print F "\t\t</ul>";
170close F;
171
172exec "cat $entete $avant $milieu $apres 1> $html" or die "$!\n";