Convert module stuff to lua 5.3

Tue, 16 Jan 2018 16:39:48 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 16 Jan 2018 16:39:48 +0000
changeset 7
038275cd92ed
parent 6
219d7a7304f8
child 8
836dac92eced

Convert module stuff to lua 5.3

config.lua file | annotate | diff | comparison | revisions
dependency.lua file | annotate | diff | comparison | revisions
err.lua file | annotate | diff | comparison | revisions
filecache.lua file | annotate | diff | comparison | revisions
handlers.lua file | annotate | diff | comparison | revisions
handlers/copy.lua file | annotate | diff | comparison | revisions
handlers/ignore.lua file | annotate | diff | comparison | revisions
handlers/render.lua file | annotate | diff | comparison | revisions
lgen.lua file | annotate | diff | comparison | revisions
log.lua file | annotate | diff | comparison | revisions
path.lua file | annotate | diff | comparison | revisions
scan.lua file | annotate | diff | comparison | revisions
tuple.lua file | annotate | diff | comparison | revisions
--- a/config.lua	Sun May 07 20:02:53 2017 +0100
+++ b/config.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,6 +1,10 @@
 
-module("config")
+--@module config
+
+local config={}
 
-dirsep = "/"
-meta_marker = "^---%s*$"
-debug=true
+config.dirsep = "/"
+config.meta_marker = "^---%s*$"
+config.debug=true
+
+return config
--- a/dependency.lua	Sun May 07 20:02:53 2017 +0100
+++ b/dependency.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,9 +1,11 @@
 
-module("dependency", package.seeall)
+--@module dependency
 
-require('lfs')
+local dependency={}
 
-function simple_update_check(tgt, srcs)
+lfs=require('lfs')
+
+function dependency.simple_update_check(tgt, srcs)
     local a=lfs.attributes(tgt)
     if not a then
         return true
@@ -16,3 +18,5 @@
     end
     return false
 end
+
+return dependency
--- a/err.lua	Sun May 07 20:02:53 2017 +0100
+++ b/err.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,11 +1,13 @@
 
-module("err", package.seeall)
+--@module err
+local err={}
 
-function file_pos(f, pos, e)
+function err.file_pos(f, pos, e)
     error(string.format("%s:%d: %s", f, pos, e))
 end
 
-function file(f, e)
+function err.file(f, e)
     error(string.format("%s %s", f, e))
 end
 
+return err
--- a/filecache.lua	Sun May 07 20:02:53 2017 +0100
+++ b/filecache.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,9 +1,11 @@
 
-module("filecache", package.seeall)
+--@module filecache
+
+local filecache={}
 
 local cache={}
 
-function get(file)
+function filecache.get(file)
     if not cache[file] then
         local f=io.openX(file, "r")
         cache[file]=f:read('*a')
@@ -11,3 +13,5 @@
     end
     return cache[file]
 end
+
+return filecache
--- a/handlers.lua	Sun May 07 20:02:53 2017 +0100
+++ b/handlers.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,9 +1,11 @@
 
-module("handlers", package.seeall)
+--@module handlers
 
-require("handlers.render")
-require("handlers.copy")
-require("handlers.ignore")
+local handlers={}
+
+handlers.render=require("handlers.render")
+handlers.copy=require("handlers.copy")
+handlers.ignore=require("handlers.ignore")
 
 local available={
     { pattern = "%.lg$", handler = handlers.render},
@@ -12,7 +14,7 @@
 }
 
 
-function find(f)
+function handlers.find(f)
     for _, h in ipairs(available) do
         if string.match(f, h.pattern) then
             return h.handler
@@ -23,17 +25,18 @@
 
 local cached={}
 
-function choose(f, env)
-    cached[f] = find(f)
+function handlers.choose(f, env)
+    cached[f] = handlers.find(f)
 end
 
 -- load
-function phase1(f, env)
+function handlers.phase1(f, env)
     return cached[f].phase1(f, env)
 end
 
 -- write
-function phase2(f, env)
+function handlers.phase2(f, env)
     return cached[f].phase2(f, env)
 end
 
+return handlers
--- a/handlers/copy.lua	Sun May 07 20:02:53 2017 +0100
+++ b/handlers/copy.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,13 +1,17 @@
 
-module("handlers.copy", package.seeall)
+--@module handlers.copy
+
+local path=require("path")
 
-require("dependency")
-require("log")
+local handlers_copy={}
 
-function phase1(file, env)
+local dependency=require("dependency")
+local log=require("log")
+
+function handlers_copy.phase1(file, env)
 end
     
-function phase2(file, env)
+function handlers_copy.phase2(file, env)
     local src=path.join(env.paths.src, file)
     local dst=path.join(env.paths.dst, file)
     if dependency.simple_update_check(dst, {src}) then
