aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/notYetAnotherWiki.lua
diff options
context:
space:
mode:
authordvs12024-12-21 17:56:41 +1000
committerdvs12024-12-21 17:56:41 +1000
commit2adacbe580ae8ce68d8e48add43a812e6ae8b759 (patch)
tree0a47449c6e05c8d30db2215d9c53447b9178f936 /notYetAnotherWiki.lua
parentMinor doc tweaks. (diff)
downloadnotYetAnotherWiki-2adacbe580ae8ce68d8e48add43a812e6ae8b759.zip
notYetAnotherWiki-2adacbe580ae8ce68d8e48add43a812e6ae8b759.tar.gz
notYetAnotherWiki-2adacbe580ae8ce68d8e48add43a812e6ae8b759.tar.bz2
notYetAnotherWiki-2adacbe580ae8ce68d8e48add43a812e6ae8b759.tar.xz
Major rewrite, mostly of the directory scanning code.
It can now populate the links around the edges, and turn an entire directory structure into a web site.
Diffstat (limited to 'notYetAnotherWiki.lua')
-rwxr-xr-xnotYetAnotherWiki.lua170
1 files changed, 101 insertions, 69 deletions
diff --git a/notYetAnotherWiki.lua b/notYetAnotherWiki.lua
index 1746b0a..9d6f3f7 100755
--- a/notYetAnotherWiki.lua
+++ b/notYetAnotherWiki.lua
@@ -2,13 +2,12 @@
2 2
3local lcmark = require("lcmark") 3local lcmark = require("lcmark")
4 4
5local globalData = {header = '', footer = '', menu = '', ['_'] = ' '} 5local globalData = {header = '', footer = '', menu = '', ['_'] = ' ', ['dlr'] = '$'}
6local site = {} 6local site = {}
7 7local dirs = {}
8
9local createHTML = function(cm, file) 8local createHTML = function(cm, file)
10-- cm = string.gsub(cm, '. ', '.  ') 9-- cm = string.gsub(cm, '. ', '.  ')
11 if nil ~= file then print('About to parse file ' .. file) end 10 if nil ~= file then print('\nAbout to parse file ' .. file) end
12 local result = '' 11 local result = ''
13 local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0}) 12 local body, metadata, err = lcmark.convert(cm, "html", {smart = true, yaml_metadata = true, columns = 0})
14 13
@@ -25,11 +24,62 @@ local createHTML = function(cm, file)
25 if nill == metadata[k] then metadata[k] = v else print('metadata already has ' .. k) end 24 if nill == metadata[k] then metadata[k] = v else print('metadata already has ' .. k) end
26 end 25 end
27 end 26 end
27
28 if nil ~= file then
29 local depth = 0
30 local base = ''
31 for p in string.gmatch(file, '(%w+)/') do
32 depth = depth + 1
33 base = p
34 end
35 local path = string.sub(file, 1, #base)
36
37 metadata.header = ''
38 metadata.menu = ''
39 local trl = {}
40 metadata.trail = ''
41 local dir = dirs['']
42 local dr, pdr, tr = '', '', ''
43 while nil ~= dir.subs do
44 local old = dir
45 for m, x in pairs(dir.subs) do
46 if x == string.sub(file, 1, #x) then
47 pdr = pdr .. '/' .. m
48 tr = tr .. '../'
49 dr = m
50 dir = dirs[dr]
51 table.insert(trl, '">' .. m .. '</a> &#x1f463;')
52 break
53 end
54 end
55 if old == dir then break end
56 end
57 table.remove(trl)
58 for m, x in ipairs(trl) do
59 tr = string.sub(tr, 4)
60 trl[m] = '&nbsp;<a href="' .. tr .. x
61 end
62 metadata.trail = table.concat(trl)
63
64 if nil ~= dir.files then
65 for m, x in pairs(dir.files) do
66 local u = string.sub(x, 1 + #pdr)
67 metadata.menu = metadata.menu .. '<p><a href="' .. u .. '.HTML">' .. m .. '</a></p>\n'
68 end
69 end
70 if nil ~= dir.subs then
71 for m, x in pairs(dir.subs) do
72 local u = string.sub(x, 1 + #pdr)
73 metadata.header = metadata.header .. '<a href="' .. u .. '">' .. m .. '</a> &nbsp; '
74 end
75 end
76 end
77
28 metadata.body = lcmark.apply_template(bod, metadata) 78 metadata.body = lcmark.apply_template(bod, metadata)
29 79
30 local tm = '' 80 local tm = ''
31 if nil ~= templateFile then 81 if nil ~= templateFile then
32 local h = io.open(templateFile, 'r') 82 local h = io.open(templateFile, 'r')
33 if nil ~= h then 83 if nil ~= h then
34 tm = tm .. h:read('*a') 84 tm = tm .. h:read('*a')
35 h:close() 85 h:close()
@@ -60,82 +110,64 @@ local createHTML = function(cm, file)
60end 110end
61 111
62 112
113
63for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do 114for l in io.popen('find . -name "*.md" -type f -printf "%P\n"'):lines() do
64 local s = {} 115 local dir = ''
65 local f = '' 116 local files, subs = {}, {}
66 local c = 1 117 local c, parent = 1, ''
118
67 for p in string.gmatch(l, '(%w+)/') do 119 for p in string.gmatch(l, '(%w+)/') do
68 table.insert(s, p) 120 if '' == dir then
121 dir = p
122 if nil ~= dirs[dir] then
123 subs = dirs[dir].subs
124 files = dirs[dir].files
125 end
126 end
127
128 local path = string.sub(l, 1, -1 - #(string.gsub(l, '.*/', '')))
129 if nil ~= dirs[parent] then
130 if nil == dirs[parent].subs then dirs[parent].subs = {} end
131 dirs[parent].subs[p] = path
132 elseif 1 == c then subs[p] = path
133 else
134 if ('' ~= parent) and (dir == parent) then subs[p] = path end
135 dirs[parent] = {subs = {[p] = path}}
136 end
69 c = c + #p + 1 137 c = c + #p + 1
138 parent = p
139 end
140
141 if (1 == c) and (nil ~= dirs[dir]) then
142 subs = dirs[dir].subs
143 files = dirs[dir].files
70 end 144 end
145
71 local base = string.gsub(string.sub(l, c, -1), '%.md$', '') 146 local base = string.gsub(string.sub(l, c, -1), '%.md$', '')
72 table.insert(s, base) 147 if nil ~= dirs[parent] then dirs[parent].files[base] = string.sub(l, 1, -4)
73 148 elseif 1 == c then files[base] = string.sub(l, 1, -4)
74-- TODO - should do the same for header.md and footer.md
75 if 'menu' == base then
76 local h = io.open(l, 'r')
77 if nil ~= h then
78 globalData.menuFound = true
79 print('Found ' .. l)
80 globalData.menu = string.gsub(createHTML(h:read('*a')), 'ul>', 'menu>')
81 h:close()
82 end
83 else 149 else
84 if 1 ~= #s then site[l] = s else site[l] = base end 150 if ('' ~= parent) and (dir == parent) then files[base] = string.sub(l, 1, -4) end
151 dirs[parent] = {files = {[base] = string.sub(l, 1, -4)}}
85 end 152 end
86end
87 153
88for k, v in pairs(site) do 154-- FIXME - still some minor bug somewhere, this fixes that, but causes other problems. Meh, I can live with excess empty subs tables.
89 if 'string' == type(v) then 155-- if (nil ~= subs) and (0 == #subs) then subs = nil end
90 if not globalData.menuFound then 156 dirs[dir] = {files = files, subs = subs}
91 local m = '[' .. v .. '](' .. v .. '.HTML)\n'
92 globalData.menu = globalData.menu .. string.gsub(createHTML(m), 'ul>', 'menu>')
93 end
94 elseif not globalData.headerFound then
95 local path = ''
96 for i, w in ipairs(v) do
97 path = path .. w .. '/'
98 end
99 globalData.header = globalData.header .. '<a href="' .. string.sub(path, 1, -2) .. '.HTML">' .. v[1] .. '</a> &nbsp; '
100 end
101end 157end
102 158
103 159for k, v in pairs(dirs) do
104local cm, filename = '', '' 160 if nil ~= v.files then
105 161 for m, x in pairs(v.files) do
106if 0 == #arg then 162 local file = x .. '.md'
107 for k, v in pairs(site) do 163 local h = io.open(file, 'r')
108 local path = '' 164 if nil ~= h then
109 if 'string' == type(v) then 165 createHTML(h:read('*a'), file)
110 path = v 166 h:close()
111 elseif not globalData.headerFound then 167 else
112 for i, w in ipairs(v) do 168 print('oops! No such file ' .. file)
113 path = path .. w .. '/'
114 end 169 end
115 path = string.sub(path, 1, -2)
116 end
117 path = path .. '.md'
118 170
119 cm = ''
120 local h = io.open(path, 'r')
121 if nil ~= h then
122 createHTML(h:read('*a'), path)
123 h:close()
124 else
125 print('oops! No such file ' .. path)
126 end
127 end
128else
129 for i,a in ipairs(arg) do
130 if filename == '' then filename = a end
131 local h = io.open(a, 'r')
132 if nil ~= h then
133 cm = cm .. h:read('*a')
134 h:close()
135 else
136 print('oops! No such file ' .. a)
137 end 171 end
138 end 172 end
139 if filename == '' then filename = 'test.md' end
140 createHTML(cm, filename)
141end 173end