@first-ch/tools-mcp 0.4.1 → 0.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -4
- package/aspect-ratio.mjs +706 -0
- package/base64.mjs +356 -0
- package/color.mjs +629 -0
- package/cron.mjs +633 -0
- package/diff.mjs +322 -0
- package/hash.mjs +234 -0
- package/html-escape.mjs +308 -0
- package/json-yaml.mjs +1693 -0
- package/jwt.mjs +639 -0
- package/lib.mjs +103 -0
- package/markdown-table.mjs +641 -0
- package/marp-theme.css +51 -0
- package/marp.mjs +219 -0
- package/package.json +130 -3
- package/px-rem.mjs +488 -0
- package/qr.mjs +744 -0
- package/server.mjs +942 -2
- package/sql-format.mjs +694 -0
- package/testdata.mjs +608 -0
- package/unixtime.mjs +505 -0
- package/url.mjs +415 -0
- package/user-agent.mjs +633 -0
- package/uuid.mjs +238 -0
package/html-escape.mjs
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// HTMLエンティティ・特殊文字のエスケープ / デコード(tools.first-ch.com/html-escape/ と同一ロジック)
|
|
2
|
+
//
|
|
3
|
+
// 「変換コア」ブロックは site 側の site/html-escape/app.js と同一の実装。
|
|
4
|
+
// 2箇所ルール: 片方を直したらもう片方も同じ内容で直す(site側が正本)。
|
|
5
|
+
// 使っているのは String/RegExp だけなので、ブラウザ版のコードをそのまま持ってこられる。
|
|
6
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
7
|
+
import { basename } from 'node:path';
|
|
8
|
+
|
|
9
|
+
/* ==================== ここから変換コア(site/html-escape/app.js と同一) ==================== */
|
|
10
|
+
|
|
11
|
+
// HTML 4.01 の名前付き文字参照(252個)+ HTML5 の apos。
|
|
12
|
+
// Latin-1 の 96個は U+00A0 から順に並ぶので、名前だけを順番に持って生成する。
|
|
13
|
+
const LATIN1_NAMES = ('nbsp iexcl cent pound curren yen brvbar sect uml copy ordf laquo not shy macr deg '
|
|
14
|
+
+ 'plusmn sup2 sup3 acute micro para middot cedil sup1 ordm raquo frac14 frac12 frac34 iquest '
|
|
15
|
+
+ 'Agrave Aacute Acirc Atilde Auml Aring AElig Ccedil Egrave Eacute Ecirc Euml Igrave Iacute Icirc Iuml '
|
|
16
|
+
+ 'ETH Ntilde Ograve Oacute Ocirc Otilde Ouml times Oslash Ugrave Uacute Ucirc Uuml Yacute THORN szlig '
|
|
17
|
+
+ 'agrave aacute acirc atilde auml aring aelig ccedil egrave eacute ecirc euml igrave iacute icirc iuml '
|
|
18
|
+
+ 'eth ntilde ograve oacute ocirc otilde ouml divide oslash ugrave uacute ucirc uuml yacute thorn yuml').split(' ');
|
|
19
|
+
|
|
20
|
+
// Latin-1 以外は「名前:コードポイント(16進)」で持つ
|
|
21
|
+
const NAMED_HEX = 'quot:22 amp:26 apos:27 lt:3C gt:3E '
|
|
22
|
+
+ 'OElig:152 oelig:153 Scaron:160 scaron:161 Yuml:178 fnof:192 circ:2C6 tilde:2DC '
|
|
23
|
+
+ 'Alpha:391 Beta:392 Gamma:393 Delta:394 Epsilon:395 Zeta:396 Eta:397 Theta:398 Iota:399 Kappa:39A '
|
|
24
|
+
+ 'Lambda:39B Mu:39C Nu:39D Xi:39E Omicron:39F Pi:3A0 Rho:3A1 Sigma:3A3 Tau:3A4 Upsilon:3A5 Phi:3A6 '
|
|
25
|
+
+ 'Chi:3A7 Psi:3A8 Omega:3A9 alpha:3B1 beta:3B2 gamma:3B3 delta:3B4 epsilon:3B5 zeta:3B6 eta:3B7 '
|
|
26
|
+
+ 'theta:3B8 iota:3B9 kappa:3BA lambda:3BB mu:3BC nu:3BD xi:3BE omicron:3BF pi:3C0 rho:3C1 sigmaf:3C2 '
|
|
27
|
+
+ 'sigma:3C3 tau:3C4 upsilon:3C5 phi:3C6 chi:3C7 psi:3C8 omega:3C9 thetasym:3D1 upsih:3D2 piv:3D6 '
|
|
28
|
+
+ 'ensp:2002 emsp:2003 thinsp:2009 zwnj:200C zwj:200D lrm:200E rlm:200F ndash:2013 mdash:2014 '
|
|
29
|
+
+ 'lsquo:2018 rsquo:2019 sbquo:201A ldquo:201C rdquo:201D bdquo:201E dagger:2020 Dagger:2021 bull:2022 '
|
|
30
|
+
+ 'hellip:2026 permil:2030 prime:2032 Prime:2033 lsaquo:2039 rsaquo:203A oline:203E frasl:2044 euro:20AC '
|
|
31
|
+
+ 'image:2111 weierp:2118 real:211C trade:2122 alefsym:2135 larr:2190 uarr:2191 rarr:2192 darr:2193 '
|
|
32
|
+
+ 'harr:2194 crarr:21B5 lArr:21D0 uArr:21D1 rArr:21D2 dArr:21D3 hArr:21D4 forall:2200 part:2202 '
|
|
33
|
+
+ 'exist:2203 empty:2205 nabla:2207 isin:2208 notin:2209 ni:220B prod:220F sum:2211 minus:2212 '
|
|
34
|
+
+ 'lowast:2217 radic:221A prop:221D infin:221E ang:2220 and:2227 or:2228 cap:2229 cup:222A int:222B '
|
|
35
|
+
+ 'there4:2234 sim:223C cong:2245 asymp:2248 ne:2260 equiv:2261 le:2264 ge:2265 sub:2282 sup:2283 '
|
|
36
|
+
+ 'nsub:2284 sube:2286 supe:2287 oplus:2295 otimes:2297 perp:22A5 sdot:22C5 lceil:2308 rceil:2309 '
|
|
37
|
+
+ 'lfloor:230A rfloor:230B lang:2329 rang:232A loz:25CA spades:2660 clubs:2663 hearts:2665 diams:2666';
|
|
38
|
+
|
|
39
|
+
const NAMED = {}; // 名前 → 文字
|
|
40
|
+
const NAME_OF = {}; // 文字 → 名前(先に定義された方を優先)
|
|
41
|
+
LATIN1_NAMES.forEach((name, i) => {
|
|
42
|
+
const ch = String.fromCodePoint(0xa0 + i);
|
|
43
|
+
NAMED[name] = ch;
|
|
44
|
+
NAME_OF[ch] = name;
|
|
45
|
+
});
|
|
46
|
+
NAMED_HEX.split(' ').forEach((pair) => {
|
|
47
|
+
const i = pair.indexOf(':');
|
|
48
|
+
const name = pair.slice(0, i);
|
|
49
|
+
const ch = String.fromCodePoint(parseInt(pair.slice(i + 1), 16));
|
|
50
|
+
NAMED[name] = ch;
|
|
51
|
+
// & < > " ' は escapeHtml 側で個別に扱うので、非ASCIIの逆引きだけ登録する
|
|
52
|
+
if (ch.codePointAt(0) > 0x7f && NAME_OF[ch] === undefined) NAME_OF[ch] = name;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// HTML仕様が定める数値文字参照の置き換え表。€ は U+0080 ではなく € になる
|
|
56
|
+
// (Windows-1252 を UTF-8 と取り違えたページが大量にあったため、仕様側が追認した)。
|
|
57
|
+
const C1_REMAP = {
|
|
58
|
+
0x80: 0x20ac, 0x82: 0x201a, 0x83: 0x0192, 0x84: 0x201e, 0x85: 0x2026, 0x86: 0x2020, 0x87: 0x2021,
|
|
59
|
+
0x88: 0x02c6, 0x89: 0x2030, 0x8a: 0x0160, 0x8b: 0x2039, 0x8c: 0x0152, 0x8e: 0x017d, 0x91: 0x2018,
|
|
60
|
+
0x92: 0x2019, 0x93: 0x201c, 0x94: 0x201d, 0x95: 0x2022, 0x96: 0x2013, 0x97: 0x2014, 0x98: 0x02dc,
|
|
61
|
+
0x99: 0x2122, 0x9a: 0x0161, 0x9b: 0x203a, 0x9c: 0x0153, 0x9e: 0x017e, 0x9f: 0x0178,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const BASIC = { '&': 'amp', '<': 'lt', '>': 'gt', '"': 'quot', "'": 'apos' };
|
|
65
|
+
const BASIC_CP = { '&': 38, '<': 60, '>': 62, '"': 34, "'": 39 };
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* テキストをHTMLエスケープする。
|
|
69
|
+
* @param {string} text
|
|
70
|
+
* @param {object} [opts]
|
|
71
|
+
* @param {boolean} [opts.quotes=true] " と ' も変換する(属性値に入れるなら必須)
|
|
72
|
+
* @param {boolean} [opts.apos=false] ' を ' にする(false なら '。HTML4には ' が無い)
|
|
73
|
+
* @param {boolean} [opts.numeric=false] 名前ではなく数値文字参照(& 形式)で出力する
|
|
74
|
+
* @param {boolean} [opts.nbsp=true] U+00A0(ノーブレークスペース)を必ず参照にする
|
|
75
|
+
* @param {'none'|'named'|'decimal'|'hex'} [opts.nonAscii='none'] 非ASCII文字の扱い
|
|
76
|
+
*/
|
|
77
|
+
export function escapeHtml(text, opts) {
|
|
78
|
+
const o = opts || {};
|
|
79
|
+
const quotes = o.quotes !== false;
|
|
80
|
+
const numeric = Boolean(o.numeric);
|
|
81
|
+
const nbsp = o.nbsp !== false;
|
|
82
|
+
const nonAscii = o.nonAscii || 'none';
|
|
83
|
+
const counts = { amp: 0, lt: 0, gt: 0, quot: 0, apos: 0, nbsp: 0, nonAscii: 0 };
|
|
84
|
+
let out = '';
|
|
85
|
+
// for…of は文字単位ではなくコードポイント単位で回るので、絵文字などのサロゲートペアが割れない
|
|
86
|
+
for (const ch of String(text)) {
|
|
87
|
+
const cp = ch.codePointAt(0);
|
|
88
|
+
if (BASIC_CP[ch] !== undefined) {
|
|
89
|
+
if ((ch === '"' || ch === "'") && !quotes) {
|
|
90
|
+
out += ch;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
counts[BASIC[ch]] += 1;
|
|
94
|
+
if (numeric) {
|
|
95
|
+
out += '&#' + BASIC_CP[ch] + ';';
|
|
96
|
+
} else if (ch === "'" && !o.apos) {
|
|
97
|
+
// ' は HTML 4.01 に無く、古いIEでそのまま表示されるため既定は数値参照
|
|
98
|
+
out += ''';
|
|
99
|
+
} else {
|
|
100
|
+
out += '&' + BASIC[ch] + ';';
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (cp === 0xa0 && nbsp) {
|
|
105
|
+
counts.nbsp += 1;
|
|
106
|
+
out += numeric ? ' ' : ' ';
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (cp > 0x7f && nonAscii !== 'none') {
|
|
110
|
+
counts.nonAscii += 1;
|
|
111
|
+
if (nonAscii === 'named' && NAME_OF[ch]) out += '&' + NAME_OF[ch] + ';';
|
|
112
|
+
else if (nonAscii === 'hex') out += '&#x' + cp.toString(16).toUpperCase() + ';';
|
|
113
|
+
else out += '&#' + cp + ';';
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
out += ch;
|
|
117
|
+
}
|
|
118
|
+
return { text: out, counts };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 数値文字参照のコードポイントを文字へ。仕様どおり不正な値は U+FFFD へ倒す。範囲外は null
|
|
122
|
+
export function charFromCodePoint(cp) {
|
|
123
|
+
if (!Number.isFinite(cp) || cp < 0 || cp > 0x10ffff) return null;
|
|
124
|
+
if (C1_REMAP[cp] !== undefined) return String.fromCodePoint(C1_REMAP[cp]);
|
|
125
|
+
if (cp === 0 || (cp >= 0xd800 && cp <= 0xdfff)) return '�';
|
|
126
|
+
return String.fromCodePoint(cp);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const REF_RE = /&(#[xX][0-9a-fA-F]+|#[0-9]+|[a-zA-Z][a-zA-Z0-9]*);/g;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* HTMLエンティティを元の文字へ戻す。名前付き(HTML4の252個+apos)・10進・16進に対応。
|
|
133
|
+
* 知らない名前や範囲外の数値はそのまま残し、何を残したかを返す(黙って壊さないため)。
|
|
134
|
+
*/
|
|
135
|
+
export function unescapeHtml(text) {
|
|
136
|
+
const counts = { named: 0, decimal: 0, hex: 0 };
|
|
137
|
+
const unknown = [];
|
|
138
|
+
const invalid = [];
|
|
139
|
+
const out = String(text).replace(REF_RE, (whole, body) => {
|
|
140
|
+
if (body.charAt(0) === '#') {
|
|
141
|
+
const hex = body.charAt(1) === 'x' || body.charAt(1) === 'X';
|
|
142
|
+
const cp = hex ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
143
|
+
const ch = charFromCodePoint(cp);
|
|
144
|
+
if (ch === null) {
|
|
145
|
+
if (invalid.indexOf(whole) === -1) invalid.push(whole);
|
|
146
|
+
return whole;
|
|
147
|
+
}
|
|
148
|
+
counts[hex ? 'hex' : 'decimal'] += 1;
|
|
149
|
+
return ch;
|
|
150
|
+
}
|
|
151
|
+
const ch = NAMED[body];
|
|
152
|
+
if (ch === undefined) {
|
|
153
|
+
if (unknown.indexOf(whole) === -1) unknown.push(whole);
|
|
154
|
+
return whole;
|
|
155
|
+
}
|
|
156
|
+
counts.named += 1;
|
|
157
|
+
return ch;
|
|
158
|
+
});
|
|
159
|
+
return { text: out, counts, unknown, invalid };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// セミコロンが無い参照らしき文字列(  や ' など)。ブラウザは一部を救済するが挙動が割れる
|
|
163
|
+
const LOOSE_RE = /&(#[xX]?[0-9a-fA-F]{1,7}|[a-zA-Z][a-zA-Z0-9]{1,30})(?![0-9a-zA-Z;])/g;
|
|
164
|
+
// 参照として成立していない裸の &
|
|
165
|
+
const BARE_AMP_RE = /&(?!(#[xX][0-9a-fA-F]+|#[0-9]+|[a-zA-Z][a-zA-Z0-9]*);)/g;
|
|
166
|
+
|
|
167
|
+
const countMatches = (re, s) => (String(s).match(re) || []).length;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* エスケープ / デコードを1回分実行し、結果・内訳・指摘事項を返す。
|
|
171
|
+
* 指摘は表示側で日英に訳せるようコード(DOUBLE_ESCAPE など)で返す。
|
|
172
|
+
*
|
|
173
|
+
* @param {object} opts
|
|
174
|
+
* @param {'escape'|'unescape'} [opts.mode='escape']
|
|
175
|
+
* @param {string} opts.text
|
|
176
|
+
* @param {boolean} [opts.quotes] @param {boolean} [opts.apos]
|
|
177
|
+
* @param {boolean} [opts.numeric] @param {boolean} [opts.nbsp]
|
|
178
|
+
* @param {'none'|'named'|'decimal'|'hex'} [opts.nonAscii]
|
|
179
|
+
*/
|
|
180
|
+
export function htmlEscapeConvert(opts) {
|
|
181
|
+
const o = opts || {};
|
|
182
|
+
const mode = o.mode === 'unescape' ? 'unescape' : 'escape';
|
|
183
|
+
const input = typeof o.text === 'string' ? o.text : '';
|
|
184
|
+
const notes = [];
|
|
185
|
+
|
|
186
|
+
if (mode === 'escape') {
|
|
187
|
+
const r = escapeHtml(input, o);
|
|
188
|
+
const already = countMatches(REF_RE, input);
|
|
189
|
+
if (already > 0) notes.push({ code: 'DOUBLE_ESCAPE', count: already });
|
|
190
|
+
const nbspCount = (input.match(/\u00a0/g) || []).length;
|
|
191
|
+
if (nbspCount) notes.push({ code: 'HAS_NBSP', count: nbspCount });
|
|
192
|
+
if (r.counts.apos > 0 && !o.apos && !o.numeric) notes.push({ code: 'APOS_NUMERIC' });
|
|
193
|
+
if (!(o.quotes !== false) && /["']/.test(input)) notes.push({ code: 'QUOTES_KEPT' });
|
|
194
|
+
const changed = r.counts.amp + r.counts.lt + r.counts.gt + r.counts.quot
|
|
195
|
+
+ r.counts.apos + r.counts.nbsp + r.counts.nonAscii;
|
|
196
|
+
if (changed === 0 && input) notes.push({ code: 'NOTHING_TO_ESCAPE' });
|
|
197
|
+
return {
|
|
198
|
+
mode,
|
|
199
|
+
text: r.text,
|
|
200
|
+
input_length: [...input].length,
|
|
201
|
+
output_length: [...r.text].length,
|
|
202
|
+
changed,
|
|
203
|
+
counts: r.counts,
|
|
204
|
+
notes,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const r = unescapeHtml(input);
|
|
209
|
+
const changed = r.counts.named + r.counts.decimal + r.counts.hex;
|
|
210
|
+
if (r.unknown.length) notes.push({ code: 'UNKNOWN_ENTITY', items: r.unknown.slice(0, 8), count: r.unknown.length });
|
|
211
|
+
if (r.invalid.length) notes.push({ code: 'INVALID_NUMERIC', items: r.invalid.slice(0, 8), count: r.invalid.length });
|
|
212
|
+
const loose = input.match(LOOSE_RE) || [];
|
|
213
|
+
if (loose.length) notes.push({ code: 'MISSING_SEMICOLON', items: loose.slice(0, 8), count: loose.length });
|
|
214
|
+
const bare = countMatches(BARE_AMP_RE, input) - loose.length;
|
|
215
|
+
if (bare > 0) notes.push({ code: 'BARE_AMP', count: bare });
|
|
216
|
+
if (/<[a-zA-Z!/]/.test(r.text)) notes.push({ code: 'HAS_TAGS' });
|
|
217
|
+
if (changed === 0 && input) notes.push({ code: 'NOTHING_TO_DECODE' });
|
|
218
|
+
return {
|
|
219
|
+
mode,
|
|
220
|
+
text: r.text,
|
|
221
|
+
input_length: [...input].length,
|
|
222
|
+
output_length: [...r.text].length,
|
|
223
|
+
changed,
|
|
224
|
+
counts: r.counts,
|
|
225
|
+
unknown: r.unknown,
|
|
226
|
+
invalid: r.invalid,
|
|
227
|
+
notes,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* ==================== ここまで変換コア ==================== */
|
|
232
|
+
|
|
233
|
+
// 指摘事項のコードを日本語の文へ。site側は同じコードを日英それぞれの文へ訳している
|
|
234
|
+
const NOTE_TEXT = {
|
|
235
|
+
DOUBLE_ESCAPE: (n) => `入力にすでに文字参照が ${n} 個含まれています(すでにエスケープ済みのテキストではありませんか)。もう一度エスケープすると & が &amp; になります。`,
|
|
236
|
+
HAS_NBSP: (n) => `ノーブレークスペース(U+00A0)が ${n} 個あります。見た目は半角スペースですが別の文字なので として書き出しました。`,
|
|
237
|
+
APOS_NUMERIC: () => "アポストロフィは ' ではなく ' で出力しています(' はHTML 4.01に無く、古いブラウザではそのまま表示されるため)。apos=true で切り替えられます。",
|
|
238
|
+
QUOTES_KEPT: () => '引用符をそのまま残しています。要素の中身なら問題ありませんが、属性値に入れると壊れます。',
|
|
239
|
+
NOTHING_TO_ESCAPE: () => 'エスケープが必要な文字はありませんでした。',
|
|
240
|
+
UNKNOWN_ENTITY: (n, items) => `知らないエンティティ名が ${n} 個あり、そのまま残しました: ${items.join(' ')}`,
|
|
241
|
+
INVALID_NUMERIC: (n, items) => `Unicodeの範囲外を指す数値文字参照が ${n} 個あり、そのまま残しました: ${items.join(' ')}`,
|
|
242
|
+
MISSING_SEMICOLON: (n, items) => `セミコロンで閉じていない参照が ${n} 個あります: ${items.join(' ')} — ブラウザによって解釈が割れるためデコードしていません。`,
|
|
243
|
+
BARE_AMP: (n) => `参照になっていない裸の「&」が ${n} 個あります。HTMLとしては & と書くのが正しい形です。`,
|
|
244
|
+
HAS_TAGS: () => 'デコード結果にタグが含まれています。ページに入れると文字ではなくHTMLとして解釈されます。',
|
|
245
|
+
NOTHING_TO_DECODE: () => '文字参照は見つかりませんでした(入力のままです)。',
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export class HtmlEscapeError extends Error {}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* テキスト(または UTF-8 のテキストファイル)をHTMLエスケープするか、
|
|
252
|
+
* HTMLエンティティを元の文字へ戻す。
|
|
253
|
+
*
|
|
254
|
+
* @param {object} opts
|
|
255
|
+
* @param {'escape'|'unescape'} [opts.mode='escape']
|
|
256
|
+
* @param {string} [opts.text] 対象テキスト(path と排他)
|
|
257
|
+
* @param {string} [opts.path] 対象ファイルの絶対パス(UTF-8として読む)
|
|
258
|
+
* @param {string} [opts.outputPath] 結果を書き出す絶対パス(指定すると text は返さない)
|
|
259
|
+
* @param {boolean}[opts.quotes=true] escape: " と ' も変換する
|
|
260
|
+
* @param {boolean}[opts.apos=false] escape: ' を ' にする(既定は ')
|
|
261
|
+
* @param {boolean}[opts.numeric=false] escape: 名前ではなく数値文字参照で出力する
|
|
262
|
+
* @param {'none'|'named'|'decimal'|'hex'} [opts.nonAscii='none'] escape: 非ASCII文字の扱い
|
|
263
|
+
*/
|
|
264
|
+
export async function htmlEscape(opts = {}) {
|
|
265
|
+
const mode = opts.mode || 'escape';
|
|
266
|
+
if (mode !== 'escape' && mode !== 'unescape') throw new HtmlEscapeError(`mode は escape か unescape: ${opts.mode}`);
|
|
267
|
+
const hasText = typeof opts.text === 'string';
|
|
268
|
+
if (hasText === Boolean(opts.path)) throw new HtmlEscapeError('text か path のどちらか一方を渡してください');
|
|
269
|
+
if (opts.nonAscii && ['none', 'named', 'decimal', 'hex'].indexOf(opts.nonAscii) === -1) {
|
|
270
|
+
throw new HtmlEscapeError(`nonAscii は none / named / decimal / hex: ${opts.nonAscii}`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const input = hasText ? opts.text : await readFile(opts.path, 'utf8');
|
|
274
|
+
const r = htmlEscapeConvert({ ...opts, mode, text: input });
|
|
275
|
+
|
|
276
|
+
const result = {
|
|
277
|
+
mode: r.mode,
|
|
278
|
+
source: hasText ? { type: 'text' } : { path: opts.path, name: basename(opts.path) },
|
|
279
|
+
text: r.text,
|
|
280
|
+
input_length: r.input_length,
|
|
281
|
+
output_length: r.output_length,
|
|
282
|
+
changed: r.changed,
|
|
283
|
+
counts: r.counts,
|
|
284
|
+
options: mode === 'escape'
|
|
285
|
+
? {
|
|
286
|
+
quotes: opts.quotes !== false,
|
|
287
|
+
apos: Boolean(opts.apos),
|
|
288
|
+
numeric: Boolean(opts.numeric),
|
|
289
|
+
non_ascii: opts.nonAscii || 'none',
|
|
290
|
+
}
|
|
291
|
+
: null,
|
|
292
|
+
notes: r.notes.map((n) => ({
|
|
293
|
+
code: n.code,
|
|
294
|
+
message: NOTE_TEXT[n.code] ? NOTE_TEXT[n.code](n.count, n.items || []) : n.code,
|
|
295
|
+
})),
|
|
296
|
+
};
|
|
297
|
+
if (mode === 'unescape') {
|
|
298
|
+
result.unknown_entities = r.unknown;
|
|
299
|
+
result.invalid_references = r.invalid;
|
|
300
|
+
}
|
|
301
|
+
if (opts.outputPath) {
|
|
302
|
+
await writeFile(opts.outputPath, r.text, 'utf8');
|
|
303
|
+
result.output = opts.outputPath;
|
|
304
|
+
// ファイルに書けたなら本文は重複した重い情報でしかない
|
|
305
|
+
delete result.text;
|
|
306
|
+
}
|
|
307
|
+
return result;
|
|
308
|
+
}
|