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

--[[ TODOs
Contstruct metadata.webtrail in createHTML from links to index.HTML in the various bits of the path of the input file.

Construct a default set of menus if menu.md / header.md doesn't exist in each directory.
Same for footer.md I guess.

For such fragments, rename their results to footer.FRAGMENT.   metadata.isFragment

BUG - sub directories get their links screwed with extra path.

Check the timestamps on the files, only update if source is newer than destination.  Meh, it's already 600 times faster than the pandoc version.

Add atom feed and history for single page.  Alas cgit only seems to have ATOM feed and commit log on the whole repo, not individual files.
]]

local lcmark = require("lcmark")

local createHTML = function(cm, file, menu)
--    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
	    metadata['_'] = ' '
	    if nil == templateFile then templateFile = 'default' end
	    templateFile = templateFile .. '.template'
	    if nil == menu then
		templateFile = nil
	    else
		metadata.menu = menu
	    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


local cm = ''
local filename = ''
local menu = ''


local h = io.open('menu.md', 'r')
if nil ~= h then
    print('Found menu.md')
    menu = createHTML(h:read('*a'))
    h:close()
    menu = string.gsub(menu, 'ul>', 'menu>')
end


if 0 ~= #arg then
    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, menu)
else
    local sticks = io.popen('find . -name "*.md" -type f -printf "%P\n"')
    for  l in sticks:lines() do
	cm = ''
	local h = io.open(l, 'r')
	if nil ~= h then
	    createHTML(h:read('*a'), l, menu)
	    h:close()
	else
	    print('oops! No such file ' .. l)
	end
    end
end