aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/notYetAnotherWiki.lua
blob: 0ee0f1049408afa30743ef12192963a7f8e10dd4 (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
#!/usr/bin/env luajit

local lcmark = require("lcmark")

local globalData = {version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'}
local site = {}

local createHTML = function(cm, file)
--    cm = string.gsub(cm, '.  ', '.  ')
    if (nil ~= file) and ('' ~= file) then io.write('Parsing ' .. file .. ' -> ') end
    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 k, v in pairs(globalData) do
		    if nill == metadata[k] then metadata[k] = v else print('metadata already has ' .. k) end
		end
	    end

	    if nil ~= file then
		local depth = 0
		local base = ''
		for p in string.gmatch(file, '(%w+)/') do
		    depth = depth + 1
		    base = p
		end
		local path = string.sub(file, 1, #base)

		metadata.header = ''
		metadata.menu = ''
		local trl = {}
		metadata.trail = ''
		local dir = site['']
		local dr, pdr, tr = '', '', ''
		while nil ~= dir.subs do
		    local old = dir
		    for m, x in pairs(dir.subs) do
			if x == string.sub(file, 1, #x) then
			    pdr = pdr .. '/' .. m
			    tr = tr .. '../'
			    dr = m
			    dir = site[dr]
			    table.insert(trl, '">' .. m .. '</a> &#x1f463;')
			    break
			end
		    end
		    if old == dir then break end
		end
		table.remove(trl)
		for m, x in ipairs(trl) do
		    tr = string.sub(tr, 4)
		    trl[m] = '&nbsp;<a href="' .. tr .. x
		end
		metadata.trail = table.concat(trl)

		if nil ~= dir.files then
		    local l = {}
		    for m, x in pairs(dir.files) do table.insert(l, {m = m; x = x}) end
		    table.sort(l, function(a, b) return (a.m <= b.m)  end)
		    for m, x in ipairs(l) do
			metadata.menu = metadata.menu .. '<p><a href="' .. string.sub(x.x, 1 + #pdr) .. '.HTML">' .. x.m .. '</a></p>\n'
		    end
		end
		if nil ~= dir.subs then
		    local l = {}
		    for m, x in pairs(dir.subs) do table.insert(l, {m = m; x = x}) end
		    table.sort(l, function(a, b) return (a.m <= b.m)  end)
		    for m, x in pairs(l) do
			metadata.header = metadata.header .. '<a href="' .. string.sub(x.x, 1 + #pdr) .. '">' .. x.m .. '</a> &nbsp; '
		    end
		end
	    end

	    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
	end
    end

    if ('' ~= result) and (nil ~= file) then
	local base = string.gsub(file, '%.md$', '')
	print(base .. '.HTML')
	local a, e = io.open(base .. '.HTML', 'w')
	if nil == a then print('Could not open ' .. base .. '.HTML - ' .. e) else
	    a:write(result)
	    a:close()
	end
    else
	print('')
    end
    return result
end

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 = ''
    local files, subs = {}, {}
    local c, parent = 1, ''

    for p in string.gmatch(l, '(%w+)/') do
	if '' == dir then
	    dir = p
	    if nil ~= site[dir] then
		subs  = site[dir].subs
		files = site[dir].files
	    end
	end

	local path = string.sub(l, 1, -1 - #(string.gsub(l, '.*/', '')))
	if nil ~= site[parent] then
	    if nil == site[parent].subs then site[parent].subs = {} end
	    site[parent].subs[p] = path
	elseif 1 == c then	    subs[p] = path
	else
	    if ('' ~= parent) and (dir == parent) then subs[p] = path end
	    site[parent] = {subs = {[p] = path}}
	end
        c = c + #p + 1
        parent = p
    end

    if (1 == c) and (nil ~= site[dir]) then
	subs  = site[dir].subs
	files = site[dir].files
    end

    local base = string.gsub(string.sub(l, c, -1), '%.md$', '')
    if nil ~= site[parent] then site[parent].files[base] = string.sub(l, 1, -4)
    elseif 1 == c then	    files[base] = string.sub(l, 1, -4)
    else
	if ('' ~= parent) and (dir == parent) then files[base] = string.sub(l, 1, -4) end
	site[parent] = {files = {[base] = string.sub(l, 1, -4)}}
    end

-- FIXME - still some minor bug somewhere, this fixes that, but causes other problems.  Meh, I can live with excess empty subs tables.
--    if (nil ~= subs) and (0 == #subs) then subs = nil end
    site[dir] = {files = files, subs = subs}
end

for k, v in pairs(site) do
    if nil ~= v.files then
	for m, x in pairs(v.files) do
	    local file = x .. '.md'
	    local h = io.open(file, 'r')
	    if nil ~= h then
		createHTML(h:read('*a'), file)
		h:close()
	    else
		print('oops! No such file ' .. file)
	    end

	end
    end
end