-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.awk
executable file
·130 lines (108 loc) · 3.36 KB
/
generate.awk
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
#!/usr/bin/env gawk -f
function acopy(a, b) {
delete b
for (k in a) {
b[k] = a[k]
}
}
function target(s) {
return targetdir s
}
function ensure_exists(path) {
system("mkdir " path " 2>/dev/null | true")
}
function chapter_link(record, alias) {
return "[[" target(record["BOOK"]) "/" record["BOOKCHAPTER"] " " record["CHAPTER"] "|" alias "]]"
}
function book_link(record) {
return "[[" target(record["BOOK"]) "|" record["BOOK"] "]]"
}
BEGIN {
OFS=""
delete blocks; delete current; delete previous;
targetdir = "av/"
ensure_exists(targetdir)
kjv_toc = target("_index.md")
# print top level TOC front matter
print "---" >> kjv_toc
print "tags: index, Bible, KJV" >> kjv_toc
printf "---\n" >> kjv_toc
}
# collect :::SET K V state
match($0, /^:::SET\s+(\w+)\s+(.*)/, ord) {
if (length(blocks) > 0 && length(current)==0) {
acopy(meta, current)
}
meta[ord[1]] = ord[2];
}
# begin TOC
match($0, /^:::SET\s+TESTAMENT\s+(.*)/, ord) {
print "- [[" ord[1] " Testament]]" >> kjv_toc
}
match($0, /^:::SET\s+BOOK\s+(.*)/, ord) {
meta["BOOKCHAPTER"] = ord[1]
print " - " book_link(meta) >> kjv_toc
}
match($0, /^:::SET\s+CHAPTER\s+(.*)/, ord) {
chapter = ord[1]
if (chapter != "END") {
if (chapter == "1") {
book_toc = target(meta["BOOK"]) ".md"
# print book index header
testament_tag = meta["TESTAMENT"] " Testament"
gsub(" ", "_", testament_tag)
print "---" >> book_toc
print "title: ", meta["TITLE"] >> book_toc
print "tags: index, Bible, KJV, ", testament_tag >> book_toc
printf "---\n\n" >> book_toc
printf "[[av/_index|Index]]\n\n" >> book_toc
print "## ", meta["TITLE"] >> book_toc
printf "\n" >> book_toc
}
index_line = "- " chapter_link(meta, meta["BOOKCHAPTER"] " " chapter)
print " ", index_line >> kjv_toc
print index_line >> book_toc
}
}
# end TOC
# flush signal, print chapter
/^:::SET\s+CHAPTER\s.*/ {
if (length(blocks) > 0) {
book_path = target(current["BOOK"])
ensure_exists("'" book_path "'")
of = book_path "/" current["BOOKCHAPTER"] " " current["CHAPTER"] ".md";
testament_tag = current["TESTAMENT"] " Testament"
gsub(" ", "_", testament_tag)
# metadata block
print "---" >> of
print "tags: Bible, KJV, ", testament_tag >> of
printf "---\n\n" >> of
# navigation block
if (length(previous)!=0) {
printf "%s ", chapter_link(previous, "<< " previous["BOOKCHAPTER"] " " previous["CHAPTER"]) >> of
}
printf "| %s |", book_link(current) >> of
if (meta["CHAPTER"]!="END") {
printf " %s", chapter_link(meta, meta["BOOKCHAPTER"] " " meta["CHAPTER"] " >>") >> of
}
printf "\n\n" >> of
print "### ", current["BOOKCHAPTER"], " " current["CHAPTER"] >> of
# chapter and verses
for (i = 1; i <= length(blocks); i++) {
print "" >> of
print blocks[i] >> of
}
delete blocks
acopy(current, previous)
delete current
}
}
/^:::.*/ { next }
# push a verse
match($0, /^([0-9]+) .*/, ord) {
verse = ord[1]
blocks[length(blocks)+1]=$0 " ^" verse
next
}
# push a note
{ blocks[length(blocks)+1]=$0 }