rtf-parser
A Rust RTF parser & lexer library designed for speed and memory efficiency.
The library is split into 2 main components:
- The lexer
- The parser
The lexer scan the document and return a Vec<Token>
which represent the RTF file in a code-understandable manner.
To use it :
use ;
let tokens: = scan;
These tokens can then be passed to the parser to transcript it to a real document : RtfDocument
.
let parser = new;
let doc: RtfDocument = parser.parse;
An RtfDocument
is composed with :
- the header, containing among others the font table and the encoding.
- the body, which is a
Vec<StyledBlock>
A StyledBlock
contains all the information about the formatting of a specific block of text.
It contains a Painter
and the text (&str
).
The Painter
is defined below, and the rendering implementation depends on the user. For now, it only supports font, bold, italic and underline.
The parser could also return the text without any formatting information, with the to_text()
method.
let rtf = r#"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard Voici du texte en {\b gras}.\par}"#;
let tokens = scan;
let text = new.to_text;
assert_eq!;
Examples
A complete example of rtf parsing is presented below :
use ;
let rtf_text = r#"{ \rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard Voici du texte en {\b gras}.\par }"#;
let tokens = scan;
let doc = new.parse;
assert_eq!;
assert_eq!;