[IMP] Lua script - code refactoring

This commit is contained in:
Olivier DOSSMANN 2013-05-15 15:39:57 +02:00
parent 234cdf618c
commit af15c636bb

View File

@ -34,6 +34,24 @@ local default_css_name = 'Défaut'
--[[ Functions ]]-- --[[ Functions ]]--
function readFile(path, mode)
result = ''
if not mode then
mode = 'r'
end
if mode ~= 'r' and mode ~= 'rb' then
print('Unknown read mode!')
os.exit(1)
end
attr = lfs.attributes(path)
if attr and attr.mode == 'file' then
f = assert(io.open(path, mode))
result = assert(f:read('*a'))
assert(f:close())
end
return result
end
function getConfig(file) function getConfig(file)
result = {} result = {}
f = assert(io.open(file, 'r')) f = assert(io.open(file, 'r'))
@ -180,37 +198,25 @@ categ_extension = config['CATEGORIES_EXT'] or default_categ_extension
css_name = config['CSS_NAME'] or default_css_name css_name = config['CSS_NAME'] or default_css_name
-- get pages -- get pages
index_file = assert(io.open(currentpath .. '/' .. component .. '/' .. index_filename, 'r')) index = readFile(currentpath .. '/' .. component .. '/' .. index_filename, 'r')
index = assert(index_file:read('*a')) template_categ = readFile(currentpath .. '/' .. component .. '/' .. template_categ_filename, 'r')
assert(index_file:close()) template_element = readFile(currentpath .. '/' .. component .. '/' .. template_element_filename, 'r')
template_categ_file = assert(io.open(currentpath .. '/' .. component .. '/' .. template_categ_filename, 'r'))
template_categ = assert(template_categ_file:read('*a'))
assert(template_categ_file:close())
template_element_file = assert(io.open(currentpath .. '/' .. component .. '/' .. template_element_filename, 'r'))
template_element = assert(template_element_file:read('*a'))
assert(template_element_file:close())
-- open menu file if menu have been activated -- open menu file if menu have been activated
local menu_content = '' local menu_content = ''
if menu ~= '' then if menu ~= '' then
menu_file = assert(io.open(component .. '/' .. menu, 'r')) menu_content = readFile(component .. '/' .. menu, 'r')
menu_content = assert(menu_file:read('*a'))
assert(menu_file:close())
end end
-- open introduction file if intro have been activated -- open introduction file if intro have been activated
local introduction_content = '' local introduction_content = ''
if introduction ~= '' then if introduction ~= '' then
intro_file = assert(io.open(component .. '/' .. introduction, 'r')) introduction_content = readFile(component .. '/' .. introduction, 'r')
introduction_content = assert(intro_file:read('*a'))
assert(intro_file:close())
end end
-- Check if public directory exists -- Check if public and image directory exists
if lfs.attributes(destination) == nil then for i, dir in pairs({destination, destination .. '/' .. img_destination}) do
assert(lfs.mkdir(destination)) if lfs.attributes(dir) == nil then
end assert(lfs.mkdir(dir))
-- Check if image directory exists end
if lfs.attributes(destination .. '/' .. img_destination) == nil then
assert(lfs.mkdir(destination .. '/' .. img_destination))
end end
-- Browse categ directory -- Browse categ directory
@ -260,9 +266,7 @@ for i, filepath in pairs(to_be_copied) do
os.exit(1) os.exit(1)
end end
dest = destination .. '/' .. basename(filepath) dest = destination .. '/' .. basename(filepath)
file = assert(io.open(filepath, 'r')) filecontent = readFile(filepath, 'r')
filecontent = assert(file:read('*a'))
assert(file:close())
destfile = assert(io.open(dest, 'wb')) destfile = assert(io.open(dest, 'wb'))
assert(destfile:write(filecontent)) assert(destfile:write(filecontent))
assert(destfile:close()) assert(destfile:close())