# HG changeset patch # User Tuomo Valkonen # Date 1252780077 -10800 # Node ID 3975fa5ed63037d2faa2ffe38c795585fcf83f09 # Parent e2face1be50e33367320992fe288cc4cdb01b6d1 (don't know) diff -r e2face1be50e -r 3975fa5ed630 config.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,5 @@ + +module("config") + +dirsep = "/" +meta_marker = "^---%s*$" diff -r e2face1be50e -r 3975fa5ed630 environment.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/environment.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,26 @@ + +local hierarchy={} + +local function descend(hierarchy, dir) + local loca=hierarchy + string.gsub(dir, "([^/]+)", + function(d) + loca=hierarchy[d] + assert(loca) + end) + return loca +end + +-- Tai sit vaan jokainen dokumentti ilman extensiota. + +function get_shortcuts(dir) + --local loca=descend(hierarchy, dir) + return {} --append(loca, append(hierarchy, {})) +end + +function get_environment(dir) + return { + location=dir, + shortcuts=get_shortcuts(dir) + } +end diff -r e2face1be50e -r 3975fa5ed630 err.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/err.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,11 @@ + +module("err", package.seeall) + +function file_pos(f, pos, e) + error(string.format("%s:%d: %s", f, pos, e)) +end + +function file(f, e) + error(string.format("%s %s", f, e)) +end + diff -r e2face1be50e -r 3975fa5ed630 handlers.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handlers.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,38 @@ + +module("handlers", package.seeall) + +require("handlers.render") +require("handlers.copy") +require("handlers.ignore") + +local available={ + { pattern = "%.page$", handler = handlers.render}, + { pattern = "", handler = handlers.copy}, +} + + +function find(f) + for _, h in ipairs(available) do + if string.match(h.pattern, f) then + return h.handler + end + end + return handlers.ignore +end + +local cached={} + +function choose(f, env) + cached[f] = find(f) +end + +-- load +function phase1(f, env) + return cached[f].phase1(f, env) +end + +-- write +function phase2(f, env) + return cached[f].phase2(f, env) +end + diff -r e2face1be50e -r 3975fa5ed630 handlers/copy.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handlers/copy.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,9 @@ + +module("handlers.copy", package.seeall) + +function phase1(f, env) +end + +function phase2(f, env) + -- copy here! +end diff -r e2face1be50e -r 3975fa5ed630 handlers/ignore.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handlers/ignore.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,8 @@ + +module("handlers.ignore", package.seeall) + +function phase1(f, env) +end + +function phase2(f, env) +end diff -r e2face1be50e -r 3975fa5ed630 handlers/render.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/handlers/render.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,104 @@ + +module("handlers.render", package.seeall) + +require('markdown') +require('etree') +require('err') +require('environment') +require('config') + +-- +-- Phase 1: load & markup +-- + +local phase1_cache={} + +function phase1(file, env) + --local dir = string.match(file, "(.*)/[^/]*") + --local env = get_environment(dir) + local f = io.open(file, 'r') + local data = '' + local in_meta = false + local linenum=1 + + for l in f:lines() do + if string.match(l, config.meta_marker) then + in_meta = not in_meta + elseif in_meta then + local key, val = string.match(l, "%s*([^:]*)%s*:%s*(.*)%s*") + if key and val then + env[key] = val + else + err.file_pos(file, linenum, "meta data syntax error") + end + else + data = data.."\n"..l + end + linenum = linenum+1 + end + + f:close() + + local data2=markdown(data) + + phase1_cache[file]=data2 +end + + +-- +-- Phase 2: Tag processing +-- + +-- Vaiko silti toistepäin? Inlinejen tagit pitää +-- prosessoida ennen inlinetystä. Mutta toisaalta +-- templateen myös prosessointi. + +local function tag_lgen_inline(env, t) + a.tag="div" + -- todo: get inlineable stuff from attrs + a.attr.class="lgen:inline" + --table.insert(a, get_inline(env, a) +end + +local function tag_lgen_a(env, t) +end + + +local operations={ + ["lgen:inline"] = tag_lgen_inline, + ["a"] = tag_lgen_a, +} + +local function scan(env, et) + for _, v in ipairs(et) do + if type(v)=="table" then + operations[v.tag](env, v) + end + end +end + +setmetatable(operations, { __index = function() return scan end }) + +function phase2(file, env) + local data=phase1_cache[file] + --print(data) + --print("----------") + local et, msg, line, col, pos = etree.fromstring([[]]..data..'') + --local et, msg, line, col, pos = lom.parse(""..data.."") + if not et then + error(string.format("%d:%d(%d): %s", line or -1 , col or -1, pos or -1, msg)) + end + + operations[et.tag](env, et) + + -- TODO: inlining + -- maybe + --phase2_cache[file]=etree.tostring(et); +end + + + +function phase3(file, env) +end diff -r e2face1be50e -r 3975fa5ed630 lgen.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lgen.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,30 @@ + +module('lgen', package.seeall) +-- export src dst hierarchy + +require('scan') +require('handlers') + +-- +-- Main +-- + +if #arg < 2 then + error("Usage: lgen src dst") +end + +src = arg[1] +dst = arg[2] + +print('Scan...') +hierarchy = scan.scan(src) + +-- Pitäisi env konstruoida. polun perusteella. +scan.map(hierarchy, function(f) handlers.choose(f, env) end) + +print('Phase 1...') +scan.map(hierarchy, function(f) handlers.phase1(f, env) end) +print('Phase 2...') +scan.map(hierarchy, function(f) handlers.phase2(f, env) end) +print('Phase 3...') +scan.map(hierarchy, function(f) handlers.phase3(f, env) end) diff -r e2face1be50e -r 3975fa5ed630 path.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/path.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,83 @@ + +module("path", package.seeall) + +require("tuple") +require("lfs") +require("err") +require("config") + +local sep=config.dirsep + +function join(p, f) + return p..sep..f +end + +function dirbasename(p) + local d, b = string.match(p, + "(.*)"..sep.."+([^"..sep.."]+)"..sep.."*$") + if not b then + return string.match(p, "([^"..sep.."]*") + else + return d, b + end +end + +function dirname(p) + return tuple.fst(dirbasename(p)) +end + +function basename(p) + return tuple.snd(dirbasename(p)) +end + +-- would rather do this as an iterator, but can't +-- coroutine.yield from gsub handler +function split(p) + local t={} + local head="" + local s, p2 = string.match(p, "^("..sep.."+)(.*)$") + if s then + table.insert(t, "/.") -- root + p=p2 + end + string.gsub(p, "([^"..sep.."]+)"..sep.."+", + function(d) table.insert(t, d); end) + return t +end + +function parts(p) + local s={i=1, t=split(p)} + local function g(s) + local v=s.t[s.i] + s.i=s.i+1 + return v + end + return g, s +end + +function makepath(p) + local head="" + for d in parts(p) do + head=head..d + local a=lfs.attributes(head) + if not a then + local success, e = lfs.mkdir(head) + if not success then + err.file(head, e) + end + elseif a.mode~="directory" then + err.file(head, "not a directory") + end + head=head..sep + end +end + +--[[ +print(dirbasename("/foo/bar/baz")) + +for k in parts("//foo/bar/baz/quk") do + print(k) +end + +makepath("foo/bar/baz/quk") +--]] diff -r e2face1be50e -r 3975fa5ed630 scan.lua --- a/scan.lua Tue Mar 17 21:39:41 2009 +0200 +++ b/scan.lua Sat Sep 12 21:27:57 2009 +0300 @@ -1,4 +1,8 @@ + +module("scan", package.seeall) + require('lfs') +require('path') local function filtered_dir(d) local f, s_, v_ = lfs.dir(d) @@ -13,7 +17,6 @@ return g, s_, v_ end - function scan(d) local h={} for f in filtered_dir(d) do @@ -23,7 +26,7 @@ local nh=scan(n) h[n]=nh elseif a.mode=='file' then - h[n]=true + h[f]=true end end return h @@ -38,3 +41,16 @@ end end end + +function map(h, g) + local function dir(prefix, name) + return path.join(prefix, name) + end + + local function file(prefix, name) + local f=path.join(prefix, name) + g(f) + end + + hfold(h, "", dir, file) +end diff -r e2face1be50e -r 3975fa5ed630 test.lua --- a/test.lua Tue Mar 17 21:39:41 2009 +0200 +++ b/test.lua Sat Sep 12 21:27:57 2009 +0300 @@ -1,9 +1,9 @@ require('scan') -h=scan('/home/tuomov/work/ion-homepage/src') +h=scan.scan('/home/tuomov/work/newblog/src') -hfold(h, '', +scan.hfold(h, '', function(a, n) return a.."/"..n end, - function(a, n) print(a.."/"..n) end) + function(a, n) print(a.." | "..n) end) \ No newline at end of file diff -r e2face1be50e -r 3975fa5ed630 tuple.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tuple.lua Sat Sep 12 21:27:57 2009 +0300 @@ -0,0 +1,10 @@ + +module("tuple") + +function fst(a, b) + return a +end + +function snd(a, b) + return b +end