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
|
#!/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 bit, dir, file, parent, path = '', '', '', '', ''
local bits = {}
local last = 1
for i = 1, #l do
local c = string.sub(l, i, i)
if '/' == c then
bit = string.sub(l, last, i - 1)
if 0 == ln then
parent = bit
end
table.insert(bits, bit)
last = i + 1
end
end
bit = string.sub(l, last)
bit = string.gsub(bit, '%.md$', '')
table.insert(bits, bit)
local ln = #bits
local base = table.concat(bits, '/')
if 1 < ln then
dir = bits[ln - 1]
parent = table.concat(bits, '/', 1, ln - 2)
end
if 0 < ln then path = table.concat(bits, '/', 1, ln - 1) end
local file = bit
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
sites[path].depth = ln - 1
sites[path].parent = parent
sites[path].dir = dir
sites[path].bits = bits
table.sort(sites[path].files, function(a, b) return (a.name <= b.name) end)
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
for k, v in pairs(sites) do
local st = {}
if nil ~= v.subs then
for l, w in pairs(v.subs) do
table.insert(st, w)
end
table.sort(st, function(a, b) return (a.name <= b.name) end)
v.subs = st
end
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.dir
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.depth
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> '
-- p = w[parent]
-- if nil == p then break end
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
|