Expand description
Typeset books from Rust with Postext: a
print-ready PDF, HTML pages, an EPUB, page images, a Word document or an
InDesign package from a .postext book, a book folder or loose Markdown
files.
let report = postext::pdf("book.postext").out("book.pdf").set("page.dpi", 300).run()?;
println!("{} pages", report.output().and_then(|o| o.pages).unwrap_or(0));
for warning in &report.warnings {
eprintln!("{warning}"); // warning missingFont (chapters/02.md:14): …
}
let pdf: Vec<u8> = postext::pdf(std::fs::read("book.postext")?).bytes()?; // bytes in, bytes outThe crate drives the self-contained postext command line, the same
executable as brew install drnachio/postext/postext, npx postext-cli
or pip install postext: the layout is the Sandbox’s, and the same book
breaks into the same pages. The executable of your system (185 to 230 MB,
too large for a crate) is downloaded on first use from the GitHub Release
of the crate’s version, checked against the release’s SHA256SUMS and
kept in the cache (see find_binary). Nothing is downloaded at build
time.
| Environment variable | Effect |
|---|---|
POSTEXT_BIN | Run this executable instead (Homebrew, Scoop, a local build); nothing is downloaded |
POSTEXT_CACHE_DIR | Cache folder (default ~/.cache/postext, %LOCALAPPDATA%\postext\cache on Windows) |
POSTEXT_RELEASE_URL | Download from a mirror: a URL with {version} where the release’s files are |
§Commands
Every command of the command line is a builder, made by a function of
the same name (pdf, html, epub, image, images,
docx, idml, pack, unpack, design, info,
check, build, import_docx, import_idml). Its options are
methods; Command::run runs it with --json and returns its report,
Command::bytes returns the file instead.
A book is a path (a .postext file, a book folder, a Markdown file), a
list of Markdown files (the chapters, in order), or the bytes of a
.postext bundle. The commands that read a book take the book options:
use serde_json::json;
postext::pdf(["01.md", "02.md"])
.out("notes.pdf")
.name("Notes") // --name
.config_json(&json!({"page": {"sizePreset": "A5"}})) // --config (or .config("config.json"))
.set("bodyText.fontSize.value", 11) // --set, one per key
.locale("es") // --locale
.chapters(1..=2) // --chapters 1-2 (also 3, "1,3-5", [1, 3])
.resources("img/") // --resources
.fonts("fonts/") // --fonts
.font_dir("/Library/Fonts") // --font-dir
.offline(true) // --offline: never download fonts
.design("novel.postext") // --design: a design file (a path or bytes), or another book
.design_parts(["page", "text"]) // --design-parts page,text
.composition("auto") // --composition
.run()?;§Errors
A failed run is an Error: Error::Failed (exit code 1),
Error::Usage (a wrong option or value, exit code 2),
Error::ProblemsFound (check found an error, or import_docx could
not apply an edit, exit code 3; allow_problems() returns the report
instead), Error::BinaryNotFound (no executable for this system, a
failed download or checksum, a POSTEXT_BIN that is not a file). The
report and stderr of a failed run are in Error::report and
Failure::stderr.
§One executable, many runs
A Postext client has the same methods with its own executable,
folder, environment and timeout. Postext::run_inherit runs any
command as typed, build … --watch included. Each call starts the
executable again.
§Features
download(default): download the executable on first use. Without it, the crate runsPOSTEXT_BINor an executable already in the cache.
Modules§
- kind
- The commands, as types:
Command<'_, kind::Pdf>builds apdfrun.
Structs§
- Book
Summary - The book a command read.
chaptersis the number of chapters laid out (InfoReport::chapterslists them instead). - Books
- One book, or several Markdown files (the chapters, in order).
- Chapter
Info - A chapter
infolists. - Chapters
- Chapters to lay out, from 1:
3,"1,3-5",1..=3,[1, 3]. - Check
Report check: whether the book passed, its error and warning counts, and the contrast, reflow and preflight results as the command gives them.- Command
- A command being built: its book,
outand options, run byrun(orbytes). - Design
Report design: the design file’snameand thepartsit took (page,text,headings…).- Diagnostic
- A warning, error or note the command reported.
- Download
- Downloads the executable of a version, checks its SHA256 against the
release’s
SHA256SUMSand moves it into place. - Failure
- What a run that failed left behind: the message, the exit code, the
parsed
--jsonreport when the command printed one, and what it wrote to stderr. - Font
Info - A font family the book uses: where its faces come from (
embedded,book,font-dir,google,missing…) and its variants (400,400i,700…). - Image
Report image: the page that was rendered.- Import
Docx Report import_docx. Into a book: theeditsapplied, in conflict or rejected, thecomments, thefileschanged and the unifieddiff. As a new book: the number ofchaptersand the document’squalityreport.- Import
Idml Report import_idml: whether the book was set as composed pages or as a flowing book (layout:pagesorflow) and why (reason), the number ofchapters, the importer’squalityreport, thefontsthe document names, the linked pictures not found (missing_links) and, for a zipped InDesign package,package.- Info
Report info: the book’s chapters, resources by kind and fonts.- Output
- A file or folder the command wrote (
pathis"-"for stdout). - PageRef
- A page: its printed label (
"iv","12"), its position from 1 and its chapter. - Postext
- Runs one postext executable.
- Report
- What a command did: its outputs, warnings, timings and page count.
- Unpack
Report unpack: how many files the bundle held.
Enums§
- Book
- A book: a
.postextfile, a book folder or a Markdown file, or the bytes of a.postextbundle (written to a temporary file for the run). - Error
- Everything that can go wrong running postext.
Constants§
- ENV_BIN
- Names an executable to run instead of the cached one; nothing is downloaded.
- ENV_
CACHE - The cache folder (fonts, and the executables in
bin/<version>/). - ENV_
RELEASE_ URL - A mirror of the releases: a URL with
{version}where a release’s files are. - FILES
- The executables each release publishes.
- RELEASE_
URL - Where the releases are,
{version}being the crate’s version. - STDOUT
out("-"): the command writes to stdout and the bytes come back inReport::content.- VERSION
- The crate’s version, which is the version of the executable it runs.
Traits§
- From
Report - A report a command returns:
Report, or one of the reports with fields of their own, which dereference toReport. - Kind
- A command of the command line. The types in
kindname them;Commandis built for one. - Reads
Book - A command that reads a book and takes the book options.
- Takes
Fonts - A command that takes
fonts: font files bundled with loose Markdown. - Writes
File - A command that writes one file, and so can return its bytes.
Functions§
- build
Postext::buildwith the default client.- cache_
dir - The folder postext keeps downloads in (fonts, and here the executables).
- cached_
binary - Where the executable of
versionis kept once downloaded. - check
Postext::checkwith the default client.- client
- The client the functions at the crate’s root use.
- design
Postext::designwith the default client.- docx
Postext::docxwith the default client.- epub
Postext::epubwith the default client.- find_
binary - The postext executable to run:
POSTEXT_BIN, else the cached executable of the crate’s version, downloaded first if needed (with thedownloadfeature). - find_
binary_ version find_binaryfor another version. Withdownloadfalse, a missing executable is an error instead of a download;quietleaves out the line the download prints to stderr.- html
Postext::htmlwith the default client.- idml
Postext::idmlwith the default client.- image
Postext::imagewith the default client.- images
Postext::imageswith the default client.- import_
docx Postext::import_docxwith the default client.- import_
idml Postext::import_idmlwith the default client.- info
Postext::infowith the default client.- is_musl
- Whether this Linux runs on musl (Alpine) rather than glibc.
- pack
Postext::packwith the default client.- parse_
sums SHA256SUMS(<hex> <file>per line) as{file: hex}.Postext::pdfwith the default client.- platform_
file - The name of this system’s executable on the GitHub Release.
- platform_
file_ for - The name of the executable for
osandarch(as instd::env::consts), and musl on Linux. An error for a system no release builds for. - run_
inherit - Run the executable with
argsas given, its output on the terminal (Postext::run_inheritwith the default client). - unpack
Postext::unpackwith the default client.
Type Aliases§
- Result
Result<T, postext::Error>.