diff options
Diffstat (limited to 'lua')
-rw-r--r-- | lua/allwords.lua | 17 | ||||
-rw-r--r-- | lua/river.lua | 228 | ||||
-rw-r--r-- | lua/sample-writer.lua | 324 |
3 files changed, 0 insertions, 569 deletions
diff --git a/lua/allwords.lua b/lua/allwords.lua deleted file mode 100644 index b87f08a..0000000 --- a/lua/allwords.lua +++ /dev/null | |||
@@ -1,17 +0,0 @@ | |||
1 | function allwords () | ||
2 | local line = io.read() | ||
3 | local pos = 1 | ||
4 | return function () | ||
5 | while line do | ||
6 | local s, e = string.find(line, "%w+", pos) | ||
7 | if s then | ||
8 | pos = e + 1 | ||
9 | return string.sub(line, s, e) | ||
10 | else | ||
11 | line = io.read() | ||
12 | pos = 1 | ||
13 | end | ||
14 | end | ||
15 | return nil | ||
16 | end | ||
17 | end | ||
diff --git a/lua/river.lua b/lua/river.lua deleted file mode 100644 index cd867af..0000000 --- a/lua/river.lua +++ /dev/null | |||
@@ -1,228 +0,0 @@ | |||
1 | -- Pandoc River writer | ||
2 | -- it takes out all formatting, leaving only a river of text | ||
3 | -- running down the page: one word per line | ||
4 | -- vim: fdm=marker | ||
5 | -- invoke with: pandoc -t river.lua | ||
6 | |||
7 | os.setlocale("en_US.UTF-8") | ||
8 | |||
9 | local function flow(s) | ||
10 | return s:gsub("%s+", "\n") | ||
11 | end | ||
12 | |||
13 | local function nude(s) | ||
14 | -- Expand contractions | ||
15 | s = s:gsub("'%a+%s", function (x) | ||
16 | if x == "'ll" then | ||
17 | return " will " | ||
18 | elseif x == "'ve" then | ||
19 | return " have " | ||
20 | elseif x == "'re" then | ||
21 | return " are " | ||
22 | else | ||
23 | return x | ||
24 | end | ||
25 | end) | ||
26 | -- Get rid of quotes around words | ||
27 | s = s:gsub('"', ' ') | ||
28 | s = s:gsub("%s+'", ' ') | ||
29 | s = s:gsub("'%s+", ' ') | ||
30 | -- Remove HTML entities | ||
31 | s = s:gsub('&.-;', ' ') | ||
32 | s = s:gsub('%b<>', ' ') | ||
33 | -- Remove end-of-line backslashes | ||
34 | s = s:gsub('%s+\\$', ' ') | ||
35 | -- Remove dashes (not hyphens) | ||
36 | s = s:gsub('%-%-+', ' ') | ||
37 | s = s:gsub('%-%s', ' ') | ||
38 | -- Remove everything that is not letters or numbers | ||
39 | s = s:gsub('[%.!%?:;,%[%]%(%)<>]', ' ') | ||
40 | -- Remove extra spaces | ||
41 | s = s:gsub('%s+', ' ') | ||
42 | return s:lower() | ||
43 | end | ||
44 | |||
45 | -- This function is called once for the whole document. Parameters: | ||
46 | -- body is a string, metadata is a table, variables is a table. | ||
47 | -- One could use some kind of templating | ||
48 | -- system here; this just gives you a simple standalone HTML file. | ||
49 | function Doc(body, metadata, variables) | ||
50 | local buffer = "" | ||
51 | local function add(s) | ||
52 | buffer = buffer .. nude(s) .. "\n" | ||
53 | end | ||
54 | if metadata['title'] then | ||
55 | add(metadata['title']) | ||
56 | end | ||
57 | if metadata['subtitle'] then | ||
58 | add(metadata['subtitle']) | ||
59 | end | ||
60 | -- TODO: epigraph.content, epigraph.attrib, dedication, other metadata? | ||
61 | add(body) | ||
62 | return flow(buffer) | ||
63 | end | ||
64 | |||
65 | -- Remove all formatting {{{ | ||
66 | function Note(s) | ||
67 | return nude(s) | ||
68 | end | ||
69 | |||
70 | function Blocksep() | ||
71 | return "\n" | ||
72 | end | ||
73 | function Emph(s) | ||
74 | return nude(s) | ||
75 | end | ||
76 | |||
77 | function Strong(s) | ||
78 | return nude(s) | ||
79 | end | ||
80 | |||
81 | function Subscript(s) | ||
82 | return nude(s) | ||
83 | end | ||
84 | |||
85 | function Superscript(s) | ||
86 | return nude(s) | ||
87 | end | ||
88 | |||
89 | function SmallCaps(s) | ||
90 | return nude(s) | ||
91 | end | ||
92 | |||
93 | function Strikeout(s) | ||
94 | return nude(s) | ||
95 | end | ||
96 | |||
97 | function Code(s, attr) | ||
98 | return nude(s) | ||
99 | end | ||
100 | |||
101 | function CodeBlock(s, attr) | ||
102 | return nude(s) | ||
103 | end | ||
104 | |||
105 | function InlineMath(s) | ||
106 | return nude(s) | ||
107 | end | ||
108 | |||
109 | function DisplayMath(s) | ||
110 | return nude(s) | ||
111 | end | ||
112 | |||
113 | function Span(s, attr) | ||
114 | return nude(s) | ||
115 | end | ||
116 | |||
117 | function Cite(s) | ||
118 | return nude(s) | ||
119 | end | ||
120 | |||
121 | function Plain(s) | ||
122 | return nude(s) | ||
123 | end | ||
124 | |||
125 | -- Links only include the link text | ||
126 | function Link(s, src, tit) | ||
127 | return nude(s) | ||
128 | end | ||
129 | |||
130 | -- Images have nothing to give us | ||
131 | -- (but add a space just in case) | ||
132 | function Image(s, src, tit) | ||
133 | return "\n" | ||
134 | end | ||
135 | |||
136 | function CaptionedImage(s, src, tit) | ||
137 | return "\n" | ||
138 | end | ||
139 | |||
140 | function Str(s) | ||
141 | return nude(s) | ||
142 | end | ||
143 | |||
144 | function Div(s, attr) | ||
145 | return nude(s) | ||
146 | end | ||
147 | |||
148 | function Space(s) | ||
149 | return "\n" | ||
150 | end | ||
151 | |||
152 | function LineBreak() | ||
153 | return "\n" | ||
154 | end | ||
155 | |||
156 | function Para(s) | ||
157 | return nude(s) | ||
158 | end | ||
159 | |||
160 | function Header(lev, s, attr) | ||
161 | return nude(s) | ||
162 | end | ||
163 | |||
164 | function BlockQuote(s) | ||
165 | return nude(s) | ||
166 | end | ||
167 | |||
168 | function HorizontalRule() | ||
169 | return "\n" | ||
170 | end | ||
171 | |||
172 | function BulletList(items) | ||
173 | local buffer = "" | ||
174 | for _, item in pairs(items) do | ||
175 | buffer = buffer .. nude(item) .. "\n" | ||
176 | end | ||
177 | return buffer .. "\n" | ||
178 | end | ||
179 | |||
180 | function OrderedList(items) | ||
181 | local buffer = "" | ||
182 | for _, item in pairs(items) do | ||
183 | buffer = buffer .. nude(item) .. "\n" | ||
184 | end | ||
185 | return buffer .. "\n" | ||
186 | end | ||
187 | |||
188 | function DefinitionList(items) | ||
189 | local buffer = "" | ||
190 | for _, item in pairs(items) do | ||
191 | for k, v in pairs(item) do | ||
192 | buffer = buffer .. nude(k) .. "\n" .. nude(v) .. "\n" | ||
193 | end | ||
194 | end | ||
195 | return buffer .. "\n" | ||
196 | end | ||
197 | |||
198 | function Table(caption, aligns, widths, headers, rows) | ||
199 | local buffer = "" | ||
200 | local function add(s) | ||
201 | buffer = buffer .. nude(s) .. "\n" | ||
202 | end | ||
203 | if caption ~= "" then | ||
204 | add(caption) | ||
205 | end | ||
206 | for _,h in pairs(headers) do | ||
207 | add(h) | ||
208 | end | ||
209 | for _, row in pairs(rows) do | ||
210 | for _, cell in pairs(row) do | ||
211 | add(cell) | ||
212 | end | ||
213 | end | ||
214 | return buffer | ||
215 | end | ||
216 | -- }}} | ||
217 | |||
218 | -- The following code will produce runtime warnings when you haven't defined | ||
219 | -- all of the functions you need for the custom writer, so it's useful | ||
220 | -- to include when you're working on a writer. | ||
221 | local meta = {} | ||
222 | meta.__index = | ||
223 | function(_, key) | ||
224 | io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | ||
225 | return function() return "" end | ||
226 | end | ||
227 | setmetatable(_G, meta) | ||
228 | |||
diff --git a/lua/sample-writer.lua b/lua/sample-writer.lua deleted file mode 100644 index a0c3c29..0000000 --- a/lua/sample-writer.lua +++ /dev/null | |||
@@ -1,324 +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 | ||
16 | local function escape(s, in_attribute) | ||
17 | return s:gsub("[<>&\"']", | ||
18 | function(x) | ||
19 | if x == '<' then | ||
20 | return '<' | ||
21 | elseif x == '>' then | ||
22 | return '>' | ||
23 | elseif x == '&' then | ||
24 | return '&' | ||
25 | elseif x == '"' then | ||
26 | return '"' | ||
27 | elseif x == "'" then | ||
28 | return ''' | ||
29 | else | ||
30 | return x | ||
31 | end | ||
32 | end) | ||
33 | end | ||
34 | |||
35 | -- Helper function to convert an attributes table into | ||
36 | -- a string that can be put into HTML tags. | ||
37 | local 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) | ||
45 | end | ||
46 | |||
47 | -- Run cmd on a temporary file containing inp and return result. | ||
48 | local 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 | ||
58 | end | ||
59 | |||
60 | -- Table to store footnotes, so they can be included at the end. | ||
61 | local notes = {} | ||
62 | |||
63 | -- Blocksep is used to separate block elements. | ||
64 | function Blocksep() | ||
65 | return "\n\n" | ||
66 | end | ||
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. | ||
72 | function Doc(body, metadata, variables) | ||
73 | local buffer = {} | ||
74 | local function add(s) | ||
75 | table.insert(buffer, s) | ||
76 | end | ||
77 | add('<!DOCTYPE html>') | ||
78 | add('<html>') | ||
79 | add('<head>') | ||
80 | add('<title>' .. (metadata['title'] or '') .. '</title>') | ||
81 | add('</head>') | ||
82 | add('<body>') | ||
83 | if metadata['title'] and metadata['title'] ~= "" then | ||
84 | add('<h1 class="title">' .. metadata['title'] .. '</h1>') | ||
85 | end | ||
86 | for _, author in pairs(metadata['author'] or {}) do | ||
87 | add('<h2 class="author">' .. author .. '</h2>') | ||
88 | end | ||
89 | if metadata['date'] and metadata['date'] ~= "" then | ||
90 | add('<h3 class="date">' .. metadata.date .. '</h3>') | ||
91 | end | ||
92 | add(body) | ||
93 | if #notes > 0 then | ||
94 | add('<ol class="footnotes">') | ||
95 | for _,note in pairs(notes) do | ||
96 | add(note) | ||
97 | end | ||
98 | add('</ol>') | ||
99 | end | ||
100 | add('</body>') | ||
101 | add('</html>') | ||
102 | return table.concat(buffer,'\n') | ||
103 | end | ||
104 | |||
105 | -- The functions that follow render corresponding pandoc elements. | ||
106 | -- s is always a string, attr is always a table of attributes, and | ||
107 | -- items is always an array of strings (the items in a list). | ||
108 | -- Comments indicate the types of other variables. | ||
109 | |||
110 | function Str(s) | ||
111 | return escape(s) | ||
112 | end | ||
113 | |||
114 | function Space() | ||
115 | return " " | ||
116 | end | ||
117 | |||
118 | function LineBreak() | ||
119 | return "<br/>" | ||
120 | end | ||
121 | |||
122 | function Emph(s) | ||
123 | return "<em>" .. s .. "</em>" | ||
124 | end | ||
125 | |||
126 | function Strong(s) | ||
127 | return "<strong>" .. s .. "</strong>" | ||
128 | end | ||
129 | |||
130 | function Subscript(s) | ||
131 | return "<sub>" .. s .. "</sub>" | ||
132 | end | ||
133 | |||
134 | function Superscript(s) | ||
135 | return "<sup>" .. s .. "</sup>" | ||
136 | end | ||
137 | |||
138 | function SmallCaps(s) | ||
139 | return '<span style="font-variant: small-caps;">' .. s .. '</span>' | ||
140 | end | ||
141 | |||
142 | function Strikeout(s) | ||
143 | return '<del>' .. s .. '</del>' | ||
144 | end | ||
145 | |||
146 | function Link(s, src, tit) | ||
147 | return "<a href='" .. escape(src,true) .. "' title='" .. | ||
148 | escape(tit,true) .. "'>" .. s .. "</a>" | ||
149 | end | ||
150 | |||
151 | function Image(s, src, tit) | ||
152 | return "<img src='" .. escape(src,true) .. "' title='" .. | ||
153 | escape(tit,true) .. "'/>" | ||
154 | end | ||
155 | |||
156 | function Code(s, attr) | ||
157 | return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>" | ||
158 | end | ||
159 | |||
160 | function InlineMath(s) | ||
161 | return "\\(" .. escape(s) .. "\\)" | ||
162 | end | ||
163 | |||
164 | function DisplayMath(s) | ||
165 | return "\\[" .. escape(s) .. "\\]" | ||
166 | end | ||
167 | |||
168 | function Note(s) | ||
169 | local num = #notes + 1 | ||
170 | -- insert the back reference right before the final closing tag. | ||
171 | s = string.gsub(s, | ||
172 | '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></') | ||
173 | -- add a list item with the note to the note table. | ||
174 | table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>') | ||
175 | -- return the footnote reference, linked to the note. | ||
176 | return '<a id="fnref' .. num .. '" href="#fn' .. num .. | ||
177 | '"><sup>' .. num .. '</sup></a>' | ||
178 | end | ||
179 | |||
180 | function Span(s, attr) | ||
181 | return "<span" .. attributes(attr) .. ">" .. s .. "</span>" | ||
182 | end | ||
183 | |||
184 | function Cite(s) | ||
185 | return "<span class=\"cite\">" .. s .. "</span>" | ||
186 | end | ||
187 | |||
188 | function Plain(s) | ||
189 | return s | ||
190 | end | ||
191 | |||
192 | function Para(s) | ||
193 | return "<p>" .. s .. "</p>" | ||
194 | end | ||
195 | |||
196 | -- lev is an integer, the header level. | ||
197 | function Header(lev, s, attr) | ||
198 | return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">" | ||
199 | end | ||
200 | |||
201 | function BlockQuote(s) | ||
202 | return "<blockquote>\n" .. s .. "\n</blockquote>" | ||
203 | end | ||
204 | |||
205 | function HorizontalRule() | ||
206 | return "<hr/>" | ||
207 | end | ||
208 | |||
209 | function CodeBlock(s, attr) | ||
210 | -- If code block has class 'dot', pipe the contents through dot | ||
211 | -- and base64, and include the base64-encoded png as a data: URL. | ||
212 | if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then | ||
213 | local png = pipe("base64", pipe("dot -Tpng", s)) | ||
214 | return '<img src="data:image/png;base64,' .. png .. '"/>' | ||
215 | -- otherwise treat as code (one could pipe through a highlighter) | ||
216 | else | ||
217 | return "<pre><code" .. attributes(attr) .. ">" .. escape(s) .. | ||
218 | "</code></pre>" | ||
219 | end | ||
220 | end | ||
221 | |||
222 | function BulletList(items) | ||
223 | local buffer = {} | ||
224 | for _, item in pairs(items) do | ||
225 | table.insert(buffer, "<li>" .. item .. "</li>") | ||
226 | end | ||
227 | return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>" | ||
228 | end | ||
229 | |||
230 | function OrderedList(items) | ||
231 | local buffer = {} | ||
232 | for _, item in pairs(items) do | ||
233 | table.insert(buffer, "<li>" .. item .. "</li>") | ||
234 | end | ||
235 | return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>" | ||
236 | end | ||
237 | |||
238 | -- Revisit association list STackValue instance. | ||
239 | function DefinitionList(items) | ||
240 | local buffer = {} | ||
241 | for _,item in pairs(items) do | ||
242 | for k, v in pairs(item) do | ||
243 | table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" .. | ||
244 | table.concat(v,"</dd>\n<dd>") .. "</dd>") | ||
245 | end | ||
246 | end | ||
247 | return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>" | ||
248 | end | ||
249 | |||
250 | -- Convert pandoc alignment to something HTML can use. | ||
251 | -- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault. | ||
252 | function html_align(align) | ||
253 | if align == 'AlignLeft' then | ||
254 | return 'left' | ||
255 | elseif align == 'AlignRight' then | ||
256 | return 'right' | ||
257 | elseif align == 'AlignCenter' then | ||
258 | return 'center' | ||
259 | else | ||
260 | return 'left' | ||
261 | end | ||
262 | end | ||
263 | |||
264 | -- Caption is a string, aligns is an array of strings, | ||
265 | -- widths is an array of floats, headers is an array of | ||
266 | -- strings, rows is an array of arrays of strings. | ||
267 | function Table(caption, aligns, widths, headers, rows) | ||
268 | local buffer = {} | ||
269 | local function add(s) | ||
270 | table.insert(buffer, s) | ||
271 | end | ||
272 | add("<table>") | ||
273 | if caption ~= "" then | ||
274 | add("<caption>" .. caption .. "</caption>") | ||
275 | end | ||
276 | if widths and widths[1] ~= 0 then | ||
277 | for _, w in pairs(widths) do | ||
278 | add('<col width="' .. string.format("%d%%", w * 100) .. '" />') | ||
279 | end | ||
280 | end | ||
281 | local header_row = {} | ||
282 | local empty_header = true | ||
283 | for i, h in pairs(headers) do | ||
284 | local align = html_align(aligns[i]) | ||
285 | table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>') | ||
286 | empty_header = empty_header and h == "" | ||
287 | end | ||
288 | if empty_header then | ||
289 | head = "" | ||
290 | else | ||
291 | add('<tr class="header">') | ||
292 | for _,h in pairs(header_row) do | ||
293 | add(h) | ||
294 | end | ||
295 | add('</tr>') | ||
296 | end | ||
297 | local class = "even" | ||
298 | for _, row in pairs(rows) do | ||
299 | class = (class == "even" and "odd") or "even" | ||
300 | add('<tr class="' .. class .. '">') | ||
301 | for i,c in pairs(row) do | ||
302 | add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>') | ||
303 | end | ||
304 | add('</tr>') | ||
305 | end | ||
306 | add('</table') | ||
307 | return table.concat(buffer,'\n') | ||
308 | end | ||
309 | |||
310 | function Div(s, attr) | ||
311 | return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>" | ||
312 | end | ||
313 | |||
314 | -- The following code will produce runtime warnings when you haven't defined | ||
315 | -- all of the functions you need for the custom writer, so it's useful | ||
316 | -- to include when you're working on a writer. | ||
317 | local meta = {} | ||
318 | meta.__index = | ||
319 | function(_, key) | ||
320 | io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | ||
321 | return function() return "" end | ||
322 | end | ||
323 | setmetatable(_G, meta) | ||
324 | |||