Summary Files Commits Branches @commit @diff @stats
path: root / single.awk
blob: 47cb391adbf90ca3fa46899c9cf5de3f762f3bbc (plain) (blame)
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
# Status:
# [.] Inline HTML
# [ ] Autoescape
# [.] Block elements
#     [x] Paragraph
#     [x] Line break
#     [x] Headers (atx and setex)
#     [x] Blockquotes
#     [ ] Lists
#     [ ] Horizontal rules
#     [x] Code blocks
# [ ] Span elements
#     [ ] Links
#         [ ] Inline
#         [ ] Reference
#     [ ] Emphasis
#     [ ] Code
#     [ ] Images
#         [ ] Inline
#         [ ] Reference
# [ ] Other
#     [ ] Autolinks
#     [ ] Escapes
#
# [ ] Non-standard features
#     [ ] Strikethrough ~~...~~
#     [ ] Superscript ^...^
#     [ ] Subscript ~...~
#     [ ] Footnote [^...] (with user customizations)
#     [ ] Task list - [ ] (readonly)
#     [ ] Definition lists
#         [x] Single line
#         [ ] Multi-line
#     [ ] Fenced code blocks ```lang ```
#     [ ] Tables
#     [ ] Links
#         [ ] Link destination (<link> inside anchors)
#         [ ] Nested links
# No character classes, since they aren't supported on every system
#
# Limitations:
# - definition lists always contain one element, i.e. multiple definitions
#   aren't merged into the same list
# - emphasis which isn't surrounded by blanks is still treated as emphasis
#   e.g. "some_text_here" will become "some<em>text</em>here"
# - emphasis inside inline code block is still converted to HTML
# - backticks inside `` emphasis blocks aren't escaped

function empty(value) {
	return value == ""
}

function _next() {
	__buffered_print()
	not_paragraph = 1
	next
}

{
	not_paragraph_p1 = not_paragraph
	not_paragraph = 0
	empty0  = empty($0)
	emptyP1 = empty(P1)
}

# Output is buffered on multiple lines, held in these variables:
# P2 = previous non-empty line
# P1 = previous line
# $0 = current line
# N1 = next new line (N1 is always set by the user)

# Inline HTML
/^[ \t]*<\/?[0-9A-Za-z]+/ {
	_next()
}

# Blockquotes
blockquote_level > 0 && empty0 {
	for (P1 = P1 ORS; blockquote_level > 0; --blockquote_level) {
		P1 = P1 "</blockquote>"
	}
	not_paragraph_p1 = 1
}
/^>[ \t]/ {
	match($0, /^[> \t]+[ \t]+/)
	level = substr($0, RSTART, RLENGTH)
	$0 = substr($0, RSTART + RLENGTH)
	gsub(/[ \t]/, "", level)
	level = length(level)

	for (;blockquote_level < level; ++blockquote_level) {
		P1 = P1 "<blockquote>"
	}
	if (blockquote_level > level) { P2 = P2 ORS }
	for (;blockquote_level > level; --blockquote_level) {
		P2 = P2 "</blockquote>"
	}
	not_paragraph = 1
}
/^>$/ { $0 = ""; not_paragraph = 1 }

# Code blocks
/^    / {
	if (!inside_codeblock) {
		P1 = P1 "<pre><code>"
		inside_codeblock = 1
	}
	_next()
}
inside_codeblock && ! /^[ \t]*$/ {
	if (emptyP1) {
		P2 = P2 ORS "</code></pre>"
	}
	else {
		P1 = P1 ORS "</code></pre>"
	}
	inside_codeblock = 0
	emptyP1 = 1
}

# Line break
/  $/ {
	sub(/[ \t]*  $/, "</br>")
}

# atx-style headers
/^[ \t]*#+/ {
	match($0, /#+/)
	if (RLENGTH <= 6) {
		## Remove prefix/suffix
		gsub(/^[ \t]*#+[ \t]*/, "")
		gsub(/[ \t]*#+[ \t]*$/, "")

		$0 = "<h" RLENGTH ">" $0 "</h" RLENGTH ">"
		not_paragraph = 1
	}
}

# setex-style headers
!emptyP1 && /^[ \t]*(=|-)+[ \t]*$/ {
	hlevel = 1 + ($0 ~ /-/)
	P1 = "<h" hlevel ">" substr(P1, 4) "</h" hlevel ">"
	$0 = ""
	not_paragraph = 1
}

# Paragraph
emptyP1 && !empty0 && !not_paragraph { $0 = "<p>" $0 }
!emptyP1 && empty0 && !not_paragraph_p1 { P1 = P1 "</p>" }

# After all $0 parsing
{
	__buffered_print()
}
function __buffered_print() {
	empty0  = empty($0)
	emptyP1 = empty(P1)

	if (empty0 && emptyP1) {
		++P1_repeats
	}
	else if (!empty0 && emptyP1) {
		P1 = $0
	}
	else {
		print P2
		while (P1_repeats > 0) {
			print ""
			--P1_repeats
		}
		P2 = P1
		P1 = $0
		if (empty0) {
			P1_repeats = 1
		}
	}

	if (!empty(N1)) {
		if (emptyP1) {
			P1 = N1
		}
		else {
			print P2
			while (P1_repeats > 0) {
				print ""
				--P1_repeats
			}
			P2 = P1
			P1 = N1
			N1 = ""
		}
	}
}

END {
	print P2
	while (P1_repeats > 0) {
		print ""
		--P1_repeats
	}
	if (!empty(P1)) { print P1 }
	print
	if (!empty(N1)) { print N1 }
}