电速宝智配引擎
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fix_json.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const fs = require('fs');
  2. const path = require('path');
  3. const { execSync } = require('child_process');
  4. const BASE = __dirname;
  5. const DOCX = path.join(BASE, '智配引擎数据库设计文档.docx');
  6. const WORK = path.join(BASE, 'docx_work3');
  7. const OUTPUT = path.join(BASE, '智配引擎数据库设计文档_规范后.docx');
  8. if (fs.existsSync(WORK)) fs.rmSync(WORK, { recursive: true });
  9. fs.mkdirSync(WORK, { recursive: true });
  10. execSync(`unzip -o "${DOCX}" -d "${WORK}"`, { stdio: 'pipe' });
  11. let xml = fs.readFileSync(path.join(WORK, 'word/document.xml'), 'utf-8');
  12. const changes = [];
  13. // ============================================================
  14. // Step 1: 合并跨 <w:r> 的 A + 残片模式
  15. // 使用 [\s\S]* 来匹配换行
  16. // ============================================================
  17. const mergePairs = [
  18. { find: 'nnualpowersupply', neu: 'annual_power_supply', label: 'A+nnualpowersupply' },
  19. { find: 'verageelectricityprice', neu: 'average_electricity_price', label: 'A+verageelectricityprice' },
  20. { find: 'nnualincome', neu: 'annual_income', label: 'A+nnualincome' },
  21. { find: 'nnualprofit', neu: 'annual_profit', label: 'A+nnualprofit' },
  22. ];
  23. for (const mp of mergePairs) {
  24. const escaped = mp.find.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  25. // Match: <w:t>A</w:t></w:r>...<w:r>...<w:t>AnnualpowersupplySUFFIX</w:t>
  26. // where SUFFIX is either :CHINESE or just : alone
  27. const regex = new RegExp(
  28. '<w:t>A</w:t></w:r>[\\s\\S]*?<w:r>[\\s\\S]*?<w:t>' + escaped + '([^:<]*)(:[^<]*)<\/w:t>',
  29. 'g'
  30. );
  31. const matches = [...xml.matchAll(regex)];
  32. if (matches.length > 0) {
  33. xml = xml.replace(regex, (m, suffix, rest) => {
  34. changes.push(`${mp.label} -> ${mp.neu}`);
  35. return `<w:t>${mp.neu}${rest}</w:t>`;
  36. });
  37. }
  38. }
  39. // ============================================================
  40. // Step 2: 单 <w:t> 标签替换
  41. // ============================================================
  42. const r = (old, neu) => {
  43. if (xml.includes(old)) {
  44. xml = xml.split(old).join(neu);
  45. changes.push(`"${old.substring(0,30)}" -> "${neu.substring(0,50)}"`);
  46. }
  47. };
  48. r('assignment_duration:年度作业时长,',
  49. 'assignment_duration: (Annual Assignment Duration/年作业时长)');
  50. r('annual_power_supply: 年供电量',
  51. 'annual_power_supply: (Annual Power Supply/年供电量)');
  52. r('average_electricity_price: 平均电价',
  53. 'average_electricity_price: (Average Electricity Price/平均电价)');
  54. r('年运营成本',
  55. 'annual_operating_cost: (Annual Operating Cost/年运营成本)');
  56. r('year:年示例:(第一年)',
  57. 'year: (Year Example e.g. Year 1/示例:如第一年)');
  58. r('annual_income: 年收入',
  59. 'annual_income: (Annual Income/年收入)');
  60. r('annual_profit: 年利润',
  61. 'annual_profit: (Annual Profit/年利润)');
  62. r('annualpretaxprofit年税前利润',
  63. 'annual_pre_tax_profit: (Annual Pre-Tax Profit/年税前利润)');
  64. r('annualaftertaxprofit年税后利润',
  65. 'annual_after_tax_profit: (Annual After-Tax Profit/年税后利润)');
  66. r('paybackperiod投资回收期',
  67. 'pay_back_period: (Payback Period/投资回收期)');
  68. r('irr:内部收益率(IRR)',
  69. 'irr: (Internal Rate of Return/内部收益率IRR)');
  70. r('ppv:净现值(NPV)',
  71. 'npv: (Net Present Value/净现值NPV)');
  72. r('cumulativeprofit:累计利润',
  73. 'cumulative_profit: (Cumulative Profit/累计利润)');
  74. r('Cumulativeinvestment累计投资',
  75. 'cumulative_investment: (Cumulative Investment/累计投资)');
  76. r('revenuemodel',
  77. 'revenue_model: (Revenue Model/收益模型)');
  78. r('year:',
  79. 'year: (Year/年份)');
  80. console.log('Changes:');
  81. changes.forEach(c => console.log(' ', c));
  82. fs.writeFileSync(path.join(WORK, 'word/document.xml'), xml, 'utf-8');
  83. if (fs.existsSync(OUTPUT)) fs.unlinkSync(OUTPUT);
  84. execSync(`cd "${WORK}" && zip -r "${OUTPUT}" . -x "*.DS_Store"`, { stdio: 'pipe' });
  85. console.log(`\nSaved: ${OUTPUT}`);
  86. fs.rmSync(WORK, { recursive: true });