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

local lcmark = require("lcmark")

local globalData = {header = '', footer = '', menu = '', ['_'] = ' '}
local site = {}


local createHTML = function(cm, file)
--    cm = string.gsub(cm, '.  ', '.  ')
    if nil ~= file then print('About 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
	    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 s = {}
    local f = ''
    local c = 1
    for p in string.gmatch(l, '(%w+)/') do
        table.insert(s, p)
        c = c + #p + 1
    end
    local base = string.gsub(string.sub(l, c, -1), '%.md$', '')
    table.insert(s, base)

-- TODO - should do the same for header.md and footer.md
    if 'menu' == base then
	local h = io.open(l, 'r')
	if nil ~= h then
	    globalData.menuFound = true
	    print('Found ' .. l)
	    globalData.menu = string.gsub(createHTML(h:read('*a')), 'ul>', 'menu>')
	    h:close()
	end
    else
	if 1 ~= #s then site[l] = s else site[l] = base end
    end
end

for k, v in pairs(site) do
    if 'string' == type(v) then
	if not globalData.menuFound then
	    local m = '[' .. v .. '](' .. v .. '.HTML)\n'
	    globalData.menu = globalData.menu .. string.gsub(createHTML(m), 'ul>', 'menu>')
	end
    elseif not globalData.headerFound then
	local path = ''
	for i, w in ipairs(v) do
	    path = path .. w .. '/'
	end
	globalData.header = globalData.header .. '<a href="' .. string.sub(path, 1, -2) .. '.HTML">' .. v[1] .. '</a> &nbsp; '
    end
end


local cm, filename = '', ''

if 0 == #arg then
    for k, v in pairs(site) do
	local path = ''
	if 'string' == type(v) then
	    path = v
	elseif not globalData.headerFound then
	    for i, w in ipairs(v) do
		path = path .. w .. '/'
	    end
	    path = string.sub(path, 1, -2)
	end
	path = path .. '.md'

	cm = ''
	local h = io.open(path, 'r')
	if nil ~= h then
	    createHTML(h:read('*a'), path)
	    h:close()
	else
	    print('oops! No such file ' .. path)
	end
    end
else
    for i,a in ipairs(arg) do
	if filename == '' then filename = a end
        local h = io.open(a, 'r')
	if nil ~= h then
	    cm = cm .. h:read('*a')
	    h:close()
	else
	    print('oops! No such file ' .. a)
	end
    end
    if filename == '' then filename = 'test.md' end
    createHTML(cm, filename)
end