diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-04-20 14:23:26 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-04-20 14:23:36 +0200 |
| commit | c117e2dc276d19b022abccac0d252bd7fc1cd44e (patch) | |
| tree | 10884ac2ecd4d01053b2d38856c395e2e115775c /docs/src/html.rs | |
| parent | 2a682f0008b91e7c33c6e65b3ecfc690268ab405 (diff) | |
List contributors in changelog
Co-Authored-By: Martin Haug <mhaug@live.de>
Diffstat (limited to 'docs/src/html.rs')
| -rw-r--r-- | docs/src/html.rs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/docs/src/html.rs b/docs/src/html.rs index b189a09c..d3566b07 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -1,3 +1,5 @@ +use std::ops::Range; + use comemo::Prehashed; use md::escape::escape_html; use pulldown_cmark as md; @@ -108,16 +110,21 @@ impl<'a> Handler<'a> { // Rewrite HTML images. md::Event::Html(html) if html.starts_with("<img") => { - let needle = "src=\""; - let offset = html.find(needle).unwrap() + needle.len(); - let len = html[offset..].find('"').unwrap(); - let range = offset..offset + len; + let range = html_attr_range(html, "src").unwrap(); let path = &html[range.clone()]; let mut buf = html.to_string(); buf.replace_range(range, &self.handle_image(path)); *html = buf.into(); } + // Rewrite contributor sectinos. + md::Event::Html(html) if html.starts_with("<contributors") => { + let from = html_attr(html, "from").unwrap(); + let to = html_attr(html, "to").unwrap(); + let Some(output) = contributors(from, to) else { return false }; + *html = output.raw.into(); + } + // Rewrite links. md::Event::Start(md::Tag::Link(ty, dest, _)) => { assert!( @@ -327,6 +334,19 @@ fn code_block(resolver: &dyn Resolver, lang: &str, text: &str) -> Html { resolver.example(highlighted, &frames) } +/// Extract an attribute value from an HTML element. +fn html_attr<'a>(html: &'a str, attr: &str) -> Option<&'a str> { + html.get(html_attr_range(html, attr)?) +} + +/// Extract the range of the attribute value of an HTML element. +fn html_attr_range(html: &str, attr: &str) -> Option<Range<usize>> { + let needle = format!("{attr}=\""); + let offset = html.find(&needle)? + needle.len(); + let len = html[offset..].find('"')?; + Some(offset..offset + len) +} + /// World for example compilations. struct DocWorld(Source); |
