chengjie 3 months ago
parent
commit
4193668b65
1 changed files with 139 additions and 0 deletions
  1. 139 0
      src/test/build.古诗文背诵.js

+ 139 - 0
src/test/build.古诗文背诵.js

@@ -0,0 +1,139 @@
1
+import commonModel from '../model/commonModel.js';
2
+import { stringUtils } from '../util/stringClass.js';
3
+import fs from 'fs';
4
+
5
+async function runScript(){
6
+    try {
7
+        var data = fs.readFileSync('/Users/chengjie/Documents/git/miaoguo_system_server/src/test/古诗词.html', 'utf8');
8
+        
9
+        // 提取表格数据
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
+        const matches = [...data.matchAll(tableRegex)];
12
+        
13
+        // 生成竖排网页
14
+        let htmlContent = `
15
+<!DOCTYPE html>
16
+<html>
17
+<head>
18
+    <meta charset="UTF-8">
19
+    <title>古诗文背诵</title>
20
+    <style>
21
+        @media print {
22
+            @page {
23
+                size: A4;
24
+                margin: 1cm;
25
+            }
26
+        }
27
+        body {
28
+            font-family: "SimSun", "宋体", serif;
29
+            margin: 0;
30
+            padding: 20px;
31
+            background-color: #f9f5e9;
32
+        }
33
+        .poem-container {
34
+            page-break-after: always;
35
+            margin-bottom: 30px;
36
+            padding: 20px;
37
+            border: 1px solid #d1c7b7;
38
+            background-color: #fff;
39
+            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
40
+            min-height: 50vh; /* 设置最小高度 */
41
+            position: relative;
42
+            display: flex;
43
+            flex-direction: column;
44
+            align-items: center;
45
+        }
46
+        .poem-title {
47
+            font-size: 32px;
48
+            font-weight: bold;
49
+            margin: 0 0 20px 0;
50
+            text-align: center;
51
+            color: #8b4513;
52
+            text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
53
+        }
54
+        .poem-info {
55
+            font-size: 18px;
56
+            margin-bottom: 15px;
57
+            text-align: center;
58
+            color: #666;
59
+        }
60
+        .poem-content {
61
+            font-size: 24px;
62
+            line-height: 2;
63
+            margin: 20px auto;
64
+            text-align: center;
65
+            letter-spacing: 2px;
66
+        }
67
+        .poem-translation {
68
+            font-size: 16px;
69
+            line-height: 1.8;
70
+            margin: 20px auto;
71
+            color: #666;
72
+            text-align: left;
73
+            max-width: 80%;
74
+        }
75
+        .tag {
76
+            font-size: 12px;
77
+            color: #999;
78
+            position: absolute;
79
+            bottom: 20px;
80
+            right: 20px;
81
+            text-align: right;
82
+        }
83
+    </style>
84
+</head>
85
+<body>`;
86
+
87
+        // 跳过表头,从索引1开始
88
+        for (let i = 1; i < matches.length; i++) {
89
+            const match = matches[i];
90
+            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>');
110
+            
111
+            htmlContent += `
112
+    <div class="poem-container">
113
+        <div class="poem-title">${title}</div>
114
+        <div class="poem-info">${dynasty && author ? `${dynasty} ${author}` : dynasty || author}</div>
115
+        <div class="poem-content">${cleanedPoem}</div>
116
+        <div class="poem-translation">${cleanedTranslation}</div>
117
+        <div class="tag">${tag}</div>
118
+    </div>`;
119
+        }
120
+        
121
+        htmlContent += `
122
+</body>
123
+</html>`;
124
+
125
+        // 写入生成的HTML文件
126
+        fs.writeFileSync('/Users/chengjie/Documents/git/miaoguo_system_server/src/test/古诗文背诵.html', htmlContent, 'utf8');
127
+        
128
+        console.log("完成,已生成古诗文背诵.html文件");
129
+        process.exit(0);
130
+    } catch (error) {
131
+        console.error('Error executing script:', error);
132
+        process.exit(1);
133
+    }
134
+}
135
+
136
+// 处理Promise并添加错误捕获
137
+runScript().catch(error => {
138
+    console.error('Error in runScript:', error);
139
+});