bst: separate names with ' - ' instead of 'and'
[lematema] / crea_numero.rb
1 #!/usr/bin/ruby
2 #
3 # Le Matematiche
4 #
5 # Crea un numero per la rivista Le Matematiche, usando le informazioni contenute in una specifica cartella
6 #
7 # Author: Giuseppe Bilotta <bilotta@dmi.unict.it>
8 #
9 # Created: 20070317
10 #
11 # Last Modified: 20070317
12 #
13 # Copyright: (C) 2007 Giuseppe Bilotta
14 #
15
16 require 'yaml'
17 require 'roman_numerals'
18
19 def uso
20         puts "#{$0} <nome cartella>"
21         puts "\tcrea un numero per la rivista Le Matematiche, usando le informazioni contenute nella cartella"
22 end
23
24 def errore(msg)
25         $stderr.puts "ERRORE: %s" % msg
26         exit 2
27 end
28
29 def avviso(msg)
30         $stderr.puts "AVVISO: %s" % msg
31 end
32
33 NOT_BRACE = /[^\{]/
34 SIMPLE_GROUP = /#{NOT_BRACE}+?|\{#{NOT_BRACE}*?\}/
35 NESTED_GROUP = /#{SIMPLE_GROUP}+?|\{#{SIMPLE_GROUP}*?\}/
36 TITLE_RX = /\\title\{(#{NESTED_GROUP})\}/m
37 AUTHOR_RX = /\\author\{(#{NESTED_GROUP})\}/m
38
39 def titolo_e_autore(name, file)
40         res = Hash.new
41         content = File.open(file).read
42
43         titoli = content.scan(TITLE_RX)
44         case titoli.length
45         when 0
46                 errore "Impossibile determinare il titolo di %s" % name
47         when 1
48                 res[:titolo] = titoli.first.first.gsub("\\\\", "\n").gsub(/\n|\r/, " ").gsub(/\s+/, " ")
49         else
50                 avviso "%s sembra avere più d'un titolo." % name
51                 res[:titolo] = titoli.first.first.gsub("\\\\", "\n").gsub(/\n|\r/, " ").gsub(/\s+/, " ")
52         end
53
54         autori = content.scan(AUTHOR_RX)
55         case autori.length
56         when 0
57                 errore "Impossibile determinare il titolo di %s" % name
58         else
59                 res[:autori] = autori.map { |ar|
60                         ar.first
61                 }
62         end
63         return res
64 end
65
66 $pg = 3
67
68 def compila(name)
69         puts "Prossimo articolo: %s a pagina %d" % [name, $pg]
70         tex_file = name.dup
71         tex_file << '.tex'
72         if File.exists?(tex_file)
73                 begin
74                         art_info =  titolo_e_autore(name, tex_file)
75                         puts "\tTitolo:\n\t\t"+art_info[:titolo]
76                         puts "\tAutori:\n\t\t"+art_info[:autori].join("\n\t\t")
77                 rescue
78                         errore "Impossibile trovare titolo o autore per l'articolo \"%s\" (\"%s\")" % [name, tex_file]
79                 end
80
81                 begin
82                         $sommario << "\\articolo{#{art_info[:autori].join(' - ')}}{#{art_info[:titolo]}}{#{$pg}}\n"
83                 rescue
84                         errore "Impossibile aggiornare il sommario con le informazioni sull'articolo \"%s\" (\"%s\")" % [name, tex_file]
85                 end
86         else
87                 errore "Impossibile trovare l'articolo \"%s\" (\"%s\")" % [name, tex_file]
88                 exit 2
89         end
90
91         pub_file = name.dup
92         pub_file << '.pub'
93         begin
94                 pub = File.new(pub_file, 'w')
95                 pub.puts $yvn
96                 pub.puts "\\gdef\\LM@FirstPg{#{$pg}}%"
97                 pub.close
98         rescue
99                 errore "Impossibile impostare le informazioni sull'articolo \"%s\" (\"%s\")" % [name, pub_file]
100         end
101
102         begin
103                 compile = system 'pdflatex', "-interaction=batchmode", tex_file
104                 raise if not compile
105         rescue
106                 errore "Errore durante la compilazione dell'articolo \"%s\" (\"%s\")" % [name, tex_file]
107         end
108
109         bib_file = name.dup
110         bib_file << '.bib'
111
112         log_file = name.dup
113         log_file << '.log'
114
115         begin
116                 if File.exists?(bib_file)
117                         system 'bibtex', name
118                 end
119
120                 compile = system 'pdflatex', "-interaction=batchmode", tex_file
121
122                 raise if not compile
123
124                 log = File.new(log_file).read
125                 if log.match(/Output written on .+ \((\d+) pages, (\d+) bytes\).\s*\r?\n?/)
126                         pagine = $1.to_i
127                         puts "Ok! %d pagine" % pagine
128                 else
129                         errore "Impossibile determinare il numero di pagine per l'articolo \"%s\" (\"%s\")" % [name, tex_file]
130                 end
131         rescue
132                 errore "Errore durante la compilazione dell'articolo \"%s\" (\"%s\"). Consultare il file di log \"%s\" per maggiori informazioni" % [name, tex_file, log_file]
133         end
134         return pagine
135 end
136
137 cartella = ARGV[0]
138
139 if !cartella or cartella.empty?
140         uso
141         exit 1
142 end
143
144 begin
145         Dir.chdir(cartella)
146 rescue
147         errore "Impossibile accedere alla cartella \"#{cartella}\""
148 end
149
150 indice = './indice'
151
152 if not File.exists?(indice)
153         errore "La cartella \"%s\" non contiene il file di indice!" % cartella
154 end
155
156 begin
157         info = YAML.load(File.read(indice))
158 rescue
159         errore "Impossibile leggere il file di indice in \"%s\"" % cartella
160 end
161
162 if info.has_key?(:anno)
163         anno = info[:anno].to_i
164 else
165         errore "Il file di indice in \"%s\" non specifica l'anno!" % cartella
166 end
167 if info.has_key?(:volume)
168         vol = info[:volume].to_i
169         vol_rm = vol.to_s_roman
170 else
171         errore "Il file di indice in \"%s\" non specifica il volume!" % cartella
172 end
173 if info.has_key?(:fasc)
174         num = info[:fasc].to_i
175         num_rm = num.to_s_roman
176 else
177         errore "Il file di indice in \"%s\" non specifica il fascicolo!" % cartella
178 end
179 if info.has_key?(:articoli)
180         articoli = info[:articoli]
181 else
182         errore "Il file di indice in \"%s\" non specifica alcun articolo!" % cartella
183 end
184
185 puts "Preparazione: Le Matematiche, volume #{vol_rm}, fascicolo #{num_rm}, anno #{anno}"
186
187 $yvn = <<EOS
188 \\gdef\\LM@Year{#{anno}}%
189 \\gdef\\LM@Vol{#{vol_rm}}%
190 \\gdef\\LM@Num{#{num_rm}}%
191 EOS
192
193 tex_sommario = 'sommario.tex'
194
195 $somm_begin = <<EOS
196 \\documentclass{lematema}
197 \\usepackage[latin1]{inputenc}
198 \\let\\maketitle\\relax
199 \\newif\\ifpag\\pagtrue
200 \\makeatletter
201 \\setbox\\@tempboxa\\hbox{pag}
202 \\newlength\\paglength
203 \\paglength\\wd\\@tempboxa
204 \\makeatother
205 \\newlength\\fullplength
206
207 \\def\\articolo#1#2#3{%
208 \\hangindent0.8cm\\hangafter1\\noindent
209 #1\ --\ \\textit{#2}\\dotfill &
210 \\parbox[b]{\\fullplength}{%
211 \\parbox[b]{\\paglength}{\\ifpag pag.\\global\\pagfalse\\else\\hfil"\\hfil\\fi}
212 \\hfil#3
213 }\\smallskip\\cr
214 }
215 \\begin{document}
216 \\pagestyle{empty}
217 \\makeatletter
218 \\renewcommand{\\@oddfoot}{%
219 \\parbox[t]{\\textwidth}{%
220 \\hrule\\medskip\\baselineskip11pt
221 \\centerline{Pubblicazione realizzata con il contributo finanziario}
222 \\centerline{dell'Universt\\`a degli Studi di Catania}
223 }}%
224 \\makeatother
225 \\centerline{\\Large S O M M A R I O}
226 \\vskip0.5cm\\relax
227 \\fullplength\\textwidth
228 \\advance\\fullplength-11cm\\relax
229 \\begin{tabular*}{\\textwidth}{p{11cm}r}
230 EOS
231
232 $somm_end = <<EOS
233 \\end{tabular*}
234 \\end{document}
235 EOS
236
237 tex_fasc = "le_matematiche_#{vol_rm}_#{num_rm}.tex"
238
239 $fasc_begin = <<EOS
240 \\documentclass{lematema}
241 \\usepackage[final]{pdfpages}
242 \\let\\maketitle\\relax
243
244 \\begin{document}
245 \\pagestyle{empty}
246 EOS
247
248 $fasc_end = <<EOS
249 \\end{document}
250 EOS
251
252 begin
253         $sommario = File.open(tex_sommario, 'w')
254         $sommario << $somm_begin
255 rescue
256         errore "Impossibile creare il sommario in \"%s\"" % cartella
257 end
258
259 begin
260         $fasc = File.open(tex_fasc, 'w')
261         $fasc << $fasc_begin
262 rescue
263         errore "Impossibile creare il fascicolo in \"%s\"" % cartella
264 end
265
266 articoli.each { |art|
267         puts "\n\n"
268         puts "=" * 80
269         puts "\n\n"
270         pagine = compila art
271         $pg += pagine
272         $fasc << "\\includepdf[fitpaper=true,pages=-]{#{art}}\n"
273         if $pg % 2 == 0
274                 $pg += 1
275                 $fasc << "\\null\\cleardoublepage\n"
276         end
277 }
278
279 puts "\n\n"
280 puts "=" * 80
281 puts "\n\n"
282
283 puts "Compilo il sommario . . ."
284
285 log_sommario = 'sommario.log'
286 begin
287         $sommario << $somm_end
288         $sommario.close
289         compile = system 'pdflatex', "-interaction=batchmode", tex_sommario
290         raise if not compile
291 rescue
292         errore "Errore durante la compilazione del sommario (\"%s\")" % tex_sommario
293 end
294
295 puts "Compilo il fascicolo . . ."
296 begin
297         $fasc << $fasc_end
298         $fasc.close
299         compile = system 'pdflatex', "-interaction=batchmode", tex_fasc
300         raise if not compile
301 rescue
302         errore "Errore durante la compilazione del fascicolo (\"%s\")" % tex_fasc
303 end
304
305 puts "Fatto!"