-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosis.lua
362 lines (305 loc) · 8.54 KB
/
osis.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
--
-- Invoke with: pandoc -t osis.lua
--
-- This variable lets us add at least 1 level of chapter div in a document
gotheader = false
local pipe = pandoc.pipe
local stringify = (require "pandoc.utils").stringify
-- The global variable PANDOC_DOCUMENT contains the full AST of
-- the document which is going to be written. It can be used to
-- configure the writer.
local meta = PANDOC_DOCUMENT.meta
-- Character escaping
local function escape(s, in_attribute)
return s:gsub("[<>&]",
function(x)
if x == '<' then
return '<'
elseif x == '>' then
return '>'
elseif x == '&' then
return '&'
else
return x
end
end)
end
-- Helper function to convert an attributes table into
-- a string that can be put into HTML tags.
local function attributes(attr)
local attr_table = {}
for x,y in pairs(attr) do
if y and y ~= "" then
table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
end
end
return table.concat(attr_table)
end
-- Table to store footnotes, so they can be included at the end.
local notes = {}
-- Blocksep is used to separate block elements.
function Blocksep()
return "\n\n"
end
-- This function is called once for the whole document. Parameters:
-- body is a string, metadata is a table, variables is a table.
-- This gives you a fragment. You could use the metadata table to
-- fill variables in a custom lua template. Or, pass `--template=...`
-- to pandoc, and pandoc will do the template processing as usual.
function Doc(body, metadata, variables)
local buffer = {}
local hbuffer = {}
local header = [[<?xml version="1.0" encoding="UTF-8"?>
<osis xsi:schemaLocation="http://www.bibletechnologies.net/2003/OSIS/namespace
http://www.bibletechnologies.net/osisCore.2.1.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.bibletechnologies.net/2003/OSIS/namespace">
<osisText osisRefWork="GenBook" xml:lang="en" osisIDWork="WorkID">
]]
local footer = [[</div>
</osisText>
</osis>
]]
local function add(s)
table.insert(buffer, s)
end
table.insert(hbuffer, '<header>\n<work osisWork="WorkID">')
if metadata.title then
table.insert(hbuffer, '<title>' .. metadata.title .. '</title>')
else
table.insert(hbuffer, '<title>OSISGenbook</title>')
end
if metadata.author then
table.insert(hbuffer, '<creator role="aut">' .. metadata.author .. '</creator>')
end
table.insert(hbuffer, [[</work>
<work osisWork="Bible">
<refSystem>Bible</refSystem>
</work>
</header>
<div type="book" osisID="Book">
]])
add(header)
add(table.concat(hbuffer, "\n"))
add(body)
if gotheader == true then
add("</div>")
end
add(footer)
return table.concat(buffer,'\n') .. '\n'
end
-- The functions that follow render corresponding pandoc elements.
-- s is always a string, attr is always a table of attributes, and
-- items is always an array of strings (the items in a list).
-- Comments indicate the types of other variables.
function Str(s)
return escape(s)
end
function Space()
return " "
end
function SoftBreak()
return "\n"
end
function LineBreak()
return "<lb/>\n"
end
function Emph(s)
return "<hi type='emphasis'>" .. s .. "</hi>"
end
function Strong(s)
return "<hi type='bold'>" .. s .. "</hi>"
end
function Subscript(s)
return "<hi type='sub'>" .. s .. "</hi>"
end
function Superscript(s)
return "<hi type='super'>" .. s .. "</hi>"
end
function SmallCaps(s)
return '<hi type="small-caps">' .. s .. '</hi>'
end
function Strikeout(s)
return '<hi type="line-through">' .. s .. '</hi>'
end
function Underline(s)
return '<hi type="underline">' .. s .. '</hi>'
end
function Link(s, src, tit, attr)
return "<a href='" .. escape(src,true) .. "' >" .. s .. "</a>"
end
function Image(s, src, tit, attr)
return "<figure src='" .. escape(src,true) .. "' alt='" ..
escape(tit,true) .. "'/>"
end
function Code(s, attr)
return ''
end
function InlineMath(s)
return ''
end
function DisplayMath(s)
return ''
end
function SingleQuoted(s)
return "‘" .. s .. "’"
end
function DoubleQuoted(s)
return "“" .. s .. "”"
end
function Note(s)
return '<note type="x-footnote">' .. s .. '</note>'
end
function Span(s, attr)
local attrib = attributes(attr)
if attrib ~= "" then
return "<!-- span " .. attrib .. " -->" .. s .. "<!-- end span -->"
else
return s
end
end
function RawInline(format, str)
return ''
end
function Cite(s, cs)
-- local ids = {}
-- for _,cit in ipairs(cs) do
-- table.insert(ids, cit.citationId)
-- end
-- return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
-- "\">" .. s .. "</span>"
return s
end
function Plain(s)
return s
end
function Para(s)
return "<p>" .. s .. "</p>"
end
-- lev is an integer, the header level.
function Header(lev, s, attr)
if lev == 1 then
local oid = s
oid = string.gsub(oid, '</?hi[^>]*>', '') -- remove hi tags from osisID
oid = string.gsub(oid, '</?a[^>]*>', '') -- remove links from osisID
oid = string.gsub(oid, '[^%w ]', '') -- remove non-alphanumeric chars from osisID
if gotheader == true then
return '</div>\n<div type="chapter" osisID="' .. oid .. '">\n<title level="' .. lev .. '" ID="' .. attr.id .. '" type="main">' .. s .. '</title>'
else
gotheader = true
return '<div type="chapter" osisID="' .. oid .. '">\n<title level="' .. lev .. '" type="main">' .. s .. '</title>'
end
else
return '<title level="' .. lev .. '" ID="' .. attr.id .. '" type="main">' .. s .. '</title>'
end
end
function BlockQuote(s)
return '<q type="block">\n' .. s .. '\n</q>'
end
function HorizontalRule()
return '<milestone type="line" />'
end
function LineBlock(ls)
local buffer = {}
for _, item in pairs(ls) do
if item == "" then
table.insert(buffer, "<lb />")
else
table.insert(buffer, "<l>" .. item .. "</l>")
end
end
return '<lg>\n' .. table.concat(buffer, '\n') .. '\n</lg>'
end
function CodeBlock(s, attr)
return ''
end
function BulletList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "<item>" .. item .. "</item>")
end
return '<list type="x-unordered">\n' .. table.concat(buffer, '\n') .. '\n</list>'
end
function OrderedList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "<item>" .. item .. "</item>")
end
return '<list type="x-ordered">\n' .. table.concat(buffer, '\n') .. '\n</list>'
end
function DefinitionList(items)
local buffer = {}
for _,item in pairs(items) do
local k, v = next(item)
table.insert(buffer, "<item><hi type='bold'>" .. k .. "</hi></item>\n<list><item>" ..
table.concat(v, "</item>\n<item>") .. "</item></list>")
end
return '<list type="x-definition">\n' .. table.concat(buffer, '\n') .. '\n</list>'
end
function CaptionedImage(src, tit, caption, attr)
return '<figure src="' .. escape(src,true) .. '"/>\n' ..
'<p>' .. caption .. '</p>\n'
end
-- Caption is a string, aligns is an array of strings,
-- widths is an array of floats, headers is an array of
-- strings, rows is an array of arrays of strings.
function Table(caption, aligns, widths, headers, rows)
local buffer = {}
local function add(s)
table.insert(buffer, s)
end
add("<table>")
if caption ~= "" then
add("<head>" .. caption .. "</head>")
end
if widths and widths[1] ~= 0 then
for _, w in pairs(widths) do
add('<cell />')
end
end
local header_row = {}
local empty_header = true
for i, h in pairs(headers) do
table.insert(header_row,'<cell role="label">' .. h .. '</cell>')
empty_header = empty_header and h == ""
end
if empty_header then
head = ""
else
add('<row>')
for _,h in pairs(header_row) do
add(h)
end
add('</row>')
end
for _, row in pairs(rows) do
add('<row>')
for i,c in pairs(row) do
add('<cell>' .. c .. '</cell>')
end
add('</row>')
end
add('</table>')
return table.concat(buffer,'\n')
end
function RawBlock(format, str)
return ''
end
function Div(s, attr)
local attrib = attributes(attr)
if attrib ~= "" then
return "<!-- div " .. attrib .. " -->\n" .. s .. "\n<!-- div end -->\n"
else
return s
end
end
-- The following code will produce runtime warnings when you haven't defined
-- all of the functions you need for the custom writer, so it's useful
-- to include when you're working on a writer.
local meta = {}
meta.__index =
function(_, key)
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
return function() return "" end
end
setmetatable(_G, meta)