完成部分文章生成

This commit is contained in:
hydrogen committed 2026-04-18 23:20:31 +08:00
1 parent 2a8f56470f
commit 1d37f84b6c
10 files changed
+330

No files matched your search

+1
View File
@@ -0,0 +1 @@
use flakes
+48
View File
@@ -0,0 +1,48 @@
# GP 工农解放社刊物生成器
## 简介
工农解放社文章生成器用于根据MARKDOWN文件和TOML配置文件,自动化生成报纸,促进工农解放社从手工化到工业化的转变
## 技术栈
| 技术栈 | 作用 |
| --- | --- |
| deno | 提供运行时 |
| playwright无头浏览器| HTML转PDF |
| markdown-it | Markdown转化为HTML |
## Flake 使用(推荐)
本项目提供了 `flake.nix`,可在 NixOS 和非 NixOS(只要安装了 Nix 且启用 flakes)上获得一致的开发环境。
### 1. 进入开发环境
```bash
nix develop
```
进入后会自动设置:
- `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1`
- `CHROMIUM_PATH`(若能自动探测到 chromium/chrome)
### 2. 运行
```bash
deno run -A main.ts
deno test -A
```
### 3. 若 CHROMIUM_PATH 未自动设置
手动设置后再运行:
```bash
export CHROMIUM_PATH="$(command -v chromium)"
```
macOS 可使用:
```bash
export CHROMIUM_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
```
+12
View File
@@ -0,0 +1,12 @@
import { parse } from "jsr:@std/toml";
import path from "node:path";
const text = await Deno.readTextFile("config.toml");
const data = parse(text);
console.log(data);
export async function parse_config_file(
path: path.ParsedPath,
): Promise<Record<string, unknown>> {
const toml = await Deno.readTextFile(path.toString());
return parse(toml);
}
+10
View File
@@ -0,0 +1,10 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1",
"markdown-it": "npm:markdown-it@^14.1.1",
"playwright": "npm:playwright@^1.59.1"
}
}
+81
View File
@@ -0,0 +1,81 @@
{
"version": "5",
"specifiers": {
"jsr:@std/assert@1": "1.0.19",
"jsr:@std/internal@^1.0.12": "1.0.12",
"npm:markdown-it@^14.1.1": "14.1.1",
"npm:playwright@^1.59.1": "1.59.1"
},
"jsr": {
"@std/[email protected]": {
"integrity": "eaada96ee120cb980bc47e040f82814d786fe8162ecc53c91d8df60b8755991e",
"dependencies": [
"jsr:@std/internal"
]
},
"@std/[email protected]": {
"integrity": "972a634fd5bc34b242024402972cd5143eac68d8dffaca5eaa4dba30ce17b027"
}
},
"npm": {
"[email protected]": {
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"[email protected]": {
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw=="
},
"[email protected]": {
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"os": ["darwin"],
"scripts": true
},
"[email protected]": {
"integrity": "sha512-5aHCbzQRADcdP+ATqnDuhhJ/MRIqDkZX5pyjFHRRysS8vZ5AbqGEoFIb6pYHPZ+L/OC2Lc+xT8uHVVR5CAK/wQ==",
"dependencies": [
"uc.micro"
]
},
"[email protected]": {
"integrity": "sha512-BuU2qnTti9YKgK5N+IeMubp14ZUKUUw7yeJbkjtosvHiP0AZ5c8IAgEMk79D0eC8F23r4Ac/q8cAIFdm2FtyoA==",
"dependencies": [
"argparse",
"entities",
"linkify-it",
"mdurl",
"punycode.js",
"uc.micro"
],
"bin": true
},
"[email protected]": {
"integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w=="
},
"[email protected]": {
"integrity": "sha512-HBV/RJg81z5BiiZ9yPzIiClYV/QMsDCKUyogwH9p3MCP6IYjUFu/MActgYAvK0oWyV9NlwM3GLBjADyWgydVyg==",
"bin": true
},
"[email protected]": {
"integrity": "sha512-C8oWjPR3F81yljW9o5OxcWzfh6avkVwDD2VYdwIGqTkl+OGFISgypqzfu7dOe4QNLL2aqcWBmI3PMtLIK233lw==",
"dependencies": [
"playwright-core"
],
"optionalDependencies": [
"fsevents"
],
"bin": true
},
"[email protected]": {
"integrity": "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA=="
},
"[email protected]": {
"integrity": "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A=="
}
},
"workspace": {
"dependencies": [
"jsr:@std/assert@1",
"npm:markdown-it@^14.1.1",
"npm:playwright@^1.59.1"
]
}
}
+61
View File
@@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1776169885,
"narHash": "sha256-l/iNYDZ4bGOAFQY2q8y5OAfBBtrDAaPuRQqWaFHVRXM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "4bd9165a9165d7b5e33ae57f3eecbcb28fb231c9",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+55
View File
@@ -0,0 +1,55 @@
{
description = "GP markdown to PDF toolchain (Deno + Playwright + Chromium)";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachSystem [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
] (system:
let
pkgs = import nixpkgs { inherit system; };
lib = pkgs.lib;
in {
devShells.default = pkgs.mkShell {
packages = [
pkgs.deno
pkgs.nodejs_22
] ++ lib.optionals pkgs.stdenv.isLinux [
pkgs.chromium
pkgs.noto-fonts-cjk-sans
];
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1";
shellHook = ''
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
if [ -z "$CHROMIUM_PATH" ]; then
if command -v chromium >/dev/null 2>&1; then
export CHROMIUM_PATH="$(command -v chromium)"
elif command -v chromium-browser >/dev/null 2>&1; then
export CHROMIUM_PATH="$(command -v chromium-browser)"
elif [ -x "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
export CHROMIUM_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
fi
fi
echo "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=$PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD"
if [ -n "$CHROMIUM_PATH" ]; then
echo "CHROMIUM_PATH=$CHROMIUM_PATH"
else
echo "CHROMIUM_PATH is not set."
echo "Linux: add nixpkgs#chromium or install chromium system-wide."
echo "macOS: install Google Chrome/Chromium and set CHROMIUM_PATH."
fi
'';
};
});
}
+62
View File
@@ -0,0 +1,62 @@
import MarkdownIt from "markdown-it";
function resolveChromiumPath(): string {
const chromiumPath = Deno.env.get("CHROMIUM_PATH");
if (chromiumPath) {
return chromiumPath;
}
throw new Error(
[
"CHROMIUM_PATH is not set.",
"Use the provided flake shell or set CHROMIUM_PATH manually.",
'Example: export CHROMIUM_PATH="$(command -v chromium)"',
].join("\n"),
);
}
export async function mdToPdf(
markdownContent: string,
outputPath = "output.pdf",
): Promise<void> {
const { chromium } = await import("playwright");
const md = new MarkdownIt();
const htmlContent = md.render(markdownContent);
const browser = await chromium.launch({
executablePath: resolveChromiumPath(),
headless: true,
args: ["--no-sandbox", "--disable-dev-shm-usage"],
});
try {
const page = await browser.newPage();
const fullHtml = `
<html>
<head>
<style>
body { font-family: "Noto Sans CJK SC", sans-serif; margin: 40px; }
h1 { color: #333; }
code { background: #f4f4f4; padding: 2px 4px; }
</style>
</head>
<body>${htmlContent}</body>
</html>
`;
await page.setContent(fullHtml);
await page.pdf({
path: outputPath,
format: "A4",
printBackground: true,
margin: { top: "20mm", bottom: "20mm", left: "10mm", right: "10mm" },
});
} finally {
await browser.close();
}
}
if (import.meta.main) {
await mdToPdf("# Test Title\n\nHello **PDF**", "output.pdf");
console.log("PDF generated: output.pdf");
}
View File
Whitespace-only changes.
Binary file not shown.