rtf-parser-tt
An RTF parser that reads the text, formatting and structure of RTF documents. Fork of rtf-parser by @d0rianb.
Why rtf-parser-tt
rtf-parser-tt began as a fork of rtf-parser to preserve special characters such as em dashes and smart quotes. That support was merged into rtf-parser and released in version 0.4.3. rtf-parser-tt is maintained independently, with a focus on reliable handling of real-world documents. Its lexer follows the RTF 1.9.1 specification, and the parser decodes code pages, separates paragraphs and table cells, places footnotes, headers and footers, and reads document properties. Malformed input returns an error instead of causing a panic.
Support
If you find this crate useful, consider supporting development:
Installation
[]
= "0.7"
Usage
use RtfDocument;
Files and code pages
RtfDocument::from_filepath and RtfDocument::from_bytes accept a document in any code page. UTF-8 input is read as is; any other input is decoded with the code page the document declares.
use RtfDocument;
Plain text
get_text returns the body text followed by footnotes, headers and footers, in the order Word uses when it saves a document as plain text, and omits hidden text. get_text_with selects what the text includes: hidden text, footnotes, the numbers at the start of footnotes, and headers and footers.
use ;
Document structure
RtfDocument::body is a list of StyleBlock values. Each block holds a run of text with its character formatting (Painter), its paragraph properties (Paragraph) and the part of the document it belongs to (Part). A footnote's number in the body is Part::NoteReference(n); the footnote itself is Part::NoteNumber(n) for its number and Part::Note(n) for its text.
use ;
Special Characters and Breaks
| Control Word | Unicode | Character |
|---|---|---|
\emdash |
U+2014 | — |
\endash |
U+2013 | – |
\bullet |
U+2022 | • |
\lquote |
U+2018 | ‘ |
\rquote |
U+2019 | ’ |
\ldblquote |
U+201C | “ |
\rdblquote |
U+201D | ” |
\~ |
U+00A0 | (no-break space) |
\- |
U+00AD | (soft hyphen) |
\_ |
U+2011 | (non-breaking hyphen) |
\tab, \cell |
U+0009 | (tab) |
\line, \par, \sect, \page, \row |
U+000A | (newline) |
Features
| Feature | Default | Description |
|---|---|---|
encoding_rs |
Enabled | Decodes the Japanese (932), Simplified Chinese (936), Korean (949) and Traditional Chinese (950) code pages, KOI8-R (20866), KOI8-U (21866), ISO 8859 (28592 to 28606) and Mac Cyrillic (10007) through encoding_rs. Without it, the four East Asian code pages are read as Latin-1 and the others as Windows-1252. |
jsbindings |
Disabled | WebAssembly bindings through wasm-bindgen. |
To build without encoding_rs:
[]
= { = "0.7", = false }
API
Key types:
RtfDocument- Parsed document: header, document properties and bodyStyleBlock- A run of text with its formatting, paragraph properties and part of the documentPart- Body text, footnote reference, footnote number, footnote text, header or footerTextOptions- Whatget_text_withincludesPainter- Character formatting (bold, italic, hidden, etc.)Paragraph- Paragraph properties (alignment, spacing, indents)DocumentInfo- Title, author and other document propertiesRtfError- Error returned for malformed inputLexer- Tokenizes RTF inputParser- Converts tokens to a document
Changes from 0.5 that affect existing code are listed in the changelog.
License
MIT License - same as upstream.
Credits
- Original crate: rtf-parser by @d0rianb
- Fork maintained by TwelveTake Studios