aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/notYetAnotherWiki.lua
blob: 13437390dcaf916ae5f214d3606bb7700a564add (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env luajit

local lcmark = require("lcmark")



local globalData = {
    ['_'] = ' ', ['dlr'] = '$', ['karenpurple'] = '#8800ff',
    favicon = 'nYAW_icon.png', logo = 'nYAW.png', header = '', --menu = '',
    history = '',  footer = 'Powered by <a href="https://sledjhamr.org/cgit/notYetAnotherWiki/about/">notYetAnotherWiki</a> Version -0.1. &nbsp;',
}
local Sites, Files, Subs = {}, {}, {}


local copyTable = function(t, strip)
    -- A simple table.subtable = subtable wont work, you end up with a reference so that changes to the later get applaid to the former.
    local argh = {}
    for l, y in ipairs(t) do
	if (l ~= y.name) and strip then table.insert(argh, y) end
    end
    return argh
end

local stringBits = function(l)
    local bits = {}
    local last = 1
    for j = 1, #l do
	if '/' == string.sub(l, j, j) then
	    table.insert(bits, string.sub(l, last, j - 1))
	    last = j + 1
	end
    end
    return bits, string.sub(l, last)
end



local directory = arg[1]
if nil == directory then directory = '.' end
if '.' ~= directory then
    for l in io.popen('find . -name "*.md" -type f,l -printf "%P\n"'):lines() do
	Files[string.gsub(l, '%.md$', '')] = {}
    end
end
for l in io.popen('find ' .. directory .. ' -name "*.md" -type f,l -printf "%P\n"'):lines() do
    local n = string.gsub(l, '%.md$', '')
    if nil == Files[n] then Files[n] = {} end
end

-- Gotta figure out all the files and subs first.  File and sub metadata comes along for the ride.
Subs[''] = {files = {}, subs = {}, bits = {}}
for name, file in pairs(Files) do
    local bitter, path = '', ''
    local bits, bit = stringBits(name)
    local ln = #bits

    Files[name].bits = bits
    Files[name].bit = bit
    if ln > 0 then bitter = bits[1] end
    if '' ~= bitter then Subs[''].subs[bitter] = bitter end	-- "bitter end" was entirely by accident, I'm keeping it.  B-)
    for i, d in ipairs(bits) do
	if '' ~= path then path = path .. '/' end
	path = path .. d
	if nil == Subs[path]	then Subs[path] = {files = {}, subs = {}} end
	if i < ln 		then Subs[path].subs[bits[i + 1]] = bits[i + 1] end
	Subs[path].bits = copyTable(bits, true)
	if i < ln 		then table.remove(Subs[path].bits, #bits) end
    end

    print('Parsing ' .. name .. '.md')
    local h = io.open(name  .. '.md', 'r')
-- TODO - should bail here on error?
    if nil ~= h then file.cm = h:read('*a') ; h:close() else print('oops! No such name ' .. name) end
    local body, metadata, err = lcmark.convert(file.cm, "html", {smart = true, yaml_metadata = true, columns = 0})
    if    nil == body then print('oops! ' .. err)
    elseif '' == body then
	Subs[path].metadata = metadata
	Files[name] = nil
    else
	file.metadata = metadata
	file.body = body
	table.insert(Subs[path].files, bit)
    end
end



-- These functions assume the above file and sub scan has completed.
local whichPage = function(f)
    local fl = ''
    if (nil ~= Subs[f]) then
	if 1 == #(Subs[f].files) then	fl = Subs[f].files[1] .. '.HTML' else
	    for i, v in ipairs{'README', 'readme', 'INDEX', 'index'} do
		for j, w in ipairs(Subs[f].files) do
		    if v == w then
			print('FOUND ' .. v .. '.HTML')
			fl = v .. '.HTML'
			break
		    end
		end
		if '' ~= fl then break end
	    end
	end
    end
    return fl
end


local linkFrom = function(source, dest)
    local depth = 0
    local link = ''
    if source ~= dest then
	for i, v in ipairs(Subs[source].bits) do
	    if v ~= Subs[dest].bits[i] then
		depth = i
		break
	    end
	end
	depth = #(Subs[source].bits) - depth
	depth = depth + 1
	link = string.rep('../', depth)
	if (0 == depth) or (depth > #(Subs[dest].bits)) then
	    for i, v in ipairs(Subs[dest].bits) do
		if i >= depth then
		    if '' ~= link then link = link .. '/' end
		    link = link .. Subs[dest].bits[i]
		end
	    end
	end
    end
    return link
end



for name, file in pairs(Files) do
    local path, result = '', ''
    local body, metadata = Files[name].body, Files[name].metadata
    local bits, bit = Files[name].bits, Files[name].bit
    local ln = #bits

    path = table.concat(bits, '/', 1, ln)

    if '' ~= body then
	local bod, err = lcmark.compile_template(body)
	if nil == bod then print('oops! ' .. err) else
	    local templateFile = metadata.template
	    if nil == templateFile then templateFile = 'default' end
	    file.template = templateFile .. '.template'
	end

	local pth = ''
	for i, d in ipairs(bits) do
	    if '' ~= pth then pth = pth .. '/' end
	    pth = pth .. d
	    if nil ~= Subs[pth] then
		if nil ~= Subs[pth].metadata then
		    for m, x in pairs(Subs[pth].metadata) do
			if nil == metadata[m] then
			    metadata[m] = x
			else
		    end
		 end
	    end
	end
    end

	if nil ~= Subs[''].metadata then
	    for	m, x in pairs(Subs[''].metadata)	do if nil == metadata[m] then metadata[m] = x end end
	end
	for		m, x in pairs(globalData)		do if nil == metadata[m] then metadata[m] = x end end
	for n, y in ipairs{'favicon', 'logo'} do
	    local pith = ''
	    if nil ~= metadata[y] then
		local pth = ''
		for m, x in ipairs(bits) do
		    if '' ~= pth then pth = pth .. '/' end
		    pth = pth .. x
		    if (nil ~= Subs[pth].metadata) and (nil ~= Subs[pth].metadata[y]) then pith = pth end
		end
		if ('' == pith) and (nil ~= Subs[''].metadata) and (nil ~= Subs[''].metadata[y]) then pith = pth end
		if '' == pith then	metadata[y] = linkFrom(path, pith) .. globalData[y]
		else			metadata[y] = linkFrom(path, pith) .. metadata[y]
		end
	    end
	end

	metadata.header = ''
	local pth = ''
	for i, f in pairs(Subs[path].subs) do
	    local fl = ''
	    if '' ~= path then
		if '' ~= pth then pth = pth .. '/' end
		pth = pth .. f
		fl = whichPage(pth)
	    else
		fl = whichPage(f)
	    end

--	    if '' == fl then
--		metadata.header = metadata.header .. f .. '</a> &nbsp; '
--	    else
		metadata.header = metadata.header .. '<a href="' .. f .. '/' .. fl .. '">' .. f .. '</a> &nbsp; '
--	    end
	end

	metadata.menu = ''
	if nil ~= Subs[path].files then table.sort(Subs[path].files) end
	for i, f in ipairs(Subs[path].files) do
	    if name == f then
		metadata.menu = metadata.menu .. '<p>' .. f .. '</p>'
	    else
		metadata.menu = metadata.menu .. '<p><a href="' .. f .. '.HTML">' .. f .. '</a></p>'
	    end
	end

	metadata.trail = ''
	for i, b in ipairs(bits) do
	    if i < #bits then
		metadata.trail = metadata.trail .. '<a href="' .. linkFrom(path, table.concat(bits, '/', 1, i)) .. whichPage(b) .. '">' .. b .. '</a> &nbsp; '
		linkFrom(path, table.concat(bits, '/', 1, i))
	    else
		metadata.trail = metadata.trail ..  b .. ' &nbsp; '
	    end
	end
--	if '' == metadata.trail then metadata.trail = '<a href="' .. string.rep('../', ln) .. '/' .. whichPage('') .. '">home</a> &nbsp; ' end

	if nil ~= metadata.pagehistory then metadata.history = '<p>Page&nbsp;<a href="' .. metadata.pagehistory .. '">history</a></p>' end
	if nil ~= metadata.sourcecode	then metadata.footer = '<a href="' .. metadata.sourcecode	.. '">source code</a> &nbsp; &nbsp; ' .. metadata.footer    end
	if nil ~= metadata.feedatom		then metadata.footer = '<a href="' .. metadata.feedatom		..   '">atom feed</a> &nbsp; &nbsp; ' .. metadata.footer    end
	if metadata.footer ~= globalData.footer then metadata.footer = 'Web site ' .. metadata.footer end
	metadata.footer = '<p>' .. metadata.footer .. '</p>'

	metadata.body = lcmark.apply_template(bod, metadata)

	local tm = ''
	if nil ~= file.template then
	    local h = io.open(file.template, 'r')
	    if nil ~= h then
		tm = tm .. h:read('*a')
		h:close()
	    else
		print('oops! No such file ' .. file.template)
	    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 then
	    local base = name .. '.HTML'
	    print('From ' .. name .. '.md -> ' .. 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
	end

    else
	print('')
    end
end