@@ -23,3 +27,5 @@
         end
     end
 end
+
+return handlers_copy
--- a/handlers/ignore.lua	Sun May 07 20:02:53 2017 +0100
+++ b/handlers/ignore.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,8 +1,12 @@
 
-module("handlers.ignore", package.seeall)
+--@module handlers.ignore
 
-function phase1(f, env)
+local handlers_ignore={}
+
+function handlers_ignore.phase1(f, env)
 end
     
-function phase2(f, env)
+function handlers_ignore.phase2(f, env)
 end
+
+return handlers_ignore
--- a/handlers/render.lua	Sun May 07 20:02:53 2017 +0100
+++ b/handlers/render.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,18 +1,21 @@
 
-module("handlers.render", package.seeall)
+--@module handlers.render
+
+local handlers_render={}
 
 local ltp=require('ltp.template')
-require('markdown')
-require('config')
-require('path')
-require('filecache')
-require('log')
+local markdown=require('markdown')
+local config=require('config')
+local path=require('path')
+local filecache=require('filecache')
+local log=require('log')
+local dependency=require('dependency')
 
 --
 -- Phase 1: load & markup
 --
 
-function phase1(file, env)
+function handlers_render.phase1(file, env)
     local f = io.open(path.join(env.paths.src, file), 'r')
     local data = nil
     local in_meta = false
@@ -63,14 +66,14 @@
     env.pages[file]=page
 end
 
-function process_lua(template, env)
+function handlers_render.process_lua(template, env)
     env=table.join(env, {env=env}) -- TODO: should use __index
     --return ltp.render(nil, 1, template, env, {}, "<%", "%>", {})
     return ltp.render_template(template, "<%", "%>", 
                                ltp.merge_index(env, _G))
 end
 
-function env_for(file, env, path_prefix)
+function handlers_render.env_for(file, env, path_prefix)
     local newenv=table.copy(env)
     
     newenv.base_url=path.to_root(file)
@@ -80,16 +83,16 @@
     return newenv
 end
 
-function render(file, env, path_prefix)
+function handlers_render.render(file, env, path_prefix)
     local data=env.pages[file].data
     if data then
-        local newenv=env_for(file, env, path_prefix)
-        local data2=process_lua(data, newenv)
+        local newenv=handlers_render.env_for(file, env, path_prefix)
+        local data2=handlers_render.process_lua(data, newenv)
         return markdown(data2)
     end
 end
 
-function phase2(file, env)
+function handlers_render.phase2(file, env)
     local page=env.pages[file]
     local src = path.join(env.paths.src, file)
     local dst = path.join(env.paths.dst, page.destination)
@@ -112,11 +115,11 @@
     
     if build then 
         log.log("Render "..file.."\n")
-        local content=render(file, env)
+        local content=handlers_render.render(file, env)
         local page_template=filecache.get(tmpl)
 
-        local newenv=table.join({content=content}, env_for(file, env))
-        local data2=process_lua(page_template, newenv)
+        local newenv=table.join({content=content}, handlers_render.env_for(file, env))
+        local data2=handlers_render.process_lua(page_template, newenv)
         
         log.log("Write "..page.destination.."\n")
         local f=io.openX(dst, "w")
@@ -124,3 +127,4 @@
     end
 end
 
+return handlers_render
--- a/lgen.lua	Sun May 07 20:02:53 2017 +0100
+++ b/lgen.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,16 +1,20 @@
-     
-module('lgen', package.seeall)
--- export src dst hierarchy
+
+local path=string.gsub(arg[0], "(.*)/[^/]*", "%1");
+if path~=nil then
+    package.path=package.path .. ';' .. path .. '/?.lua'
+end
 
 -- Lua 5.1 setfenv/getfenv compatibility for Lua 5.2
 local CE = require('compat_env')
 _G.setfenv = CE.setfenv
 _G.getfenv = CE.getfenv
 
-require('scan')
-require('handlers')
+local scan=require('scan')
+local handlers=require('handlers')
 -- globally add missing stuff 
-require('luaext')
+local luaext=require('luaext')
+local lfs=require('lfs')
+local path=require('path')
 
 --
 -- Main
@@ -20,12 +24,13 @@
     error("Usage: lgen src tmpl dst")
 end
 
-src = arg[1]
-tmpl = arg[2]
-dst = arg[3]
+local src = arg[1]
+local tmpl = arg[2]
+local dst = arg[3]
 
 print('Scan...')
