]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | use BibTeX::Parser; | |
26 | use IO::File; | |
27 | use utf8; | |
28 | use strict; | |
29 | use open ':utf8'; | |
30 | ||
31 | sub 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 | ||
60 | my %Config; | |
61 | &parse_config_file ($ENV{"HOME"}."/.parse_bibtex_html.rc", \%Config); | |
62 | ||
63 | my $biblio = $Config{"biblio"}; | |
64 | my $entete = $Config{"entete"}; | |
65 | my $avant = $Config{"avant"}; | |
66 | my $milieu = $Config{"milieu"}; | |
67 | my $apres = $Config{"apres"}; | |
68 | my $html = $Config{"html"}; | |
69 | ||
70 | my $dossier = $Config{"dossier"}; | |
71 | my $dossierweb = $Config{"dossier_web"}; | |
72 | ||
73 | # http://webdesign.about.com/library/bl_htmlcodes.htm | |
74 | sub echap { | |
75 | my $t = shift or return; | |
76 | $t =~ s/&/&/g; | |
77 | $t =~ s/</</g; | |
78 | $t =~ s/>/>/g; | |
79 | ||
80 | $t =~ s/--/—/g; | |
81 | ||
82 | $t =~ s/{?\\'a}?/á/g; | |
83 | $t =~ s/{?\\`a}?/à/g; | |
84 | $t =~ s/{?\\"a}?/ä/g; | |
85 | ||
86 | $t =~ s/{?\\r A}?/Å/g; | |
87 | ||
88 | $t =~ s/{?\\'e}?/é/g; | |
89 | $t =~ s/{?\\`e}?/è/g; | |
90 | $t =~ s/{?\\'E}?/É/g; | |
91 | $t =~ s/{?\\"e}?/ë/g; | |
92 | ||
93 | $t =~ s/{?\\\^i}?/î/g; | |
94 | $t =~ s/{?\\"i}?/ï/g; | |
95 | ||
96 | $t =~ s/{?\\"o}?/ö/g; | |
97 | $t =~ s/{?\\"o}?/ö/g; | |
98 | $t =~ s/{?\\=o}?/ō/g; | |
99 | $t =~ s/{?\\o}?/ø/g; | |
100 | ||
101 | $t =~ s/{?\\"u}?/ü/g; | |
102 | $t =~ s/{?\\'u}?/ú/g; | |
103 | ||
104 | $t =~ s/{?\\~n}?/ñ/g; | |
105 | ||
106 | $t =~ s/{?\\c{?c}?}? ?/ç/g; | |
107 | $t =~ s/{?\\'{?c}?}? ?/ć/g; | |
108 | ||
109 | $t =~ s/{?\\v{? ?s}?}? ?/š/g; | |
110 | ||
111 | $t =~ s/{?\^({[^}]+}|.)}?/<sup>$1<\/sup>/g; | |
112 | $t =~ s/{(.*)}/$1/g; | |
113 | return $t; | |
114 | } | |
115 | ||
116 | open F, ">".$milieu; | |
117 | opendir(DIR, $dossier); | |
118 | my @FILES = readdir(DIR); | |
119 | my $fh = IO::File->new($biblio); | |
120 | my $parser = BibTeX::Parser->new($fh); | |
121 | print F "\t<ul>\n"; | |
122 | my %liste = (); | |
123 | ||
124 | while (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/&/&/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 | ||
165 | foreach my $key (sort keys %liste) { | |
166 | print F $liste{$key}; | |
167 | } | |
168 | ||
169 | print F "\t\t</ul>"; | |
170 | close F; | |
171 | ||
172 | exec "cat $entete $avant $milieu $apres 1> $html" or die "$!\n"; |