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 docuemnt and return a Vec<Token>
which represent the RTF file in a code-understandable manner.
To use it :
use Lexer;
let tokens = scan;
These tokens can then be passed to the parser to transcript it to a real document : RtfDocument
.
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.
The parser could also return the text without any formatting information, with the to_text()
method.
Example
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!;