Skip to content

Commit 3670a5a

Browse files
Add new HtmlFragments type and new Term::render_html_fragments method
1 parent 1e6d164 commit 3670a5a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/anstyle-svg/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,40 @@ impl Term {
316316
}
317317
writeln!(buffer, r#" </div>"#).unwrap();
318318
}
319+
320+
/// Returns the various parts needed to create an HTML page.
321+
pub fn render_html_fragments(&self, ansi: &str) -> HtmlFragments {
322+
let mut styled = adapter::AnsiBytes::new();
323+
let mut elements = styled.extract_next(ansi.as_bytes()).collect::<Vec<_>>();
324+
preprocess_invert_style(&mut elements, self.bg_color, self.fg_color);
325+
326+
let styled_lines = split_lines(&elements);
327+
328+
let mut style = String::new();
329+
let mut body = String::new();
330+
331+
self.render_classes(&mut style, &elements);
332+
self.render_content(&mut body, styled_lines);
333+
HtmlFragments { style, body }
334+
}
335+
}
336+
337+
/// Contains the different parts of a HTML rendered page.
338+
pub struct HtmlFragments {
339+
style: String,
340+
body: String,
341+
}
342+
343+
impl HtmlFragments {
344+
/// Content that can be used directly in a `<style>` tag.
345+
pub fn style(&self) -> &str {
346+
&self.style
347+
}
348+
349+
/// Content that can be put in the HTML body or any tag inside the `<body>`.
350+
pub fn body(&self) -> &str {
351+
&self.body
352+
}
319353
}
320354

321355
const FG_COLOR: anstyle::Color = anstyle::Color::Ansi(anstyle::AnsiColor::White);

0 commit comments

Comments
 (0)