gov-doc-format-mcp 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -2
- package/index.js +186 -0
- package/lib/docx.js +257 -0
- package/package.json +26 -5
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# gov-doc-format-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MCP server for checking a Chinese official document (`.docx`) layout against **GB/T 9704-2012**.
|
|
4
|
+
|
|
5
|
+
Checks: paper size (A4), margins (37/35/28/26 mm ±1), body font (FangSong), body size (16 pt),
|
|
6
|
+
line spacing (28–30 pt), title format (22 pt XiaoBiaoSong). Read-only, no network, no auth.
|
|
7
|
+
|
|
8
|
+
Used by the WorkBuddy plugin **公文格式校验**.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npx -y gov-doc-format-mcp@1.0.0
|
|
12
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* gov-doc-format-mcp —— 公文格式校验 MCP Server(WorkBuddy 插件)
|
|
4
|
+
*
|
|
5
|
+
* 用户说「帮我看看这份公文格式对不对」,AI 调这里真去读 docx 的版式参数,
|
|
6
|
+
* 按 GB/T 9704-2012 逐项判,给出「哪一项不合规 + 在 Word 里怎么改」。
|
|
7
|
+
*
|
|
8
|
+
* 只读、纯本地、不联网、零认证、零依赖:自带 stdio JSON-RPC 传输层,
|
|
9
|
+
* npx 拉起即用,不需要下载任何运行时依赖。
|
|
10
|
+
* 不猜观感:页码位置、字体嵌入、印章位置一律标「需目视」。
|
|
11
|
+
*/
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import readline from "node:readline";
|
|
15
|
+
import { parseDocx, checkGbt9704 } from "./lib/docx.js";
|
|
16
|
+
|
|
17
|
+
const NAME = "gov-doc-format";
|
|
18
|
+
const VERSION = "1.0.0";
|
|
19
|
+
const PROTOCOL = "2024-11-05";
|
|
20
|
+
|
|
21
|
+
/* 每一项在 Word 里的改法(用户最需要的是这个,不是"你不合规") */
|
|
22
|
+
const HOWTO = {
|
|
23
|
+
paper: "布局 → 页面设置 → 纸张 → 纸张大小选 A4(21×29.7 厘米)",
|
|
24
|
+
margin_top: "布局 → 页面设置 → 页边距 → 上 3.7 厘米",
|
|
25
|
+
margin_bottom: "布局 → 页面设置 → 页边距 → 下 3.5 厘米",
|
|
26
|
+
margin_left: "布局 → 页面设置 → 页边距 → 左 2.8 厘米",
|
|
27
|
+
margin_right: "布局 → 页面设置 → 页边距 → 右 2.6 厘米",
|
|
28
|
+
body_font: "全选正文 → 字体设为「仿宋_GB2312」(没有就选「仿宋」)",
|
|
29
|
+
body_size: "全选正文 → 字号设为「三号」",
|
|
30
|
+
line_spacing: "全选正文 → 段落 → 行距 → 固定值 → 28 磅(或 29 磅)",
|
|
31
|
+
title: "标题段落 → 字体「方正小标宋简体」(没有就用「宋体」加粗代替)→ 字号「二号」→ 居中",
|
|
32
|
+
pagenum: "插入 → 页码 → 页面底端;单页居右空一字、双页居左空一字,四号宋体",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function fmtReport(file, r) {
|
|
36
|
+
const L = [];
|
|
37
|
+
const name = path.basename(file);
|
|
38
|
+
L.push(`## 公文格式检查:${name}`);
|
|
39
|
+
L.push("");
|
|
40
|
+
const { pass, warn, fail } = r.summary;
|
|
41
|
+
const verdict =
|
|
42
|
+
fail === 0
|
|
43
|
+
? `**结论:版式参数全部合规**(${pass} 项通过${warn ? `,${warn} 项需目视` : ""})`
|
|
44
|
+
: `**结论:${fail} 项不合规,改完再报送**(另有 ${pass} 项通过${warn ? `、${warn} 项需目视` : ""})`;
|
|
45
|
+
L.push(verdict);
|
|
46
|
+
L.push("");
|
|
47
|
+
L.push("| 检查项 | 现状 | 应为 | 判定 |");
|
|
48
|
+
L.push("|---|---|---|---|");
|
|
49
|
+
const tag = { pass: "通过", warn: "需目视", fail: "**不合规**" };
|
|
50
|
+
for (const c of r.checks) {
|
|
51
|
+
L.push(`| ${c.label} | ${c.actual} | ${c.expect} | ${tag[c.status]} |`);
|
|
52
|
+
}
|
|
53
|
+
L.push("");
|
|
54
|
+
|
|
55
|
+
const fails = r.checks.filter((c) => c.status === "fail");
|
|
56
|
+
if (fails.length) {
|
|
57
|
+
L.push("### 怎么改(按顺序,改完再跑一次)");
|
|
58
|
+
L.push("");
|
|
59
|
+
fails.forEach((c, i) => {
|
|
60
|
+
L.push(`${i + 1}. **${c.label}**:${HOWTO[c.id] || c.note || "按标准调整"}`);
|
|
61
|
+
if (c.note) L.push(` - 依据:${c.note}`);
|
|
62
|
+
});
|
|
63
|
+
L.push("");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const warns = r.checks.filter((c) => c.status === "warn");
|
|
67
|
+
if (warns.length) {
|
|
68
|
+
L.push("### 本工具不判、需要你自己看一眼");
|
|
69
|
+
L.push("");
|
|
70
|
+
for (const c of warns) L.push(`- **${c.label}**:${c.expect}${c.note ? `|${c.note}` : ""}`);
|
|
71
|
+
L.push("");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
L.push("---");
|
|
75
|
+
L.push(
|
|
76
|
+
`版心实测 ${r.page.w ? (r.page.w / 56.6929).toFixed(0) : "?"}×${r.page.h ? (r.page.h / 56.6929).toFixed(0) : "?"}mm|` +
|
|
77
|
+
`正文样本 ${r.body ? `${r.body.font} ${r.body.sz ? r.body.sz / 2 : "?"}pt` : "未取到"}|` +
|
|
78
|
+
`标题「${r.title || "未取到"}」`
|
|
79
|
+
);
|
|
80
|
+
L.push("");
|
|
81
|
+
L.push("> 只读这份文档,没有改动它。本工具判的是版式参数(纸张/页边距/字体/字号/行距),不判文字内容、不判印章与版记位置。");
|
|
82
|
+
return L.join("\n");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const TOOLS = [
|
|
86
|
+
{
|
|
87
|
+
name: "check_docx",
|
|
88
|
+
description:
|
|
89
|
+
"按 GB/T 9704-2012 检查一份 .docx 的公文版式:纸张、页边距、正文字体字号、行距、标题格式,逐项给出「现状 / 应为 / 怎么改」。只读文件。",
|
|
90
|
+
inputSchema: {
|
|
91
|
+
type: "object",
|
|
92
|
+
properties: {
|
|
93
|
+
path: { type: "string", description: ".docx 文件的绝对路径" },
|
|
94
|
+
},
|
|
95
|
+
required: ["path"],
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
function callTool(name, args = {}) {
|
|
101
|
+
try {
|
|
102
|
+
if (name !== "check_docx") {
|
|
103
|
+
return { content: [{ type: "text", text: `未知工具:${name}` }], isError: true };
|
|
104
|
+
}
|
|
105
|
+
const p = args.path;
|
|
106
|
+
if (!p) return { content: [{ type: "text", text: "缺少 path 参数" }], isError: true };
|
|
107
|
+
if (!fs.existsSync(p)) {
|
|
108
|
+
return { content: [{ type: "text", text: `找不到文件:${p}` }], isError: true };
|
|
109
|
+
}
|
|
110
|
+
const ext = path.extname(p).toLowerCase();
|
|
111
|
+
if (ext !== ".docx") {
|
|
112
|
+
return {
|
|
113
|
+
content: [{
|
|
114
|
+
type: "text",
|
|
115
|
+
text: `只支持 .docx(当前是 ${ext || "无扩展名"})。` +
|
|
116
|
+
`旧版 .doc 请在 Word 里「另存为 → Word 文档(*.docx)」再检查——` +
|
|
117
|
+
`.doc 是二进制格式,读不出版式参数。`,
|
|
118
|
+
}],
|
|
119
|
+
isError: true,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const parsed = parseDocx(fs.readFileSync(p));
|
|
123
|
+
const res = checkGbt9704(parsed);
|
|
124
|
+
return { content: [{ type: "text", text: fmtReport(p, res) }] };
|
|
125
|
+
} catch (e) {
|
|
126
|
+
return { content: [{ type: "text", text: `出错:${e.message}` }], isError: true };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ---------- stdio JSON-RPC 传输层(零依赖) ---------- */
|
|
131
|
+
function reply(id, result) {
|
|
132
|
+
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, result }) + "\n");
|
|
133
|
+
}
|
|
134
|
+
function replyError(id, code, message) {
|
|
135
|
+
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, error: { code, message } }) + "\n");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function handle(msg) {
|
|
139
|
+
const { id, method, params } = msg;
|
|
140
|
+
const isNotification = id === undefined || id === null;
|
|
141
|
+
|
|
142
|
+
switch (method) {
|
|
143
|
+
case "initialize":
|
|
144
|
+
reply(id, {
|
|
145
|
+
protocolVersion: (params && params.protocolVersion) || PROTOCOL,
|
|
146
|
+
capabilities: { tools: {} },
|
|
147
|
+
serverInfo: { name: NAME, version: VERSION },
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
case "notifications/initialized":
|
|
151
|
+
case "notifications/cancelled":
|
|
152
|
+
return;
|
|
153
|
+
case "ping":
|
|
154
|
+
if (!isNotification) reply(id, {});
|
|
155
|
+
return;
|
|
156
|
+
case "tools/list":
|
|
157
|
+
reply(id, { tools: TOOLS });
|
|
158
|
+
return;
|
|
159
|
+
case "tools/call": {
|
|
160
|
+
const { name, arguments: args } = params || {};
|
|
161
|
+
reply(id, callTool(name, args || {}));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
default:
|
|
165
|
+
if (!isNotification) replyError(id, -32601, `Method not found: ${method}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const rl = readline.createInterface({ input: process.stdin });
|
|
170
|
+
rl.on("line", (line) => {
|
|
171
|
+
const s = line.trim();
|
|
172
|
+
if (!s) return;
|
|
173
|
+
let msg;
|
|
174
|
+
try {
|
|
175
|
+
msg = JSON.parse(s);
|
|
176
|
+
} catch {
|
|
177
|
+
replyError(null, -32700, "Parse error");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
handle(msg);
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (msg.id !== undefined) replyError(msg.id, -32603, `Internal error: ${e.message}`);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
rl.on("close", () => process.exit(0));
|
package/lib/docx.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* docx 版式解析(纯 Node 标准库,零第三方依赖)
|
|
3
|
+
*
|
|
4
|
+
* docx = zip + XML。这里自己解 zip(zlib.inflateRawSync),
|
|
5
|
+
* 只为拿到 word/document.xml / styles.xml / footer*.xml,不引 jszip。
|
|
6
|
+
*
|
|
7
|
+
* 单位约定(踩过的坑都在这):
|
|
8
|
+
* twips = 1/1440 英寸 = 1/56.6929 毫米
|
|
9
|
+
* 字号 sz 是「半磅」:sz 32 = 16pt = 三号
|
|
10
|
+
* 行距 w:line 在 lineRule=exact/atLeast 时单位是 twips(560 = 28 磅)
|
|
11
|
+
*/
|
|
12
|
+
import zlib from "node:zlib";
|
|
13
|
+
|
|
14
|
+
const TWIPS_PER_MM = 56.692913;
|
|
15
|
+
|
|
16
|
+
/* ---------------- zip ---------------- */
|
|
17
|
+
|
|
18
|
+
export function unzip(buf) {
|
|
19
|
+
let eocd = -1;
|
|
20
|
+
for (let i = buf.length - 22; i >= 0; i--) {
|
|
21
|
+
if (buf.readUInt32LE(i) === 0x06054b50) { eocd = i; break; }
|
|
22
|
+
}
|
|
23
|
+
if (eocd < 0) throw new Error("不是有效的 docx(找不到 zip 结尾,可能已损坏或是 .doc 旧格式)");
|
|
24
|
+
const count = buf.readUInt16LE(eocd + 10);
|
|
25
|
+
let off = buf.readUInt32LE(eocd + 16);
|
|
26
|
+
const out = {};
|
|
27
|
+
for (let i = 0; i < count; i++) {
|
|
28
|
+
if (off + 46 > buf.length || buf.readUInt32LE(off) !== 0x02014b50) break;
|
|
29
|
+
const method = buf.readUInt16LE(off + 10);
|
|
30
|
+
let csize = buf.readUInt32LE(off + 20);
|
|
31
|
+
const nameLen = buf.readUInt16LE(off + 28);
|
|
32
|
+
const extraLen = buf.readUInt16LE(off + 30);
|
|
33
|
+
const cmtLen = buf.readUInt16LE(off + 32);
|
|
34
|
+
const lho = buf.readUInt32LE(off + 42);
|
|
35
|
+
const name = buf.toString("utf8", off + 46, off + 46 + nameLen);
|
|
36
|
+
const lNameLen = buf.readUInt16LE(lho + 26);
|
|
37
|
+
const lExtraLen = buf.readUInt16LE(lho + 28);
|
|
38
|
+
const start = lho + 30 + lNameLen + lExtraLen;
|
|
39
|
+
// 用 data descriptor 时中央目录的 csize 才是准的;这里再兜一次到下一区段
|
|
40
|
+
if (!csize) csize = Math.max(0, buf.length - start);
|
|
41
|
+
const raw = buf.subarray(start, start + csize);
|
|
42
|
+
try {
|
|
43
|
+
out[name] = method === 0 ? raw : zlib.inflateRawSync(raw);
|
|
44
|
+
} catch {
|
|
45
|
+
out[name] = Buffer.alloc(0);
|
|
46
|
+
}
|
|
47
|
+
off += 46 + nameLen + extraLen + cmtLen;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ---------------- XML 小工具 ---------------- */
|
|
53
|
+
|
|
54
|
+
const attr = (s, n) => {
|
|
55
|
+
const m = s.match(new RegExp(n + '="([^"]*)"'));
|
|
56
|
+
return m ? m[1] : null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function textOf(xml) {
|
|
60
|
+
const parts = xml.match(/<w:t(?:\s[^>]*)?>([^<]*)<\/w:t>/g) || [];
|
|
61
|
+
return parts.map((p) => p.replace(/<[^>]+>/g, "")).join("").trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function styleMap(stylesXml) {
|
|
65
|
+
const map = {};
|
|
66
|
+
const blocks = stylesXml.match(/<w:style\s[^>]*>[\s\S]*?<\/w:style>/g) || [];
|
|
67
|
+
for (const b of blocks) {
|
|
68
|
+
const id = attr(b, "w:styleId");
|
|
69
|
+
if (!id) continue;
|
|
70
|
+
const rPr = (b.match(/<w:rPr>[\s\S]*?<\/w:rPr>/) || [""])[0];
|
|
71
|
+
const pPr = (b.match(/<w:pPr>[\s\S]*?<\/w:pPr>/) || [""])[0];
|
|
72
|
+
map[id] = {
|
|
73
|
+
sz: rPr ? Number(attr((rPr.match(/<w:sz[^>]*>/) || [""])[0], "w:val")) || null : null,
|
|
74
|
+
eastAsia: rPr ? attr((rPr.match(/<w:rFonts[^>]*>/) || [""])[0], "w:eastAsia") : null,
|
|
75
|
+
line: pPr ? Number(attr((pPr.match(/<w:spacing[^>]*>/) || [""])[0], "w:line")) || null : null,
|
|
76
|
+
lineRule: pPr ? attr((pPr.match(/<w:spacing[^>]*>/) || [""])[0], "w:lineRule") : null,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return map;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* ---------------- 解析 ---------------- */
|
|
83
|
+
|
|
84
|
+
export function parseDocx(buf) {
|
|
85
|
+
const files = unzip(buf);
|
|
86
|
+
const doc = (files["word/document.xml"] || Buffer.alloc(0)).toString("utf8");
|
|
87
|
+
if (!doc) throw new Error("docx 里没有 word/document.xml,可能不是 Word 文档");
|
|
88
|
+
const stylesXml = (files["word/styles.xml"] || Buffer.alloc(0)).toString("utf8");
|
|
89
|
+
const smap = styleMap(stylesXml);
|
|
90
|
+
|
|
91
|
+
// 文档默认(docDefaults)
|
|
92
|
+
const dd = (stylesXml.match(/<w:docDefaults>[\s\S]*?<\/w:docDefaults>/) || [""])[0];
|
|
93
|
+
const defSz = Number(attr((dd.match(/<w:sz[^>]*>/) || [""])[0], "w:val")) || null;
|
|
94
|
+
const defFont = attr((dd.match(/<w:rFonts[^>]*>/) || [""])[0], "w:eastAsia");
|
|
95
|
+
|
|
96
|
+
// 页面设置:取最后一个 sectPr(分节时以末节为准)
|
|
97
|
+
const sects = doc.match(/<w:sectPr[\s>][\s\S]*?<\/w:sectPr>/g) || [];
|
|
98
|
+
const sect = sects[sects.length - 1] || "";
|
|
99
|
+
const pgSz = (sect.match(/<w:pgSz[^>]*>/) || [""])[0];
|
|
100
|
+
const pgMar = (sect.match(/<w:pgMar[^>]*>/) || [""])[0];
|
|
101
|
+
const page = {
|
|
102
|
+
w: Number(attr(pgSz, "w:w")) || null,
|
|
103
|
+
h: Number(attr(pgSz, "w:h")) || null,
|
|
104
|
+
top: Number(attr(pgMar, "w:top")) || null,
|
|
105
|
+
bottom: Number(attr(pgMar, "w:bottom")) || null,
|
|
106
|
+
left: Number(attr(pgMar, "w:left")) || null,
|
|
107
|
+
right: Number(attr(pgMar, "w:right")) || null,
|
|
108
|
+
header: Number(attr(pgMar, "w:header")) || null,
|
|
109
|
+
footer: Number(attr(pgMar, "w:footer")) || null,
|
|
110
|
+
sections: sects.length,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// 段落
|
|
114
|
+
const paras = [];
|
|
115
|
+
const pBlocks = doc.match(/<w:p(?:\s[^>]*)?>[\s\S]*?<\/w:p>|<w:p(?:\s[^>]*)?\/>/g) || [];
|
|
116
|
+
for (const p of pBlocks) {
|
|
117
|
+
const text = textOf(p);
|
|
118
|
+
const pPr = (p.match(/<w:pPr>[\s\S]*?<\/w:pPr>/) || [""])[0];
|
|
119
|
+
const styleId = attr((pPr.match(/<w:pStyle[^>]*>/) || [""])[0], "w:val");
|
|
120
|
+
const st = styleId ? smap[styleId] || {} : {};
|
|
121
|
+
const jc = attr((pPr.match(/<w:jc[^>]*>/) || [""])[0], "w:val");
|
|
122
|
+
const spTag = (pPr.match(/<w:spacing[^>]*>/) || [""])[0];
|
|
123
|
+
const line = Number(attr(spTag, "w:line")) || st.line || null;
|
|
124
|
+
const lineRule = attr(spTag, "w:lineRule") || st.lineRule || null;
|
|
125
|
+
// 段内 run 的字号/字体,取出现最多的
|
|
126
|
+
const runSzs = [...p.matchAll(/<w:sz\s+w:val="(\d+)"/g)].map((m) => Number(m[1]));
|
|
127
|
+
const runFonts = [...p.matchAll(/<w:rFonts[^>]*w:eastAsia="([^"]+)"/g)].map((m) => m[1]);
|
|
128
|
+
const mode = (arr) => {
|
|
129
|
+
if (!arr.length) return null;
|
|
130
|
+
const c = {};
|
|
131
|
+
arr.forEach((x) => (c[x] = (c[x] || 0) + 1));
|
|
132
|
+
return Object.keys(c).sort((a, b) => c[b] - c[a])[0];
|
|
133
|
+
};
|
|
134
|
+
paras.push({
|
|
135
|
+
text,
|
|
136
|
+
styleId: styleId || null,
|
|
137
|
+
jc: jc || null,
|
|
138
|
+
sz: Number(mode(runSzs)) || st.sz || defSz || null,
|
|
139
|
+
font: mode(runFonts) || st.eastAsia || defFont || null,
|
|
140
|
+
line,
|
|
141
|
+
lineRule,
|
|
142
|
+
chars: text.length,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 页脚是否存在(页码通常在这里)
|
|
147
|
+
const footers = Object.keys(files).filter((n) => /^word\/footer\d*\.xml$/.test(n));
|
|
148
|
+
const hasFooterRef = /<w:footerReference[^>]*>/.test(sect);
|
|
149
|
+
|
|
150
|
+
return { page, paras, footers, hasFooterRef, files: Object.keys(files) };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/* ---------------- GB/T 9704-2012 判定 ---------------- */
|
|
154
|
+
|
|
155
|
+
const mm = (v) => Math.round(v * TWIPS_PER_MM);
|
|
156
|
+
const TOL = 57; // ±1mm
|
|
157
|
+
const ptOf = (sz) => (sz ? sz / 2 : null);
|
|
158
|
+
const CN_NUM = { 42: "初号", 36: "小初", 26: "一号", 24: "小一", 22: "二号", 18: "小二", 16: "三号", 15: "小三", 14: "四号", 12: "小四", 10.5: "五号", 9: "小五" };
|
|
159
|
+
|
|
160
|
+
function szLabel(pt) {
|
|
161
|
+
return pt && CN_NUM[pt] ? `${CN_NUM[pt]}(${pt}pt)` : pt ? `${pt}pt` : "未设置";
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function near(actual, expect, tol = TOL) {
|
|
165
|
+
return actual != null && Math.abs(actual - expect) <= tol;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function checkGbt9704(parsed) {
|
|
169
|
+
const { page, paras } = parsed;
|
|
170
|
+
const checks = [];
|
|
171
|
+
const add = (id, label, status, actual, expect, note) =>
|
|
172
|
+
checks.push({ id, label, status, actual, expect, note: note || "" });
|
|
173
|
+
|
|
174
|
+
/* 1. 纸张 */
|
|
175
|
+
const isA4 = near(page.w, 11906, 120) && near(page.h, 16838, 120);
|
|
176
|
+
add(
|
|
177
|
+
"paper", "纸张规格", isA4 ? "pass" : page.w ? "fail" : "warn",
|
|
178
|
+
page.w ? `${(page.w / TWIPS_PER_MM).toFixed(0)}×${(page.h / TWIPS_PER_MM).toFixed(0)}mm` : "未读到 pgSz",
|
|
179
|
+
"A4(210×297mm)",
|
|
180
|
+
isA4 ? "" : "公文用纸幅面尺寸为 A4,请把页面设置改成 A4"
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
/* 2. 页边距 */
|
|
184
|
+
const margins = [
|
|
185
|
+
["top", "上", 37], ["bottom", "下", 35], ["left", "左", 28], ["right", "右", 26],
|
|
186
|
+
];
|
|
187
|
+
for (const [k, cn, want] of margins) {
|
|
188
|
+
const v = page[k];
|
|
189
|
+
const ok = near(v, mm(want));
|
|
190
|
+
add(
|
|
191
|
+
`margin_${k}`, `${cn}页边距`, ok ? "pass" : v ? "fail" : "warn",
|
|
192
|
+
v != null ? `${(v / TWIPS_PER_MM).toFixed(1)}mm` : "未读到 pgMar",
|
|
193
|
+
`${want}mm ±1mm`,
|
|
194
|
+
ok ? "" : `GB/T 9704 规定${cn}白边 ${want}mm(允许 ±1mm)`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* 3. 正文(取字符数最多的段落作为正文样本,跳过标题和空行) */
|
|
199
|
+
const body = paras.filter((p) => p.text.length >= 10).sort((a, b) => b.chars - a.chars)[0] || null;
|
|
200
|
+
if (!body) {
|
|
201
|
+
add("body_font", "正文字体", "warn", "没有找到正文段落", "三号仿宋", "文档为空或只有短行");
|
|
202
|
+
add("body_size", "正文字号", "warn", "没有找到正文段落", "三号(16pt)", "");
|
|
203
|
+
} else {
|
|
204
|
+
const fontOk = /仿宋/.test(body.font || "");
|
|
205
|
+
add("body_font", "正文字体", fontOk ? "pass" : "fail",
|
|
206
|
+
body.font || "未设置中文字体", "仿宋(推荐 仿宋_GB2312)",
|
|
207
|
+
fontOk ? "" : "公文正文用三号仿宋,当前字体不是仿宋");
|
|
208
|
+
const pt = ptOf(body.sz);
|
|
209
|
+
const sizeOk = pt === 16;
|
|
210
|
+
add("body_size", "正文字号", body.sz ? (sizeOk ? "pass" : "fail") : "warn",
|
|
211
|
+
szLabel(pt), "三号(16pt)",
|
|
212
|
+
sizeOk ? "" : "正文应为三号字(16pt),当前不是");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* 4. 行距 */
|
|
216
|
+
const lineCand = paras
|
|
217
|
+
.filter((p) => p.text.length >= 10 && p.line)
|
|
218
|
+
.map((p) => ({ p, pt: p.lineRule && /auto/i.test(p.lineRule) ? (p.line / 240) * 12 : p.line / 20 }))
|
|
219
|
+
.sort((a, b) => b.p.chars - a.p.chars)[0];
|
|
220
|
+
if (!lineCand) {
|
|
221
|
+
add("line_spacing", "行距", "warn", "未读到段落行距(可能用的是默认单倍行距)", "28–30 磅", "建议设为固定值 28 磅或 29 磅");
|
|
222
|
+
} else {
|
|
223
|
+
const pt = lineCand.pt;
|
|
224
|
+
const ok = pt >= 27.5 && pt <= 30.5;
|
|
225
|
+
add("line_spacing", "行距", ok ? "pass" : "fail",
|
|
226
|
+
`${pt.toFixed(1)} 磅${lineCand.p.lineRule ? `(${lineCand.p.lineRule})` : ""}`,
|
|
227
|
+
"28–30 磅",
|
|
228
|
+
ok ? "" : "GB/T 9704 版心为 22 行×28 字,行距一般设 28–30 磅");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* 5. 标题(第一个非空段落) */
|
|
232
|
+
const title = paras.find((p) => p.text.length >= 2) || null;
|
|
233
|
+
if (!title) {
|
|
234
|
+
add("title", "标题格式", "warn", "没有找到标题段落", "二号小标宋", "");
|
|
235
|
+
} else {
|
|
236
|
+
const pt = ptOf(title.sz);
|
|
237
|
+
const fontOk = /小标宋/.test(title.font || "");
|
|
238
|
+
const sizeOk = pt === 22;
|
|
239
|
+
const ok = fontOk && sizeOk;
|
|
240
|
+
add("title", "标题格式", ok ? "pass" : "fail",
|
|
241
|
+
`${title.font || "未设置字体"} / ${szLabel(pt)}`,
|
|
242
|
+
"二号小标宋(22pt)",
|
|
243
|
+
ok ? "" : `首个段落应按标题核:「${title.text.slice(0, 16)}${title.text.length > 16 ? "…" : ""}」,` +
|
|
244
|
+
`应为二号小标宋,当前是 ${title.font || "未设字体"} / ${szLabel(pt)}`)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/* 6. 页码(只做存在性提示,不猜位置) */
|
|
248
|
+
const pageNumHint = parsed.footers.length || parsed.hasFooterRef;
|
|
249
|
+
add("pagenum", "页码", pageNumHint ? "warn" : "warn",
|
|
250
|
+
pageNumHint ? `检测到 ${parsed.footers.length} 个页脚` : "未检测到页脚",
|
|
251
|
+
"四号宋体,单页居右、双页居左,位于版心下缘之下",
|
|
252
|
+
"页码位置需要目视确认——本工具只判版式参数,不判位置观感");
|
|
253
|
+
|
|
254
|
+
const summary = { pass: 0, warn: 0, fail: 0 };
|
|
255
|
+
checks.forEach((c) => summary[c.status]++);
|
|
256
|
+
return { page, checks, summary, body: body ? { font: body.font, sz: body.sz, chars: body.chars } : null, title: title ? title.text.slice(0, 30) : null };
|
|
257
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "gov-doc-format-mcp",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "gov-doc-format-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP server that checks a Chinese official document (.docx) layout against GB/T 9704-2012.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"gov-doc-format-mcp": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"index.js",
|
|
12
|
+
"lib/",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18.0.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"mcp",
|
|
20
|
+
"docx",
|
|
21
|
+
"gbt9704",
|
|
22
|
+
"公文",
|
|
23
|
+
"格式校验"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"dependencies": {}
|
|
6
27
|
}
|