aboutsummaryrefslogtreecommitdiff
path: root/examples/spans.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2023-09-03 23:02:01 +0200
committerMartin Fischer <martin@push-f.com>2023-09-03 23:02:01 +0200
commit0c590e90564870fa8600460c866c7395b7d865cd (patch)
treed702e200ed5b815a32b6bbd3cf7c6e8c5c370c9c /examples/spans.rs
parent6e6bbcd053c6114a9fa75052b09e701eaa2f3465 (diff)
docs: add spans example
Diffstat (limited to 'examples/spans.rs')
-rw-r--r--examples/spans.rs34
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();
+}