Skip to main content

Module writer

Module writer 

Source
Expand description

XML/HTML output sink — port of LaTeXML::Post::Writer.

Two related concerns live here:

  1. The Writer post-processor (last in the chain) that serializes a PostDocument to its destination, handling DOCTYPE removal, TEMPORARY_DOCUMENT_ID cleanup, and HTML vs XML serialization.
  2. Free-standing helpers (write_output, ensure_parent_dir) used by binary main()s that already have the serialized string in hand (post-processing returns a String) and need to route it to a destination path or stdout. Replaces the duplicated File::create + write! + ensure_parent_dir boilerplate that used to live in latexml_oxide.rs (and the now-retired latexmlpost_oxide.rs).

Companion module: crate::pack (the LaTeXML::Post::Pack analog) handles archive bundling when the destination is a zip.

Structs§

MadeDirs
The directories a page render already made: a split run writes thousands of pages into a handful of directories, and each create_dir_all is a mkdir and a statx (Nasser’s 71,476-page document, #777 FAQ).
Staged
A private file beside a destination that parallel render workers may share (the CSS, a figure every page uses): written or converted there, then installed by one rename, so no reader sees a partial file and no writer touches another’s. Named .<stem>.tmp.<pid>.<seq>.<ext> (the stem bounded), its extension kept last for converters that read the format from it; registered with the watchdog’s exit sweep, and removed when dropped — after an install too, since a rename onto a hard link of the same file succeeds and leaves both names (POSIX rename(2); a destination an older binary hard-linked to a graphics-cache entry). A name some file already holds — a killed run’s leftover, perhaps a link to a cache entry, or a process with the same pid in another PID namespace sharing the cache — is skipped, never written through.
Writer
Writer post-processor: serializes document to file.

Enums§

OutputFormat
Output format for the writer.

Functions§

bounded_name
name, or for a longer one its first 64 bytes and a hash of the whole: every name derived from a destination (its staged file, a stage of that, a converter’s temp) then stays under the 255-byte NAME_MAX, which a figure name of 220 characters or more took them past.
copy_atomic
Atomically copy src to dest through a Staged file. Copying a file onto itself is refused: it would only rewrite it, and a caller that then changes dest would change its source. The copy is dest’s own: a hard link let an edit of one graphics output in place rewrite the cache entry and every other document’s copy (GFXHARDLINK).
ensure_parent_dir
Ensure the parent directory of path exists, creating it (and any missing ancestors) as needed. No-op when path has no parent or the parent is the current directory.
made_dir_count
How many directories MadeDirs::ensure_parent asked the OS for on this thread (a test seam).
needs_write
Whether a bundled resource’s embedded bytes are written to dest: only where it is missing or an empty regular file (#312’s truncated leftover). A non-empty file in place is used as-is, a user’s or an older release’s; a directory still goes to the write and its error.
same_bytes
Whether a and b are regular files holding the same bytes.
same_file
Whether a and b name the same existing file (by canonical path). A copy onto itself truncates the file: the destination directory on the resource search path made a stale resource its own only match (#312); a plain != on two spellings (./, relative) misses it.
write_atomic
Atomically write bytes to dest through a Staged file.
write_output
Write the serialized output content to dest if Some, else to stdout. Creates parent directories as needed.
write_output_segments
Like write_output but writes several segments back-to-back without concatenating them into one buffer first. Used for the conversion log, where the core and post-phase segments are each already-allocated and large for real articles — a format!("{core}{post}") would allocate a third copy of their combined size on the conversion hot path. Segments are written verbatim and in order through a single BufWriter (one file open/truncate); insert any separators (e.g. "\n") as their own segments.