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/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
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"),
})
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 ErrUnsupportedFormat = errors.New("downmark: unsupported format")
ErrUnsupportedFormat is returned when no registered converter accepted the input under any detected interpretation of its type.
Functions ¶
This section is empty.
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.
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) 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 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 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, DOCX, XLSX, PPTX, HTML, CSV, plain text) to Markdown.
|
Command downmark converts documents (PDF, DOCX, XLSX, PPTX, HTML, CSV, 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. |
|
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 (scripts and styles 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 (scripts and styles 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. |
|
internal
|
|
|
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. |
|
mdutil
Package mdutil holds small shared Markdown emission helpers.
|
Package mdutil holds small shared Markdown emission helpers. |
|
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. |