about summary refs log tree commit diff stats
path: root/ht3.awk
blob: 996cb1a041384708015cc8b10b4fcb4512066bbf (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
#!/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 = ""
	# The current block type.  We start with a standard paragraph.
	BLOCK = "p"
}

### RAW TEXT
/^>>>/ {
}

/^<<</ {
}

### BLOCKS
/^#+/ {			# Headers
}

/^>/ {				# Block quote
}

/^-/ {				# Unordered list
}

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

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

### 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)
{
	# Push STR onto the buffer after a newline.
	BUFFER = BUFFER (BUFFER ? "\n" : "") 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++
	}
}