From c5bb0284eeb3ece53d95d1e54df68be966f9e683 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Tue, 14 May 2013 14:48:31 +0200 Subject: [PATCH 01/12] [ADD] Example of what could be done to display a category content --- categ/display.lua | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 categ/display.lua diff --git a/categ/display.lua b/categ/display.lua new file mode 100755 index 0000000..c8359eb --- /dev/null +++ b/categ/display.lua @@ -0,0 +1,58 @@ +#!/usr/bin/env lua + +-- display.lua +-- Display content of a file and attempt to match some specific words +-- author: Olivier DOSSMANN (olivier+lua@dossmann.net) + +--[[ requirement ]]-- +require 'lfs' -- apt-get install liblua5.1-filesystem0 || luarocks install luafilesystem + +function process(filepath) + -- parse given file + for line in io.lines(filepath) do + -- check if this line is a comment ("# my comment"), a category ("[[My category]]Its description") or an element ("Title##Description##URL##Image") + is_comment = string.find(line, '^#(.*)') + is_title = string.find(line, '%[%[(.*)%]%](.*)') + is_element = string.find(line, '(.*)##(.*)##(.*)##(.*)') + if is_comment then + -- do nothing because it's a comment + elseif is_title then + title = '' + for t in string.gmatch(line, '%[%[(.*)%]%].*') do title = title .. t end + description ='' + for d in string.gmatch(line, '%[%[.*%]%](.*)') do description = description .. d end + print("TITLE: " .. '\n\t' .. title .. '\n\t' .. description) + elseif is_element then + title = '' + description = '' + url = '' + img = '' + for t in string.gmatch(line, '(.*)##.*##.*##.*') do title = title .. t end + for d in string.gmatch(line, '.*##(.*)##.*##.*') do description = description .. d end + for u in string.gmatch(line, '.*##.*##(.*)##.*') do url = url .. u end + for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end + print("ELEMENT: " .. '\n\t' .. title .. '\n\t' .. description .. '\n\t' .. url .. '\n\t' .. img) + end + end +end + +function help() + print('Usage: ' .. arg[0] .. ' FILE...') +end + +if arg and table.getn(arg) > 0 then + for i, file in pairs(arg) do + if i > 0 then + attr = lfs.attributes(file) + if attr and attr.mode == 'file' then + process(file) + else + print('INFO: ' .. file .. ' is not a file or doesn\'t exists.') + end + end + end +else + print('WARNING: No argument given.') + help() + os.exit(1) +end From 73557fb83bf3d92b56d61473364d806097175c60 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Tue, 14 May 2013 16:19:35 +0200 Subject: [PATCH 02/12] [FIX] Problem with commented lines in configuration files --- categ/display.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/categ/display.lua b/categ/display.lua index c8359eb..315b923 100755 --- a/categ/display.lua +++ b/categ/display.lua @@ -11,7 +11,7 @@ function process(filepath) -- parse given file for line in io.lines(filepath) do -- check if this line is a comment ("# my comment"), a category ("[[My category]]Its description") or an element ("Title##Description##URL##Image") - is_comment = string.find(line, '^#(.*)') + is_comment = string.find(line, '^#+.*') is_title = string.find(line, '%[%[(.*)%]%](.*)') is_element = string.find(line, '(.*)##(.*)##(.*)##(.*)') if is_comment then From b885aec6090462b9f1993b9fa8f17f996fcc8147 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Tue, 14 May 2013 16:34:10 +0200 Subject: [PATCH 03/12] [ADD] Lua script that reads the categories content --- porteail.lua | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100755 porteail.lua diff --git a/porteail.lua b/porteail.lua new file mode 100755 index 0000000..7b96536 --- /dev/null +++ b/porteail.lua @@ -0,0 +1,124 @@ +#!/usr/bin/env lua + +-- porteail.lua + +--[[ Requirements ]]-- + +require 'lfs' + +--[[ Variables ]]-- + +-- Mandatories files +configFile = './' .. 'configrc' +-- Default directories values +local currentpath = os.getenv('CURDIR') or '.' +default_dir_category = 'categ' +default_dir_component = 'composants' +-- Default files values +default_img_filename = 'generique.png' +default_header_filename = 'entete.html' +default_footer_filename = 'enqueue.html' +default_categ_extension = 'txt' + + +--[[ Functions ]]-- + +function getConfig(file) + result = {} + f = assert(io.open(file, 'r')) + while true do + line = f.read(f) + if not line then break end + local key = line:match('([^#].-)[ ]+=') + local val = line:match('=[ ]+(.*)') + local comment = string.find(line, '^#+(.*)') + if comment then + -- do nothing with comment + elseif key then + result[key] = val + end + end + assert(f:close()) + return result +end + +function listing (path, extension) + files = {} + if lfs.attributes(path) then + for file in lfs.dir(path) do + if file ~= "." and file ~= ".." then + local f = path..'/'..file + local attr = lfs.attributes (f) + filext = (string.match(file, "[^\\/]-%.?([^%.\\/]*)$")) + if attr.mode == 'file' and filext == extension then + table.insert(files, f) + end + end + end + else + files = nil + end + return files +end + +function process(filepath) + -- parse given file + for line in io.lines(filepath) do + -- check if this line is a comment ("# my comment"), a category ("[[My category]]Its description") or an element ("Title##Description##URL##Image") + is_comment = string.find(line, '^#+.*') + is_title = string.find(line, '%[%[(.*)%]%](.*)') + is_element = string.find(line, '(.*)##(.*)##(.*)##(.*)') + if is_comment then + -- do nothing because it's a comment + elseif is_title then + title = '' + for t in string.gmatch(line, '%[%[(.*)%]%].*') do title = title .. t end + description ='' + for d in string.gmatch(line, '%[%[.*%]%](.*)') do description = description .. d end + print("TITLE: " .. '\n\t' .. title .. '\n\t' .. description) + elseif is_element then + title = '' + description = '' + url = '' + img = '' + for t in string.gmatch(line, '(.*)##.*##.*##.*') do title = title .. t end + for d in string.gmatch(line, '.*##(.*)##.*##.*') do description = description .. d end + for u in string.gmatch(line, '.*##.*##(.*)##.*') do url = url .. u end + for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end + print("ELEMENT: " .. '\n\t' .. title .. '\n\t' .. description .. '\n\t' .. url .. '\n\t' .. img) + end + end +end + +--[[ Principal ]]-- + +-- fetch user defined values +config = getConfig(configFile) +-- create values for directories +categ = config['CATEGORIES'] or default_dir_category +component = config['COMPOSANTS'] or default_dir_component +-- create values for files +header_filename = config['ENTETE'] or default_header_filename +footer_filename = config['ENQUEUE'] or default_footer_filename +-- other default values +categ_extension = config['CATEGORIES_EXT'] or default_categ_extension +-- get header and footer content +header_file = assert(io.open(currentpath .. '/' .. component .. '/' .. header_filename, 'r')) +header = assert(header_file:read('*a')) +assert(header_file:close()) +footer_file = assert(io.open(currentpath .. '/' .. component .. '/' .. footer_filename, 'r')) +footer = assert(footer_file:read('*a')) +assert(footer_file:close()) +-- Browse categ directory +local categories_files = listing (currentpath .. '/' .. categ, categ_extension) +if categories_files then + for k,v in pairs(categories_files) do + -- read category content + attr = lfs.attributes(v) + if attr and attr.mode == 'file' then + process(v) + end + end +else + print ("-- No category file found!") +end From e326b7331a72d4c18530fcd534e69ca600a41f2d Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Tue, 14 May 2013 18:40:41 +0200 Subject: [PATCH 04/12] [ADD] New template for Lua script --- composants/index.html | 31 ++++++++++++++++++++++ porteail.lua | 61 +++++++++++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 composants/index.html diff --git a/composants/index.html b/composants/index.html new file mode 100644 index 0000000..8881867 --- /dev/null +++ b/composants/index.html @@ -0,0 +1,31 @@ + + + + ${TITLE} + + + + + + +
+

