|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | "use strict";
|
3 |
| -const index = require("../shared/index.cjs"); |
| 3 | +var __defProp$1 = Object.defineProperty; |
| 4 | +var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; |
| 5 | +var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, key + "", value); |
| 6 | +const CJK = "\u2E80-\u2EFF\u2F00-\u2FDF\u3040-\u309F\u30A0-\u30FA\u30FC-\u30FF\u3100-\u312F\u3200-\u32FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF"; |
| 7 | +const UNIX_ABSOLUTE_FILE_PATH = /\/(?:\.?(?:home|root|usr|etc|var|opt|tmp|dev|mnt|proc|sys|bin|boot|lib|media|run|sbin|srv|node_modules|path|project|src|dist|test|tests|docs|templates|assets|public|static|config|scripts|tools|build|out|target|your)|\.(?:[A-Za-z0-9_\-]+))(?:\/[A-Za-z0-9_\-\.@\+\*]+)*/; |
| 8 | +const UNIX_RELATIVE_FILE_PATH = /(?:\.\/)?(?:src|dist|test|tests|docs|templates|assets|public|static|config|scripts|tools|build|out|target|node_modules|\.claude|\.git|\.vscode)(?:\/[A-Za-z0-9_\-\.@\+\*]+)+/; |
| 9 | +const WINDOWS_FILE_PATH = /[A-Z]:\\(?:[A-Za-z0-9_\-\. ]+\\?)+/; |
| 10 | +const ANY_CJK = new RegExp(`[${CJK}]`); |
| 11 | +const CONVERT_TO_FULLWIDTH_CJK_SYMBOLS_CJK = new RegExp(`([${CJK}])[ ]*([\\:]+|\\.)[ ]*([${CJK}])`, "g"); |
| 12 | +const CONVERT_TO_FULLWIDTH_CJK_SYMBOLS = new RegExp(`([${CJK}])[ ]*([~\\!;,\\?]+)[ ]*`, "g"); |
| 13 | +const DOTS_CJK = new RegExp(`([\\.]{2,}|\u2026)([${CJK}])`, "g"); |
| 14 | +const FIX_CJK_COLON_ANS = new RegExp(`([${CJK}])\\:([A-Z0-9\\(\\)])`, "g"); |
| 15 | +const CJK_QUOTE = new RegExp(`([${CJK}])([\`"\u05F4])`, "g"); |
| 16 | +const QUOTE_CJK = new RegExp(`([\`"\u05F4])([${CJK}])`, "g"); |
| 17 | +const FIX_QUOTE_ANY_QUOTE = /([`"\u05f4]+)[ ]*(.+?)[ ]*([`"\u05f4]+)/g; |
| 18 | +const QUOTE_AN = /([\u201d])([A-Za-z0-9])/g; |
| 19 | +const CJK_QUOTE_AN = new RegExp(`([${CJK}])(")([A-Za-z0-9])`, "g"); |
| 20 | +const CJK_SINGLE_QUOTE_BUT_POSSESSIVE = new RegExp(`([${CJK}])('[^s])`, "g"); |
| 21 | +const SINGLE_QUOTE_CJK = new RegExp(`(')([${CJK}])`, "g"); |
| 22 | +const FIX_POSSESSIVE_SINGLE_QUOTE = new RegExp(`([A-Za-z0-9${CJK}])( )('s)`, "g"); |
| 23 | +const HASH_ANS_CJK_HASH = new RegExp(`([${CJK}])(#)([${CJK}]+)(#)([${CJK}])`, "g"); |
| 24 | +const CJK_HASH = new RegExp(`([${CJK}])(#([^ ]))`, "g"); |
| 25 | +const HASH_CJK = new RegExp(`(([^ ])#)([${CJK}])`, "g"); |
| 26 | +const CJK_OPERATOR_ANS = new RegExp(`([${CJK}])([\\+\\-\\*=&])([A-Za-z0-9])`, "g"); |
| 27 | +const ANS_OPERATOR_CJK = new RegExp(`([A-Za-z0-9])([\\+\\-\\*=&])([${CJK}])`, "g"); |
| 28 | +const ANS_OPERATOR_ANS = new RegExp(`([A-Za-z0-9])([\\+\\*=&])([A-Za-z0-9])`, "g"); |
| 29 | +const ANS_HYPHEN_ANS_NOT_COMPOUND = new RegExp(`([A-Za-z])(-(?![a-z]))([A-Za-z0-9])|([A-Za-z]+[0-9]+)(-(?![a-z]))([0-9])|([0-9])(-(?![a-z0-9]))([A-Za-z])`, "g"); |
| 30 | +const CJK_SLASH_CJK = new RegExp(`([${CJK}])([/])([${CJK}])`, "g"); |
| 31 | +const CJK_SLASH_ANS = new RegExp(`([${CJK}])([/])([A-Za-z0-9])`, "g"); |
| 32 | +const ANS_SLASH_CJK = new RegExp(`([A-Za-z0-9])([/])([${CJK}])`, "g"); |
| 33 | +const ANS_SLASH_ANS = new RegExp(`([A-Za-z0-9])([/])([A-Za-z0-9])`, "g"); |
| 34 | +const SINGLE_LETTER_GRADE_CJK = new RegExp(`\\b([A-Za-z])([\\+\\-\\*])([${CJK}])`, "g"); |
| 35 | +const CJK_LESS_THAN = new RegExp(`([${CJK}])(<)([A-Za-z0-9])`, "g"); |
| 36 | +const LESS_THAN_CJK = new RegExp(`([A-Za-z0-9])(<)([${CJK}])`, "g"); |
| 37 | +const CJK_GREATER_THAN = new RegExp(`([${CJK}])(>)([A-Za-z0-9])`, "g"); |
| 38 | +const GREATER_THAN_CJK = new RegExp(`([A-Za-z0-9])(>)([${CJK}])`, "g"); |
| 39 | +const ANS_LESS_THAN_ANS = new RegExp(`([A-Za-z0-9])(<)([A-Za-z0-9])`, "g"); |
| 40 | +const ANS_GREATER_THAN_ANS = new RegExp(`([A-Za-z0-9])(>)([A-Za-z0-9])`, "g"); |
| 41 | +const CJK_LEFT_BRACKET = new RegExp(`([${CJK}])([\\(\\[\\{<>\u201C])`, "g"); |
| 42 | +const RIGHT_BRACKET_CJK = new RegExp(`([\\)\\]\\}<>\u201D])([${CJK}])`, "g"); |
| 43 | +const ANS_CJK_LEFT_BRACKET_ANY_RIGHT_BRACKET = new RegExp(`([A-Za-z0-9${CJK}])[ ]*([\u201C])([A-Za-z0-9${CJK}\\-_ ]+)([\u201D])`, "g"); |
| 44 | +const LEFT_BRACKET_ANY_RIGHT_BRACKET_ANS_CJK = new RegExp(`([\u201C])([A-Za-z0-9${CJK}\\-_ ]+)([\u201D])[ ]*([A-Za-z0-9${CJK}])`, "g"); |
| 45 | +const AN_LEFT_BRACKET = new RegExp("([A-Za-z0-9])(?<!\\.[A-Za-z0-9]*)([\\(\\[\\{])", "g"); |
| 46 | +const RIGHT_BRACKET_AN = /([\)\]\}])([A-Za-z0-9])/g; |
| 47 | +const CJK_UNIX_ABSOLUTE_FILE_PATH = new RegExp(`([${CJK}])(${UNIX_ABSOLUTE_FILE_PATH.source})`, "g"); |
| 48 | +const CJK_UNIX_RELATIVE_FILE_PATH = new RegExp(`([${CJK}])(${UNIX_RELATIVE_FILE_PATH.source})`, "g"); |
| 49 | +const CJK_WINDOWS_PATH = new RegExp(`([${CJK}])(${WINDOWS_FILE_PATH.source})`, "g"); |
| 50 | +const UNIX_ABSOLUTE_FILE_PATH_SLASH_CJK = new RegExp(`(${UNIX_ABSOLUTE_FILE_PATH.source}/)([${CJK}])`, "g"); |
| 51 | +const UNIX_RELATIVE_FILE_PATH_SLASH_CJK = new RegExp(`(${UNIX_RELATIVE_FILE_PATH.source}/)([${CJK}])`, "g"); |
| 52 | +const CJK_ANS = new RegExp(`([${CJK}])([A-Za-z\u0370-\u03FF0-9@\\$%\\^&\\*\\-\\+\\\\=\xA1-\xFF\u2150-\u218F\u2700\u2014\u27BF])`, "g"); |
| 53 | +const ANS_CJK = new RegExp(`([A-Za-z\u0370-\u03FF0-9~\\$%\\^&\\*\\-\\+\\\\=!;:,\\.\\?\xA1-\xFF\u2150-\u218F\u2700\u2014\u27BF])([${CJK}])`, "g"); |
| 54 | +const S_A = /(%)([A-Za-z])/g; |
| 55 | +const MIDDLE_DOT = /([ ]*)([\u00b7\u2022\u2027])([ ]*)/g; |
| 56 | +class Pangu { |
| 57 | + constructor() { |
| 58 | + __publicField$1(this, "version"); |
| 59 | + this.version = "6.1.1"; |
| 60 | + } |
| 61 | + spacingText(text) { |
| 62 | + if (typeof text !== "string") { |
| 63 | + console.warn(`spacingText(text) only accepts string but got ${typeof text}`); |
| 64 | + return text; |
| 65 | + } |
| 66 | + if (text.length <= 1 || !ANY_CJK.test(text)) { |
| 67 | + return text; |
| 68 | + } |
| 69 | + const self = this; |
| 70 | + let newText = text; |
| 71 | + const htmlTags = []; |
| 72 | + const HTML_TAG_PLACEHOLDER = "\0HTML_TAG_PLACEHOLDER_"; |
| 73 | + let hasHtmlTags = false; |
| 74 | + if (newText.includes("<")) { |
| 75 | + hasHtmlTags = true; |
| 76 | + const HTML_TAG_PATTERN = /<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?>/g; |
| 77 | + newText = newText.replace(HTML_TAG_PATTERN, (match) => { |
| 78 | + const processedTag = match.replace(/(\w+)="([^"]*)"/g, (_attrMatch, attrName, attrValue) => { |
| 79 | + const processedValue = self.spacingText(attrValue); |
| 80 | + return `${attrName}="${processedValue}"`; |
| 81 | + }); |
| 82 | + const index = htmlTags.length; |
| 83 | + htmlTags.push(processedTag); |
| 84 | + return `${HTML_TAG_PLACEHOLDER}${index}\0`; |
| 85 | + }); |
| 86 | + } |
| 87 | + newText = newText.replace(CONVERT_TO_FULLWIDTH_CJK_SYMBOLS_CJK, (_match, leftCjk, symbols, rightCjk) => { |
| 88 | + const fullwidthSymbols = self.convertToFullwidth(symbols); |
| 89 | + return `${leftCjk}${fullwidthSymbols}${rightCjk}`; |
| 90 | + }); |
| 91 | + newText = newText.replace(CONVERT_TO_FULLWIDTH_CJK_SYMBOLS, (_match, cjk, symbols) => { |
| 92 | + const fullwidthSymbols = self.convertToFullwidth(symbols); |
| 93 | + return `${cjk}${fullwidthSymbols}`; |
| 94 | + }); |
| 95 | + newText = newText.replace(DOTS_CJK, "$1 $2"); |
| 96 | + newText = newText.replace(FIX_CJK_COLON_ANS, "$1\uFF1A$2"); |
| 97 | + newText = newText.replace(CJK_QUOTE, "$1 $2"); |
| 98 | + newText = newText.replace(QUOTE_CJK, "$1 $2"); |
| 99 | + newText = newText.replace(FIX_QUOTE_ANY_QUOTE, "$1$2$3"); |
| 100 | + newText = newText.replace(QUOTE_AN, "$1 $2"); |
| 101 | + newText = newText.replace(CJK_QUOTE_AN, "$1$2 $3"); |
| 102 | + newText = newText.replace(CJK_SINGLE_QUOTE_BUT_POSSESSIVE, "$1 $2"); |
| 103 | + newText = newText.replace(SINGLE_QUOTE_CJK, "$1 $2"); |
| 104 | + newText = newText.replace(FIX_POSSESSIVE_SINGLE_QUOTE, "$1's"); |
| 105 | + const textLength = newText.length; |
| 106 | + const slashCount = (newText.match(/\//g) || []).length; |
| 107 | + if (slashCount === 0) { |
| 108 | + if (textLength >= 5) { |
| 109 | + newText = newText.replace(HASH_ANS_CJK_HASH, "$1 $2$3$4 $5"); |
| 110 | + } |
| 111 | + newText = newText.replace(CJK_HASH, "$1 $2"); |
| 112 | + newText = newText.replace(HASH_CJK, "$1 $3"); |
| 113 | + } else if (slashCount <= 1) { |
| 114 | + if (textLength >= 5) { |
| 115 | + newText = newText.replace(HASH_ANS_CJK_HASH, "$1 $2$3$4 $5"); |
| 116 | + } |
| 117 | + newText = newText.replace(CJK_HASH, "$1 $2"); |
| 118 | + newText = newText.replace(HASH_CJK, "$1 $3"); |
| 119 | + } else { |
| 120 | + if (textLength >= 5) { |
| 121 | + newText = newText.replace(HASH_ANS_CJK_HASH, "$1 $2$3$4 $5"); |
| 122 | + } |
| 123 | + newText = newText.replace(new RegExp(`([^/])([${CJK}])(#[A-Za-z0-9]+)$`), "$1$2 $3"); |
| 124 | + } |
| 125 | + const COMPOUND_WORD_PLACEHOLDER = "\uE002"; |
| 126 | + const compoundWords = []; |
| 127 | + let compoundIndex = 0; |
| 128 | + const COMPOUND_WORD_PATTERN = /\b(?:[A-Za-z0-9]*[a-z][A-Za-z0-9]*-[A-Za-z0-9]+|[A-Za-z0-9]+-[A-Za-z0-9]*[a-z][A-Za-z0-9]*|[A-Za-z]+-[0-9]+|[A-Za-z]+[0-9]+-[A-Za-z0-9]+)(?:-[A-Za-z0-9]+)*\b/g; |
| 129 | + newText = newText.replace(COMPOUND_WORD_PATTERN, (match) => { |
| 130 | + compoundWords[compoundIndex] = match; |
| 131 | + return `${COMPOUND_WORD_PLACEHOLDER}${compoundIndex++}\uE003`; |
| 132 | + }); |
| 133 | + newText = newText.replace(SINGLE_LETTER_GRADE_CJK, "$1$2 $3"); |
| 134 | + newText = newText.replace(CJK_OPERATOR_ANS, "$1 $2 $3"); |
| 135 | + newText = newText.replace(ANS_OPERATOR_CJK, "$1 $2 $3"); |
| 136 | + newText = newText.replace(ANS_OPERATOR_ANS, "$1 $2 $3"); |
| 137 | + newText = newText.replace(ANS_HYPHEN_ANS_NOT_COMPOUND, (match, ...groups) => { |
| 138 | + if (groups[0] && groups[1] && groups[2]) { |
| 139 | + return `${groups[0]} ${groups[1]} ${groups[2]}`; |
| 140 | + } else if (groups[3] && groups[4] && groups[5]) { |
| 141 | + return `${groups[3]} ${groups[4]} ${groups[5]}`; |
| 142 | + } else if (groups[6] && groups[7] && groups[8]) { |
| 143 | + return `${groups[6]} ${groups[7]} ${groups[8]}`; |
| 144 | + } |
| 145 | + return match; |
| 146 | + }); |
| 147 | + newText = newText.replace(CJK_LESS_THAN, "$1 $2 $3"); |
| 148 | + newText = newText.replace(LESS_THAN_CJK, "$1 $2 $3"); |
| 149 | + newText = newText.replace(ANS_LESS_THAN_ANS, "$1 $2 $3"); |
| 150 | + newText = newText.replace(CJK_GREATER_THAN, "$1 $2 $3"); |
| 151 | + newText = newText.replace(GREATER_THAN_CJK, "$1 $2 $3"); |
| 152 | + newText = newText.replace(ANS_GREATER_THAN_ANS, "$1 $2 $3"); |
| 153 | + newText = newText.replace(CJK_UNIX_ABSOLUTE_FILE_PATH, "$1 $2"); |
| 154 | + newText = newText.replace(CJK_UNIX_RELATIVE_FILE_PATH, "$1 $2"); |
| 155 | + newText = newText.replace(CJK_WINDOWS_PATH, "$1 $2"); |
| 156 | + newText = newText.replace(UNIX_ABSOLUTE_FILE_PATH_SLASH_CJK, "$1 $2"); |
| 157 | + newText = newText.replace(UNIX_RELATIVE_FILE_PATH_SLASH_CJK, "$1 $2"); |
| 158 | + if (slashCount === 1) { |
| 159 | + const FILE_PATH_PLACEHOLDER = "\uE000"; |
| 160 | + const filePaths = []; |
| 161 | + let pathIndex = 0; |
| 162 | + const allFilePathPattern = new RegExp(`(${UNIX_ABSOLUTE_FILE_PATH.source}|${UNIX_RELATIVE_FILE_PATH.source})`, "g"); |
| 163 | + newText = newText.replace(allFilePathPattern, (match) => { |
| 164 | + filePaths[pathIndex] = match; |
| 165 | + return `${FILE_PATH_PLACEHOLDER}${pathIndex++}\uE001`; |
| 166 | + }); |
| 167 | + newText = newText.replace(CJK_SLASH_CJK, "$1 $2 $3"); |
| 168 | + newText = newText.replace(CJK_SLASH_ANS, "$1 $2 $3"); |
| 169 | + newText = newText.replace(ANS_SLASH_CJK, "$1 $2 $3"); |
| 170 | + newText = newText.replace(ANS_SLASH_ANS, "$1 $2 $3"); |
| 171 | + const FILE_PATH_RESTORE = new RegExp(`${FILE_PATH_PLACEHOLDER}(\\d+)\uE001`, "g"); |
| 172 | + newText = newText.replace(FILE_PATH_RESTORE, (_match, index) => { |
| 173 | + return filePaths[parseInt(index, 10)] || ""; |
| 174 | + }); |
| 175 | + } |
| 176 | + const COMPOUND_WORD_RESTORE = new RegExp(`${COMPOUND_WORD_PLACEHOLDER}(\\d+)\uE003`, "g"); |
| 177 | + newText = newText.replace(COMPOUND_WORD_RESTORE, (_match, index) => { |
| 178 | + return compoundWords[parseInt(index, 10)] || ""; |
| 179 | + }); |
| 180 | + newText = newText.replace(CJK_LEFT_BRACKET, "$1 $2"); |
| 181 | + newText = newText.replace(RIGHT_BRACKET_CJK, "$1 $2"); |
| 182 | + newText = newText.replace(ANS_CJK_LEFT_BRACKET_ANY_RIGHT_BRACKET, "$1 $2$3$4"); |
| 183 | + newText = newText.replace(LEFT_BRACKET_ANY_RIGHT_BRACKET_ANS_CJK, "$1$2$3 $4"); |
| 184 | + newText = newText.replace(AN_LEFT_BRACKET, "$1 $2"); |
| 185 | + newText = newText.replace(RIGHT_BRACKET_AN, "$1 $2"); |
| 186 | + newText = newText.replace(CJK_ANS, "$1 $2"); |
| 187 | + newText = newText.replace(ANS_CJK, "$1 $2"); |
| 188 | + newText = newText.replace(S_A, "$1 $2"); |
| 189 | + newText = newText.replace(MIDDLE_DOT, "\u30FB"); |
| 190 | + const fixBracketSpacing = (text2) => { |
| 191 | + const processBracket = (pattern, openBracket, closeBracket) => { |
| 192 | + text2 = text2.replace(pattern, (_match, innerContent) => { |
| 193 | + if (!innerContent) { |
| 194 | + return `${openBracket}${closeBracket}`; |
| 195 | + } |
| 196 | + const trimmedContent = innerContent.replace(/^ +| +$/g, ""); |
| 197 | + return `${openBracket}${trimmedContent}${closeBracket}`; |
| 198 | + }); |
| 199 | + }; |
| 200 | + processBracket(/<([^<>]*)>/g, "<", ">"); |
| 201 | + processBracket(/\(([^()]*)\)/g, "(", ")"); |
| 202 | + processBracket(/\[([^\[\]]*)\]/g, "[", "]"); |
| 203 | + processBracket(/\{([^{}]*)\}/g, "{", "}"); |
| 204 | + return text2; |
| 205 | + }; |
| 206 | + newText = fixBracketSpacing(newText); |
| 207 | + if (hasHtmlTags) { |
| 208 | + const HTML_TAG_RESTORE = new RegExp(`${HTML_TAG_PLACEHOLDER}(\\d+)\0`, "g"); |
| 209 | + newText = newText.replace(HTML_TAG_RESTORE, (_match, index) => { |
| 210 | + return htmlTags[parseInt(index, 10)] || ""; |
| 211 | + }); |
| 212 | + } |
| 213 | + return newText; |
| 214 | + } |
| 215 | + // alias for spacingText() |
| 216 | + spacing(text) { |
| 217 | + return this.spacingText(text); |
| 218 | + } |
| 219 | + hasProperSpacing(text) { |
| 220 | + return this.spacingText(text) === text; |
| 221 | + } |
| 222 | + convertToFullwidth(symbols) { |
| 223 | + return symbols.replace(/~/g, "\uFF5E").replace(/!/g, "\uFF01").replace(/;/g, "\uFF1B").replace(/:/g, "\uFF1A").replace(/,/g, "\uFF0C").replace(/\./g, "\u3002").replace(/\?/g, "\uFF1F"); |
| 224 | + } |
| 225 | +} |
| 226 | +new Pangu(); |
4 | 227 | var __defProp = Object.defineProperty;
|
5 | 228 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
6 | 229 | var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
@@ -35,7 +258,7 @@ function debounce(func, delay, mustRunDelay = Infinity) {
|
35 | 258 | }
|
36 | 259 | };
|
37 | 260 | }
|
38 |
| -class BrowserPangu extends index.Pangu { |
| 261 | +class BrowserPangu extends Pangu { |
39 | 262 | constructor() {
|
40 | 263 | super();
|
41 | 264 | __publicField(this, "isAutoSpacingPageExecuted");
|
|
0 commit comments