diff options
-rwxr-xr-x | notYetAnotherWiki.lua | 279 |
1 files changed, 131 insertions, 148 deletions
diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua index 0ee0f10..f3956f8 100755 --- a/notYetAnotherWiki.lua +++ b/notYetAnotherWiki.lua | |||
@@ -2,118 +2,12 @@ | |||
2 | 2 | ||
3 | local lcmark = require("lcmark") | 3 | local lcmark = require("lcmark") |
4 | 4 | ||
5 | local globalData = {version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'} | ||
6 | local site = {} | ||
7 | |||
8 | local createHTML = function(cm, file) | ||
9 | -- cm = string.gsub(cm, '. ', '. ') | ||
10 | if (nil ~= file) and ('' ~= file) then io.write('Parsing ' .. file .. ' -> ') end | ||
11 | local result = '' | ||
12 | local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0}) | ||
13 | |||
14 | if nil == body then print('oops! ' .. err) else | ||
15 | local bod, err = lcmark.compile_template(body) | ||
16 | if nil == bod then print('oops! ' .. err) else | ||
17 | local templateFile = metadata.template | ||
18 | if nil == file then | ||
19 | templateFile = nil | ||
20 | else | ||
21 | if nil == templateFile then templateFile = 'default' end | ||
22 | templateFile = templateFile .. '.template' | ||
23 | for k, v in pairs(globalData) do | ||
24 | if nill == metadata[k] then metadata[k] = v else print('metadata already has ' .. k) end | ||
25 | end | ||
26 | end | ||
27 | |||
28 | if nil ~= file then | ||
29 | local depth = 0 | ||
30 | local base = '' | ||
31 | for p in string.gmatch(file, '(%w+)/') do | ||
32 | depth = depth + 1 | ||
33 | base = p | ||
34 | end | ||
35 | local path = string.sub(file, 1, #base) | ||
36 | |||
37 | metadata.header = '' | ||
38 | metadata.menu = '' | ||
39 | local trl = {} | ||
40 | metadata.trail = '' | ||
41 | local dir = site[''] | ||
42 | local dr, pdr, tr = '', '', '' | ||
43 | while nil ~= dir.subs do | ||
44 | local old = dir | ||
45 | for m, x in pairs(dir.subs) do | ||
46 | if x == string.sub(file, 1, #x) then | ||
47 | pdr = pdr .. '/' .. m | ||
48 | tr = tr .. '../' | ||
49 | dr = m | ||
50 | dir = site[dr] | ||
51 | table.insert(trl, '">' .. m .. '</a> 👣') | ||
52 | break | ||
53 | end | ||
54 | end | ||
55 | if old == dir then break end | ||
56 | end | ||
57 | table.remove(trl) | ||
58 | for m, x in ipairs(trl) do | ||
59 | tr = string.sub(tr, 4) | ||
60 | trl[m] = ' <a href="' .. tr .. x | ||
61 | end | ||
62 | metadata.trail = table.concat(trl) | ||
63 | |||
64 | if nil ~= dir.files then | ||
65 | local l = {} | ||
66 | for m, x in pairs(dir.files) do table.insert(l, {m = m; x = x}) end | ||
67 | table.sort(l, function(a, b) return (a.m <= b.m) end) | ||
68 | for m, x in ipairs(l) do | ||
69 | metadata.menu = metadata.menu .. '<p><a href="' .. string.sub(x.x, 1 + #pdr) .. '.HTML">' .. x.m .. '</a></p>\n' | ||
70 | end | ||
71 | end | ||
72 | if nil ~= dir.subs then | ||
73 | local l = {} | ||
74 | for m, x in pairs(dir.subs) do table.insert(l, {m = m; x = x}) end | ||
75 | table.sort(l, function(a, b) return (a.m <= b.m) end) | ||
76 | for m, x in pairs(l) do | ||
77 | metadata.header = metadata.header .. '<a href="' .. string.sub(x.x, 1 + #pdr) .. '">' .. x.m .. '</a> ' | ||
78 | end | ||
79 | end | ||
80 | end | ||
81 | 5 | ||
82 | metadata.body = lcmark.apply_template(bod, metadata) | ||
83 | 6 | ||
84 | local tm = '' | 7 | local globalData = {favicon = 'nYAW_icon.png', logo = 'nYAW.png', version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'} |
85 | if nil ~= templateFile then | 8 | local sites = {} |
86 | local h = io.open(templateFile, 'r') | ||
87 | if nil ~= h then | ||
88 | tm = tm .. h:read('*a') | ||
89 | h:close() | ||
90 | else | ||
91 | print('oops! No such file ' .. templateFile) | ||
92 | end | ||
93 | 9 | ||
94 | local template, err = lcmark.compile_template(tm) | ||
95 | if nil == template then print('oops! ' .. err) else | ||
96 | result = lcmark.apply_template(template, metadata) | ||
97 | end | ||
98 | else | ||
99 | result = body | ||
100 | end | ||
101 | end | ||
102 | end | ||
103 | 10 | ||
104 | if ('' ~= result) and (nil ~= file) then | ||
105 | local base = string.gsub(file, '%.md$', '') | ||
106 | print(base .. '.HTML') | ||
107 | local a, e = io.open(base .. '.HTML', 'w') | ||
108 | if nil == a then print('Could not open ' .. base .. '.HTML - ' .. e) else | ||
109 | a:write(result) | ||
110 | a:close() | ||
111 | end | ||
112 | else | ||
113 | print('') | ||
114 | end | ||
115 | return result | ||
116 | end | ||
117 | 11 | ||
118 | local directory = arg[1] | 12 | local directory = arg[1] |
119 | if nil == directory then directory = '.' end | 13 | if nil == directory then directory = '.' end |
@@ -128,57 +22,146 @@ for l in io.popen('find ' .. directory .. ' -name "*.md" -type f,l -printf "%P\n | |||
128 | end | 22 | end |
129 | 23 | ||
130 | for i, l in pairs(all) do | 24 | for i, l in pairs(all) do |
131 | local dir = '' | 25 | local bit, dir, file, parent, path = '', '', '', '', '' |
132 | local files, subs = {}, {} | 26 | local bits = {} |
133 | local c, parent = 1, '' | 27 | local last = 1 |
134 | 28 | for i = 1, #l do | |
135 | for p in string.gmatch(l, '(%w+)/') do | 29 | local c = string.sub(l, i, i) |
136 | if '' == dir then | 30 | if '/' == c then |
137 | dir = p | 31 | bit = string.sub(l, last, i - 1) |
138 | if nil ~= site[dir] then | 32 | if 0 == ln then |
139 | subs = site[dir].subs | 33 | parent = bit |
140 | files = site[dir].files | ||
141 | end | 34 | end |
35 | table.insert(bits, bit) | ||
36 | last = i + 1 | ||
142 | end | 37 | end |
143 | |||
144 | local path = string.sub(l, 1, -1 - #(string.gsub(l, '.*/', ''))) | ||
145 | if nil ~= site[parent] then | ||
146 | if nil == site[parent].subs then site[parent].subs = {} end | ||
147 | site[parent].subs[p] = path | ||
148 | elseif 1 == c then subs[p] = path | ||
149 | else | ||
150 | if ('' ~= parent) and (dir == parent) then subs[p] = path end | ||
151 | site[parent] = {subs = {[p] = path}} | ||
152 | end | ||
153 | c = c + #p + 1 | ||
154 | parent = p | ||
155 | end | 38 | end |
156 | 39 | bit = string.sub(l, last) | |
157 | if (1 == c) and (nil ~= site[dir]) then | 40 | bit = string.gsub(bit, '%.md$', '') |
158 | subs = site[dir].subs | 41 | table.insert(bits, bit) |
159 | files = site[dir].files | 42 | |
43 | local ln = #bits | ||
44 | local base = table.concat(bits, '/') | ||
45 | if 1 < ln then | ||
46 | dir = bits[ln - 1] | ||
47 | parent = table.concat(bits, '/', 1, ln - 2) | ||
160 | end | 48 | end |
49 | if 0 < ln then path = table.concat(bits, '/', 1, ln - 1) end | ||
50 | local file = bit | ||
161 | 51 | ||
162 | local base = string.gsub(string.sub(l, c, -1), '%.md$', '') | 52 | local files = {name = file, URL = '<p><a href="' .. file .. '.HTML">' .. file .. '</a></p>'} |
163 | if nil ~= site[parent] then site[parent].files[base] = string.sub(l, 1, -4) | 53 | if nil == sites[path] then sites[path] = {files = {files}} |
164 | elseif 1 == c then files[base] = string.sub(l, 1, -4) | 54 | elseif nil == sites[path].files then sites[path].files = {files} |
165 | else | 55 | else table.insert(sites[path].files, files) |
166 | if ('' ~= parent) and (dir == parent) then files[base] = string.sub(l, 1, -4) end | 56 | end |
167 | site[parent] = {files = {[base] = string.sub(l, 1, -4)}} | 57 | sites[path].depth = ln - 1 |
58 | sites[path].parent = parent | ||
59 | sites[path].dir = dir | ||
60 | sites[path].bits = bits | ||
61 | table.sort(sites[path].files, function(a, b) return (a.name <= b.name) end) | ||
62 | |||
63 | files = {name = dir, URL = '<a href="' .. dir .. '/">' .. dir .. '</a> '} | ||
64 | if nil == sites[parent] then sites[parent] = {subs = {[files.name] = files}} | ||
65 | elseif nil == sites[parent].subs then sites[parent].subs = {[files.name] = files} | ||
66 | else sites[parent].subs[files.name] = files | ||
168 | end | 67 | end |
169 | |||
170 | -- FIXME - still some minor bug somewhere, this fixes that, but causes other problems. Meh, I can live with excess empty subs tables. | ||
171 | -- if (nil ~= subs) and (0 == #subs) then subs = nil end | ||
172 | site[dir] = {files = files, subs = subs} | ||
173 | end | 68 | end |
174 | 69 | ||
175 | for k, v in pairs(site) do | 70 | for k, v in pairs(sites) do |
71 | local st = {} | ||
72 | if nil ~= v.subs then | ||
73 | for l, w in pairs(v.subs) do | ||
74 | table.insert(st, w) | ||
75 | end | ||
76 | table.sort(st, function(a, b) return (a.name <= b.name) end) | ||
77 | v.subs = st | ||
78 | end | ||
79 | |||
176 | if nil ~= v.files then | 80 | if nil ~= v.files then |
177 | for m, x in pairs(v.files) do | 81 | for l, w in pairs(v.files) do |
178 | local file = x .. '.md' | 82 | local path = v.parent |
83 | if '' ~= path then path = path .. '/' end | ||
84 | path = path .. v.dir | ||
85 | if '' ~= path then path = path .. '/' end | ||
86 | local file = path .. w.name .. '.md' | ||
87 | if (nil ~= file) and ('' ~= file) then io.write('Parsing ' .. file .. ' -> ') end | ||
88 | |||
179 | local h = io.open(file, 'r') | 89 | local h = io.open(file, 'r') |
180 | if nil ~= h then | 90 | if nil ~= h then |
181 | createHTML(h:read('*a'), file) | 91 | local cm = h:read('*a') |
92 | local result = '' | ||
93 | local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0}) | ||
94 | |||
95 | if nil == body then print('oops! ' .. err) else | ||
96 | local bod, err = lcmark.compile_template(body) | ||
97 | if nil == bod then print('oops! ' .. err) else | ||
98 | local templateFile = metadata.template | ||
99 | if nil == file then | ||
100 | templateFile = nil | ||
101 | else | ||
102 | if nil == templateFile then templateFile = 'default' end | ||
103 | templateFile = templateFile .. '.template' | ||
104 | for m, x in pairs(globalData) do | ||
105 | if nil == metadata[m] then metadata[m] = x else print('metadata already has ' .. m) end | ||
106 | end | ||
107 | end | ||
108 | |||
109 | if nil ~= v.files then | ||
110 | for m, x in ipairs(v.files) do | ||
111 | metadata.menu = metadata.menu .. x.URL .. '\n' | ||
112 | end | ||
113 | end | ||
114 | if nil ~= v.subs then | ||
115 | for m, x in pairs(v.subs) do | ||
116 | metadata.header = metadata.header .. x.URL | ||
117 | end | ||
118 | end | ||
119 | |||
120 | local num = v.depth | ||
121 | local trail = '<a href="' .. string.rep('../', num) .. '">home</a> ' | ||
122 | local p = v.parent | ||
123 | for i = 1, num - 1 do | ||
124 | trail = trail .. '<a href="' .. string.rep('../', num - i) .. '">' .. v.bits[i] .. '</a> ' | ||
125 | p = w[parent] | ||
126 | if nil == p then break end | ||
127 | end | ||
128 | trail = trail .. v.bits[#(v.bits)] | ||
129 | metadata.trail = trail | ||
130 | |||
131 | metadata.body = lcmark.apply_template(bod, metadata) | ||
132 | |||
133 | local tm = '' | ||
134 | if nil ~= templateFile then | ||
135 | local h = io.open(templateFile, 'r') | ||
136 | if nil ~= h then | ||
137 | tm = tm .. h:read('*a') | ||
138 | h:close() | ||
139 | else | ||
140 | print('oops! No such file ' .. templateFile) | ||
141 | end | ||
142 | |||
143 | local template, err = lcmark.compile_template(tm) | ||
144 | if nil == template then print('oops! ' .. err) else | ||
145 | result = lcmark.apply_template(template, metadata) | ||
146 | end | ||
147 | else | ||
148 | result = body | ||
149 | end | ||
150 | |||
151 | if ('' ~= result) and (nil ~= file) then | ||
152 | local base = path .. w.name .. '.HTML' | ||
153 | print(base) | ||
154 | local a, e = io.open(base, 'w') | ||
155 | if nil == a then print('Could not open ' .. base .. ' - ' .. e) else | ||
156 | a:write(result) | ||
157 | a:close() | ||
158 | end | ||
159 | else | ||
160 | print('') | ||
161 | end | ||
162 | end | ||
163 | end | ||
164 | |||
182 | h:close() | 165 | h:close() |
183 | else | 166 | else |
184 | print('oops! No such file ' .. file) | 167 | print('oops! No such file ' .. file) |