Method: rich.console.Console.export_html
Calls: 3, Exceptions: 0, Paths: 3Back
Path 1: 1 calls (0.33)
None (1)
True (1)
None (1)
False (1)
'\n
\n\n\n\n...
1def export_html(
2 self,
3 *,
4 theme: Optional[TerminalTheme] = None,
5 clear: bool = True,
6 code_format: Optional[str] = None,
7 inline_styles: bool = False,
8 ) -> str:
9 """Generate HTML from console contents (requires record=True argument in constructor).
10
11 Args:
12 theme (TerminalTheme, optional): TerminalTheme object containing console colors.
13 clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``.
14 code_format (str, optional): Format string to render HTML. In addition to '{foreground}',
15 '{background}', and '{code}', should contain '{stylesheet}' if inline_styles is ``False``.
16 inline_styles (bool, optional): If ``True`` styles will be inlined in to spans, which makes files
17 larger but easier to cut and paste markup. If ``False``, styles will be embedded in a style tag.
18 Defaults to False.
19
20 Returns:
21 str: String containing console contents as HTML.
22 """
23 assert (
24 self.record
25 ), "To export console contents set record=True in the constructor or instance"
26 fragments: List[str] = []
27 append = fragments.append
28 _theme = theme or DEFAULT_TERMINAL_THEME
29 stylesheet = ""
30
31 render_code_format = CONSOLE_HTML_FORMAT if code_format is None else code_format
32
33 with self._record_buffer_lock:
34 if inline_styles:
35 for text, style, _ in Segment.filter_control(
36 Segment.simplify(self._record_buffer)
37 ):
38 text = escape(text)
39 if style:
40 rule = style.get_html_style(_theme)
41 if style.link:
42 text = f'<a href="{style.link}">{text}</a>'
43 text = f'<span style="{rule}">{text}</span>' if rule else text
44 append(text)
45 else:
46 styles: Dict[str, int] = {}
47 for text, style, _ in Segment.filter_control(
48 Segment.simplify(self._record_buffer)
49 ):
50 text = escape(text)
51 if style:
52 rule = style.get_html_style(_theme)
53 style_number = styles.setdefault(rule, len(styles) + 1)
54 if style.link:
55 text = f'<a class="r{style_number}" href="{style.link}">{text}</a>'
56 else:
57 text = f'<span class="r{style_number}">{text}</span>'
58 append(text)
59 stylesheet_rules: List[str] = []
60 stylesheet_append = stylesheet_rules.append
61 for style_rule, style_number in styles.items():
62 if style_rule:
63 stylesheet_append(f".r{style_number} {{{style_rule}}}")
64 stylesheet = "\n".join(stylesheet_rules)
65
66 rendered_code = render_code_format.format(
67 code="".join(fragments),
68 stylesheet=stylesheet,
69 foreground=_theme.foreground_color.hex,
70 background=_theme.background_color.hex,
71 )
72 if clear:
73 del self._record_buffer[:]
74 return rendered_code
Path 3: 1 calls (0.33)
None (1)
True (1)
'\n
\n\n\n\n...
1def export_html(
2 self,
3 *,
4 theme: Optional[TerminalTheme] = None,
5 clear: bool = True,
6 code_format: Optional[str] = None,
7 inline_styles: bool = False,
8 ) -> str:
9 """Generate HTML from console contents (requires record=True argument in constructor).
10
11 Args:
12 theme (TerminalTheme, optional): TerminalTheme object containing console colors.
13 clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``.
14 code_format (str, optional): Format string to render HTML. In addition to '{foreground}',
15 '{background}', and '{code}', should contain '{stylesheet}' if inline_styles is ``False``.
16 inline_styles (bool, optional): If ``True`` styles will be inlined in to spans, which makes files
17 larger but easier to cut and paste markup. If ``False``, styles will be embedded in a style tag.
18 Defaults to False.
19
20 Returns:
21 str: String containing console contents as HTML.
22 """
23 assert (
24 self.record
25 ), "To export console contents set record=True in the constructor or instance"
26 fragments: List[str] = []
27 append = fragments.append
28 _theme = theme or DEFAULT_TERMINAL_THEME
29 stylesheet = ""
30
31 render_code_format = CONSOLE_HTML_FORMAT if code_format is None else code_format
32
33 with self._record_buffer_lock:
34 if inline_styles:
35 for text, style, _ in Segment.filter_control(
36 Segment.simplify(self._record_buffer)
37 ):
38 text = escape(text)
39 if style:
40 rule = style.get_html_style(_theme)
41 if style.link:
42 text = f'<a href="{style.link}">{text}</a>'
43 text = f'<span style="{rule}">{text}</span>' if rule else text
44 append(text)
45 else:
46 styles: Dict[str, int] = {}
47 for text, style, _ in Segment.filter_control(
48 Segment.simplify(self._record_buffer)
49 ):
50 text = escape(text)
51 if style:
52 rule = style.get_html_style(_theme)
53 style_number = styles.setdefault(rule, len(styles) + 1)
54 if style.link:
55 text = f'<a class="r{style_number}" href="{style.link}">{text}</a>'
56 else:
57 text = f'<span class="r{style_number}">{text}</span>'
58 append(text)
59 stylesheet_rules: List[str] = []
60 stylesheet_append = stylesheet_rules.append
61 for style_rule, style_number in styles.items():
62 if style_rule:
63 stylesheet_append(f".r{style_number} {{{style_rule}}}")
64 stylesheet = "\n".join(stylesheet_rules)
65
66 rendered_code = render_code_format.format(
67 code="".join(fragments),
68 stylesheet=stylesheet,
69 foreground=_theme.foreground_color.hex,
70 background=_theme.background_color.hex,
71 )
72 if clear:
73 del self._record_buffer[:]
74 return rendered_code