Skip to content

Commit 90425dd

Browse files
Add new HtmlFragments type and new Term::render_html_fragments method
1 parent 2b78e5f commit 90425dd

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
@@ -318,6 +318,40 @@ impl Term {
318318
}
319319
writeln!(buffer, r#" </div>"#).unwrap();
320320
}
321+
322+
/// Returns the various parts needed to create an HTML page.
323+
pub fn render_html_fragments(&self, ansi: &str) -> HtmlFragments {
324+
let mut styled = adapter::AnsiBytes::new();
325+
let mut elements = styled.extract_next(ansi.as_bytes()).collect::<Vec<_>>();
326+
preprocess_invert_style(&mut elements, self.bg_color, self.fg_color);
327+
328+
let styled_lines = split_lines(&elements);
329+
330+
let mut style = String::new();
331+
let mut body = String::new();
332+
333+
self.render_classes(&mut style, &elements);
334+
self.render_content(&mut body, styled_lines);
335+
HtmlFragments { style, body }
336+
}
337+
}
338+
339+
/// Contains the different parts of a HTML rendered page.
340+
pub struct HtmlFragments {
341+
style: String,
342+
body: String,
343+
}
344+
345+
impl HtmlFragments {
346+
/// Content that can be used directly in a `<style>` tag.
347+
pub fn style(&self) -> &str {
348+
&self.style
349+
}
350+
351+
/// Content that can be put in the HTML body or any tag inside the `<body>`.
352+
pub fn body(&self) -> &str {
353+
&self.body
354+
}
321355
}
322356

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

0 commit comments

Comments
 (0)