The script now actually compiles the articles. Rename italian test to lematest-it.tex
[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 # TODO check if this is indeed the initial page
67 $pg = 4
68
69 def compila(name)
70         puts "Prossimo articolo: %s a pagina %d" % [name, $pg]
71         tex_file = name.dup
72         tex_file << '.tex'
73         if File.exists?(tex_file)
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                 # TODO update index file
78         else
79                 errore "Impossibile trovare l'articolo \"%s\" (\"%s\")" % [name, tex_file]
80                 exit 2
81         end
82
83         pub_file = name.dup
84         pub_file << '.pub'
85         begin
86                 pub = File.new(pub_file, 'w')
87                 pub.puts $yvn
88                 pub.puts "\\gdef\\LM@FirstPg{#{$pg}}%"
89                 pub.close
90         rescue
91                 errore "Impossibile impostare le informazioni sull'articolo \"%s\" (\"%s\")" % [name, pub_file]
92         end
93
94         begin
95                 compile = system 'pdflatex', "-interaction=batchmode", tex_file
96         rescue
97                 errore "Impossibile compilare l'articolo \"%s\" (\"%s\")" % [name, tex_file]
98         end
99
100         log_file = name.dup
101         log_file << '.log'
102         if compile
103                 log = File.new(log_file).read
104                 if log.match(/Output written on .+ \((\d+) pages, (\d+) bytes\).\s*\r?\n?/)
105                         pagine = $1.to_i
106                         puts "Ok! %d pagine" % pagine
107                 else
108                         errore "Impossibile determinare il numero di pagine per l'articolo \"%s\" (\"%s\")" % [name, tex_file]
109                 end
110
111         else
112                 errore "Impossibile compilare l'articolo \"%s\" (\"%s\")" % [name, tex_file]
113         end
114         return pagine
115 end
116
117 cartella = ARGV[0]
118
119 if !cartella or cartella.empty?
120         uso
121         exit 1
122 end
123
124 begin
125         Dir.chdir(cartella)
126 rescue
127         errore "Impossibile accedere alla cartella \"#{cartella}\""
128 end
129
130 indice = './indice'
131
132 if not File.exists?(indice)
133         errore "La cartella \"%s\" non contiene il file di indice!" % cartella
134 end
135
136 begin
137         info = YAML.load(File.read(indice))
138 rescue
139         errore "Impossibile leggere il file di indice in \"%s\"" % cartella
140 end
141
142 if info.has_key?(:anno)
143         anno = info[:anno].to_i
144 else
145         errore "Il file di indice in \"%s\" non specifica l'anno!" % cartella
146 end
147 if info.has_key?(:volume)
148         vol = info[:volume].to_i
149         vol_rm = vol.to_s_roman
150 else
151         errore "Il file di indice in \"%s\" non specifica il volume!" % cartella
152 end
153 if info.has_key?(:numero)
154         num = info[:numero].to_i
155         num_rm = num.to_s_roman
156 else
157         errore "Il file di indice in \"%s\" non specifica il numero!" % cartella
158 end
159 if info.has_key?(:articoli)
160         articoli = info[:articoli]
161 else
162         errore "Il file di indice in \"%s\" non specifica alcun articolo!" % cartella
163 end
164
165 puts "Preparazione: Le Matematiche, volume #{vol_rm}, numero #{num_rm}, anno #{anno}"
166
167 $yvn = <<EOS
168 \\gdef\\LM@Year{#{anno}}%
169 \\gdef\\LM@Vol{#{vol_rm}}%
170 \\gdef\\LM@Num{#{num_rm}}%
171 EOS
172
173 articoli.each { |art|
174         pagine = compila art
175         $pg += pagine
176         if $pg % 2 == 1
177                 $pg += 1
178         end
179 }
180
181 puts "Fatto!"