Repository navigation
Expand file tree
/
Copy pathautotypesetting.html
More file actions
491 lines (454 loc) · 18 KB
/
Copy pathautotypesetting.html
File metadata and controls
491 lines (454 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在线自动排版编辑器</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1400px;
margin: 0 auto;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
padding: 30px;
}
.header {
text-align: center;
margin-bottom: 30px;
border-bottom: 3px solid #667eea;
padding-bottom: 20px;
}
.header h1 {
color: #333;
font-size: 28px;
margin-bottom: 10px;
}
.header p {
color: #666;
font-size: 14px;
}
.content {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.editor-section, .preview-section {
display: flex;
flex-direction: column;
}
.editor-section label, .preview-section label {
font-weight: 600;
color: #333;
margin-bottom: 8px;
font-size: 14px;
}
#editor {
width: 100%;
height: 400px;
padding: 15px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 14px;
font-family: 'Courier New', monospace;
resize: vertical;
transition: border-color 0.3s;
}
#editor:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 8px rgba(102, 126, 234, 0.2);
}
#formatted {
width: 100%;
height: 400px;
padding: 15px;
border: 2px solid #e0e0e0;
border-radius: 8px;
background-color: #fafafa;
overflow-y: auto;
line-height: 1.6;
}
#formatted p {
text-indent: 2em;
margin: 0.8em 0;
color: #333;
}
#formatted pre {
background-color: #f5f5f5;
padding: 12px;
border-radius: 5px;
overflow-x: auto;
font-size: 12px;
}
#formatted img {
max-width: 100%;
height: auto;
margin: 10px 0;
border-radius: 5px;
}
#formatted h1, #formatted h2, #formatted h3,
#formatted h4, #formatted h5, #formatted h6 {
margin: 1em 0 0.5em 0;
color: #333;
}
#formatted h1 { font-size: 24px; color: #667eea; }
#formatted h2 { font-size: 20px; }
#formatted h3 { font-size: 18px; }
#formatted ul, #formatted ol {
margin: 0.8em 0;
padding-left: 2em;
}
#formatted li {
margin: 0.4em 0;
}
#buttons {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 12px;
margin-bottom: 20px;
}
button {
padding: 12px 20px;
font-size: 14px;
font-weight: 600;
color: #fff;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}
button:active {
transform: translateY(0);
}
.info-bar {
background-color: #f0f0f0;
padding: 12px 15px;
border-radius: 8px;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 13px;
color: #666;
margin-bottom: 15px;
}
.info-bar span {
display: flex;
align-items: center;
gap: 5px;
}
.toast {
position: fixed;
bottom: 20px;
right: 20px;
background-color: #333;
color: #fff;
padding: 15px 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
z-index: 1000;
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
transform: translateX(400px);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.success {
background-color: #27ae60;
}
.error {
background-color: #e74c3c;
}
@media (max-width: 1024px) {
.content {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📝 在线自动排版编辑器</h1>
<p>强大的文本处理工具集,支持Markdown、字数统计、Curl转Python等多种功能</p>
</div>
<div class="info-bar">
<span>📊 实时统计:</span>
<span>字数: <strong id="charCount">0</strong> | 行数: <strong id="lineCount">0</strong></span>
<span style="margin-left: auto;">💡 Tip: 输入内容会实时更新统计数据</span>
</div>
<div id="buttons"></div>
<div class="content">
<div class="editor-section">
<label for="editor">📥 输入区域</label>
<textarea id="editor" placeholder="在此输入文本、Markdown、或Curl命令..."></textarea>
</div>
<div class="preview-section">
<label for="formatted">📤 输出/预览区域</label>
<div id="formatted"></div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
// 初始化按钮
const buttons = [
{ id: 'formatButton', text: '✨ Markdown 排版', action: formatMarkdown },
{ id: 'copyButton', text: '📋 一键复制', action: copyContent },
{ id: 'clearButton', text: '🗑️ 清空所有', action: clearAll },
{ id: 'previewButton', text: '👁️ 新窗口预览', action: previewInWindow },
{ id: 'wordCountButton', text: '📊 详细统计', action: showDetailedStats },
{ id: 'curlToPythonButton', text: '🔄 Curl转Python', action: curlToPython }
];
const buttonsContainer = document.getElementById('buttons');
buttons.forEach(btn => {
const button = document.createElement('button');
button.id = btn.id;
button.textContent = btn.text;
button.addEventListener('click', btn.action);
buttonsContainer.appendChild(button);
});
// 实时统计功能
const editor = document.getElementById('editor');
editor.addEventListener('input', updateStats);
function updateStats() {
const text = editor.value;
const charCount = text.length;
const lineCount = text.split('\n').length;
document.getElementById('charCount').textContent = charCount;
document.getElementById('lineCount').textContent = lineCount;
}
// 显示提示信息
function showToast(message, type = 'success') {
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
}
// 1. Markdown 排版功能 - 增强版
function formatMarkdown() {
const editorContent = editor.value;
if (!editorContent.trim()) {
showToast('⚠️ 请先输入内容!', 'error');
return;
}
try {
const formattedContent = marked.parse(editorContent);
const formattedDiv = document.getElementById('formatted');
formattedDiv.innerHTML = formattedContent;
showToast('✓ Markdown 排版成功!');
} catch (error) {
showToast('✗ 排版失败: ' + error.message, 'error');
}
}
// 2. 一键复制功能 - 增强版
function copyContent() {
const formattedDiv = document.getElementById('formatted');
let contentToCopy = '';
// 复制HTML内容或纯文本
if (formattedDiv.innerHTML.includes('<pre>')) {
// 如果是代码块,复制代码内容
const codeElement = formattedDiv.querySelector('pre');
contentToCopy = codeElement ? codeElement.textContent : formattedDiv.innerText;
} else if (formattedDiv.innerHTML) {
// 复制渲染后的文本
contentToCopy = formattedDiv.innerText;
} else {
showToast('⚠️ 没有内容可复制!', 'error');
return;
}
navigator.clipboard.writeText(contentToCopy).then(() => {
showToast('✓ 复制成功!');
}).catch(() => {
showToast('✗ 复制失败,请手动复制', 'error');
});
}
// 3. 清空所有功能 - 增强版
function clearAll() {
if (editor.value.trim() || document.getElementById('formatted').innerHTML) {
if (confirm('确定要清空所有内容吗?')) {
editor.value = '';
document.getElementById('formatted').innerHTML = '';
updateStats();
showToast('✓ 已清空所有内容');
}
} else {
showToast('⚠️ 没有内容可清空', 'error');
}
}
// 4. 新窗口预览功能 - 增强版
function previewInWindow() {
const formattedDiv = document.getElementById('formatted');
if (!formattedDiv.innerHTML.trim()) {
showToast('⚠️ 没有内容可预览!', 'error');
return;
}
const previewWindow = window.open('', '_blank');
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>预览</title>
<style>
body { font-family: 'Segoe UI', sans-serif; max-width: 800px; margin: 40px auto; padding: 20px; line-height: 1.6; }
p { text-indent: 2em; margin: 1em 0; }
h1, h2, h3 { margin-top: 1.5em; margin-bottom: 0.5em; }
h1 { color: #667eea; }
code { background-color: #f5f5f5; padding: 2px 5px; border-radius: 3px; }
pre { background-color: #f5f5f5; padding: 15px; border-radius: 8px; overflow-x: auto; }
img { max-width: 100%; border-radius: 5px; }
</style>
</head>
<body>
${formattedDiv.innerHTML}
</body>
</html>
`;
previewWindow.document.write(htmlContent);
previewWindow.document.close();
showToast('✓ 预览窗口已打开');
}
// 5. 详细统计功能 - 增强版
function showDetailedStats() {
const text = editor.value;
if (!text.trim()) {
showToast('⚠️ 请先输入内容!', 'error');
return;
}
const stats = {
总字符数: text.length,
总行数: text.split('\n').length,
字数: text.replace(/\s/g, '').length,
单词数: text.trim().split(/\s+/).length,
空格数: (text.match(/\s/g) || []).length,
中文字符数: (text.match(/[\u4e00-\u9fff]/g) || []).length,
英文字符数: (text.match(/[a-zA-Z]/g) || []).length,
数字个数: (text.match(/[0-9]/g) || []).length,
标点符号数: (text.match(/[,。!?;:""''()【】…—·]/g) || []).length
};
let statsHtml = '<style>body { font-family: Arial, sans-serif; padding: 30px; max-width: 500px; margin: 50px auto; } h1 { color: #667eea; } .stat-item { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid #eee; } .stat-item strong { color: #333; } .stat-value { color: #667eea; font-weight: bold; }</style>';
statsHtml += '<h1>📊 统计详情</h1>';
Object.entries(stats).forEach(([key, value]) => {
statsHtml += `<div class="stat-item"><strong>${key}:</strong><span class="stat-value">${value}</span></div>`;
});
const statsWindow = window.open('', '_blank');
statsWindow.document.write(`<html><head><meta charset="UTF-8"></head><body>${statsHtml}</body></html>`);
statsWindow.document.close();
showToast('✓ 统计窗口已打开');
}
// 6. Curl转Python功能 - 增强版
function curlToPython() {
const curlCommand = editor.value.trim();
if (!curlCommand) {
showToast('⚠️ 请输入 curl 命令!', 'error');
return;
}
try {
let pythonCode = 'import requests\nimport json\n\n';
// 提取URL - 改进的正则
const urlMatch = curlCommand.match(/(?:curl\s+)?(?:-[a-zA-Z]*\s+)*['"]?([^'"\s]+(?:https?:\/\/)[^'"\s]*)['"]?/);
const url = urlMatch ? urlMatch[1].replace(/['"`]/g, '') : 'https://example.com';
// 提取方法
const method = curlCommand.includes('-X') ?
curlCommand.match(/-X\s+([^\s]+)/)[1].toUpperCase() :
curlCommand.includes('--data') || curlCommand.includes('-d') ? 'POST' : 'GET';
// 提取headers
const headers = {};
const headerRegex = /-H\s+['"]?([^'"]+?)['"]?(?=\s+-|$)/g;
let headerMatch;
while ((headerMatch = headerRegex.exec(curlCommand)) !== null) {
const [key, value] = headerMatch[1].split(': ');
if (key && value) {
headers[key.trim()] = value.trim();
}
}
// 提取cookies
let cookies = null;
const cookieMatch = curlCommand.match(/(?:--cookie|-b)\s+['"]([^'"]+)['"]/);
if (cookieMatch) {
cookies = cookieMatch[1];
}
// 提取data - 处理多种格式
let data = null;
const dataMatch = curlCommand.match(/(?:-d|--data)\s+['"]([^'"]+)['"]/);
if (dataMatch) {
data = dataMatch[1];
}
// 生成Python代码
pythonCode += `# 请求配置\nurl = '${url}'\n`;
if (Object.keys(headers).length > 0) {
pythonCode += `\nheaders = ${JSON.stringify(headers, null, 4)}\n`;
}
if (cookies) {
pythonCode += `\n# Cookie处理\ncookies = '${cookies}'\ncookie_dict = {pair.split('=')[0]: pair.split('=')[1] for pair in cookies.split('; ') if '=' in pair}\n`;
}
if (data) {
pythonCode += `\n# 请求数据\ndata = '${data}'\n`;
pythonCode += `# 如果是JSON数据,可转换为:\n# data = json.loads(data)\n`;
}
pythonCode += `\n# 发送请求\nresponse = requests.${method.toLowerCase()}(\n`;
pythonCode += ` url,\n`;
if (Object.keys(headers).length > 0) {
pythonCode += ` headers=headers,\n`;
}
if (cookies) {
pythonCode += ` cookies=cookie_dict,\n`;
}
if (data) {
pythonCode += ` data=data,\n`;
}
pythonCode += ` timeout=10\n`;
pythonCode += `)\n\n`;
pythonCode += `# 处理响应\nprint(f'状态码: {response.status_code}')\nprint(f'响应内容:\\n{response.text}')\n`;
const formattedDiv = document.getElementById('formatted');
formattedDiv.innerHTML = `<pre style="background-color: #f5f5f5; padding: 15px; border-radius: 8px; overflow-x: auto;">${escapeHtml(pythonCode)}</pre>`;
showToast('✓ Curl 已成功转换为 Python!');
} catch (error) {
showToast('✗ 转换失败: ' + error.message, 'error');
}
}
// HTML转义辅助函数
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
// 页面加载时初始化统计
updateStats();
</script>
</body>
</html>