2013-05-14 14:34:10 +00:00
|
|
|
#!/usr/bin/env lua
|
|
|
|
|
|
|
|
-- porteail.lua
|
|
|
|
|
|
|
|
--[[ Requirements ]]--
|
|
|
|
|
|
|
|
require 'lfs'
|
|
|
|
|
|
|
|
--[[ Variables ]]--
|
|
|
|
|
|
|
|
-- Mandatories files
|
2013-05-14 16:40:41 +00:00
|
|
|
local configFile = './' .. 'configrc'
|
2013-05-14 14:34:10 +00:00
|
|
|
-- Default directories values
|
|
|
|
local currentpath = os.getenv('CURDIR') or '.'
|
2013-05-14 16:40:41 +00:00
|
|
|
local default_dir_category = 'categ'
|
|
|
|
local default_dir_component = 'composants'
|
|
|
|
local default_dir_destination = 'porteail'
|
2013-05-15 12:23:37 +00:00
|
|
|
local default_dir_img_destination = 'image'
|
|
|
|
local default_dir_img_source = 'img'
|
2013-05-15 13:14:20 +00:00
|
|
|
local default_dir_css_source = 'style'
|
2013-06-05 14:37:20 +00:00
|
|
|
local default_dir_lang = 'lang'
|
2013-05-14 14:34:10 +00:00
|
|
|
-- Default files values
|
2013-05-14 16:40:41 +00:00
|
|
|
local default_img_filename = 'generique.png'
|
|
|
|
local default_index_filename = 'index.html'
|
|
|
|
local default_template_index_filename = 'index.html'
|
2013-05-15 15:16:05 +00:00
|
|
|
local default_template_categ_filename = 'categories.html'
|
|
|
|
local default_template_element_filename = 'element.html'
|
2013-05-16 09:39:01 +00:00
|
|
|
local default_css_filename = 'black.css'
|
|
|
|
local default_css_menu_without = 'without_menu.css'
|
|
|
|
local default_css_menu_with = 'with_menu.css'
|
2013-05-14 16:40:41 +00:00
|
|
|
-- Other defaults values
|
2013-06-05 14:46:27 +00:00
|
|
|
local version = os.getenv('VERSION') or '(Unknown version)'
|
2013-05-14 16:40:41 +00:00
|
|
|
local default_categ_extension = 'txt'
|
2013-05-15 10:00:45 +00:00
|
|
|
local DIR_SEP = '/'
|
2013-05-17 09:11:52 +00:00
|
|
|
local default_css_name = 'Default'
|
|
|
|
local default_title = 'My portal'
|
|
|
|
local default_homepage_title = ' - Homepage'
|
2013-06-05 14:37:20 +00:00
|
|
|
local default_language = 'en'
|
2013-05-14 14:34:10 +00:00
|
|
|
|
|
|
|
--[[ Functions ]]--
|
|
|
|
|
2013-05-15 13:39:57 +00:00
|
|
|
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
|
|
|
|
|
2013-05-14 14:34:10 +00:00
|
|
|
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
|
|
|
|
|
2013-05-14 16:40:41 +00:00
|
|
|
function replace(string, table)
|
|
|
|
return string:gsub("$(%b{})", function(string)
|
|
|
|
return table[string:sub(2,-2)]
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2013-05-14 14:34:10 +00:00
|
|
|
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
|
|
|
|
|
2013-05-15 10:00:45 +00:00
|
|
|
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
|
|
|
|
|
2013-05-15 12:23:37 +00:00
|
|
|
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)
|
2013-05-14 14:34:10 +00:00
|
|
|
-- parse given file
|
2013-05-15 09:22:55 +00:00
|
|
|
local elements = {}
|
2013-05-16 08:41:48 +00:00
|
|
|
local categ_title = ''
|
|
|
|
local categ_description = ''
|
|
|
|
local categ_count = 0
|
2013-05-16 09:24:27 +00:00
|
|
|
-- parse category file
|
2013-05-14 14:34:10 +00:00
|
|
|
for line in io.lines(filepath) do
|
2013-05-15 09:22:55 +00:00
|
|
|
local element = ''
|
2013-05-16 09:24:27 +00:00
|
|
|
-- check if this line is:
|
|
|
|
-- a comment ("# my comment"),
|
|
|
|
-- a category ("[[My category]]Its description")
|
|
|
|
-- or an element ("Title##Description##URL##Image")
|
2013-05-14 14:34:10 +00:00
|
|
|
is_comment = string.find(line, '^#+.*')
|
|
|
|
is_title = string.find(line, '%[%[(.*)%]%](.*)')
|
|
|
|
is_element = string.find(line, '(.*)##(.*)##(.*)##(.*)')
|
2013-05-16 09:24:27 +00:00
|
|
|
-- processing lines to fetch data
|
2013-05-14 14:34:10 +00:00
|
|
|
if is_comment then
|
|
|
|
-- do nothing because it's a comment
|
|
|
|
elseif is_title then
|
2013-05-16 08:41:48 +00:00
|
|
|
for t in string.gmatch(line, '%[%[(.*)%]%].*') do categ_title = categ_title .. t end
|
|
|
|
for d in string.gmatch(line, '%[%[.*%]%](.*)') do categ_description = categ_description .. d end
|
|
|
|
categ_count = categ_count + 1
|
2013-05-14 14:34:10 +00:00
|
|
|
elseif is_element then
|
|
|
|
title = ''
|
|
|
|
description = ''
|
|
|
|
url = ''
|
|
|
|
img = ''
|
|
|
|
for t in string.gmatch(line, '(.*)##.*##.*##.*') do title = title .. t end
|
2013-05-15 09:22:55 +00:00
|
|
|
for d in string.gmatch(line, '.*##.*##(.*)##.*') do description = description .. d end
|
|
|
|
for u in string.gmatch(line, '.*##(.*)##.*##.*') do url = url .. u end
|
2013-05-14 14:34:10 +00:00
|
|
|
for i in string.gmatch(line, '.*##.*##.*##(.*)') do img = img .. i end
|
2013-05-15 12:23:37 +00:00
|
|
|
img_description = " "
|
2013-05-16 09:24:27 +00:00
|
|
|
-- copy image and fetch result
|
2013-05-15 12:23:37 +00:00
|
|
|
img_url = processImage(img, img_source, img_destination, destination, default_img)
|
2013-05-16 09:24:27 +00:00
|
|
|
-- replace some chars in URL to avoid HTML5 problems
|
2013-05-15 12:23:37 +00:00
|
|
|
url = url:gsub('%&', '%&')
|
2013-05-16 09:24:27 +00:00
|
|
|
-- create element's result
|
2013-05-15 12:23:37 +00:00
|
|
|
element = replace(template_element, {ELEMENT_URL=url, ELEMENT_DESC=description, ELEMENT_TITLE=title, IMG_URL=img_url, IMG_DESC=img_description})
|
2013-05-16 09:24:27 +00:00
|
|
|
-- add it to elements table
|
2013-05-15 09:22:55 +00:00
|
|
|
table.insert(elements, element)
|
2013-05-14 14:34:10 +00:00
|
|
|
end
|
|
|
|
end
|
2013-05-16 09:24:27 +00:00
|
|
|
-- check if category is ok
|
|
|
|
error_msg = ' ' .. filepath .. ' not imported: '
|
2013-05-16 08:41:48 +00:00
|
|
|
if categ_count > 1 then
|
2013-05-16 09:24:27 +00:00
|
|
|
print (error_msg .. 'too many categories.')
|
|
|
|
return ''
|
|
|
|
elseif categ_count == 0 then
|
|
|
|
print (error_msg .. 'no category found.')
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
-- check elements
|
|
|
|
if table.getn(elements) < 1 then
|
|
|
|
print (error_msg .. 'no elements found.')
|
2013-05-16 08:41:48 +00:00
|
|
|
return ''
|
|
|
|
end
|
2013-05-16 09:24:27 +00:00
|
|
|
-- parse elements to add them to result
|
2013-05-15 09:22:55 +00:00
|
|
|
local text_elements = ''
|
|
|
|
for k, v in pairs(elements) do
|
|
|
|
text_elements = text_elements .. v
|
|
|
|
end
|
2013-05-16 09:24:27 +00:00
|
|
|
-- do substitutions on result
|
2013-05-16 08:41:48 +00:00
|
|
|
local result = replace(template_categ, {CATEG_TITLE=categ_title, CATEG_DESC=categ_description, ELEMENTS=text_elements})
|
2013-05-15 09:22:55 +00:00
|
|
|
return result
|
2013-05-14 14:34:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[ Principal ]]--
|
|
|
|
|
|
|
|
-- fetch user defined values
|
|
|
|
config = getConfig(configFile)
|
2013-05-14 16:40:41 +00:00
|
|
|
|
2013-05-14 14:34:10 +00:00
|
|
|
-- create values for directories
|
|
|
|
categ = config['CATEGORIES'] or default_dir_category
|
2013-05-15 15:16:05 +00:00
|
|
|
component = config['COMPONENTS'] or default_dir_component
|
|
|
|
destination = config['DESTINATION'] or default_dir_destination
|
|
|
|
img_destination = config['IMAGE_DESTINATION'] or default_dir_img_destination
|
2013-05-15 12:23:37 +00:00
|
|
|
img_source = config['IMAGES'] or default_dir_img_source
|
2013-05-15 13:14:20 +00:00
|
|
|
css_source = config['CSS'] or default_dir_css_source
|
2013-05-14 14:34:10 +00:00
|
|
|
-- create values for files
|
2013-05-14 16:40:41 +00:00
|
|
|
index_filename = config['INDEX'] or default_index_filename
|
|
|
|
main_template = config['TEMPLATE_INDEX'] or default_template_index_filename
|
2013-05-15 09:22:55 +00:00
|
|
|
template_categ_filename = config['TEMPLATE_CATEG'] or default_template_categ_filename
|
|
|
|
template_element_filename = config['TEMPLATE_ELEMENT'] or default_template_element_filename
|
2013-05-15 15:16:05 +00:00
|
|
|
default_img = config['DEFAULT_IMG'] or default_img_filename
|
2013-05-15 13:14:20 +00:00
|
|
|
css_filename = config['STYLE'] or default_css_filename
|
|
|
|
css_menu = default_css_menu_without
|
2013-05-15 13:26:13 +00:00
|
|
|
local menu = config['MENU'] or ''
|
2013-05-15 13:14:20 +00:00
|
|
|
if config['MENU'] then
|
|
|
|
css_menu = default_css_menu_with
|
|
|
|
end
|
2013-05-15 13:26:13 +00:00
|
|
|
local introduction = config['INTRO'] or ''
|
2013-05-14 14:34:10 +00:00
|
|
|
-- other default values
|
|
|
|
categ_extension = config['CATEGORIES_EXT'] or default_categ_extension
|
2013-05-15 13:14:20 +00:00
|
|
|
css_name = config['CSS_NAME'] or default_css_name
|
2013-05-14 16:40:41 +00:00
|
|
|
|
2013-05-15 09:22:55 +00:00
|
|
|
-- get pages
|
2013-05-15 13:39:57 +00:00
|
|
|
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')
|
2013-05-15 13:19:33 +00:00
|
|
|
-- open menu file if menu have been activated
|
|
|
|
local menu_content = ''
|
|
|
|
if menu ~= '' then
|
2013-05-15 13:39:57 +00:00
|
|
|
menu_content = readFile(component .. '/' .. menu, 'r')
|
2013-05-15 13:19:33 +00:00
|
|
|
end
|
2013-05-15 13:26:13 +00:00
|
|
|
-- open introduction file if intro have been activated
|
|
|
|
local introduction_content = ''
|
|
|
|
if introduction ~= '' then
|
2013-05-15 13:39:57 +00:00
|
|
|
introduction_content = readFile(component .. '/' .. introduction, 'r')
|
2013-05-15 13:26:13 +00:00
|
|
|
end
|
2013-05-14 16:40:41 +00:00
|
|
|
|
2013-05-15 13:39:57 +00:00
|
|
|
-- 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
|
2013-05-15 12:23:37 +00:00
|
|
|
end
|
|
|
|
|
2013-05-14 14:34:10 +00:00
|
|
|
-- Browse categ directory
|
|
|
|
local categories_files = listing (currentpath .. '/' .. categ, categ_extension)
|
2013-08-22 13:30:41 +00:00
|
|
|
-- sort table in order to show categories in the alphabetical order
|
|
|
|
table.sort(categories_files)
|
2013-05-15 09:22:55 +00:00
|
|
|
local content = ''
|
2013-05-14 14:34:10 +00:00
|
|
|
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
|
2013-05-15 12:23:37 +00:00
|
|
|
content = content .. process(v, template_categ, template_element, img_destination, destination, img_source, default_img)
|
2013-05-14 14:34:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print ("-- No category file found!")
|
|
|
|
end
|
2013-05-14 16:40:41 +00:00
|
|
|
|
|
|
|
-- Create index file in destination directory
|
2013-06-05 14:37:20 +00:00
|
|
|
index_result = assert(io.open(destination .. '/' .. main_template, 'wb'))
|
2013-05-14 16:40:41 +00:00
|
|
|
-- create substitution table
|
|
|
|
substitutions = {
|
2013-05-17 09:11:52 +00:00
|
|
|
TITLE=config['HOMEPAGE'] or (config['TITLE'] and config['TITLE'] .. default_homepage_title) or (default_title .. default_homepage_title),
|
2013-05-15 15:16:05 +00:00
|
|
|
PORTEAIL_TITLE=config['TITLE'] or default_title,
|
2013-05-15 09:22:55 +00:00
|
|
|
CONTENT=content,
|
2013-05-15 13:26:13 +00:00
|
|
|
INTRODUCTION=introduction_content,
|
2013-05-15 13:19:33 +00:00
|
|
|
MENU=menu_content,
|
2013-05-15 13:14:20 +00:00
|
|
|
CSS_COLOR=css_filename,
|
2013-05-15 13:21:13 +00:00
|
|
|
CSS_NAME=css_name,
|
2013-05-15 13:14:20 +00:00
|
|
|
DEFAULT_CSS=css_menu,
|
2013-06-05 14:46:27 +00:00
|
|
|
VERSION=version,
|
2013-05-14 16:40:41 +00:00
|
|
|
}
|
2013-06-05 14:37:20 +00:00
|
|
|
|
|
|
|
-- Get language configuration
|
|
|
|
language = config['LANG'] or default_language
|
|
|
|
languagerc = getConfig(default_dir_lang .. '/' .. language)
|
|
|
|
-- Add language translation to replacements table
|
|
|
|
for k, v in pairs(languagerc) do
|
|
|
|
substitutions[k] = v
|
|
|
|
end
|
|
|
|
|
2013-05-14 16:40:41 +00:00
|
|
|
-- replace variables in result
|
2013-05-15 09:22:55 +00:00
|
|
|
homepage = replace(index, substitutions)
|
2013-06-05 14:37:20 +00:00
|
|
|
assert(index_result:write(homepage))
|
2013-05-14 16:40:41 +00:00
|
|
|
-- close file
|
2013-06-05 14:37:20 +00:00
|
|
|
assert(index_result:close())
|
2013-05-14 16:40:41 +00:00
|
|
|
|
2013-05-15 13:14:20 +00:00
|
|
|
-- 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)
|
2013-05-15 13:39:57 +00:00
|
|
|
filecontent = readFile(filepath, 'r')
|
2013-05-15 13:14:20 +00:00
|
|
|
destfile = assert(io.open(dest, 'wb'))
|
|
|
|
assert(destfile:write(filecontent))
|
|
|
|
assert(destfile:close())
|
|
|
|
end
|
|
|
|
|
2013-05-14 16:40:41 +00:00
|
|
|
--[[ END ]]--
|