about summary refs log tree commit diff stats
path: root/trunk/hapax.lua
diff options
context:
space:
mode:
authorCase Duckworth2015-04-14 16:36:17 -0700
committerCase Duckworth2015-04-14 16:36:17 -0700
commit9fce418b46c9f0894f429384ef9e3dabaeffbeb4 (patch)
treeb2339220ee50cf48b8887f0cc1fed4813a95901b /trunk/hapax.lua
parentAdd toc metadata (diff)
downloadautocento-9fce418b46c9f0894f429384ef9e3dabaeffbeb4.tar.gz
autocento-9fce418b46c9f0894f429384ef9e3dabaeffbeb4.zip
Change file hierarchy and rewrite makefile
- File hierarchy is now as follows:
    - /
        - appendix/  < appendix source files
        - backlinks/ < backlink sources & builds
        - hapax/     < *.hapax source files
        - scripts/   < scripts, like *.js, *.hs, etc.
        - templates/ < templates for outputs
        - text/      < source files
        - trunk/     < assets, like css, images, heads, etc.
        - index.html
        - *.html
        - Makefile
Diffstat (limited to 'trunk/hapax.lua')
-rw-r--r--trunk/hapax.lua251
1 files changed, 0 insertions, 251 deletions
diff --git a/trunk/hapax.lua b/trunk/hapax.lua deleted file mode 100644 index af59e59..0000000 --- a/trunk/hapax.lua +++ /dev/null
@@ -1,251 +0,0 @@
1-- Pandoc Hapax writer
2-- it takes out all formatting, leaving only a river of text
3-- running down the page: one word per line, stripping all duplicates
4-- vim: fdm=marker
5-- invoke with: pandoc -t hapax.lua
6
7os.setlocale("en_US.UTF-8")
8
9function hapax(s)
10 local function tablify (s, p)
11 local t={}
12 for w in s:gmatch(p) do
13 table.insert(t, w)
14 end
15 return t
16 end
17 local function stripDupes (t)
18 local seen = {}
19 local remove = {}
20 for i = 1, #t do
21 element = t[i]
22 if seen[element] then
23 remove[element] = true
24 else
25 seen[element] = true
26 end
27 end
28 for i = #t, 1, -1 do
29 if remove[t[i]] then
30 table.remove(t, i)
31 end
32 end
33 return t
34 end
35 return table.concat(stripDupes(tablify(s, "%S+")), "\n")
36end
37
38function flow(s)
39 return s:gsub("%s+", "\n")
40end
41
42function nude(s)
43 s = s:lower()
44 -- Expand contractions
45 s = s:gsub("'ll", " will ")
46 s = s:gsub("'ve", " have ")
47 s = s:gsub("'re", " are ")
48 s = s:gsub("i'm", " i am ")
49 s = s:gsub("it's", "it is")
50 s = s:gsub("n't", " not ")
51 s = s:gsub("&", " and ")
52 -- -- Remove dashes (not hyphens)
53 s = s:gsub('%-[%-%s]+', ' ')
54 -- Remove everything that is not letters or numbers
55 s = s:gsub('[^A-Za-z0-9/\'-]', ' ')
56 -- Remove extra spaces
57 s = s:gsub('%s+', ' ')
58 return " "..s.." "
59end
60
61-- This function is called once for the whole document. Parameters:
62-- body is a string, metadata is a table, variables is a table.
63-- One could use some kind of templating
64-- system here; this just gives you a simple standalone HTML file.
65function Doc(body, metadata, variables)
66 local buffer = ""
67 local function add(s)
68 buffer = buffer .. nude(s) .. "\n"
69 end
70 if metadata['title'] then
71 add(metadata['title'])
72 end
73 if metadata['subtitle'] then
74 add(metadata['subtitle'])
75 end
76 add(body)
77 return hapax(flow(buffer))
78 -- return flow(buffer)
79end
80
81-- Remove all formatting {{{
82function Note(s)
83 return s
84end
85
86function Blocksep()
87 return "\n"
88end
89function Emph(s)
90 return s
91end
92
93function Strong(s)
94 return s
95end
96
97function Subscript(s)
98 return s
99end
100
101function Superscript(s)
102 return s
103end
104
105function SmallCaps(s)
106 return s
107end
108
109function Strikeout(s)
110 return s
111end
112
113function Code(s, attr)
114 return s
115end
116
117function CodeBlock(s, attr)
118 return s
119end
120
121function InlineMath(s)
122 return s
123end
124
125function DisplayMath(s)
126 return s
127end
128
129function Span(s, attr)
130 return s
131end
132
133function Cite(s)
134 return s
135end
136
137function Plain(s)
138 return s
139end
140
141-- Links only include the link text
142function Link(s, src, tit)
143 return s
144end
145
146-- Images have nothing to give us
147-- (but add a space just in case)
148function Image(s, src, tit)
149 return "\n"
150end
151
152function RawBlock(s)
153 return s
154end
155
156function RawInline(s)
157 return s
158end
159
160function CaptionedImage(s, src, tit)
161 return "\n"
162end
163
164function Str(s)
165 return s
166end
167
168function Div(s, attr)
169 return s
170end
171
172function Space(s)
173 return "\n"
174end
175
176function LineBreak()
177 return "\n"
178end
179
180function Para(s)
181 return s
182end
183
184function Header(lev, s, attr)
185 return s
186end
187
188function BlockQuote(s)
189 return s
190end
191
192function HorizontalRule()
193 return "\n"
194end
195
196function BulletList(items)
197 local buffer = ""
198 for _, item in pairs(items) do
199 buffer = buffer .. " " .. item .. "\n"
200 end
201 return buffer .. "\n"
202end
203
204function OrderedList(items)
205 local buffer = ""
206 for _, item in pairs(items) do
207 buffer = buffer .. " " .. item .. "\n"
208 end
209 return buffer .. "\n"
210end
211
212function DefinitionList(items)
213 local buffer = ""
214 for _, item in pairs(items) do
215 for k, v in pairs(item) do
216 buffer = buffer .. " " .. k .. "\n" .. v .. "\n"
217 end
218 end
219 return buffer .. "\n"
220end
221
222function Table(caption, aligns, widths, headers, rows)
223 local buffer = ""
224 local function add(s)
225 buffer = buffer .. " " .. s .. "\n"
226 end
227 if caption ~= "" then
228 add(caption)
229 end
230 for _,h in pairs(headers) do
231 add(h)
232 end
233 for _, row in pairs(rows) do
234 for _, cell in pairs(row) do
235 add(cell)
236 end
237 end
238 return buffer
239end
240-- }}}
241
242-- The following code will produce runtime warnings when you haven't defined
243-- all of the functions you need for the custom writer, so it's useful
244-- to include when you're working on a writer.
245local meta = {}
246meta.__index =
247 function(_, key)
248 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
249 return function() return "" end
250 end
251setmetatable(_G, meta)