scrivener-rtf
A pure Rust RTF (Rich Text Format) parser and generator, optimized for Scrivener workflows.
Features
- Parse RTF documents into an Abstract Syntax Tree (AST) representation
- Generate RTF from the AST
- Full Unicode support including surrogate pairs for emoji
- Extract document properties (fonts, colors, styles)
- Zero unsafe code - memory safe by design
- No external dependencies on system RTF libraries
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Parsing RTF
use ;
// Parse RTF from bytes
let doc = parse.unwrap;
// Parse RTF from string
let doc = parse_str.unwrap;
// Parse RTF from file
let doc = parse_file.unwrap;
// Access document structure
for group in &doc.groups
// Access document properties
println!;
println!;
Generating RTF
use ;
let doc = Document ;
let rtf = doc.to_rtf;
println!; // {\\rtf1 \\ansi Hello, World!}
Writing to File
use File;
use ;
let doc = Document ;
let mut output = create.unwrap;
doc.write_to.unwrap;
API Overview
Main Types
| Type | Description |
|---|---|
Document |
Root node of a parsed RTF file |
Group |
A { ... } group in RTF |
Content |
Content within a group (text, control words, nested groups) |
DocumentProperties |
Extracted header properties |
Font |
Font table entry |
Color |
Color table entry |
Style |
Stylesheet entry |
Parsing Functions
| Function | Description |
|---|---|
parse |
Parse RTF bytes into a Document |
parse_str |
Parse RTF from a string |
parse_file |
Parse an RTF file from disk |
Generation Methods
| Method | Description |
|---|---|
Document::to_rtf() |
Convert document to RTF string |
Document::write_to() |
Write document to any Write implementation |
Supported RTF Features
- ✅ Group structure (
{ ... }) - ✅ Control words with parameters (
\fs24,\cf1) - ✅ Control symbols (
\~,\-,\_) - ✅ Destination groups (
\*\generator) - ✅ Font table (
\fonttbl) - ✅ Color table (
\colortbl) - ✅ Stylesheet (
\stylesheet) - ✅ Unicode escapes (
\uN) - ✅ Surrogate pairs for emoji
Scrivener Compatibility
This library is designed to work with RTF files produced by Scrivener, handling:
- Scrivener-specific destination groups
- Unicode content in annotations and notes
- Style definitions used in Scrivener projects
Documentation
Full API documentation is available at docs.rs/scrivener-rtf.
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.