Skip to main content

Crate postext

Crate postext 

Source
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 out

The 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 variableEffect
POSTEXT_BINRun this executable instead (Homebrew, Scoop, a local build); nothing is downloaded
POSTEXT_CACHE_DIRCache folder (default ~/.cache/postext, %LOCALAPPDATA%\postext\cache on Windows)
POSTEXT_RELEASE_URLDownload 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 runs POSTEXT_BIN or an executable already in the cache.

Modules§

kind
The commands, as types: Command<'_, kind::Pdf> builds a pdf run.

Structs§

BookSummary
The book a command read. chapters is the number of chapters laid out (InfoReport::chapters lists them instead).
Books
One book, or several Markdown files (the chapters, in order).
ChapterInfo
A chapter info lists.
Chapters
Chapters to lay out, from 1: 3, "1,3-5", 1..=3, [1, 3].
CheckReport
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, out and options, run by run (or bytes).
DesignReport
design: the design file’s name and the parts it 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 SHA256SUMS and moves it into place.
Failure
What a run that failed left behind: the message, the exit code, the parsed --json report when the command printed one, and what it wrote to stderr.
FontInfo
A font family the book uses: where its faces come from (embedded, book, font-dir, google, missing…) and its variants (400, 400i, 700…).
ImageReport
image: the page that was rendered.
ImportDocxReport
import_docx. Into a book: the edits applied, in conflict or rejected, the comments, the files changed and the unified diff. As a new book: the number of chapters and the document’s quality report.
ImportIdmlReport
import_idml: whether the book was set as composed pages or as a flowing book (layout: pages or flow) and why (reason), the number of chapters, the importer’s quality report, the fonts the document names, the linked pictures not found (missing_links) and, for a zipped InDesign package, package.
InfoReport
info: the book’s chapters, resources by kind and fonts.
Output
A file or folder the command wrote (path is "-" 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.
UnpackReport
unpack: how many files the bundle held.

Enums§

Book
A book: a .postext file, a book folder or a Markdown file, or the bytes of a .postext bundle (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 in Report::content.
VERSION
The crate’s version, which is the version of the executable it runs.

Traits§

FromReport
A report a command returns: Report, or one of the reports with fields of their own, which dereference to Report.
Kind
A command of the command line. The types in kind name them; Command is built for one.
ReadsBook
A command that reads a book and takes the book options.
TakesFonts
A command that takes fonts: font files bundled with loose Markdown.
WritesFile
A command that writes one file, and so can return its bytes.

Functions§

build
Postext::build with the default client.
cache_dir
The folder postext keeps downloads in (fonts, and here the executables).
cached_binary
Where the executable of version is kept once downloaded.
check
Postext::check with the default client.
client
The client the functions at the crate’s root use.
design
Postext::design with the default client.
docx
Postext::docx with the default client.
epub
Postext::epub with 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 the download feature).
find_binary_version
find_binary for another version. With download false, a missing executable is an error instead of a download; quiet leaves out the line the download prints to stderr.
html
Postext::html with the default client.
idml
Postext::idml with the default client.
image
Postext::image with the default client.
images
Postext::images with the default client.
import_docx
Postext::import_docx with the default client.
import_idml
Postext::import_idml with the default client.
info
Postext::info with the default client.
is_musl
Whether this Linux runs on musl (Alpine) rather than glibc.
pack
Postext::pack with the default client.
parse_sums
SHA256SUMS (<hex> <file> per line) as {file: hex}.
pdf
Postext::pdf with 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 os and arch (as in std::env::consts), and musl on Linux. An error for a system no release builds for.
run_inherit
Run the executable with args as given, its output on the terminal (Postext::run_inherit with the default client).
unpack
Postext::unpack with the default client.

Type Aliases§

Result
Result<T, postext::Error>.