${PORTEAIL_TITLE}

+
+ + +
+ ${INTRODUCTION} + ${CONTENT} +
+ +${MENU} + + + +
+

Page générée à l'aide de PorteAil

+
+ + diff --git a/porteail.lua b/porteail.lua index 7b96536..cb95171 100755 --- a/porteail.lua +++ b/porteail.lua @@ -9,17 +9,18 @@ require 'lfs' --[[ Variables ]]-- -- Mandatories files -configFile = './' .. 'configrc' +local configFile = './' .. 'configrc' -- Default directories values local currentpath = os.getenv('CURDIR') or '.' -default_dir_category = 'categ' -default_dir_component = 'composants' +local default_dir_category = 'categ' +local default_dir_component = 'composants' +local default_dir_destination = 'porteail' -- Default files values -default_img_filename = 'generique.png' -default_header_filename = 'entete.html' -default_footer_filename = 'enqueue.html' -default_categ_extension = 'txt' - +local default_img_filename = 'generique.png' +local default_index_filename = 'index.html' +local default_template_index_filename = 'index.html' +-- Other defaults values +local default_categ_extension = 'txt' --[[ Functions ]]-- @@ -42,6 +43,12 @@ function getConfig(file) return result end +function replace(string, table) + return string:gsub("$(%b{})", function(string) + return table[string:sub(2,-2)] + end) +end + function listing (path, extension) files = {} if lfs.attributes(path) then @@ -94,21 +101,22 @@ end -- fetch user defined values config = getConfig(configFile) + -- create values for directories categ = config['CATEGORIES'] or default_dir_category component = config['COMPOSANTS'] or default_dir_component +destination = config['CIBLE'] or default_dir_destination -- create values for files -header_filename = config['ENTETE'] or default_header_filename -footer_filename = config['ENQUEUE'] or default_footer_filename +index_filename = config['INDEX'] or default_index_filename +main_template = config['TEMPLATE_INDEX'] or default_template_index_filename -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension --- get header and footer content -header_file = assert(io.open(currentpath .. '/' .. component .. '/' .. header_filename, 'r')) -header = assert(header_file:read('*a')) -assert(header_file:close()) -footer_file = assert(io.open(currentpath .. '/' .. component .. '/' .. footer_filename, 'r')) -footer = assert(footer_file:read('*a')) -assert(footer_file:close()) + +-- get page +index_file = assert(io.open(currentpath .. '/' .. component .. '/' .. index_filename, 'r')) +index = assert(index_file:read('*a')) +assert(index_file:close()) + -- Browse categ directory local categories_files = listing (currentpath .. '/' .. categ, categ_extension) if categories_files then @@ -122,3 +130,22 @@ if categories_files then else print ("-- No category file found!") end + +-- Check if public directory exists +if lfs.attributes(destination) == nil then + assert(lfs.mkdir(destination)) +end + +-- Create index file in destination directory +result = assert(io.open(destination .. '/' .. main_template, 'wb')) +-- create substitution table +substitutions = { + TITLE=config['TITRE'] .. ' - Accueil', + PORTEAIL_TITLE=config['TITRE'], +} +-- replace variables in result +assert(result:write(replace(index, substitutions))) +-- close file +assert(result:close()) + +--[[ END ]]-- From 370d5006afa8a1d797372f2c76f64b23f1e819c0 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 11:22:55 +0200 Subject: [PATCH 05/12] [IMP] Lua script - Get categories and elements to display them on homepage --- composants/categ.html | 8 +++++++ composants/one_element.html | 10 +++++++++ porteail.lua | 45 ++++++++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 composants/categ.html create mode 100644 composants/one_element.html diff --git a/composants/categ.html b/composants/categ.html new file mode 100644 index 0000000..703c10d --- /dev/null +++ b/composants/categ.html @@ -0,0 +1,8 @@ + + +

