chengjie 2 月之前
父節點
當前提交
2e58800e89
共有 1 個文件被更改,包括 112 次插入26 次删除
  1. 112 26
      src/test/build.古诗文背诵.js

+ 112 - 26
src/test/build.古诗文背诵.js

@@ -2,14 +2,14 @@ import commonModel from '../model/commonModel.js';
2 2
 import { stringUtils } from '../util/stringClass.js';
3 3
 import fs from 'fs';
4 4
 
5
-async function runScript(){
5
+async function runScript() {
6 6
     try {
7 7
         var data = fs.readFileSync('/Users/chengjie/Documents/git/miaoguo_system_server/src/test/古诗词.html', 'utf8');
8
-        
8
+
9 9
         // 提取表格数据
10 10
         const tableRegex = /<tr>\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>(.*?)<\/td>\s*<\/tr>/gs;
11 11
         const matches = [...data.matchAll(tableRegex)];
12
-        
12
+
13 13
         // 生成竖排网页
14 14
         let htmlContent = `
15 15
 <!DOCTYPE html>
@@ -84,47 +84,133 @@ async function runScript(){
84 84
 </head>
85 85
 <body>`;
86 86
 
87
+        // 按年级和学期分组
88
+        const groupedByGradeAndTerm = {};
89
+
87 90
         // 跳过表头,从索引1开始
88 91
         for (let i = 1; i < matches.length; i++) {
89 92
             const match = matches[i];
90 93
             const tag = match[1];
91
-            const title = match[2];
92
-            const author = match[3] === 'NULL' ? '' : match[3];
93
-            const dynasty = match[4] === 'NULL' ? '' : match[4].replace(/\\[|\\]/g, ''); // 去掉朝代中的[]
94
-            
95
-            // 处理诗词内容,去掉[]等符号
96
-            let poemContent = match[5];
97
-            // 使用正则表达式提取诗句
98
-            const poemMatches = poemContent.match(/\"([^\"]+)\"/g) || [];
99
-            const cleanedPoem = poemMatches.map(line => {
100
-                return line.replace(/\"/g, '');
101
-            }).join('<br>');
102
-            
103
-            // 处理译文
104
-            let translation = match[6];
105
-            // 使用正则表达式提取译文
106
-            const translationMatches = translation.match(/\"([^\"]+)\"/g) || [];
107
-            const cleanedTranslation = translationMatches.map(line => {
108
-                return line.replace(/\"/g, '');
109
-            }).join('<br>');
94
+            // 提取年级信息(仅保留年级部分,如 "六年级")
95
+            const grade = tag.split('_')[0].split(',')[0];
96
+            const term = tag.includes('上') ? '上' : '下'; // 提取学期信息
97
+
98
+            const key = `${grade}_${term}`;
99
+            if (!groupedByGradeAndTerm[key]) {
100
+                groupedByGradeAndTerm[key] = [];
101
+            }
102
+            groupedByGradeAndTerm[key].push(match);
103
+        }
104
+
105
+        // 确保诗词排在日积月累之前
106
+        for (const key in groupedByGradeAndTerm) {
107
+            groupedByGradeAndTerm[key].sort((a, b) => {
108
+                const isAccumulationA = a[1].includes('日积月累');
109
+                const isAccumulationB = b[1].includes('日积月累');
110
+
111
+                if (isAccumulationA && !isAccumulationB) return 1;
112
+                if (!isAccumulationA && isAccumulationB) return -1;
113
+                return 0;
114
+            });
115
+        }
116
+
117
+        // 自定义年级排序函数
118
+        const gradeOrder = ['一', '二', '三', '四', '五', '六', '七', '八', '九'];
119
+
120
+        // 按年级和学期顺序处理
121
+        const sortedKeys = Object.keys(groupedByGradeAndTerm).sort((a, b) => {
122
+            const [gradeA, termA] = a.split('_');
123
+            const [gradeB, termB] = b.split('_');
110 124
             
111
-            htmlContent += `
125
+            // 提取年级数字部分(如 "一年级" -> "一")
126
+            const gradeNumA = gradeA.replace('年级', '');
127
+            const gradeNumB = gradeB.replace('年级', '');
128
+
129
+            // 先按年级排序(从一年级到九年级)
130
+            const gradeCompare = gradeOrder.indexOf(gradeNumA) - gradeOrder.indexOf(gradeNumB);
131
+            //console.log(gradeA+" "+gradeB+"=" +gradeCompare);
132
+
133
+            if (gradeCompare !== 0) return gradeCompare;
134
+
135
+            //console.log(termA);
136
+
137
+            // 同年级按学期排序(上在前)
138
+            return termA === '上' ? -1 : 1;
139
+        });
140
+
141
+
142
+        // 确保排序后的键符合预期顺序
143
+        console.log('Sorted Keys:', sortedKeys);
144
+
145
+        for (const key of sortedKeys) {
146
+            const gradeMatches = groupedByGradeAndTerm[key];
147
+
148
+            // 先处理诗词,再处理日积月累
149
+            const poems = gradeMatches.filter(m => !m[1].includes('日积月累'));
150
+            const accumulations = gradeMatches.filter(m => m[1].includes('日积月累'));
151
+
152
+            // 处理诗词
153
+            for (const match of poems) {
154
+                const tag = match[1];
155
+                const title = match[2];
156
+                const author = match[3] === 'NULL' ? '' : match[3];
157
+                const dynasty = match[4] === 'NULL' ? '' : match[4].replace(/\\[|\\]/g, '');
158
+
159
+                let poemContent = match[5];
160
+                const poemMatches = poemContent.match(/\"([^\"]+)\"/g) || [];
161
+                const cleanedPoem = poemMatches.map(line => line.replace(/\"/g, '')).join('<br>');
162
+
163
+                let translation = match[6];
164
+                const translationMatches = translation.match(/\"([^\"]+)\"/g) || [];
165
+                const cleanedTranslation = translationMatches.map(line => line.replace(/\"/g, '')).join('<br>');
166
+
167
+                const formattedTitle = title.replace(/([。]|(?<!——)\s+)(?!——)\s*/g, '$1<br>');
168
+
169
+                htmlContent += `
170
+    <div class="poem-container">
171
+        <div class="poem-title">${formattedTitle}</div>
172
+        <div class="poem-info">${dynasty && author ? `${dynasty} ${author}` : dynasty || author}</div>
173
+        <div class="poem-content">${cleanedPoem}</div>
174
+        <div class="poem-translation">${cleanedTranslation}</div>
175
+        <div class="tag">${tag}</div>
176
+    </div>`;
177
+            }
178
+
179
+            // 处理日积月累
180
+            for (const match of accumulations) {
181
+                const tag = match[1];
182
+                const title = match[2];
183
+                const author = match[3] === 'NULL' ? '' : match[3];
184
+                const dynasty = match[4] === 'NULL' ? '' : match[4].replace(/\\[|\\]/g, '');
185
+
186
+                let poemContent = match[5];
187
+                const poemMatches = poemContent.match(/\"([^\"]+)\"/g) || [];
188
+                const cleanedPoem = poemMatches.map(line => line.replace(/\"/g, '')).join('<br>');
189
+
190
+                let translation = match[6];
191
+                const translationMatches = translation.match(/\"([^\"]+)\"/g) || [];
192
+                const cleanedTranslation = translationMatches.map(line => line.replace(/\"/g, '')).join('<br>');
193
+
194
+                const formattedTitle = title.replace(/([。]|(?<!——)\s+)(?!——)\s*/g, '$1<br>');
195
+
196
+                htmlContent += `
112 197
     <div class="poem-container">
113
-        <div class="poem-title">${title}</div>
198
+        <div class="poem-title">${formattedTitle}</div>
114 199
         <div class="poem-info">${dynasty && author ? `${dynasty} ${author}` : dynasty || author}</div>
115 200
         <div class="poem-content">${cleanedPoem}</div>
116 201
         <div class="poem-translation">${cleanedTranslation}</div>
117 202
         <div class="tag">${tag}</div>
118 203
     </div>`;
204
+            }
119 205
         }
120
-        
206
+
121 207
         htmlContent += `
122 208
 </body>
123 209
 </html>`;
124 210
 
125 211
         // 写入生成的HTML文件
126 212
         fs.writeFileSync('/Users/chengjie/Documents/git/miaoguo_system_server/src/test/古诗文背诵.html', htmlContent, 'utf8');
127
-        
213
+
128 214
         console.log("完成,已生成古诗文背诵.html文件");
129 215
         process.exit(0);
130 216
     } catch (error) {