-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiki.lua
More file actions
156 lines (144 loc) · 3.26 KB
/
wiki.lua
File metadata and controls
156 lines (144 loc) · 3.26 KB
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
local c = require("common")
local pageLinks = {}
function Link(el)
if el.classes:includes("wikilink") then
normalName = c.normalize(el.target:gsub("%.html$", ""))
pageLinks[normalName] = true
el.target = normalName .. ".html"
mdName = "md-src/" .. normalName .. ".md"
if not c.exists(mdName) then
print(
string.format(
"⛔ %s is a broken link in %s",
normalName,
PANDOC_STATE.input_files[1]
)
)
end
end
return el
end
function Pandoc(doc)
local linksCount = 0
for _ in pairs(pageLinks) do
linksCount = linksCount + 1
end
if linksCount < 2 then
print(
string.format(
"🔗 %d outgoing links from page %s",
linksCount,
PANDOC_STATE.input_files[1]
)
)
end
local src = PANDOC_STATE.input_files[1]
if c.skipPages[c.normalize(src)] then
return doc
end
backlinks = c.readBacklinks()
for dest, _ in pairs(pageLinks) do
backlinks[dest] = (backlinks[dest] or 0) + 1
end
c.writeBacklinks(backlinks)
return doc
end
function Meta(meta)
if meta.title then
local t = pandoc.utils.stringify(meta.title)
meta.pagetitle = pandoc.MetaString(t .. " - AetherWiki")
end
return meta
end
local attributions -- loaded lazily
-- minimal HTML escape
local function esc(s)
if not s then
return ""
end
return (
tostring(s)
:gsub("&", "&")
:gsub("<", "<")
:gsub(">", ">")
:gsub('"', """)
)
end
local function loadAttributions()
local path = "attribution.txt"
local map = {}
local f = io.open(path, "r")
for line in f:lines() do
line = (line:gsub("\r", ""))
local s = line:match("^%s*(.-)%s*$") or ""
if s ~= "" and not s:match("^#") then
local key, val = s:match("^([^:]+):%s*(.+)$")
if key and val then
key = key:match("^%s*(.-)%s*$")
val = val:match("^%s*(.-)%s*$")
map[key] = val
else
print(
string.format(
"⛔ Bad line in attribution.txt: %s",
line,
PANDOC_STATE.input_files[1] or "?"
)
)
end
end
end
f:close()
return map
end
local function getAttributionFor(src)
if not attributions then
attributions = loadAttributions()
end
local norm = src:gsub("[?#].*$", "")
local filename = norm:match("([^/\\]+)$") or norm
local attr = attributions[filename]
if not attr or attr == "" then
print(
string.format(
"⛔ %s is missing attribution info in %s",
filename,
PANDOC_STATE.input_files[1] or "?"
)
)
return "Missing attribution info. Please contact a11ce to report this."
end
if attr == "a11ce" then
return ""
end
return attr
end
function Image(el)
if not el.classes:includes("fig-right") then
return el
end
local caption_text = pandoc.utils.stringify(el.caption)
local classes = table.concat(el.classes, " ")
local title_attr = (el.title and el.title ~= "")
and (' title="' .. esc(el.title) .. '"')
or ""
local attribution = getAttributionFor(el.src)
local html = string.format(
'<details class="zoom"><summary>'
.. '<div class="zoom-img-block">'
.. '<img src="%s" alt="%s" class="%s"%s />'
.. "</div>"
.. '<div class="zoom-info">'
.. "<figcaption>%s</figcaption>"
.. "<small>%s</small>"
.. "</div>"
.. "</summary></details>",
esc(el.src),
esc(caption_text),
esc(classes),
title_attr,
esc(caption_text),
attribution
)
return pandoc.RawInline("html", html)
end