script.js// 置換テーブルを用いて文章をフォーマットする
scrapbox.PopupMenu.addButton({
title: 'YuTFormat',
onClick: text => {
// 置換テーブル
const replacements = {
// 置換
'自分フィールド': '自場',
'相手フィールド': '相手の場',
'フィールド(?!魔法|ゾーン)': '場', // フィールドの後ろに魔法が続かない場合にのみ置換
'エクストラデッキ': 'EXデッキ',
'エクシーズ素材': 'X素材',
/// 召喚方法
/*'(?<!通常|反転|特殊|融合|リンク)召喚・': 'NS-', */'NS-': '召喚-',
///'通常召喚': 'NS',
'特殊召喚': 'SS',
'融合召喚': 'F召喚',
'リンク召喚': 'L召喚',
/// モンスターの種類
'融合モンスター': 'Fモンスター',
'シンクロモンスター': 'Sモンスター',
'エクシーズモンスター': 'Xモンスター',
'リンクモンスター': 'Lモンスター',
/// 対象をとる効果
'対象として': '対象にとって',
/// 語のつなぎ
'・': '-',
// 削除
'「|」|●': '',
'カードテキスト': '',
'。$': '', // 行末の`。`
// 効果の番号の置換
'①[::]': '1. ',
'②[::]': '2. ',
'③[::]': '3. ',
'④[::]': '4. ',
'⑤[::]': '5. ',
'⑥[::]': '6. ',
// 全角を半角にする
'+': '+',
'/': '/',
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9'
};
// 置換処理
let tempReplacements = []; // 処理をバイパスするためのリスト
let result = text.split(/\n/)
.map(line => {
// カード名称テキストの処理: 「カード名」を除く→`カード名`を除く
line = line.replace(/「(.*?)」(を除く|以外)?/g, function(match, p1, p2) {
if (p1.startsWith('[') && p1.endsWith(']')) {
return match;
}
return p2 ? '`' + p1 + '`' + p2 : match;
});
// ブラケティングされている部分を一時的に置換して処理をバイパスする
line = line.replace(/(\[(.*?)\])|(`.*?`)/g, function(match) {
let tempReplacement = `TEMP_REPLACEMENT_${tempReplacements.length}_TEMP`;
tempReplacements.push(match);
return tempReplacement;
});
// 置換処理
for (let key in replacements) {
let regex = new RegExp(key, 'g');
line = line.replace(regex, replacements[key]);
}
// バイパスのために行った置換の後片付け
tempReplacements.forEach((replacement, index) => {
line = line.replace(new RegExp(`TEMP_REPLACEMENT_${index}_TEMP`, 'g'), replacement);
//line = line.replace(/TEMP_REPLACEMENT_(\d+)/g, function(match, index) {
// return tempReplacements[Number(index)];
});
return line;
}).join('\n');
// テロメアが無駄に更新されるのを防ぐ
if(text == result) return;
return result;
}
});