发布 GB/T 9704-2012 公文排版技能
This commit is contained in:
Executable
+534
@@ -0,0 +1,534 @@
|
||||
#!/usr/bin/env node
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import os from "node:os";
|
||||
import { execFileSync } from "node:child_process";
|
||||
|
||||
const PAGE_W = 11906;
|
||||
const PAGE_H = 16838;
|
||||
const MARGIN = { top: 2098, bottom: 1984, left: 1588, right: 1474 };
|
||||
const CONTENT_W = PAGE_W - MARGIN.left - MARGIN.right;
|
||||
const FIRST_LINE_INDENT = 640;
|
||||
|
||||
function installedFontFamilies() {
|
||||
try {
|
||||
return new Set(
|
||||
execFileSync("fc-list", ["-f", "%{family}\n"], { encoding: "utf8" })
|
||||
.split(/\r?\n/)
|
||||
.flatMap((line) => line.split(","))
|
||||
.map((name) => name.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
} catch {
|
||||
return new Set();
|
||||
}
|
||||
}
|
||||
|
||||
const INSTALLED_FONTS = installedFontFamilies();
|
||||
function preferredFont(candidates) {
|
||||
return candidates.find((font) => INSTALLED_FONTS.has(font)) ?? candidates[0];
|
||||
}
|
||||
|
||||
const FONT = {
|
||||
title: { eastAsia: preferredFont(["方正小标宋简体", "方正小标宋_GBK", "FZXiaoBiaoSong-B05S"]), ascii: "Times New Roman", cs: "Times New Roman" },
|
||||
subtitle: { eastAsia: preferredFont(["仿宋", "仿宋_GB2312", "STFangsong"]), ascii: "Times New Roman", cs: "Times New Roman" },
|
||||
body: { eastAsia: preferredFont(["仿宋", "仿宋_GB2312", "STFangsong"]), ascii: "Times New Roman", cs: "Times New Roman" },
|
||||
h1: { eastAsia: preferredFont(["黑体", "SimHei", "Heiti SC", "STHeiti"]), ascii: "Times New Roman", cs: "Times New Roman" },
|
||||
h2: { eastAsia: preferredFont(["楷体", "楷体_GB2312", "KaiTi", "Kaiti SC", "STKaiti"]), ascii: "Times New Roman", cs: "Times New Roman" },
|
||||
song: { eastAsia: preferredFont(["宋体", "SimSun"]), ascii: "宋体", cs: "宋体" },
|
||||
};
|
||||
|
||||
function usage() {
|
||||
console.log(`Usage:
|
||||
node generate_gongwen_docx.mjs --input source.md --output out.docx [--org 发文机关] [--doc-no 发文字号] [--title 标题] [--subtitle 副标题] [--to 主送机关] [--sender 落款] [--date 日期] [--page-number center|standard|none]
|
||||
|
||||
Notes:
|
||||
- Converts Markdown to a GB/T 9704-2012 page-layout DOCX.
|
||||
- Generates a DOCX from the supplied content and formatting fields. Its default page number is centered; use standard for odd/even page-number placement or none to suppress it.
|
||||
- --org and --doc-no are printed exactly as supplied. The tool formats document content; it does not infer missing information or decide the document's use.
|
||||
- Supports headings, paragraphs, ordered/unordered lines, and pipe tables.
|
||||
- No npm dependencies; requires zip in PATH.`);
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {};
|
||||
const flags = new Set(["help", "no-page-number", "no-page-numbers"]);
|
||||
for (let i = 2; i < argv.length; i += 1) {
|
||||
const key = argv[i];
|
||||
if (!key.startsWith("--")) continue;
|
||||
const name = key.slice(2);
|
||||
if (flags.has(name)) {
|
||||
args[name] = true;
|
||||
continue;
|
||||
}
|
||||
args[name] = argv[i + 1];
|
||||
i += 1;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function esc(value) {
|
||||
return String(value ?? "")
|
||||
.replace(/\*\*([^*]+)\*\*/g, "$1")
|
||||
.replace(/`([^`]+)`/g, "$1")
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function attr(value) {
|
||||
return String(value ?? "")
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function fontAttrs(opts = {}) {
|
||||
const preset = opts.fontPreset ? FONT[opts.fontPreset] : null;
|
||||
const eastAsia = opts.eastAsia ?? opts.font ?? preset?.eastAsia ?? FONT.body.eastAsia;
|
||||
const ascii = opts.ascii ?? preset?.ascii ?? FONT.body.ascii;
|
||||
const hAnsi = opts.hAnsi ?? ascii;
|
||||
const cs = opts.cs ?? preset?.cs ?? ascii;
|
||||
return `w:ascii="${attr(ascii)}" w:hAnsi="${attr(hAnsi)}" w:eastAsia="${attr(eastAsia)}" w:cs="${attr(cs)}" w:hint="eastAsia"`;
|
||||
}
|
||||
|
||||
function run(text, opts = {}) {
|
||||
const size = opts.size ?? "32";
|
||||
const bold = opts.bold ? "<w:b/>" : "";
|
||||
const color = opts.color ?? "000000";
|
||||
return `<w:r><w:rPr><w:rFonts ${fontAttrs(opts)}/>${bold}<w:color w:val="${color}"/><w:sz w:val="${size}"/><w:szCs w:val="${size}"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:t xml:space="preserve">${esc(text)}</w:t></w:r>`;
|
||||
}
|
||||
|
||||
function paragraph(text, opts = {}) {
|
||||
const alignValue = opts.align === undefined ? "both" : opts.align;
|
||||
const align = alignValue ? `<w:jc w:val="${alignValue}"/>` : "";
|
||||
const indAttrs = [];
|
||||
if (opts.indent !== false) indAttrs.push(`w:firstLine="${opts.firstLine ?? FIRST_LINE_INDENT}"`);
|
||||
if (opts.left) indAttrs.push(`w:left="${opts.left}"`);
|
||||
if (opts.right) indAttrs.push(`w:right="${opts.right}"`);
|
||||
const indent = indAttrs.length ? `<w:ind ${indAttrs.join(" ")}/>` : "";
|
||||
const keepNext = opts.keepNext ? "<w:keepNext/>" : "";
|
||||
const before = opts.before ?? "0";
|
||||
const after = opts.after ?? "0";
|
||||
const pPr = `<w:pPr>${keepNext}${align}${indent}<w:spacing w:before="${before}" w:after="${after}" w:line="560" w:lineRule="exact"/><w:adjustRightInd w:val="true"/><w:snapToGrid w:val="true"/><w:kinsoku w:val="true"/></w:pPr>`;
|
||||
return `<w:p>${pPr}${run(text, opts)}</w:p>`;
|
||||
}
|
||||
|
||||
function titleParagraph(text) {
|
||||
return paragraph(text, {
|
||||
align: "center",
|
||||
indent: false,
|
||||
fontPreset: "title",
|
||||
size: "44",
|
||||
before: "0",
|
||||
after: "560",
|
||||
});
|
||||
}
|
||||
|
||||
function agencyMarkParagraph(text) {
|
||||
return paragraph(text, {
|
||||
align: "center",
|
||||
indent: false,
|
||||
fontPreset: "title",
|
||||
size: "56",
|
||||
color: "FF0000",
|
||||
after: "280",
|
||||
});
|
||||
}
|
||||
|
||||
function documentNumberParagraph(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
indent: false,
|
||||
fontPreset: "body",
|
||||
size: "32",
|
||||
after: "160",
|
||||
});
|
||||
}
|
||||
|
||||
function subtitleParagraph(text) {
|
||||
return paragraph(text, {
|
||||
align: "center",
|
||||
indent: false,
|
||||
fontPreset: "subtitle",
|
||||
size: "32",
|
||||
after: "280",
|
||||
});
|
||||
}
|
||||
|
||||
function mainSendParagraph(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
indent: false,
|
||||
fontPreset: "body",
|
||||
size: "32",
|
||||
});
|
||||
}
|
||||
|
||||
function h1(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
fontPreset: "h1",
|
||||
size: "32",
|
||||
keepNext: true,
|
||||
before: "160",
|
||||
after: "0",
|
||||
});
|
||||
}
|
||||
|
||||
function h2(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
fontPreset: "h2",
|
||||
size: "32",
|
||||
keepNext: true,
|
||||
before: "80",
|
||||
after: "0",
|
||||
});
|
||||
}
|
||||
|
||||
function h3(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
fontPreset: "body",
|
||||
size: "32",
|
||||
keepNext: true,
|
||||
before: "40",
|
||||
after: "0",
|
||||
});
|
||||
}
|
||||
|
||||
function h4(text) {
|
||||
return paragraph(text, {
|
||||
align: "left",
|
||||
fontPreset: "body",
|
||||
size: "32",
|
||||
keepNext: true,
|
||||
before: "0",
|
||||
after: "0",
|
||||
});
|
||||
}
|
||||
|
||||
function right(text) {
|
||||
return paragraph(text, { align: "right", indent: false, right: FIRST_LINE_INDENT });
|
||||
}
|
||||
|
||||
function emptyLine() {
|
||||
return `<w:p><w:pPr><w:spacing w:line="560" w:lineRule="exact"/></w:pPr></w:p>`;
|
||||
}
|
||||
|
||||
function tableXml(headers, rows) {
|
||||
const colCount = headers.length;
|
||||
const colWidth = Math.floor(CONTENT_W / colCount);
|
||||
const grid = Array.from({ length: colCount }, () => `<w:gridCol w:w="${colWidth}"/>`).join("");
|
||||
const tr = (cells, header = false) => {
|
||||
const trPr = header ? "<w:trPr><w:tblHeader/></w:trPr>" : "";
|
||||
return `<w:tr>${trPr}${cells.map((cell) => tableCell(cell, colWidth, header)).join("")}</w:tr>`;
|
||||
};
|
||||
return `<w:tbl><w:tblPr><w:tblW w:w="${CONTENT_W}" w:type="dxa"/><w:tblBorders><w:top w:val="single" w:sz="4" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000"/><w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="4" w:space="0" w:color="000000"/><w:insideV w:val="single" w:sz="4" w:space="0" w:color="000000"/></w:tblBorders><w:tblLook w:firstRow="1" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid>${grid}</w:tblGrid>${tr(headers, true)}${rows.map((row) => tr(row)).join("")}</w:tbl>`;
|
||||
}
|
||||
|
||||
function tableCell(text, width, header) {
|
||||
const shade = header ? '<w:shd w:val="clear" w:color="auto" w:fill="EDEDED"/>' : "";
|
||||
const props = `<w:tcPr><w:tcW w:w="${width}" w:type="dxa"/>${shade}<w:tcMar><w:top w:w="80" w:type="dxa"/><w:left w:w="80" w:type="dxa"/><w:bottom w:w="80" w:type="dxa"/><w:right w:w="80" w:type="dxa"/></w:tcMar><w:vAlign w:val="center"/></w:tcPr>`;
|
||||
const pPr = '<w:pPr><w:spacing w:before="0" w:after="0" w:line="560" w:lineRule="exact"/><w:jc w:val="left"/></w:pPr>';
|
||||
const r = run(
|
||||
text,
|
||||
header
|
||||
? { fontPreset: "h1", size: "32" }
|
||||
: { size: "32" },
|
||||
);
|
||||
return `<w:tc>${props}<w:p>${pPr}${r}</w:p></w:tc>`;
|
||||
}
|
||||
|
||||
function stripFrontmatter(md) {
|
||||
return md.replace(/^---\n[\s\S]*?\n---\n?/, "");
|
||||
}
|
||||
|
||||
function parseMarkdown(md) {
|
||||
const lines = stripFrontmatter(md).split(/\r?\n/);
|
||||
const blocks = [];
|
||||
let para = [];
|
||||
let i = 0;
|
||||
const flush = () => {
|
||||
const text = para.join("").trim();
|
||||
if (text) blocks.push({ type: "paragraph", text });
|
||||
para = [];
|
||||
};
|
||||
|
||||
while (i < lines.length) {
|
||||
const line = lines[i];
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) {
|
||||
flush();
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (/^\|.+\|$/.test(trimmed)) {
|
||||
flush();
|
||||
const tableLines = [];
|
||||
while (i < lines.length && /^\|.+\|$/.test(lines[i].trim())) {
|
||||
tableLines.push(lines[i].trim());
|
||||
i += 1;
|
||||
}
|
||||
const rows = tableLines
|
||||
.filter((l) => !/^\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)+\|?$/.test(l))
|
||||
.map((l) => l.replace(/^\||\|$/g, "").split("|").map((c) => c.trim()));
|
||||
if (rows.length > 0) blocks.push({ type: "table", headers: rows[0], rows: rows.slice(1) });
|
||||
continue;
|
||||
}
|
||||
const heading = trimmed.match(/^(#{1,6})\s+(.+)$/);
|
||||
if (heading) {
|
||||
flush();
|
||||
blocks.push({ type: "heading", level: heading[1].length, text: heading[2].trim() });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
const bullet = trimmed.match(/^[-*]\s+(.+)$/);
|
||||
if (bullet) {
|
||||
flush();
|
||||
blocks.push({ type: "paragraph", text: bullet[1].trim() });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
const ordered = trimmed.match(/^(\d+[..、])\s*(.+)$/);
|
||||
if (ordered) {
|
||||
flush();
|
||||
blocks.push({ type: "paragraph", text: `${ordered[1]} ${ordered[2].trim()}` });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
para.push(trimmed);
|
||||
i += 1;
|
||||
}
|
||||
flush();
|
||||
return blocks;
|
||||
}
|
||||
|
||||
function normalizeSignatureText(text) {
|
||||
return String(text ?? "").replace(/\s+/g, "");
|
||||
}
|
||||
|
||||
function isMatchingParagraph(block, text) {
|
||||
return (
|
||||
block?.type === "paragraph" &&
|
||||
normalizeSignatureText(block.text) === normalizeSignatureText(text)
|
||||
);
|
||||
}
|
||||
|
||||
function isChineseDateParagraph(block) {
|
||||
return (
|
||||
block?.type === "paragraph" &&
|
||||
/^\d{4}年\d{1,2}月\d{1,2}日$/.test(normalizeSignatureText(block.text))
|
||||
);
|
||||
}
|
||||
|
||||
function stripTrailingMatchingSignature(blocks, args) {
|
||||
if (args.date && (isMatchingParagraph(blocks.at(-1), args.date) || isChineseDateParagraph(blocks.at(-1)))) {
|
||||
blocks.pop();
|
||||
}
|
||||
if (args.sender && isMatchingParagraph(blocks.at(-1), args.sender)) {
|
||||
blocks.pop();
|
||||
}
|
||||
}
|
||||
|
||||
function isFirstLayerHeadingText(text) {
|
||||
return /^[一二三四五六七八九十百]+、/.test(String(text ?? "").trim());
|
||||
}
|
||||
|
||||
function isSecondLayerHeadingText(text) {
|
||||
return /^([一二三四五六七八九十百]+)/.test(String(text ?? "").trim());
|
||||
}
|
||||
|
||||
function isThirdLayerHeadingText(text) {
|
||||
return /^\d+[..]/.test(String(text ?? "").trim());
|
||||
}
|
||||
|
||||
function isFourthLayerHeadingText(text) {
|
||||
return /^(\d+)/.test(String(text ?? "").trim());
|
||||
}
|
||||
|
||||
function looksLikeMainSend(text) {
|
||||
const compact = String(text ?? "").replace(/\s+/g, "");
|
||||
if (!/[::]$/.test(compact) || compact.length > 60) return false;
|
||||
if (/[。!?;;]/.test(compact)) return false;
|
||||
return /(各|贵|省|市|县|区|镇|乡|机关|单位|公司|部门|委员会|办公室|厅|局|处|中心|领导|集团|党委|党总支|党支部)/.test(compact.slice(0, -1));
|
||||
}
|
||||
|
||||
function renderHeading(block) {
|
||||
if (isFirstLayerHeadingText(block.text)) return h1(block.text);
|
||||
if (isSecondLayerHeadingText(block.text)) return h2(block.text);
|
||||
if (isThirdLayerHeadingText(block.text)) return h3(block.text);
|
||||
if (isFourthLayerHeadingText(block.text)) return h4(block.text);
|
||||
if (block.level <= 2) return h1(block.text);
|
||||
if (block.level === 3) return h2(block.text);
|
||||
if (block.level === 4) return h3(block.text);
|
||||
return h4(block.text);
|
||||
}
|
||||
|
||||
function renderParagraph(block) {
|
||||
if (isFirstLayerHeadingText(block.text)) return h1(block.text);
|
||||
if (isSecondLayerHeadingText(block.text)) return h2(block.text);
|
||||
return paragraph(block.text);
|
||||
}
|
||||
|
||||
function buildDocument(blocks, args) {
|
||||
let title = args.title;
|
||||
const pageNumberMode = args["no-page-number"] || args["no-page-numbers"]
|
||||
? "none"
|
||||
: ["center", "standard", "none"].includes(args["page-number"])
|
||||
? args["page-number"]
|
||||
: "center";
|
||||
const body = [];
|
||||
const sourceBlocks = [...blocks];
|
||||
if (!title && sourceBlocks[0]?.type === "heading") {
|
||||
title = sourceBlocks.shift().text;
|
||||
}
|
||||
if (title && sourceBlocks[0]?.type === "heading" && sourceBlocks[0].text.trim() === title.trim()) {
|
||||
sourceBlocks.shift();
|
||||
}
|
||||
stripTrailingMatchingSignature(sourceBlocks, args);
|
||||
let mainSend = args.to;
|
||||
if (mainSend && isMatchingParagraph(sourceBlocks[0], mainSend)) sourceBlocks.shift();
|
||||
if (!mainSend && sourceBlocks[0]?.type === "paragraph" && looksLikeMainSend(sourceBlocks[0].text)) {
|
||||
mainSend = sourceBlocks.shift().text;
|
||||
}
|
||||
if (args.org) body.push(agencyMarkParagraph(args.org));
|
||||
if (args["doc-no"]) body.push(documentNumberParagraph(args["doc-no"]));
|
||||
if (title) body.push(titleParagraph(title));
|
||||
if (args.subtitle) body.push(subtitleParagraph(args.subtitle));
|
||||
if (mainSend) body.push(mainSendParagraph(mainSend));
|
||||
|
||||
for (const block of sourceBlocks) {
|
||||
if (block.type === "heading") {
|
||||
body.push(renderHeading(block));
|
||||
continue;
|
||||
}
|
||||
if (block.type === "table") {
|
||||
body.push(tableXml(block.headers, block.rows));
|
||||
body.push(emptyLine());
|
||||
continue;
|
||||
}
|
||||
body.push(renderParagraph(block));
|
||||
}
|
||||
if (args.sender || args.date) {
|
||||
body.push(emptyLine());
|
||||
if (args.sender) body.push(right(args.sender));
|
||||
if (args.date) body.push(right(args.date));
|
||||
}
|
||||
const footerRefs = pageNumberMode === "standard"
|
||||
? '<w:footerReference w:type="default" r:id="rIdFooterOdd"/><w:footerReference w:type="even" r:id="rIdFooterEven"/>'
|
||||
: pageNumberMode === "center"
|
||||
? '<w:footerReference w:type="default" r:id="rIdFooterCenter"/>'
|
||||
: "";
|
||||
body.push(`<w:sectPr>${footerRefs}<w:pgSz w:w="${PAGE_W}" w:h="${PAGE_H}"/><w:pgMar w:top="${MARGIN.top}" w:right="${MARGIN.right}" w:bottom="${MARGIN.bottom}" w:left="${MARGIN.left}" w:header="720" w:footer="1588" w:gutter="0"/><w:docGrid w:type="lines" w:linePitch="560"/></w:sectPr>`);
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>${body.join("")}</w:body></w:document>`;
|
||||
}
|
||||
|
||||
function stylesXml() {
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts ${fontAttrs({ fontPreset: "body" })}/><w:color w:val="000000"/><w:sz w:val="32"/><w:szCs w:val="32"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:before="0" w:after="0" w:line="560" w:lineRule="exact"/><w:adjustRightInd w:val="true"/><w:snapToGrid w:val="true"/><w:kinsoku w:val="true"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/><w:rPr><w:rFonts ${fontAttrs({ fontPreset: "body" })}/><w:sz w:val="32"/><w:szCs w:val="32"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:style></w:styles>`;
|
||||
}
|
||||
|
||||
function fontDef(name, altName, family = "roman", charset = "86") {
|
||||
const alt = altName ? `<w:altName w:val="${attr(altName)}"/>` : "";
|
||||
return `<w:font w:name="${attr(name)}">${alt}<w:charset w:val="${charset}"/><w:family w:val="${family}"/><w:pitch w:val="variable"/></w:font>`;
|
||||
}
|
||||
|
||||
function fontTableXml() {
|
||||
const fonts = [
|
||||
fontDef("方正小标宋简体", "Songti SC"),
|
||||
fontDef("方正小标宋_GBK", "Songti SC"),
|
||||
fontDef("FZXiaoBiaoSong-B05S", "方正小标宋简体"),
|
||||
fontDef("FZXiaoBiaoSong-B13S", "方正小标宋_GBK"),
|
||||
fontDef("华文中宋", "STZhongsong"),
|
||||
fontDef("STZhongsong", "华文中宋"),
|
||||
fontDef("仿宋", "STFangsong"),
|
||||
fontDef("仿宋_GB2312", "STFangsong"),
|
||||
fontDef("FangSong", "仿宋"),
|
||||
fontDef("FangSong_GB2312", "仿宋_GB2312"),
|
||||
fontDef("STFangsong", "仿宋"),
|
||||
fontDef("黑体", "Heiti SC", "swiss"),
|
||||
fontDef("SimHei", "黑体", "swiss"),
|
||||
fontDef("Heiti SC", "黑体", "swiss"),
|
||||
fontDef("楷体", "Kaiti SC"),
|
||||
fontDef("楷体_GB2312", "Kaiti SC"),
|
||||
fontDef("KaiTi", "楷体"),
|
||||
fontDef("KaiTi_GB2312", "楷体_GB2312"),
|
||||
fontDef("Kaiti SC", "楷体"),
|
||||
fontDef("宋体", "Songti SC"),
|
||||
fontDef("SimSun", "宋体"),
|
||||
fontDef("Songti SC", "宋体"),
|
||||
fontDef("Times New Roman", "", "roman", "00"),
|
||||
].join("");
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:fonts xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${fonts}</w:fonts>`;
|
||||
}
|
||||
|
||||
function footerXml(align = "center") {
|
||||
const pageRunProps = `<w:rPr><w:rFonts ${fontAttrs({ fontPreset: "song" })}/><w:sz w:val="28"/><w:szCs w:val="28"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr>`;
|
||||
const sideIndent = align === "right"
|
||||
? '<w:ind w:right="280"/>'
|
||||
: align === "left"
|
||||
? '<w:ind w:left="280"/>'
|
||||
: "";
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p><w:pPr><w:jc w:val="${align}"/>${sideIndent}<w:spacing w:before="0" w:after="0"/></w:pPr><w:r>${pageRunProps}<w:t xml:space="preserve">— </w:t></w:r><w:fldSimple w:instr="PAGE"><w:r>${pageRunProps}<w:t>1</w:t></w:r></w:fldSimple><w:r>${pageRunProps}<w:t xml:space="preserve"> —</w:t></w:r></w:p></w:ftr>`;
|
||||
}
|
||||
|
||||
function settingsXml(pageNumberMode) {
|
||||
const evenOdd = pageNumberMode === "standard" ? "<w:evenAndOddHeaders/>" : "";
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${evenOdd}<w:updateFields w:val="true"/></w:settings>`;
|
||||
}
|
||||
|
||||
function documentRelsXml(pageNumberMode) {
|
||||
const footerRels = pageNumberMode === "standard"
|
||||
? '<Relationship Id="rIdFooterOdd" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footerOdd.xml"/><Relationship Id="rIdFooterEven" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footerEven.xml"/>'
|
||||
: pageNumberMode === "center"
|
||||
? '<Relationship Id="rIdFooterCenter" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footerCenter.xml"/>'
|
||||
: "";
|
||||
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdStyles" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rIdFontTable" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/><Relationship Id="rIdSettings" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>${footerRels}</Relationships>`;
|
||||
}
|
||||
|
||||
function writeDocx(output, documentXml, pageNumberMode) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "gongwen-docx-"));
|
||||
fs.mkdirSync(path.join(dir, "_rels"), { recursive: true });
|
||||
fs.mkdirSync(path.join(dir, "word"), { recursive: true });
|
||||
fs.mkdirSync(path.join(dir, "word/_rels"), { recursive: true });
|
||||
const footerTypes = pageNumberMode === "standard"
|
||||
? '<Override PartName="/word/footerOdd.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/><Override PartName="/word/footerEven.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/>'
|
||||
: pageNumberMode === "center"
|
||||
? '<Override PartName="/word/footerCenter.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/>'
|
||||
: "";
|
||||
fs.writeFileSync(path.join(dir, "[Content_Types].xml"), `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/><Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>${footerTypes}</Types>`);
|
||||
fs.writeFileSync(path.join(dir, "_rels/.rels"), `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>`);
|
||||
fs.writeFileSync(path.join(dir, "word/_rels/document.xml.rels"), documentRelsXml(pageNumberMode));
|
||||
fs.writeFileSync(path.join(dir, "word/document.xml"), documentXml);
|
||||
fs.writeFileSync(path.join(dir, "word/styles.xml"), stylesXml());
|
||||
fs.writeFileSync(path.join(dir, "word/fontTable.xml"), fontTableXml());
|
||||
fs.writeFileSync(path.join(dir, "word/settings.xml"), settingsXml(pageNumberMode));
|
||||
if (pageNumberMode === "standard") {
|
||||
fs.writeFileSync(path.join(dir, "word/footerOdd.xml"), footerXml("right"));
|
||||
fs.writeFileSync(path.join(dir, "word/footerEven.xml"), footerXml("left"));
|
||||
} else if (pageNumberMode === "center") {
|
||||
fs.writeFileSync(path.join(dir, "word/footerCenter.xml"), footerXml("center"));
|
||||
}
|
||||
fs.rmSync(output, { force: true });
|
||||
execFileSync("zip", ["-qr", output, "."], { cwd: dir });
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
const args = parseArgs(process.argv);
|
||||
if (args.help || !args.input || !args.output) {
|
||||
usage();
|
||||
process.exit(args.help ? 0 : 1);
|
||||
}
|
||||
const md = fs.readFileSync(args.input, "utf8");
|
||||
const blocks = parseMarkdown(md);
|
||||
const documentXml = buildDocument(blocks, args);
|
||||
const pageNumberMode = args["no-page-number"] || args["no-page-numbers"]
|
||||
? "none"
|
||||
: ["center", "standard", "none"].includes(args["page-number"])
|
||||
? args["page-number"]
|
||||
: "center";
|
||||
writeDocx(path.resolve(args.output), documentXml, pageNumberMode);
|
||||
console.log(`Generated: ${path.resolve(args.output)}`);
|
||||
@@ -0,0 +1,41 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[ValidateSet('codex', 'claude', 'opencode', 'trae-code', 'trae-cli', 'kimi-cli', 'kimi-code', 'workbuddy', 'zcode')]
|
||||
[string[]]$Target,
|
||||
[switch]$All,
|
||||
[switch]$Link,
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
$SkillDir = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
||||
if (-not $Target -and -not $All) { throw 'Pass -All or at least one -Target.' }
|
||||
if ($All) { $Target = @('codex', 'claude', 'opencode', 'trae-code', 'trae-cli', 'kimi-cli', 'kimi-code', 'workbuddy', 'zcode') }
|
||||
|
||||
$Roots = @{
|
||||
'codex' = "$HOME\.codex\skills"
|
||||
'claude' = "$HOME\.claude\skills"
|
||||
'opencode' = "$HOME\.config\opencode\skills"
|
||||
'trae-code' = "$HOME\.trae-cn\skills"
|
||||
'trae-cli' = "$HOME\.traecli\skills"
|
||||
'kimi-cli' = "$HOME\.kimi\skills"
|
||||
'kimi-code' = "$HOME\.kimi-code\skills"
|
||||
'workbuddy' = "$HOME\.workbuddy\skills"
|
||||
'zcode' = "$HOME\.zcode\skills"
|
||||
}
|
||||
|
||||
foreach ($Name in $Target) {
|
||||
$Root = $Roots[$Name]
|
||||
$Destination = Join-Path $Root 'gongwen'
|
||||
New-Item -ItemType Directory -Force -Path $Root | Out-Null
|
||||
if (Test-Path $Destination) {
|
||||
if (-not $Force) { throw "Refusing to replace $Destination. Review it, then rerun with -Force." }
|
||||
Remove-Item -Recurse -Force $Destination
|
||||
}
|
||||
if ($Link) {
|
||||
New-Item -ItemType SymbolicLink -Path $Destination -Target $SkillDir | Out-Null
|
||||
} else {
|
||||
Copy-Item -Recurse -Force -Exclude '.git', 'dist' -Path $SkillDir -Destination $Destination
|
||||
}
|
||||
if (-not (Test-Path (Join-Path $Destination 'SKILL.md'))) { throw "Installation failed for $Name." }
|
||||
Write-Output "Installed: $Name -> $Destination"
|
||||
}
|
||||
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
skill_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
force=false
|
||||
targets=()
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: scripts/install.sh [--all | --target NAME ...] [--force]
|
||||
|
||||
Targets: codex, claude, opencode, trae-code, trae-cli, kimi-cli, kimi-code,
|
||||
workbuddy, zcode, traework
|
||||
|
||||
Installs the canonical skill as a symbolic link. Existing non-linked gongwen
|
||||
directories are preserved unless --force is supplied. The traework target
|
||||
creates a ZIP for import in TraeWork instead of guessing an unsupported path.
|
||||
EOF
|
||||
}
|
||||
|
||||
while (($#)); do
|
||||
case "$1" in
|
||||
--all) targets=(codex claude opencode trae-code trae-cli kimi-cli kimi-code workbuddy zcode traework) ;;
|
||||
--target) shift; targets+=("${1:?missing target name}") ;;
|
||||
--force) force=true ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) printf 'Unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if ((${#targets[@]} == 0)); then
|
||||
usage >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
target_root() {
|
||||
case "$1" in
|
||||
codex) printf '%s/.codex/skills' "$HOME" ;;
|
||||
claude) printf '%s/.claude/skills' "$HOME" ;;
|
||||
opencode) printf '%s/.config/opencode/skills' "$HOME" ;;
|
||||
trae-code) printf '%s/.trae-cn/skills' "$HOME" ;;
|
||||
trae-cli) printf '%s/.traecli/skills' "$HOME" ;;
|
||||
kimi-cli) printf '%s/.kimi/skills' "$HOME" ;;
|
||||
kimi-code) printf '%s/.kimi-code/skills' "$HOME" ;;
|
||||
workbuddy) printf '%s/.workbuddy/skills' "$HOME" ;;
|
||||
zcode) printf '%s/.zcode/skills' "$HOME" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
install_link() {
|
||||
local name=$1 root destination
|
||||
root=$(target_root "$name") || { printf 'Unsupported target: %s\n' "$name" >&2; exit 2; }
|
||||
destination="$root/gongwen"
|
||||
mkdir -p "$root"
|
||||
if [[ -e "$destination" || -L "$destination" ]]; then
|
||||
if [[ -L "$destination" && $(readlink "$destination") == "$skill_dir" ]]; then
|
||||
printf 'Already installed: %s\n' "$name"
|
||||
return
|
||||
fi
|
||||
if [[ "$force" != true ]]; then
|
||||
printf 'Refusing to replace existing %s; rerun with --force after review.\n' "$destination" >&2
|
||||
exit 3
|
||||
fi
|
||||
rm -rf "$destination"
|
||||
fi
|
||||
ln -s "$skill_dir" "$destination"
|
||||
test -f "$destination/SKILL.md"
|
||||
printf 'Installed: %s -> %s\n' "$name" "$destination"
|
||||
}
|
||||
|
||||
package_traework() {
|
||||
local stage output
|
||||
stage=$(mktemp -d "${TMPDIR:-/tmp}/gongwen-traework.XXXXXX")
|
||||
output="$skill_dir/dist/traework-gongwen-skill.zip"
|
||||
mkdir -p "$skill_dir/dist" "$stage/gongwen"
|
||||
cp -R "$skill_dir/SKILL.md" "$skill_dir/scripts" "$skill_dir/references" "$stage/gongwen/"
|
||||
(cd "$stage" && zip -qr "$output" gongwen)
|
||||
unzip -t "$output" >/dev/null
|
||||
rm -rf "$stage"
|
||||
printf 'Packaged TraeWork import ZIP: %s\n' "$output"
|
||||
}
|
||||
|
||||
for target in "${targets[@]}"; do
|
||||
if [[ "$target" == traework ]]; then
|
||||
package_traework
|
||||
else
|
||||
install_link "$target"
|
||||
fi
|
||||
done
|
||||
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
|
||||
function usage() {
|
||||
console.log("Usage: node verify_gongwen_docx.mjs --input file.docx [--profile layout|standard-pages]");
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {};
|
||||
for (let i = 2; i < argv.length; i += 1) {
|
||||
if (!argv[i].startsWith("--")) continue;
|
||||
args[argv[i].slice(2)] = argv[i + 1];
|
||||
i += 1;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function readPart(file, part, optional = false) {
|
||||
try {
|
||||
return execFileSync("unzip", ["-p", file, part], { encoding: "utf8" });
|
||||
} catch (error) {
|
||||
if (optional) return "";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function paragraphsForText(xml, pattern) {
|
||||
const paragraphs = xml.match(/<w:p(?:\s[^>]*)?>[\s\S]*?<\/w:p>/g) ?? [];
|
||||
return paragraphs.filter((paragraph) => pattern.test(paragraph.replace(/<[^>]+>/g, "")));
|
||||
}
|
||||
|
||||
const args = parseArgs(process.argv);
|
||||
if (!args.input || !fs.existsSync(args.input)) {
|
||||
usage();
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const profile = args.profile ?? "layout";
|
||||
if (!["layout", "standard-pages"].includes(profile)) {
|
||||
console.error("Unsupported profile. Use layout or standard-pages.");
|
||||
process.exit(2);
|
||||
}
|
||||
const doc = readPart(args.input, "word/document.xml");
|
||||
const styles = readPart(args.input, "word/styles.xml");
|
||||
const h1 = paragraphsForText(doc, /^[一二三四五六七八九十]+、/);
|
||||
const h2 = paragraphsForText(doc, /^([一二三四五六七八九十]+)/);
|
||||
const h3 = paragraphsForText(doc, /(?:^|\s)\d+[..]/);
|
||||
const h4 = paragraphsForText(doc, /(\d+)/);
|
||||
const checks = [];
|
||||
|
||||
function check(name, ok, detail) {
|
||||
checks.push({ name, ok, detail });
|
||||
}
|
||||
|
||||
check("A4 page size", /<w:pgSz[^>]*w:w="11906"[^>]*w:h="16838"/.test(doc), "210 mm x 297 mm");
|
||||
check("Page margins", /<w:pgMar[^>]*w:top="2098"[^>]*w:right="1474"[^>]*w:bottom="1984"[^>]*w:left="1588"/.test(doc), "top 37, bottom 35, left 28, right 26 mm");
|
||||
check("Document grid", /<w:docGrid[^>]*w:linePitch="560"/.test(doc), "28 pt grid");
|
||||
const fangSong = /w:eastAsia="(?:仿宋|仿宋_GB2312|FangSong|FangSong_GB2312|STFangsong)"/;
|
||||
const xiaoBiaoSong = /w:eastAsia="(?:方正小标宋简体|方正小标宋_GBK|FZXiaoBiaoSong-B05S)"/;
|
||||
const heiTi = /w:eastAsia="(?:黑体|SimHei|Heiti SC|STHeiti)"/;
|
||||
const kaiTi = /w:eastAsia="(?:楷体|楷体_GB2312|KaiTi|KaiTi_GB2312|Kaiti SC|STKaiti)"/;
|
||||
check("Default body font", fangSong.test(styles) && /<w:sz w:val="32"/.test(styles), "FangSong-compatible 16 pt");
|
||||
const allMatch = (paragraphs, matcher) => paragraphs.length > 0 && paragraphs.every(matcher);
|
||||
check("Title font", xiaoBiaoSong.test(doc) && /<w:sz w:val="44"/.test(doc), "Xiaobiaosong 22 pt requested");
|
||||
check("Level-1 headings", allMatch(h1, (paragraph) => /w:firstLine="640"/.test(paragraph) && heiTi.test(paragraph) && /w:sz w:val="32"/.test(paragraph)), "every 一、 heading is Heiti-compatible 16 pt with two-character indent");
|
||||
check("Level-2 headings", allMatch(h2, (paragraph) => /w:firstLine="640"/.test(paragraph) && kaiTi.test(paragraph) && /w:sz w:val="32"/.test(paragraph)), "every (一) heading is Kaiti-compatible 16 pt with two-character indent");
|
||||
check("Level-3 headings", allMatch(h3, (paragraph) => /w:firstLine="640"/.test(paragraph) && fangSong.test(paragraph) && /w:sz w:val="32"/.test(paragraph)), "every 1. heading is FangSong-compatible 16 pt with two-character indent");
|
||||
check("Level-4 headings", allMatch(h4, (paragraph) => /w:firstLine="640"/.test(paragraph) && fangSong.test(paragraph) && /w:sz w:val="32"/.test(paragraph)), "every (1) heading is FangSong-compatible 16 pt with two-character indent");
|
||||
if (profile === "standard-pages") {
|
||||
const oddFooter = readPart(args.input, "word/footerOdd.xml", true);
|
||||
const evenFooter = readPart(args.input, "word/footerEven.xml", true);
|
||||
check("Odd-page footer", /w:jc w:val="right"/.test(oddFooter) && /w:instr="PAGE"/.test(oddFooter) && /w:ascii="(?:宋体|SimSun)"/.test(oddFooter), "right-aligned page number in Songti-compatible font");
|
||||
check("Even-page footer", /w:jc w:val="left"/.test(evenFooter) && /w:instr="PAGE"/.test(evenFooter) && /w:ascii="(?:宋体|SimSun)"/.test(evenFooter), "left-aligned page number in Songti-compatible font");
|
||||
} else {
|
||||
const footer = readPart(args.input, "word/footerCenter.xml", true);
|
||||
check("Layout page footer", /w:jc w:val="center"/.test(footer) && /w:instr="PAGE"/.test(footer) && /w:ascii="(?:宋体|SimSun)"/.test(footer), "centered page number in Songti-compatible font");
|
||||
}
|
||||
|
||||
for (const item of checks) {
|
||||
console.log(`${item.ok ? "PASS" : "FAIL"} ${item.name}: ${item.detail}`);
|
||||
}
|
||||
|
||||
const failed = checks.filter((item) => !item.ok);
|
||||
if (failed.length) {
|
||||
console.error(`Verification failed: ${failed.length} item(s).`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Verification passed (${profile} profile): ${checks.length} item(s).`);
|
||||
Reference in New Issue
Block a user