Skip to main content

rich_rs/
export_format.rs

1//! Export format templates for SVG and HTML.
2//!
3//! This module contains the template strings used to generate SVG and HTML
4//! output from recorded console content.
5
6/// SVG format template for console export.
7///
8/// This template uses the following placeholder variables:
9///
10/// - `{unique_id}` - Unique identifier for CSS classes and element IDs
11/// - `{width}` - Total SVG width including margins
12/// - `{height}` - Total SVG height including margins
13/// - `{char_width}` - Character width in pixels
14/// - `{char_height}` - Character height in pixels
15/// - `{line_height}` - Line height in pixels
16/// - `{terminal_width}` - Terminal content width (excluding padding)
17/// - `{terminal_height}` - Terminal content height (excluding padding)
18/// - `{terminal_x}` - X offset for terminal content
19/// - `{terminal_y}` - Y offset for terminal content
20/// - `{styles}` - CSS style rules for text styling
21/// - `{chrome}` - Terminal window chrome (background, title, buttons)
22/// - `{backgrounds}` - Background rectangles for styled text
23/// - `{matrix}` - Text content elements
24/// - `{lines}` - Clip path definitions for each line
25pub const CONSOLE_SVG_FORMAT: &str = r#"<svg class="rich-terminal" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">
26    <!-- Generated with Rich-rs https://github.com/Textualize/rich -->
27    <style>
28
29    @font-face {
30        font-family: "Fira Code";
31        src: local("FiraCode-Regular"),
32                url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
33                url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
34        font-style: normal;
35        font-weight: 400;
36    }
37    @font-face {
38        font-family: "Fira Code";
39        src: local("FiraCode-Bold"),
40                url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
41                url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
42        font-style: bold;
43        font-weight: 700;
44    }
45
46    .{unique_id}-matrix {
47        font-family: Fira Code, monospace;
48        font-size: {char_height}px;
49        line-height: {line_height}px;
50        font-variant-east-asian: full-width;
51    }
52
53    .{unique_id}-title {
54        font-size: 18px;
55        font-weight: bold;
56        font-family: arial;
57    }
58
59    {styles}
60    </style>
61
62    <defs>
63    <clipPath id="{unique_id}-clip-terminal">
64      <rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />
65    </clipPath>
66    {lines}
67    </defs>
68
69    {chrome}
70    <g transform="translate({terminal_x}, {terminal_y})" clip-path="url(#{unique_id}-clip-terminal)">
71    {backgrounds}
72    <g class="{unique_id}-matrix">
73    {matrix}
74    </g>
75    </g>
76</svg>
77"#;
78
79/// HTML format template for console export.
80///
81/// This template uses the following placeholder variables:
82///
83/// - `{stylesheet}` - Additional CSS styles
84/// - `{foreground}` - Foreground color (CSS color value)
85/// - `{background}` - Background color (CSS color value)
86/// - `{code}` - HTML-encoded console content
87pub const CONSOLE_HTML_FORMAT: &str = r#"<!DOCTYPE html>
88<html>
89<head>
90<meta charset="UTF-8">
91<style>
92{stylesheet}
93body {
94    color: {foreground};
95    background-color: {background};
96}
97</style>
98</head>
99<body>
100    <pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><code style="font-family:inherit">{code}</code></pre>
101</body>
102</html>
103"#;
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_svg_format_contains_placeholders() {
111        assert!(CONSOLE_SVG_FORMAT.contains("{unique_id}"));
112        assert!(CONSOLE_SVG_FORMAT.contains("{width}"));
113        assert!(CONSOLE_SVG_FORMAT.contains("{height}"));
114        assert!(CONSOLE_SVG_FORMAT.contains("{styles}"));
115        assert!(CONSOLE_SVG_FORMAT.contains("{chrome}"));
116        assert!(CONSOLE_SVG_FORMAT.contains("{matrix}"));
117    }
118
119    #[test]
120    fn test_html_format_contains_placeholders() {
121        assert!(CONSOLE_HTML_FORMAT.contains("{stylesheet}"));
122        assert!(CONSOLE_HTML_FORMAT.contains("{foreground}"));
123        assert!(CONSOLE_HTML_FORMAT.contains("{background}"));
124        assert!(CONSOLE_HTML_FORMAT.contains("{code}"));
125    }
126}