-
Notifications
You must be signed in to change notification settings - Fork 0
/
loco.lua
238 lines (219 loc) · 6.57 KB
/
loco.lua
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
-- Loco
-- ====
-- Port of [Docco](http://jashkenas.github.io/docco/):
-- The original quick-and-dirty, hundred-line-long,
-- literate-programming-style documentation generator.
--
-- Loco outputs HTML alongside code for simple and easy documentation.
-- No javadoc or other crap, just markdown and code.
--
-- Comments are passed through Niklas Frykholm's Markdown module.
--
--
-- If you're reading this in a browser, this page is generated using loco.
--
local markdown = require("markdown")
local templates = {}
--## Utilities
-- String split function from Turbo.lua
function strsplit(str, sep, max, pattern)
local record = {}
if str:len() > 0 then
local plain = not pattern
max = max or -1
local field = 1
start = 1
local first, last = str:find(sep, start, plain)
while first and max ~= 0 do
record[field] = str:sub(start, first - 1)
field = field + 1
start = last + 1
first, last = str:find(sep, start, plain)
max = max - 1
end
record[field] = str:sub(start)
end
return record
end
-- Encodes a string to html entities..
-- This is used to avoid problems when html occurs inside
-- the source code.
function htmlentities(s)
return string.gsub(s, "([^A-Za-z0-9_])", function(c)
return string.format("&#%d;", string.byte(c))
end)
end
--## Comment/Code splitter
-- `parse()`: Splits sourcecode into a list of {doc, code} pairs
function parse(code)
local lines = strsplit(code, "\n")
local sections = {}
local has_code = false
local docs_text = ""
local code_text = ""
if lines[1]:sub(1, 2) == "#!" then
table.remove(lines, 1)
end
for i, line in ipairs(lines) do
if line:match("^%s*%-%-.*$") and not line:match("^%s*%-%-|.*$") then
-- If the line 'starts with' a comment and not with --|
-- We use --| to avoid inclusion.
-- This is useful in some cases where code is supposed to be
-- commented out.
if has_code then
sections[#sections + 1] = {
docs_text = docs_text,
code_text = code_text,
}
has_code = false
docs_text = ""
code_text = ""
end
-- Remove the comment marker (--)
docs_text = docs_text .. line:gsub("^%s*%-%-", "") .. "\n"
else
-- Not a comment
has_code = true
code_text = code_text .. line .. "\n"
end
end
sections[#sections + 1] = {
docs_text = docs_text,
code_text = code_text,
}
return sections
end
--## HTML Generator
-- Make the html..
--
function generate_html(sections)
local out = templates.header
for i, section in ipairs(sections) do
local docs_text = section.docs_text
out = out .. string.format(templates.section, i, i, markdown(docs_text), htmlentities(section.code_text))
end
out = out .. templates.footer
return out
end
function loco(source)
return generate_html(parse(source))
end
--## HTML Templates
templates.header = [[
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/base16/tomorrow.min.css" integrity="sha512-5D/fcZ3y3nuaeHSxDbFwWDEy1Fvj5qQKsU0tilD7bhWAA+IN/Jl9fzGdUotzvA7wgXtsnZmafcuunH+6nyuA0A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/lua.min.js" integrity="sha512-6gwhO3LqvF+WM7eGJIQ482KAwlTOvw2d6/5QMUZHRX9tsa4gZWJLZYZmiyDH8+SRKjZgkOAXP+TQm6bveuBDEw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<meta charset="utf-8"/>
<style>
body {
font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
font-size: 15px;
line-height: 22px;
color: #2B2B2B;
margin: 0; padding: 0;
}
table td { border: 0; outline: 0; }
td.docs {
max-widht: 30rem;
min-width: 30rem;
min-height: 1rem;
padding: 10px 25px 1px 50px;
vertical-align: top;
text-align: left;
}
td.code {
padding: 40px 15px 16px 50px;
width: 100%;
vertical-align: top;
border-left: 1px solid #e5e5ee;
}
pre, code {
font-size: 14px;
line-height: 22px;
margin: 0; padding: 0;
}
.section-link {
display: inline;
text-decoration: none;
color: rgba(0,0,0,0.1);
}
.section-link:hover {
color: #2b2b2b;
text-decoration: none;
}
h1,h2,h3,h4,h5,h6 {
padding-top: 10px;
}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
color: inherit;
text-decoration: inherit;
cursor: pointer;
}
</style>
</head>
<body>
<div id="container">
<table cellspacing=0 cellpadding=0>
<tbody>
]]
templates.footer = [[
</tbody>
</table>
</div>
<script>
hljs.highlightAll();
document.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach(function(x){
x.id = x.innerHTML.replace(/:.*/, "").replaceAll(" ", "-");
x.innerHTML = `<a href="#` + x.id + `">` + x.innerHTML + `</a>`;
});
</script>
</body>
</html>
]]
templates.section = [[
<tr id="section-%d">
<td class="docs"><a class="section-link" href="#section-%d">#</a>%s</td>
<td class="code">
<pre><code>%s</code></pre>
</td>
</tr>
]]
--## Command line part
if arg and arg[0]:find("loco%.lua$") then
local io = require("io")
local os = require("os")
local USAGE = [[
lua loco.lua INPUTFILE
or
cat INPUTFILE | lua loco.lua
]]
local infile = arg[1]
local source = ""
for _, v in ipairs(arg) do
-- If -h/--help is in the argument list
-- print the usage message and exit
if v == "--help" or v == "-h" then
print(USAGE)
os.exit(1)
end
end
if infile ~= nil and infile ~= "--" then
-- User supplied an input file
local f = io.open(infile)
if f == nil then
print(string.format("Could not open file '%s'", infile))
os.exit(1)
end
source = f:read("*all")
else
-- No file specified, use stdin
local f = io.input()
source = f:read("*all")
end
print(loco(source))
else
return loco
end