Crates.io | syntax-tree |
lib.rs | syntax-tree |
version | 0.3.2 |
source | src |
created_at | 2020-03-19 23:53:16.923898 |
updated_at | 2020-03-24 14:31:37.24616 |
description | Tree structure managing syntax/format information for text |
homepage | |
repository | https://github.com/bennyboer/syntax-tree |
max_upload_size | |
id | 220586 |
size | 44,520 |
syntax-tree
... is a tree structure managing syntax/format information for text. It can be used to build the backing model for a WYSIWYG editor or to do syntax highlighting.
The below example code and console output is taken from the example app under example/fmt/main.rs
.
println!("# Create new tree with text 'Hello World'");
let mut tree: Tree<FontStyle> = Tree::new("Hello World", None);
println!("{:#?}", tree);
println!("# Format 'o W' underlined");
tree.set(4, 7, FontStyle::Underline);
println!("{:#?}", tree);
println!("# Format 'World' bold");
tree.set(6, "Hello World".len(), FontStyle::Bold);
println!("{:#?}", tree);
println!("# Format 'Wor' underlined");
tree.set(6, 9, FontStyle::Underline);
println!("{:#?}", tree);
println!("# Remove 'o '");
tree.remove(4, 2);
println!("{:#?}", tree);
println!("# Remove format underlined from every node in range 'HellW'");
tree.unset(0, 5, FontStyle::Underline);
println!("{:#?}", tree);
println!("# Format 'ellW' italic");
tree.set(1, 5, FontStyle::Italic);
println!("{:#?}", tree);
println!("# Could be rendered to HTML like this:");
println!("{}", to_html(&tree));
The output should be:
# Create new tree with text 'Hello World'
|-- 'Hello World' []
# Format 'o W' underlined
|-- 'Hello World' []
|-- 'Hell' []
|-- 'o W' [Underline]
|-- 'orld' []
# Format 'World' bold
|-- 'Hello World' []
|-- 'Hell' []
|-- 'o W' [Underline]
|-- 'o ' []
|-- 'W' [Bold]
|-- 'orld' [Bold]
# Format 'Wor' underlined
|-- 'Hello World' []
|-- 'Hell' []
|-- 'o W' [Underline]
|-- 'o ' []
|-- 'W' [Bold]
|-- 'orld' [Bold]
# Remove 'o '
|-- 'HellWorld' []
|-- 'Hell' []
|-- 'World' [Bold]
|-- 'Wor' [Underline]
|-- 'ld' []
# Remove format underlined from every node in range 'HellW'
|-- 'HellWorld' []
|-- 'Hell' []
|-- 'World' [Bold]
|-- 'W' []
|-- 'or' [Underline]
|-- 'ld' []
# Format 'ellW' italic
|-- 'HellWorld' []
|-- 'H' []
|-- 'ell' [Italic]
|-- 'World' [Bold]
|-- 'W' [Italic]
|-- 'or' [Underline]
|-- 'ld' []
# Could be rendered to HTML like this:
<p>H<em>ell</em><strong><em>W</em><u>or</u>ld</strong></p>
The above example HTML rendering would look like this when rendered in a browser:
HellWorld