comparison mod_muc_log_http/muc_log_http/mod_muc_log_http.lua @ 582:92ff305e7e32

mod_muc_log_http: Reduce theme loading code.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 25 Jan 2012 06:13:39 +0500
parents a9a97df60b8c
children a634b116c694
comparison
equal deleted inserted replaced
581:a9a97df60b8c 582:92ff305e7e32
667 local room = hosts[host].modules.muc.rooms[node.."@"..host]; 667 local room = hosts[host].modules.muc.rooms[node.."@"..host];
668 return createDoc(parseDay(node.."@"..host, room._data.subject or "", day:gsub("%-", ""))); 668 return createDoc(parseDay(node.."@"..host, room._data.subject or "", day:gsub("%-", "")));
669 end 669 end
670 end 670 end
671 671
672 -- Compatibility: Lua-5.1
673 function split(str, pat)
674 local t = {} -- NOTE: use {n = 0} in Lua-5.0
675 local fpat = "(.-)" .. pat
676 local last_end = 1
677 local s, e, cap = str:find(fpat, 1)
678 while s do
679 if s ~= 1 or cap ~= "" then
680 table.insert(t,cap)
681 end
682 last_end = e+1
683 s, e, cap = str:find(fpat, last_end)
684 end
685 if last_end <= #str then
686 cap = str:sub(last_end)
687 table.insert(t, cap)
688 end
689 return t
690 end
691
692 local function assign(arr, content)
693 local tmp = html;
694 local idx = nil;
695 for _,i in ipairs(arr) do
696 if idx ~= nil then
697 if tmp[idx] == nil then
698 tmp[idx] = {};
699 end
700 tmp = tmp[idx];
701 end
702 idx = i;
703 end
704 tmp[idx] = content;
705 end
706
707 local function readFile(filepath) 672 local function readFile(filepath)
708 local f,err = io_open(filepath, "r"); 673 local f,err = io_open(filepath, "r");
709 if not f then return f,err; end 674 if not f then return f,err; end
710 local t = f:read("*all"); 675 local t = f:read("*all");
711 f:close() 676 f:close()
714 679
715 local function loadTheme(path) 680 local function loadTheme(path)
716 for file in lfs.dir(path) do 681 for file in lfs.dir(path) do
717 if file:match("%.html$") then 682 if file:match("%.html$") then
718 module:log("debug", "opening theme file: " .. file); 683 module:log("debug", "opening theme file: " .. file);
719 local tmp = split(file:match("(.*)%.html$"), "_");
720 local content,err = readFile(path .. "/" .. file); 684 local content,err = readFile(path .. "/" .. file);
721 if not content then return content,err; end 685 if not content then return content,err; end
722 assign(tmp, content); 686
687 -- html.a.b.c = content of a_b_c.html
688 local tmp = html;
689 for idx in file:gmatch("([^_]*)_") do
690 tmp[idx] = tmp[idx] or {};
691 tmp = tmp[idx];
692 end
693 tmp[file:match("([^_]*)%.html$")] = content;
723 end 694 end
724 end 695 end
725 return true; 696 return true;
726 end 697 end
727 698
740 if attributes == nil or attributes.mode ~= "directory" then 711 if attributes == nil or attributes.mode ~= "directory" then
741 module:log("error", "Theme folder of theme \"".. tostring(theme) .. "\" isn't existing. expected Path: " .. themePath); 712 module:log("error", "Theme folder of theme \"".. tostring(theme) .. "\" isn't existing. expected Path: " .. themePath);
742 return false; 713 return false;
743 end 714 end
744 715
745 -- module:log("debug", (require "util.serialization").serialize(html));
746 local themeLoaded,err = loadTheme(themePath); 716 local themeLoaded,err = loadTheme(themePath);
747 if not themeLoaded then 717 if not themeLoaded then
748 module:log("error", "Theme \"%s\" is missing something: %s", tostring(theme), err); 718 module:log("error", "Theme \"%s\" is missing something: %s", tostring(theme), err);
749 return false; 719 return false;
750 end 720 end
751 -- module:log("debug", (require "util.serialization").serialize(html));
752 721
753 httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 }); 722 httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 });
754 end 723 end