diff --git a/.github/workflows/pangu.yml b/.github/workflows/pangu.yml new file mode 100644 index 0000000..80ab33a --- /dev/null +++ b/.github/workflows/pangu.yml @@ -0,0 +1,40 @@ +name: Pangu + +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - run: npm install pangu + + - run: node process.js + + - name: Push to pangu branch + run: | + git config user.name github-actions + git config user.email github-actions@github.com + + git checkout -B pangu + + git add texts/zh_CN.lang + + git diff --cached --quiet || \ + git commit -m "Auto pangu" + + git push origin pangu --force \ No newline at end of file diff --git a/process.js b/process.js new file mode 100644 index 0000000..3da777a --- /dev/null +++ b/process.js @@ -0,0 +1,101 @@ +const fs = require('fs'); +const pangu = require('pangu'); + +const file = 'texts/zh_CN.lang'; + +let text = fs.readFileSync(file, 'utf8'); + +const protectedItems = []; + + + +/** + * 保护但视为英文 + */ +function protect(regex) { + text = text.replace(regex, match => { + const id = protectedItems.length; + protectedItems.push(match); + return `Protected${id}`; + }); +} + +// 我真的是疯了 +// 这个早一天的生日礼物,你喜欢吗 +const protectedStrictItems = []; + +/** + * 保护且防止被处理 + */ +function protectStrict(regex) { + text = text.replace(regex, (match, ...args) => { + const id = protectedStrictItems.length; + protectedStrictItems.push(match); + + const offset = args[args.length - 2]; + const prevChar = text[offset - 1]; + + let suffix = ''; + +console.log(prevChar); + if (prevChar === '=') { + suffix = ''; + } else if (/[\u4E00-\u9FFF]/.test(prevChar)) { + suffix = '桀夜我喜欢你'; + } else if (/[A-Za-z]/.test(prevChar)) { + suffix = 'ILoveUFromLuoYunXi'; + } + + return `⟦P${id}⟧${suffix}`; + }); +} + + +// %s +// %d +// %1$s +// %2$d +protect(/%\d*\$?[a-zA-Z]/g); + +// :tip_virtual_button_action_build_or_use: +// 他妈这里有个蛋 +protect(/:[a-zA-Z0-9_.-]+:/g); + + +// §a +// §b +// §l +protectStrict(/§./g); + + +/** + * 执行 pangu + */ +text = pangu.spacingText(text); + +/** + * 恢复所有保护内容 + */ +text = text.replace(/Protected(\d+)/g, (_, index) => { + return protectedItems[Number(index)]; +}); + +// 我真几把疯了 +text = text.replace( + /⟦P(\d+)⟧(?:桀夜我喜欢你|ILoveUFromLuoYunXi)?/g, + (_, index) => protectedStrictItems[Number(index)] +); + +/** + * 额外修正 + */ + +// key = value → key=value +text = text.replace(/\s+=\s+/g, '='); + +/** + * 写回文件 + */ +fs.writeFileSync(file, text, 'utf8'); + +console.log(`Processed ${file}`); \ No newline at end of file