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
|
local pipe = pandoc.pipe
local stringify = (require 'pandoc.utils').stringify
local meta_tools = require("tools/meta_tools")
-- Load meta from global if available
-- otherwise we will load it from Writer method
local meta = nil
if PANDOC_DOCUMENT then
meta = PANDOC_DOCUMENT.meta
end
local preview = ""
local long_preview = ""
local internal_links = {}
local max_string_length = 100
local max_long_string_length = 500
local function append_str(buf, s, max_length)
if (#buf + #s) < max_length then
buf = buf .. s
end
return buf
end
local function item_in_table(table, item)
local inside = false
for i,v in ipairs(table) do
if v == item then
inside = true
break
end
end
return inside
end
local function get_filename(file)
return file:match("^.+/(.+)$")
end
local function get_input_file()
local file = PANDOC_STATE.input_files[1]
file = get_filename(file)
file = string.gsub(file, ".md", "")
return file
end
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function Doc(body, metadata, variables)
local input_file = get_input_file()
-- Write out link file for table
for i,v in ipairs(internal_links) do
local markdown_file = "./notes/" .. v .. ".md"
local link_file = "./build/" .. v .. ".links"
-- check if markdown version of the file exists
if meta_tools.file_exists(markdown_file) then
links = meta_tools.read_link_file(link_file)
if not item_in_table(links, input_file) then
table.insert(links, input_file)
end
meta_tools.write_link_file(link_file, links)
else
io.stderr:write(string.format("Linking to non-existant file '%s'\n", v))
end
end
local values = {}
local output = ""
for k,v in pairs(meta) do
values[k] = stringify(v)
end
values["preview"] = preview
values["long_preview"] = long_preview
for k,v in pairs(values) do
output = output .. string.format("%s,%s\n", k, v)
end
return output
end
function Header(s)
preview = append_str(preview, " - ", max_string_length)
long_preview = append_str(long_preview, " - ", max_long_string_length)
return ""
end
function Str(s)
preview = append_str(preview, s, max_string_length)
long_preview = append_str(long_preview, s, max_long_string_length)
return ""
end
function Space()
preview = append_str(preview, " ", max_long_string_length)
long_preview = append_str(long_preview, " ", max_long_string_length)
return ""
end
function SoftBreak()
return Space()
end
function LineBreak()
return Space()
end
function Link(s, tgt, tit, attr)
if not string.find(tgt, "://") then
-- Check if link is already found elsewhere in document
local value_found = false
for index, value in ipairs(internal_links) do
if value == tgt then value_found = true end
end
if not value_found then
table.insert(internal_links, tgt)
end
end
return ""
end
function Writer(doc, opts)
meta = doc.meta
return pandoc.write_classic(doc, opts)
end
-- Ignore functions we haven't implemented as we don't need them
local meta = {}
meta.__index =
function(_, key)
return function() return '' end
end
setmetatable(_G, meta)
|