-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_processors.lua
More file actions
120 lines (90 loc) · 2.61 KB
/
inline_processors.lua
File metadata and controls
120 lines (90 loc) · 2.61 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
local element = require 'element'
local inline_processors = {}
function run_inline_code(silt, children, s)
local text_node_start = 1
while true do
local start, next, match = string.find(s, "(`+)", text_node_start)
if match == nil then break end
local end_, _, _ = string.find(s, match, next + 1, true)
if end_ == nil then break end
local first = s:sub(text_node_start, start-1)
local code = s:sub(next + 1, end_ - 1)
code = code:gsub('<', '<')
code = code:gsub('>', '>')
if #first > 0 then table.insert(children, first) end
table.insert(children, element.from { tag = 'code', children = { code } } )
text_node_start = end_ + #match
end
local first = s:sub(text_node_start)
if #first > 0 then table.insert(children, first) end
end
function run_inline_link(silt, children, s)
local search_start = 1
while true do
local before, content, href, end_ = find_link_pattern(s, search_start)
if not before then break end
local is_image = false
if (before > 0 and s:sub(before, before) == '!') then
is_image = true
before = before - 1
end
local first = s:sub(search_start, before)
if #first > 0 then table.insert(children, unescape(first)) end
content = unescape(content)
if is_image then
table.insert(children, element.from {
tag = 'img',
attr = { src = href, alt = content },
children = { }
})
else
table.insert(children, element.from {
tag = 'a',
attr = { href = href },
children = { content }
})
end
search_start = end_
end
local first = s:sub(search_start)
if #first > 0 then table.insert(children, first) end
end
function run_inline_emphasis(silt, children, s)
local search_start = 1
while true do
local before, tag, content, next = find_emphasis_pattern(s, search_start)
if not tag then break end
local first = s:sub(search_start, before)
if #first > 0 then table.insert(children, unescape(first)) end
content = unescape(content)
if #tag == 1 then
table.insert(children, element.from {
tag = 'em',
children = { content }
})
elseif #tag == 2 then
table.insert(children, element.from {
tag = 'strong',
children = { content }
})
elseif #tag == 3 then
table.insert(children, element.from {
tag = 'strong',
children = {
element.from { tag='em', children={ content } }
}
})
end
search_start = next
end
local first = s:sub(search_start)
if #first > 0 then table.insert(children, first) end
end
function inline_processors.init(silt, config)
silt.default_inline_processors = {
run_inline_code,
run_inline_link,
run_inline_emphasis
}
end
return inline_processors