-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMAKEMANUALHTML
executable file
·458 lines (371 loc) · 13.4 KB
/
MAKEMANUALHTML
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env bash
#
# MAKEMANUAL: MANUAL/*.txt から MANUALHTML を作成する
#
# : written by 柳 日向 ([email protected]; 2024-02-11)
#
####################################################################
#実行用環境変数の設定
LANG=ja_JP.UTF-8
####################################################################
#ベースとなる一時ファイルの指定
tmp=$(mktemp)
exec 2> >(cat > /dev/stderr; rm "${tmp}"*)
####################################################################
#作業ディレクトリとコンテンツ区分の指定
commands=COMMANDS
dir=$(realpath .)
if hash gawk 2> /dev/null; then
awk() { gawk "$@"; }
elif [ "$(uname)" = Darwin ]; then
echo "GNU awk is required." >&2
exit 1
fi
if ! hash tidy; then
echo '`tidy` command is required to tidy MANUALHTML up.' >&2
tidy() { cat "$1"; }
fi
ERROR_CHECK() {
if awk '{sum = 0; for (i = 1; i <= NF; i++) if ($(i) > 0) sum = 1} END {exit !sum}' <<< "${PIPESATUS[@]}"; then
echo "An error occured." >&2
exit 1
fi
}
html2man() {
TARGET=${1}
if [ -z "${uec:-}" ]; then
mkdir -p MANUALHTML
OUT=MANUALHTML/${TARGET}.html
else
mkdir -p MANUALUEC
SECTION=$(sed -n 3p "MANUAL/${TARGET}.txt" |
sed -n -e 's|^.*(\([15]\))$|\1|p');
OUT=MANUALUEC/MAN${SECTION}_${TARGET}
fi
OUTDIR=${2}
####################################################################
#テンプレートから基本コンテンツを生成
{
if [ -z "${uec:-}" ]; then
awk < ${dir}/MANUALHTML/COMMON/HTML/TEMPLATE.HTML '/^<!--CONTENTS_CONTENTS-->$/ {exit} 1'
else
echo -n
fi |
sed 's/<!--CONTENTS_TITLE-->/'"${TARGET} - "'/'
version=`
egrep 2> /dev/null "^_version = " "${dir}/COMMANDS/${TARGET}" |
cut -d" " -f 3-9 |
sed -e 's/^.\(.*\)./\1/' `
commands_pattern=$(
find "${dir}/COMMANDS" -type f -printf "%f\n" |
awk '
NR == 1 {printf $0; next}
{printf "|" $0}'
)
awk -vtmp="${tmp}" '
NR <= 2 {next}
/^last modified:/ {
sub(/^last modified: /, "");
# MANUAL/* の最終更新日時を $tmp-last-modified に書き込む。
print > (tmp "-lastmodified")
exit
} 1' "MANUAL/${TARGET}.txt" |
awk -vuec="${uec:-}" \
-vcommands_pattern="(${commands_pattern}|sorter)[(]1[)]" \
-vcommand="${TARGET}" '
function hyperlink_category5(content) {
if (uec) {
format = "?POMPA=MAN5_%s"
} else {
format = "%s.html"
}
content = gensub(/フィールド形式\(5\)/, "<a href=\"" sprintf(format, "field-format") "\" class=\"command\">\\0</a>", "g", content)
content = gensub(/マスタファイル\(5\)/, "<a href=\"" sprintf(format, "master-file") "\" class=\"command\">\\0</a>", "g", content)
content = gensub(/ネームファイル\(5\)/, "<a href=\"" sprintf(format, "name-file") "\" class=\"command\">\\0</a>", "g", content)
content = gensub(/ネーム形式\(5\)/, "<a href=\"" sprintf(format, "name-format") "\" class=\"command\">\\0</a>", "g", content)
content = gensub(/タグ形式\(5\)/, "<a href=\"" sprintf(format, "tag-format") "\" class=\"command\">\\0</a>", "g", content)
content = gensub(/num=<n>/, "<code>\\0</code>", "g", content)
return gensub(/トランザクションファイル\(5\)/, "<a href=\"" sprintf(format, "transaction-file") "\" class=\"command\">\\0</a>", "g", content)
}
function prettyprint(content) {
# `<\/?タグ[^<]*>` は formhame のため。
if (match(content, /<タグ \/>|<タグ>|<\/タグ>|input type="checkbox"/)) {
return gensub(/<タグ \/>|<タグ>|"."|<\/タグ>|(\w+ )?\w+="\w+"/, "<code>\\0</code>", "g", content);
}
# スクリプト以外の複数行にわたるコード断片は multiline クラスを付加する。
if (match(content, /\n\n/)) {
return gensub(/\n*([-&;A-Za-z\\-][ -&*-~\n]*[!-&*-~])\n*/, "<code class=\"multiline\">\\1</code>", "g", content)
}
# コード断片に準じる用語。
content = gensub(/(/, "(", "g", content)
content = gensub(/)/, ")", "g", content)
content = gensub(/\s*(keycut\.c|'"${commands_pattern}"'|(lock|mime|counter)-file|<(file|filename|(json|lock)-file|counter-file)>|%0D%0A|NFS|NF(-[0-9x]+)?|-<.>|\\_|+(age|ng|yarr|[a-z])|\x27.{1,2}\x27|\\n|+<n>h|--|--?[a-z]+( <[-a-z_]+>)*|(<\w+>)+( +<\w+>)*|([0-9][\/@][0-9]\x20?)+|"(.|NF)"|(")?[a-z]+=([\x2F0-9a-z;=@.]|"|<|>|&)*)|<\w{5,}>|awk[(]1\)|\x27..?\x27)\s*/, "<code>\\1</code>", "g", content)
content = hyperlink_category5(content)
content = gensub(/touch [(]1[)]/, "<a href=\"https://linuxjm.osdn.jp/html/gnumaniak/man1/touch.1.html\">\\0</a>", "g", content)
content = gensub(/([^0-9])([0-9]{6,})([^0-9])/, "\\1<code>\\2</code>\\3", "g", content)
content = gensub(/(\w)オプション/, "<code>\\1</code>オプション", "g", content)
content = gensub(/(^|[^-A-Za-z0-9])([-])([^-A-Za-z0-9&])/, "\\1<code>\\2</code>\\3", "g", content)
content = gensub(/<\x2Fcode><code>/, " ", "g", content)
content = gensub(/<\x2Fcode> /, "</code>", "g", content)
return content
}
FNR == 1 { sub(/\xef\xbb\xbf/, "") }
/^Version :/ {
content = content "Version : '"${version}"'\n"
next
}
function print_content() {
if (content == "") {
return
}
gsub(/&/, "\\&", content);
gsub(/</, "\\<", content);
gsub(/>/, "\\>", content);
gsub(/"/, "\\"", content);
gsub(/[ \t]+$/, "", content);
if (mode == "list" || mode == "prettyprint") {
gsub(/\s+$/, "", content)
printf "<pre class=\"%s\">%s</pre>\n", mode, content
} else if (mode == "unordered_list" || mode == "ordered_list") {
tag = mode == "unordered_list" ? "ul" : "ol"
print " <" tag ">"
split(content, items, /\s*\n\s*/)
label = ""
for (position = 0; position < length(items); position++) {
if (items[position] ~ /^\s+$/) {
continue
}
if (match(items[position], /^\s*([0-9]+)\.\s*(.*)\s*$/, matches)) {
if (label !~ /^\s*$/) {
printf "<li>%s</li>\n", prettyprint(label)
}
label = matches[2]
} else {
label = label items[position] "\n"
}
}
if (label !~ /^\s*$/) {
printf " <li>%s</li>\n", prettyprint(label)
}
print "</" tag ">"
} else {
content = gensub(/[ \t]+/, " ", "g", content)
content = gensub(/\n+/, "", "g", content)
content = gensub(/[((] +([^))]+) +[))]/, "(\\1)", "g", content)
content = gensub(/ {2,}/, " ", "g", content)
if (content == "") {
next
}
if (name_has_occured) {
if (uec) {
content = gensub(commands_pattern, "<a href=\"?POMPA=MAN1_\\1\" class=\"command\">\\0</a>", "g", content)
} else {
content = gensub(commands_pattern, "<a href=\"\\1.html\" class=\"command\">\\0</a>", "g", content)
}
if (match(content, /([-a-z0-9_]+)\(1\)|[ぁ-ん亜-熙]+\(5\)/)) {
content = hyperlink_category5(content)
content = gensub(/-?<数値>/, "<code>\\0</code>","g", content)
content = gensub(/gunzip *[(]1[)]/, "<a href=\"https://linuxjm.osdn.jp/html/GNU_gzip/man1/zcat.1.html\">\\0</a>", "g", content)
content = gensub(/paste *[(]1[)]/, "<a href=\"https://linuxjm.osdn.jp/html/gnumaniak/man1/paste.1.html\">\\0</a>", "g", content)
content = gensub(/(^|\n+)([-&;A-Za-z][ !#-&*-;=?-~(]*[!#-&*-;=?-~])\n*/, "<code>\\2</code>", "g", content)
content = gensub(/<\x2Fcode> /, "</code>", "g", content)
} else {
content = gensub(/\n+/, "", "g", content)
content = prettyprint(content);
}
} else {
content = gensub(/\s*\n+\s*/, "", "g", content)
content = gensub(/\n*([-&;0-9A-Za-z][!-&*-~]*) :/, "<code>\\1</code> : ", "g", content)
}
name_has_occured = 1
printf "<p>%s</p>\n", gensub(/\n+/, "", "g", content)
}
content = ""
mode = ""
}
NR == 1 {
print_content()
level = uec ? 1 : 2
printf "<h%d>%s</h%d>\n", level, $0, level
next
}
$0 ~ /^Usage +:/ || $0 ~ /^ /{mode = "list"}
command == "check_cmp_name" && $0 ~ /^ / {
if (mode != "check_cmp_name") {
print "<pre>"
}
mode = "check_cmp_name"
print $0
next
}
command == "formhame" && $0 ~ /^【備考】$/ {
inside_note = 1
}
command == "formhame" && inside_note && $0 ~ /^name_(radio|checkbox|pulldown) [a-z0-9]+$/ {
if (mode != "formhame") {
print "<pre style=\"margin-left: 3.5em;\">"
}
mode = "formhame"
print $0
next
}
command == "getfirst" && $0 ~ /^【注意】/ {
inside_note = 1
}
command == "getfirst" && inside_note && $0 ~ /^0000007/{
if (mode != "getfirst") {
print "<pre style=\"margin-left: 3.5em;\">"
}
mode = "getfirst"
print $0
next
}
command == "msort" && $0 ~ /^ msort/ {
if (mode != "msort") {
print "<pre style=\"margin-left: 3.5em;\">"
}
mode = "msort"
print $0
next
}
command == "ulock" && match($0, /^ (.--) (.+)$/, matches) {
print "<p><em style=\"margin-right: 0.5em; letter-spacing: 0.5em; font-family: monospace;\">" matches[1] "</em>" matches[2]"</p>"
next
}
/^([a-z]+) コマンド = (.+)$/ {
print "<p><code>" $1 "</code> コマンド: " $4 "</p>"
next
}
/^-([a-z]{4}) +[-A-Za-z0-9]+ *から *[-A-Za-z0-9]+$/ {
if (mode != "uconv") {
mode = "uconv"
print "<ul>"
}
print "<li><code>" $1 "</code>: <code>" $2 "</code> から <code>" $4 "</code></li>"
next
}
substr($0, 0, 1) == "$" ||
$0 ~ /^ [^ ]/ ||
substr($0, 0, 4) == "[usp" ||
substr($0, 0, 7) =="before1" ||
substr($0, 0, 8) == "yyyymmdd" {
if (mode == "" || mode == "unordered_list" || mode == "ordered_list") {
print_content()
}
mode = "prettyprint"
}
/^【.+】.*$/ {
print_content()
gsub(/&/, "\\&");
gsub(/</, "\\<");
gsub(/</, "\\>");
level = uec ? 2 : 3
printf "<h%d>%s</h%d>\n", level, $0, level
mode = ""
next
}
match($0, /^<([亜-熙ぁ-んァ-ヶ].+)>$/, title) {
print_content()
level = uec ? 3 : 4
printf "<h%d>%s</h%d>\n", level, title[1], level
mode = ""
next
}
/^[ \t]*1:.+$/ {
print_content()
print "<dl class=\"layout\">"
for (position = 1; position <= NF; position++) {
split($(position), label, ":")
printf "<dt>%s</dt>\n", label[1]
printf "<dd>%s</dd>\n", label[2]
}
print "</dl>"
next
}
/^- / {
mode = "unordered_list";
$1 = "";
print "<li>" hyperlink_category5($0) "</li>";
next;
}
/^[ \t]+1\..+$/ {
mode = "ordered_list"
}
NF == 0 && ( \
mode == "" ||
mode == "prettyprint" ||
mode == "unordered_list" ||
mode == "ordered_list" ||
mode == "formhame" ||
mode == "getfirst" ||
mode == "msort" ||
mode == "uconv" ) {
print_content()
if (mode == "ordered_list") {
print "</ol>"
}
if (mode == "unordered_list" || mode =="uconv") {
print "</ul>"
}
if (mode == "formhame" ||
mode == "getfirst" ||
mode == "msort") {
print "</pre>"
}
mode = ""
next
}
mode != "" {
content = content $0 "\n"
next
}
mode == "" || mode == "unordered_list" || mode == "ordered_list" {
content = content $0 "\n"
}
END {print_content()}'
if [ -z "${uec:-}" ]; then
sed -n '/CONTENTS_CONTENTS/,$p' ${dir}/MANUALHTML/COMMON/HTML/TEMPLATE.HTML
else
echo -n
fi |
tail -n+2 |
sed 's/<!--YEAR-->/'"$(date +%Y)"'/' |
sed 's/<!--LASTMODIFIED-->/'"$(
egrep '^last modified:' "${dir}/MANUAL/${TARGET}.txt" |
cut -d " " -f3- )"'/'
ERROR_CHECK
} |
# マニュアル・メニューを作成する。
awk -vcommands_string="$(find ${dir}/COMMANDS -not -path "${dir}/COMMANDS/.*" -type f -printf "%f " | sort -u)" '
/^<!-- CONTENTS_MENU -->/ {
template = ""
for (position = 4; position <= NF; position++) {
template = template $(position) (position < NF ? " " : "")
}
split(commands_string, commands, " ")
printf(" ")
for (position = 1; position <= length(commands); position++) {
printf(template "\n ", commands[position], commands[position])
}
for (position = 0; position <= NR % 3 - 1; position++) {
print "<li></li>\n"
}
next
}
1' > "${OUTDIR}/${OUT}"
}
ERROR_CHECK
####################################################################
#コンテンツ出力
mkdir -p "${dir}"
export uec
if [ "$#" -eq 1 ]; then
# 単一コマンドのマニュアルを作成する
html2man "$1" "${dir}"
else
# アルファベット順にコマンド一覧を列挙する
for command in $(tr ' ' '\n' <<< 'field-format master-file name-file name-format tag-format'; find ${commands} -type f -printf "%f\n" | sort); do
echo building a MANUAL"$([ ! -s "${uec:-}" ] && echo -n UEC || echo -n HTML)" of ${command} >&2
html2man "${command}" "${dir}"
done
fi