diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/spans.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/spans.rs b/examples/spans.rs new file mode 100644 index 0000000..fc3c6a1 --- /dev/null +++ b/examples/spans.rs @@ -0,0 +1,34 @@ +use codespan_reporting::{ + diagnostic::{Diagnostic, Label}, + files::SimpleFiles, + term, + term::termcolor::{ColorChoice, StandardStream}, +}; +use html5tokenizer::{offset::PosTrackingReader, NaiveParser, Token}; + +fn main() { + let html = r#"<img src=example.jpg alt="some description">"#; + let parser = NaiveParser::new(PosTrackingReader::new(html)); + + let Token::StartTag(tag) = parser.flatten().next().unwrap() else { + panic!() + }; + + let mut files = SimpleFiles::new(); + let file_id = files.add("file.html", html); + + let mut labels = Vec::new(); + + labels.push(Label::primary(file_id, tag.name_span).with_message("tag name")); + + for attr in &tag.attributes { + labels.push(Label::primary(file_id, attr.name_span()).with_message("attr name")); + labels.push(Label::primary(file_id, attr.value_span().unwrap()).with_message("attr value")); + } + + let diagnostic = Diagnostic::note().with_labels(labels); + + let mut writer = StandardStream::stdout(ColorChoice::Never); + let config = codespan_reporting::term::Config::default(); + term::emit(&mut writer, &config, &files, &diagnostic).unwrap(); +} |