1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#!/usr/bin/env luajit
local lcmark = require("lcmark")
local globalData = {favicon = 'nYAW_icon.png', logo = 'nYAW.png', version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'}
local sites = {}
local directory = arg[1]
if nil == directory then directory = '.' end
local all = {}
if '.' ~= directory then
for l in io.popen('find . -name "*.md" -type f,l -printf "%P\n"'):lines() do
all[l] = l
end
end
for l in io.popen('find ' .. directory .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do
if nil == all[l] then all[l] = l end
end
for i, l in pairs(all) do
local dir, file, parent, path = '', '', '', '', ''
local bits = {}
local last = 1
for j = 1, #l do
local c = string.sub(l, j, j)
if '/' == c then
table.insert(bits, string.sub(l, last, j - 1))
last = j + 1
end
end
file = string.sub(l, last)
file = string.gsub(file, '%.md$', '')
local ln = #bits
if 0 < ln then
dir = bits[ln]
path = table.concat(bits, '/', 1, ln)
if 1 < ln then parent = table.concat(bits, '/', 1, ln - 1) end
end
local files = {name = file, URL = '<p><a href="' .. file .. '.HTML">' .. file .. '</a></p>', }
if nil == sites[path] then sites[path] = {files = {files}}
elseif nil == sites[path].files then sites[path].files = {files}
else table.insert(sites[path].files, files)
end
if '' ~= dir then
-- FIXME - not sure why this is needed anymore, but it fails building subs without it. Just slows things down otherwise.
files = {name = dir, URL = '<a href="' .. dir .. '/">' .. dir .. '</a> ', }
if nil == sites[parent] then sites[parent] = {subs = {[files.name] = files}}
elseif nil == sites[parent].subs then sites[parent].subs = {[files.name] = files}
else sites[parent].subs[files.name] = files
end
end
end
for k, v in pairs(sites) do
local bit, dir, file, parent, path = '', '', '', '', ''
local bits = {}
local last = 1
for j = 1, #k do
local c = string.sub(k, j, j)
if '/' == c then
table.insert(bits, string.sub(k, last, j - 1))
last = j + 1
end
end
sites[k].parent = table.concat(bits, '/')
table.insert(bits, string.sub(k, last))
sites[k].bits = bits
if nil ~= sites[k].files then table.sort(sites[k].files, function(a, b) return (a.name <= b.name) end) end
dir = bits[#bits - 1]
file = bits[#bits]
if nil ~= sites[dir] then
if nil == sites[dir].subs then sites[dir].subs = {} end
last = nil
for j, g in pairs(sites[dir].subs) do
if (g.name == dir) or (g.name == file) then
last = j
break
end
end
if nil == last then
sites[dir].subs[file] = {name = file, URL = '<a href="' .. file .. '/">' .. file .. '</a> '}
end
end
bits = {}
if nil ~= v.subs then
for l, w in pairs(v.subs) do
table.insert(bits, w)
end
table.sort(bits, function(a, b) return (a.name <= b.name) end)
v.subs = bits
end
end
for k, v in pairs(sites) do
if nil ~= v.files then
for l, w in pairs(v.files) do
local path = v.parent
if '' ~= path then path = path .. '/' end
path = path .. v.bits[#(v.bits)]
if '' ~= path then path = path .. '/' end
local file = path .. w.name .. '.md'
if (nil ~= file) and ('' ~= file) then io.write('Parsing ' .. file .. ' -> ') end
local h = io.open(file, 'r')
if nil ~= h then
local cm = h:read('*a')
local result = ''
local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0})
if nil == body then print('oops! ' .. err) else
local bod, err = lcmark.compile_template(body)
if nil == bod then print('oops! ' .. err) else
local templateFile = metadata.template
if nil == file then
templateFile = nil
else
if nil == templateFile then templateFile = 'default' end
templateFile = templateFile .. '.template'
for m, x in pairs(globalData) do
if nil == metadata[m] then metadata[m] = x else print('metadata already has ' .. m) end
end
end
if nil ~= v.files then
for m, x in ipairs(v.files) do
metadata.menu = metadata.menu .. x.URL .. '\n'
end
end
if nil ~= v.subs then
for m, x in pairs(v.subs) do
metadata.header = metadata.header .. x.URL
end
end
local num = #(v.bits)
local trail = '<a href="' .. string.rep('../', num) .. '">home</a> '
local p = v.parent
for i = 1, num do
trail = trail .. '<a href="' .. string.rep('../', num - i) .. '">' .. v.bits[i] .. '</a> '
end
trail = trail .. w.name
metadata.trail = trail
metadata.body = lcmark.apply_template(bod, metadata)
local tm = ''
if nil ~= templateFile then
local h = io.open(templateFile, 'r')
if nil ~= h then
tm = tm .. h:read('*a')
h:close()
else
print('oops! No such file ' .. templateFile)
end
local template, err = lcmark.compile_template(tm)
if nil == template then print('oops! ' .. err) else
result = lcmark.apply_template(template, metadata)
end
else
result = body
end
if ('' ~= result) and (nil ~= file) then
local base = path .. w.name .. '.HTML'
print(base)
local a, e = io.open(base, 'w')
if nil == a then print('Could not open ' .. base .. ' - ' .. e) else
a:write(result)
a:close()
end
else
print('')
end
end
end
h:close()
else
print('oops! No such file ' .. file)
end
end
end
end
|