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

local lcmark = require("lcmark")

local globalData = {version = '-0.1', header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'}
local site = {}
local dirs = {}
local createHTML = function(cm, file)
--    cm = string.gsub(cm, '.  ', '.  ')
    if nil ~= file then print('\nAbout to parse file ' .. 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 = dirs['']
		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 = dirs[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
		    for m, x in pairs(dir.files) do
			local u = string.sub(x, 1 + #pdr)
			    metadata.menu = metadata.menu .. '<p><a href="' .. u .. '.HTML">' .. m .. '</a></p>\n'
		    end
		end
		if nil ~= dir.subs then
		    for m, x in pairs(dir.subs) do
			local u = string.sub(x, 1 + #pdr)
			metadata.header = metadata.header .. '<a href="' .. u .. '">' .. 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('Creating file ' .. 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
    end
    return result
end



for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() 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 ~= dirs[dir] then
		subs  = dirs[dir].subs
		files = dirs[dir].files
	    end
	end

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

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

    local base = string.gsub(string.sub(l, c, -1), '%.md$', '')
    if nil ~= dirs[parent] then dirs[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
	dirs[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
    dirs[dir] = {files = files, subs = subs}
end

for k, v in pairs(dirs) 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