-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhellodocs.awk
More file actions
539 lines (478 loc) · 16.9 KB
/
Copy pathhellodocs.awk
File metadata and controls
539 lines (478 loc) · 16.9 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# hellodocs.awk - Embedded project documentation
#
# Convert documentation inside your project from markdown to raw HTML
#
# Usage:
# awk -f hellodocs.awk -v title='Hello, Docs!' -v link='https://github.com/...' README.md
#
# Input:
# README.md
#
# Output:
# index.html - Index file
# hellodocs-nav.html - Navigation
# hellodocs-N.html - Content sections
#
# https://www.tirreno.com/hellodocs
BEGIN {
# Version
VERSION = "0.0.2"
# Default title
if (!title) title = "hellodocs"
# Colors
NAV_BG = "lavender"
CONTENT_BG = "gainsboro"
CODE_BG = "blanchedalmond"
LINK = "dodgerblue"
VLINK = "dimgray"
ALINK = "tomato"
TEXT = "black"
# State
n = 0; inlist = 0; insublist = 0; inpre = 0; intable = 0; tablerow = 0
para = ""; bullet = ""; listtype = ""; codebuf = ""; tabidx = 0
intro = ""; inintro = 0; incodeblock = 0; intoc = 0
nav = ""
}
function to_anchor(title, a) {
a = tolower(title)
gsub(/[^a-z0-9 -]/, "", a)
gsub(/ /, "-", a)
return a
}
function md_links(s, pre, url, txt, post, rest, idx, paren, k, c) {
while (match(s, /\[[^\]]+\]\(/)) {
pre = substr(s, 1, RSTART-1)
rest = substr(s, RSTART)
idx = index(rest, "](")
txt = substr(rest, 2, idx - 2)
rest = substr(rest, idx + 2)
paren = 1
for (k = 1; k <= length(rest); k++) {
c = substr(rest, k, 1)
if (c == "(") paren++
else if (c == ")") paren--
if (paren == 0) break
}
url = substr(rest, 1, k - 1)
post = substr(rest, k + 1)
if (url ~ /^https?:\/\//) {
s = pre "<a href=\"" url "\" target=\"_blank\">" txt "</a>" post
} else if (url ~ /^#/) {
# Internal anchor - mark for later resolution
s = pre "<a href=\"" url "\" class=\"internal\">" txt "</a>" post
} else {
s = pre "<a href=\"" url "\">" txt "</a>" post
}
}
return s
}
function inline_fmt(s, pre, code, post) {
# Strip markdown image badges: [](url) -> (remove entirely)
gsub(/\[!\[[^\]]*\]\([^)]*\([^)]*\)[^)]*\)\]\([^)]*\)/, "", s)
gsub(/\[!\[[^\]]*\]\([^)]*\)\]\([^)]*\)/, "", s)
# Strip markdown images:  -> (remove entirely)
gsub(/!\[[^\]]*\]\([^)]*\)/, "", s)
# Strip HTML tags (img, p, a that are part of image wrappers)
gsub(/<img[^>]*>/, "", s)
gsub(/<p [^>]*>/, "", s)
gsub(/<\/p>/, "", s)
gsub(/^[[:space:]]*<a [^>]*>[[:space:]]*$/, "", s)
gsub(/^[[:space:]]*<\/a>[[:space:]]*$/, "", s)
s = md_links(s)
# Bold: **text** -> <b>text</b>
gsub(/\*\*[^*]+\*\*/, "<b>&</b>", s)
gsub(/<b>\*\*/, "<b>", s)
gsub(/\*\*<\/b>/, "</b>", s)
# Italic: *text* -> <i>text</i>
gsub(/\*[^*]+\*/, "<i>&</i>", s)
gsub(/<i>\*/, "<i>", s)
gsub(/\*<\/i>/, "</i>", s)
# Inline code: `code` -> <code>code</code>
while (match(s, /`[^`]+`/)) {
pre = substr(s, 1, RSTART-1)
code = substr(s, RSTART+1, RLENGTH-2)
post = substr(s, RSTART+RLENGTH)
gsub(/</, "\\<", code)
gsub(/>/, "\\>", code)
s = pre "<code contenteditable>" code "</code>" post
}
return s
}
function html_head(t) {
return "<html><head><meta charset=\"utf-8\" /><title>" t "</title><meta name=\"generator\" content=\"hellodocs " VERSION "\" /></head>"
}
function flush_bullet() {
if (bullet != "") {
print "<li>" inline_fmt(bullet) > f
bullet = ""
openli = 1
}
}
function close_openli() {
if (openli) { print "</li>" > f; openli = 0 }
}
function close_sublist() {
if (insublist) { print "</ul>" > f; insublist = 0 }
}
function close_list() {
flush_bullet()
close_sublist()
close_openli()
if (inlist) {
if (listtype == "ol") print "</ol>" > f
else print "</ul>" > f
inlist = 0
}
}
function flush_para( formatted) {
if (para != "") {
formatted = inline_fmt(para)
if (formatted !~ /^[[:space:]]*$/) {
print "<p>" formatted "</p>" > f
}
para = ""
}
}
function print_footer() {
printf "<code>________________________________________________________________________________</code><br><footer>" > f
printf "<code>%s</code>", title > f
if (link) printf " <code>|</code> <code><a href=\"%s\" target=\"_blank\">Source code</a></code>", link > f
printf " <code>|</code> <code>Generated by <a href=\"https://www.tirreno.com/hellodocs\" target=\"_blank\">hellodocs</a></code>" > f
print "</footer>" > f
}
function emit_code_open() {
printf "<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\"><tbody><tr><td bgcolor=\"" CODE_BG "\"><code><pre contenteditable>" > f
}
function emit_code_close(buffer) {
sub(/\n$/, "", buffer)
gsub(/</, "\\<", buffer)
gsub(/>/, "\\>", buffer)
printf "%s</pre></code></td></tr></tbody></table>\n", buffer > f
}
function emit_table_open() {
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"3\" frame=\"void\">" > f
}
function emit_table_close() {
print "</table>" > f
}
function emit_table_row(line, row_num, tmpline, ncells, cells, j) {
tmpline = line
gsub(/\\\|/, "\x01", tmpline)
gsub(/^\| *| *\|$/, "", tmpline)
ncells = split(tmpline, cells, / *\| */)
if (row_num == 1) {
printf "<tr>" > f
for (j = 1; j <= ncells; j++) {
gsub(/\x01/, "|", cells[j])
printf "<th>%s</th>", inline_fmt(cells[j]) > f
}
print "</tr>" > f
} else {
printf "<tr>" > f
for (j = 1; j <= ncells; j++) {
gsub(/\x01/, "|", cells[j])
if (j == 1) printf "<td><b>%s</b></td>", inline_fmt(cells[j]) > f
else printf "<td>%s</td>", inline_fmt(cells[j]) > f
}
print "</tr>" > f
}
}
function emit_para(line, formatted) {
formatted = inline_fmt(line)
if (formatted ~ /^[[:space:]]*$/) return
print "<p>" formatted "</p>" > f
}
# Process intro content into HTML
function process_intro_content(intro_text, nlines, lines, i, incode, intbl, tblrow, cbuf, inlst, tmp) {
nlines = split(intro_text, lines, "\n")
incode = 0; intbl = 0; tblrow = 0; cbuf = ""; inlst = 0
for (i = 1; i <= nlines; i++) {
if (lines[i] ~ /^ *```/) {
if (inlst) { print "</ul>" > f; inlst = 0 }
if (incode) {
emit_code_close(cbuf); cbuf = ""; incode = 0
} else {
if (intbl) { emit_table_close(); intbl = 0 }
emit_code_open(); incode = 1
}
} else if (incode) {
cbuf = cbuf lines[i] "\n"
} else if (lines[i] ~ /^-+$/ || lines[i] ~ /^---/) {
if (inlst) { print "</ul>" > f; inlst = 0 }
} else if (lines[i] ~ /^## /) {
if (inlst) { print "</ul>" > f; inlst = 0 }
if (intbl) { emit_table_close(); intbl = 0 }
tmp = lines[i]; sub(/^## +/, "", tmp)
print "<hr color=\"" TEXT "\" noshade size=\"1\"><h2>" inline_fmt(tmp) "</h2>" > f
} else if (lines[i] ~ /^### /) {
if (inlst) { print "</ul>" > f; inlst = 0 }
if (intbl) { emit_table_close(); intbl = 0 }
tmp = lines[i]; sub(/^### +/, "", tmp)
print "<h3>" inline_fmt(tmp) "</h3>" > f
} else if (lines[i] ~ /^\* / || lines[i] ~ /^- /) {
if (intbl) { emit_table_close(); intbl = 0 }
if (!inlst) { print "<ul>" > f; inlst = 1 }
tmp = lines[i]; sub(/^[\*-] +/, "", tmp)
print "<li>" inline_fmt(tmp) "</li>" > f
} else if (lines[i] == "") {
# blank
} else if (lines[i] ~ /^\|.*\|$/) {
if (inlst) { print "</ul>" > f; inlst = 0 }
if (lines[i] !~ /^\|[-| :]+\|$/) {
if (!intbl) { emit_table_open(); intbl = 1; tblrow = 0 }
tblrow++
emit_table_row(lines[i], tblrow)
}
} else {
if (inlst) { print "</ul>" > f; inlst = 0 }
if (intbl) { emit_table_close(); intbl = 0 }
if (lines[i] != "") emit_para(lines[i])
}
}
if (inlst) print "</ul>" > f
if (intbl) emit_table_close()
}
# Create intro section file
function create_intro_section(intro_text, intro_title) {
n = 1
f = "hellodocs-1.html"
sections[1] = to_anchor(intro_title)
print html_head(title) "<body topmargin=\"25\" leftmargin=\"50\" bgcolor=\"" CONTENT_BG "\" text=\"" TEXT "\" link=\"" LINK "\" vlink=\"" VLINK "\" alink=\"" ALINK "\"><hr color=\"" TEXT "\" noshade size=\"1\"><h2>" intro_title "</h2>" > f
process_intro_content(intro_text)
print_footer()
print "</body></html>" > f
close(f)
f = ""
# Add to nav
tabidx++
nav = nav "<b><a href=\"hellodocs-1.html\" target=\"c\" tabindex=\"" tabidx "\">" intro_title "</a></b><br>\n"
}
# Close current section
function close_section() {
if (n > 0 && f != "") {
flush_para()
close_list()
if (intable) { emit_table_close(); intable = 0; tablerow = 0 }
if (inpre) { emit_code_close(codebuf); codebuf = ""; inpre = 0 }
# Close subsection list in nav if open
if (navsub) { nav = nav "</ol>\n"; navsub = 0 }
print_footer()
print "</body></html>" > f
close(f)
}
}
# Start new section
function start_section(sectitle) {
close_section()
n++
anchor = to_anchor(sectitle)
sections[n] = anchor
f = "hellodocs-" n ".html"
print html_head(title) "<body topmargin=\"25\" leftmargin=\"50\" bgcolor=\"" CONTENT_BG "\" text=\"" TEXT "\" link=\"" LINK "\" vlink=\"" VLINK "\" alink=\"" ALINK "\"><hr color=\"" TEXT "\" noshade size=\"1\"><h2>" sectitle "</h2>" > f
# Add to nav
tabidx++
nav = nav "<b><a href=\"hellodocs-" n ".html\" target=\"c\" tabindex=\"" tabidx "\">" sectitle "</a></b><br>\n"
}
# Track code blocks globally
/^ *```/ {
incodeblock = !incodeblock
}
# Capture # title line
!incodeblock && /^# / && n == 0 {
doctitle = substr($0, 3)
inintro = 1
next
}
# Skip TOC section entirely (## Table of contents or ## Contents)
!incodeblock && (/^## [Tt]able of [Cc]ontents$/ || /^## [Cc]ontents$/) {
# If we have intro, create it first
if (inintro && intro != "") {
introtitle = (doctitle != "") ? doctitle : title
create_intro_section(intro, introtitle)
intro = ""
inintro = 0
}
intoc = 1 # Flag to skip TOC content
next
}
# End of TOC section (next ## header)
intoc && !incodeblock && /^## / {
intoc = 0
# Fall through to section handler
}
# Skip TOC content
intoc { next }
# Capture intro content (between # title and first ## section)
inintro && !/^## / {
intro = intro $0 "\n"
next
}
# Section header (## )
!incodeblock && /^## / {
# If we have intro content, create intro section first
if (intro != "" && n == 0) {
introtitle = (doctitle != "") ? doctitle : title
create_intro_section(intro, introtitle)
intro = ""
}
inintro = 0 # Done capturing intro
sectitle = substr($0, 4)
start_section(sectitle)
next
}
# Subsection header (### ) - add to nav
!incodeblock && /^### / {
if (n > 0) {
flush_para(); close_list()
if (intable) { emit_table_close(); intable = 0; tablerow = 0 }
sectitle = substr($0, 5)
anchor = to_anchor(sectitle)
subsections[n, ++subcount[n]] = anchor
print "<h3 id=\"" anchor "\">" sectitle "</h3>" > f
# Add to nav as subsection
if (!navsub) { nav = nav "<ol>\n"; navsub = 1 }
nav = nav "<li><a href=\"hellodocs-" n ".html#" anchor "\" target=\"c\">" sectitle "</a></li>\n"
}
next
}
# Sub-subsection header (#### )
!incodeblock && /^#### / {
if (n > 0) {
flush_para(); close_list()
if (intable) { emit_table_close(); intable = 0; tablerow = 0 }
sectitle = substr($0, 6)
anchor = to_anchor(sectitle)
print "<h4 id=\"" anchor "\">" sectitle "</h4>" > f
}
next
}
# Skip everything before first section
n == 0 { next }
# Content parsing
n > 0 {
# code block toggle
if (/^ *```/) {
flush_para(); close_list()
if (inpre) {
emit_code_close(codebuf); codebuf = ""; inpre = 0
} else {
if (intable) { emit_table_close(); intable = 0 }
emit_code_open(); inpre = 1
}
next
}
if (inpre) { codebuf = codebuf $0 "\n"; next }
if (/^[[:space:]]*$/) { flush_para(); flush_bullet(); next }
if (/^---+$/) { next }
# cite line
if (/^> \*\*[^*]+\*\*/) {
flush_para(); close_list()
line = $0; sub(/^> /, "", line)
print "<cite>" inline_fmt(line) "</cite>" > f
next
}
# table
if (/^\|/) {
flush_para(); close_list()
if (/^\|[-| ]+\|$/) { next }
if (!intable) { emit_table_open(); intable = 1; tablerow = 0 }
tablerow++
emit_table_row($0, tablerow)
next
}
if (intable) { emit_table_close(); intable = 0; tablerow = 0 }
# numbered list
if (/^[0-9]+\. /) {
flush_para()
if (inlist && listtype != "ol") close_list()
if (!inlist) { print "<ol>" > f; inlist = 1; listtype = "ol" }
close_sublist()
close_openli()
flush_bullet()
line = $0; sub(/^[0-9]+\. /, "", line)
bullet = line
next
}
# unordered list
if (/^\* / || /^- /) {
flush_para()
if (inlist && listtype != "ul") close_list()
if (!inlist) { print "<ul>" > f; inlist = 1; listtype = "ul" }
close_sublist()
close_openli()
flush_bullet()
line = $0; sub(/^[\*-] +/, "", line)
bullet = line
next
}
# sub-list (2-4 spaces indent)
if (/^[ ][ ][-*] / || /^[ ][ ][ ][-*] / || /^[ ][ ][ ][ ][-*] /) {
flush_para(); flush_bullet()
if (!insublist && inlist) { print "<ul>" > f; insublist = 1 }
line = $0; sub(/^[ ]+[-*] +/, "", line)
print "<li>" inline_fmt(line) "</li>" > f
next
}
# continue bullet
if (bullet != "" && /^ [^*-]/) {
line = $0; sub(/^ +/, "", line)
bullet = bullet " " line
next
}
# paragraph
close_list()
if (para == "") para = $0
else para = para " " $0
}
END {
# Handle documents with only intro (no ## sections)
if (n == 0 && intro != "") {
introtitle = (doctitle != "") ? doctitle : title
create_intro_section(intro, introtitle)
}
if (n == 0) {
print "hellodocs: error - no content found" > "/dev/stderr"
print "hellodocs: input file must have '# Title' header" > "/dev/stderr"
exit 1
}
close_section()
# Resolve internal #anchor links in all content files
for (i = 1; i <= n; i++) {
fname = "hellodocs-" i ".html"
content = ""
while ((getline line < fname) > 0) {
content = content line "\n"
}
close(fname)
# Resolve links to sections and subsections
for (k = 1; k <= n; k++) {
gsub("href=\"#" sections[k] "\" class=\"internal\"", "href=\"hellodocs-" k ".html\" target=\"c\"", content)
for (j = 1; j <= subcount[k]; j++) {
gsub("href=\"#" subsections[k, j] "\" class=\"internal\"", "href=\"hellodocs-" k ".html#" subsections[k, j] "\" target=\"c\"", content)
}
}
gsub(/ class="internal"/, "", content)
printf "%s", content > fname
close(fname)
}
# Resolve internal #anchor links in nav
for (i = 1; i <= n; i++) {
gsub("href=\"#" sections[i] "\" class=\"internal\"", "href=\"hellodocs-" i ".html\" target=\"c\"", nav)
for (j = 1; j <= subcount[i]; j++) {
gsub("href=\"#" subsections[i, j] "\" class=\"internal\"", "href=\"hellodocs-" i ".html#" subsections[i, j] "\" target=\"c\"", nav)
}
}
gsub(/ class="internal"/, "", nav)
# Navigation file
print html_head(title) "<body leftmargin=\"25\" topmargin=\"25\" bgcolor=\"" NAV_BG "\" text=\"" TEXT "\" link=\"" TEXT "\" vlink=\"" TEXT "\" alink=\"" ALINK "\">" nav "</body></html>" > "hellodocs-nav.html"
# Title bar file
print html_head(title) "<body bgcolor=\"" TEXT "\" topmargin=\"15\" leftmargin=\"25\"><font size=\"5\" color=\"white\" face=\"Helvetica\">" title "</font></body></html>" > "hellodocs-title.html"
# Noscript for crawlers
noscript_nav = nav
gsub(/ target="c"/, "", noscript_nav)
noscript = "<noscript>" noscript_nav "</noscript>"
# Index frameset
print html_head(title) "<frameset border=\"0\" rows=\"60,*\"><frame src=\"hellodocs-title.html\" noresize frameborder=\"0\" scrolling=\"no\" /><frameset border=\"0\" cols=\"25%,75%\"><frame src=\"hellodocs-nav.html\" frameborder=\"0\" /><frame name=\"c\" src=\"hellodocs-1.html\" frameborder=\"0\" scrolling=\"yes\" /></frameset></frameset>" noscript "</html>" > "index.html"
print "hellodocs: index.html + " n " sections" > "/dev/stderr"
}
#---end of file---