| |
1 |
| |
2 module("handlers.render", package.seeall) |
| |
3 |
| |
4 require('markdown') |
| |
5 require('etree') |
| |
6 require('err') |
| |
7 require('environment') |
| |
8 require('config') |
| |
9 |
| |
10 -- |
| |
11 -- Phase 1: load & markup |
| |
12 -- |
| |
13 |
| |
14 local phase1_cache={} |
| |
15 |
| |
16 function phase1(file, env) |
| |
17 --local dir = string.match(file, "(.*)/[^/]*") |
| |
18 --local env = get_environment(dir) |
| |
19 local f = io.open(file, 'r') |
| |
20 local data = '' |
| |
21 local in_meta = false |
| |
22 local linenum=1 |
| |
23 |
| |
24 for l in f:lines() do |
| |
25 if string.match(l, config.meta_marker) then |
| |
26 in_meta = not in_meta |
| |
27 elseif in_meta then |
| |
28 local key, val = string.match(l, "%s*([^:]*)%s*:%s*(.*)%s*") |
| |
29 if key and val then |
| |
30 env[key] = val |
| |
31 else |
| |
32 err.file_pos(file, linenum, "meta data syntax error") |
| |
33 end |
| |
34 else |
| |
35 data = data.."\n"..l |
| |
36 end |
| |
37 linenum = linenum+1 |
| |
38 end |
| |
39 |
| |
40 f:close() |
| |
41 |
| |
42 local data2=markdown(data) |
| |
43 |
| |
44 phase1_cache[file]=data2 |
| |
45 end |
| |
46 |
| |
47 |
| |
48 -- |
| |
49 -- Phase 2: Tag processing |
| |
50 -- |
| |
51 |
| |
52 -- Vaiko silti toistepäin? Inlinejen tagit pitää |
| |
53 -- prosessoida ennen inlinetystä. Mutta toisaalta |
| |
54 -- templateen myös prosessointi. |
| |
55 |
| |
56 local function tag_lgen_inline(env, t) |
| |
57 a.tag="div" |
| |
58 -- todo: get inlineable stuff from attrs |
| |
59 a.attr.class="lgen:inline" |
| |
60 --table.insert(a, get_inline(env, a) |
| |
61 end |
| |
62 |
| |
63 local function tag_lgen_a(env, t) |
| |
64 end |
| |
65 |
| |
66 |
| |
67 local operations={ |
| |
68 ["lgen:inline"] = tag_lgen_inline, |
| |
69 ["a"] = tag_lgen_a, |
| |
70 } |
| |
71 |
| |
72 local function scan(env, et) |
| |
73 for _, v in ipairs(et) do |
| |
74 if type(v)=="table" then |
| |
75 operations[v.tag](env, v) |
| |
76 end |
| |
77 end |
| |
78 end |
| |
79 |
| |
80 setmetatable(operations, { __index = function() return scan end }) |
| |
81 |
| |
82 function phase2(file, env) |
| |
83 local data=phase1_cache[file] |
| |
84 --print(data) |
| |
85 --print("----------") |
| |
86 local et, msg, line, col, pos = etree.fromstring([[<!DOCTYPE html |
| |
87 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| |
88 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><body>]]..data..'</body></html>') |
| |
89 --local et, msg, line, col, pos = lom.parse("<foo>"..data.."</foo>") |
| |
90 if not et then |
| |
91 error(string.format("%d:%d(%d): %s", line or -1 , col or -1, pos or -1, msg)) |
| |
92 end |
| |
93 |
| |
94 operations[et.tag](env, et) |
| |
95 |
| |
96 -- TODO: inlining |
| |
97 -- maybe |
| |
98 --phase2_cache[file]=etree.tostring(et); |
| |
99 end |
| |
100 |
| |
101 |
| |
102 |
| |
103 function phase3(file, env) |
| |
104 end |