diff options
Diffstat (limited to 'lua/river.lua')
-rw-r--r-- | lua/river.lua | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/lua/river.lua b/lua/river.lua new file mode 100644 index 0000000..d060ba9 --- /dev/null +++ b/lua/river.lua | |||
@@ -0,0 +1,226 @@ | |||
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 | local function flow(s) | ||
8 | return s:gsub("%s+", "\n") | ||
9 | end | ||
10 | |||
11 | local function nude(s) | ||
12 | -- Expand contractions | ||
13 | s = s:gsub("'%a+%s", function (x) | ||
14 | if x == "'ll" then | ||
15 | return " will " | ||
16 | elseif x == "'ve" then | ||
17 | return " have " | ||
18 | elseif x == "'re" then | ||
19 | return " are " | ||
20 | else | ||
21 | return x | ||
22 | end | ||
23 | end) | ||
24 | -- Get rid of quotes around words | ||
25 | s = s:gsub('"', ' ') | ||
26 | s = s:gsub("%s+'", ' ') | ||
27 | s = s:gsub("'%s+", ' ') | ||
28 | -- Remove HTML entities | ||
29 | s = s:gsub('&.-;', ' ') | ||
30 | s = s:gsub('%b<>', ' ') | ||
31 | -- Remove end-of-line backslashes | ||
32 | s = s:gsub('%s+\\$', ' ') | ||
33 | -- Remove dashes (not hyphens) | ||
34 | s = s:gsub('%-%-+', ' ') | ||
35 | s = s:gsub('%-%s', ' ') | ||
36 | -- Remove general punctuation | ||
37 | s = s:gsub('[%.!%?:;,%[%]%(%)<>]', ' ') | ||
38 | -- Remove extra spaces | ||
39 | s = s:gsub('%s+', ' ') | ||
40 | return s:lower() | ||
41 | end | ||
42 | |||
43 | -- This function is called once for the whole document. Parameters: | ||
44 | -- body is a string, metadata is a table, variables is a table. | ||
45 | -- One could use some kind of templating | ||
46 | -- system here; this just gives you a simple standalone HTML file. | ||
47 | function Doc(body, metadata, variables) | ||
48 | local buffer = "" | ||
49 | local function add(s) | ||
50 | buffer = buffer .. nude(s) .. "\n" | ||
51 | end | ||
52 | if metadata['title'] then | ||
53 | add(metadata['title']) | ||
54 | end | ||
55 | if metadata['subtitle'] then | ||
56 | add(metadata['subtitle']) | ||
57 | end | ||
58 | -- TODO: epigraph.content, epigraph.attrib, dedication, other metadata? | ||
59 | add(body) | ||
60 | return flow(buffer) | ||
61 | end | ||
62 | |||
63 | -- Remove all formatting {{{ | ||
64 | function Note(s) | ||
65 | return nude(s) | ||
66 | end | ||
67 | |||
68 | function Blocksep() | ||
69 | return "\n" | ||
70 | end | ||
71 | function Emph(s) | ||
72 | return nude(s) | ||
73 | end | ||
74 | |||
75 | function Strong(s) | ||
76 | return nude(s) | ||
77 | end | ||
78 | |||
79 | function Subscript(s) | ||
80 | return nude(s) | ||
81 | end | ||
82 | |||
83 | function Superscript(s) | ||
84 | return nude(s) | ||
85 | end | ||
86 | |||
87 | function SmallCaps(s) | ||
88 | return nude(s) | ||
89 | end | ||
90 | |||
91 | function Strikeout(s) | ||
92 | return nude(s) | ||
93 | end | ||
94 | |||
95 | function Code(s, attr) | ||
96 | return nude(s) | ||
97 | end | ||
98 | |||
99 | function CodeBlock(s, attr) | ||
100 | return nude(s) | ||
101 | end | ||
102 | |||
103 | function InlineMath(s) | ||
104 | return nude(s) | ||
105 | end | ||
106 | |||
107 | function DisplayMath(s) | ||
108 | return nude(s) | ||
109 | end | ||
110 | |||
111 | function Span(s, attr) | ||
112 | return nude(s) | ||
113 | end | ||
114 | |||
115 | function Cite(s) | ||
116 | return nude(s) | ||
117 | end | ||
118 | |||
119 | function Plain(s) | ||
120 | return nude(s) | ||
121 | end | ||
122 | |||
123 | -- Links only include the link text | ||
124 | function Link(s, src, tit) | ||
125 | return nude(s) | ||
126 | end | ||
127 | |||
128 | -- Images have nothing to give us | ||
129 | -- (but add a space just in case) | ||
130 | function Image(s, src, tit) | ||
131 | return "\n" | ||
132 | end | ||
133 | |||
134 | function CaptionedImage(s, src, tit) | ||
135 | return "\n" | ||
136 | end | ||
137 | |||
138 | function Str(s) | ||
139 | return nude(s) | ||
140 | end | ||
141 | |||
142 | function Div(s, attr) | ||
143 | return nude(s) | ||
144 | end | ||
145 | |||
146 | function Space(s) | ||
147 | return "\n" | ||
148 | end | ||
149 | |||
150 | function LineBreak() | ||
151 | return "\n" | ||
152 | end | ||
153 | |||
154 | function Para(s) | ||
155 | return nude(s) | ||
156 | end | ||
157 | |||
158 | function Header(lev, s, attr) | ||
159 | return nude(s) | ||
160 | end | ||
161 | |||
162 | function BlockQuote(s) | ||
163 | return nude(s) | ||
164 | end | ||
165 | |||
166 | function HorizontalRule() | ||
167 | return "\n" | ||
168 | end | ||
169 | |||
170 | function BulletList(items) | ||
171 | local buffer = "" | ||
172 | for _, item in pairs(items) do | ||
173 | buffer = buffer .. nude(item) .. "\n" | ||
174 | end | ||
175 | return buffer .. "\n" | ||
176 | end | ||
177 | |||
178 | function OrderedList(items) | ||
179 | local buffer = "" | ||
180 | for _, item in pairs(items) do | ||
181 | buffer = buffer .. nude(item) .. "\n" | ||
182 | end | ||
183 | return buffer .. "\n" | ||
184 | end | ||
185 | |||
186 | function DefinitionList(items) | ||
187 | local buffer = "" | ||
188 | for _, item in pairs(items) do | ||
189 | for k, v in pairs(item) do | ||
190 | buffer = buffer .. nude(k) .. "\n" .. nude(v) .. "\n" | ||
191 | end | ||
192 | end | ||
193 | return buffer .. "\n" | ||
194 | end | ||
195 | |||
196 | function Table(caption, aligns, widths, headers, rows) | ||
197 | local buffer = "" | ||
198 | local function add(s) | ||
199 | buffer = buffer .. nude(s) .. "\n" | ||
200 | end | ||
201 | if caption ~= "" then | ||
202 | add(caption) | ||
203 | end | ||
204 | for _,h in pairs(headers) do | ||
205 | add(h) | ||
206 | end | ||
207 | for _, row in pairs(rows) do | ||
208 | for _, cell in pairs(row) do | ||
209 | add(cell) | ||
210 | end | ||
211 | end | ||
212 | return buffer | ||
213 | end | ||
214 | -- }}} | ||
215 | |||
216 | -- The following code will produce runtime warnings when you haven't defined | ||
217 | -- all of the functions you need for the custom writer, so it's useful | ||
218 | -- to include when you're working on a writer. | ||
219 | local meta = {} | ||
220 | meta.__index = | ||
221 | function(_, key) | ||
222 | io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | ||
223 | return function() return "" end | ||
224 | end | ||
225 | setmetatable(_G, meta) | ||
226 | |||