summary refs log tree commit diff stats
path: root/ht.awk
blob: 7581cdb12e74ae1895fe343e81dc5b2ffb0e5d7e (plain)
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
#!/bin/awk -f
# HAT TRICK
# Copyright (C) 2022 Case Duckworth <acdw@acdw.net>
#
BEGIN {
	# Configuration
	DEFAULT_CONFIG_MODE = "config"
	config_initialize()
	config_parse(ENVIRON["HT_CONFIG"] ? ENVIRON["HT_CONFIG"] : "ht.conf")
	# State
	DEFTAG = CONFIG["default_tag"]
	DEFATTR = CONFIG["default_attr"]
	TAG = DEFTAG
	ATTR = DEFATTR
}

# Handle raw sections
$0 ~ CONFIG["raw_delim"] {
	RAW = ! RAW
	if (RAW) {
		buflush()
		bufpush(CONFIG["raw_beg"], -1)
	} else {
		bufpush(CONFIG["raw_end"], -1)
		print BUFFER
		BUFFER = ""
	}
	next
}

RAW {
	bufpush($0)
	next
}

# Comments
$0 ~ ("^" COMMENT_DELIM) {
	next
}

# HTML escape hatch
/^</ {
	HTML = 1
	bufpush($0)
	next
}

# Sure, let's do templating!  This makes it less... weird.
/\$/ {
	# XXX: This is probably the dumbest way to do it.
	gsub(/\$\$/, "$\a", $0)
	gsub(/\$[^\a]/, "\\\\&", $0)
	gsub(/\$\a/, "$", $0)
}

# Blocks of text
/./ {
	# EOL escape
	if (match($0, /\\$/)) {
		sep = -1
		$0 = substr($0, 1, RSTART - 1)
	} else {
		sep = "\n"
	}
	# Loop through BLOCK_TYPES
	for (bt in BLOCK_TYPES) {
		if (match($0, "^" bt "[ \t]*")) {
			$0 = substr($0, RSTART + RLENGTH)
			if (match(BLOCK_TYPES[bt], "[ \t]*>[ \t]*")) {
				parent = substr(BLOCK_TYPES[bt], 1, RSTART - 1)
				child = substr(BLOCK_TYPES[bt], RSTART + RLENGTH)
			}
			if (parent) {
				split(parent, pa, FS)
				split(child, bl, FS)
				if (! IN_PARENT) {
					IN_PARENT = pa[1]
				}
				TAG = IN_PARENT
				ATTR = ""
				for (i = 2; i <= length(pa); i++) {
					ATTR = ATTR (ATTR ? " " : "") pa[i]
				}
				bufpush("<" child ">" $0 "</" bl[1] ">")
				next	# XXX: This is messy.
			} else {
				split(BLOCK_TYPES[bt], bl, FS)
				if (IN_PARENT) {
					bufpush("</" IN_PARENT ">")
					IN_PARENT = ""
				}
				if (! BUFFER) {
					TAG = bl[1]
					for (b = 2; b <= length(bl); b++) {
						ATTR = ATTR (ATTR ? " " : "") bl[b]
					}
				} else {
					$0 = "<" BLOCK_TYPES[bt] ">" $0 "</" bl[1] ">"
				}
			}
		}
	}
	# Loop through LINE_TYPES
	for (lt in LINE_TYPES) {
		if (match($0, "^" lt "[ \t]*")) {
			$0 = substr($0, RSTART + RLENGTH)
			templ = LINE_TYPES[lt]
			while (match(templ, /\$[0-9]+/)) {
				sub(/\$[0-9]+/, $(substr(templ, RSTART + 1, RLENGTH - 1)), templ)
			}
			$0 = templ
		}
	}
	# Push to buffer
	bufpush($0, sep)
}

# Blank lines end blocks
/^$/ {
	if (HTML) {
		html_end()
	}
	if (! RAW) {
		buflush()
	}
}

# Clean up
END {
	if (HTML) {
		html_end()
	} else if (RAW) {
		bufpush(CONFIG["raw_end"], -1)
		print BUFFER
	} else {
		buflush()
	}
}


### Buffer-y functions
function buflush()
{
	buftrim()
	if (BUFFER) {
		if (TAG) {
			TAG_BEG = "<" TAG (ATTR ? " " ATTR : "") ">"
			TAG_END = "</" TAG ">"
		}
		print TAG_BEG BUFFER TAG_END
		BUFFER = ""
		TAG = DEFTAG
		ATTR = DEFATTR
		IN_PARENT = ""
	}
}

function bufpush(text, separator)
{
	if (! separator) {
		separator = "\n"
	}
	if (separator == -1) {
		separator = ""
	}
	BUFFER = BUFFER text (separator ? separator : "")
}

function buftrim()
{
	if (match(BUFFER, "\n+$")) {
		BUFFER = substr(BUFFER, 1, RSTART - 1)
	}
}

### Config functions
function config_initialize()
{
	COMMENT_DELIM = ";"
	CONFIG["raw_delim"] = "```"
	CONFIG["raw_beg"] = "<pre><code>"
	CONFIG["raw_end"] = "</code></pre>"
	CONFIG["default_tag"] = "p"
	CONFIG["default_attr"] = ""
	LINE_TYPES["@"] = "<a href=\"$1\">$2</a>"
	LINE_TYPES["`"] = "<code>$0</code>"
	BLOCK_TYPES["#"] = "h1"
	BLOCK_TYPES["##"] = "h2"
	BLOCK_TYPES["###"] = "h3"
	BLOCK_TYPES["-"] = "ul>li"
}

function config_parse(file)
{
	mode = DEFAULT_CONFIG_MODE
	while ((getline < file) > 0) {
		if (match($0, /^#/) || ! $0) {
			continue
		}
		if (match($0, /^\\/)) {
			$0 = substr($0, 2)
		}
		if (match($0, /\[[^\]]+\]/)) {
			mode = substr($0, RSTART + 1, RLENGTH - 2)
			continue
		} else {
			var = $1
			val = ""
			for (i = 2; i <= NF; i++) {
				val = val (val ? " " : "") $i
			}
			if (mode == "config") {
				CONFIG[var] = val
			} else if (mode == "block") {
				BLOCK_TYPES[var] = val
			} else if (mode == "line") {
				LINE_TYPES[var] = val
			}
		}
	}
}

### Other functions
function html_end()
{
	buftrim()
	print BUFFER
	BUFFER = ""
	HTML = 0
}