-hierarchy = scan.scan(src)
+
+local hierarchy = scan.scan(src)
 
 
 local env={
--- a/log.lua	Sun May 07 20:02:53 2017 +0100
+++ b/log.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,10 +1,14 @@
 
-module("log", package.seeall)
+--@module log
 
-require("config")
+local log={}
 
-function log(str)
+local config=require("config")
+
+function log.log(str)
     if config.debug then
         io.stderr:write(str)
     end
 end
+
+return log
--- a/path.lua	Sun May 07 20:02:53 2017 +0100
+++ b/path.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,14 +1,15 @@
 
-module("path", package.seeall)
+--@module path
+local path={}
 
-require("tuple")
-require("lfs")
-require("err")
-require("config")
+local tuple=require("tuple")
+local lfs=require("lfs")
+local err=require("err")
+local config=require("config")
 
 local sep=config.dirsep
 
-function join(p, f)
+function path.join(p, f)
     if p=="" then
         return f
     else
@@ -16,7 +17,7 @@
     end
 end
 
-function dirbasename(p)
+function path.dirbasename(p)
     local d, b = string.match(p,
         "^(.*)"..sep.."+([^"..sep.."]+)"..sep.."*$")
     if not b then
@@ -26,11 +27,11 @@
     end
 end
 
-function dirname(p)
+function path.dirname(p)
     return tuple.fst(dirbasename(p))
 end
 
-function dirname_slash(p)
+function path.dirname_slash(p)
     local dn=dirname(p)
     if dn=="" then
         return dn
@@ -39,17 +40,17 @@
     end
 end
 
-function basename(p)
+function path.basename(p)
     return tuple.snd(dirbasename(p))
 end
 
-function rmext(p)
+function path.rmext(p)
     return string.gsub(p, "%.[^.]*$", "")
 end
 
 -- would rather do this as an iterator, but can't
 -- coroutine.yield from gsub handler
-function split(p)
+function path.split(p)
     local t={}
     local head=""
     local s, p2 = string.match(p, "^("..sep.."+)(.*)$")
@@ -63,7 +64,7 @@
 end
 
 --[[
-function parts(p)
+function path.parts(p)
     local s={i=1, t=split(p)}
     local function g(s)
         local v=s.t[s.i]
@@ -73,7 +74,7 @@
     return g, s
 end
 
-function makepath(p, head)
+function path.makepath(p, head)
     head=head or ""
     for d in parts(p) do
         head=head..d
@@ -91,7 +92,7 @@
 end
 ]]
 
-function simplify(p)
+function path.simplify(p)
     local capts={}
     local start=string.match(p, "^(/)")
     string.gsub(p, "([^/]+)",
@@ -105,6 +106,8 @@
     return (start or "")..table.concat(capts, "/")
 end
 
-function to_root(p)
-    return string.rep("../",#(split(p))-1)
+function path.to_root(p)
+    return string.rep("../",#(path.split(p))-1)
 end
+
+return path
--- a/scan.lua	Sun May 07 20:02:53 2017 +0100
+++ b/scan.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,8 +1,9 @@
-
-module("scan", package.seeall)
 
-require('lfs')
-require('path')
+--@module scan
+local scan={}
+
+local lfs=require('lfs')
+local path=require('path')
 
 local function filtered_dir(d) 
     local f, s_, v_ = lfs.dir(d)
@@ -17,13 +18,13 @@
     return g, s_, v_
 end
 
-function scan(d)
+function scan.scan(d)
     local h={}
     for f in filtered_dir(d) do
         local n=d..'/'..f
         local a=lfs.attributes(n)
         if a.mode=='directory' then
-            local nh=scan(n)
+            local nh=scan.scan(n)
             h[f]=nh
         elseif a.mode=='file' then
             h[f]=true
@@ -32,17 +33,17 @@
     return h
 end
 
-function hfold(h, a, f, g)
+function scan.hfold(h, a, f, g)
     for n, c in pairs(h) do
         if type(c)=='table' then
-            hfold(c, f(a, n), f, g)
+            scan.hfold(c, f(a, n), f, g)
         else
             g(a, n)
         end
     end
 end
 
-function map(h, g, d)
+function scan.map(h, g, d)
     local function dir(prefix, name)
         local p=path.join(prefix, name)
         if d then
@@ -56,5 +57,7 @@
         g(f)
     end
     
-    hfold(h, "", dir, file)
+    scan.hfold(h, "", dir, file)
 end
+
+return scan
--- a/tuple.lua	Sun May 07 20:02:53 2017 +0100
+++ b/tuple.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,10 +1,13 @@
 
-module("tuple")
+--@module tuple
+local tuple={}
 
-function fst(a, b)
+function tuplefst(a, b)
     return a
 end
 
-function snd(a, b)
+function tuple.snd(a, b)
     return b
 end
+
+return tuple

mercurial