chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,3 @@
|
||||
target/
|
||||
lib/
|
||||
patched-anydoc/
|
||||
+1487
File diff suppressed because it is too large.
Load diff
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
# Go bindings for anydoc.
|
||||
#
|
||||
# `crate-type = ["staticlib"]` produces libanydoc_go.a, which the Go module
|
||||
# links at build time via cgo. Not a cdylib: cgo wants an archive it can pull
|
||||
# symbols from, not a loadable shared object.
|
||||
[package]
|
||||
name = "anydoc-go"
|
||||
version = "0.2.4"
|
||||
edition = "2024"
|
||||
description = "Go bindings (C ABI) for anydoc"
|
||||
license = "MIT"
|
||||
repository = "https://github.com/firecrawl/anydoc"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
# Standalone crate: it lives inside the Academy tree, not the anydoc
|
||||
# workspace, so it takes the published crate rather than a path dependency.
|
||||
# Pinned exactly: the C ABI mirrors this version's error and format enums.
|
||||
[dependencies]
|
||||
anydoc = "=0.2.4"
|
||||
|
||||
[build-dependencies]
|
||||
cbindgen = "0.29"
|
||||
|
||||
# document_to_markdown is crate-private in published anydoc. The build script
|
||||
# copies the pinned version above and re-exports that one function so we can
|
||||
# keep the official GFM serializer after rewriting asset images to External URLs.
|
||||
# It also raises anydoc's zip requirement to 9 for RUSTSEC-2026-0367/0368/0369.
|
||||
[patch.crates-io]
|
||||
anydoc = { path = "patched-anydoc" }
|
||||
|
||||
[workspace]
|
||||
|
||||
# Mirrors the anydoc workspace's release profile: without it the archive keeps
|
||||
# every debug symbol and roughly doubles in size.
|
||||
[profile.release]
|
||||
lto = "thin"
|
||||
strip = "symbols"
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Sideguide Technologies Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
+249
@@ -0,0 +1,249 @@
|
||||
# anydoc-go (vendored)
|
||||
|
||||
Go bindings for [anydoc](https://github.com/firecrawl/anydoc), the Rust library
|
||||
that converts Word, PowerPoint, Excel, OpenDocument, RTF, EPUB, CSV, and PDF
|
||||
documents to GitHub-Flavored Markdown. Academy links them through cgo so office
|
||||
documents can be parsed inside the Go process, without the Python docreader
|
||||
service.
|
||||
|
||||
## Why this is vendored
|
||||
|
||||
The bindings come from the open pull request
|
||||
[firecrawl/anydoc#30](https://github.com/firecrawl/anydoc/pull/30). Until it
|
||||
merges and upstream tags `go/vX.Y.Z`, there is no module to `go get`: the PR
|
||||
also expects a maintainer-only workflow to commit per-platform archives that do
|
||||
not exist yet. So the binding source lives here and `go.mod` points at it:
|
||||
|
||||
```
|
||||
replace github.com/firecrawl/anydoc/go => ./third_party/anydoc-go
|
||||
```
|
||||
|
||||
When an upstream module is published, first verify that it includes the local
|
||||
safety fixes, the 0.2.x model/error additions, and the asset-link rendering API
|
||||
listed below. Only then replace this directory and the `replace` directive.
|
||||
Academy's backend calls our additional `ToDocumentWithAssetLinks` API, so the
|
||||
original PR cannot replace this fork unchanged.
|
||||
|
||||
## Provenance
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| Upstream PR | firecrawl/anydoc#30 ("feat: add Go bindings") |
|
||||
| PR head | `1a7a6c0` |
|
||||
| Rebased onto | `4e3089b` (`chore: release v0.1.8`) |
|
||||
| anydoc crate | `0.2.4` (from crates.io, pinned with `=`) |
|
||||
| Upstream release commit | `42bf1c5ecdde9eb0d96d6bd75a9e6698cf93b14c` |
|
||||
| Last upstream check | 2026-09-29: PR #30 is open, unmerged, and conflicting; head unchanged |
|
||||
| License | MIT (see LICENSE) |
|
||||
|
||||
The PR branched before anydoc 0.1.7, so it was rebased onto v0.1.8 before
|
||||
vendoring. The rebase conflicts were all release bookkeeping — the workspace
|
||||
member list, the README binding sections, and the version-agreement script,
|
||||
which the wasm bindings had touched in the meantime — plus the binding's own
|
||||
version, bumped from 0.1.3 to 0.1.8. Taking v0.1.8 also picks up the
|
||||
`pdf-inspector` 0.1.8 bump that fixes [RUSTSEC-2026-0187](https://rustsec.org/advisories/RUSTSEC-2026-0187.html),
|
||||
the `lopdf` stack overflow that aborts the process on a hostile PDF.
|
||||
|
||||
The pin then moved to 0.1.9, which is that same binding source against a newer
|
||||
PDF stack: anydoc's `src/` is byte-identical between the two releases, so the C
|
||||
ABI, the document model, and the GFM serializer this binding reaches into are
|
||||
unchanged, and none of the local modifications below had to be re-applied. What
|
||||
0.1.9 does change is `pdf-inspector`, from the crate published as 0.1.8 to
|
||||
1.14.2 (the jump in the number is that project unifying its Rust, Python, and
|
||||
Node versions, not fourteen major releases). See "Why 0.1.9 matters here" below.
|
||||
|
||||
## Current upgrade: 0.2.4
|
||||
|
||||
The [0.1.9 to 0.2.4 source diff](https://github.com/firecrawl/anydoc/compare/v0.1.9...v0.2.4)
|
||||
changes the document model and parsing behavior:
|
||||
|
||||
- 0.2.0 replaces calamine with native XLS/XLSX/XLSB readers and improves number
|
||||
formatting, merged cells, DOCX whitespace, EPUB repeated spine parts, and
|
||||
Markdown escaping.
|
||||
- 0.2.1/0.2.2 preserve equations as inline/block LaTeX and refine dollar escaping.
|
||||
- 0.2.3 adds inline checkboxes and spreadsheet form controls. Task-list state
|
||||
moves from `ListItem.checked` to `Inline::Checkbox`.
|
||||
- 0.2.4 returns `NeedsOcr` for PDFs containing scanned pages, including mixed
|
||||
PDFs that previously returned incomplete text. The Go ABI now has a distinct
|
||||
`needs_ocr` error; page numbers remain in `ConvertError.Detail`. `NeedsOCR()`
|
||||
lets Academy recognize wrapped errors without matching English messages.
|
||||
|
||||
Node/Python/CLI hosted OCR is outside the Rust crate. Academy continues using
|
||||
its existing builtin docreader fallback and does not send documents to Firecrawl.
|
||||
The PDF default engine remains builtin. See [upgrade verification and benchmarks](UPGRADE-0.2.4.md).
|
||||
|
||||
## Previous upgrade: why 0.1.9 matters here
|
||||
|
||||
anydoc's changelog lists 0.1.9 as a single dependency bump, which reads like
|
||||
housekeeping. It is nine `pdf-inspector` fixes, and seven of them are
|
||||
denial-of-service bounds on PDF parsing.
|
||||
|
||||
Those seven bound work that had no bound at all: Form XObject expansion per
|
||||
page, CID `/W` ranges, Encoding `cidrange` and ToUnicode `bfrange` expansion,
|
||||
content-stream decode before operators are allocated, the detector's `Tj`/`TJ`
|
||||
operand lookback, and disjoint-rect table clustering. Every one sits on the path
|
||||
`pdf_inspector::process_pdf_mem` takes — the only way anydoc converts a PDF —
|
||||
and every one is driven by nothing but the bytes of the uploaded file.
|
||||
|
||||
This is the class of bug the `guarded()` panic catcher explicitly cannot
|
||||
contain, for the same reason `RUSTSEC-2026-0187` could not be: unbounded CPU
|
||||
never panics, and an allocation failure aborts. The detector lookback is the
|
||||
cheapest to demonstrate — before 1.14.2, each `Tj`/`TJ` whose operand was
|
||||
missing rescanned the whole content stream, so a stream of bare `] TJ` tokens
|
||||
was quadratic:
|
||||
|
||||
| `] TJ` tokens | PDF size | 0.1.8 | 0.1.9 |
|
||||
| --- | --- | --- | --- |
|
||||
| 40,000 | 195 KB | 1.0 s | 2.0 ms |
|
||||
| 120,000 | 586 KB | 9.7 s | 4.1 ms |
|
||||
| 200,000 | 977 KB | 25.9 s | 6.5 ms |
|
||||
| 400,000 | 1.9 MB | 105.6 s | 12.2 ms |
|
||||
|
||||
One run on a 4-core VM, same Go code either side, only the linked archive
|
||||
differing. Quadrupling the input cost 16× the time before the fix and roughly
|
||||
4× after. A 2 MB upload holding a core for nearly two minutes needs no
|
||||
privilege and no malformed container — the file parses fine, it just takes
|
||||
forever, and every concurrent upload takes a core of its own.
|
||||
`TestDetectorLookbackStaysLinear` in `internal/infrastructure/docparser/anydoc`
|
||||
fails if a bump reintroduces it.
|
||||
|
||||
Two commits improve extraction rather than bound it, and both change what gets
|
||||
indexed: text inside Form XObjects now tracks the text line matrix and honors
|
||||
`T*`, `TL`, `'`, `"`, `Tc`, and `Tw`, so nested form text keeps its line breaks
|
||||
and spacing instead of running together, and small-caps runs merge instead of
|
||||
being read as extra table columns — a spurious column reaches the model as a
|
||||
misaligned markdown table.
|
||||
|
||||
Nothing else in the range reaches Academy, which is worth recording so the next
|
||||
upgrade does not go looking for it. The crate published as 0.1.8 was cut at
|
||||
`pdf-inspector`'s `packages-2026-08-10` tag, one release behind 1.14.1, so 0.1.9
|
||||
formally spans that release too — but its two functional commits both miss this
|
||||
binding. One only touches the Node and Python surfaces. The other serves
|
||||
invisible (`Tr 3`) OCR text layers instead of reporting `needs_ocr`, which
|
||||
sounds like it would spare Academy an OCR round trip, except that it landed in
|
||||
`extract_text_in_regions`; anydoc uses the positioned-text path, which already
|
||||
retried with invisible text included. Scanned PDFs still come back as "OCR is
|
||||
required" and still fall back to the docreader.
|
||||
|
||||
## Local modifications
|
||||
|
||||
Keep this list current: it is the diff a future upgrade has to re-apply. Items
|
||||
2–4 are bugs in the upstream PR and are worth sending back to it.
|
||||
|
||||
1. `Cargo.toml` — depends on the published `anydoc = "=0.2.4"` crate instead of
|
||||
the workspace path dependency, declares its own empty `[workspace]`, and
|
||||
repeats the upstream release profile (`lto`, `strip`), which it would
|
||||
otherwise inherit from the anydoc workspace.
|
||||
2. `anydoc.go` — every ABI call runs inside `call()`, which pins the goroutine
|
||||
with `runtime.LockOSThread` for the duration. The ABI reports the error
|
||||
message through a thread-local slot that a *second* call
|
||||
(`anydoc_last_error`) reads, and Go may resume a goroutine on a different OS
|
||||
thread once a cgo call returns: without pinning, a failed conversion reports
|
||||
an empty message, or one belonging to another document parsed on the thread
|
||||
it landed on. Reproduced at roughly 1 in 1500 concurrent conversions; the
|
||||
regression test is `TestErrorDetailSurvivesConcurrency` in
|
||||
`internal/infrastructure/docparser/anydoc`.
|
||||
3. `model.go` — decoder preallocations are bounded by the bytes left in the
|
||||
buffer (`capFor`), and `need` rejects a negative length. A count taken
|
||||
straight from the buffer is only trustworthy while the Rust encoder and this
|
||||
decoder agree; on a skew, `make([]Block, 0, n)` would exhaust memory before
|
||||
the first bounds check, turning a version mismatch into a dead process.
|
||||
4. `src/lib.rs` — conversion entry points run inside `guarded()`,
|
||||
which catches a panic and reports it as a malformed document. A panic
|
||||
escaping an `extern "C"` function aborts the process, and Academy parses
|
||||
untrusted uploads in the same process that serves the API. Note the limit:
|
||||
this cannot contain a stack overflow or an allocation failure, which is why
|
||||
the dependency pin below matters as much as the guard.
|
||||
5. Removed the upstream CLI (`cmd/anydoc`) and the binding test suite, which
|
||||
reads fixtures from the anydoc repository. Academy's own tests live in
|
||||
`internal/infrastructure/docparser/anydoc`.
|
||||
6. `scripts/build-anydoc-lib.sh` copies the pinned anydoc release to
|
||||
`patched-anydoc/` (gitignored) and re-exports `document_to_markdown`, which
|
||||
is crate-private in the published crate. It reads the version from the
|
||||
`anydoc = "=X.Y.Z"` pin above and checks it against `version.go`, so a
|
||||
dependency bump cannot silently leave the reported version behind.
|
||||
`src/asset_links.rs` then rewrites `ImageSource::Asset`
|
||||
to `External("images/image-N.ext")` so the official serializer emits in-place
|
||||
image links. `anydoc_to_markdown_with_asset_links` is the ABI for that path.
|
||||
7. `src/model.rs`, `model.go`, and the generated header carry block/inline math
|
||||
and inline checkbox nodes. Existing wire tags are unchanged. The legacy
|
||||
`ListItem.Checked` slot is retained as nil for compatibility; callers read
|
||||
`Inline.Checked`. Asset traversal accepts math blocks. Heading text retains
|
||||
math and checkbox content when assigning image sections.
|
||||
8. `src/lib.rs` and `errors.go` add `ERR_NEEDS_OCR` / `needs_ocr` and `NeedsOCR()`.
|
||||
Page numbers are preserved in the detail string; structured `pages` and
|
||||
`page_count` fields are not exposed by this Go ABI.
|
||||
9. `anydoc_to_document_with_asset_links` / `ToDocumentWithAssetLinks` parse once,
|
||||
encode the original document (preserving asset IDs), then rewrite image
|
||||
sources and append the official Markdown to the same owned buffer. The
|
||||
backend uses this combined API to avoid parsing each uploaded document twice.
|
||||
The standalone APIs remain available. Corpus tests compare both paths for
|
||||
identical document trees and Markdown; all allocations use the existing
|
||||
`anydoc_buffer_free` ownership contract.
|
||||
|
||||
## Dependency pinning and audit
|
||||
|
||||
`Cargo.lock` is committed and `scripts/build-anydoc-lib.sh` builds with
|
||||
`--locked`, so the archive is always the audited dependency tree. CI runs
|
||||
`cargo audit` against it, because the crate that fails here is the one parsing
|
||||
untrusted uploads inside the API process.
|
||||
|
||||
That matters concretely: with `lopdf` 0.41 — what the upstream PR's own
|
||||
lockfile resolved to — a ~100 KB PDF holding a deeply nested catalog array
|
||||
kills the process with a stack overflow (`RUSTSEC-2026-0187`), which neither
|
||||
`guarded()` nor Go's `recover` can contain. anydoc 0.1.8 moved to `lopdf` 0.42
|
||||
and the same input comes back as an ordinary error;
|
||||
`TestDeeplyNestedPDFFailsWithoutKillingTheProcess` keeps it that way.
|
||||
|
||||
Moving the pin to 0.1.9 added no crate and removed none: `lopdf` stays at 0.42
|
||||
and the whole tree below `pdf-inspector` is unchanged, so the lockfile diff is
|
||||
three version lines and one checksum. That was the 0.1.9 dependency-only update;
|
||||
the 0.2.4 changes are described below.
|
||||
|
||||
The 0.2.4 lockfile removes calamine and five exclusive dependencies, retains
|
||||
`pdf-inspector` 1.14.2 and `lopdf` 0.42.0, and updates `chacha20` from the yanked
|
||||
0.10.1 to 0.10.2. The latter fixes an SSE4.1 intrinsic used by the SSE2 backend
|
||||
([upstream fix](https://github.com/RustCrypto/stream-ciphers/pull/580)); other
|
||||
transitive versions are retained.
|
||||
|
||||
The local patch raises anydoc 0.2.4's `zip` requirement from `8.6.0` to `9`,
|
||||
with `9.0.4` pinned in `Cargo.lock`. This fixes
|
||||
`RUSTSEC-2026-0367`, `RUSTSEC-2026-0368`, and `RUSTSEC-2026-0369`; zip 8.x has
|
||||
no patched release. The patch cache marker includes a revision so existing
|
||||
`patched-anydoc/` copies are regenerated when the local patches change.
|
||||
|
||||
`cargo audit` on 2026-10-17 reports no vulnerabilities and one allowed warning:
|
||||
`ttf-parser` 0.25.1 is
|
||||
unmaintained (`RUSTSEC-2026-0192`), pulled in transitively by the PDF stack. It
|
||||
is not a vulnerability and nothing here can fix it, so warnings report without
|
||||
failing the job.
|
||||
|
||||
## Known upstream limitation
|
||||
|
||||
Markdown rendering drops embedded images: `ImageSource::Asset` renders as its
|
||||
alt text, and the bytes are only reachable through the document model. The
|
||||
renderer (`document_to_markdown`) is also crate-private. Academy keeps that
|
||||
serializer: `scripts/build-anydoc-lib.sh` re-exports the one function, then
|
||||
`anydoc_to_markdown_with_asset_links` rewrites `Asset` images to
|
||||
`ImageSource::External("images/image-N.ext")` so the official GFM output
|
||||
places them in reading order. PDF has no document model; PDFs containing scanned
|
||||
pages fall back to the builtin docreader so they can be rasterized for OCR.
|
||||
|
||||
## Building the archive
|
||||
|
||||
cgo links `lib/<platform>/libanydoc_go.a`, which is a build artifact (~30 MB)
|
||||
and is therefore git-ignored rather than committed:
|
||||
|
||||
```bash
|
||||
scripts/build-anydoc-lib.sh # host platform
|
||||
TARGET=aarch64-unknown-linux-musl scripts/build-anydoc-lib.sh
|
||||
```
|
||||
|
||||
Then build Academy with the engine linked in:
|
||||
|
||||
```bash
|
||||
make build-anydoc # or: go build -tags anydoc ./cmd/server
|
||||
```
|
||||
|
||||
Builds without the `anydoc` tag need no Rust toolchain and no archive; the
|
||||
engine simply reports itself as unavailable.
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
# AnyDoc 0.2.4 升级核查与压测
|
||||
|
||||
核查日期:2026-09-29。项目基线:`9114e4e4f905be71976a77c684d3f92731e6f9a6`。
|
||||
|
||||
## 结论
|
||||
|
||||
从 0.1.9 跟进到 0.2.4,并继续维护 vendored Go binding。升级重点是文档完整性:公式、复选框及混合 PDF 的扫描页不再被静默丢弃。
|
||||
|
||||
- [Go binding PR #30](https://github.com/firecrawl/anydoc/pull/30) 仍为 open,`merged=false`、`mergeable=false`,head 为 `1a7a6c04ff8f2689bf8ebbd1e0d9d61d2d109164`,最后更新时间为 2026-08-08。没有 `go/v*` 发布 tag。
|
||||
- 最新 release tag 为 [v0.2.4](https://github.com/firecrawl/anydoc/tree/v0.2.4),2026-08-27 发布,commit `42bf1c5ecdde9eb0d96d6bd75a9e6698cf93b14c`。main 在该 tag 后的三次提交只改 README。
|
||||
- 已逐文件确认下载的 crate 源码与 v0.2.4 tag 一致,仅保留原有 renderer re-export 补丁。
|
||||
- 检查了 [v0.1.9…v0.2.4 完整源码差异](https://github.com/firecrawl/anydoc/compare/v0.1.9...v0.2.4),包括模型、序列化、PDF 路径、Excel 解析器和 bindings;不是仅根据版本说明判断。
|
||||
|
||||
## 核心变化与本项目处理
|
||||
|
||||
| 上游变化 | 影响与处理 |
|
||||
| --- | --- |
|
||||
| 0.2.0:移除 calamine,自行解析 XLS/XLSX/XLSB;改进数值显示、合并单元格与隐藏内容处理 | 保留已有格式路由,回归各容器。上游 XLSB 样例在旧版失败,新版成功;这不代表应用新增了 XLSB 上传入口。 |
|
||||
| 0.2.0:DOCX run 边缘空格、重复 EPUB spine、Markdown 分隔符及表格代码中的竖线修复 | 使用上游 66 个快照,经实际 Go ABI 比较 Markdown 与错误详情。 |
|
||||
| 0.2.1–0.2.2:OMML/MathML/RTF 公式转 LaTeX,美元符号转义修正 | 新增 block/inline math ABI tag、编码及解码;图片路径接受公式节点。回归 DOCX/PPTX/ODT/EPUB/RTF。 |
|
||||
| 0.2.3:复选框成为 inline 节点,移除 Rust `ListItem.checked` | 新增 checkbox tag 与 bool 字段。旧 wire slot 保留为 nil;Go 的旧字段标为 deprecated。实测有标题的选中、未选中控件。 |
|
||||
| 0.2.4:混合或扫描 PDF 返回 `NeedsOcr`,错误标出页码 | 新增独立错误码及 `NeedsOCR()`,通过 `errors.As` 识别包装错误,实际 Reader 测试验证回退与无 OCR 时的失败行为。页码保留在 Detail,未新增结构化页码 API。 |
|
||||
| Node/Python/CLI 可选择 Firecrawl hosted OCR | Rust 不含该功能;本项目继续使用原有 builtin/docreader 回退。 |
|
||||
|
||||
本地线程绑定、panic 捕获、解码分配上限和图片链接 API 均保留。
|
||||
本次还消除了图片提取路径的重复解析:新的 `ToDocumentWithAssetLinks` 一次解析返回原始文档树和图片 Markdown,复用单一 buffer 的分配/释放协议。先编码原始 asset ID,再重写图片链接,避免丢失图片位置与章节信息。上游语料和内置 13 种非 PDF 样例均验证合并接口与两个独立接口的输出一致。不能在 PR 合入后直接删除本地版本:还需确认上游补齐这些能力。详细差异见 [README](README.md)。
|
||||
|
||||
依赖锁保持 `pdf-inspector=1.14.2`、`lopdf=0.42.0`,移除 calamine 及五个专属依赖。审计发现原有 `chacha20=0.10.1` 已撤回,按[上游修复 #580](https://github.com/RustCrypto/stream-ciphers/pull/580) 更新到 0.10.2,修复 SSE2 后端误用 SSE4.1 指令的问题;其他传递依赖版本未更新。
|
||||
|
||||
## 已完成验证
|
||||
|
||||
- `scripts/build-anydoc-lib.sh`:release archive 构建成功,使用 `--locked`;C header 已同步生成。
|
||||
- `ANYDOC_UPSTREAM_DIR=... go test -tags anydoc -count=1 ./internal/infrastructure/docparser/...`:通过,包括 66 个上游快照、正常文档的 Go 模型解码及图片 Markdown 路径。空输入保留 Go binding 原有的前置错误,单独断言。
|
||||
- `go test -race -tags anydoc -count=1 ./internal/infrastructure/docparser/...`:通过。Go race 检测不覆盖 Rust 内存访问。
|
||||
- `go test -count=1 ./internal/infrastructure/docparser/...`:未链接引擎路径通过。
|
||||
- `go vet -tags anydoc ./internal/infrastructure/docparser/...`:通过。
|
||||
- `go build -tags anydoc -o /tmp/academy-anydoc-server ./cmd/server`:完整服务编译通过。
|
||||
- 既有 4,000 次并发错误详情、恶意 PDF 深层嵌套和 TJ lookback 回归:通过。
|
||||
- `cargo audit --file third_party/anydoc-go/Cargo.lock`:无已知漏洞;保留 `ttf-parser 0.25.1` 的停止维护警告 `RUSTSEC-2026-0192`。
|
||||
|
||||
## 性能测量方法
|
||||
|
||||
Apple M4 Pro,64 GiB RAM,macOS arm64;Go 1.26.3、Rust 1.97.1,Rust release + thin LTO。
|
||||
使用相同的 Go benchmark 和上游固定样例分别链接 0.1.9 与 0.2.4,排除文件读取和编译时间。
|
||||
`WithAssets=true` 覆盖生产 Reader 默认路径:Rust 解析、Go 文档解码、图片收集与官方 Markdown 输出。旧版/直接升级版解析两次,最终版解析一次。PDF 仍走文本路径。
|
||||
每项 500 ms × 3 次,`GOMAXPROCS=1,4`;表中给出三次中位数。四并发的 ns/op 是总耗时除以总次数,表示吞吐能力,不是单个请求延迟。`B/op` 只统计 Go 堆,不能代表 Rust 内存。
|
||||
编译和其他测试结束后顺序测量;这是本机小样本比较,不声称统计显著,也不等价于生产端到端性能。
|
||||
|
||||
### 同机对比:单并发
|
||||
|
||||
单位为 µs/op,越低越好;变化率相对 0.1.9。
|
||||
|
||||
| 格式 | 0.1.9 | 0.2.4 直接升级 | 0.2.4 最终版 | 最终变化 |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| doc | 228.7 | 237.7 | 147.2 | -35.6% |
|
||||
| docx | 1040.3 | 1082.4 | 559.8 | -46.2% |
|
||||
| ppt | 60.0 | 62.7 | 37.0 | -38.4% |
|
||||
| pptx | 887.2 | 895.4 | 450.3 | -49.2% |
|
||||
| odt | 719.5 | 727.5 | 390.7 | -45.7% |
|
||||
| ods | 478.2 | 483.2 | 259.3 | -45.8% |
|
||||
| odp | 1157.2 | 1167.6 | 588.2 | -49.2% |
|
||||
| rtf | 742.8 | 757.4 | 407.2 | -45.2% |
|
||||
| epub | 313.4 | 314.5 | 174.9 | -44.2% |
|
||||
| xls | 69.8 | 127.9 | 76.1 | +9.0% |
|
||||
| xlsx | 217.7 | 500.3 | 261.2 | +20.0% |
|
||||
| xlsb | 转换失败 | 84.3 | 48.7 | 新增可用样例 |
|
||||
| csv | 155.9 | 161.2 | 90.1 | -42.2% |
|
||||
| pdf | 1941.6 | 3605.4 | 3631.9 | +87.1% |
|
||||
|
||||
### 四并发吞吐
|
||||
|
||||
单位为 µs/op(每次转换分摊的墙钟时间,非请求延迟)。
|
||||
|
||||
| 格式 | 0.1.9 | 最终版 | 变化 |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| doc | 53.8 | 36.6 | -32.0% |
|
||||
| docx | 268.3 | 142.5 | -46.9% |
|
||||
| ppt | 15.9 | 9.8 | -38.6% |
|
||||
| pptx | 226.5 | 115.0 | -49.2% |
|
||||
| odt | 179.6 | 99.8 | -44.4% |
|
||||
| ods | 120.5 | 64.1 | -46.8% |
|
||||
| odp | 297.9 | 150.2 | -49.6% |
|
||||
| rtf | 187.9 | 104.3 | -44.5% |
|
||||
| epub | 78.7 | 43.6 | -44.6% |
|
||||
| xls | 19.1 | 19.8 | +3.5% |
|
||||
| xlsx | 52.3 | 65.6 | +25.4% |
|
||||
| xlsb | 转换失败 | 12.5 | 新增可用样例 |
|
||||
| csv | 38.3 | 22.6 | -41.0% |
|
||||
| pdf | 577.5 | 1060.4 | +83.6% |
|
||||
|
||||
### 性能判断
|
||||
|
||||
- 消除重复解析后,DOC/DOCX/PPT/PPTX/ODT/ODS/ODP/RTF/EPUB 的单并发样例耗时降低约 36%–49%,CSV 降低约 42%。
|
||||
- XLS 从 69.8 µs 到 76.1 µs(约 +9%),XLSX 从 217.7 µs 到 261.2 µs(约 +20%)。新解析器的数值/布局/控件处理仍有额外代价;不能宣称所有格式加速。
|
||||
- PDF 从 1.94 ms 到 3.63 ms(约 +87%)。源码新增对被标记扫描页的二次提取确认,以避免丢页和检测误报。这个路径不使用文档模型,合并解析优化不作用于它。保留完整性检查;应用默认 PDF 引擎仍是 builtin,只有选择 AnyDoc 的 PDF 路径受影响。
|
||||
- Go 模型新增一个 checkbox 指针字段,Go 分配字节略有增加;原始输出保留了 B/op 和 allocs/op。
|
||||
|
||||
### 持续混合负载
|
||||
|
||||
8 workers、14 种格式轮换、30 秒,逐次检查 Markdown 与图片字节/名称一致。
|
||||
|
||||
| 指标 | 直接升级 | 最终版 |
|
||||
| --- | ---: | ---: |
|
||||
| 完成转换 | 231,291 | 306,252 |
|
||||
| 吞吐 | 7,708.5 docs/s | 10,207.1 docs/s |
|
||||
| p50 | 0.644 ms | 0.359 ms |
|
||||
| p95 | 5.320 ms | 5.491 ms |
|
||||
| p99 | 6.684 ms | 6.843 ms |
|
||||
| 进程峰值 RSS(含 Rust) | 61.1 MiB | 63.3 MiB |
|
||||
|
||||
两轮均零错误、无输出或图片不一致。最终吞吐比直接升级提高约 32%;尾延迟没有改善,PDF 占据较长执行时间。RSS 包含测试保存的延迟样本,因此不能简单归因于解析器。该测试均匀轮换小文档,不能外推为大文档生产容量。
|
||||
|
||||
### 输入规模测试
|
||||
|
||||
| 输入 | 中位耗时 | Go B/op |
|
||||
| --- | ---: | ---: |
|
||||
| csv_100_rows | 0.398 ms | 129,360 |
|
||||
| csv_10000_rows | 32.133 ms | 12,581,816 |
|
||||
| pdf_40000_operators | 1.074 ms | 152 |
|
||||
| pdf_200000_operators | 4.346 ms | 152 |
|
||||
| xlsx_100_rows | 0.592 ms | 82,624 |
|
||||
| xlsx_10000_rows | 46.186 ms | 7,946,442 |
|
||||
|
||||
CSV 为三列,XLSX 为两列;每种格式内按相同行结构扩展,生成及压缩输入不计入转换耗时。万行 XLSX 约 46.2 ms,校验最后一行未丢失。PDF 的裸 TJ token 从 4 万增到 20 万(5 倍输入),耗时约增至 4 倍,没有重现历史平方级退化。Go 分配不包括 Rust 原生堆。XLSX 原始结果见 [规模压测](benchmarks/2026-09-29/xlsx-scaling.txt)。
|
||||
|
||||
原始数据:[0.1.9](benchmarks/2026-09-29/baseline.txt)、[直接升级](benchmarks/2026-09-29/upgrade.txt)、[最终版](benchmarks/2026-09-29/optimized.txt)、[直接升级压力](benchmarks/2026-09-29/upgrade-stress.txt)、[最终版压力](benchmarks/2026-09-29/optimized-stress.txt)、[规模测试](benchmarks/2026-09-29/scaling.txt)、[审计](benchmarks/2026-09-29/audit.txt)。
|
||||
|
||||
## 复现
|
||||
|
||||
在仓库根目录执行功能核查:
|
||||
|
||||
```bash
|
||||
scripts/build-anydoc-lib.sh
|
||||
git clone --branch v0.2.4 --depth 1 https://github.com/firecrawl/anydoc.git /tmp/anydoc-v0.2.4
|
||||
ANYDOC_UPSTREAM_DIR=/tmp/anydoc-v0.2.4 go test -tags anydoc -count=1 ./internal/infrastructure/docparser/...
|
||||
go test -race -tags anydoc -count=1 ./internal/infrastructure/docparser/...
|
||||
go vet -tags anydoc ./internal/infrastructure/docparser/...
|
||||
cargo audit --file third_party/anydoc-go/Cargo.lock
|
||||
```
|
||||
|
||||
性能与混合负载:
|
||||
|
||||
```bash
|
||||
go test -tags anydoc -run '^$' -bench '^BenchmarkConvert$' -benchmem -benchtime=500ms -count=3 -cpu=1,4 ./internal/infrastructure/docparser/anydoc
|
||||
go test -tags anydoc -run '^$' -bench '^BenchmarkConvertScaling$' -benchmem -benchtime=500ms -count=3 -cpu=1 ./internal/infrastructure/docparser/anydoc
|
||||
go test -tags anydoc -c -o /tmp/anydoc.test ./internal/infrastructure/docparser/anydoc
|
||||
cd internal/infrastructure/docparser/anydoc
|
||||
ANYDOC_STRESS_DURATION=30s /usr/bin/time -l /tmp/anydoc.test -test.run '^TestConcurrentConversionStress$' -test.v
|
||||
```
|
||||
|
||||
Linux 用 `/usr/bin/time -v` 记录 RSS。测试二进制需从包目录运行以读取样例。基线比较需要在旧版本单独编译同一 benchmark 和样例,再顺序运行两个二进制;旧版 XLSB 样例转换失败,基线应排除该项,不能把失败计入吞吐。
|
||||
|
||||
## 验证边界
|
||||
|
||||
本次运行平台为 macOS arm64,Linux glibc/musl、Windows 交叉构建未在本机执行。混合 PDF 测试使用 stub 验证回退请求和结果;未调用外部 OCR 服务、存储或检索索引。压测覆盖解析层,不含上传排队、网络与 OCR 耗时。峰值 RSS 和短时压力不能证明长期不存在原生内存泄漏。
|
||||
Vendored
+235
@@ -0,0 +1,235 @@
|
||||
// Package anydoc converts documents (Word, PowerPoint, Excel, OpenDocument,
|
||||
// RTF, EPUB, CSV, and PDF) to GitHub-Flavored Markdown, with full access to
|
||||
// the parsed document model and embedded assets.
|
||||
//
|
||||
// This is the Go binding. It links a Rust static library through cgo; the
|
||||
// module packages archives for every supported platform. See the README for
|
||||
// the supported platform matrix and musl build tag.
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I${SRCDIR}/include
|
||||
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin_arm64 -lanydoc_go -lm -lstdc++
|
||||
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin_amd64 -lanydoc_go -lm -lstdc++
|
||||
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/windows_amd64 -lanydoc_go
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "include/anydoc.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// call runs one ABI entry point and turns its status code into an error.
|
||||
//
|
||||
// The goroutine is pinned for the duration because the ABI reports the error
|
||||
// message through a thread-local slot that a *second* call
|
||||
// (`anydoc_last_error`) reads. Go is free to resume a goroutine on a different
|
||||
// OS thread after a cgo call returns, and a conversion is long enough for that
|
||||
// to happen: the message would then be missing, or belong to another
|
||||
// conversion that ran on the thread we landed on.
|
||||
func call(entry func() C.int) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
if code := entry(); code != errOK {
|
||||
return convertError(code)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToMarkdown converts a document file to Markdown. The format is detected
|
||||
// from the file content; the extension is the fallback for signature-less
|
||||
// formats (CSV) and unrecognizable containers.
|
||||
func ToMarkdown(path string) (string, error) {
|
||||
cpath := C.CString(path)
|
||||
defer C.free(unsafe.Pointer(cpath))
|
||||
var out *C.char
|
||||
var outLen C.uintptr_t
|
||||
if err := call(func() C.int { return C.anydoc_to_markdown(cpath, &out, &outLen) }); err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer C.anydoc_string_free(out)
|
||||
markdown, err := cStringN(out, outLen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return markdown, nil
|
||||
}
|
||||
|
||||
// ToMarkdownBytes converts an in-memory document to Markdown. Pass a Format
|
||||
// to select the parser, or nil to detect it from the content, which
|
||||
// signature-less formats (CSV) have to name explicitly.
|
||||
func ToMarkdownBytes(data []byte, format *Format) (string, error) {
|
||||
if len(data) == 0 {
|
||||
return "", &ConvertError{Kind: "unsupported", Detail: "empty input"}
|
||||
}
|
||||
tag := C.int(C.ANYDOC_FORMAT_NONE)
|
||||
if format != nil {
|
||||
tag = formatToTag(*format)
|
||||
if tag == C.int(C.ANYDOC_FORMAT_NONE) {
|
||||
return "", &ConvertError{Kind: "unknown_format", Detail: "unknown format: " + string(*format)}
|
||||
}
|
||||
}
|
||||
var out *C.char
|
||||
var outLen C.uintptr_t
|
||||
if err := call(func() C.int {
|
||||
return C.anydoc_to_markdown_bytes(
|
||||
(*C.uint8_t)(unsafe.Pointer(&data[0])), C.uintptr_t(len(data)), tag, &out, &outLen,
|
||||
)
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer C.anydoc_string_free(out)
|
||||
markdown, err := cStringN(out, outLen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return markdown, nil
|
||||
}
|
||||
|
||||
// ToMarkdownWithAssetLinks converts an in-memory document to Markdown with
|
||||
// embedded images rewritten as `` so they keep
|
||||
// their original positions. The Markdown itself is produced by anydoc's
|
||||
// official GFM serializer.
|
||||
func ToMarkdownWithAssetLinks(data []byte, format *Format) (string, error) {
|
||||
if len(data) == 0 {
|
||||
return "", &ConvertError{Kind: "unsupported", Detail: "empty input"}
|
||||
}
|
||||
tag := C.int(C.ANYDOC_FORMAT_NONE)
|
||||
if format != nil {
|
||||
tag = formatToTag(*format)
|
||||
if tag == C.int(C.ANYDOC_FORMAT_NONE) {
|
||||
return "", &ConvertError{Kind: "unknown_format", Detail: "unknown format: " + string(*format)}
|
||||
}
|
||||
}
|
||||
var out *C.char
|
||||
var outLen C.uintptr_t
|
||||
if err := call(func() C.int {
|
||||
return C.anydoc_to_markdown_with_asset_links(
|
||||
(*C.uint8_t)(unsafe.Pointer(&data[0])), C.uintptr_t(len(data)), tag, &out, &outLen,
|
||||
)
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer C.anydoc_string_free(out)
|
||||
markdown, err := cStringN(out, outLen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return markdown, nil
|
||||
}
|
||||
|
||||
// ToDocument parses an in-memory document into the document model, which also
|
||||
// carries the embedded assets. Pass a Format to select the parser, or nil to
|
||||
// detect it from the content.
|
||||
//
|
||||
// Unsupported for PDF: PDF conversion produces Markdown directly and has no
|
||||
// document-model form; use ToMarkdownBytes.
|
||||
func ToDocument(data []byte, format *Format) (*Document, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, &ConvertError{Kind: "unsupported", Detail: "empty input"}
|
||||
}
|
||||
tag := C.int(C.ANYDOC_FORMAT_NONE)
|
||||
if format != nil {
|
||||
tag = formatToTag(*format)
|
||||
if tag == C.int(C.ANYDOC_FORMAT_NONE) {
|
||||
return nil, &ConvertError{Kind: "unknown_format", Detail: "unknown format: " + string(*format)}
|
||||
}
|
||||
}
|
||||
var buf *C.uint8_t
|
||||
var bufLen C.uintptr_t
|
||||
if err := call(func() C.int {
|
||||
return C.anydoc_to_document(
|
||||
(*C.uint8_t)(unsafe.Pointer(&data[0])), C.uintptr_t(len(data)), tag, &buf, &bufLen,
|
||||
)
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C.anydoc_buffer_free(buf, bufLen)
|
||||
if buf == nil || bufLen == 0 {
|
||||
return &Document{}, nil
|
||||
}
|
||||
// Copy the C buffer into a Go-owned slice before decoding, so the C
|
||||
// buffer can be freed immediately rather than tying its lifetime to the
|
||||
// decoded Document.
|
||||
raw, err := cGoBytes(unsafe.Pointer(buf), bufLen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeDocument(raw)
|
||||
}
|
||||
|
||||
// ToDocumentWithAssetLinks parses once and returns both the original model
|
||||
// (including asset IDs and bytes) and Markdown with embedded image links.
|
||||
// PDF has no document model; use ToMarkdownBytes for it.
|
||||
func ToDocumentWithAssetLinks(data []byte, format *Format) (*Document, string, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, "", &ConvertError{Kind: "unsupported", Detail: "empty input"}
|
||||
}
|
||||
tag := C.int(C.ANYDOC_FORMAT_NONE)
|
||||
if format != nil {
|
||||
tag = formatToTag(*format)
|
||||
if tag == C.int(C.ANYDOC_FORMAT_NONE) {
|
||||
return nil, "", &ConvertError{Kind: "unknown_format", Detail: "unknown format: " + string(*format)}
|
||||
}
|
||||
}
|
||||
var buf *C.uint8_t
|
||||
var bufLen C.uintptr_t
|
||||
if err := call(func() C.int {
|
||||
return C.anydoc_to_document_with_asset_links(
|
||||
(*C.uint8_t)(unsafe.Pointer(&data[0])), C.uintptr_t(len(data)), tag, &buf, &bufLen,
|
||||
)
|
||||
}); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer C.anydoc_buffer_free(buf, bufLen)
|
||||
raw, err := cGoBytes(unsafe.Pointer(buf), bufLen)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
d := &decoder{buf: raw}
|
||||
document, err := d.document()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
markdown, err := d.str()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return document, markdown, nil
|
||||
}
|
||||
|
||||
// cStringN reads a length-prefixed C buffer as a Go string. The bytes are
|
||||
// copied, so the caller may free the C buffer immediately after.
|
||||
func cStringN(s *C.char, n C.uintptr_t) (string, error) {
|
||||
if s == nil || n == 0 {
|
||||
return "", nil
|
||||
}
|
||||
b, err := cGoBytes(unsafe.Pointer(s), n)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func cGoBytes(ptr unsafe.Pointer, n C.uintptr_t) ([]byte, error) {
|
||||
// C.GoBytes accepts a C int length even on 64-bit Go builds.
|
||||
if uint64(n) > 1<<31-1 {
|
||||
return nil, &ConvertError{Kind: "resource_limit", Detail: fmt.Sprintf("FFI output is too large for Go: %d bytes", n)}
|
||||
}
|
||||
return C.GoBytes(ptr, C.int(n)), nil
|
||||
}
|
||||
|
||||
// cString reads a NUL-terminated C string as a Go string.
|
||||
func cString(s *C.char) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return C.GoString(s)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
Fetching advisory database from `https://github.com/RustSec/advisory-db.git`
|
||||
Loaded 1273 security advisories (from /Users/wizard/.cargo/advisory-db)
|
||||
Updating crates.io index
|
||||
Scanning third_party/anydoc-go/Cargo.lock for vulnerabilities (162 crate dependencies)
|
||||
Crate: ttf-parser
|
||||
Version: 0.25.1
|
||||
Warning: unmaintained
|
||||
Title: `ttf-parser` is unmaintained
|
||||
Date: 2026-06-28
|
||||
ID: RUSTSEC-2026-0192
|
||||
URL: https://rustsec.org/advisories/RUSTSEC-2026-0192
|
||||
|
||||
warning: 1 allowed warning found
|
||||
@@ -0,0 +1,83 @@
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc
|
||||
cpu: Apple M4 Pro
|
||||
BenchmarkConvert/doc 2556 228702 ns/op 89.55 MB/s 22793 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc 2606 227909 ns/op 89.86 MB/s 22793 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc 2588 229236 ns/op 89.34 MB/s 22793 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 9982 53926 ns/op 379.78 MB/s 22798 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 10000 53771 ns/op 380.87 MB/s 22796 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 10000 53729 ns/op 381.18 MB/s 22796 B/op 325 allocs/op
|
||||
BenchmarkConvert/docx 572 1044082 ns/op 9.73 MB/s 23945 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx 574 1040160 ns/op 9.77 MB/s 23945 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx 578 1040338 ns/op 9.77 MB/s 23945 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2168 268537 ns/op 37.83 MB/s 23950 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2217 267350 ns/op 38.00 MB/s 23949 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2229 268327 ns/op 37.86 MB/s 23950 B/op 342 allocs/op
|
||||
BenchmarkConvert/ppt 9592 60023 ns/op 7736.71 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt 10000 59602 ns/op 7791.48 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt 9822 60202 ns/op 7713.73 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 37984 15919 ns/op 29171.29 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 37519 16036 ns/op 28958.44 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 37756 15898 ns/op 29210.70 MB/s 5312 B/op 73 allocs/op
|
||||
BenchmarkConvert/pptx 661 885527 ns/op 17.77 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx 674 887205 ns/op 17.73 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx 670 887945 ns/op 17.72 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2632 226007 ns/op 69.62 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2641 226665 ns/op 69.42 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2631 226480 ns/op 69.47 MB/s 6016 B/op 85 allocs/op
|
||||
BenchmarkConvert/odt 822 719914 ns/op 35.41 MB/s 23761 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt 806 719450 ns/op 35.43 MB/s 23761 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt 837 709504 ns/op 35.93 MB/s 23761 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3228 180369 ns/op 141.32 MB/s 23765 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3318 179551 ns/op 141.96 MB/s 23767 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3246 179267 ns/op 142.18 MB/s 23765 B/op 348 allocs/op
|
||||
BenchmarkConvert/ods 1246 478194 ns/op 33.76 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods 1254 475562 ns/op 33.94 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods 1250 478694 ns/op 33.72 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4875 120484 ns/op 133.98 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4864 120548 ns/op 133.90 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4912 120833 ns/op 133.59 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/odp 512 1157174 ns/op 16.48 MB/s 12184 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp 518 1157307 ns/op 16.47 MB/s 12184 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp 524 1149194 ns/op 16.59 MB/s 12184 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 2001 297899 ns/op 64.00 MB/s 12186 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 2006 297995 ns/op 63.98 MB/s 12187 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 2017 296884 ns/op 64.22 MB/s 12186 B/op 101 allocs/op
|
||||
BenchmarkConvert/rtf 798 742790 ns/op 27.11 MB/s 25865 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf 807 737679 ns/op 27.30 MB/s 25865 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf 811 751647 ns/op 26.79 MB/s 25865 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3104 187634 ns/op 107.32 MB/s 25871 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3144 188102 ns/op 107.05 MB/s 25870 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3145 187886 ns/op 107.17 MB/s 25872 B/op 383 allocs/op
|
||||
BenchmarkConvert/epub 1939 314142 ns/op 19.63 MB/s 15392 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub 1911 312957 ns/op 19.71 MB/s 15392 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub 1920 313373 ns/op 19.68 MB/s 15392 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 6810 79097 ns/op 77.97 MB/s 15395 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 7555 78542 ns/op 78.52 MB/s 15395 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 7537 78666 ns/op 78.40 MB/s 15395 B/op 216 allocs/op
|
||||
BenchmarkConvert/xls 8110 69795 ns/op 102.70 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls 8925 69836 ns/op 102.64 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls 8622 69393 ns/op 103.30 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 31742 19081 ns/op 375.65 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 31390 19092 ns/op 375.44 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 31503 19032 ns/op 376.63 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 2749 216875 ns/op 32.77 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 2732 217725 ns/op 32.64 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 2757 218701 ns/op 32.49 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 10000 52325 ns/op 135.80 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 10000 52190 ns/op 136.16 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 10000 52334 ns/op 135.78 MB/s 12720 B/op 208 allocs/op
|
||||
BenchmarkConvert/csv 4044 154284 ns/op 1.36 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv 3782 155902 ns/op 1.35 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv 3861 156793 ns/op 1.34 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15680 38338 ns/op 5.48 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15589 38179 ns/op 5.50 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15614 38383 ns/op 5.47 MB/s 9856 B/op 166 allocs/op
|
||||
BenchmarkConvert/pdf 297 1945666 ns/op 46.29 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 308 1939296 ns/op 46.44 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 307 1941629 ns/op 46.38 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 951 579561 ns/op 155.39 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 1015 577545 ns/op 155.94 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 1051 574035 ns/op 156.89 MB/s 2408 B/op 7 allocs/op
|
||||
PASS
|
||||
@@ -0,0 +1,22 @@
|
||||
=== RUN TestConcurrentConversionStress
|
||||
stress_linked_test.go:85: workers=8 conversions=306252 elapsed=30.003928708s throughput=10207.1 docs/s p50=358.792µs p95=5.490542ms p99=6.842708ms GoHeapAlloc=9018048 GoTotalAlloc=4384020160
|
||||
--- PASS: TestConcurrentConversionStress (30.04s)
|
||||
PASS
|
||||
30.04 real 255.23 user 34.26 sys
|
||||
66420736 maximum resident set size
|
||||
0 average shared memory size
|
||||
0 average unshared data size
|
||||
0 average unshared stack size
|
||||
4413 page reclaims
|
||||
32 page faults
|
||||
0 swaps
|
||||
0 block input operations
|
||||
0 block output operations
|
||||
0 messages sent
|
||||
0 messages received
|
||||
11824 signals received
|
||||
1459 voluntary context switches
|
||||
4500950 involuntary context switches
|
||||
3742707465228 instructions retired
|
||||
1062875391967 cycles elapsed
|
||||
57263016 peak memory footprint
|
||||
@@ -0,0 +1,89 @@
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc
|
||||
cpu: Apple M4 Pro
|
||||
BenchmarkConvert/doc 4023 147242 ns/op 139.09 MB/s 23409 B/op 322 allocs/op
|
||||
BenchmarkConvert/doc 3994 147396 ns/op 138.95 MB/s 23409 B/op 322 allocs/op
|
||||
BenchmarkConvert/doc 4172 147069 ns/op 139.25 MB/s 23409 B/op 322 allocs/op
|
||||
BenchmarkConvert/doc-4 16426 36492 ns/op 561.22 MB/s 23413 B/op 322 allocs/op
|
||||
BenchmarkConvert/doc-4 16423 36598 ns/op 559.60 MB/s 23413 B/op 322 allocs/op
|
||||
BenchmarkConvert/doc-4 16438 36571 ns/op 560.00 MB/s 23412 B/op 322 allocs/op
|
||||
BenchmarkConvert/docx 1050 559418 ns/op 18.16 MB/s 25233 B/op 339 allocs/op
|
||||
BenchmarkConvert/docx 1066 559967 ns/op 18.14 MB/s 25233 B/op 339 allocs/op
|
||||
BenchmarkConvert/docx 1063 559776 ns/op 18.15 MB/s 25233 B/op 339 allocs/op
|
||||
BenchmarkConvert/docx-4 4047 142463 ns/op 71.31 MB/s 25238 B/op 339 allocs/op
|
||||
BenchmarkConvert/docx-4 4219 142316 ns/op 71.38 MB/s 25238 B/op 339 allocs/op
|
||||
BenchmarkConvert/docx-4 4200 142903 ns/op 71.09 MB/s 25238 B/op 339 allocs/op
|
||||
BenchmarkConvert/ppt 16210 36971 ns/op 12560.88 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/ppt 16308 37312 ns/op 12446.12 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/ppt 16060 36782 ns/op 12625.21 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/ppt-4 61270 9777 ns/op 47497.14 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/ppt-4 60988 9764 ns/op 47558.72 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/ppt-4 61044 9798 ns/op 47396.27 MB/s 5496 B/op 70 allocs/op
|
||||
BenchmarkConvert/pptx 1304 450272 ns/op 34.94 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/pptx 1338 448129 ns/op 35.11 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/pptx 1318 457588 ns/op 34.38 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/pptx-4 5043 115265 ns/op 136.50 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/pptx-4 5125 114971 ns/op 136.85 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/pptx-4 5152 114589 ns/op 137.31 MB/s 6232 B/op 82 allocs/op
|
||||
BenchmarkConvert/odt 1538 390421 ns/op 65.29 MB/s 25033 B/op 345 allocs/op
|
||||
BenchmarkConvert/odt 1534 390736 ns/op 65.23 MB/s 25033 B/op 345 allocs/op
|
||||
BenchmarkConvert/odt 1522 393443 ns/op 64.78 MB/s 25033 B/op 345 allocs/op
|
||||
BenchmarkConvert/odt-4 5874 99768 ns/op 255.48 MB/s 25037 B/op 345 allocs/op
|
||||
BenchmarkConvert/odt-4 5952 99740 ns/op 255.55 MB/s 25038 B/op 345 allocs/op
|
||||
BenchmarkConvert/odt-4 6004 99918 ns/op 255.10 MB/s 25038 B/op 345 allocs/op
|
||||
BenchmarkConvert/ods 2328 255942 ns/op 63.07 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/ods 2349 259334 ns/op 62.24 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/ods 2276 259975 ns/op 62.09 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/ods-4 8428 64285 ns/op 251.10 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/ods-4 9387 63957 ns/op 252.39 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/ods-4 9322 64114 ns/op 251.77 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/odp 1016 588178 ns/op 32.41 MB/s 12320 B/op 98 allocs/op
|
||||
BenchmarkConvert/odp 1026 592461 ns/op 32.18 MB/s 12320 B/op 98 allocs/op
|
||||
BenchmarkConvert/odp 1008 586609 ns/op 32.50 MB/s 12320 B/op 98 allocs/op
|
||||
BenchmarkConvert/odp-4 3958 149873 ns/op 127.21 MB/s 12322 B/op 98 allocs/op
|
||||
BenchmarkConvert/odp-4 3969 150168 ns/op 126.96 MB/s 12323 B/op 98 allocs/op
|
||||
BenchmarkConvert/odp-4 3978 150448 ns/op 126.72 MB/s 12323 B/op 98 allocs/op
|
||||
BenchmarkConvert/rtf 1465 408854 ns/op 49.25 MB/s 27057 B/op 380 allocs/op
|
||||
BenchmarkConvert/rtf 1471 406746 ns/op 49.51 MB/s 27057 B/op 380 allocs/op
|
||||
BenchmarkConvert/rtf 1478 407178 ns/op 49.45 MB/s 27057 B/op 380 allocs/op
|
||||
BenchmarkConvert/rtf-4 5679 103736 ns/op 194.11 MB/s 27064 B/op 380 allocs/op
|
||||
BenchmarkConvert/rtf-4 5692 104277 ns/op 193.10 MB/s 27064 B/op 380 allocs/op
|
||||
BenchmarkConvert/rtf-4 5516 104262 ns/op 193.13 MB/s 27064 B/op 380 allocs/op
|
||||
BenchmarkConvert/epub 3432 177030 ns/op 34.84 MB/s 15784 B/op 213 allocs/op
|
||||
BenchmarkConvert/epub 3459 174859 ns/op 35.27 MB/s 15784 B/op 213 allocs/op
|
||||
BenchmarkConvert/epub 3442 174040 ns/op 35.43 MB/s 15784 B/op 213 allocs/op
|
||||
BenchmarkConvert/epub-4 13758 43527 ns/op 141.68 MB/s 15788 B/op 213 allocs/op
|
||||
BenchmarkConvert/epub-4 13788 43570 ns/op 141.54 MB/s 15788 B/op 213 allocs/op
|
||||
BenchmarkConvert/epub-4 13702 43761 ns/op 140.92 MB/s 15788 B/op 213 allocs/op
|
||||
BenchmarkConvert/xls 7941 75715 ns/op 94.67 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xls 7932 76079 ns/op 94.22 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xls 7746 76241 ns/op 94.02 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xls-4 30616 19507 ns/op 367.45 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xls-4 30681 19899 ns/op 360.23 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xls-4 30157 19757 ns/op 362.81 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx 2239 263510 ns/op 26.97 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx 2320 261242 ns/op 27.20 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx 2313 259344 ns/op 27.40 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx-4 8738 65600 ns/op 108.32 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx-4 9079 65609 ns/op 108.31 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsx-4 9020 65581 ns/op 108.35 MB/s 13288 B/op 205 allocs/op
|
||||
BenchmarkConvert/xlsb 12316 48681 ns/op 32.91 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/xlsb 12358 48634 ns/op 32.94 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/xlsb 12310 48954 ns/op 32.72 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/xlsb-4 48037 12567 ns/op 127.48 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/xlsb-4 47362 12528 ns/op 127.88 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/xlsb-4 48376 12470 ns/op 128.47 MB/s 5768 B/op 81 allocs/op
|
||||
BenchmarkConvert/csv 6783 90056 ns/op 2.33 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/csv 6709 90130 ns/op 2.33 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/csv 6710 89996 ns/op 2.33 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/csv-4 26893 22533 ns/op 9.32 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/csv-4 26581 22763 ns/op 9.23 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/csv-4 26870 22610 ns/op 9.29 MB/s 10168 B/op 163 allocs/op
|
||||
BenchmarkConvert/pdf 163 3827329 ns/op 23.53 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 160 3622717 ns/op 24.86 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 163 3631949 ns/op 24.80 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 576 1061518 ns/op 84.84 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 562 1060361 ns/op 84.93 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 556 1046297 ns/op 86.07 MB/s 2408 B/op 7 allocs/op
|
||||
PASS
|
||||
@@ -0,0 +1,17 @@
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc
|
||||
cpu: Apple M4 Pro
|
||||
BenchmarkConvertScaling/csv_100_rows 1497 397701 ns/op 9.12 MB/s 129360 B/op 1930 allocs/op
|
||||
BenchmarkConvertScaling/csv_100_rows 1510 398194 ns/op 9.11 MB/s 129360 B/op 1930 allocs/op
|
||||
BenchmarkConvertScaling/csv_100_rows 1504 395770 ns/op 9.16 MB/s 129360 B/op 1930 allocs/op
|
||||
BenchmarkConvertScaling/csv_10000_rows 18 31730509 ns/op 11.35 MB/s 12581816 B/op 190030 allocs/op
|
||||
BenchmarkConvertScaling/csv_10000_rows 16 32328646 ns/op 11.14 MB/s 12581816 B/op 190030 allocs/op
|
||||
BenchmarkConvertScaling/csv_10000_rows 16 32133409 ns/op 11.20 MB/s 12581816 B/op 190030 allocs/op
|
||||
BenchmarkConvertScaling/pdf_40000_operators 547 1076646 ns/op 186.32 MB/s 152 B/op 7 allocs/op
|
||||
BenchmarkConvertScaling/pdf_40000_operators 559 1074017 ns/op 186.77 MB/s 152 B/op 7 allocs/op
|
||||
BenchmarkConvertScaling/pdf_40000_operators 558 1064584 ns/op 188.43 MB/s 152 B/op 7 allocs/op
|
||||
BenchmarkConvertScaling/pdf_200000_operators 138 4345923 ns/op 230.24 MB/s 152 B/op 7 allocs/op
|
||||
BenchmarkConvertScaling/pdf_200000_operators 136 4356177 ns/op 229.70 MB/s 152 B/op 7 allocs/op
|
||||
BenchmarkConvertScaling/pdf_200000_operators 138 4319291 ns/op 231.66 MB/s 152 B/op 7 allocs/op
|
||||
PASS
|
||||
@@ -0,0 +1,22 @@
|
||||
=== RUN TestConcurrentConversionStress
|
||||
stress_linked_test.go:85: workers=8 conversions=231291 elapsed=30.004531458s throughput=7708.5 docs/s p50=644.084µs p95=5.320084ms p99=6.684167ms GoHeapAlloc=2712016 GoTotalAlloc=3303175600
|
||||
--- PASS: TestConcurrentConversionStress (30.04s)
|
||||
PASS
|
||||
30.04 real 253.87 user 28.25 sys
|
||||
64110592 maximum resident set size
|
||||
0 average shared memory size
|
||||
0 average unshared data size
|
||||
0 average unshared stack size
|
||||
4274 page reclaims
|
||||
32 page faults
|
||||
0 swaps
|
||||
0 block input operations
|
||||
0 block output operations
|
||||
0 messages sent
|
||||
0 messages received
|
||||
8607 signals received
|
||||
879 voluntary context switches
|
||||
3964104 involuntary context switches
|
||||
3957497347565 instructions retired
|
||||
1043824362343 cycles elapsed
|
||||
54395768 peak memory footprint
|
||||
@@ -0,0 +1,89 @@
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc
|
||||
cpu: Apple M4 Pro
|
||||
BenchmarkConvert/doc 2497 237727 ns/op 86.15 MB/s 23673 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc 2479 237260 ns/op 86.32 MB/s 23673 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc 2466 237862 ns/op 86.10 MB/s 23673 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 9816 58760 ns/op 348.54 MB/s 23678 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 9968 58307 ns/op 351.25 MB/s 23677 B/op 325 allocs/op
|
||||
BenchmarkConvert/doc-4 10000 58467 ns/op 350.28 MB/s 23677 B/op 325 allocs/op
|
||||
BenchmarkConvert/docx 565 1054643 ns/op 9.63 MB/s 24857 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx 556 1082361 ns/op 9.39 MB/s 24857 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx 541 1100535 ns/op 9.23 MB/s 24857 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2130 271626 ns/op 37.40 MB/s 24862 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2180 269896 ns/op 37.64 MB/s 24861 B/op 342 allocs/op
|
||||
BenchmarkConvert/docx-4 2109 270594 ns/op 37.54 MB/s 24861 B/op 342 allocs/op
|
||||
BenchmarkConvert/ppt 9344 62726 ns/op 7403.33 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt 9786 62456 ns/op 7435.34 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt 9612 63643 ns/op 7296.73 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 36694 16371 ns/op 28366.29 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 36626 16350 ns/op 28402.93 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/ppt-4 36825 16367 ns/op 28373.05 MB/s 5520 B/op 73 allocs/op
|
||||
BenchmarkConvert/pptx 657 898570 ns/op 17.51 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx 674 895435 ns/op 17.57 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx 678 893230 ns/op 17.61 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2608 227670 ns/op 69.11 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2575 227581 ns/op 69.14 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/pptx-4 2533 227921 ns/op 69.03 MB/s 6224 B/op 85 allocs/op
|
||||
BenchmarkConvert/odt 817 730569 ns/op 34.89 MB/s 24673 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt 822 727546 ns/op 35.03 MB/s 24673 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt 826 726398 ns/op 35.09 MB/s 24673 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3181 183722 ns/op 138.74 MB/s 24677 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3240 183166 ns/op 139.16 MB/s 24677 B/op 348 allocs/op
|
||||
BenchmarkConvert/odt-4 3290 182945 ns/op 139.33 MB/s 24677 B/op 348 allocs/op
|
||||
BenchmarkConvert/ods 1221 483640 ns/op 33.38 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods 1249 483216 ns/op 33.41 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods 1252 482840 ns/op 33.43 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4846 121986 ns/op 132.33 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4741 122610 ns/op 131.65 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/ods-4 4682 122870 ns/op 131.37 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/odp 508 1167552 ns/op 16.33 MB/s 12408 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp 512 1163936 ns/op 16.38 MB/s 12408 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp 516 1172509 ns/op 16.26 MB/s 12408 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 2010 296137 ns/op 64.38 MB/s 12411 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 1977 296402 ns/op 64.32 MB/s 12411 B/op 101 allocs/op
|
||||
BenchmarkConvert/odp-4 2013 297303 ns/op 64.13 MB/s 12410 B/op 101 allocs/op
|
||||
BenchmarkConvert/rtf 781 760683 ns/op 26.47 MB/s 26809 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf 796 757376 ns/op 26.59 MB/s 26809 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf 792 757098 ns/op 26.60 MB/s 26809 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3044 192481 ns/op 104.61 MB/s 26815 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3078 192209 ns/op 104.76 MB/s 26816 B/op 383 allocs/op
|
||||
BenchmarkConvert/rtf-4 3087 192072 ns/op 104.84 MB/s 26816 B/op 383 allocs/op
|
||||
BenchmarkConvert/epub 1946 314520 ns/op 19.61 MB/s 16064 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub 1914 313805 ns/op 19.65 MB/s 16064 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub 1910 318963 ns/op 19.33 MB/s 16064 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 7438 79126 ns/op 77.94 MB/s 16068 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 7432 78533 ns/op 78.53 MB/s 16068 B/op 216 allocs/op
|
||||
BenchmarkConvert/epub-4 7393 78452 ns/op 78.61 MB/s 16068 B/op 216 allocs/op
|
||||
BenchmarkConvert/xls 4747 126316 ns/op 56.75 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls 4825 127954 ns/op 56.02 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls 4706 127874 ns/op 56.06 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 18931 31484 ns/op 227.67 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 18969 31668 ns/op 226.35 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xls-4 18921 31662 ns/op 226.39 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 1207 500980 ns/op 14.18 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 1207 498708 ns/op 14.25 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx 1209 500326 ns/op 14.20 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 4579 126024 ns/op 56.39 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 4734 125090 ns/op 56.81 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsx-4 4790 124966 ns/op 56.86 MB/s 13216 B/op 208 allocs/op
|
||||
BenchmarkConvert/xlsb 6988 84341 ns/op 18.99 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/xlsb 7204 83794 ns/op 19.12 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/xlsb 6828 84787 ns/op 18.89 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/xlsb-4 27576 21564 ns/op 74.29 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/xlsb-4 27964 21786 ns/op 73.53 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/xlsb-4 27409 21783 ns/op 73.55 MB/s 5792 B/op 84 allocs/op
|
||||
BenchmarkConvert/csv 3846 161235 ns/op 1.30 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv 3718 161213 ns/op 1.30 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv 3720 161295 ns/op 1.30 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15117 39604 ns/op 5.30 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15140 39760 ns/op 5.28 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/csv-4 15150 39692 ns/op 5.29 MB/s 10240 B/op 166 allocs/op
|
||||
BenchmarkConvert/pdf 162 3627058 ns/op 24.83 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 165 3605448 ns/op 24.98 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf 165 3559522 ns/op 25.30 MB/s 2409 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 560 1048032 ns/op 85.93 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 554 1052198 ns/op 85.59 MB/s 2408 B/op 7 allocs/op
|
||||
BenchmarkConvert/pdf-4 576 1039713 ns/op 86.62 MB/s 2408 B/op 7 allocs/op
|
||||
PASS
|
||||
@@ -0,0 +1,12 @@
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc
|
||||
cpu: Apple M4 Pro
|
||||
BenchmarkConvertScaling/xlsx_100_rows 982 606582 ns/op 11.68 MB/s 82624 B/op 1353 allocs/op
|
||||
BenchmarkConvertScaling/xlsx_100_rows 1010 591741 ns/op 11.97 MB/s 82624 B/op 1353 allocs/op
|
||||
BenchmarkConvertScaling/xlsx_100_rows 1010 590599 ns/op 11.99 MB/s 82624 B/op 1353 allocs/op
|
||||
BenchmarkConvertScaling/xlsx_10000_rows 12 46185531 ns/op 2.51 MB/s 7946440 B/op 130053 allocs/op
|
||||
BenchmarkConvertScaling/xlsx_10000_rows 12 46568236 ns/op 2.49 MB/s 7946442 B/op 130053 allocs/op
|
||||
BenchmarkConvertScaling/xlsx_10000_rows 13 45783734 ns/op 2.53 MB/s 7946442 B/op 130053 allocs/op
|
||||
PASS
|
||||
ok github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc 4.609s
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
//! Regenerate `include/anydoc.h` from the crate's C ABI on every build.
|
||||
//! `cbindgen` is a build-dependency, so this runs whenever `cargo build
|
||||
//! -p anydoc-go` runs inside the workspace. The committed header is the
|
||||
//! source of truth for the Go side; consumers link the packaged static archive
|
||||
//! and never run this.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
|
||||
let include_dir = manifest_dir.join("include");
|
||||
let header = include_dir.join("anydoc.h");
|
||||
|
||||
std::fs::create_dir_all(&include_dir).expect("create include directory");
|
||||
|
||||
let config = cbindgen::Config {
|
||||
language: cbindgen::Language::C,
|
||||
include_guard: Some("ANYDOC_H".to_string()),
|
||||
autogen_warning: Some(
|
||||
"/* Warning: this file is generated by `cargo build -p anydoc-go`. Do not edit by hand. */"
|
||||
.to_string(),
|
||||
),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
match cbindgen::Builder::new().with_crate(&manifest_dir).with_config(config).generate() {
|
||||
Ok(bindings) => {
|
||||
let _ = bindings.write_to_file(&header);
|
||||
}
|
||||
Err(err) => {
|
||||
println!("cargo:warning=cbindgen failed to generate anydoc.h: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
println!("cargo:rerun-if-changed=src/lib.rs");
|
||||
println!("cargo:rerun-if-changed=src/model.rs");
|
||||
println!("cargo:rerun-if-changed=src/asset_links.rs");
|
||||
}
|
||||
Vendored
+117
@@ -0,0 +1,117 @@
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
#include "include/anydoc.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Error codes mirroring the C ABI (ERR_*). Stable across versions.
|
||||
const (
|
||||
errNeedsOCR = C.ERR_NEEDS_OCR
|
||||
errOK = C.ERR_OK
|
||||
errUnsupported = C.ERR_UNSUPPORTED
|
||||
errMalformed = C.ERR_MALFORMED
|
||||
errEncrypted = C.ERR_ENCRYPTED
|
||||
errResourceLimit = C.ERR_RESOURCE_LIMIT
|
||||
errMissingPart = C.ERR_MISSING_PART
|
||||
errIO = C.ERR_IO
|
||||
errPDFNoModel = C.ERR_PDF_NO_MODEL
|
||||
errInvalidArg = C.ERR_INVALID_ARG
|
||||
errUnknownFormat = C.ERR_UNKNOWN_FORMAT
|
||||
)
|
||||
|
||||
// ConvertError is the typed error every conversion function returns. It
|
||||
// carries the same variant kind the Node and Python bindings expose
|
||||
// ("unsupported", "malformed", "encrypted", ...) plus the crate's
|
||||
// human-readable detail.
|
||||
type ConvertError struct {
|
||||
// Kind is the snake-case variant name: "unsupported", "malformed", "encrypted", "resource_limit",
|
||||
// "missing_part", "io", "pdf_no_model", "needs_ocr". Go also reports
|
||||
// "unknown_format" for an invalid explicit Format.
|
||||
Kind string
|
||||
// Detail is the crate's Display output for the error.
|
||||
Detail string
|
||||
}
|
||||
|
||||
// NeedsOCR reports the typed scanned-page failure without matching error text.
|
||||
func (e *ConvertError) NeedsOCR() bool {
|
||||
return e != nil && e.Kind == "needs_ocr"
|
||||
}
|
||||
|
||||
// ResourceLimit reports a permanent parser budget rejection. Retrying the
|
||||
// same document cannot succeed.
|
||||
func (e *ConvertError) ResourceLimit() bool {
|
||||
return e != nil && e.Kind == "resource_limit"
|
||||
}
|
||||
|
||||
// LimitDetail is the converter's description of the exceeded budget, for
|
||||
// example "resource limit exceeded (max_xml_nodes): part exceeds 2000000 xml nodes".
|
||||
func (e *ConvertError) LimitDetail() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.Detail
|
||||
}
|
||||
|
||||
func (e *ConvertError) Error() string {
|
||||
if e == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
if e.Detail == "" {
|
||||
return "anydoc: " + e.Kind
|
||||
}
|
||||
return "anydoc: " + e.Kind + ": " + e.Detail
|
||||
}
|
||||
|
||||
// convertError builds a ConvertError from an ABI error code, pulling the
|
||||
// detail message from the thread-local last-error slot.
|
||||
func convertError(code C.int) error {
|
||||
if code == errOK {
|
||||
return nil
|
||||
}
|
||||
kind := errorKind(code)
|
||||
detail := lastError()
|
||||
return &ConvertError{Kind: kind, Detail: detail}
|
||||
}
|
||||
|
||||
func errorKind(code C.int) string {
|
||||
switch code {
|
||||
case errNeedsOCR:
|
||||
return "needs_ocr"
|
||||
case errUnsupported:
|
||||
return "unsupported"
|
||||
case errMalformed:
|
||||
return "malformed"
|
||||
case errEncrypted:
|
||||
return "encrypted"
|
||||
case errResourceLimit:
|
||||
return "resource_limit"
|
||||
case errMissingPart:
|
||||
return "missing_part"
|
||||
case errIO:
|
||||
return "io"
|
||||
case errPDFNoModel:
|
||||
return "pdf_no_model"
|
||||
case errInvalidArg:
|
||||
return "invalid_argument"
|
||||
case errUnknownFormat:
|
||||
return "unknown_format"
|
||||
default:
|
||||
return fmt.Sprintf("unknown_error(%d)", int(code))
|
||||
}
|
||||
}
|
||||
|
||||
// lastError returns the human-readable message the last ABI call stashed on
|
||||
// the current OS thread, or "" when there was none. The C side returns a
|
||||
// freshly allocated string the caller must free.
|
||||
func lastError() string {
|
||||
ptr := C.anydoc_last_error()
|
||||
if ptr == nil {
|
||||
return ""
|
||||
}
|
||||
defer C.anydoc_string_free(ptr)
|
||||
return cString(ptr)
|
||||
}
|
||||
Vendored
+143
@@ -0,0 +1,143 @@
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#include "include/anydoc.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Format names a document format. The string values are the lowercase
|
||||
// extension names, matching the Node and Python bindings ("doc", "docx",
|
||||
// "pdf", ...). Use the exported constants rather than raw strings where
|
||||
// possible: the constants are pinned to the C ABI's tag values at build
|
||||
// time.
|
||||
type Format string
|
||||
|
||||
// Format constants. Every value is stable across versions.
|
||||
const (
|
||||
FormatDoc Format = "doc"
|
||||
FormatDocx Format = "docx"
|
||||
FormatOdt Format = "odt"
|
||||
FormatPdf Format = "pdf"
|
||||
FormatPpt Format = "ppt"
|
||||
FormatPptx Format = "pptx"
|
||||
FormatRtf Format = "rtf"
|
||||
FormatEpub Format = "epub"
|
||||
FormatXlsx Format = "xlsx"
|
||||
FormatOds Format = "ods"
|
||||
FormatOdp Format = "odp"
|
||||
FormatCsv Format = "csv"
|
||||
)
|
||||
|
||||
// formatToTag maps a Format to its C ABI tag. Returns ANYDOC_FORMAT_NONE for
|
||||
// an unknown format.
|
||||
func formatToTag(f Format) C.int {
|
||||
switch f {
|
||||
case FormatDoc:
|
||||
return C.int(C.ANYDOC_FORMAT_DOC)
|
||||
case FormatDocx:
|
||||
return C.int(C.ANYDOC_FORMAT_DOCX)
|
||||
case FormatOdt:
|
||||
return C.int(C.ANYDOC_FORMAT_ODT)
|
||||
case FormatPdf:
|
||||
return C.int(C.ANYDOC_FORMAT_PDF)
|
||||
case FormatPpt:
|
||||
return C.int(C.ANYDOC_FORMAT_PPT)
|
||||
case FormatPptx:
|
||||
return C.int(C.ANYDOC_FORMAT_PPTX)
|
||||
case FormatRtf:
|
||||
return C.int(C.ANYDOC_FORMAT_RTF)
|
||||
case FormatEpub:
|
||||
return C.int(C.ANYDOC_FORMAT_EPUB)
|
||||
case FormatXlsx:
|
||||
return C.int(C.ANYDOC_FORMAT_XLSX)
|
||||
case FormatOds:
|
||||
return C.int(C.ANYDOC_FORMAT_ODS)
|
||||
case FormatOdp:
|
||||
return C.int(C.ANYDOC_FORMAT_ODP)
|
||||
case FormatCsv:
|
||||
return C.int(C.ANYDOC_FORMAT_CSV)
|
||||
}
|
||||
return C.int(C.ANYDOC_FORMAT_NONE)
|
||||
}
|
||||
|
||||
// tagToFormat maps a C ABI tag back to a Format. ok is false for NONE or an
|
||||
// unknown tag.
|
||||
func tagToFormat(tag C.int) (Format, bool) {
|
||||
switch tag {
|
||||
case C.ANYDOC_FORMAT_DOC:
|
||||
return FormatDoc, true
|
||||
case C.ANYDOC_FORMAT_DOCX:
|
||||
return FormatDocx, true
|
||||
case C.ANYDOC_FORMAT_ODT:
|
||||
return FormatOdt, true
|
||||
case C.ANYDOC_FORMAT_PDF:
|
||||
return FormatPdf, true
|
||||
case C.ANYDOC_FORMAT_PPT:
|
||||
return FormatPpt, true
|
||||
case C.ANYDOC_FORMAT_PPTX:
|
||||
return FormatPptx, true
|
||||
case C.ANYDOC_FORMAT_RTF:
|
||||
return FormatRtf, true
|
||||
case C.ANYDOC_FORMAT_EPUB:
|
||||
return FormatEpub, true
|
||||
case C.ANYDOC_FORMAT_XLSX:
|
||||
return FormatXlsx, true
|
||||
case C.ANYDOC_FORMAT_ODS:
|
||||
return FormatOds, true
|
||||
case C.ANYDOC_FORMAT_ODP:
|
||||
return FormatOdp, true
|
||||
case C.ANYDOC_FORMAT_CSV:
|
||||
return FormatCsv, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// FormatFromBytes detects the format from the content itself: the signature
|
||||
// and identity each container specification designates (PDF header, RTF open
|
||||
// group, OLE stream names, ZIP package mimetype/content types). Plain-text
|
||||
// formats (CSV) carry no signature and return the zero Format and ok=false;
|
||||
// so does anything unrecognized.
|
||||
func FormatFromBytes(data []byte) (f Format, ok bool) {
|
||||
if len(data) == 0 {
|
||||
return "", false
|
||||
}
|
||||
var tag C.int
|
||||
code := C.anydoc_format_from_bytes((*C.uint8_t)(unsafe.Pointer(&data[0])), C.uintptr_t(len(data)), &tag)
|
||||
if code != errOK {
|
||||
return "", false
|
||||
}
|
||||
return tagToFormat(tag)
|
||||
}
|
||||
|
||||
// FormatFromExtension returns the format a bare extension names (with or
|
||||
// without a leading dot), matched case-insensitively. ok is false for
|
||||
// anything unrecognized.
|
||||
func FormatFromExtension(ext string) (f Format, ok bool) {
|
||||
ext = strings.TrimPrefix(ext, ".")
|
||||
cext := C.CString(strings.ToLower(ext))
|
||||
defer C.free(unsafe.Pointer(cext))
|
||||
var tag C.int
|
||||
code := C.anydoc_format_from_extension(cext, &tag)
|
||||
if code != errOK {
|
||||
return "", false
|
||||
}
|
||||
return tagToFormat(tag)
|
||||
}
|
||||
|
||||
// FormatFromPath returns the format a path's extension names. ok is false
|
||||
// when the path has no extension or names nothing recognized.
|
||||
func FormatFromPath(path string) (f Format, ok bool) {
|
||||
cpath := C.CString(path)
|
||||
defer C.free(unsafe.Pointer(cpath))
|
||||
var tag C.int
|
||||
code := C.anydoc_format_from_path(cpath, &tag)
|
||||
if code != errOK {
|
||||
return "", false
|
||||
}
|
||||
return tagToFormat(tag)
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
module github.com/firecrawl/anydoc/go
|
||||
|
||||
go 1.22
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
#ifndef ANYDOC_H
|
||||
#define ANYDOC_H
|
||||
|
||||
/* Warning: this file is generated by `cargo build -p anydoc-go`. Do not edit by hand. */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* Success.
|
||||
*/
|
||||
#define ERR_OK 0
|
||||
|
||||
/**
|
||||
* `ConvertError::Unsupported`.
|
||||
*/
|
||||
#define ERR_UNSUPPORTED 1
|
||||
|
||||
/**
|
||||
* `ConvertError::Malformed`.
|
||||
*/
|
||||
#define ERR_MALFORMED 2
|
||||
|
||||
/**
|
||||
* `ConvertError::Encrypted`.
|
||||
*/
|
||||
#define ERR_ENCRYPTED 3
|
||||
|
||||
/**
|
||||
* `ConvertError::ResourceLimit`.
|
||||
*/
|
||||
#define ERR_RESOURCE_LIMIT 4
|
||||
|
||||
/**
|
||||
* `ConvertError::MissingPart`.
|
||||
*/
|
||||
#define ERR_MISSING_PART 5
|
||||
|
||||
/**
|
||||
* `ConvertError::Io`.
|
||||
*/
|
||||
#define ERR_IO 6
|
||||
|
||||
/**
|
||||
* PDF was passed to `anydoc_to_document` (unsupported there).
|
||||
*/
|
||||
#define ERR_PDF_NO_MODEL 7
|
||||
|
||||
/**
|
||||
* Null pointer or invalid argument passed to the ABI.
|
||||
*/
|
||||
#define ERR_INVALID_ARG 8
|
||||
|
||||
/**
|
||||
* Unknown format name passed to the ABI.
|
||||
*/
|
||||
#define ERR_UNKNOWN_FORMAT 9
|
||||
|
||||
/**
|
||||
* `ConvertError::NeedsOcr`; page numbers are retained in the error detail.
|
||||
*/
|
||||
#define ERR_NEEDS_OCR 10
|
||||
|
||||
/**
|
||||
* C-side format tag. Stable; mirrors the Node/Python lowercase string names
|
||||
* via `format_name`. `ANYDOC_FORMAT_NONE` is the `Option::None` sentinel.
|
||||
*/
|
||||
#define ANYDOC_FORMAT_NONE -1
|
||||
|
||||
#define ANYDOC_FORMAT_DOC 0
|
||||
|
||||
#define ANYDOC_FORMAT_DOCX 1
|
||||
|
||||
#define ANYDOC_FORMAT_ODT 2
|
||||
|
||||
#define ANYDOC_FORMAT_PDF 3
|
||||
|
||||
#define ANYDOC_FORMAT_PPT 4
|
||||
|
||||
#define ANYDOC_FORMAT_PPTX 5
|
||||
|
||||
#define ANYDOC_FORMAT_RTF 6
|
||||
|
||||
#define ANYDOC_FORMAT_EPUB 7
|
||||
|
||||
#define ANYDOC_FORMAT_XLSX 8
|
||||
|
||||
#define ANYDOC_FORMAT_ODS 9
|
||||
|
||||
#define ANYDOC_FORMAT_ODP 10
|
||||
|
||||
#define ANYDOC_FORMAT_CSV 11
|
||||
|
||||
/**
|
||||
* Tag values for `Block` variants. Stable across versions; the Go side maps
|
||||
* these to the same lowercase string names the Node and Python bindings use.
|
||||
*/
|
||||
#define BLOCK_HEADING 0
|
||||
|
||||
#define BLOCK_PARAGRAPH 1
|
||||
|
||||
#define BLOCK_LIST 2
|
||||
|
||||
#define BLOCK_TABLE 3
|
||||
|
||||
#define BLOCK_QUOTE 4
|
||||
|
||||
#define BLOCK_CODE 5
|
||||
|
||||
#define BLOCK_RULE 6
|
||||
|
||||
#define BLOCK_MATH 7
|
||||
|
||||
#define INLINE_TEXT 0
|
||||
|
||||
#define INLINE_LINK 1
|
||||
|
||||
#define INLINE_IMAGE 2
|
||||
|
||||
#define INLINE_ANCHOR 3
|
||||
|
||||
#define INLINE_NOTEREF 4
|
||||
|
||||
#define INLINE_LINEBREAK 5
|
||||
|
||||
#define INLINE_MATH 6
|
||||
|
||||
#define INLINE_CHECKBOX 7
|
||||
|
||||
#define LINK_EXTERNAL 0
|
||||
|
||||
#define LINK_RELATIVE 1
|
||||
|
||||
#define LINK_ANCHOR 2
|
||||
|
||||
#define IMG_EXTERNAL 0
|
||||
|
||||
#define IMG_ASSET 1
|
||||
|
||||
#define IMG_UNAVAILABLE 2
|
||||
|
||||
#define MARKER_BULLET 0
|
||||
|
||||
#define MARKER_DECIMAL 1
|
||||
|
||||
#define MARKER_LOWER_ALPHA 2
|
||||
|
||||
#define MARKER_UPPER_ALPHA 3
|
||||
|
||||
#define MARKER_LOWER_ROMAN 4
|
||||
|
||||
#define MARKER_UPPER_ROMAN 5
|
||||
|
||||
#define TABLE_DATA 0
|
||||
|
||||
#define TABLE_LAYOUT 1
|
||||
|
||||
#define SLOT_ORIGIN 0
|
||||
|
||||
#define SLOT_COVERED 1
|
||||
|
||||
#define NOTE_FOOTNOTE 0
|
||||
|
||||
#define NOTE_ENDNOTE 1
|
||||
|
||||
/**
|
||||
* Detect the format from the content. Writes a format tag to `*out` (one of
|
||||
* `ANYDOC_FORMAT_*`), or `ANYDOC_FORMAT_NONE` when nothing matches.
|
||||
*
|
||||
* Returns `ERR_OK` on success (including no-match), `ERR_INVALID_ARG` on null
|
||||
* pointers.
|
||||
*/
|
||||
int anydoc_format_from_bytes(const uint8_t *bytes, uintptr_t len, int *out);
|
||||
|
||||
/**
|
||||
* The format a bare extension names (no leading dot). Writes a format tag to
|
||||
* `*out`, or `ANYDOC_FORMAT_NONE` when unrecognized.
|
||||
*/
|
||||
int anydoc_format_from_extension(const char *ext, int *out);
|
||||
|
||||
/**
|
||||
* The format a path's extension names. Writes a format tag to `*out`, or
|
||||
* `ANYDOC_FORMAT_NONE` when unrecognized.
|
||||
*/
|
||||
int anydoc_format_from_path(const char *path, int *out);
|
||||
|
||||
/**
|
||||
* Convert a document file to Markdown. The format is detected from the file
|
||||
* content, with the extension as the fallback for signature-less formats
|
||||
* (CSV).
|
||||
*
|
||||
* On success writes a `*mut c_char` to `*out_str` and its byte length to
|
||||
* `*out_len`. The caller must free the string with `anydoc_string_free`.
|
||||
*/
|
||||
int anydoc_to_markdown(const char *path, char **out_str, uintptr_t *out_len);
|
||||
|
||||
/**
|
||||
* Convert an in-memory document to Markdown. `format_tag` is one of the
|
||||
* `ANYDOC_FORMAT_*` constants, or `ANYDOC_FORMAT_NONE` to detect from the
|
||||
* content.
|
||||
*/
|
||||
int anydoc_to_markdown_bytes(const uint8_t *bytes,
|
||||
uintptr_t len,
|
||||
int format_tag,
|
||||
char **out_str,
|
||||
uintptr_t *out_len);
|
||||
|
||||
/**
|
||||
* Convert an in-memory document to Markdown with embedded images rewritten
|
||||
* as `` so they keep their original positions.
|
||||
*
|
||||
* This is `to_document` + anydoc's official GFM serializer. Asset images are
|
||||
* first turned into `ImageSource::External` URLs; the serializer already
|
||||
* emits those as ordinary Markdown image links. PDF has no document model
|
||||
* and is converted the same way as `anydoc_to_markdown_bytes`.
|
||||
*/
|
||||
int anydoc_to_markdown_with_asset_links(const uint8_t *bytes,
|
||||
uintptr_t len,
|
||||
int format_tag,
|
||||
char **out_str,
|
||||
uintptr_t *out_len);
|
||||
|
||||
/**
|
||||
* Parse an in-memory document into the document model. `format_tag` is one of
|
||||
* the `ANYDOC_FORMAT_*` constants, or `ANYDOC_FORMAT_NONE` to detect from the
|
||||
* content.
|
||||
*
|
||||
* On success writes a heap-allocated byte buffer to `*out_buf` and its length
|
||||
* to `*out_len`. The buffer holds the flat serialization (see `model`). The
|
||||
* caller must free it with `anydoc_buffer_free`.
|
||||
*
|
||||
* Returns `ERR_PDF_NO_MODEL` for PDF input: PDF conversion produces Markdown
|
||||
* directly and has no document-model form. Use `anydoc_to_markdown_bytes`.
|
||||
*/
|
||||
int anydoc_to_document(const uint8_t *bytes,
|
||||
uintptr_t len,
|
||||
int format_tag,
|
||||
uint8_t **out_buf,
|
||||
uintptr_t *out_len);
|
||||
|
||||
/**
|
||||
* Parse once and return the original document model followed by asset-linked
|
||||
* Markdown in a single flat buffer. Free it with `anydoc_buffer_free`.
|
||||
* PDF has no document model and returns `ERR_PDF_NO_MODEL`.
|
||||
*/
|
||||
int anydoc_to_document_with_asset_links(const uint8_t *bytes,
|
||||
uintptr_t len,
|
||||
int format_tag,
|
||||
uint8_t **out_buf,
|
||||
uintptr_t *out_len);
|
||||
|
||||
/**
|
||||
* Free a string returned by `anydoc_to_markdown*` or `anydoc_last_error`.
|
||||
* Null is a no-op (the empty-string sentinel).
|
||||
*/
|
||||
void anydoc_string_free(char *s);
|
||||
|
||||
/**
|
||||
* Free a document buffer returned by `anydoc_to_document`. Null is a no-op.
|
||||
* `len` must be the length written to `*out_len` at allocation time; it is
|
||||
* validated against the header for safety.
|
||||
*/
|
||||
void anydoc_buffer_free(uint8_t *buf, uintptr_t len);
|
||||
|
||||
/**
|
||||
* Return the human-readable message for the last error on the current
|
||||
* thread. The returned `*mut c_char` is freshly allocated and must be freed
|
||||
* with `anydoc_string_free`. Returns null when there is no last error.
|
||||
*/
|
||||
char *anydoc_last_error(void);
|
||||
|
||||
/**
|
||||
* Return the lowercase name of a format tag, or null for an unknown tag.
|
||||
* The returned string must be freed with `anydoc_string_free`.
|
||||
*/
|
||||
char *anydoc_format_name(int tag);
|
||||
|
||||
/**
|
||||
* Parse a lowercase format name into a tag. Writes the tag to `*out`, or
|
||||
* `ANYDOC_FORMAT_NONE` when the name is unknown.
|
||||
*/
|
||||
int anydoc_format_from_name(const char *name, int *out);
|
||||
|
||||
#endif /* ANYDOC_H */
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
//go:build linux && !musl
|
||||
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib/linux_amd64_gnu -lanydoc_go -lm -lstdc++ -ldl -lpthread
|
||||
#cgo arm64 LDFLAGS: -L${SRCDIR}/lib/linux_arm64_gnu -lanydoc_go -lm -lstdc++ -ldl -lpthread
|
||||
*/
|
||||
import "C"
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
//go:build linux && musl
|
||||
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib/linux_amd64_musl -lanydoc_go -lm -lstdc++ -ldl -lpthread
|
||||
#cgo arm64 LDFLAGS: -L${SRCDIR}/lib/linux_arm64_musl -lanydoc_go -lm -lstdc++ -ldl -lpthread
|
||||
*/
|
||||
import "C"
|
||||
Vendored
+752
@@ -0,0 +1,752 @@
|
||||
package anydoc
|
||||
|
||||
/*
|
||||
#include "include/anydoc.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Document is a parsed document: its body, its notes, and the bytes of
|
||||
// everything it embedded. A Document is self-contained — embedded assets
|
||||
// carry their bytes, so it stays usable after the source archive is gone.
|
||||
type Document struct {
|
||||
Blocks []Block
|
||||
// Notes are footnote and endnote bodies, referenced from text by an
|
||||
// Inline with Kind == "note_ref".
|
||||
Notes []Note
|
||||
// Assets holds every embedded asset, indexed by Asset.ID as referenced
|
||||
// by an Inline image source.
|
||||
Assets []Asset
|
||||
}
|
||||
|
||||
// Block is one block-level piece of a document body. The Kind field selects
|
||||
// which of the optional fields is populated; the rest are nil.
|
||||
type Block struct {
|
||||
// "heading", "paragraph", "list", "table", "block_quote", "code_block",
|
||||
// "rule", or "math".
|
||||
Kind string
|
||||
Level *uint8 // heading: 1-based outline depth
|
||||
Anchor *string // heading: stable anchor id when the document targets it
|
||||
Content []Inline // heading, paragraph
|
||||
List *List // list
|
||||
Table *Table // table
|
||||
Blocks []Block // block_quote
|
||||
Lang *string // code_block
|
||||
Text *string // code_block, math
|
||||
}
|
||||
|
||||
// Inline is one span of inline content. The Kind field selects which optional
|
||||
// fields are populated.
|
||||
type Inline struct {
|
||||
// "text", "link", "image", "anchor", "note_ref", "line_break", "math", or "checkbox".
|
||||
Kind string
|
||||
Text *string // text, math
|
||||
Style *Style // text
|
||||
Content []Inline // link
|
||||
Target *LinkTarget // link
|
||||
Alt *string // image
|
||||
Source *ImageSource // image
|
||||
Anchor *string // anchor: the anchor id
|
||||
NoteID *string // note_ref: the id of the note in Document.Notes
|
||||
Checked *bool // checkbox
|
||||
}
|
||||
|
||||
// Style is a fully resolved character style.
|
||||
type Style struct {
|
||||
Bold bool
|
||||
Italic bool
|
||||
Strike bool
|
||||
Code bool
|
||||
}
|
||||
|
||||
// LinkTarget is where a link points.
|
||||
type LinkTarget struct {
|
||||
// "external" (absolute URL with a scheme), "relative" (scheme-less
|
||||
// relative reference, preserved as written), or "anchor" (internal
|
||||
// target: a heading anchor or an "anchor" inline).
|
||||
Kind string
|
||||
Value string // the URL, relative reference, or anchor id
|
||||
}
|
||||
|
||||
// ImageSource is where an image's bytes live.
|
||||
type ImageSource struct {
|
||||
// "external" (absolute URL), "asset" (embedded image, carried in
|
||||
// Document.Assets), or "unavailable" (no usable source; only the alt
|
||||
// text remains).
|
||||
Kind string
|
||||
URL *string // external
|
||||
AssetID *uint64 // asset: index into Document.Assets
|
||||
}
|
||||
|
||||
// List is a fully resolved list: numbering identity and marker resolution
|
||||
// happen in the parser frontends.
|
||||
type List struct {
|
||||
// "bullet", "decimal", "lower_alpha", "upper_alpha", "lower_roman", or
|
||||
// "upper_roman".
|
||||
Marker string
|
||||
Start uint64
|
||||
Items []ListItem
|
||||
}
|
||||
|
||||
// ListItem is one item of a List, which may hold nested blocks including
|
||||
// further lists.
|
||||
type ListItem struct {
|
||||
Blocks []Block
|
||||
Checked *bool // Deprecated: always nil since 0.2.3; read Inline.Checked.
|
||||
MarkerLabel *string // literal marker text overriding the level marker
|
||||
}
|
||||
|
||||
// Table is a canonical table grid: every logical grid position appears
|
||||
// exactly once. Content and spans live on the origin slot, and each position
|
||||
// a span covers holds a CellSlot with Kind == "covered" pointing back at that
|
||||
// origin.
|
||||
type Table struct {
|
||||
Grid [][]CellSlot
|
||||
HeaderRows uint32 // number of leading rows that are header rows (0 = none)
|
||||
Kind string // "data" or "layout"
|
||||
}
|
||||
|
||||
// CellSlot is one position in a Table grid: either a cell or the shadow of
|
||||
// one.
|
||||
type CellSlot struct {
|
||||
// "origin" (the cell itself) or "covered" (a position swallowed by a
|
||||
// span, pointing back at the origin that covers it).
|
||||
Kind string
|
||||
Cell *Cell // origin
|
||||
OriginRow *uint32 // covered: row of the covering origin
|
||||
OriginCol *uint32 // covered: column of the covering origin
|
||||
}
|
||||
|
||||
// Cell is a table cell and the extent it spans.
|
||||
type Cell struct {
|
||||
Blocks []Block
|
||||
ColSpan uint32
|
||||
RowSpan uint32
|
||||
}
|
||||
|
||||
// Note is a footnote or endnote body, referenced from text by an Inline with
|
||||
// Kind == "note_ref".
|
||||
type Note struct {
|
||||
ID string
|
||||
Kind string // "footnote" or "endnote"
|
||||
Blocks []Block
|
||||
}
|
||||
|
||||
// Asset is an embedded binary asset (image, object payload). Bytes are always
|
||||
// retained, so a document stays self-contained.
|
||||
type Asset struct {
|
||||
ID uint64 // index into Document.Assets, as referenced by an image source
|
||||
MediaType string
|
||||
OriginPart string
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// ---- Decoding the flat FFI buffer -----------------------------------------
|
||||
//
|
||||
// The buffer is the flat, length-prefixed serialization written by
|
||||
// go/src/model.rs. Every field is little-endian. A decoder tracks the read
|
||||
// cursor and reports a short or implausible buffer as an error — the Rust side
|
||||
// always produces a well-formed buffer, so a short read is a bug or a version
|
||||
// skew, not user input, and it must not take the process down.
|
||||
|
||||
type decoder struct {
|
||||
buf []byte
|
||||
pos int
|
||||
}
|
||||
|
||||
func decodeDocument(raw []byte) (*Document, error) {
|
||||
d := &decoder{buf: raw}
|
||||
return d.document()
|
||||
}
|
||||
|
||||
func (d *decoder) document() (*Document, error) {
|
||||
doc := &Document{}
|
||||
var err error
|
||||
if doc.Blocks, err = d.blocks(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if doc.Notes, err = d.notes(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if doc.Assets, err = d.assets(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// capFor bounds a preallocation by what is left in the buffer. Every element
|
||||
// costs at least one byte to encode, so a count larger than the remaining
|
||||
// bytes can only come from a corrupt buffer or a Rust/Go version skew — and
|
||||
// preallocating it would exhaust memory before the first bounds check runs.
|
||||
func (d *decoder) capFor(n uint32) int {
|
||||
remaining := len(d.buf) - d.pos
|
||||
if remaining < 0 {
|
||||
return 0
|
||||
}
|
||||
if uint64(n) > uint64(remaining) {
|
||||
return remaining
|
||||
}
|
||||
return int(n)
|
||||
}
|
||||
|
||||
func (d *decoder) need(n int) error {
|
||||
// A negative n means a length prefix overflowed int (32-bit builds); it is
|
||||
// as unreadable as a truncated buffer.
|
||||
if n < 0 || d.pos+n > len(d.buf) {
|
||||
return fmt.Errorf("anydoc: truncated document buffer at offset %d (need %d bytes, have %d)", d.pos, n, len(d.buf)-d.pos)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *decoder) u8() (uint8, error) {
|
||||
if err := d.need(1); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v := d.buf[d.pos]
|
||||
d.pos++
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (d *decoder) bool() (bool, error) {
|
||||
v, err := d.u8()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return v != 0, nil
|
||||
}
|
||||
|
||||
func (d *decoder) u32() (uint32, error) {
|
||||
if err := d.need(4); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v := uint32(d.buf[d.pos]) | uint32(d.buf[d.pos+1])<<8 | uint32(d.buf[d.pos+2])<<16 | uint32(d.buf[d.pos+3])<<24
|
||||
d.pos += 4
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (d *decoder) u64() (uint64, error) {
|
||||
if err := d.need(8); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v := uint64(d.buf[d.pos]) | uint64(d.buf[d.pos+1])<<8 | uint64(d.buf[d.pos+2])<<16 | uint64(d.buf[d.pos+3])<<24 |
|
||||
uint64(d.buf[d.pos+4])<<32 | uint64(d.buf[d.pos+5])<<40 | uint64(d.buf[d.pos+6])<<48 | uint64(d.buf[d.pos+7])<<56
|
||||
d.pos += 8
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (d *decoder) i32() (int32, error) {
|
||||
u, err := d.u32()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(u), nil
|
||||
}
|
||||
|
||||
// optStr reads a length-prefixed string or a -1 sentinel (None).
|
||||
func (d *decoder) optStr() (*string, error) {
|
||||
length, err := d.i32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if length < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
s, err := d.strN(int(length))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func (d *decoder) str() (string, error) {
|
||||
length, err := d.u32()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return d.strN(int(length))
|
||||
}
|
||||
|
||||
func (d *decoder) strN(n int) (string, error) {
|
||||
if err := d.need(n); err != nil {
|
||||
return "", err
|
||||
}
|
||||
s := string(d.buf[d.pos : d.pos+n])
|
||||
d.pos += n
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (d *decoder) bytes() ([]byte, error) {
|
||||
length, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := d.need(int(length)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b := make([]byte, length)
|
||||
copy(b, d.buf[d.pos:d.pos+int(length)])
|
||||
d.pos += int(length)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (d *decoder) blocks() ([]Block, error) {
|
||||
n, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Block, 0, d.capFor(n))
|
||||
for i := uint32(0); i < n; i++ {
|
||||
b, err := d.block()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (d *decoder) block() (Block, error) {
|
||||
tag, err := d.i32()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
switch tag {
|
||||
case C.BLOCK_HEADING:
|
||||
level, err := d.u8()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
anchor, err := d.optStr()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
content, err := d.inlines()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
lv := level
|
||||
return Block{Kind: "heading", Level: &lv, Anchor: anchor, Content: content}, nil
|
||||
case C.BLOCK_PARAGRAPH:
|
||||
content, err := d.inlines()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "paragraph", Content: content}, nil
|
||||
case C.BLOCK_LIST:
|
||||
list, err := d.list()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "list", List: list}, nil
|
||||
case C.BLOCK_TABLE:
|
||||
table, err := d.table()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "table", Table: table}, nil
|
||||
case C.BLOCK_QUOTE:
|
||||
inner, err := d.blocks()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "block_quote", Blocks: inner}, nil
|
||||
case C.BLOCK_CODE:
|
||||
lang, err := d.optStr()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
text, err := d.str()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "code_block", Lang: lang, Text: &text}, nil
|
||||
case C.BLOCK_MATH:
|
||||
text, err := d.str()
|
||||
if err != nil {
|
||||
return Block{}, err
|
||||
}
|
||||
return Block{Kind: "math", Text: &text}, nil
|
||||
case C.BLOCK_RULE:
|
||||
return Block{Kind: "rule"}, nil
|
||||
default:
|
||||
return Block{}, fmt.Errorf("anydoc: unknown block kind tag %d", tag)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) inlines() ([]Inline, error) {
|
||||
n, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Inline, 0, d.capFor(n))
|
||||
for i := uint32(0); i < n; i++ {
|
||||
in, err := d.inline()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, in)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (d *decoder) inline() (Inline, error) {
|
||||
tag, err := d.i32()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
switch tag {
|
||||
case C.INLINE_TEXT:
|
||||
text, err := d.str()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
style, err := d.style()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "text", Text: &text, Style: &style}, nil
|
||||
case C.INLINE_LINK:
|
||||
content, err := d.inlines()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
target, err := d.linkTarget()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "link", Content: content, Target: &target}, nil
|
||||
case C.INLINE_IMAGE:
|
||||
alt, err := d.str()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
src, err := d.imageSource()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "image", Alt: &alt, Source: &src}, nil
|
||||
case C.INLINE_ANCHOR:
|
||||
id, err := d.str()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "anchor", Anchor: &id}, nil
|
||||
case C.INLINE_NOTEREF:
|
||||
id, err := d.str()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "note_ref", NoteID: &id}, nil
|
||||
case C.INLINE_MATH:
|
||||
text, err := d.str()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "math", Text: &text}, nil
|
||||
case C.INLINE_CHECKBOX:
|
||||
checked, err := d.bool()
|
||||
if err != nil {
|
||||
return Inline{}, err
|
||||
}
|
||||
return Inline{Kind: "checkbox", Checked: &checked}, nil
|
||||
case C.INLINE_LINEBREAK:
|
||||
return Inline{Kind: "line_break"}, nil
|
||||
default:
|
||||
return Inline{}, fmt.Errorf("anydoc: unknown inline kind tag %d", tag)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) style() (Style, error) {
|
||||
bold, err := d.bool()
|
||||
if err != nil {
|
||||
return Style{}, err
|
||||
}
|
||||
italic, err := d.bool()
|
||||
if err != nil {
|
||||
return Style{}, err
|
||||
}
|
||||
strike, err := d.bool()
|
||||
if err != nil {
|
||||
return Style{}, err
|
||||
}
|
||||
code, err := d.bool()
|
||||
if err != nil {
|
||||
return Style{}, err
|
||||
}
|
||||
return Style{Bold: bold, Italic: italic, Strike: strike, Code: code}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) linkTarget() (LinkTarget, error) {
|
||||
tag, err := d.i32()
|
||||
if err != nil {
|
||||
return LinkTarget{}, err
|
||||
}
|
||||
value, err := d.str()
|
||||
if err != nil {
|
||||
return LinkTarget{}, err
|
||||
}
|
||||
switch tag {
|
||||
case C.LINK_EXTERNAL:
|
||||
return LinkTarget{Kind: "external", Value: value}, nil
|
||||
case C.LINK_RELATIVE:
|
||||
return LinkTarget{Kind: "relative", Value: value}, nil
|
||||
case C.LINK_ANCHOR:
|
||||
return LinkTarget{Kind: "anchor", Value: value}, nil
|
||||
default:
|
||||
return LinkTarget{}, fmt.Errorf("anydoc: unknown link target tag %d", tag)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) imageSource() (ImageSource, error) {
|
||||
tag, err := d.i32()
|
||||
if err != nil {
|
||||
return ImageSource{}, err
|
||||
}
|
||||
switch tag {
|
||||
case C.IMG_EXTERNAL:
|
||||
url, err := d.str()
|
||||
if err != nil {
|
||||
return ImageSource{}, err
|
||||
}
|
||||
return ImageSource{Kind: "external", URL: &url}, nil
|
||||
case C.IMG_ASSET:
|
||||
id, err := d.u64()
|
||||
if err != nil {
|
||||
return ImageSource{}, err
|
||||
}
|
||||
return ImageSource{Kind: "asset", AssetID: &id}, nil
|
||||
case C.IMG_UNAVAILABLE:
|
||||
return ImageSource{Kind: "unavailable"}, nil
|
||||
default:
|
||||
return ImageSource{}, fmt.Errorf("anydoc: unknown image source tag %d", tag)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) list() (*List, error) {
|
||||
markerTag, err := d.i32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start, err := d.u64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]ListItem, 0, d.capFor(n))
|
||||
for i := uint32(0); i < n; i++ {
|
||||
item, err := d.listItem()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
marker, err := markerName(markerTag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &List{Marker: marker, Start: start, Items: items}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) listItem() (ListItem, error) {
|
||||
blocks, err := d.blocks()
|
||||
if err != nil {
|
||||
return ListItem{}, err
|
||||
}
|
||||
checkedTag, err := d.i32()
|
||||
if err != nil {
|
||||
return ListItem{}, err
|
||||
}
|
||||
var checked *bool
|
||||
switch checkedTag {
|
||||
case 0:
|
||||
f := false
|
||||
checked = &f
|
||||
case 1:
|
||||
t := true
|
||||
checked = &t
|
||||
case -1:
|
||||
checked = nil
|
||||
default:
|
||||
return ListItem{}, fmt.Errorf("anydoc: unknown checked tag %d", checkedTag)
|
||||
}
|
||||
label, err := d.optStr()
|
||||
if err != nil {
|
||||
return ListItem{}, err
|
||||
}
|
||||
return ListItem{Blocks: blocks, Checked: checked, MarkerLabel: label}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) table() (*Table, error) {
|
||||
headerRows, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kindTag, err := d.i32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
grid := make([][]CellSlot, 0, d.capFor(rows))
|
||||
for i := uint32(0); i < rows; i++ {
|
||||
cols, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row := make([]CellSlot, 0, d.capFor(cols))
|
||||
for j := uint32(0); j < cols; j++ {
|
||||
slot, err := d.slot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row = append(row, slot)
|
||||
}
|
||||
grid = append(grid, row)
|
||||
}
|
||||
kind := "data"
|
||||
if kindTag == C.TABLE_LAYOUT {
|
||||
kind = "layout"
|
||||
}
|
||||
return &Table{Grid: grid, HeaderRows: headerRows, Kind: kind}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) slot() (CellSlot, error) {
|
||||
tag, err := d.i32()
|
||||
if err != nil {
|
||||
return CellSlot{}, err
|
||||
}
|
||||
switch tag {
|
||||
case C.SLOT_ORIGIN:
|
||||
cell, err := d.cell()
|
||||
if err != nil {
|
||||
return CellSlot{}, err
|
||||
}
|
||||
return CellSlot{Kind: "origin", Cell: &cell}, nil
|
||||
case C.SLOT_COVERED:
|
||||
r, err := d.u32()
|
||||
if err != nil {
|
||||
return CellSlot{}, err
|
||||
}
|
||||
c, err := d.u32()
|
||||
if err != nil {
|
||||
return CellSlot{}, err
|
||||
}
|
||||
rr := r
|
||||
cc := c
|
||||
return CellSlot{Kind: "covered", OriginRow: &rr, OriginCol: &cc}, nil
|
||||
default:
|
||||
return CellSlot{}, fmt.Errorf("anydoc: unknown cell slot tag %d", tag)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) cell() (Cell, error) {
|
||||
colSpan, err := d.u32()
|
||||
if err != nil {
|
||||
return Cell{}, err
|
||||
}
|
||||
rowSpan, err := d.u32()
|
||||
if err != nil {
|
||||
return Cell{}, err
|
||||
}
|
||||
blocks, err := d.blocks()
|
||||
if err != nil {
|
||||
return Cell{}, err
|
||||
}
|
||||
return Cell{Blocks: blocks, ColSpan: colSpan, RowSpan: rowSpan}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) notes() ([]Note, error) {
|
||||
n, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Note, 0, d.capFor(n))
|
||||
for i := uint32(0); i < n; i++ {
|
||||
note, err := d.note()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, note)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (d *decoder) note() (Note, error) {
|
||||
id, err := d.str()
|
||||
if err != nil {
|
||||
return Note{}, err
|
||||
}
|
||||
kindTag, err := d.i32()
|
||||
if err != nil {
|
||||
return Note{}, err
|
||||
}
|
||||
blocks, err := d.blocks()
|
||||
if err != nil {
|
||||
return Note{}, err
|
||||
}
|
||||
kind := "footnote"
|
||||
if kindTag == C.NOTE_ENDNOTE {
|
||||
kind = "endnote"
|
||||
}
|
||||
return Note{ID: id, Kind: kind, Blocks: blocks}, nil
|
||||
}
|
||||
|
||||
func (d *decoder) assets() ([]Asset, error) {
|
||||
n, err := d.u32()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Asset, 0, d.capFor(n))
|
||||
for i := uint32(0); i < n; i++ {
|
||||
asset, err := d.asset()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, asset)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (d *decoder) asset() (Asset, error) {
|
||||
id, err := d.u64()
|
||||
if err != nil {
|
||||
return Asset{}, err
|
||||
}
|
||||
mediaType, err := d.str()
|
||||
if err != nil {
|
||||
return Asset{}, err
|
||||
}
|
||||
originPart, err := d.str()
|
||||
if err != nil {
|
||||
return Asset{}, err
|
||||
}
|
||||
data, err := d.bytes()
|
||||
if err != nil {
|
||||
return Asset{}, err
|
||||
}
|
||||
return Asset{ID: id, MediaType: mediaType, OriginPart: originPart, Data: data}, nil
|
||||
}
|
||||
|
||||
func markerName(tag int32) (string, error) {
|
||||
switch tag {
|
||||
case C.MARKER_BULLET:
|
||||
return "bullet", nil
|
||||
case C.MARKER_DECIMAL:
|
||||
return "decimal", nil
|
||||
case C.MARKER_LOWER_ALPHA:
|
||||
return "lower_alpha", nil
|
||||
case C.MARKER_UPPER_ALPHA:
|
||||
return "upper_alpha", nil
|
||||
case C.MARKER_LOWER_ROMAN:
|
||||
return "lower_roman", nil
|
||||
case C.MARKER_UPPER_ROMAN:
|
||||
return "upper_roman", nil
|
||||
}
|
||||
return "", fmt.Errorf("anydoc: unknown list marker tag %d", tag)
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
//! Rewrite embedded images to markdown-linkable sources so anydoc's official
|
||||
//! GFM serializer will emit `` in place.
|
||||
//!
|
||||
//! `document_to_markdown` drops `ImageSource::Asset` to alt text because
|
||||
//! Markdown cannot carry bytes. `ImageSource::External` is already rendered
|
||||
//! as a normal image link. We keep the official serializer and only change
|
||||
//! how those assets are addressed.
|
||||
|
||||
use anydoc::model::{Block, CellSlot, Document, ImageSource, Inline};
|
||||
|
||||
pub fn rewrite_asset_images(document: &mut Document) {
|
||||
let urls: Vec<Option<String>> = document
|
||||
.assets
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, asset)| {
|
||||
if asset.bytes.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(format!("images/image-{}{}", i + 1, extension_for(&asset.media_type)))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rewrite_blocks(&mut document.blocks, &urls);
|
||||
for note in &mut document.notes {
|
||||
rewrite_blocks(&mut note.blocks, &urls);
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_blocks(blocks: &mut [Block], urls: &[Option<String>]) {
|
||||
for block in blocks {
|
||||
match block {
|
||||
Block::Heading { content, .. } | Block::Paragraph(content) => {
|
||||
rewrite_inlines(content, urls);
|
||||
}
|
||||
Block::List(list) => {
|
||||
for item in &mut list.items {
|
||||
rewrite_blocks(&mut item.blocks, urls);
|
||||
}
|
||||
}
|
||||
Block::Table(table) => {
|
||||
for row in &mut table.grid {
|
||||
for slot in row {
|
||||
if let CellSlot::Origin(cell) = slot {
|
||||
rewrite_blocks(&mut cell.blocks, urls);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Block::BlockQuote(inner) => rewrite_blocks(inner, urls),
|
||||
Block::CodeBlock { .. } | Block::Rule | Block::Math(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_inlines(inlines: &mut [Inline], urls: &[Option<String>]) {
|
||||
for inline in inlines {
|
||||
match inline {
|
||||
Inline::Image { source, .. } => {
|
||||
if let ImageSource::Asset(id) = source {
|
||||
if let Some(Some(url)) = urls.get(id.0) {
|
||||
*source = ImageSource::External(url.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Inline::Link { content, .. } => rewrite_inlines(content, urls),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Markdown file extension for one asset media type.
|
||||
///
|
||||
/// This table is one half of a contract with the Go side. The Markdown link
|
||||
/// written here (`images/image-N<ext>`) is looked up by
|
||||
/// `internal/infrastructure/docparser/image_resolver.go` in a map keyed by the
|
||||
/// `ImageRef.OriginalRef` that `internal/infrastructure/docparser/anydoc/
|
||||
/// backend_cgo.go` builds from the same media type. The two must agree
|
||||
/// character for character or the image is dropped, so `extensionFor` there
|
||||
/// lists exactly the same media types with exactly the same extensions.
|
||||
fn extension_for(media_type: &str) -> &'static str {
|
||||
match media_type.to_ascii_lowercase().as_str() {
|
||||
"image/jpeg" | "image/jpg" => ".jpg",
|
||||
"image/png" => ".png",
|
||||
"image/gif" => ".gif",
|
||||
"image/webp" => ".webp",
|
||||
"image/bmp" => ".bmp",
|
||||
"image/tiff" => ".tiff",
|
||||
"image/svg+xml" => ".svg",
|
||||
// Vector metafiles. `shared::assets::media_type_for` and the OfficeArt
|
||||
// blip decoder both emit these for `.emf`/`.wmf` parts. They used to
|
||||
// fall through to ".bin" here while the Go side asked the platform MIME
|
||||
// registry and got ".emf"/".wmf", so the link never matched the ref and
|
||||
// the image never reached storage.
|
||||
"image/emf" => ".emf",
|
||||
"image/wmf" => ".wmf",
|
||||
_ => ".bin",
|
||||
}
|
||||
}
|
||||
Vendored
+656
@@ -0,0 +1,656 @@
|
||||
//! C ABI surface for the Go bindings.
|
||||
//!
|
||||
//! Memory ownership rule: **Rust allocates, C ABI exposes a `_free` for every
|
||||
//! allocation family, Go releases handles.** There are two allocation
|
||||
//! families:
|
||||
//!
|
||||
//! - strings: `*mut c_char` + length out-params, returned by
|
||||
//! `anydoc_to_markdown*` and `anydoc_last_error`. Released via
|
||||
//! `anydoc_string_free`. Length is passed separately so binary-safe.
|
||||
//! - document buffer: a single `*mut u8` + length, returned by
|
||||
//! `anydoc_to_document`, holding the flat serialization from [`model`].
|
||||
//! Released via `anydoc_buffer_free`.
|
||||
//!
|
||||
//! Errors surface as stable non-zero codes (see `ERR_*`); the human-readable
|
||||
//! message is available through `anydoc_last_error`, thread-local.
|
||||
|
||||
#![allow(clippy::missing_safety_doc)]
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::ptr;
|
||||
|
||||
use anydoc::{ConvertError, Format, document_to_markdown};
|
||||
|
||||
mod asset_links;
|
||||
mod model;
|
||||
|
||||
// ---- Error codes -----------------------------------------------------------
|
||||
|
||||
/// Success.
|
||||
pub const ERR_OK: c_int = 0;
|
||||
/// `ConvertError::Unsupported`.
|
||||
pub const ERR_UNSUPPORTED: c_int = 1;
|
||||
/// `ConvertError::Malformed`.
|
||||
pub const ERR_MALFORMED: c_int = 2;
|
||||
/// `ConvertError::Encrypted`.
|
||||
pub const ERR_ENCRYPTED: c_int = 3;
|
||||
/// `ConvertError::ResourceLimit`.
|
||||
pub const ERR_RESOURCE_LIMIT: c_int = 4;
|
||||
/// `ConvertError::MissingPart`.
|
||||
pub const ERR_MISSING_PART: c_int = 5;
|
||||
/// `ConvertError::Io`.
|
||||
pub const ERR_IO: c_int = 6;
|
||||
/// PDF was passed to `anydoc_to_document` (unsupported there).
|
||||
pub const ERR_PDF_NO_MODEL: c_int = 7;
|
||||
/// Null pointer or invalid argument passed to the ABI.
|
||||
pub const ERR_INVALID_ARG: c_int = 8;
|
||||
/// Unknown format name passed to the ABI.
|
||||
pub const ERR_UNKNOWN_FORMAT: c_int = 9;
|
||||
/// `ConvertError::NeedsOcr`; page numbers are retained in the error detail.
|
||||
pub const ERR_NEEDS_OCR: c_int = 10;
|
||||
|
||||
thread_local! {
|
||||
static LAST_ERROR: std::cell::RefCell<Option<CString>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
fn set_last_error(msg: &str) {
|
||||
let cstring = CString::new(msg).unwrap_or_else(|_| CString::new("<invalid utf-8>").unwrap());
|
||||
LAST_ERROR.with(|cell| *cell.borrow_mut() = Some(cstring));
|
||||
}
|
||||
|
||||
fn error_code(err: &ConvertError) -> c_int {
|
||||
match err {
|
||||
ConvertError::Unsupported(_) => ERR_UNSUPPORTED,
|
||||
ConvertError::NeedsOcr { .. } => ERR_NEEDS_OCR,
|
||||
ConvertError::Malformed { .. } => ERR_MALFORMED,
|
||||
ConvertError::Encrypted => ERR_ENCRYPTED,
|
||||
ConvertError::ResourceLimit { .. } => ERR_RESOURCE_LIMIT,
|
||||
ConvertError::MissingPart { .. } => ERR_MISSING_PART,
|
||||
ConvertError::Io(_) => ERR_IO,
|
||||
_ => ERR_UNSUPPORTED,
|
||||
}
|
||||
}
|
||||
|
||||
fn fail(err: ConvertError) -> c_int {
|
||||
let code = error_code(&err);
|
||||
set_last_error(&err.to_string());
|
||||
code
|
||||
}
|
||||
|
||||
/// Run a conversion with panics contained. A panic that escapes an
|
||||
/// `extern "C"` function aborts the process (Rust 1.81+), so one malformed
|
||||
/// document would take the whole host process down instead of failing a single
|
||||
/// conversion. A caught panic surfaces as `Malformed`, the same as any other
|
||||
/// unusable input.
|
||||
///
|
||||
/// This cannot contain an abort the runtime raises on its own (stack overflow,
|
||||
/// allocation failure); those still kill the process.
|
||||
fn guarded<T>(
|
||||
entry: &str,
|
||||
parse: impl FnOnce() -> Result<T, ConvertError>,
|
||||
) -> Result<T, ConvertError> {
|
||||
// AssertUnwindSafe: the closure only reads the input slice and returns an
|
||||
// owned result. Nothing observable is left half-written when it panics,
|
||||
// because the out-params are written by the caller after this returns.
|
||||
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(parse)) {
|
||||
Ok(result) => result,
|
||||
Err(_) => Err(ConvertError::Malformed {
|
||||
part: None,
|
||||
detail: format!("panic while converting in {entry}"),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Format enum -----------------------------------------------------------
|
||||
|
||||
/// C-side format tag. Stable; mirrors the Node/Python lowercase string names
|
||||
/// via `format_name`. `ANYDOC_FORMAT_NONE` is the `Option::None` sentinel.
|
||||
pub const ANYDOC_FORMAT_NONE: c_int = -1;
|
||||
pub const ANYDOC_FORMAT_DOC: c_int = 0;
|
||||
pub const ANYDOC_FORMAT_DOCX: c_int = 1;
|
||||
pub const ANYDOC_FORMAT_ODT: c_int = 2;
|
||||
pub const ANYDOC_FORMAT_PDF: c_int = 3;
|
||||
pub const ANYDOC_FORMAT_PPT: c_int = 4;
|
||||
pub const ANYDOC_FORMAT_PPTX: c_int = 5;
|
||||
pub const ANYDOC_FORMAT_RTF: c_int = 6;
|
||||
pub const ANYDOC_FORMAT_EPUB: c_int = 7;
|
||||
pub const ANYDOC_FORMAT_XLSX: c_int = 8;
|
||||
pub const ANYDOC_FORMAT_ODS: c_int = 9;
|
||||
pub const ANYDOC_FORMAT_ODP: c_int = 10;
|
||||
pub const ANYDOC_FORMAT_CSV: c_int = 11;
|
||||
|
||||
fn format_to_c(format: Format) -> c_int {
|
||||
match format {
|
||||
Format::Doc => ANYDOC_FORMAT_DOC,
|
||||
Format::Docx => ANYDOC_FORMAT_DOCX,
|
||||
Format::Odt => ANYDOC_FORMAT_ODT,
|
||||
Format::Pdf => ANYDOC_FORMAT_PDF,
|
||||
Format::Ppt => ANYDOC_FORMAT_PPT,
|
||||
Format::Pptx => ANYDOC_FORMAT_PPTX,
|
||||
Format::Rtf => ANYDOC_FORMAT_RTF,
|
||||
Format::Epub => ANYDOC_FORMAT_EPUB,
|
||||
Format::Excel => ANYDOC_FORMAT_XLSX,
|
||||
Format::Ods => ANYDOC_FORMAT_ODS,
|
||||
Format::Odp => ANYDOC_FORMAT_ODP,
|
||||
Format::Csv => ANYDOC_FORMAT_CSV,
|
||||
}
|
||||
}
|
||||
|
||||
fn format_from_c(tag: c_int) -> Option<Format> {
|
||||
Some(match tag {
|
||||
ANYDOC_FORMAT_DOC => Format::Doc,
|
||||
ANYDOC_FORMAT_DOCX => Format::Docx,
|
||||
ANYDOC_FORMAT_ODT => Format::Odt,
|
||||
ANYDOC_FORMAT_PDF => Format::Pdf,
|
||||
ANYDOC_FORMAT_PPT => Format::Ppt,
|
||||
ANYDOC_FORMAT_PPTX => Format::Pptx,
|
||||
ANYDOC_FORMAT_RTF => Format::Rtf,
|
||||
ANYDOC_FORMAT_EPUB => Format::Epub,
|
||||
ANYDOC_FORMAT_XLSX => Format::Excel,
|
||||
ANYDOC_FORMAT_ODS => Format::Ods,
|
||||
ANYDOC_FORMAT_ODP => Format::Odp,
|
||||
ANYDOC_FORMAT_CSV => Format::Csv,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Format names, lowercase, matching the Node/Python convention.
|
||||
const FORMAT_NAMES: [&str; 12] =
|
||||
["doc", "docx", "odt", "pdf", "ppt", "pptx", "rtf", "epub", "xlsx", "ods", "odp", "csv"];
|
||||
|
||||
fn format_name(format: Format) -> &'static str {
|
||||
match format {
|
||||
Format::Doc => FORMAT_NAMES[0],
|
||||
Format::Docx => FORMAT_NAMES[1],
|
||||
Format::Odt => FORMAT_NAMES[2],
|
||||
Format::Pdf => FORMAT_NAMES[3],
|
||||
Format::Ppt => FORMAT_NAMES[4],
|
||||
Format::Pptx => FORMAT_NAMES[5],
|
||||
Format::Rtf => FORMAT_NAMES[6],
|
||||
Format::Epub => FORMAT_NAMES[7],
|
||||
Format::Excel => FORMAT_NAMES[8],
|
||||
Format::Ods => FORMAT_NAMES[9],
|
||||
Format::Odp => FORMAT_NAMES[10],
|
||||
Format::Csv => FORMAT_NAMES[11],
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_format_name(name: &str) -> Option<Format> {
|
||||
FORMAT_NAMES
|
||||
.iter()
|
||||
.zip([
|
||||
Format::Doc,
|
||||
Format::Docx,
|
||||
Format::Odt,
|
||||
Format::Pdf,
|
||||
Format::Ppt,
|
||||
Format::Pptx,
|
||||
Format::Rtf,
|
||||
Format::Epub,
|
||||
Format::Excel,
|
||||
Format::Ods,
|
||||
Format::Odp,
|
||||
Format::Csv,
|
||||
])
|
||||
.find(|(n, _)| **n == name)
|
||||
.map(|(_, f)| f)
|
||||
}
|
||||
|
||||
// ---- String out-param helper ----------------------------------------------
|
||||
|
||||
/// Box a `String` into a heap allocation the caller owns and must free with
|
||||
/// `anydoc_string_free`. Writes the byte length (excluding any NUL) to
|
||||
/// `*out_len`. The buffer is NUL-terminated for safety with C string readers,
|
||||
/// but binary-safe content relies on `out_len`.
|
||||
///
|
||||
/// Returns a dangling null pointer for empty strings so the Go side can treat
|
||||
/// a null result as "empty" without needing to free it. Length is still
|
||||
/// written as 0.
|
||||
fn string_out(s: String, out_str: *mut *mut c_char, out_len: *mut usize) -> c_int {
|
||||
if out_str.is_null() {
|
||||
set_last_error("anydoc: out_str is null");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let len = s.len();
|
||||
// SAFETY: we write the length unconditionally; null is the empty sentinel.
|
||||
if !out_len.is_null() {
|
||||
unsafe { *out_len = len };
|
||||
}
|
||||
if len == 0 {
|
||||
// Empty string: return null sentinel, no allocation to free.
|
||||
unsafe { *out_str = ptr::null_mut() };
|
||||
return ERR_OK;
|
||||
}
|
||||
let cstring = match CString::new(s.into_bytes()) {
|
||||
Ok(cs) => cs,
|
||||
Err(_) => {
|
||||
// Contains an interior NUL: fall back to lossy replacement so the
|
||||
// caller still gets something. Markdown shouldn't contain NULs.
|
||||
set_last_error("anydoc: output contained an interior NUL byte");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
};
|
||||
let raw = cstring.into_raw();
|
||||
// into_raw returns a buffer of capacity len+1 (with the trailing NUL).
|
||||
unsafe { *out_str = raw };
|
||||
ERR_OK
|
||||
}
|
||||
|
||||
// ---- C ABI functions -------------------------------------------------------
|
||||
|
||||
/// Detect the format from the content. Writes a format tag to `*out` (one of
|
||||
/// `ANYDOC_FORMAT_*`), or `ANYDOC_FORMAT_NONE` when nothing matches.
|
||||
///
|
||||
/// Returns `ERR_OK` on success (including no-match), `ERR_INVALID_ARG` on null
|
||||
/// pointers.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_format_from_bytes(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
out: *mut c_int,
|
||||
) -> c_int {
|
||||
if bytes.is_null() || out.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_format_from_bytes");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let slice = unsafe { std::slice::from_raw_parts(bytes, len) };
|
||||
let tag = Format::from_bytes(slice).map_or(ANYDOC_FORMAT_NONE, format_to_c);
|
||||
unsafe { *out = tag };
|
||||
ERR_OK
|
||||
}
|
||||
|
||||
/// The format a bare extension names (no leading dot). Writes a format tag to
|
||||
/// `*out`, or `ANYDOC_FORMAT_NONE` when unrecognized.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_format_from_extension(
|
||||
ext: *const c_char,
|
||||
out: *mut c_int,
|
||||
) -> c_int {
|
||||
if ext.is_null() || out.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_format_from_extension");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let ext_str = match unsafe { std::ffi::CStr::from_ptr(ext) }.to_str() {
|
||||
Ok(s) => s.trim_start_matches('.'),
|
||||
Err(_) => {
|
||||
set_last_error("anydoc: extension is not valid UTF-8");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
};
|
||||
let tag = Format::from_extension(ext_str).map_or(ANYDOC_FORMAT_NONE, format_to_c);
|
||||
unsafe { *out = tag };
|
||||
ERR_OK
|
||||
}
|
||||
|
||||
/// The format a path's extension names. Writes a format tag to `*out`, or
|
||||
/// `ANYDOC_FORMAT_NONE` when unrecognized.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_format_from_path(path: *const c_char, out: *mut c_int) -> c_int {
|
||||
if path.is_null() || out.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_format_from_path");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let path_str = match unsafe { std::ffi::CStr::from_ptr(path) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
set_last_error("anydoc: path is not valid UTF-8");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
};
|
||||
let tag =
|
||||
Format::from_path(std::path::Path::new(path_str)).map_or(ANYDOC_FORMAT_NONE, format_to_c);
|
||||
unsafe { *out = tag };
|
||||
ERR_OK
|
||||
}
|
||||
|
||||
/// Convert a document file to Markdown. The format is detected from the file
|
||||
/// content, with the extension as the fallback for signature-less formats
|
||||
/// (CSV).
|
||||
///
|
||||
/// On success writes a `*mut c_char` to `*out_str` and its byte length to
|
||||
/// `*out_len`. The caller must free the string with `anydoc_string_free`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_to_markdown(
|
||||
path: *const c_char,
|
||||
out_str: *mut *mut c_char,
|
||||
out_len: *mut usize,
|
||||
) -> c_int {
|
||||
if path.is_null() || out_str.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_to_markdown");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let path_str = match unsafe { std::ffi::CStr::from_ptr(path) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
set_last_error("anydoc: path is not valid UTF-8");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
};
|
||||
match guarded("anydoc_to_markdown", || anydoc::to_markdown(path_str)) {
|
||||
Ok(md) => string_out(md, out_str, out_len),
|
||||
Err(e) => fail(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an in-memory document to Markdown. `format_tag` is one of the
|
||||
/// `ANYDOC_FORMAT_*` constants, or `ANYDOC_FORMAT_NONE` to detect from the
|
||||
/// content.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_to_markdown_bytes(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
format_tag: c_int,
|
||||
out_str: *mut *mut c_char,
|
||||
out_len: *mut usize,
|
||||
) -> c_int {
|
||||
if bytes.is_null() || out_str.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_to_markdown_bytes");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let format = if format_tag == ANYDOC_FORMAT_NONE {
|
||||
None
|
||||
} else {
|
||||
match format_from_c(format_tag) {
|
||||
Some(f) => Some(f),
|
||||
None => {
|
||||
set_last_error("anydoc: unknown format tag");
|
||||
return ERR_UNKNOWN_FORMAT;
|
||||
}
|
||||
}
|
||||
};
|
||||
let slice = unsafe { std::slice::from_raw_parts(bytes, len) };
|
||||
match guarded("anydoc_to_markdown_bytes", || anydoc::to_markdown_bytes(slice, format)) {
|
||||
Ok(md) => string_out(md, out_str, out_len),
|
||||
Err(e) => fail(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an in-memory document to Markdown with embedded images rewritten
|
||||
/// as `` so they keep their original positions.
|
||||
///
|
||||
/// This is `to_document` + anydoc's official GFM serializer. Asset images are
|
||||
/// first turned into `ImageSource::External` URLs; the serializer already
|
||||
/// emits those as ordinary Markdown image links. PDF has no document model
|
||||
/// and is converted the same way as `anydoc_to_markdown_bytes`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_to_markdown_with_asset_links(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
format_tag: c_int,
|
||||
out_str: *mut *mut c_char,
|
||||
out_len: *mut usize,
|
||||
) -> c_int {
|
||||
if bytes.is_null() || out_str.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_to_markdown_with_asset_links");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let format = if format_tag == ANYDOC_FORMAT_NONE {
|
||||
None
|
||||
} else {
|
||||
match format_from_c(format_tag) {
|
||||
Some(f) => Some(f),
|
||||
None => {
|
||||
set_last_error("anydoc: unknown format tag");
|
||||
return ERR_UNKNOWN_FORMAT;
|
||||
}
|
||||
}
|
||||
};
|
||||
let slice = unsafe { std::slice::from_raw_parts(bytes, len) };
|
||||
match guarded("anydoc_to_markdown_with_asset_links", || {
|
||||
markdown_with_asset_links(slice, format)
|
||||
}) {
|
||||
Ok(md) => string_out(md, out_str, out_len),
|
||||
Err(e) => fail(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn markdown_with_asset_links(
|
||||
bytes: &[u8],
|
||||
format: Option<Format>,
|
||||
) -> Result<String, ConvertError> {
|
||||
let resolved = format
|
||||
.or_else(|| Format::from_bytes(bytes))
|
||||
.ok_or_else(|| {
|
||||
ConvertError::Unsupported(
|
||||
"unrecognized file content: name the format explicitly".into(),
|
||||
)
|
||||
})?;
|
||||
if resolved == Format::Pdf {
|
||||
return anydoc::to_markdown_bytes(bytes, resolved);
|
||||
}
|
||||
let mut document = anydoc::to_document(bytes, resolved)?;
|
||||
asset_links::rewrite_asset_images(&mut document);
|
||||
Ok(document_to_markdown(&document))
|
||||
}
|
||||
|
||||
/// Parse an in-memory document into the document model. `format_tag` is one of
|
||||
/// the `ANYDOC_FORMAT_*` constants, or `ANYDOC_FORMAT_NONE` to detect from the
|
||||
/// content.
|
||||
///
|
||||
/// On success writes a heap-allocated byte buffer to `*out_buf` and its length
|
||||
/// to `*out_len`. The buffer holds the flat serialization (see `model`). The
|
||||
/// caller must free it with `anydoc_buffer_free`.
|
||||
///
|
||||
/// Returns `ERR_PDF_NO_MODEL` for PDF input: PDF conversion produces Markdown
|
||||
/// directly and has no document-model form. Use `anydoc_to_markdown_bytes`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_to_document(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
format_tag: c_int,
|
||||
out_buf: *mut *mut u8,
|
||||
out_len: *mut usize,
|
||||
) -> c_int {
|
||||
unsafe { document_output(bytes, len, format_tag, out_buf, out_len, false) }
|
||||
}
|
||||
|
||||
/// Parse once and return the original document model followed by asset-linked
|
||||
/// Markdown in a single flat buffer. Free it with `anydoc_buffer_free`.
|
||||
/// PDF has no document model and returns `ERR_PDF_NO_MODEL`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_to_document_with_asset_links(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
format_tag: c_int,
|
||||
out_buf: *mut *mut u8,
|
||||
out_len: *mut usize,
|
||||
) -> c_int {
|
||||
unsafe { document_output(bytes, len, format_tag, out_buf, out_len, true) }
|
||||
}
|
||||
|
||||
unsafe fn document_output(
|
||||
bytes: *const u8,
|
||||
len: usize,
|
||||
format_tag: c_int,
|
||||
out_buf: *mut *mut u8,
|
||||
out_len: *mut usize,
|
||||
with_markdown: bool,
|
||||
) -> c_int {
|
||||
if bytes.is_null() || out_buf.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_to_document");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let format = if format_tag == ANYDOC_FORMAT_NONE {
|
||||
None
|
||||
} else {
|
||||
match format_from_c(format_tag) {
|
||||
Some(f) => Some(f),
|
||||
None => {
|
||||
set_last_error("anydoc: unknown format tag");
|
||||
return ERR_UNKNOWN_FORMAT;
|
||||
}
|
||||
}
|
||||
};
|
||||
let slice = unsafe { std::slice::from_raw_parts(bytes, len) };
|
||||
// Resolve the format up front so we can give the typed PDF error before
|
||||
// parsing, matching the crate's `to_document` contract.
|
||||
let resolved = match format {
|
||||
Some(f) => f,
|
||||
None => match Format::from_bytes(slice) {
|
||||
Some(f) => f,
|
||||
None => {
|
||||
set_last_error(
|
||||
"unsupported input: unrecognized file content: name the format explicitly",
|
||||
);
|
||||
return ERR_UNSUPPORTED;
|
||||
}
|
||||
},
|
||||
};
|
||||
if resolved == Format::Pdf {
|
||||
set_last_error(
|
||||
"unsupported input: PDF conversion produces Markdown directly and has no document-model form; use to_markdown_bytes",
|
||||
);
|
||||
return ERR_PDF_NO_MODEL;
|
||||
}
|
||||
let body = match guarded("anydoc_to_document", || {
|
||||
let mut document = anydoc::to_document(slice, resolved)?;
|
||||
let mut encoder = model::Encoder::new();
|
||||
// Encode before rewriting: Go needs the original asset IDs for image
|
||||
// placement and section metadata, while Markdown needs external URLs.
|
||||
model::write_document(&mut encoder, &document);
|
||||
if with_markdown {
|
||||
asset_links::rewrite_asset_images(&mut document);
|
||||
encoder.str(&document_to_markdown(&document));
|
||||
}
|
||||
Ok(encoder.into_vec())
|
||||
}) {
|
||||
Ok(body) => body,
|
||||
Err(e) => return fail(e),
|
||||
};
|
||||
let len = body.len();
|
||||
// Layout: [usize len][usize cap][u8 data..len]. The caller receives a
|
||||
// pointer to the data area and the length; `anydoc_buffer_free` walks back
|
||||
// to the header to recover (len, cap) for a sound deallocation.
|
||||
let header_size = std::mem::size_of::<BufferHeader>();
|
||||
let total = header_size + len;
|
||||
let layout = std::alloc::Layout::from_size_align(total, std::mem::align_of::<BufferHeader>())
|
||||
.expect("document buffer layout");
|
||||
let base = unsafe { std::alloc::alloc(layout) as *mut BufferHeader };
|
||||
if base.is_null() {
|
||||
set_last_error("anydoc: out of memory");
|
||||
return ERR_IO;
|
||||
}
|
||||
// The capacity we record is the *total* allocation size, so the free path
|
||||
// can reconstruct a layout of the same size to pass back to `dealloc`.
|
||||
unsafe {
|
||||
std::ptr::write(base, BufferHeader { len, cap: total });
|
||||
let data = (base as *mut u8).add(header_size);
|
||||
std::ptr::copy_nonoverlapping(body.as_ptr(), data, len);
|
||||
*out_buf = data;
|
||||
if !out_len.is_null() {
|
||||
*out_len = len;
|
||||
}
|
||||
}
|
||||
// `body` drops here, freeing its own backing allocation.
|
||||
ERR_OK
|
||||
}
|
||||
|
||||
/// Header prepended to every document buffer, so `anydoc_buffer_free` can
|
||||
/// soundly reclaim the allocation without any thread-local state. The data
|
||||
/// the caller sees starts immediately after this header.
|
||||
#[repr(C)]
|
||||
struct BufferHeader {
|
||||
len: usize,
|
||||
/// Total allocation size (header + data), used to rebuild the Layout for
|
||||
/// `dealloc`.
|
||||
cap: usize,
|
||||
}
|
||||
|
||||
/// Free a string returned by `anydoc_to_markdown*` or `anydoc_last_error`.
|
||||
/// Null is a no-op (the empty-string sentinel).
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_string_free(s: *mut c_char) {
|
||||
if s.is_null() {
|
||||
return;
|
||||
}
|
||||
// SAFETY: the pointer was produced by `CString::into_raw`; reclaiming it
|
||||
// with `from_raw` is the matching deallocator.
|
||||
unsafe {
|
||||
let _ = CString::from_raw(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// Free a document buffer returned by `anydoc_to_document`. Null is a no-op.
|
||||
/// `len` must be the length written to `*out_len` at allocation time; it is
|
||||
/// validated against the header for safety.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_buffer_free(buf: *mut u8, len: usize) {
|
||||
if buf.is_null() {
|
||||
return;
|
||||
}
|
||||
let header_size = std::mem::size_of::<BufferHeader>();
|
||||
// SAFETY: the pointer was produced by `anydoc_to_document` with a
|
||||
// `BufferHeader` immediately before it. Walk back to read the header and
|
||||
// rebuild the Layout, then dealloc the whole block.
|
||||
unsafe {
|
||||
let base = (buf.sub(header_size)) as *mut BufferHeader;
|
||||
let header = base.read();
|
||||
if header.len != len {
|
||||
// Length mismatch: caller bug. Leak rather than risk a bad free.
|
||||
set_last_error("anydoc: anydoc_buffer_free length does not match allocation");
|
||||
return;
|
||||
}
|
||||
let layout = std::alloc::Layout::from_size_align_unchecked(
|
||||
header.cap,
|
||||
std::mem::align_of::<BufferHeader>(),
|
||||
);
|
||||
std::alloc::dealloc(base as *mut u8, layout);
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the human-readable message for the last error on the current
|
||||
/// thread. The returned `*mut c_char` is freshly allocated and must be freed
|
||||
/// with `anydoc_string_free`. Returns null when there is no last error.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_last_error() -> *mut c_char {
|
||||
LAST_ERROR
|
||||
.with(|cell| cell.borrow_mut().take())
|
||||
.and_then(|cstring| {
|
||||
// Re-allocate as a fresh owned C string the caller can free.
|
||||
let bytes = cstring.into_bytes_with_nul();
|
||||
CString::from_vec_with_nul(bytes).ok().map(|cs| cs.into_raw())
|
||||
})
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
// ---- Go-side helpers exposed for parity tests -----------------------------
|
||||
|
||||
/// Return the lowercase name of a format tag, or null for an unknown tag.
|
||||
/// The returned string must be freed with `anydoc_string_free`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_format_name(tag: c_int) -> *mut c_char {
|
||||
if tag == ANYDOC_FORMAT_NONE {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
match format_from_c(tag) {
|
||||
Some(f) => {
|
||||
let name = format_name(f);
|
||||
match CString::new(name) {
|
||||
Ok(cs) => cs.into_raw(),
|
||||
Err(_) => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
None => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a lowercase format name into a tag. Writes the tag to `*out`, or
|
||||
/// `ANYDOC_FORMAT_NONE` when the name is unknown.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn anydoc_format_from_name(name: *const c_char, out: *mut c_int) -> c_int {
|
||||
if name.is_null() || out.is_null() {
|
||||
set_last_error("anydoc: null pointer passed to anydoc_format_from_name");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
let name_str = match unsafe { std::ffi::CStr::from_ptr(name) }.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
set_last_error("anydoc: format name is not valid UTF-8");
|
||||
return ERR_INVALID_ARG;
|
||||
}
|
||||
};
|
||||
let tag = parse_format_name(name_str).map_or(ANYDOC_FORMAT_NONE, format_to_c);
|
||||
unsafe { *out = tag };
|
||||
ERR_OK
|
||||
}
|
||||
Vendored
+334
@@ -0,0 +1,334 @@
|
||||
//! `#[repr(C)]` DTOs and a flat, self-describing serialization of the document
|
||||
//! model.
|
||||
//!
|
||||
//! The document tree is recursive (blocks hold blocks, inlines hold inlines,
|
||||
//! tables hold cells that hold blocks), so rather than surfacing it through a
|
||||
//! forest of opaque handles and one `_free` per node type, the FFI serializes
|
||||
//! the whole `Document` into a single length-prefixed byte buffer. That gives
|
||||
//! exactly one allocation family for the tree (`anydoc_buffer_free`) and keeps
|
||||
//! the C ABI tiny. Variant tags are written inline as `c_int`-compatible
|
||||
//! values, mirroring the Node/Python `kind` string convention.
|
||||
//!
|
||||
//! Format: see `write_document` below. Every field is length-prefixed so the
|
||||
//! Go side can decode without guessing. Strings carry their byte length as a
|
||||
//! u32; slices carry a u32 count followed by that many elements. Booleans are
|
||||
//! a single byte (0 or 1). Integers are little-endian fixed width.
|
||||
|
||||
use std::os::raw::c_int;
|
||||
|
||||
use anydoc::model;
|
||||
|
||||
/// Tag values for `Block` variants. Stable across versions; the Go side maps
|
||||
/// these to the same lowercase string names the Node and Python bindings use.
|
||||
pub const BLOCK_HEADING: c_int = 0;
|
||||
pub const BLOCK_PARAGRAPH: c_int = 1;
|
||||
pub const BLOCK_LIST: c_int = 2;
|
||||
pub const BLOCK_TABLE: c_int = 3;
|
||||
pub const BLOCK_QUOTE: c_int = 4;
|
||||
pub const BLOCK_CODE: c_int = 5;
|
||||
pub const BLOCK_RULE: c_int = 6;
|
||||
pub const BLOCK_MATH: c_int = 7;
|
||||
|
||||
pub const INLINE_TEXT: c_int = 0;
|
||||
pub const INLINE_LINK: c_int = 1;
|
||||
pub const INLINE_IMAGE: c_int = 2;
|
||||
pub const INLINE_ANCHOR: c_int = 3;
|
||||
pub const INLINE_NOTEREF: c_int = 4;
|
||||
pub const INLINE_LINEBREAK: c_int = 5;
|
||||
pub const INLINE_MATH: c_int = 6;
|
||||
pub const INLINE_CHECKBOX: c_int = 7;
|
||||
|
||||
pub const LINK_EXTERNAL: c_int = 0;
|
||||
pub const LINK_RELATIVE: c_int = 1;
|
||||
pub const LINK_ANCHOR: c_int = 2;
|
||||
|
||||
pub const IMG_EXTERNAL: c_int = 0;
|
||||
pub const IMG_ASSET: c_int = 1;
|
||||
pub const IMG_UNAVAILABLE: c_int = 2;
|
||||
|
||||
pub const MARKER_BULLET: c_int = 0;
|
||||
pub const MARKER_DECIMAL: c_int = 1;
|
||||
pub const MARKER_LOWER_ALPHA: c_int = 2;
|
||||
pub const MARKER_UPPER_ALPHA: c_int = 3;
|
||||
pub const MARKER_LOWER_ROMAN: c_int = 4;
|
||||
pub const MARKER_UPPER_ROMAN: c_int = 5;
|
||||
|
||||
pub const TABLE_DATA: c_int = 0;
|
||||
pub const TABLE_LAYOUT: c_int = 1;
|
||||
|
||||
pub const SLOT_ORIGIN: c_int = 0;
|
||||
pub const SLOT_COVERED: c_int = 1;
|
||||
|
||||
pub const NOTE_FOOTNOTE: c_int = 0;
|
||||
pub const NOTE_ENDNOTE: c_int = 1;
|
||||
|
||||
/// Encoder for the flat document buffer. Little-endian, length-prefixed.
|
||||
pub struct Encoder {
|
||||
buf: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn new() -> Self {
|
||||
Encoder { buf: Vec::with_capacity(8 * 1024) }
|
||||
}
|
||||
|
||||
pub fn into_vec(self) -> Vec<u8> {
|
||||
self.buf
|
||||
}
|
||||
|
||||
pub fn u8(&mut self, v: u8) {
|
||||
self.buf.push(v);
|
||||
}
|
||||
|
||||
pub fn bool(&mut self, v: bool) {
|
||||
self.buf.push(if v { 1 } else { 0 });
|
||||
}
|
||||
|
||||
pub fn u32(&mut self, v: u32) {
|
||||
self.buf.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
|
||||
pub fn u64(&mut self, v: u64) {
|
||||
self.buf.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
|
||||
pub fn i32(&mut self, v: i32) {
|
||||
self.buf.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
|
||||
/// A length-prefixed UTF-8 string. `None` writes a -1 length sentinel.
|
||||
pub fn opt_str(&mut self, v: &Option<String>) {
|
||||
match v {
|
||||
Some(s) => self.str(s),
|
||||
None => self.i32(-1),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn str(&mut self, s: &str) {
|
||||
self.u32(s.len() as u32);
|
||||
self.buf.extend_from_slice(s.as_bytes());
|
||||
}
|
||||
|
||||
/// A length-prefixed byte slice.
|
||||
pub fn bytes(&mut self, b: &[u8]) {
|
||||
self.u32(b.len() as u32);
|
||||
self.buf.extend_from_slice(b);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_document(e: &mut Encoder, doc: &model::Document) {
|
||||
write_blocks(e, &doc.blocks);
|
||||
e.u32(doc.notes.len() as u32);
|
||||
for note in &doc.notes {
|
||||
write_note(e, note);
|
||||
}
|
||||
e.u32(doc.assets.len() as u32);
|
||||
for asset in &doc.assets {
|
||||
write_asset(e, asset);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_blocks(e: &mut Encoder, blocks: &[model::Block]) {
|
||||
e.u32(blocks.len() as u32);
|
||||
for block in blocks {
|
||||
write_block(e, block);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_block(e: &mut Encoder, block: &model::Block) {
|
||||
match block {
|
||||
model::Block::Heading { level, anchor, content } => {
|
||||
e.i32(BLOCK_HEADING);
|
||||
e.u8(*level);
|
||||
e.opt_str(anchor);
|
||||
write_inlines(e, content);
|
||||
}
|
||||
model::Block::Paragraph(content) => {
|
||||
e.i32(BLOCK_PARAGRAPH);
|
||||
write_inlines(e, content);
|
||||
}
|
||||
model::Block::List(list) => {
|
||||
e.i32(BLOCK_LIST);
|
||||
write_list(e, list);
|
||||
}
|
||||
model::Block::Table(table) => {
|
||||
e.i32(BLOCK_TABLE);
|
||||
write_table(e, table);
|
||||
}
|
||||
model::Block::BlockQuote(inner) => {
|
||||
e.i32(BLOCK_QUOTE);
|
||||
write_blocks(e, inner);
|
||||
}
|
||||
model::Block::CodeBlock { lang, text } => {
|
||||
e.i32(BLOCK_CODE);
|
||||
e.opt_str(lang);
|
||||
e.str(text);
|
||||
}
|
||||
model::Block::Math(text) => {
|
||||
e.i32(BLOCK_MATH);
|
||||
e.str(text);
|
||||
}
|
||||
model::Block::Rule => {
|
||||
e.i32(BLOCK_RULE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_inlines(e: &mut Encoder, inlines: &[model::Inline]) {
|
||||
e.u32(inlines.len() as u32);
|
||||
for inline in inlines {
|
||||
write_inline(e, inline);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_inline(e: &mut Encoder, inline: &model::Inline) {
|
||||
match inline {
|
||||
model::Inline::Text { text, style } => {
|
||||
e.i32(INLINE_TEXT);
|
||||
e.str(text);
|
||||
write_style(e, *style);
|
||||
}
|
||||
model::Inline::Link { content, target } => {
|
||||
e.i32(INLINE_LINK);
|
||||
write_inlines(e, content);
|
||||
write_link_target(e, target);
|
||||
}
|
||||
model::Inline::Image { alt, source } => {
|
||||
e.i32(INLINE_IMAGE);
|
||||
e.str(alt);
|
||||
write_image_source(e, source);
|
||||
}
|
||||
model::Inline::Anchor(id) => {
|
||||
e.i32(INLINE_ANCHOR);
|
||||
e.str(id);
|
||||
}
|
||||
model::Inline::NoteRef(id) => {
|
||||
e.i32(INLINE_NOTEREF);
|
||||
e.str(id);
|
||||
}
|
||||
model::Inline::Math(text) => {
|
||||
e.i32(INLINE_MATH);
|
||||
e.str(text);
|
||||
}
|
||||
model::Inline::Checkbox(checked) => {
|
||||
e.i32(INLINE_CHECKBOX);
|
||||
e.bool(*checked);
|
||||
}
|
||||
model::Inline::LineBreak => {
|
||||
e.i32(INLINE_LINEBREAK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_style(e: &mut Encoder, style: model::Style) {
|
||||
e.bool(style.bold);
|
||||
e.bool(style.italic);
|
||||
e.bool(style.strike);
|
||||
e.bool(style.code);
|
||||
}
|
||||
|
||||
fn write_link_target(e: &mut Encoder, target: &model::LinkTarget) {
|
||||
match target {
|
||||
model::LinkTarget::External(v) => {
|
||||
e.i32(LINK_EXTERNAL);
|
||||
e.str(v);
|
||||
}
|
||||
model::LinkTarget::Relative(v) => {
|
||||
e.i32(LINK_RELATIVE);
|
||||
e.str(v);
|
||||
}
|
||||
model::LinkTarget::Anchor(v) => {
|
||||
e.i32(LINK_ANCHOR);
|
||||
e.str(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_image_source(e: &mut Encoder, source: &model::ImageSource) {
|
||||
match source {
|
||||
model::ImageSource::External(url) => {
|
||||
e.i32(IMG_EXTERNAL);
|
||||
e.str(url);
|
||||
}
|
||||
model::ImageSource::Asset(id) => {
|
||||
e.i32(IMG_ASSET);
|
||||
e.u64(id.0 as u64);
|
||||
}
|
||||
model::ImageSource::Unavailable => {
|
||||
e.i32(IMG_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_list(e: &mut Encoder, list: &model::List) {
|
||||
e.i32(marker_tag(list.marker));
|
||||
e.u64(list.start);
|
||||
e.u32(list.items.len() as u32);
|
||||
for item in &list.items {
|
||||
write_list_item(e, item);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_list_item(e: &mut Encoder, item: &model::ListItem) {
|
||||
write_blocks(e, &item.blocks);
|
||||
// Preserve the legacy optional field in the wire format. Upstream 0.2.3
|
||||
// moved task state into Inline::Checkbox; new callers read those nodes.
|
||||
e.i32(-1);
|
||||
e.opt_str(&item.marker_label);
|
||||
}
|
||||
|
||||
fn write_table(e: &mut Encoder, table: &model::Table) {
|
||||
e.u32(table.header_rows as u32);
|
||||
e.i32(if table.kind == model::TableKind::Layout { TABLE_LAYOUT } else { TABLE_DATA });
|
||||
e.u32(table.grid.len() as u32);
|
||||
for row in &table.grid {
|
||||
e.u32(row.len() as u32);
|
||||
for slot in row {
|
||||
write_slot(e, slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_slot(e: &mut Encoder, slot: &model::CellSlot) {
|
||||
match slot {
|
||||
model::CellSlot::Origin(cell) => {
|
||||
e.i32(SLOT_ORIGIN);
|
||||
write_cell(e, cell);
|
||||
}
|
||||
model::CellSlot::Covered { origin_row, origin_col } => {
|
||||
e.i32(SLOT_COVERED);
|
||||
e.u32(*origin_row as u32);
|
||||
e.u32(*origin_col as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_cell(e: &mut Encoder, cell: &model::Cell) {
|
||||
e.u32(cell.col_span);
|
||||
e.u32(cell.row_span);
|
||||
write_blocks(e, &cell.blocks);
|
||||
}
|
||||
|
||||
fn write_note(e: &mut Encoder, note: &model::Note) {
|
||||
e.str(¬e.id);
|
||||
e.i32(if note.kind == model::NoteKind::Endnote { NOTE_ENDNOTE } else { NOTE_FOOTNOTE });
|
||||
write_blocks(e, ¬e.blocks);
|
||||
}
|
||||
|
||||
fn write_asset(e: &mut Encoder, asset: &model::Asset) {
|
||||
e.u64(asset.id.0 as u64);
|
||||
e.str(&asset.media_type);
|
||||
e.str(&asset.origin_part);
|
||||
e.bytes(&asset.bytes);
|
||||
}
|
||||
|
||||
fn marker_tag(marker: model::MarkerKind) -> c_int {
|
||||
match marker {
|
||||
model::MarkerKind::Bullet => MARKER_BULLET,
|
||||
model::MarkerKind::Decimal => MARKER_DECIMAL,
|
||||
model::MarkerKind::LowerAlpha => MARKER_LOWER_ALPHA,
|
||||
model::MarkerKind::UpperAlpha => MARKER_UPPER_ALPHA,
|
||||
model::MarkerKind::LowerRoman => MARKER_LOWER_ROMAN,
|
||||
model::MarkerKind::UpperRoman => MARKER_UPPER_ROMAN,
|
||||
}
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
package anydoc
|
||||
|
||||
// Version is the version of the anydoc-go module, bumped in lockstep with the
|
||||
// Rust crate version (go/Cargo.toml) and the GitHub release tag. The
|
||||
// packaged static libraries.
|
||||
const Version = "0.2.4"
|
||||
Reference in new issue
Block a user