-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
executable file
·286 lines (258 loc) · 9 KB
/
main.lua
File metadata and controls
executable file
·286 lines (258 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#! /usr/bin/env lua5.4
general = {}
html = {}
verbose = false
--
-- Enigma Lua Homepage Gluer - main program
-- (c) 2007 Enigma Team
-- The Enigma Lua Homepage Gluer is licensed under GPL v.2 or above,
-- please find a copy of it here:
-- http://www.gnu.org/licenses/gpl-2.0.html
-- This program and some of its input files are loosely based on
-- a predecessor by Daniel Heck.
--
-- The Enigma Lua Homepage Gluer incorporates the subprograms
-- "read_news.lua" and "read_lotm.lua" and calls "output-files.lua"
-- and "lotm_archive_data.lua" for input. All of them are under the
-- GPL v.2 as stated above.
--
-- Syntax:
--
-- Call without arguments. The main input file is
-- input/output-files.lua,
-- where all input files and the overall structure of the homepage
-- as well as the meaning of the $$...$$-macros are defined.
-- This main program only replaces the $$...$$-macros according to
-- the rules specified in output-files.lua.
--
-- Each file to be created has an own structure, holding the
-- strings infile (mostly schema.html) and outfile (name of result).
-- Infile is read and written to outfile.
-- If somewhere in infile occurs the pattern "$$mything$$" then
-- this pattern is replaced, dependend on the field mything of the
-- overall structure:
-- a string -> replace directly by this string
-- a function -> start this function
-- a table -> parse each entry as a new html-file
--
----------------------------------------------------------------------
-- Tools
----------------------------------------------------------------------
function mydebug(st)
--io.write(st)
end
function add_lang_to_filename(filename, lang)
if lang == "" then
return filename
else
return string.gsub(filename, "%.", lang..".", 1)
end
end
-- translate_month returns a translated string of the month and
-- year in the DATE_TABLE. If year is nil, no year is appended.
-- Data for translation is provided in output-files.lua.
function translate_month(lang, date_table)
if type(date_table) ~= "table" then
error("Trying to translate month, but argument is "..type(date_table).."!")
end
local month = date_table.month
if month == nil then
error("Trying to translate month, but month is NIL!")
end
local translation_table = date_translation_field["months"..lang]
if translation_table == nil then
translation_table = date_translation_field.months
end
month = translation_table[month]
local year = date_table.year
if year == nil then
return month
else
-- circumvent mod->fmod change in 5.1!
--year = math.fmod(year, 100)
if year >= 2000 then
year = year - 2000
else
year = year - 1900
end
year = string.format("%02d", year)
if lang == "_es" then
return month.." del '"..year
else
return month.." '"..year
end
end
end
function translate_month_for_news(lang, date_table)
if type(date_table) ~= "table" then
error("Trying to translate month, but argument is "..type(date_table).."!")
end
local month = date_table.month
if month == nil then
error("Trying to translate month, but month is NIL!")
end
local translation_table = date_translation_field["months_news"..lang]
if translation_table == nil then
translation_table = date_translation_field.months
end
month = translation_table[month]
local year = date_table.year
if year == nil then
return month
else
-- circumvent mod->fmod change in 5.1!
--year = math.fmod(year, 100)
if year >= 2000 then
year = year - 2000
else
year = year - 1900
end
year = string.format("%02d", year)
if lang == "_es" then
return month.." del '"..year
else
return month.." '"..year
end
end
end
-- scribble writes string s to output if and only if it wasn't
-- written there before. Hence, double infos are omitted.
scribbleblock = {}
function scribble(s)
if type(s) ~= "string" then
error("Can't scribble that, it's type is "..type(s).."!")
end
for k, t in pairs(scribbleblock) do
if t == s then
mydebug(" repeat: ["..s.."]")
return
end
end
io.write(s)
table.insert(scribbleblock, s)
end
----------------------------------------------------------------------
-- Parsing and Macro-Replacing - The Program's Recursive Heartbeat
----------------------------------------------------------------------
function parse_text(v, text, lang, context)
return string.gsub(text, "%$%$(.-)%$%$", function (s)
local process = v[s] or general[s]
local process_with_lang = v[s..lang] or general[s..lang]
local stype = type(process)
if stype == "nil" then
-- Assume the entry is short for an html-output-file
mydebug(" --> "..s.."\n")
if type(html[s]) == "table" and type(html[s].outfile) == "string" then
return add_lang_to_filename(html[s].outfile, lang)
else
scribble("ERROR during evaluation of "..context
..": Entry "..s.." not defined.\n")
return ""
end
end
if stype == "string" then
if type(process_with_lang) == "string" then
return parse_text(v, process_with_lang, lang, context)
else
if verbose then
scribble(" Missing entry "..s..lang.." for "
..context..", using fallback.\n")
else
scribble(" Missing entry "..s..lang..", using fallback.\n")
end
return parse_text(v, process, lang, context)
end
end
if stype == "function" then
return process(v,s,lang)
end
if stype == "table" then
local addstring = ""
for j,f in ipairs(process) do
-- remove slash for name- and id-tags
fn = string.gsub(f, "/", "_")
if (f ~= fn) and verbose then
scribble(" Note: changed anchors for "..f.." to "..fn..
". Please correct href's.\n")
end
if not process.noanchor then
addstring = addstring.."<a name=\""..fn.."\" id=\""..fn.."\"></a>\n"
end
addstring = addstring..parse_html(v, directory..f..suffix, lang)
end
return addstring
end
error("Error during evaluation of "..context..", "..s.." (type "..stype..").")
end)
end
function replace_quots(v, text, lang, context)
local is_left_quot = true
local result = string.gsub(text, """, function (s)
if is_left_quot then
is_left_quot = not is_left_quot
return "$$left_quot$$"
else
is_left_quot = not is_left_quot
return "$$right_quot$$"
end
end)
if not is_left_quot then
scribble(" Missing quotation mark in "..context..".\n")
end
return result
end
function parse_text_quots(v, text, lang, context)
return parse_text(v, replace_quots(v, text, lang, context), lang, context)
end
function parse_html(v, infilename0, lang)
if not infilename0 then
error("Error during creation of "..v.title..": Expected filename, got nil.")
end
-- Open infile dependend relative to lang, use English as fallback.
local infilename = add_lang_to_filename(infilename0, lang)
local infile = io.open(infilename, "r")
if infile == nil then
infile = io.open(infilename0, "r")
if infile == nil then
error("Error: Neither "..infilename.." nor "..infilename0.." exist.")
else
if verbose then
scribble ("No file "..infilename..", using fallback "..infilename0.."...\n")
end
infilename = infilename0
end
else
if verbose then
scribble ("Reading input from "..infilename.." ...\n")
end
end
-- Read and process infile
local body = infile:read("*a")
infile:close()
return parse_text_quots(v, body, lang, infilename)
end
----------------------------------------------------------------------
-- Main Routine
----------------------------------------------------------------------
-- Initialise random number generator
math.randomseed(os.time())
-- Add functions for parsing the news and LotM archive
dofile("input/news/read_news.lua")
dofile("input/lotm/read_lotm.lua")
-- Get general data and LotM archive data
dofile("input/output-files.lua")
dofile("input/lotm/lotm_archive_data.lua")
-- Autogenerate LotM-entries into the html-field (see read_lotm.lua)
generate_lotm_entries(html)
-- Now glue everything together
for lang_nr, lang in pairs(language_list) do
for k,v in pairs(html) do
if type(v.outfile) ~= "string" then
error("Error during main.lua: Outfile not properly defined.")
end
local outfilename = add_lang_to_filename(v.outfile, lang)
local outfile = io.open (outfilename, "w")
outfile:write(parse_html(v, v.infile or general.infile, lang).."\n")
outfile:close()
end
end