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,231 @@
|
||||
// Package anydoc converts office documents to Markdown inside the Go process,
|
||||
// without the Python docreader service.
|
||||
//
|
||||
// The conversion itself is the anydoc Rust library, linked as a static archive
|
||||
// through cgo (see third_party/anydoc-go). That archive needs a Rust toolchain
|
||||
// to produce, so it is opt-in: only builds tagged `anydoc` link it. Every other
|
||||
// build compiles the stub in backend_stub.go, where Available reports false and
|
||||
// the engine registry hides the engine with a reason the UI can show.
|
||||
//
|
||||
// The exported surface is deliberately narrow — bytes in, Markdown and embedded
|
||||
// images out — so the backend can be replaced (a WASI runtime, a future
|
||||
// upstream Go module) without touching the parser code that calls it.
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ImageDir is the markdown path prefix for extracted images. The image
|
||||
// resolver matches references by this path and swaps them for storage URLs.
|
||||
const ImageDir = "images/"
|
||||
|
||||
// Result is one converted document.
|
||||
type Result struct {
|
||||
// Markdown is GitHub-Flavored Markdown for the whole document.
|
||||
// When WithAssets is set, embedded images appear in place as
|
||||
// `` via anydoc's official serializer.
|
||||
Markdown string
|
||||
// Assets are the images embedded in the document, in document order.
|
||||
// Always empty for PDF, which anydoc renders straight to Markdown
|
||||
// without a document model.
|
||||
Assets []Asset
|
||||
// AssetsError explains why Assets is empty when images were asked for.
|
||||
// The document-model parse is what both extracts images and places them
|
||||
// in the markdown; when it fails, Markdown still comes from the text-only
|
||||
// renderer so the conversion succeeds and the caller decides whether to
|
||||
// log or ignore the loss.
|
||||
AssetsError error
|
||||
}
|
||||
|
||||
// Asset is one image embedded in a document.
|
||||
type Asset struct {
|
||||
// ID is the document-model asset index, as referenced by in-place image
|
||||
// sources. It is stable for a single conversion.
|
||||
ID uint64
|
||||
// Name is a generated, extension-carrying file name ("image-1.png"):
|
||||
// embedded assets have no name of their own inside the container.
|
||||
Name string
|
||||
// MediaType is the IANA media type the container declared.
|
||||
MediaType string
|
||||
// Data is the raw image bytes.
|
||||
Data []byte
|
||||
// Alt is the image's alternative text as the document author wrote it,
|
||||
// empty when the document carries none.
|
||||
Alt string
|
||||
// Section is the text of the nearest heading above the image, empty when
|
||||
// the image sits before the first heading.
|
||||
Section string
|
||||
}
|
||||
|
||||
// Options tunes a single conversion.
|
||||
type Options struct {
|
||||
// Format names the parser explicitly ("docx", "csv", ...). Empty means
|
||||
// the format is detected from the content, which every format except
|
||||
// CSV carries a signature for.
|
||||
Format string
|
||||
// WithAssets extracts embedded images and renders them in place in the
|
||||
// markdown using anydoc's official GFM serializer (asset images are
|
||||
// rewritten to `images/image-N.ext` links first). Ignored for PDF.
|
||||
WithAssets bool
|
||||
}
|
||||
|
||||
// ErrUnavailable is returned by Convert when the binding is not linked into
|
||||
// this build. Callers that can fall back to another engine should check for it
|
||||
// with errors.Is.
|
||||
var ErrUnavailable = errors.New("anydoc: binding not built into this binary")
|
||||
|
||||
// ErrResourceLimit marks a permanent anydoc rejection: the document exceeds a
|
||||
// parser budget (for example max_xml_nodes) and retrying the same bytes cannot
|
||||
// succeed. Callers detect it with errors.Is. The wrapped error's text names
|
||||
// the limit the converter reported.
|
||||
var ErrResourceLimit = errors.New("document is too large for the parser")
|
||||
|
||||
// resourceLimiter is the method set anydoc's *ConvertError uses to report a
|
||||
// parser budget failure. The cgo binding implements it; the default build does
|
||||
// not import that package, so classification goes through this interface.
|
||||
type resourceLimiter interface {
|
||||
ResourceLimit() bool
|
||||
LimitDetail() string
|
||||
}
|
||||
|
||||
// permanentResourceLimit is the errors.Is(ErrResourceLimit) wrapper. Error
|
||||
// keeps the user-facing reason and the converter's limit detail, without the
|
||||
// transport prefixes that sit further down the chain.
|
||||
type permanentResourceLimit struct {
|
||||
detail string
|
||||
cause error
|
||||
}
|
||||
|
||||
func (e *permanentResourceLimit) Error() string {
|
||||
if e.detail == "" {
|
||||
return ErrResourceLimit.Error()
|
||||
}
|
||||
return ErrResourceLimit.Error() + ": " + e.detail
|
||||
}
|
||||
|
||||
func (e *permanentResourceLimit) Unwrap() error { return e.cause }
|
||||
|
||||
func (e *permanentResourceLimit) Is(target error) bool { return target == ErrResourceLimit }
|
||||
|
||||
// markResourceLimit wraps a converter error whose kind is resource_limit.
|
||||
// Other errors, including untyped text that merely mentions the kind, are
|
||||
// returned unchanged so a transient failure can still be retried.
|
||||
func markResourceLimit(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var limiter resourceLimiter
|
||||
if !errors.As(err, &limiter) || !limiter.ResourceLimit() {
|
||||
return err
|
||||
}
|
||||
return &permanentResourceLimit{detail: strings.TrimSpace(limiter.LimitDetail()), cause: err}
|
||||
}
|
||||
|
||||
// supportedExtensions are the file types anydoc converts, mapped to the format
|
||||
// name its parser selector uses. The list is static so that engine metadata
|
||||
// (file types shown in the UI) is identical whether or not the binding is
|
||||
// linked in.
|
||||
var supportedExtensions = map[string]string{
|
||||
"doc": "doc",
|
||||
"docx": "docx",
|
||||
"docm": "docx",
|
||||
"odt": "odt",
|
||||
"rtf": "rtf",
|
||||
"ppt": "ppt",
|
||||
"pptx": "pptx",
|
||||
"pptm": "pptx",
|
||||
"odp": "odp",
|
||||
"xls": "xlsx",
|
||||
"xlsx": "xlsx",
|
||||
"xlsm": "xlsx",
|
||||
"ods": "ods",
|
||||
"epub": "epub",
|
||||
"csv": "csv",
|
||||
"pdf": "pdf",
|
||||
}
|
||||
|
||||
// SupportedFileTypes returns the extensions anydoc can convert, sorted so the
|
||||
// engine list is stable across restarts.
|
||||
func SupportedFileTypes() []string {
|
||||
types := make([]string, 0, len(supportedExtensions))
|
||||
for ext := range supportedExtensions {
|
||||
types = append(types, ext)
|
||||
}
|
||||
slices.Sort(types)
|
||||
return types
|
||||
}
|
||||
|
||||
// FormatForFile resolves the anydoc format name for a file type or file name.
|
||||
// ok is false for anything anydoc does not convert.
|
||||
func FormatForFile(fileType, fileName string) (format string, ok bool) {
|
||||
ext := normalizeExt(fileType)
|
||||
if ext == "" {
|
||||
ext = normalizeExt(filepath.Ext(fileName))
|
||||
}
|
||||
format, ok = supportedExtensions[ext]
|
||||
return format, ok
|
||||
}
|
||||
|
||||
// Supports reports whether anydoc converts this file type, regardless of
|
||||
// whether the binding is linked into this build.
|
||||
func Supports(fileType, fileName string) bool {
|
||||
_, ok := FormatForFile(fileType, fileName)
|
||||
return ok
|
||||
}
|
||||
|
||||
// Available reports whether conversions can actually run in this binary.
|
||||
func Available() bool { return backendAvailable() }
|
||||
|
||||
// UnavailableReason explains, for the UI, why Available is false. It returns
|
||||
// "" when the binding is available.
|
||||
func UnavailableReason() string { return backendUnavailableReason() }
|
||||
|
||||
// Version reports the anydoc release the linked binding was built from, or ""
|
||||
// when no binding is linked.
|
||||
func Version() string { return backendVersion() }
|
||||
|
||||
// Convert turns document bytes into Markdown. It returns ErrUnavailable when
|
||||
// the binding is not linked into this build.
|
||||
func Convert(data []byte, opts Options) (*Result, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, fmt.Errorf("anydoc: empty document")
|
||||
}
|
||||
if !backendAvailable() {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnavailable, backendUnavailableReason())
|
||||
}
|
||||
// PDF has no document model, so asset extraction would only produce an
|
||||
// error the caller has to special-case. Drop the request instead.
|
||||
if opts.Format == "pdf" {
|
||||
opts.WithAssets = false
|
||||
}
|
||||
result, err := backendConvert(data, opts)
|
||||
if err != nil {
|
||||
err = markResourceLimit(err)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
// PDFNeedsOCR reports whether a Convert error means the PDF has no usable
|
||||
// text layer. Scanned exam papers fail this way; the caller should fall back
|
||||
// to an engine that can rasterize pages for OCR.
|
||||
func PDFNeedsOCR(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var ocrError interface{ NeedsOCR() bool }
|
||||
if errors.As(err, &ocrError) && ocrError.NeedsOCR() {
|
||||
return true
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
return strings.Contains(msg, "ocr is required") ||
|
||||
strings.Contains(msg, "no extractable text")
|
||||
}
|
||||
|
||||
func normalizeExt(s string) string {
|
||||
return strings.ToLower(strings.TrimPrefix(strings.TrimSpace(s), "."))
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatForFileResolvesTypeAndName(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fileType string
|
||||
fileName string
|
||||
wantFormat string
|
||||
wantOK bool
|
||||
}{
|
||||
{name: "file type", fileType: "docx", wantFormat: "docx", wantOK: true},
|
||||
{name: "dotted file type", fileType: ".DOCX", wantFormat: "docx", wantOK: true},
|
||||
{name: "macro format maps to base format", fileType: "pptm", wantFormat: "pptx", wantOK: true},
|
||||
{name: "legacy excel maps to xlsx parser", fileType: "xls", wantFormat: "xlsx", wantOK: true},
|
||||
{name: "falls back to file name", fileName: "report.odt", wantFormat: "odt", wantOK: true},
|
||||
{name: "unsupported type", fileType: "png", wantOK: false},
|
||||
{name: "nothing to go on", wantOK: false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
format, ok := FormatForFile(tc.fileType, tc.fileName)
|
||||
if ok != tc.wantOK {
|
||||
t.Fatalf("FormatForFile(%q, %q) ok = %v, want %v", tc.fileType, tc.fileName, ok, tc.wantOK)
|
||||
}
|
||||
if format != tc.wantFormat {
|
||||
t.Fatalf("FormatForFile(%q, %q) = %q, want %q", tc.fileType, tc.fileName, format, tc.wantFormat)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The engine list must look the same whether or not the converter is linked
|
||||
// in, so that the UI can show the file types with a "not built in" reason.
|
||||
func TestSupportedFileTypesIsStableAndSorted(t *testing.T) {
|
||||
types := SupportedFileTypes()
|
||||
if !slices.IsSorted(types) {
|
||||
t.Fatalf("SupportedFileTypes is not sorted: %v", types)
|
||||
}
|
||||
for _, want := range []string{"docx", "pptx", "xlsx", "epub", "pdf", "csv"} {
|
||||
if !slices.Contains(types, want) {
|
||||
t.Errorf("SupportedFileTypes does not include %q: %v", want, types)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPDFNeedsOCR(t *testing.T) {
|
||||
if PDFNeedsOCR(nil) {
|
||||
t.Fatal("nil error was treated as needing OCR")
|
||||
}
|
||||
if !PDFNeedsOCR(errors.New("PDF has no extractable text (Scanned, 5 pages): OCR is required")) {
|
||||
t.Error("scanned-PDF error was not detected")
|
||||
}
|
||||
if PDFNeedsOCR(errors.New("not a readable zip archive")) {
|
||||
t.Error("unrelated error was treated as needing OCR")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertRejectsEmptyInput(t *testing.T) {
|
||||
if _, err := Convert(nil, Options{Format: "docx"}); err == nil {
|
||||
t.Fatal("Convert(nil) succeeded, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// Without the build tag there is no converter, and callers must be able to
|
||||
// detect that so they can fall back to another engine.
|
||||
func TestConvertReportsUnavailableBackend(t *testing.T) {
|
||||
if Available() {
|
||||
t.Skip("converter is linked into this build")
|
||||
}
|
||||
_, err := Convert([]byte("id,name\n1,a\n"), Options{Format: "csv"})
|
||||
if !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatalf("Convert error = %v, want ErrUnavailable", err)
|
||||
}
|
||||
if UnavailableReason() == "" {
|
||||
t.Error("UnavailableReason is empty while the converter is unavailable")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
upstream "github.com/firecrawl/anydoc/go"
|
||||
)
|
||||
|
||||
// This build links the anydoc Rust archive through the upstream cgo bindings
|
||||
// (vendored under third_party/anydoc-go until they are published as a module).
|
||||
// Conversion runs in-process: no subprocess, no HTTP hop, no Python.
|
||||
|
||||
// *ConvertError must keep implementing resourceLimiter so Convert can classify
|
||||
// a parser budget failure without this file's build tag.
|
||||
var _ resourceLimiter = (*upstream.ConvertError)(nil)
|
||||
|
||||
func backendAvailable() bool { return true }
|
||||
|
||||
func backendUnavailableReason() string { return "" }
|
||||
|
||||
func backendVersion() string { return upstream.Version }
|
||||
|
||||
func backendConvert(data []byte, opts Options) (*Result, error) {
|
||||
format, err := upstreamFormat(opts.Format)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !opts.WithAssets {
|
||||
markdown, err := upstream.ToMarkdownBytes(data, format)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("anydoc: markdown conversion failed: %w", err)
|
||||
}
|
||||
return &Result{Markdown: markdown}, nil
|
||||
}
|
||||
|
||||
// Official GFM serializer after rewriting Asset images to External URLs
|
||||
// (`images/image-N.ext`). One Rust parse supplies both the original model
|
||||
// (for image bytes and placements) and the rendered Markdown.
|
||||
document, markdown, err := upstream.ToDocumentWithAssetLinks(data, format)
|
||||
if err != nil {
|
||||
markdown, mdErr := upstream.ToMarkdownBytes(data, format)
|
||||
if mdErr != nil {
|
||||
return nil, fmt.Errorf("anydoc: markdown conversion failed: %w", mdErr)
|
||||
}
|
||||
return &Result{
|
||||
Markdown: markdown,
|
||||
AssetsError: fmt.Errorf("anydoc: image extraction failed: %w", err),
|
||||
}, nil
|
||||
}
|
||||
return &Result{Markdown: markdown, Assets: collectAssets(document)}, nil
|
||||
}
|
||||
|
||||
// upstreamFormat maps our format name onto the binding's constant. An empty
|
||||
// name means "detect from content".
|
||||
func upstreamFormat(name string) (*upstream.Format, error) {
|
||||
if name == "" {
|
||||
return nil, nil
|
||||
}
|
||||
format, ok := upstream.FormatFromExtension(name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("anydoc: unsupported format %q", name)
|
||||
}
|
||||
return &format, nil
|
||||
}
|
||||
|
||||
// collectAssets names each embedded image after its position in the document,
|
||||
// because containers store assets without a usable file name of their own, and
|
||||
// attaches the alt text and section the image appeared under.
|
||||
func collectAssets(document *upstream.Document) []Asset {
|
||||
if document == nil || len(document.Assets) == 0 {
|
||||
return nil
|
||||
}
|
||||
placements := placeAssets(document.Blocks)
|
||||
assets := make([]Asset, 0, len(document.Assets))
|
||||
for i, asset := range document.Assets {
|
||||
if len(asset.Data) == 0 {
|
||||
continue
|
||||
}
|
||||
placement := placements[uint64(i)]
|
||||
assets = append(assets, Asset{
|
||||
ID: uint64(i),
|
||||
Name: fmt.Sprintf("image-%d%s", i+1, extensionFor(asset.MediaType)),
|
||||
MediaType: asset.MediaType,
|
||||
Data: asset.Data,
|
||||
Alt: placement.alt,
|
||||
Section: placement.section,
|
||||
})
|
||||
}
|
||||
return assets
|
||||
}
|
||||
|
||||
// placement is where in the document an asset was referenced from.
|
||||
type placement struct {
|
||||
alt string
|
||||
section string
|
||||
}
|
||||
|
||||
// placeAssets walks the document body and records, per asset, the alt text of
|
||||
// the image that referenced it and the heading it sat under. The walk
|
||||
// recurses into lists, tables, and quotes, since an image can be nested
|
||||
// anywhere a paragraph can.
|
||||
func placeAssets(blocks []upstream.Block) map[uint64]placement {
|
||||
placements := make(map[uint64]placement)
|
||||
section := ""
|
||||
walkBlocks(blocks, §ion, placements)
|
||||
return placements
|
||||
}
|
||||
|
||||
func walkBlocks(blocks []upstream.Block, section *string, placements map[uint64]placement) {
|
||||
for _, block := range blocks {
|
||||
switch block.Kind {
|
||||
case "heading":
|
||||
*section = inlineText(block.Content)
|
||||
walkInlines(block.Content, *section, placements)
|
||||
case "paragraph":
|
||||
walkInlines(block.Content, *section, placements)
|
||||
case "block_quote":
|
||||
walkBlocks(block.Blocks, section, placements)
|
||||
case "list":
|
||||
if block.List != nil {
|
||||
for _, item := range block.List.Items {
|
||||
walkBlocks(item.Blocks, section, placements)
|
||||
}
|
||||
}
|
||||
case "table":
|
||||
if block.Table != nil {
|
||||
for _, row := range block.Table.Grid {
|
||||
for _, slot := range row {
|
||||
if slot.Cell != nil {
|
||||
walkBlocks(slot.Cell.Blocks, section, placements)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func walkInlines(inlines []upstream.Inline, section string, placements map[uint64]placement) {
|
||||
for _, inline := range inlines {
|
||||
switch inline.Kind {
|
||||
case "image":
|
||||
if inline.Source == nil || inline.Source.Kind != "asset" || inline.Source.AssetID == nil {
|
||||
continue
|
||||
}
|
||||
alt := ""
|
||||
if inline.Alt != nil {
|
||||
alt = strings.TrimSpace(*inline.Alt)
|
||||
}
|
||||
// First reference wins: the same asset can be placed more than
|
||||
// once, and the earliest placement is the one a reader would
|
||||
// associate it with.
|
||||
if _, seen := placements[*inline.Source.AssetID]; !seen {
|
||||
placements[*inline.Source.AssetID] = placement{alt: alt, section: section}
|
||||
}
|
||||
case "link":
|
||||
walkInlines(inline.Content, section, placements)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// inlineText flattens inline content to plain text, which is all a heading is
|
||||
// needed for here.
|
||||
func inlineText(inlines []upstream.Inline) string {
|
||||
var text strings.Builder
|
||||
for _, inline := range inlines {
|
||||
switch inline.Kind {
|
||||
case "text", "math":
|
||||
if inline.Text != nil {
|
||||
text.WriteString(*inline.Text)
|
||||
}
|
||||
case "checkbox":
|
||||
if inline.Checked != nil && *inline.Checked {
|
||||
text.WriteString("[x]")
|
||||
} else {
|
||||
text.WriteString("[ ]")
|
||||
}
|
||||
case "link":
|
||||
text.WriteString(inlineText(inline.Content))
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(text.String())
|
||||
}
|
||||
|
||||
// extensionFor returns the extension that the Markdown link for an embedded
|
||||
// image carries.
|
||||
//
|
||||
// This table is one half of a contract with the Rust serializer. The link it
|
||||
// writes (`images/image-N<ext>`, see third_party/anydoc-go/src/asset_links.rs)
|
||||
// has to equal the ImageRef.OriginalRef built from this name, because
|
||||
// ImageResolver looks the link up in a map keyed by that exact string. The
|
||||
// media types and extensions below must therefore match `extension_for` in
|
||||
// asset_links.rs exactly.
|
||||
//
|
||||
// It is deliberately a pure table. Asking the platform MIME registry made the
|
||||
// answer depend on the host's /etc/mime.types, which is how EMF/WMF ended up
|
||||
// as ".emf"/".wmf" here and ".bin" on the Rust side: the link never matched
|
||||
// the ref and the image bytes were never stored. Unknown types fall back to
|
||||
// ".bin" on both sides, so downstream storage never writes an extension-less
|
||||
// blob.
|
||||
func extensionFor(mediaType string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(mediaType)) {
|
||||
case "image/jpeg", "image/jpg":
|
||||
return ".jpg"
|
||||
case "image/png":
|
||||
return ".png"
|
||||
case "image/gif":
|
||||
return ".gif"
|
||||
case "image/webp":
|
||||
return ".webp"
|
||||
case "image/bmp":
|
||||
return ".bmp"
|
||||
case "image/tiff":
|
||||
return ".tiff"
|
||||
case "image/svg+xml":
|
||||
return ".svg"
|
||||
case "image/emf":
|
||||
return ".emf"
|
||||
case "image/wmf":
|
||||
return ".wmf"
|
||||
}
|
||||
return ".bin"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//go:build !anydoc || !cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
// The default build links no converter: the anydoc archive is a Rust build
|
||||
// artifact, and requiring a Rust toolchain to compile Academy would be a steep
|
||||
// price for one optional engine. Everything still compiles and the engine
|
||||
// registry simply reports the engine as unavailable.
|
||||
|
||||
const unavailableReason = "anydoc engine not built into this binary " +
|
||||
"(rebuild with `make build-anydoc` / `-tags anydoc`; " +
|
||||
"Docker images need `--build-arg WITH_ANYDOC=1`)"
|
||||
|
||||
func backendAvailable() bool { return false }
|
||||
|
||||
func backendUnavailableReason() string { return unavailableReason }
|
||||
|
||||
func backendVersion() string { return "" }
|
||||
|
||||
func backendConvert(_ []byte, _ Options) (*Result, error) {
|
||||
// Convert checks Available first, so this is only reachable if a caller
|
||||
// bypasses it.
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var benchmarkDocuments = []string{
|
||||
"doc/text.doc", "docx/text.docx", "ppt/pres.ppt", "pptx/pres.pptx",
|
||||
"odt/text.odt", "ods/sheet.ods", "odp/pres.odp", "rtf/text.rtf",
|
||||
"epub/book.epub", "xls/sheet.xls", "xlsx/sheet.xlsx",
|
||||
"xlsb/handmade-sheet.xlsb", "csv/sheet.csv", "pdf/text.pdf",
|
||||
}
|
||||
|
||||
// Synthetic size sweeps cover the Go/Rust document-copy path and the bounded
|
||||
// hostile-PDF detector. They are separate from the upstream fixture comparison.
|
||||
func BenchmarkConvertScaling(b *testing.B) {
|
||||
for _, rows := range []int{100, 10000} {
|
||||
b.Run(fmt.Sprintf("csv_%d_rows", rows), func(b *testing.B) {
|
||||
data := []byte("name,quantity,description\n" +
|
||||
strings.Repeat("widget,42,Quarterly shipping report\n", rows))
|
||||
b.SetBytes(int64(len(data)))
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
result, err := Convert(data, Options{Format: "csv", WithAssets: true})
|
||||
if err != nil || result.AssetsError != nil {
|
||||
b.Fatalf("conversion: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, rows := range []int{100, 10000} {
|
||||
b.Run(fmt.Sprintf("xlsx_%d_rows", rows), func(b *testing.B) {
|
||||
data := largeWorkbook(b, rows)
|
||||
result, err := Convert(data, Options{Format: "xlsx", WithAssets: true})
|
||||
if err != nil || !strings.Contains(result.Markdown, fmt.Sprintf("row-%d", rows)) {
|
||||
b.Fatalf("preflight: last row was lost, err=%v", err)
|
||||
}
|
||||
b.SetBytes(int64(len(data)))
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
result, err := Convert(data, Options{Format: "xlsx", WithAssets: true})
|
||||
if err != nil || result.AssetsError != nil {
|
||||
b.Fatalf("conversion: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, operators := range []int{40000, 200000} {
|
||||
b.Run(fmt.Sprintf("pdf_%d_operators", operators), func(b *testing.B) {
|
||||
data := unmatchedShowTextPDF(operators)
|
||||
b.SetBytes(int64(len(data)))
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
if _, err := Convert(data, Options{Format: "pdf"}); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func largeWorkbook(t testing.TB, rows int) []byte {
|
||||
t.Helper()
|
||||
data := readFixture(t, "xlsx/sheet.xlsx")
|
||||
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
w := zip.NewWriter(&out)
|
||||
for _, file := range r.File {
|
||||
if file.Name == "xl/worksheets/sheet1.xml" {
|
||||
continue
|
||||
}
|
||||
if err := w.Copy(file); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var sheet strings.Builder
|
||||
sheet.WriteString(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>`)
|
||||
for row := 1; row <= rows; row++ {
|
||||
fmt.Fprintf(&sheet, `<row r="%d"><c r="A%d" t="inlineStr"><is><t>row-%d</t></is></c>`+
|
||||
`<c r="B%d"><v>42</v></c></row>`,
|
||||
row, row, row, row)
|
||||
}
|
||||
sheet.WriteString(`</sheetData></worksheet>`)
|
||||
part, err := w.Create("xl/worksheets/sheet1.xml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write([]byte(sheet.String())); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out.Bytes()
|
||||
}
|
||||
|
||||
func readFixture(t testing.TB, name string) []byte {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(filepath.Join("testdata", "upstream", name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// WithAssets exercises the default reader path, including Rust parsing,
|
||||
// the document ABI, Go decoding, image collection, and Markdown serialization.
|
||||
// Input I/O is excluded. B/op measures Go allocations, not the Rust heap.
|
||||
func BenchmarkConvert(b *testing.B) {
|
||||
for _, name := range benchmarkDocuments {
|
||||
b.Run(strings.Split(name, "/")[0], func(b *testing.B) {
|
||||
data := readFixture(b, name)
|
||||
format := strings.TrimPrefix(filepath.Ext(name), ".")
|
||||
if format == "xls" || format == "xlsb" {
|
||||
format = "xlsx"
|
||||
}
|
||||
opts := Options{Format: format, WithAssets: true}
|
||||
if result, err := Convert(data, opts); err != nil ||
|
||||
result.Markdown == "" ||
|
||||
result.AssetsError != nil {
|
||||
b.Fatalf("preflight conversion: result=%+v err=%v", result, err)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(len(data)))
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
result, err := Convert(data, opts)
|
||||
if err != nil || result.Markdown == "" || result.AssetsError != nil {
|
||||
b.Errorf("conversion failed: result=%+v err=%v", result, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// These tests exercise the linked Rust converter, so they only build with the
|
||||
// `anydoc` tag. Run them with:
|
||||
//
|
||||
// scripts/build-anydoc-lib.sh && go test -tags anydoc ./internal/infrastructure/docparser/...
|
||||
|
||||
// SupportedFileTypes is a static list, so that the engine can advertise its
|
||||
// file types in builds that link no converter. This is the check that it still
|
||||
// agrees with what the converter actually accepts, in either direction.
|
||||
func TestSupportedFileTypesMatchTheConverter(t *testing.T) {
|
||||
for _, fileType := range SupportedFileTypes() {
|
||||
format, ok := FormatForFile(fileType, "")
|
||||
if !ok {
|
||||
t.Errorf("%q is advertised but maps to no format", fileType)
|
||||
continue
|
||||
}
|
||||
if _, err := upstreamFormat(format); err != nil {
|
||||
t.Errorf("%q maps to format %q, which the converter rejects: %v", fileType, format, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertCSV(t *testing.T) {
|
||||
result, err := Convert([]byte("quarter,widgets\nQ1,12\nQ2,15\n"), Options{Format: "csv"})
|
||||
if err != nil {
|
||||
t.Fatalf("Convert: %v", err)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "| quarter | widgets |") {
|
||||
t.Fatalf("expected a markdown table header, got:\n%s", result.Markdown)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "| Q1 | 12 |") {
|
||||
t.Fatalf("expected the first data row, got:\n%s", result.Markdown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertDocxWithEmbeddedImage(t *testing.T) {
|
||||
document := buildDocx(t)
|
||||
|
||||
result, err := Convert(document, Options{Format: "docx", WithAssets: true})
|
||||
if err != nil {
|
||||
t.Fatalf("Convert: %v", err)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "# Quarterly report") {
|
||||
t.Fatalf("expected the heading, got:\n%s", result.Markdown)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "Widgets shipped on time.") {
|
||||
t.Fatalf("expected the body paragraph, got:\n%s", result.Markdown)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "Closing remarks.") {
|
||||
t.Fatalf("expected the trailing paragraph, got:\n%s", result.Markdown)
|
||||
}
|
||||
if len(result.Assets) != 1 {
|
||||
t.Fatalf("got %d assets, want 1", len(result.Assets))
|
||||
}
|
||||
asset := result.Assets[0]
|
||||
if asset.Name != "image-1.png" {
|
||||
t.Errorf("asset name = %q, want image-1.png", asset.Name)
|
||||
}
|
||||
if !bytes.Equal(asset.Data, onePixelPNG()) {
|
||||
t.Errorf("asset data does not round-trip the embedded image")
|
||||
}
|
||||
if asset.Alt != "Shipping chart" {
|
||||
t.Errorf("asset alt = %q, want %q", asset.Alt, "Shipping chart")
|
||||
}
|
||||
if asset.Section != "Quarterly report" {
|
||||
t.Errorf("asset section = %q, want %q", asset.Section, "Quarterly report")
|
||||
}
|
||||
imageLink := ""
|
||||
if !strings.Contains(result.Markdown, imageLink) {
|
||||
t.Fatalf("expected the image in place, got:\n%s", result.Markdown)
|
||||
}
|
||||
before := strings.Index(result.Markdown, "Widgets shipped on time.")
|
||||
at := strings.Index(result.Markdown, imageLink)
|
||||
after := strings.Index(result.Markdown, "Closing remarks.")
|
||||
if before < 0 || at < 0 || after < 0 || !(before < at && at < after) {
|
||||
t.Fatalf("image is not between the surrounding paragraphs:\n%s", result.Markdown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertTextlessPDFNeedsOCR(t *testing.T) {
|
||||
_, err := Convert(textlessPDF(), Options{Format: "pdf"})
|
||||
if err == nil {
|
||||
t.Fatal("Convert succeeded on a PDF with no text, want an error")
|
||||
}
|
||||
if !PDFNeedsOCR(err) {
|
||||
t.Fatalf("Convert error = %v, want an OCR-required error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The ABI reports error messages through a thread-local slot that a second
|
||||
// call reads, so a goroutine resumed on another OS thread would report an
|
||||
// empty or someone else's message. The binding pins the goroutine for the
|
||||
// pair; this is the regression test for that.
|
||||
func TestErrorDetailSurvivesConcurrency(t *testing.T) {
|
||||
const conversions = 4000
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var empty, crossed atomic.Int64
|
||||
for i := 0; i < conversions; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
// docx and xlsx failures carry distinctive messages, so a detail
|
||||
// naming the other format proves it came from the wrong thread.
|
||||
format, want, other := "docx", "not a readable zip archive", "not a readable workbook container"
|
||||
if i%2 == 1 {
|
||||
format, want, other = "xlsx", "not a readable workbook container", "not a readable zip archive"
|
||||
}
|
||||
_, err := Convert([]byte(fmt.Sprintf("garbage %d", i)), Options{Format: format})
|
||||
if err == nil {
|
||||
t.Errorf("Convert(garbage) succeeded")
|
||||
return
|
||||
}
|
||||
detail := err.Error()
|
||||
switch {
|
||||
case strings.Contains(detail, other):
|
||||
crossed.Add(1)
|
||||
case !strings.Contains(detail, want):
|
||||
empty.Add(1)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if empty.Load() > 0 || crossed.Load() > 0 {
|
||||
t.Fatalf("error details lost or crossed between goroutines: lost=%d crossed=%d (of %d)",
|
||||
empty.Load(), crossed.Load(), conversions)
|
||||
}
|
||||
}
|
||||
|
||||
// Detection reads the container itself, so a document whose format is not
|
||||
// named still converts.
|
||||
func TestConvertDetectsFormatFromContent(t *testing.T) {
|
||||
result, err := Convert(buildDocx(t), Options{})
|
||||
if err != nil {
|
||||
t.Fatalf("Convert: %v", err)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "# Quarterly report") {
|
||||
t.Fatalf("expected the heading, got:\n%s", result.Markdown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertRejectsGarbage(t *testing.T) {
|
||||
if _, err := Convert([]byte("not a document at all"), Options{Format: "docx"}); err == nil {
|
||||
t.Fatal("Convert succeeded on garbage input, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// PDF has no document model, so WithAssets has to be dropped rather than turn
|
||||
// a working conversion into an error.
|
||||
func TestConvertPDFIgnoresAssetRequest(t *testing.T) {
|
||||
result, err := Convert(minimalPDF(), Options{Format: "pdf", WithAssets: true})
|
||||
if err != nil {
|
||||
t.Fatalf("Convert: %v", err)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, "Shipping summary") {
|
||||
t.Fatalf("expected the page text, got:\n%s", result.Markdown)
|
||||
}
|
||||
if len(result.Assets) != 0 {
|
||||
t.Errorf("got %d assets for a PDF, want 0", len(result.Assets))
|
||||
}
|
||||
}
|
||||
|
||||
// A PDF whose catalog holds a deeply nested array is the shape of
|
||||
// RUSTSEC-2026-0187: lopdf 0.41 recursed per nesting level and killed the
|
||||
// process with an uncatchable stack overflow, which no Go-side or Rust-side
|
||||
// recover can contain. The pinned dependency in third_party/anydoc-go bounds
|
||||
// the recursion, so this must come back as an ordinary error — and if a
|
||||
// dependency bump ever reintroduces it, this test crashes rather than passing
|
||||
// quietly.
|
||||
func TestDeeplyNestedPDFFailsWithoutKillingTheProcess(t *testing.T) {
|
||||
const depth = 50000
|
||||
nested := strings.Repeat("[", depth) + strings.Repeat("]", depth)
|
||||
|
||||
_, err := Convert(nestedPDF(nested), Options{Format: "pdf"})
|
||||
if err == nil {
|
||||
t.Fatal("Convert succeeded on a PDF with no text, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// A show-text operator with no operand made the page-classification scan walk
|
||||
// back over the whole content stream looking for one, so a stream of bare
|
||||
// `] TJ` tokens cost time quadratic in its length: before pdf-inspector 1.14.2
|
||||
// this input took 26 seconds of CPU, and a 2 MB one took nearly two minutes.
|
||||
// Unbounded work is the half of the hostile-PDF problem that guarded() cannot
|
||||
// catch — it never panics, it just holds the core — so the bound is pinned
|
||||
// here. The budget is deliberately far above the ~7ms a bounded lookback needs:
|
||||
// what it has to distinguish is linear from quadratic, not fast from slow.
|
||||
func TestDetectorLookbackStaysLinear(t *testing.T) {
|
||||
const (
|
||||
operators = 200000
|
||||
budget = 15 * time.Second
|
||||
)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
// The result is irrelevant: the page carries one real text run, so
|
||||
// this converts either way. Only how long it takes is under test.
|
||||
_, _ = Convert(unmatchedShowTextPDF(operators), Options{Format: "pdf"})
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(budget):
|
||||
t.Fatalf("converting a PDF with %d unmatched TJ operators took over %s; "+
|
||||
"the detector's operand lookback is no longer bounded", operators, budget)
|
||||
}
|
||||
}
|
||||
|
||||
// buildDocx writes the smallest OOXML package that carries a heading, a
|
||||
// paragraph, and one embedded image.
|
||||
func buildDocx(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
|
||||
parts := map[string]string{
|
||||
"[Content_Types].xml": `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
||||
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
||||
<Default Extension="xml" ContentType="application/xml"/>
|
||||
<Default Extension="png" ContentType="image/png"/>
|
||||
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
|
||||
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
|
||||
</Types>`,
|
||||
"_rels/.rels": `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
|
||||
</Relationships>`,
|
||||
"word/styles.xml": `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
||||
<w:style w:type="paragraph" w:styleId="Heading1">
|
||||
<w:name w:val="heading 1"/>
|
||||
<w:pPr><w:outlineLvl w:val="0"/></w:pPr>
|
||||
</w:style>
|
||||
</w:styles>`,
|
||||
"word/_rels/document.xml.rels": `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
|
||||
<Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>
|
||||
</Relationships>`,
|
||||
"word/document.xml": `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
||||
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
|
||||
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
|
||||
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
||||
<w:body>
|
||||
<w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr><w:r><w:t>Quarterly report</w:t></w:r></w:p>
|
||||
<w:p><w:r><w:t>Widgets shipped on time.</w:t></w:r></w:p>
|
||||
<w:p><w:r><w:drawing><wp:inline><wp:docPr id="1" name="Chart" descr="Shipping chart"/>
|
||||
<a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
||||
<pic:pic><pic:nvPicPr><pic:cNvPr id="1" name="Chart"/><pic:cNvPicPr/></pic:nvPicPr>
|
||||
<pic:blipFill><a:blip r:embed="rId10"/></pic:blipFill>
|
||||
<pic:spPr/></pic:pic>
|
||||
</a:graphicData></a:graphic>
|
||||
</wp:inline></w:drawing></w:r></w:p>
|
||||
<w:p><w:r><w:t>Closing remarks.</w:t></w:r></w:p>
|
||||
</w:body>
|
||||
</w:document>`,
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
archive := zip.NewWriter(&buf)
|
||||
for name, content := range parts {
|
||||
writer, err := archive.Create(name)
|
||||
if err != nil {
|
||||
t.Fatalf("create %s: %v", name, err)
|
||||
}
|
||||
if _, err := writer.Write([]byte(content)); err != nil {
|
||||
t.Fatalf("write %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
image, err := archive.Create("word/media/image1.png")
|
||||
if err != nil {
|
||||
t.Fatalf("create image part: %v", err)
|
||||
}
|
||||
if _, err := image.Write(onePixelPNG()); err != nil {
|
||||
t.Fatalf("write image part: %v", err)
|
||||
}
|
||||
if err := archive.Close(); err != nil {
|
||||
t.Fatalf("close archive: %v", err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// onePixelPNG is a 1x1 transparent PNG: the smallest thing a container will
|
||||
// accept as an image part.
|
||||
func onePixelPNG() []byte {
|
||||
return []byte{
|
||||
0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a,
|
||||
0x00, 0x00, 0x00, 0x0d, 'I', 'H', 'D', 'R',
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
|
||||
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89,
|
||||
0x00, 0x00, 0x00, 0x0a, 'I', 'D', 'A', 'T',
|
||||
0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01,
|
||||
0x0d, 0x0a, 0x2d, 0xb4,
|
||||
0x00, 0x00, 0x00, 0x00, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82,
|
||||
}
|
||||
}
|
||||
|
||||
// minimalPDF is a one-page PDF with a single text run, written by hand so the
|
||||
// test carries no binary fixture.
|
||||
func minimalPDF() []byte {
|
||||
content := "BT /F1 12 Tf 20 100 Td (Shipping summary) Tj ET\n"
|
||||
return writePDF([]string{
|
||||
"<< /Type /Catalog /Pages 2 0 R >>",
|
||||
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R " +
|
||||
"/Resources << /Font << /F1 5 0 R >> >> >>",
|
||||
fmt.Sprintf("<< /Length %d >>\nstream\n%sendstream", len(content), content),
|
||||
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
|
||||
})
|
||||
}
|
||||
|
||||
// nestedPDF is a one-page PDF carrying the given nested value in its catalog.
|
||||
func nestedPDF(nested string) []byte {
|
||||
return writePDF([]string{
|
||||
"<< /Type /Catalog /Pages 2 0 R /Nested " + nested + " >>",
|
||||
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] >>",
|
||||
})
|
||||
}
|
||||
|
||||
// unmatchedShowTextPDF is a one-page PDF whose content stream shows text once
|
||||
// and then repeats a TJ operator whose array operand is never opened.
|
||||
func unmatchedShowTextPDF(operators int) []byte {
|
||||
content := "BT /F1 12 Tf 20 100 Td (Shipping summary) Tj ET\n" +
|
||||
strings.Repeat("] TJ\n", operators)
|
||||
return writePDF([]string{
|
||||
"<< /Type /Catalog /Pages 2 0 R >>",
|
||||
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R " +
|
||||
"/Resources << /Font << /F1 5 0 R >> >> >>",
|
||||
fmt.Sprintf("<< /Length %d >>\nstream\n%sendstream", len(content), content),
|
||||
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
|
||||
})
|
||||
}
|
||||
|
||||
// textlessPDF is a one-page PDF with no text stream: the shape of a scanned
|
||||
// exam paper as far as pdf-inspector is concerned.
|
||||
func textlessPDF() []byte {
|
||||
return writePDF([]string{
|
||||
"<< /Type /Catalog /Pages 2 0 R >>",
|
||||
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
||||
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] >>",
|
||||
})
|
||||
}
|
||||
|
||||
func writePDF(objects []string) []byte {
|
||||
var pdf bytes.Buffer
|
||||
offsets := make([]int, len(objects))
|
||||
pdf.WriteString("%PDF-1.4\n")
|
||||
for i, object := range objects {
|
||||
offsets[i] = pdf.Len()
|
||||
fmt.Fprintf(&pdf, "%d 0 obj\n%s\nendobj\n", i+1, object)
|
||||
}
|
||||
|
||||
xref := pdf.Len()
|
||||
fmt.Fprintf(&pdf, "xref\n0 %d\n0000000000 65535 f \n", len(objects)+1)
|
||||
for _, offset := range offsets {
|
||||
fmt.Fprintf(&pdf, "%010d 00000 n \n", offset)
|
||||
}
|
||||
fmt.Fprintf(&pdf, "trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n", len(objects)+1, xref)
|
||||
return pdf.Bytes()
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
upstream "github.com/firecrawl/anydoc/go"
|
||||
)
|
||||
|
||||
// ANYDOC_UPSTREAM_DIR points to the checked-out release tag. This opt-in
|
||||
// upgrade check runs the full upstream corpus through our actual Go ABI and
|
||||
// compares Markdown/error details to the upstream Rust snapshots.
|
||||
func TestUpstreamCorpus(t *testing.T) {
|
||||
root := os.Getenv("ANYDOC_UPSTREAM_DIR")
|
||||
if root == "" {
|
||||
t.Skip("set ANYDOC_UPSTREAM_DIR to an anydoc release checkout")
|
||||
}
|
||||
snapshots, err := filepath.Glob(filepath.Join(root, "tests/snapshots/snapshots__*.snap"))
|
||||
if err != nil || len(snapshots) == 0 {
|
||||
t.Fatalf("no snapshots: %v", err)
|
||||
}
|
||||
for _, path := range snapshots {
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(filepath.Base(path), "snapshots__"), ".snap")
|
||||
parts := strings.Split(name, "__")
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
data, err := os.ReadFile(filepath.Join(root, "tests/fixtures", parts[0], parts[1]))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, expected, ok := strings.Cut(string(snapshot[4:]), "---\n")
|
||||
if !ok {
|
||||
t.Fatal("invalid snapshot header")
|
||||
}
|
||||
format, ok := upstream.FormatFromExtension(strings.TrimPrefix(filepath.Ext(parts[1]), "."))
|
||||
if !ok {
|
||||
t.Fatal("unrecognized fixture extension")
|
||||
}
|
||||
// The Go binding rejects empty slices before entering C, retaining
|
||||
// its established empty-input error instead of the ZIP diagnostic.
|
||||
if len(data) == 0 {
|
||||
if !strings.HasPrefix(expected, "ERROR:") {
|
||||
t.Fatal("empty fixture should fail")
|
||||
}
|
||||
expected = "ERROR: empty input"
|
||||
}
|
||||
actual, err := upstream.ToMarkdownBytes(data, &format)
|
||||
if err != nil {
|
||||
var typed *upstream.ConvertError
|
||||
if !errors.As(err, &typed) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual = "ERROR: " + typed.Detail
|
||||
}
|
||||
if strings.TrimSpace(actual) != strings.TrimSpace(expected) {
|
||||
t.Fatalf("snapshot mismatch\nwant: %s\ngot: %s", expected, actual)
|
||||
}
|
||||
if err == nil && format != upstream.FormatPdf {
|
||||
document, err := upstream.ToDocument(data, &format)
|
||||
if err != nil {
|
||||
t.Fatalf("document ABI: %v", err)
|
||||
}
|
||||
markdown, err := upstream.ToMarkdownWithAssetLinks(data, &format)
|
||||
if err != nil {
|
||||
t.Fatalf("asset renderer: %v", err)
|
||||
}
|
||||
combined, combinedMarkdown, err := upstream.ToDocumentWithAssetLinks(data, &format)
|
||||
if err != nil {
|
||||
t.Fatalf("combined ABI: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(combined, document) || combinedMarkdown != markdown {
|
||||
t.Fatal("single-parse output differs from independent document/Markdown APIs")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// fakeConvertError stands in for the cgo binding's *ConvertError. The
|
||||
// production type exposes the same two methods; this fake lets the default
|
||||
// build classify a resource_limit failure without linking the parser.
|
||||
type fakeConvertError struct {
|
||||
kind string
|
||||
detail string
|
||||
}
|
||||
|
||||
func (e *fakeConvertError) Error() string {
|
||||
if e.detail == "" {
|
||||
return "anydoc: " + e.kind
|
||||
}
|
||||
return "anydoc: " + e.kind + ": " + e.detail
|
||||
}
|
||||
|
||||
func (e *fakeConvertError) ResourceLimit() bool { return e.kind == "resource_limit" }
|
||||
|
||||
func (e *fakeConvertError) LimitDetail() string { return e.detail }
|
||||
|
||||
func TestMarkResourceLimitClassifiesPermanentBudgetFailure(t *testing.T) {
|
||||
const detail = "resource limit exceeded (max_xml_nodes): part exceeds 2000000 xml nodes"
|
||||
raw := &fakeConvertError{kind: "resource_limit", detail: detail}
|
||||
// Same wrap the cgo backend applies before the reader names the file.
|
||||
backendErr := fmt.Errorf("anydoc: markdown conversion failed: %w", raw)
|
||||
|
||||
got := markResourceLimit(backendErr)
|
||||
if !errors.Is(got, ErrResourceLimit) {
|
||||
t.Fatalf("markResourceLimit() = %v, want errors.Is ErrResourceLimit", got)
|
||||
}
|
||||
// The reader keeps the chain with %w, which is what convert inspects.
|
||||
readerErr := fmt.Errorf("anydoc conversion failed for %q: %w", "xlsx50k.xlsx", got)
|
||||
if !errors.Is(readerErr, ErrResourceLimit) {
|
||||
t.Fatalf("reader wrap = %v, want errors.Is ErrResourceLimit", readerErr)
|
||||
}
|
||||
message := readerErr.Error()
|
||||
for _, want := range []string{"too large", "max_xml_nodes", "2000000"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("message %q does not mention %q", message, want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(message, "DocReader") {
|
||||
t.Errorf("message points at DocReader, which did not parse the file: %q", message)
|
||||
}
|
||||
if !errors.Is(got, raw) {
|
||||
t.Fatal("resource-limit wrap dropped the converter error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkResourceLimitLeavesOtherFailuresRetryable(t *testing.T) {
|
||||
malformed := &fakeConvertError{kind: "malformed", detail: "not a readable zip archive"}
|
||||
in := fmt.Errorf("anydoc: markdown conversion failed: %w", malformed)
|
||||
if got := markResourceLimit(in); got != in || errors.Is(got, ErrResourceLimit) {
|
||||
t.Fatalf("malformed error = %v, want it unchanged and retryable", got)
|
||||
}
|
||||
|
||||
// The kind has to be typed. A matching sentence from somewhere else is
|
||||
// not a permanent parser rejection.
|
||||
untyped := errors.New("anydoc: resource_limit: resource limit exceeded (max_xml_nodes): " +
|
||||
"part exceeds 2000000 xml nodes")
|
||||
if got := markResourceLimit(untyped); errors.Is(got, ErrResourceLimit) {
|
||||
t.Fatalf("untyped error was classified as a resource limit: %v", got)
|
||||
}
|
||||
|
||||
if got := markResourceLimit(nil); got != nil {
|
||||
t.Fatalf("markResourceLimit(nil) = %v, want nil", got)
|
||||
}
|
||||
|
||||
bare := &fakeConvertError{kind: "resource_limit"}
|
||||
got := markResourceLimit(bare)
|
||||
if !errors.Is(got, ErrResourceLimit) {
|
||||
t.Fatalf("limit without detail = %v, want ErrResourceLimit", got)
|
||||
}
|
||||
if !strings.Contains(got.Error(), "too large") {
|
||||
t.Fatalf("limit without detail = %q, want the permanent-failure reason", got.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Opt-in sustained mixed-format load through the production WithAssets path.
|
||||
// Use /usr/bin/time -l (macOS) or -v (Linux) to measure native peak RSS too:
|
||||
// Go's memory statistics do not include Rust allocations.
|
||||
func TestConcurrentConversionStress(t *testing.T) {
|
||||
setting := os.Getenv("ANYDOC_STRESS_DURATION")
|
||||
if setting == "" {
|
||||
t.Skip("set ANYDOC_STRESS_DURATION, e.g. 30s")
|
||||
}
|
||||
duration, err := time.ParseDuration(setting)
|
||||
if err != nil || duration <= 0 {
|
||||
t.Fatalf("invalid stress duration %q", setting)
|
||||
}
|
||||
type input struct {
|
||||
data []byte
|
||||
opts Options
|
||||
want *Result
|
||||
}
|
||||
inputs := make([]input, 0, len(benchmarkDocuments))
|
||||
for _, name := range benchmarkDocuments {
|
||||
format := strings.Split(name, "/")[0]
|
||||
if format == "xls" || format == "xlsb" {
|
||||
format = "xlsx"
|
||||
}
|
||||
i := input{data: readFixture(t, name), opts: Options{Format: format, WithAssets: true}}
|
||||
i.want, err = Convert(i.data, i.opts)
|
||||
if err != nil || i.want.AssetsError != nil {
|
||||
t.Fatalf("preflight %s: %v", name, err)
|
||||
}
|
||||
inputs = append(inputs, i)
|
||||
}
|
||||
const workers = 8
|
||||
samples := make([][]time.Duration, workers)
|
||||
var wg sync.WaitGroup
|
||||
start := time.Now()
|
||||
deadline := start.Add(duration)
|
||||
for worker := 0; worker < workers; worker++ {
|
||||
wg.Add(1)
|
||||
go func(worker int) {
|
||||
defer wg.Done()
|
||||
for n := worker; time.Now().Before(deadline); n++ {
|
||||
i := inputs[n%len(inputs)]
|
||||
began := time.Now()
|
||||
got, err := Convert(i.data, i.opts)
|
||||
samples[worker] = append(samples[worker], time.Since(began))
|
||||
if err != nil ||
|
||||
got.AssetsError != nil ||
|
||||
got.Markdown != i.want.Markdown ||
|
||||
len(got.Assets) != len(i.want.Assets) {
|
||||
t.Errorf("worker %d: inconsistent conversion, err=%v", worker, err)
|
||||
return
|
||||
}
|
||||
for j, asset := range got.Assets {
|
||||
if !bytes.Equal(asset.Data, i.want.Assets[j].Data) ||
|
||||
asset.Name != i.want.Assets[j].Name {
|
||||
t.Errorf("worker %d: inconsistent asset %d", worker, j)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}(worker)
|
||||
}
|
||||
wg.Wait()
|
||||
elapsed := time.Since(start)
|
||||
var latencies []time.Duration
|
||||
for _, sample := range samples {
|
||||
latencies = append(latencies, sample...)
|
||||
}
|
||||
if len(latencies) == 0 {
|
||||
t.Fatal("no conversions completed")
|
||||
}
|
||||
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
|
||||
var memory runtime.MemStats
|
||||
runtime.ReadMemStats(&memory)
|
||||
t.Logf("workers=%d conversions=%d elapsed=%s throughput=%.1f docs/s "+
|
||||
"p50=%s p95=%s p99=%s GoHeapAlloc=%d GoTotalAlloc=%d",
|
||||
workers, len(latencies), elapsed, float64(len(latencies))/elapsed.Seconds(),
|
||||
latencies[len(latencies)*50/100], latencies[len(latencies)*95/100], latencies[len(latencies)*99/100],
|
||||
memory.HeapAlloc, memory.TotalAlloc)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# Preserve PDF byte offsets and fixed-width xref records from upstream fixtures.
|
||||
*.pdf binary
|
||||
@@ -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.
|
||||
@@ -0,0 +1 @@
|
||||
Fixtures from [firecrawl/anydoc v0.2.4](https://github.com/firecrawl/anydoc/tree/42bf1c5ecdde9eb0d96d6bd75a9e6698cf93b14c/tests/fixtures), under the included MIT license. Used for Go ABI regression tests and repeatable conversion benchmarks. Files are copied unchanged.
|
||||
@@ -0,0 +1,8 @@
|
||||
Kind,Value,Note
|
||||
Percent,15.5%,fifteen and a half
|
||||
Currency,1234.5,dollars
|
||||
Thousands,9876543,grouped
|
||||
Date,2026-03-15,ides of March
|
||||
Duration,26:30:15,over a day
|
||||
Tiny,0.0000004,four ten-millionths
|
||||
Boolean,TRUE,yes
|
||||
|
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
+50
@@ -0,0 +1,50 @@
|
||||
%PDF-1.4
|
||||
1 0 obj
|
||||
<< /Type /Catalog /Pages 2 0 R >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /Type /Pages /Kids [5 0 R 7 0 R] /Count 2 >>
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 1 >>
|
||||
stream
|
||||
€
|
||||
endstream
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 3 0 R >> /XObject << /Im1 4 0 R >> >> /Contents 6 0 R >>
|
||||
endobj
|
||||
6 0 obj
|
||||
<< /Length 53 >>
|
||||
stream
|
||||
BT /F1 24 Tf 72 700 Td (Text on the first page) Tj ET
|
||||
endstream
|
||||
endobj
|
||||
7 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 3 0 R >> /XObject << /Im1 4 0 R >> >> /Contents 8 0 R >>
|
||||
endobj
|
||||
8 0 obj
|
||||
<< /Length 32 >>
|
||||
stream
|
||||
q 468 0 0 648 72 72 cm /Im1 Do Q
|
||||
endstream
|
||||
endobj
|
||||
xref
|
||||
0 9
|
||||
0000000000 65535 f
|
||||
0000000009 00000 n
|
||||
0000000058 00000 n
|
||||
0000000121 00000 n
|
||||
0000000191 00000 n
|
||||
0000000335 00000 n
|
||||
0000000487 00000 n
|
||||
0000000590 00000 n
|
||||
0000000742 00000 n
|
||||
trailer
|
||||
<< /Size 9 /Root 1 0 R >>
|
||||
startxref
|
||||
824
|
||||
%%EOF
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
%PDF-1.4
|
||||
1 0 obj
|
||||
<< /Type /Catalog /Pages 2 0 R >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /Type /Pages /Kids [5 0 R 7 0 R] /Count 2 >>
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 1 >>
|
||||
stream
|
||||
€
|
||||
endstream
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 3 0 R >> /XObject << /Im1 4 0 R >> >> /Contents 6 0 R >>
|
||||
endobj
|
||||
6 0 obj
|
||||
<< /Length 32 >>
|
||||
stream
|
||||
q 468 0 0 648 72 72 cm /Im1 Do Q
|
||||
endstream
|
||||
endobj
|
||||
7 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 3 0 R >> /XObject << /Im1 4 0 R >> >> /Contents 8 0 R >>
|
||||
endobj
|
||||
8 0 obj
|
||||
<< /Length 32 >>
|
||||
stream
|
||||
q 468 0 0 648 72 72 cm /Im1 Do Q
|
||||
endstream
|
||||
endobj
|
||||
xref
|
||||
0 9
|
||||
0000000000 65535 f
|
||||
0000000009 00000 n
|
||||
0000000058 00000 n
|
||||
0000000121 00000 n
|
||||
0000000191 00000 n
|
||||
0000000335 00000 n
|
||||
0000000487 00000 n
|
||||
0000000569 00000 n
|
||||
0000000721 00000 n
|
||||
trailer
|
||||
<< /Size 9 /Root 1 0 R >>
|
||||
startxref
|
||||
803
|
||||
%%EOF
|
||||
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
+5
@@ -0,0 +1,5 @@
|
||||
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0 Cambria Math;}}
|
||||
\pard The roots are {\mmath{\*\moMath{\rtlch\fcs1 \af0 \ltrch\fcs0 \f0\insrsid1 }{\mr\mscr0\msty2 x}{\mr\mscr0\msty2 =}{\mf{\mfPr{\mctrlPr\f0 }}{\mnum{\mr\mscr0\msty2 -b\'b1}{\mrad{\mradPr{\mdegHide on}{\mctrlPr\f0 }}{\mdeg}{\me{\msSup{\me{\mr\mscr0\msty2 b}}{\msup{\mr\mscr0\msty2 2}}}{\mr\mscr0\msty2 -4ac}}}}{\mden{\mr\mscr0\msty2 2a}}}}{\mmathPict{\*\mmathPr}{\pict\pngblip\picw1\pich1 89504e47}}} for any a.\par
|
||||
\pard {\mmath{\*\moMathPara{\moMathParaPr{\mjc centerGroup}}{\*\moMath{\mnary{\mnaryPr{\mchr \u8721 ?}{\mlimLoc undOvr}{\mctrlPr\f0 }}{\msub{\mr\mscr0\msty2 i=1}}{\msup{\mr\mscr0\msty2 n}}{\me{\msSub{\me{\mr\mscr0\msty2 x}}{\msub{\mr\mscr0\msty2 i}}}}}{\mr\mscr0\msty2 =}{\mfunc{\mfName{\mr\mscr0\msty0 sin}}{\me{\md{\mdPr{\mctrlPr\f0 }}{\me{\mr\mscr0\msty2 \u952 ?}}}}}}{\*\moMath{\mr\mnor where }{\mr\mscr0\msty2 \u8722 ?1<x}}}}\par
|
||||
\pard A price of $5 or $6 is not math.\par
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
{\rtf1\ansi\deff3\adeflang1025
|
||||
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\froman\fprq2\fcharset2 Symbol;}{\f2\fswiss\fprq2\fcharset0 Arial;}{\f3\froman\fprq2\fcharset0 Liberation Serif{\*\falt Times New Roman};}{\f4\fswiss\fprq2\fcharset0 Liberation Sans{\*\falt Arial};}{\f5\fnil\fprq2\fcharset0 Microsoft YaHei;}{\f6\fnil\fprq2\fcharset0 Arial;}{\f7\fswiss\fprq0\fcharset128 Arial;}}
|
||||
{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}
|
||||
{\stylesheet{\s0\snext0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052 Normal;}
|
||||
{\s1\snext25\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl0\outlinelevel0\nowidctlpar\hyphpar0\ltrpar\fs36\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052 heading 1;}
|
||||
{\s2\snext25\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052 heading 2;}
|
||||
{\s3\snext25\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl2\outlinelevel2\nowidctlpar\hyphpar0\ltrpar\fs26\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052 heading 3;}
|
||||
{\*\cs15\snext15\loch\b0 NotBold;}
|
||||
{\*\cs16\snext16\loch\b Bold;}
|
||||
{\*\cs17\snext17\loch\i Italic;}
|
||||
{\*\cs18\snext18\loch\strike Struck;}
|
||||
{\*\cs19\snext19\loch\cf9\ul\ulc0 Hyperlink;}
|
||||
{\*\cs20\snext20 Footnote Characters;}
|
||||
{\*\cs21\snext21\loch\super footnote reference;}
|
||||
{\*\cs22\snext22 Endnote Characters;}
|
||||
{\*\cs23\snext23\loch\super endnote reference;}
|
||||
{\s24\sbasedon0\snext25\rtlch\af6\afs28 \ltrch\hich\af4\loch\sb240\sa120\keepn\f4\fs28\dbch\af5 Heading;}
|
||||
{\s25\sbasedon0\snext25\loch\sl276\slmult1\sb0\sa140 Body Text;}
|
||||
{\s26\sbasedon25\snext26\rtlch\af7 \ltrch List;}
|
||||
{\s27\sbasedon0\snext27\rtlch\af7\afs24\ai \ltrch\loch\sb120\sa120\noline\fs24\i caption;}
|
||||
{\s28\sbasedon0\snext28\rtlch\af7 \ltrch\loch\noline Index;}
|
||||
{\s29\snext29\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\b\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052 StrongPara;}
|
||||
{\s30\sbasedon0\snext30\loch\nowidctlpar\noline Table Contents;}
|
||||
{\s31\sbasedon0\snext31\rtlch\afs20 \ltrch\loch\fi-340\li340\lin340\noline\fs20 footnote text;}
|
||||
{\s32\sbasedon0\snext32\rtlch\afs20 \ltrch\loch\fi-340\li340\lin340\noline\fs20 endnote text;}
|
||||
{\s33\sbasedon0\snext33 Frame Contents;}
|
||||
}{\*\listtable{\list\listtemplateid1
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}
|
||||
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi0\li0}\listid1}
|
||||
{\list\listtemplateid2
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow2{\leveltext \'02\'00.;}{\levelnumbers\'01;}\fi0\li0}
|
||||
{\listlevel\levelnfc4\leveljc0\levelstartat1\levelfollow2{\leveltext \'02\'01);}{\levelnumbers\'01;}\fi0\li0}
|
||||
{\listlevel\levelnfc2\leveljc0\levelstartat1\levelfollow2{\leveltext \'02\'02.;}{\levelnumbers\'01;}\fi0\li0}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'03.;}{\levelnumbers\'01;}\fi-360\li1800}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'04.;}{\levelnumbers\'01;}\fi-360\li2160}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'05.;}{\levelnumbers\'01;}\fi-360\li2520}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'06.;}{\levelnumbers\'01;}\fi-360\li2880}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'07.;}{\levelnumbers\'01;}\fi-360\li3240}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'08.;}{\levelnumbers\'01;}\fi-360\li3600}\listid2}
|
||||
{\list\listtemplateid3
|
||||
{\listlevel\levelnfc23\leveljc0\levelstartat1\levelfollow2{\leveltext \'01\u8226 ?;}{\levelnumbers;}\f9\fi0\li0}
|
||||
{\listlevel\levelnfc23\leveljc0\levelstartat1\levelfollow2{\leveltext \'01\u9702 ?;}{\levelnumbers;}\f9\fi0\li0}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'02.;}{\levelnumbers\'01;}\fi-360\li1440}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'03.;}{\levelnumbers\'01;}\fi-360\li1800}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'04.;}{\levelnumbers\'01;}\fi-360\li2160}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'05.;}{\levelnumbers\'01;}\fi-360\li2520}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'06.;}{\levelnumbers\'01;}\fi-360\li2880}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'07.;}{\levelnumbers\'01;}\fi-360\li3240}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'08.;}{\levelnumbers\'01;}\fi-360\li3600}\listid3}
|
||||
{\list\listtemplateid4
|
||||
{\listlevel\levelnfc1\leveljc0\levelstartat1\levelfollow2{\leveltext \'02\'00.;}{\levelnumbers\'01;}\fi0\li0}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'01.;}{\levelnumbers\'01;}\fi-360\li1080}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'02.;}{\levelnumbers\'01;}\fi-360\li1440}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'03.;}{\levelnumbers\'01;}\fi-360\li1800}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'04.;}{\levelnumbers\'01;}\fi-360\li2160}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'05.;}{\levelnumbers\'01;}\fi-360\li2520}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'06.;}{\levelnumbers\'01;}\fi-360\li2880}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'07.;}{\levelnumbers\'01;}\fi-360\li3240}
|
||||
{\listlevel\levelnfc0\leveljc0\levelstartat1\levelfollow0{\leveltext \'02\'08.;}{\levelnumbers\'01;}\fi-360\li3600}\listid4}
|
||||
}{\listoverridetable{\listoverride\listid1\listoverridecount0\ls1}{\listoverride\listid2\listoverridecount0\ls2}{\listoverride\listid3\listoverridecount0\ls3}{\listoverride\listid4\listoverridecount0\ls4}}{\*\generator LibreOffice/25.2.7.2$Windows_X86_64 LibreOffice_project/5cbfd1ab6520636bb5f7b99185aa69bd7456825d}{\info{\creatim\yr0\mo0\dy0\hr0\min0}{\revtim\yr0\mo0\dy0\hr0\min0}{\printim\yr0\mo0\dy0\hr0\min0}}{\*\userprops}\deftab709\deftab709\deftab709\deftab709\deftab709
|
||||
\hyphauto1\viewscale100\formshade\nobrkwrptbl\paperh16838\paperw11906\margl1134\margr1134\margt1134\margb1134\sectd\sbknone\sftnnar\saftnnrlc\sectunlocked1\pgwsxn11906\pghsxn16838\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\ftnbj\ftnstart1\ftnrstcont\ftnnar\fet2\aftnrstcont\aftnstart1\aftnnrlc
|
||||
{\*\ftnsep\chftnsep}\pgndec\pard\plain \s1\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl0\outlinelevel0\nowidctlpar\hyphpar0\ltrpar\fs36\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Fixture Document}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Plain paragraph with }{\loch\cs16\loch\b\loch
|
||||
bold}{\loch
|
||||
, }{\loch\cs17\loch\i\loch
|
||||
italic}{\loch
|
||||
, and }{\loch\cs18\loch\strike\loch
|
||||
struck}{\loch
|
||||
runs.}
|
||||
\par \pard\plain \s29\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\b\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Style-bold paragraph with a }{\loch\cs15\loch\b0\loch
|
||||
NotBold-styled span}{\loch
|
||||
inside.}
|
||||
\par \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Lists}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain 1.\tab}\ilvl0\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
First numbered}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain 2.\tab}\ilvl0\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Second numbered}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain a)\tab}\ilvl1\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Alpha sub one}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain b)\tab}\ilvl1\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Alpha sub two}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain i.\tab}\ilvl2\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Roman sub sub}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain 3.\tab}\ilvl0\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Third numbered}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Interrupting paragraph between lists.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain 4.\tab}\ilvl0\ls2 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Fourth, continuing the count}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain IV.\tab}\ilvl0\ls4 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Roman starting at four}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain V.\tab}\ilvl0\ls4 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Roman five}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \u8226\'95\tab}\ilvl0\ls3 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Bullet one}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \u8226\'95\tab}\ilvl0\ls3 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Bullet two}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \u9702\'3f\tab}\ilvl1\ls3 \fi0\li0\lin0\ql\tx0\fi0\li0\lin0\ltrpar{\loch
|
||||
Nested bullet}
|
||||
\par \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Table}
|
||||
\par \trowd\trql\ltrrow\trpaddft3\trpaddt0\trpaddfl3\trpaddl0\trpaddfb3\trpaddb0\trpaddfr3\trpaddr0\cellx6425\cellx9638\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
Wide head}\cell\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
End}\cell\row\pard \trowd\trql\ltrrow\trpaddft3\trpaddt0\trpaddfl3\trpaddl0\trpaddfb3\trpaddb0\trpaddfr3\trpaddr0\clvmgf\cellx3213\cellx6425\cellx9638\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
Tall}\cell\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
B2}\cell\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
C2}\cell\row\pard \trowd\trql\ltrrow\trpaddft3\trpaddt0\trpaddfl3\trpaddl0\trpaddfb3\trpaddb0\trpaddfr3\trpaddr0\clvmrg\cellx3213\cellx6425\cellx9638\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar\loch
|
||||
\cell\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
B3}\cell\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\intbl\ql\ltrpar{\loch
|
||||
C3}\cell\row\pard \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Notes and special text}
|
||||
\par \ql\fi-340\li340\lin340\noline\ltrpar\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Music clef \uc0 \u55348\uc1 \u56606\'3f appears before this footnote}{{\super \loch \chftn{\*\footnote \chftn\pard\plain \s31\rtlch\afs20 \ltrch\loch\fi-340\li340\lin340\noline\fs20{\loch
|
||||
\tab Footnote after an astral character.}
|
||||
}}
|
||||
}{\loch
|
||||
reference.}
|
||||
\par \ql\fi-340\li340\lin340\noline\ltrpar\pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
An endnote follows here}{{\super \loch \chftn{\*\footnote\ftnalt \chftn\pard\plain \s32\rtlch\afs20 \ltrch\loch\fi-340\li340\lin340\noline\fs20{\loch
|
||||
\tab Endnote body text.}
|
||||
}}
|
||||
}{\loch
|
||||
.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Persian with ZWNJ: }{
|
||||
\u1605\'3f\u1740\'3f\u8204\'3f\u1582\'3f\u1608\'3f\u1575\'3f\u1607\'3f\u1605\'3f}{\loch
|
||||
. Family emoji: \uc0 \u55357\uc1 \u56424\'3f\u8205\'3f\uc0 \u55357\uc1 \u56425\'3f\u8205\'3f\uc0 \u55357\uc1 \u56423\'3f.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Markdown specials: *stars* _under_ [bracket] `tick` #hash 1. dotted | pipe.}
|
||||
\par \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Links and anchors}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
External link to }{{\field{\*\fldinst HYPERLINK "https://example.com/page" }{\fldrslt {\loch\loch\cf9\ul\ulc0\loch
|
||||
example}{}}}\loch
|
||||
.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Relative link to }{{\field{\*\fldinst HYPERLINK "file:///anydoc/tests/fixture-src/sibling.odt" }{\fldrslt {\loch\loch\cf9\ul\ulc0\loch
|
||||
a sibling file}{}}}\loch
|
||||
.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
{\*\bkmkstart plainmark}{\*\bkmkend plainmark}This plain paragraph carries a bookmark.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Jump to }{{\field{\*\fldinst HYPERLINK "#plainmark" }{\fldrslt {\loch\loch\cf9\ul\ulc0\loch
|
||||
the bookmarked paragraph}{}}}\loch
|
||||
.}
|
||||
\par \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Objects}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Inline image: }{\loch
|
||||
{\pict{\*\picprop{\sp{\sn wzDescription}{\sv tiny red dot}}{\sp{\sn wzName}{\sv }}}\picscalex1700\picscaley1700\piccropl0\piccropr0\piccropt0\piccropb0\picw1\pich1\picwgoal10\pichgoal10\pngblip
|
||||
89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000d4944415478da63fccfc0500f000485018084a98c21000000004945
|
||||
4e44ae426082}
|
||||
}{\loch
|
||||
done.}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Text box: }{
|
||||
{\shp{\*\shpinst\shpwr2\shpwrk0\shpbypara\shpbyignore\shptop-1362\shpbottom-228\shpbxcolumn\shpbxignore\shpleft0\shpright2835\shpz1{\sp{\sn shapeType}{\sv 202}}{\sp{\sn dxWrapDistLeft}{\sv 72390}}{\sp{\sn dxWrapDistRight}{\sv 72390}}{\sp{\sn dyWrapDistTop}{\sv 72390}}{\sp{\sn dyWrapDistBottom}{\sv 72390}}{\sp{\sn posrelv}{\sv 2}}{\sp{\sn posv}{\sv 1}}{\sp{\sn posrelh}{\sv 2}}{\sp{\sn posh}{\sv 2}}{\sp{\sn dxTextLeft}{\sv 53975}}{\sp{\sn dyTextTop}{\sv 53975}}{\sp{\sn dxTextRight}{\sv 53975}}{\sp{\sn dyTextBottom}{\sv 53975}}{\sp{\sn lineColor}{\sv 0}}{\sp{\sn lineWidth}{\sv 635}}{\shptxt\s33\ql{\loch
|
||||
Inside the text box.}
|
||||
\par \pard}}}
|
||||
}{\loch
|
||||
after the box.}
|
||||
\par \pard\plain \s2\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\ilvl1\outlinelevel1\nowidctlpar\hyphpar0\ltrpar\fs30\b\cf0\f3\lang2057\kerning1\dbch\af8\langfe2052{\listtext\pard\plain \tab}\ls1 \fi0\li0\lin0\ql\fi0\li0\lin0\ltrpar{\loch
|
||||
Quote and code}
|
||||
\par \pard\plain \s0\rtlch\af6\afs24\alang1081 \ltrch\lang2057\langfe2052\hich\af3\loch\nowidctlpar\hyphpar0\ltrpar\cf0\f3\fs24\lang2057\kerning1\dbch\af8\langfe2052\ql\ltrpar{\loch
|
||||
Value below one millionth: 0.0000004 should survive.}
|
||||
\par }
|
||||
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,225 @@
|
||||
//go:build anydoc && cgo
|
||||
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
upstream "github.com/firecrawl/anydoc/go"
|
||||
)
|
||||
|
||||
func TestConvertEquations(t *testing.T) {
|
||||
for _, format := range []string{"docx", "pptx", "odt", "epub", "rtf"} {
|
||||
t.Run(format, func(t *testing.T) {
|
||||
data := readFixture(t, format+"/handmade-math."+format)
|
||||
for _, assets := range []bool{false, true} {
|
||||
result, err := Convert(data, Options{Format: format, WithAssets: assets})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.AssetsError != nil {
|
||||
t.Fatalf("document ABI failed: %v", result.AssetsError)
|
||||
}
|
||||
if !strings.Contains(result.Markdown, `\frac`) ||
|
||||
!strings.Contains(result.Markdown, "$") {
|
||||
t.Fatalf("equation lost (assets=%v): %s", assets, result.Markdown)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// Check the actual decoded fields, not only the independent Markdown path.
|
||||
doc, err := upstream.ToDocument(readFixture(t, "docx/handmade-math.docx"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var inline, block bool
|
||||
for _, b := range doc.Blocks {
|
||||
block = block || b.Kind == "math" && b.Text != nil && strings.Contains(*b.Text, `\sum`)
|
||||
for _, in := range b.Content {
|
||||
inline = inline || in.Kind == "math" && in.Text != nil && strings.Contains(*in.Text, `\frac`)
|
||||
}
|
||||
}
|
||||
if !inline || !block {
|
||||
t.Fatalf("decoded equation nodes missing: inline=%v block=%v", inline, block)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertPDFReportsScannedPages(t *testing.T) {
|
||||
for _, tc := range []struct{ file, detail string }{
|
||||
{"handmade-mixed.pdf", "page 2 of 2 needs OCR"},
|
||||
{"handmade-scanned.pdf", "all 2 pages need OCR"},
|
||||
} {
|
||||
t.Run(tc.file, func(t *testing.T) {
|
||||
result, err := Convert(readFixture(t, "pdf/"+tc.file), Options{Format: "pdf", WithAssets: true})
|
||||
var typed *upstream.ConvertError
|
||||
if result != nil ||
|
||||
!errors.As(err, &typed) ||
|
||||
typed.Kind != "needs_ocr" ||
|
||||
!strings.Contains(typed.Detail, tc.detail) {
|
||||
t.Fatalf("want scanned-page error, got result=%+v err=%v", result, err)
|
||||
}
|
||||
if !PDFNeedsOCR(fmt.Errorf("reader: %w", err)) {
|
||||
t.Fatal("wrapped typed error did not trigger OCR fallback")
|
||||
}
|
||||
})
|
||||
}
|
||||
if !PDFNeedsOCR(&upstream.ConvertError{Kind: "needs_ocr", Detail: "localized detail"}) {
|
||||
t.Fatal("OCR detection still depends on English wording")
|
||||
}
|
||||
if PDFNeedsOCR(&upstream.ConvertError{Kind: "malformed", Detail: "invalid PDF"}) {
|
||||
t.Fatal("malformed PDF incorrectly triggers OCR")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertSpreadsheetCheckboxes(t *testing.T) {
|
||||
// Add VML form controls to an upstream workbook. Both boolean states must
|
||||
// survive Rust encoding and Go decoding, including subsequent table cells.
|
||||
data := readFixture(t, "xlsx/sheet.xlsx")
|
||||
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
w := zip.NewWriter(&out)
|
||||
for _, f := range reader.File {
|
||||
if f.Name == "xl/worksheets/_rels/sheet1.xml.rels" {
|
||||
continue
|
||||
}
|
||||
if err := w.Copy(f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
parts := map[string]string{
|
||||
"xl/worksheets/_rels/sheet1.xml.rels": `<Relationships ` +
|
||||
`xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><` +
|
||||
`Relationship Id="checkboxes" ` +
|
||||
`Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"` +
|
||||
` Target="../drawings/checkboxes.vml"/></Relationships>`,
|
||||
"xl/drawings/checkboxes.vml": `<xml xmlns:v="urn:schemas-microsoft-com:vml" ` +
|
||||
`xmlns:x="urn:schemas-microsoft-com:office:excel"><v:shape><v:textbox>Approved</v:text` +
|
||||
`box><x:ClientData ObjectType="Checkbox"><x:Anchor>0,0,0,0,1,0,1,0</x:Anchor><x:Checke` +
|
||||
`d>1</x:Checked></x:ClientData></v:shape><v:shape><v:textbox>Pending</v:textbox><x:Cli` +
|
||||
`entData ObjectType="Checkbox"><x:Anchor>1,0,0,0,2,0,1,0</x:Anchor></x:ClientData></v:` +
|
||||
`shape></xml>`,
|
||||
}
|
||||
for name, content := range parts {
|
||||
part, err := w.Create(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write([]byte(content)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := Convert(out.Bytes(), Options{Format: "xlsx", WithAssets: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.AssetsError != nil ||
|
||||
!strings.Contains(result.Markdown, "[x] Approved") ||
|
||||
!strings.Contains(result.Markdown, "[ ] Pending") {
|
||||
t.Fatalf("checkbox conversion: %+v", result)
|
||||
}
|
||||
doc, err := upstream.ToDocument(out.Bytes(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
states := map[bool]bool{}
|
||||
for _, block := range doc.Blocks {
|
||||
if block.Table == nil {
|
||||
continue
|
||||
}
|
||||
for _, row := range block.Table.Grid {
|
||||
for _, slot := range row {
|
||||
if slot.Cell == nil {
|
||||
continue
|
||||
}
|
||||
for _, b := range slot.Cell.Blocks {
|
||||
for _, in := range b.Content {
|
||||
if in.Kind == "checkbox" && in.Checked != nil {
|
||||
states[*in.Checked] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !states[false] || !states[true] {
|
||||
t.Fatalf("decoded checkbox states: %v", states)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertAllBenchmarkFormats(t *testing.T) {
|
||||
for _, name := range benchmarkDocuments {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
format := strings.Split(name, "/")[0]
|
||||
if format == "xls" || format == "xlsb" {
|
||||
format = "xlsx"
|
||||
}
|
||||
result, err := Convert(readFixture(t, name), Options{Format: format, WithAssets: true})
|
||||
if err != nil || result.Markdown == "" || result.AssetsError != nil {
|
||||
t.Fatalf("conversion: result=%+v err=%v", result, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCombinedConversionPreservesModelAndMarkdown(t *testing.T) {
|
||||
// Exercise auto-detection as well as all normal formats, without requiring
|
||||
// an external upstream checkout for this regression in CI.
|
||||
for _, name := range benchmarkDocuments {
|
||||
if strings.HasPrefix(name, "pdf/") {
|
||||
continue
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
data := readFixture(t, name)
|
||||
var format *upstream.Format
|
||||
if strings.HasPrefix(name, "csv/") {
|
||||
f := upstream.FormatCsv
|
||||
format = &f
|
||||
}
|
||||
want, err := upstream.ToDocument(data, format)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantMarkdown, err := upstream.ToMarkdownWithAssetLinks(data, format)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, markdown, err := upstream.ToDocumentWithAssetLinks(data, format)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) || markdown != wantMarkdown {
|
||||
t.Fatal("single parse changed model or Markdown")
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
data []byte
|
||||
format *upstream.Format
|
||||
kind string
|
||||
}{
|
||||
{"empty", nil, nil, "unsupported"},
|
||||
{"garbage", []byte("garbage"), nil, "unsupported"},
|
||||
{"pdf", minimalPDF(), nil, "pdf_no_model"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
doc, md, err := upstream.ToDocumentWithAssetLinks(tc.data, tc.format)
|
||||
var typed *upstream.ConvertError
|
||||
if doc != nil || md != "" || !errors.As(err, &typed) || typed.Kind != tc.kind {
|
||||
t.Fatalf("error contract: doc=%+v md=%q err=%v", doc, md, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user