about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--img/amber.jpgbin0 -> 20805 bytes
-rw-r--r--lua/jttm.lua193
-rw-r--r--lua/sample-writer.lua (renamed from lua/sample.lua)0
-rw-r--r--lua/test.lua304
4 files changed, 0 insertions, 497 deletions
diff --git a/img/amber.jpg b/img/amber.jpg new file mode 100644 index 0000000..7b70b35 --- /dev/null +++ b/img/amber.jpg
Binary files differ
diff --git a/lua/jttm.lua b/lua/jttm.lua deleted file mode 100644 index 2ba101a..0000000 --- a/lua/jttm.lua +++ /dev/null
@@ -1,193 +0,0 @@
1-- Pandoc "Just the text, Ma'am"
2-- (JTTM): a custom writer that
3-- strips everything except for
4-- the TEXT from a pandoc source
5-- vim: fdm=marker
6-- invoke with: pandoc -t jttm/jttm.lua
7
8-- Table to store footnotes so they are at the END of the document
9local notes = {}
10
11-- This function is called once for the whole document. Parameters:
12-- body is a string, metadata is a table, variables is a table.
13-- One could use some kind of templating
14-- system here; this just gives you a simple standalone HTML file.
15function Doc(body, metadata, variables)
16 local buffer = {}
17 local function add(s)
18 table.insert(buffer, s)
19 end
20 if metadata['title'] and metadata['title'] ~= "" then
21 add(string.upper(metadata['title']))
22 end
23 if metadata['subtitle'] and metadata['subtitle'] ~= "" then
24 add(": " .. metadata['subtitle'])
25 end
26 add("\n")
27 -- TODO: epigraph.content, epigraph.attrib, dedication, other metadata?
28 add(body)
29 -- TODO: add notes at the end.
30 return table.concat(buffer, '\n')
31end
32
33-- TODOs {{{
34function align(align)
35 -- TODO: is this necessary?
36end
37
38function Note(s)
39 -- TODO
40end
41
42-- convert Tables to csv? or tab-separated?
43function Table(caption, aligns, widths, headers, rows)
44 local buffer = {}
45 local function add(s)
46 table.insert(buffer, s)
47 end
48 add("\n\n")
49 if caption ~= "" then
50 add("[" .. caption .. "]")
51 end
52 -- TODO: finish
53end
54
55-- }}}
56-- Remove all formatting {{{
57function Blocksep()
58 return "\n\n"
59end
60function Emph(s)
61 return s
62end
63
64function Strong(s)
65 return s
66end
67
68function Subscript(s)
69 return s
70end
71
72function Superscript(s)
73 return s
74end
75
76function SmallCaps(s)
77 return s
78end
79
80function Strikeout(s)
81 return s
82end
83
84function Code(s, attr)
85 return s
86end
87
88function CodeBlock(s, attr)
89 return s
90end
91
92function InlineMath(s)
93 return s
94end
95
96function DisplayMath(s)
97 return s
98end
99
100function Span(s, attr)
101 return s
102end
103
104function Cite(s)
105 return s
106end
107
108function Plain(s)
109 return s
110end
111
112-- Links only include the link text
113function Link(s, src, tit)
114 return s
115end
116
117-- Images have nothing to give us
118-- (but add a space just in case)
119function Image(s, src, tit)
120 return " "
121end
122
123function Str(s)
124 return s
125end
126
127function Div(s, attr)
128 return s
129end
130
131function Space(s)
132 return " "
133end
134
135function LineBreak()
136 return "\n"
137end
138
139function Para(s)
140 -- add paragraphing
141 return s .. "\n\n"
142end
143-- }}}
144-- Leave just a little formatting {{{
145function Header(lev, s, attr)
146 if lev == 1 then
147 return "\n\n " .. string.upper(s) .. "\n\n"
148 elseif lev == 2 then
149 return "\n " .. string.upper(s) .. "\n"
150 else
151 return s
152 end
153end
154
155function Blockquote(s)
156 return "\n\n" .. string.gsub(s, "*\n", " %0")
157end
158
159function HorizontalRule(s)
160 return "\n\n\n"
161end
162
163function BulletList(items)
164 local buffer = {}
165 for _, item in pairs(items) do
166 table.insert(buffer, "- " .. item .. "\n")
167 end
168 return "\n\n" .. table.concat(buffer, "\n") .. "\n\n"
169end
170
171function DefinitionList(items)
172 local buffer = {}
173 for _, item in pairs(items) do
174 for k, v in pairs(item) do
175 table.insert(buffer, "\n" .. k .. ":\n " ..
176 table.concat(v, "\n "))
177 end
178 end
179 return "\n\n" .. table.concat(buffer, "\n") .. "\n\n"
180end
181-- }}}
182
183-- The following code will produce runtime warnings when you haven't defined
184-- all of the functions you need for the custom writer, so it's useful
185-- to include when you're working on a writer.
186local meta = {}
187meta.__index =
188 function(_, key)
189 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
190 return function() return "" end
191 end
192setmetatable(_G, meta)
193
diff --git a/lua/sample.lua b/lua/sample-writer.lua index a0c3c29..a0c3c29 100644 --- a/lua/sample.lua +++ b/lua/sample-writer.lua
diff --git a/lua/test.lua b/lua/test.lua deleted file mode 100644 index bfbafc2..0000000 --- a/lua/test.lua +++ /dev/null
@@ -1,304 +0,0 @@
1-- This is a sample custom writer for pandoc. It produces output
2-- that is very similar to that of pandoc's HTML writer.
3-- There is one new feature: code blocks marked with class 'dot'
4-- are piped through graphviz and images are included in the HTML
5-- output using 'data:' URLs.
6--
7-- Invoke with: pandoc -t sample.lua
8--
9-- Note: you need not have lua installed on your system to use this
10-- custom writer. However, if you do have lua installed, you can
11-- use it to test changes to the script. 'lua sample.lua' will
12-- produce informative error messages if your code contains
13-- syntax errors.
14
15-- Character escaping
16local function escape(s, in_attribute)
17 return s:gsub("[<>&\"']",
18 function(x)
19 if x == '<' then
20 return '&lt;'
21 elseif x == '>' then
22 return '&gt;'
23 elseif x == '&' then
24 return '&amp;'
25 elseif x == '"' then
26 return '&quot;'
27 elseif x == "'" then
28 return '&#39;'
29 else
30 return x
31 end
32 end)
33end
34
35-- Helper function to convert an attributes table into
36-- a string that can be put into HTML tags.
37local function attributes(attr)
38 local attr_table = {}
39 for x,y in pairs(attr) do
40 if y and y ~= "" then
41 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
42 end
43 end
44 return table.concat(attr_table)
45end
46
47-- Run cmd on a temporary file containing inp and return result.
48local function pipe(cmd, inp)
49 local tmp = os.tmpname()
50 local tmph = io.open(tmp, "w")
51 tmph:write(inp)
52 tmph:close()
53 local outh = io.popen(cmd .. " " .. tmp,"r")
54 local result = outh:read("*all")
55 outh:close()
56 os.remove(tmp)
57 return result
58end
59
60-- Table to store footnotes, so they can be included at the end.
61local notes = {}
62
63-- Blocksep is used to separate block elements.
64function Blocksep()
65 return "\n\n"
66end
67
68-- This function is called once for the whole document. Parameters:
69-- body is a string, metadata is a table, variables is a table.
70-- One could use some kind of templating
71-- system here; this just gives you a simple standalone HTML file.
72function Doc(body, metadata, variables)
73 local buffer = {}
74 for k, v in pairs(metadata) do
75 if type(v) == "string" then
76 table.insert(buffer, string.upper(k) .. ': ' .. v)
77 else
78 table.insert(buffer, string.upper(k) .. ': ' .. table.concat(v))
79 end
80 end
81 return table.concat(buffer, "\n")
82end
83
84-- The functions that follow render corresponding pandoc elements.
85-- s is always a string, attr is always a table of attributes, and
86-- items is always an array of strings (the items in a list).
87-- Comments indicate the types of other variables.
88
89function Str(s)
90 return escape(s)
91end
92
93function Space()
94 return " "
95end
96
97function LineBreak()
98 return "<br/>"
99end
100
101function Emph(s)
102 return "<em>" .. s .. "</em>"
103end
104
105function Strong(s)
106 return "<strong>" .. s .. "</strong>"
107end
108
109function Subscript(s)
110 return "<sub>" .. s .. "</sub>"
111end
112
113function Superscript(s)
114 return "<sup>" .. s .. "</sup>"
115end
116
117function SmallCaps(s)
118 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
119end
120
121function Strikeout(s)
122 return '<del>' .. s .. '</del>'
123end
124
125function Link(s, src, tit)
126 return "<a href='" .. escape(src,true) .. "' title='" ..
127 escape(tit,true) .. "'>" .. s .. "</a>"
128end
129
130function Image(s, src, tit)
131 return "<img src='" .. escape(src,true) .. "' title='" ..
132 escape(tit,true) .. "'/>"
133end
134
135function Code(s, attr)
136 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
137end
138
139function InlineMath(s)
140 return "\\(" .. escape(s) .. "\\)"
141end
142
143function DisplayMath(s)
144 return "\\[" .. escape(s) .. "\\]"
145end
146
147function Note(s)
148 local num = #notes + 1
149 -- insert the back reference right before the final closing tag.
150 s = string.gsub(s,
151 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
152 -- add a list item with the note to the note table.
153 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
154 -- return the footnote reference, linked to the note.
155 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
156 '"><sup>' .. num .. '</sup></a>'
157end
158
159function Span(s, attr)
160 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
161end
162
163function Cite(s)
164 return "<span class=\"cite\">" .. s .. "</span>"
165end
166
167function Plain(s)
168 return s
169end
170
171function Para(s)
172 return "<p>" .. s .. "</p>"
173end
174
175-- lev is an integer, the header level.
176function Header(lev, s, attr)
177 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
178end
179
180function BlockQuote(s)
181 return "<blockquote>\n" .. s .. "\n</blockquote>"
182end
183
184function HorizontalRule()
185 return "<hr/>"
186end
187
188function CodeBlock(s, attr)
189 -- If code block has class 'dot', pipe the contents through dot
190 -- and base64, and include the base64-encoded png as a data: URL.
191 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
192 local png = pipe("base64", pipe("dot -Tpng", s))
193 return '<img src="data:image/png;base64,' .. png .. '"/>'
194 -- otherwise treat as code (one could pipe through a highlighter)
195 else
196 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
197 "</code></pre>"
198 end
199end
200
201function BulletList(items)
202 local buffer = {}
203 for _, item in pairs(items) do
204 table.insert(buffer, "<li>" .. item .. "</li>")
205 end
206 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
207end
208
209function OrderedList(items)
210 local buffer = {}
211 for _, item in pairs(items) do
212 table.insert(buffer, "<li>" .. item .. "</li>")
213 end
214 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
215end
216
217-- Revisit association list STackValue instance.
218function DefinitionList(items)
219 local buffer = {}
220 for _,item in pairs(items) do
221 for k, v in pairs(item) do
222 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
223 table.concat(v,"</dd>\n<dd>") .. "</dd>")
224 end
225 end
226 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
227end
228
229-- Convert pandoc alignment to something HTML can use.
230-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
231function html_align(align)
232 if align == 'AlignLeft' then
233 return 'left'
234 elseif align == 'AlignRight' then
235 return 'right'
236 elseif align == 'AlignCenter' then
237 return 'center'
238 else
239 return 'left'
240 end
241end
242
243-- Caption is a string, aligns is an array of strings,
244-- widths is an array of floats, headers is an array of
245-- strings, rows is an array of arrays of strings.
246function Table(caption, aligns, widths, headers, rows)
247 local buffer = {}
248 local function add(s)
249 table.insert(buffer, s)
250 end
251 add("<table>")
252 if caption ~= "" then
253 add("<caption>" .. caption .. "</caption>")
254 end
255 if widths and widths[1] ~= 0 then
256 for _, w in pairs(widths) do
257 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
258 end
259 end
260 local header_row = {}
261 local empty_header = true
262 for i, h in pairs(headers) do
263 local align = html_align(aligns[i])
264 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
265 empty_header = empty_header and h == ""
266 end
267 if empty_header then
268 head = ""
269 else
270 add('<tr class="header">')
271 for _,h in pairs(header_row) do
272 add(h)
273 end
274 add('</tr>')
275 end
276 local class = "even"
277 for _, row in pairs(rows) do
278 class = (class == "even" and "odd") or "even"
279 add('<tr class="' .. class .. '">')
280 for i,c in pairs(row) do
281 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
282 end
283 add('</tr>')
284 end
285 add('</table')
286 return table.concat(buffer,'\n')
287end
288
289function Div(s, attr)
290 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
291end
292
293-- The following code will produce runtime warnings when you haven't defined
294-- all of the functions you need for the custom writer, so it's useful
295-- to include when you're working on a writer.
296local meta = {}
297meta.__index =
298 function(_, key)
299 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
300 return function() return "" end
301 end
302setmetatable(_G, meta)
303
304