-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmd2man
executable file
·207 lines (177 loc) · 5.33 KB
/
md2man
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
#!/bin/sh
set -euo pipefail
get_name() {
sed -En '/^---$/,/^---$/{
/^slug:/{
s/\//./g
s/^slug: Web\.(API|JavaScript\.Reference\.Global_Objects)\.//p
q
}
}' "$1"
}
get_title() {
sed -n '/^---$/,/^---$/{
/^title:/{
s/^title:[[:space:]]*//
s/^"//g
s/"$//g
p
q
}
}' "$1"
}
get_date() (
export LC_ALL=C
ls -dog "$1" |
awk -v abmon="$(locale abmon)" \
-v cmonth="$(date +%m)" \
-v cyear="$(date +%Y)" '
BEGIN {
n = split(abmon, months, ";")
while (++i <= n) months[months[i]] = i
}
{
month = months[$4]
day = $5
year = ($6 ~ /:/) ? (month > cmonth ? cyear-1 : cyear) : $6
printf "%04d-%02d-%02d\n", year, month, day
}'
)
yn() {
case $1 in
0) echo no;;
1) echo yes;;
esac
}
js_property_attributes() {
for i in 0 1; do
for j in 0 1; do
for k in 0 1; do
printf '/^{{js_property_attributes\(%d, %d, %d\)}}$/c\\\n| | |\\\n|-|-|\\\n|Writable|%s|\\\n|Enumerable|%s|\\\n|Configurable|%s|\\\n\n' \
"$i" "$j" "$k" "$(yn "$i")" "$(yn "$j")" "$(yn "$k")"
done
done
done
}
space="[[:space:]]*"
str="(\"([^\"]+)\"|'([^']+)')"
empty="(\"\"|'')"
ref() {
printf '%s\n' "
s/{{$1\($str\)}}/\`\2\3\`/ig
s/{{$1\($str,$space$str\)}}/\`\5\6\`/ig
s/{{$1\($str,$space$empty,$space$str\)}}/\`\2\3\`/ig
s/{{$1\($str,$space$str,$space$str\)}}/\`\5\6\`/ig
s/{{$1\($str,$space$empty,$space$empty,[^)]+\)}}/\2\3/ig
s/{{$1\($str,$space$str,$space$empty,[^)]+\)}}/\5\6/ig
s/{{$1\($str,$space$empty,$space$str,[^)]+\)\)}}/\2\3/ig
s/{{$1\($str,$space$str,$space$str,[^)]+\)}}/\5\6/ig
"
}
text() {
printf '%s\n' "
s/{{$1\($str\)}}/\2\3/ig
s/{{$1\($str,$space$str\)}}/\5\6/ig
"
}
element() {
printf '%s\n' "
s/{{$1\($space$str$space\)}}/\`<\2\3>\`/ig
s/{{$1\($str,$space$str\)}}/\`\5\6\`/ig
"
}
info() {
printf '%s\n' "s/{{$1(\(\))?}}/$2/ig"
}
ignore() {
printf '%s\n' "s/{{$1(\(($str|$empty|[^)'\"]*)*\))?}}//ig"
}
clean() {
sed -E "
/^---$/,/^---$/c\\
---\\
title: $(get_name "$1")\\
section: JS\\
header: JavaScript Reference Manual\\
footer: JavaScript\\
date: $(get_date "$1")\\
---\\
# Name\\
$(get_name "$1") - $(get_title "$1")\\
\\
# Synopsis
/^## Specifications/,/{{Specifications}}/d
/^## Browser compatibility/,/{{Compat}}/d
/^[>[:space:]]*\`\`\`[^[:space:]]+ hidden$/,/^[>[:space:]]*\`\`\`$/d
s/^([>[:space:]]*)(\`\`\`[^[:space:]]+) example-bad$/\1**Bad**\n\1\2/
s/^([>[:space:]]*)(\`\`\`[^[:space:]]+) example-good$/\1**Good**\n\1\2/
s/^#//
/{{[^}]*$/N
/{{[^}]*$/N
/{{[^}]*$/N
s/{{[[:space:]]*([^}]*[^}[:space:]])[[:space:]]*}}/{{\1}}/g
$(js_property_attributes)
$(ignore APIRef)
$(ignore DefaultAPISidebar)
$(ignore DOMAttributeMethods)
$(ignore EmbedInteractiveExample)
$(ignore EmbedGHLiveSample)
$(ignore EmbedLiveSample)
$(ignore InheritanceDiagram)
$(ignore JSRef)
$(ignore jsSidebar)
$(ignore HTTPSidebar)
$(ignore SeeCompatTable)
$(ref CSP)
$(ref cssxref)
$(ref domxref)
$(ref HTTPHeader)
$(ref HTTPMethod)
$(ref HTTPStatus)
$(ref jsxref)
$(ref SVGAttr)
$(ref WebExtAPIRef)
$(info AvailableInWorkers '\n\n**Note:** This feature is available in Web Workers.')
$(info deprecated_header '\n\n**Deprecated:** This feature is no longer recommended.')
$(info Deprecated_Inline '*(deprecated)*')
$(info Experimental_Inline '*(experimental)*')
$(info NonStandardBadge '*(non-standard)*')
$(info non-standard_header '\n\n**Non-standard:** This feature is non-standard and is not on a standards track.')
$(info non-standard_inline '*(non-standard)*')
$(info optional_inline '*(optional)*')
$(info ReadOnlyInline '*(read-only)*')
$(info securecontext_header '\n\n**Secure context:** This feature is available only in secure contexts (HTTPS).')
$(info SecureContext_Inline '*(secure context)*')
$(element HTMLElement)
$(element MathMLElement)
$(element SVGElement)
$(text Glossary)
$(text LiveSampleLink)
s/{{AvailableInWorkers\(\"dedicated\"\)}}/\n\n**Note:** This feature is only available in Dedicated Web Workers./ig
s/{{AvailableInWorkers\(\"service\"\)}}/\n\n**Note:** This feature is only available in Service Workers./ig
s/{{AvailableInWorkers\(\"window_and_dedicated\"\)}}/\n\n**Note:** This feature is available in Dedicated Web Workers./ig
s/{{AvailableInWorkers\(\"window_and_service\"\)}}/\n\n**Note:** This feature is available in Service Workers./ig
s/{{AvailableInWorkers\(\"window_and_worker_except_service\"\)}}/\n\n**Note:** This feature is available in Web Workers, except for Service Workers./ig
s/{{AvailableInWorkers\(\"worker\"\)}}/\n\n**Note:** This feature is only available in Web Workers./ig
s/{{AvailableInWorkers\(\"worker_except_service\"\)}}/\n\n**Note:** This feature is only available in Web Workers, except for Service Workers./ig
s/{{RFC\(\"?([0-9]+)\"?\)}}/RFC \1/ig
s/{{RFC\(([0-9]+),$space$str\)}}/RFC \1: \3\4/ig
s/{{RFC\(([0-9]+),$space$empty,$space([0-9.]+)\)}}/RFC \1, section \3/ig
s/{{RFC\(\"?([0-9]+)\"?,$space$empty,$space$str\)}}/RFC \1, section \4\5)/ig
s/{{RFC\(\"?([0-9]+)\"?,$space$str,$space$str\)}}/RFC \1, section \6\7: \3\4/ig
s/\[!CALLOUT]//g
s/\[!NOTE]/**Note:**/g
s/\[!WARNING]/**Warning:**/g
" "$1"
}
convert() {
pandoc --fail-if-warnings --lua-filter=filter.lua -s \
-f markdown-table_captions-markdown_in_html_blocks -t man
}
cleaned="$(clean $1)"
unknown=$(printf %s "$cleaned" | { grep -o '{{[^}]*}}' || :; } | sort | uniq)
if [ "$unknown" ]; then
printf >&2 'unknown macros: %s\n' "$unknown"
exit 1
fi
printf %s "$cleaned" | convert