Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/pangu.yml
Original file line number Diff line number Diff line change
@@ -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
101 changes: 101 additions & 0 deletions process.js
Original file line number Diff line number Diff line change
@@ -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}`);