Skip to main content

Crate scrivener_rtf

Crate scrivener_rtf 

Source
Expand description

§scrivener-rtf

A pure Rust RTF (Rich Text Format) parser and generator, optimized for Scrivener workflows.

§Features

  • Parse RTF documents into an AST representation
  • Generate RTF from the AST
  • Full Unicode support (including surrogate pairs for emoji)
  • Extract document properties (fonts, colors, styles)

§Example

use scrivener_rtf::{parse, parse_str, parse_file};

// Parse RTF from bytes
let doc = parse(b"{\\rtf1 Hello, World!}").unwrap();

// Parse RTF from string
let doc = parse_str("{\\rtf1 Hello}").unwrap();

// Parse RTF from file
// let doc = parse_file("document.rtf").unwrap();

§Generated RTF

The AST can be converted back to RTF:

use scrivener_rtf::{Document, Group, Content, DocumentProperties};

let doc = Document {
    groups: vec![Group {
        content: vec![
            Content::ControlWord("rtf".into(), Some(1)),
            Content::Text("Hello".into()),
        ],
        is_destination: false,
    }],
    properties: DocumentProperties::default(),
};
let rtf = doc.to_rtf();
assert!(rtf.starts_with("{\\rtf1"));

Re-exports§

pub use ast::CharacterSet;
pub use ast::Color;
pub use ast::Content;
pub use ast::Document;
pub use ast::DocumentProperties;
pub use ast::Font;
pub use ast::FontFamily;
pub use ast::Group;
pub use ast::Style;
pub use ast::StyleType;
pub use error::Result;
pub use error::RtfError;
pub use token::Token;

Modules§

ast
AST types for RTF documents.
error
Error types for RTF parsing and generation.
token

Functions§

parse
Parse RTF bytes into a Document.
parse_file
Read and parse an RTF file from disk.
parse_str
Parse RTF from a string.