Documentation
¶
Overview ¶
Package downmark converts documents to Markdown.
This package is the core: the Engine, converter registry, content-type detection, and a plain-text converter. Format converters live in separate packages so binaries link only the formats they use:
- github.com/giraffesyo/downmark/convert/pdf
- github.com/giraffesyo/downmark/convert/doc
- github.com/giraffesyo/downmark/convert/docx
- github.com/giraffesyo/downmark/convert/xlsx
- github.com/giraffesyo/downmark/convert/pptx
- github.com/giraffesyo/downmark/convert/html
- github.com/giraffesyo/downmark/convert/csv
- github.com/giraffesyo/downmark/convert/zipfile
The github.com/giraffesyo/downmark/all package wires every converter into one engine for consumers who want the full matrix. Standalone PDF text extraction lives in the separate github.com/giraffesyo/pdf module.
Picking formats ¶
e := downmark.New() // engine with plain-text passthrough only
pdf.Register(e) // + PDF
docx.Register(e, docx.Options{}) // + DOCX
res, err := e.ConvertFile(ctx, "report.pdf")
if err != nil { ... }
fmt.Println(res.Markdown)
Streams work too; pass whatever type hints you have and detection fills in the rest by sniffing content:
res, err := e.Convert(ctx, resp.Body, downmark.StreamInfo{
MIMEType: resp.Header.Get("Content-Type"),
})
Derive the context with WithResultLimit to reject oversized Markdown before the engine performs final normalization. Converters can inspect ResultLimit to enforce the same budget while constructing their result. Converters with hard compressed-input budgets can also implement InputLimitConverter so non-seekable streams are bounded while the engine buffers them.
Custom converters ¶
Implement Converter and register it; among converters with equal priority the most recently registered wins, so user converters shadow builtins. StreamInfo.Matches is the standard Accepts building block. A converter may also implement interface{ Name() string } to control how it is reported in errors.
Errors ¶
When no converter accepts an input, errors.Is(err, ErrUnsupportedFormat) reports true. When converters accepted but all failed, the error is a *ConversionError carrying each attempt.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInputTooLarge = errors.New("downmark: input exceeds size limit")
ErrInputTooLarge is returned when a converter's hard input budget is exceeded, including while the engine buffers a non-seekable stream.
var ErrResultTooLarge = errors.New("downmark: result exceeds size limit")
ErrResultTooLarge is returned when a conversion result exceeds the maximum size carried by its context via WithResultLimit.
var ErrUnsupportedFormat = errors.New("downmark: unsupported format")
ErrUnsupportedFormat is returned when no registered converter accepted the input under any detected interpretation of its type.
Functions ¶
func ResultLimit ¶ added in v0.2.0
ResultLimit reports the maximum accepted Markdown result size carried by ctx. Converters may use it to avoid constructing a result that the engine will reject.
func WithResultLimit ¶ added in v0.2.0
WithResultLimit returns a derived context that limits accepted Markdown results to maxBytes. A child context cannot loosen an existing limit. It panics if ctx is nil or maxBytes is not positive.
Types ¶
type AttemptError ¶
type AttemptError struct {
// Converter is the name of the converter that failed, e.g. "pdf".
Converter string
// Err is the error the converter returned.
Err error
}
AttemptError records a single converter's failed conversion attempt.
func (*AttemptError) Error ¶
func (e *AttemptError) Error() string
func (*AttemptError) Unwrap ¶
func (e *AttemptError) Unwrap() error
type ConversionError ¶
type ConversionError struct {
Attempts []*AttemptError
}
ConversionError is returned when at least one converter accepted the input but every attempt failed. Attempts lists each failure in the order tried.
func (*ConversionError) Error ¶
func (e *ConversionError) Error() string
func (*ConversionError) Unwrap ¶
func (e *ConversionError) Unwrap() []error
type Converter ¶
type Converter interface {
// Accepts reports whether the converter wants to try this stream.
Accepts(info StreamInfo) bool
// Convert reads input (positioned at offset 0) and produces Markdown.
// Implementations that can construct large results must consult
// ResultLimit(ctx) and stop before allocating output the engine will reject.
Convert(ctx context.Context, input io.ReadSeeker, info StreamInfo) (*Result, error)
}
Converter converts one class of documents to Markdown.
Implementations must judge Accepts from the StreamInfo alone, never by reading the stream; content sniffing happens in the Engine's detection layer before Accepts is called.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine holds a converter registry and conversion options. Create one with New; the zero value is not usable.
func New ¶
New returns an Engine with only the builtin plain-text converter registered (unless WithoutBuiltins is given). Register format converters explicitly from the convert/... packages, or use the all package for an engine with every format wired up.
func (*Engine) CanConvert ¶ added in v0.4.0
func (e *Engine) CanConvert(hints StreamInfo) bool
CanConvert reports whether a registered format-specific converter would accept input described by hints, judged from the hints alone (no stream is read). Catch-all fallback converters are excluded, so this means "a dedicated converter claims this format" rather than "Convert would produce something"; callers routing files can use it to send only real documents through Convert and handle plain text themselves. An empty Extension is derived from Filename. Because no content is sniffed, a mislabeled file that Convert would still classify by its bytes can read as false here.
func (*Engine) Convert ¶
Convert converts an arbitrary reader to Markdown. hints may be the zero value. If r is not an io.ReadSeeker its content is buffered into memory; seekable inputs are rewound to offset 0.
func (*Engine) ConvertFile ¶
ConvertFile opens path and converts it, deriving Extension, Filename, and LocalPath from the path.
type InputLimitConverter ¶ added in v0.2.0
type InputLimitConverter interface {
InputLimit() int64
}
InputLimitConverter is optionally implemented by converters with a hard compressed-input budget. The engine applies the smallest matching limit while buffering a non-seekable input, before conversion begins.
type Option ¶
type Option func(*Engine)
Option configures an Engine created by New.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used for URL-based conversions.
func WithoutBuiltins ¶
func WithoutBuiltins() Option
WithoutBuiltins starts the Engine with an empty converter registry (omitting even the plain-text converter). Use Register to add converters.
type Priority ¶
type Priority int
Priority orders converters: lower values are tried first.
const ( // PrioritySpecific is for converters keyed to a specific file format // (doc, docx, xlsx, pptx, pdf, csv). PrioritySpecific Priority = 0 // PriorityArchive is for archive walkers, which must run only after // converters for formats that use an archive as their container (such as // docx, xlsx, and pptx) have declined the input. PriorityArchive Priority = 50 // PriorityGeneric is for near-catch-all converters (html, plain text). PriorityGeneric Priority = 100 )
type Result ¶
type Result struct {
// Markdown is the normalized Markdown output.
Markdown string
// Title is the document title if the format provides one, else "".
Title string
}
Result is the outcome of a successful conversion.
type StreamInfo ¶
type StreamInfo struct {
// MIMEType is the media type without parameters, e.g. "application/pdf".
MIMEType string
// Extension is lowercase with a leading dot, e.g. ".docx".
Extension string
// Charset is an IANA charset name, e.g. "utf-8" or "shift_jis".
Charset string
// Filename is the base name of the source, if known.
Filename string
// LocalPath is the path of the source file, if the input came from disk.
LocalPath string
// URL is the source URL, if the input came from the network.
URL string
}
StreamInfo carries what is known or guessed about an input stream. The zero value means "nothing known"; every field is optional.
func (StreamInfo) Matches ¶
func (s StreamInfo) Matches(exts []string, mimePrefixes []string) bool
Matches reports whether the stream's extension is one of exts (lowercase, with leading dot) or its MIME type starts with any of mimePrefixes. It is the standard building block for Converter.Accepts implementations.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package all wires every downmark converter into one engine, for consumers who want the full format matrix and don't mind linking every converter's dependencies.
|
Package all wires every downmark converter into one engine, for consumers who want the full format matrix and don't mind linking every converter's dependencies. |
|
cmd
|
|
|
downmark
command
Command downmark converts documents (PDF, DOC, DOCX, XLSX, PPTX, HTML, CSV, ZIP, and plain text) to Markdown.
|
Command downmark converts documents (PDF, DOC, DOCX, XLSX, PPTX, HTML, CSV, ZIP, and plain text) to Markdown. |
|
convert
|
|
|
csv
Package csv converts delimiter-separated values to Markdown tables for the downmark engine.
|
Package csv converts delimiter-separated values to Markdown tables for the downmark engine. |
|
doc
Package doc converts Word 97-2003 binary documents to Markdown for the downmark engine.
|
Package doc converts Word 97-2003 binary documents to Markdown for the downmark engine. |
|
docx
Package docx converts Word documents to Markdown for the downmark engine: headings, emphasis, lists, tables, hyperlinks, images, and tracked changes, via an intermediate-HTML stage shared with the HTML converter.
|
Package docx converts Word documents to Markdown for the downmark engine: headings, emphasis, lists, tables, hyperlinks, images, and tracked changes, via an intermediate-HTML stage shared with the HTML converter. |
|
html
Package html converts HTML documents to Markdown for the downmark engine, sanitizing the DOM first (hidden and inert content dropped, unsafe link schemes unwrapped, data URIs truncated unless kept).
|
Package html converts HTML documents to Markdown for the downmark engine, sanitizing the DOM first (hidden and inert content dropped, unsafe link schemes unwrapped, data URIs truncated unless kept). |
|
pdf
Package pdf converts PDF documents to Markdown for the downmark engine.
|
Package pdf converts PDF documents to Markdown for the downmark engine. |
|
pptx
Package pptx converts PowerPoint presentations to Markdown for the downmark engine: slides in order with position-based reading order, tables, chart data, image alt text, and speaker notes.
|
Package pptx converts PowerPoint presentations to Markdown for the downmark engine: slides in order with position-based reading order, tables, chart data, image alt text, and speaker notes. |
|
xlsx
Package xlsx converts Excel workbooks to Markdown for the downmark engine: each sheet becomes a heading plus a table.
|
Package xlsx converts Excel workbooks to Markdown for the downmark engine: each sheet becomes a heading plus a table. |
|
zipfile
Package zipfile walks ZIP archives and converts supported members to Markdown through the downmark engine that registered it.
|
Package zipfile walks ZIP archives and converts supported members to Markdown through the downmark engine that registered it. |
|
internal
|
|
|
ctxio
Package ctxio adapts readers so blocked conversion loops observe context cancellation between reads.
|
Package ctxio adapts readers so blocked conversion loops observe context cancellation between reads. |
|
docx
Package docx extracts content from Word (.docx) files by parsing the OOXML parts directly and emitting intermediate HTML, which the caller converts to Markdown via the shared htmlmd backend (mammoth-style two-stage conversion).
|
Package docx extracts content from Word (.docx) files by parsing the OOXML parts directly and emitting intermediate HTML, which the caller converts to Markdown via the shared htmlmd backend (mammoth-style two-stage conversion). |
|
htmlmd
Package htmlmd is the shared HTML-to-Markdown backend used by the HTML and DOCX converters.
|
Package htmlmd is the shared HTML-to-Markdown backend used by the HTML and DOCX converters. |
|
legacydoc
Package legacydoc extracts text from Word 97-2003 binary (.doc) files.
|
Package legacydoc extracts text from Word 97-2003 binary (.doc) files. |
|
limitbuf
Package limitbuf provides a sticky-error string builder with a hard byte limit.
|
Package limitbuf provides a sticky-error string builder with a hard byte limit. |
|
mdutil
Package mdutil holds small shared Markdown emission helpers.
|
Package mdutil holds small shared Markdown emission helpers. |
|
ooxml
Package ooxml provides shared archive hardening for Office Open XML converters.
|
Package ooxml provides shared archive hardening for Office Open XML converters. |
|
pptx
Package pptx extracts Markdown from PowerPoint (.pptx) files by parsing the OOXML parts directly (archive/zip + encoding/xml).
|
Package pptx extracts Markdown from PowerPoint (.pptx) files by parsing the OOXML parts directly (archive/zip + encoding/xml). |
|
readerat
Package readerat adapts seekable streams to random-access readers for converters whose underlying formats (zip, pdf) need io.ReaderAt.
|
Package readerat adapts seekable streams to random-access readers for converters whose underlying formats (zip, pdf) need io.ReaderAt. |
|
textenc
Package textenc detects and decodes legacy text encodings to UTF-8.
|
Package textenc detects and decodes legacy text encodings to UTF-8. |
|
ziputil
Package ziputil contains bounded ZIP metadata helpers shared by archive and OOXML converters.
|
Package ziputil contains bounded ZIP metadata helpers shared by archive and OOXML converters. |