about summary refs log tree commit diff stats
path: root/ht3.awk
blob: 5226af7316cad4260bc298e1254042032e9a8653 (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
#!/usr/bin/awk -f
# -*- indent-tabs-mode: t; -*-
# HAT TRICK
# (C) 2022 C. Duckworth

### Commentary:

### Code:
BEGIN {
	split("html,gemini,gopher", HT_FORMATS_AVAILABLE, ",")
	process_arguments()
	normalize_ht_formats()
	if (! HT_TAGCHARS[1]) {
		split("b:**,i://,code:``", HT_TAGCHARS, ",")
	}
	# Output buffer.  The output will be chunked into blocks.
	BUFFER = ""
	# Default block type
	DEFAULT_BLOCK = "p"
	# The current block type.  We start with a standard paragraph.
	BLOCK = DEFAULT_BLOCK
}

### RAW TEXT
/^>>>/ {
	BLOCK = "raw"
	if ($1) {
		split($1, raw_type, ",")
	}
}

/^<<</ {
	BLOCK = DEFAULT_BLOCK
	for (type in raw_type) {
		delete raw_type[type]
	}
}

### BLOCKS
block == "raw" {
	bufpush($0, "\n")
	next
}

/^#+/ {	# Headers
	match($0, /^#+/)
	if (! (BLOCK == "h" RLENGTH)) {
		buflush()
		tag = "h" RLENGTH
	}
	sub(/^#+[ \t]*/, "", $0)
}

/^>/ {	# Block quote
}

/^-/ {	# Unordered list
}

/^[0-9]\./ {	# Ordered list
}

/^---$/ {	# Section break
}

/^$/ {	# End a block
	buflush()
	BLOCK = DEFAULT_BLOCK
}

### LINES
/^=>/ {	# Link
}

/^</ {	# HTML tag
}

/^;/ {	# Comment
}

# EVERYTHING ELSE
/./ {
}

### FINISH
END {
	buflush()
}


### FUNCTIONS
function buflush()
{
	# Print the buffer and close the current block.
	if (BUFFER) {
		ht_print(BUFFER)
	}
	BUFFER = ""
	if ("html" in HT_FORMATS && BLOCK != "raw") {
		ht_print("</" BLOCK ">")
	}
	print
}

function bufpush(str, sep)
{
	# Push STR onto the buffer after SEP (defaults to space).
	sep = sep ? sep : " "
	BUFFER = BUFFER (BUFFER ? sep : "") str
}

function ht_print(str)
{
	if (HT_FORMATS_COUNT == 1) {
		print str
	} else {
		split(str, arr, "\n")
		for (format in HT_FORMATS) {
			line = 1
			while (arr[line]) {
				printf "%s\t%s\n", format, arr[line++]
			}
		}
	}
}

function html_escape(str)
{
	# Escape HTML entities and beginning-line spaces.
	gsub(/&/, "\\&amp;", t)
	gsub(/</, "\\&lt;", t)
	gsub(/>/, "\\&gt;", t)
	sub(/^ /, "\\&nbsp;", t)
	return t
}

function normalize_ht_formats()
{
	for (format in HT_FORMATS_AVAILABLE) {
		normat[format] = 0
	}
	if (! HT_FORMATS[1]) {
		for (i in HT_FORMATS_AVAILABLE) {
			HT_FORMATS[i] = HT_FORMATS_AVAILABLE[i]
		}
	}
	for (format in HT_FORMATS) {
		if (format == "all") {
			for (i in HT_FORMATS_AVAILABLE) {
				HT_FORMATS[i] = HT_FORMATS_AVAILABLE[i]
			}
			return
		} else if (format ~ /^-/) {
			delete normat[substr(format, 2)]
		} else {
			normat[format] = 1
		}
	}
	for (format in normat) {
		if (normat[format]) {
			HT_FORMATS[format] = format
		}
	}
	for (format in HT_FORMATS) {
		HT_FORMATS_COUNT++
	}
}

function process_arguments()
{
	a = 1
	HT_FORMATS[1] = 0
	HT_TAGCHARS[1] = 0
	while (ARGV[a]) {
		if (a == "-c" || a ~ /^--chars=/) {
			# HTML tag <-> markup character correspondance
			if (a == "-c") {
				a++
			} else if (a ~ /^--chars=/) {
				sub(/^[^=]*=/, "", a)
				# HT_TAGCHARS is an array
			}
			split(a, HT_TAGCHARS, ",")
		} else if (a == "-f" || a ~ /^--format=/) {
			# Output format
			if (a == "-f") {
				a++
			} else if (a ~ /^--format=/) {
				sub(/^[^=]*=/, "", a)
				# HT_FORMATS is an array
			}
			split(a, HT_FORMATS, ",")
		}
		a++
	}
}