3 unstable releases

0.4.1 Nov 20, 2024
0.4.0 Nov 1, 2020
0.3.0 Dec 26, 2019

#851 in Text processing

Download history 79/week @ 2024-08-26 79/week @ 2024-09-02 84/week @ 2024-09-09 46/week @ 2024-09-16 730/week @ 2024-09-23 695/week @ 2024-09-30 514/week @ 2024-10-07 565/week @ 2024-10-14 284/week @ 2024-10-21 490/week @ 2024-10-28 362/week @ 2024-11-04 658/week @ 2024-11-11 473/week @ 2024-11-18 439/week @ 2024-11-25 417/week @ 2024-12-02 513/week @ 2024-12-09

1,870 downloads per month
Used in 13 crates (6 directly)

MIT/Apache

37KB
868 lines

document_tree

Part of the rst crate family. This crate contains structs and traits mirroring Docutils’ Document Tree model. The basic structure is a tree of elements, some of which have children and/or extra attributes.

use document_tree::*;
use document_tree::{extra_attributes as a, element_categories as c, attribute_types as t};

#[test]
fn imperative() {
    let mut doc = Document::default();
    let mut title = Title::default();
    let url = "https://example.com/image.jpg".parse().unwrap();
    let image = ImageInline::with_extra(a::ImageInline::new(url));
    title.append_child("Hi");
    title.append_child(image);
    doc.append_child(title);
    println!("{:?}", doc);
}

#[test]
fn descriptive() {
    let doc = Document::with_children(vec![
        Title::with_children(vec![
            "Hi".into(),
            ImageInline::with_extra(a::ImageInline::new(
                "https://example.com/image.jpg".parse().unwrap()
            )).into(),
        ]).into()
    ]);
    println!("{:?}", doc);
}

Check out the other crates in the family on how to create one from rST markup or render it!

The advantages of this approach are that it’s convenient to have the children interface, as well as to trivially map elements to XML. The disadvantage is that a “vector of children” is not a well-defined model for the more structured elements like e.g. a section, which always contains a title followed by blocks.

Dependencies

~4.5–6.5MB
~111K SLoC