-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.lua
94 lines (86 loc) · 2.68 KB
/
filter.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
-- Declare variables
local title = {}
local path
local binder = "https://mybinder.org/v2/gh/aerospike-examples/interactive-notebooks/main?filepath="
local fname = PANDOC_STATE.input_files[1]
--Split string at delimiter
function Split(s, delimiter)
local result = {}
for match in string.gmatch(s, "([^"..delimiter.."]+)") do
table.insert(result, match)
end
return result
end
--get level 1 header info and populate title table
function Header(el)
content_str = pandoc.utils.stringify(el.content)
if el.level == 1 then
table.insert(title, content_str)
end
--Returns headers with original content. Fixes problem with printing headers twice.
return pandoc.Header(el.level, el.content)
end
--Format document yaml metadata block
function Meta(m)
local id = Split(fname, ".")
--get language from jupyter metadata
local lang = m.jupyter.kernelspec.language
path = binder .. lang .. '/' .. fname
m.id = id[1]
m.title = title[1]
--remove jupyter metadata
m.jupyter = nil
return m
end
--Removes TOC, converts code output to BlockQuote and removes extra div tags
function Div (elem)
local output = {}
--Remove TOC
if elem.attributes['toc'] == 'true' then
elem.content = {}
--Convert output to BlockQuote
--Match div class to "output" and ensure content is a CodeBLock
elseif elem.classes[1]:match('output') and elem.content[1].t == 'CodeBlock' then
--Split CodeBlock into a table of strings to preserve line breaks
local code = Split(elem.content[1].text, "\n")
for i, str in ipairs(code) do
table.insert(output, pandoc.Str(str))
end
--Create LineBLock to feed into BlockQuote
elem.content = pandoc.BlockQuote(pandoc.LineBlock(output))
end
return elem.content
end
--[[ Still working on this stuff
function Plain (elem)
local grab = false
local arr = {}
for i, item in ipairs(elem.content) do
if item.t == 'RawInline' and item.text == '</pre>' then
grab = false
end
if grab then
table.insert(arr, item)
end
if item.t == 'RawInline' and item.text == '<pre>' then
grab = true
end
end
local code = pandoc.utils.stringify(arr)
local el = pandoc.utils.stringify(elem.content)
if code ~= '' then
print(code)
end
end
Replaces inline <pre> tags with inline code
function RawInline (raw)
if raw.format == 'html' and raw.text == '<pre>' or raw.text == '</pre>' then
return pandoc.RawInline('markdown', '\n```\n')
end
end
--Replaces block <pre> tags with code blocks
function RawBlock (raw)
if raw.format == 'html' then
return pandoc.CodeBlock(raw.text)
end
end ]]--