-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.lua
129 lines (110 loc) · 3.32 KB
/
util.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
--
-- Utilities for use by other filters.
--
-- dependencies: none
-- author:
-- - name: José de Mattos Neto
-- - address: https://github.com/jzeneto
-- date: february 2018 (first version)
-- license: GPL version 3 or later
util = {}
util.putTagsOnContent = function (textContent, startTag, endTag)
table.insert(textContent, 1, pandoc.RawInline("opendocument", startTag))
table.insert(textContent, pandoc.RawInline("opendocument", endTag))
return textContent
end
util.getElementId = function (element, fallbackId)
if element.attr then
local id = element.attr.identifier or fallbackId
return id
else return fallbackId
end
end
local imageCounter = 0
util.getImageId = function (image)
imageCounter = imageCounter + 1
return util.getElementId(image, 'img' .. imageCounter)
end
local tableCounter = 0
util.getTableId = function (table)
tableCounter = tableCounter + 1
return util.getElementId(table, 'tbl' .. tableCounter)
end
util.isTableDiv = function (div)
local tableDiv = false
pandoc.walk_block(div, {
Table = function(el)
tableDiv = true
return el
end
})
return tableDiv
end
-- Required escapes.
-- Ampersand ('&') MUST be the first element added to table `escapes`.
local escapes = {}
escapes["&"] = "&"
escapes["<"] = "<"
escapes[">"] = ">"
local escPattern = "([&<>])"
util.escape = function (text)
text = string.gsub(text, escPattern, function(char)
return escapes[char]
end)
return text
end
local tags = {}
tags.emph = '<text:span text:style-name=\"Emphasis\">'
tags.strong = '<text:span text:style-name=\"Strong_20_Emphasis\">'
tags.spanEnd = '</text:span>'
tags.linkEnd = '</text:a>'
tags.lineBreak = '<text:line-break />'
tags.noteStart = '<text:note text:id=\"ftn0\" text:note-class=\"footnote\">' ..
'<text:note-citation>1</text:note-citation><text:note-body>' ..
'<text:p text:style-name=\"Footnote\">'
tags.noteEnd = '</text:p></text:note-body></text:note>'
local filters = {}
filters.str = function(str)
local content = util.escape(pandoc.utils.stringify(str))
return pandoc.Str(content)
end
filters.emph = function(emph)
local content = tags.emph .. util.escape(pandoc.utils.stringify(emph)) .. tags.spanEnd
return pandoc.Str(content)
end
filters.strong = function(strong)
local content = tags.strong .. util.escape(pandoc.utils.stringify(strong)) .. tags.spanEnd
return pandoc.Str(content)
end
filters.link = function(link)
local linkTag = '<text:a xlink:type=\"simple\" xlink:href=\"' .. util.escape(link.target) .. '\" office:name=\"\">'
local content = linkTag .. util.escape(pandoc.utils.stringify(link)) .. tags.linkEnd
return pandoc.Str(content)
end
filters.lineBreak = function(line)
return pandoc.Str(tags.lineBreak)
end
filters.note = function(note)
local noteSpan = pandoc.Span(' ')
noteSpan.c = note.c
local content = tags.noteStart .. pandoc.utils.stringify(noteSpan) .. tags.noteEnd
return pandoc.Str(content)
end
filters.div = function(div)
return div.content
end
filters.rawInline = function(raw)
return pandoc.Str(raw.text)
end
util.inlineToRaw = {
Emph = filters.emph,
Strong = filters.strong,
Str = filters.str,
Link = filters.link,
LineBreak = filters.lineBreak,
Note = filters.note,
RawInline = filters.rawInline
}
util.blockToRaw = util.inlineToRaw
util.blockToRaw.Div = filters.div
util.tags = tags