${CATEG_TITLE}

+

${CATEG_DESC}

+
    + ${ELEMENTS} +
+
 
diff --git a/composants/one_element.html b/composants/one_element.html new file mode 100644 index 0000000..2c31c7d --- /dev/null +++ b/composants/one_element.html @@ -0,0 +1,10 @@ +
  • + + + ${ELEMENT_TITLE}
    + + ${IMG_DESC} +
    +
    +
  • + diff --git a/porteail.lua b/porteail.lua index cb95171..2e420a2 100755 --- a/porteail.lua +++ b/porteail.lua @@ -19,6 +19,8 @@ local default_dir_destination = 'porteail' local default_img_filename = 'generique.png' local default_index_filename = 'index.html' local default_template_index_filename = 'index.html' +local default_template_categ_filename = 'categ.html' +local default_template_element_filename = 'one_element.html' -- Other defaults values local default_categ_extension = 'txt' @@ -68,9 +70,12 @@ function listing (path, extension) return files end -function process(filepath) +function process(filepath, template_categ, template_element) -- parse given file + result = template_categ + local elements = {} for line in io.lines(filepath) do + local element = '' -- check if this line is a comment ("# my comment"), a category ("[[My category]]Its description") or an element ("Title##Description##URL##Image") is_comment = string.find(line, '^#+.*') is_title = string.find(line, '%[%[(.*)%]%](.*)') @@ -82,19 +87,27 @@ function process(filepath) for t in string.gmatch(line, '%[%[(.*)%]%].*') do title = title .. t end description ='' for d in string.gmatch(line, '%[%[.*%]%](.*)') do description = description .. d end - print("TITLE: " .. '\n\t' .. title .. '\n\t' .. description) + result = replace(result, {CATEG_TITLE=title, CATEG_DESC=description}) elseif is_element then title = '' description = '' url = '' img = '' for t in string.gmatch(line, '(.*)##.*##.*##.*') do title = title .. t end - for d in string.gmatch(line, '.*##(.*)##.*##.*') do description = description .. d end - for u in string.gmatch(line, '.*##.*##(.*)##.*') do url = url .. u end + for d in string.gmatch(line, '.*##.*##(.*)##.*') do description = description .. d end + for u in string.gmatch(line, '.*##(.*)##.*##.*') do url = url .. u end for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end - print("ELEMENT: " .. '\n\t' .. title .. '\n\t' .. description .. '\n\t' .. url .. '\n\t' .. img) + -- FIXME: do img process to have url, copy it and have a description + element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_TITLE=img}) + table.insert(elements, element) end end + local text_elements = '' + for k, v in pairs(elements) do + text_elements = text_elements .. v + end + result = replace(result, {ELEMENTS=text_elements}) + return result end --[[ Principal ]]-- @@ -109,22 +122,34 @@ destination = config['CIBLE'] or default_dir_destination -- create values for files index_filename = config['INDEX'] or default_index_filename main_template = config['TEMPLATE_INDEX'] or default_template_index_filename +template_categ_filename = config['TEMPLATE_CATEG'] or default_template_categ_filename +template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_element_filename -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension --- get page +-- get pages index_file = assert(io.open(currentpath .. '/' .. component .. '/' .. index_filename, 'r')) index = assert(index_file:read('*a')) assert(index_file:close()) +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()) +-- FIXME: intro and menu +local introduction = '' +local menu = '' -- Browse categ directory local categories_files = listing (currentpath .. '/' .. categ, categ_extension) +local content = '' if categories_files then for k,v in pairs(categories_files) do -- read category content attr = lfs.attributes(v) if attr and attr.mode == 'file' then - process(v) + content = content .. process(v, template_categ, template_element) end end else @@ -142,9 +167,13 @@ result = assert(io.open(destination .. '/' .. main_template, 'wb')) substitutions = { TITLE=config['TITRE'] .. ' - Accueil', PORTEAIL_TITLE=config['TITRE'], + CONTENT=content, + INTRODUCTION=introduction, + MENU=menu, } -- replace variables in result -assert(result:write(replace(index, substitutions))) +homepage = replace(index, substitutions) +assert(result:write(homepage)) -- close file assert(result:close()) From 807f6e32381ca501c8d546f57e7a8a6227d14c45 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 12:00:45 +0200 Subject: [PATCH 06/12] [WIP] work in progress for lua script - images management --- porteail.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/porteail.lua b/porteail.lua index 2e420a2..90b538c 100755 --- a/porteail.lua +++ b/porteail.lua @@ -23,6 +23,7 @@ local default_template_categ_filename = 'categ.html' local default_template_element_filename = 'one_element.html' -- Other defaults values local default_categ_extension = 'txt' +local DIR_SEP = '/' --[[ Functions ]]-- @@ -70,6 +71,15 @@ function listing (path, extension) return files end +function basename (string, suffix) + string = string or '' + local basename = string.gsub (string, '[^'.. DIR_SEP ..']*'.. DIR_SEP ..'', '') + if suffix then + basename = string.gsub (basename, suffix, '') + end + return basename +end + function process(filepath, template_categ, template_element) -- parse given file result = template_categ @@ -98,7 +108,8 @@ function process(filepath, template_categ, template_element) for u in string.gmatch(line, '.*##(.*)##.*##.*') do url = url .. u end for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end -- FIXME: do img process to have url, copy it and have a description - element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_TITLE=img}) + img_title = basename(img) + element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_TITLE=img_title}) table.insert(elements, element) end end From f7a8e5d70e1cde3c9c51c9c5f572a597d7705aa1 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 14:23:37 +0200 Subject: [PATCH 07/12] [IMP] Lua script - copy images and create their link to be displayed in homepage --- composants/one_element.html | 2 +- porteail.lua | 57 ++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/composants/one_element.html b/composants/one_element.html index 2c31c7d..8cc2a2e 100644 --- a/composants/one_element.html +++ b/composants/one_element.html @@ -3,7 +3,7 @@ ${ELEMENT_TITLE}
    - ${IMG_DESC} + ${IMG_DESC}
    diff --git a/porteail.lua b/porteail.lua index 90b538c..914a425 100755 --- a/porteail.lua +++ b/porteail.lua @@ -15,6 +15,8 @@ local currentpath = os.getenv('CURDIR') or '.' local default_dir_category = 'categ' local default_dir_component = 'composants' local default_dir_destination = 'porteail' +local default_dir_img_destination = 'image' +local default_dir_img_source = 'img' -- Default files values local default_img_filename = 'generique.png' local default_index_filename = 'index.html' @@ -80,9 +82,30 @@ function basename (string, suffix) return basename end -function process(filepath, template_categ, template_element) +function processImage(image, source, img_destination, destination, default_img) + -- if no image given, use default one + if not image or image == nil or image == '' then + image = default_img + end + -- create image weblink + result = img_destination .. '/' .. basename(image) + -- check if this image exists + attr = lfs.attributes(destination .. '/' .. result) + if not attr then + img_path = source .. '/' .. image + f = assert(io.open(img_path, 'rb')) + img_src = assert(f:read('*a')) + assert(f:close()) + img_dest = assert(io.open(destination .. '/' .. result, 'wb')) + assert(img_dest:write(img_src)) + assert(img_dest:close()) + end + return result +end + +function process(filepath, template_categ, template_element, img_destination, destination, img_source, default_img) -- parse given file - result = template_categ + local categ_page = '' local elements = {} for line in io.lines(filepath) do local element = '' @@ -97,7 +120,7 @@ function process(filepath, template_categ, template_element) for t in string.gmatch(line, '%[%[(.*)%]%].*') do title = title .. t end description ='' for d in string.gmatch(line, '%[%[.*%]%](.*)') do description = description .. d end - result = replace(result, {CATEG_TITLE=title, CATEG_DESC=description}) + categ_page = replace(template_categ, {CATEG_TITLE=title, CATEG_DESC=description}) elseif is_element then title = '' description = '' @@ -107,9 +130,10 @@ function process(filepath, template_categ, template_element) for d in string.gmatch(line, '.*##.*##(.*)##.*') do description = description .. d end for u in string.gmatch(line, '.*##(.*)##.*##.*') do url = url .. u end for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end - -- FIXME: do img process to have url, copy it and have a description - img_title = basename(img) - element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_TITLE=img_title}) + img_description = " " + img_url = processImage(img, img_source, img_destination, destination, default_img) + url = url:gsub('%&', '%&') + element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_URL=img_url, IMG_DESC=img_description}) table.insert(elements, element) end end @@ -117,7 +141,7 @@ function process(filepath, template_categ, template_element) for k, v in pairs(elements) do text_elements = text_elements .. v end - result = replace(result, {ELEMENTS=text_elements}) + local result = replace(categ_page, {ELEMENTS=text_elements}) return result end @@ -130,11 +154,14 @@ config = getConfig(configFile) categ = config['CATEGORIES'] or default_dir_category component = config['COMPOSANTS'] or default_dir_component destination = config['CIBLE'] or default_dir_destination +img_destination = config['CIBLE_IMAGE'] or default_dir_img_destination +img_source = config['IMAGES'] or default_dir_img_source -- create values for files index_filename = config['INDEX'] or default_index_filename main_template = config['TEMPLATE_INDEX'] or default_template_index_filename template_categ_filename = config['TEMPLATE_CATEG'] or default_template_categ_filename template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_element_filename +default_img = config['DEFAUT_IMG'] or default_img_filename -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension @@ -152,6 +179,15 @@ assert(template_element_file:close()) local introduction = '' local menu = '' +-- Check if public directory exists +if lfs.attributes(destination) == nil then + assert(lfs.mkdir(destination)) +end +-- Check if image directory exists +if lfs.attributes(destination .. '/' .. img_destination) == nil then + assert(lfs.mkdir(destination .. '/' .. img_destination)) +end + -- Browse categ directory local categories_files = listing (currentpath .. '/' .. categ, categ_extension) local content = '' @@ -160,18 +196,13 @@ if categories_files then -- read category content attr = lfs.attributes(v) if attr and attr.mode == 'file' then - content = content .. process(v, template_categ, template_element) + content = content .. process(v, template_categ, template_element, img_destination, destination, img_source, default_img) end end else print ("-- No category file found!") end --- Check if public directory exists -if lfs.attributes(destination) == nil then - assert(lfs.mkdir(destination)) -end - -- Create index file in destination directory result = assert(io.open(destination .. '/' .. main_template, 'wb')) -- create substitution table From ee959794e239a46b033993f195beffa08d338bee Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 15:14:20 +0200 Subject: [PATCH 08/12] [IMP] Lua script - Copy all needed file for homepage (javascript file, CSS files, etc.) --- porteail.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/porteail.lua b/porteail.lua index 914a425..93188d6 100755 --- a/porteail.lua +++ b/porteail.lua @@ -17,15 +17,20 @@ local default_dir_component = 'composants' local default_dir_destination = 'porteail' local default_dir_img_destination = 'image' local default_dir_img_source = 'img' +local default_dir_css_source = 'style' -- Default files values local default_img_filename = 'generique.png' local default_index_filename = 'index.html' local default_template_index_filename = 'index.html' local default_template_categ_filename = 'categ.html' local default_template_element_filename = 'one_element.html' +local default_css_filename = 'noir.css' +local default_css_menu_without = 'sans_menu.css' +local default_css_menu_with = 'avec_menu.css' -- Other defaults values local default_categ_extension = 'txt' local DIR_SEP = '/' +local default_css_name = 'Défaut' --[[ Functions ]]-- @@ -156,14 +161,21 @@ component = config['COMPOSANTS'] or default_dir_component destination = config['CIBLE'] or default_dir_destination img_destination = config['CIBLE_IMAGE'] or default_dir_img_destination img_source = config['IMAGES'] or default_dir_img_source +css_source = config['CSS'] or default_dir_css_source -- create values for files index_filename = config['INDEX'] or default_index_filename main_template = config['TEMPLATE_INDEX'] or default_template_index_filename template_categ_filename = config['TEMPLATE_CATEG'] or default_template_categ_filename template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_element_filename default_img = config['DEFAUT_IMG'] or default_img_filename +css_filename = config['STYLE'] or default_css_filename +css_menu = default_css_menu_without +if config['MENU'] then + css_menu = default_css_menu_with +end -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension +css_name = config['CSS_NAME'] or default_css_name -- get pages index_file = assert(io.open(currentpath .. '/' .. component .. '/' .. index_filename, 'r')) @@ -212,6 +224,8 @@ substitutions = { CONTENT=content, INTRODUCTION=introduction, MENU=menu, + CSS_COLOR=css_filename, + DEFAULT_CSS=css_menu, } -- replace variables in result homepage = replace(index, substitutions) @@ -219,4 +233,25 @@ assert(result:write(homepage)) -- close file assert(result:close()) +-- Copy miscellaneous files to destination +to_be_copied = { + component .. '/' .. 'html5.js', + css_source .. '/' .. css_filename, + css_source .. '/' .. css_menu, +} +for i, filepath in pairs(to_be_copied) do + fileattr = lfs.attributes(filepath) + if not fileattr or fileattr.mode ~= 'file' then + print (filepath .. " doesn't exist or is not a file!") + os.exit(1) + end + dest = destination .. '/' .. basename(filepath) + file = assert(io.open(filepath, 'r')) + filecontent = assert(file:read('*a')) + assert(file:close()) + destfile = assert(io.open(dest, 'wb')) + assert(destfile:write(filecontent)) + assert(destfile:close()) +end + --[[ END ]]-- From 6ddd319745911d3a05c7cb86579bc44d0fe1efba Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 15:19:33 +0200 Subject: [PATCH 09/12] [IMP] Lua script - manage menu inclusion if filled in configrc file --- porteail.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/porteail.lua b/porteail.lua index 93188d6..579a1ca 100755 --- a/porteail.lua +++ b/porteail.lua @@ -170,8 +170,10 @@ template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_eleme default_img = config['DEFAUT_IMG'] or default_img_filename css_filename = config['STYLE'] or default_css_filename css_menu = default_css_menu_without +local menu = '' if config['MENU'] then css_menu = default_css_menu_with + menu = config['MENU'] end -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension @@ -187,9 +189,15 @@ 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()) --- FIXME: intro and menu +-- open menu file if menu have been activated +local menu_content = '' +if menu ~= '' then + menu_file = assert(io.open(component .. '/' .. menu, 'r')) + menu_content = assert(menu_file:read('*a')) + assert(menu_file:close()) +end +-- FIXME: intro local introduction = '' -local menu = '' -- Check if public directory exists if lfs.attributes(destination) == nil then @@ -223,7 +231,7 @@ substitutions = { PORTEAIL_TITLE=config['TITRE'], CONTENT=content, INTRODUCTION=introduction, - MENU=menu, + MENU=menu_content, CSS_COLOR=css_filename, DEFAULT_CSS=css_menu, } From 6c0be028cefd657fd87879db9dc55d91a892d378 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 15:21:13 +0200 Subject: [PATCH 10/12] [IMP] Lua script - add css name in result --- porteail.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/porteail.lua b/porteail.lua index 579a1ca..c298a60 100755 --- a/porteail.lua +++ b/porteail.lua @@ -233,6 +233,7 @@ substitutions = { INTRODUCTION=introduction, MENU=menu_content, CSS_COLOR=css_filename, + CSS_NAME=css_name, DEFAULT_CSS=css_menu, } -- replace variables in result From 234cdf618cca3c289d75b553f01ca4fba6bb992b Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 15:26:13 +0200 Subject: [PATCH 11/12] [IMP] Lua script - Add introduction to homepage if configured --- porteail.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/porteail.lua b/porteail.lua index c298a60..18271e1 100755 --- a/porteail.lua +++ b/porteail.lua @@ -170,11 +170,11 @@ template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_eleme default_img = config['DEFAUT_IMG'] or default_img_filename css_filename = config['STYLE'] or default_css_filename css_menu = default_css_menu_without -local menu = '' +local menu = config['MENU'] or '' if config['MENU'] then css_menu = default_css_menu_with - menu = config['MENU'] end +local introduction = config['INTRO'] or '' -- other default values categ_extension = config['CATEGORIES_EXT'] or default_categ_extension css_name = config['CSS_NAME'] or default_css_name @@ -196,8 +196,13 @@ if menu ~= '' then menu_content = assert(menu_file:read('*a')) assert(menu_file:close()) end --- FIXME: intro -local introduction = '' +-- open introduction file if intro have been activated +local introduction_content = '' +if introduction ~= '' then + intro_file = assert(io.open(component .. '/' .. introduction, 'r')) + introduction_content = assert(intro_file:read('*a')) + assert(intro_file:close()) +end -- Check if public directory exists if lfs.attributes(destination) == nil then @@ -230,7 +235,7 @@ substitutions = { TITLE=config['TITRE'] .. ' - Accueil', PORTEAIL_TITLE=config['TITRE'], CONTENT=content, - INTRODUCTION=introduction, + INTRODUCTION=introduction_content, MENU=menu_content, CSS_COLOR=css_filename, CSS_NAME=css_name, From af15c636bbce47b44ea74e21f8dcb504a97f5720 Mon Sep 17 00:00:00 2001 From: Olivier DOSSMANN Date: Wed, 15 May 2013 15:39:57 +0200 Subject: [PATCH 12/12] [IMP] Lua script - code refactoring --- porteail.lua | 54 ++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/porteail.lua b/porteail.lua index 18271e1..90fb425 100755 --- a/porteail.lua +++ b/porteail.lua @@ -34,6 +34,24 @@ local default_css_name = 'Défaut' --[[ 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) result = {} 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 -- get pages -index_file = assert(io.open(currentpath .. '/' .. component .. '/' .. index_filename, 'r')) -index = assert(index_file:read('*a')) -assert(index_file:close()) -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()) +index = readFile(currentpath .. '/' .. component .. '/' .. index_filename, 'r') +template_categ = readFile(currentpath .. '/' .. component .. '/' .. template_categ_filename, 'r') +template_element = readFile(currentpath .. '/' .. component .. '/' .. template_element_filename, 'r') -- open menu file if menu have been activated local menu_content = '' if menu ~= '' then - menu_file = assert(io.open(component .. '/' .. menu, 'r')) - menu_content = assert(menu_file:read('*a')) - assert(menu_file:close()) + menu_content = readFile(component .. '/' .. menu, 'r') end -- open introduction file if intro have been activated local introduction_content = '' if introduction ~= '' then - intro_file = assert(io.open(component .. '/' .. introduction, 'r')) - introduction_content = assert(intro_file:read('*a')) - assert(intro_file:close()) + introduction_content = readFile(component .. '/' .. introduction, 'r') end --- Check if public directory exists -if lfs.attributes(destination) == nil then - assert(lfs.mkdir(destination)) -end --- Check if image directory exists -if lfs.attributes(destination .. '/' .. img_destination) == nil then - assert(lfs.mkdir(destination .. '/' .. img_destination)) +-- Check if public and image directory exists +for i, dir in pairs({destination, destination .. '/' .. img_destination}) do + if lfs.attributes(dir) == nil then + assert(lfs.mkdir(dir)) + end end -- Browse categ directory @@ -260,9 +266,7 @@ for i, filepath in pairs(to_be_copied) do os.exit(1) end dest = destination .. '/' .. basename(filepath) - file = assert(io.open(filepath, 'r')) - filecontent = assert(file:read('*a')) - assert(file:close()) + filecontent = readFile(filepath, 'r') destfile = assert(io.open(dest, 'wb')) assert(destfile:write(filecontent)) assert(destfile:close())