Debian Codegraph @

rich_rst/__main__.py

raw symbols SCIP
1 import argparse
2 import sys
3 from rich.console import Console
4 from rich_rst import RestructuredText
5 from rich.terminal_theme import TerminalTheme
6
7 def rgb(r, g, b):
8 """
9 Function to represent color in RGB format.
10
11 Parameters
12 ----------
13 r : int
14 Red color value.
15 g : int
16 Green color value.
17 b : int
18 Blue color value.
19
20 Returns
21 -------
22 tuple
23 A tuple representing the RGB color.
24 """
25 return (r, g, b)
26
27 def parse_arguments():
28 """Parse command line arguments."""
29 parser = argparse.ArgumentParser(description="Render reStructuredText to the console with rich-rst")
30 parser.add_argument("path", metavar="PATH", help="path to file, or - for stdin")
31 parser.add_argument("-c", "--force-color", dest="force_color", action="store_true", default=None, help="force color for non-terminals")
32 parser.add_argument("-e", "--encoding", dest="encoding", type=str, default="utf-8", help="encoding for file (default: utf-8)")
33 parser.add_argument("-w", "--width", type=int, dest="width", default=None, help="width of output (default will auto-detect)")
34 parser.add_argument("-hw", "--html-width", type=str, dest="html_width", default="1675px", help="width of html output (default: 1675px)")
35 parser.add_argument("-t", "--code-theme", dest="code_theme", type=str, default="monokai", help="pygments code theme")
36 parser.add_argument("-html", "--save-html", type=str, dest="html_filename", default=False, help="save to html")
37 parser.add_argument("-r", "--wrap", dest="word_wrap", action="store_true", default=False, help="word wrap long lines")
38 parser.add_argument("-s", "--soft-wrap", action="store_true", dest="soft_wrap", default=False, help="enable soft wrapping mode")
39 parser.add_argument("-gl", "--guess-lexer", action="store_true", dest="guess_lexer", default=False, help="Whether to guess the lexer for code blocks without specified language")
40 parser.add_argument("-dl", "--default-lexer", type=str, dest="default_lexer", default="python", help="The default lexer for code blocks without specified language if no lexer could be guessed or found")
41 parser.add_argument("-he", "--hide-errors", action="store_true", dest="hide_errors", default=False, help="Whether to hide errors or not")
42 return parser.parse_args()
43
44 def main():
45 """The main function."""
46 args = parse_arguments()
47 DRACULA_TERMINAL_THEME = TerminalTheme(
48 rgb(40, 42, 54),
49 rgb(248, 248, 242),
50 [
51 rgb(40, 42, 54),
52 rgb(255, 85, 85),
53 rgb(80, 250, 123),
54 rgb(241, 250, 140),
55 rgb(189, 147, 249),
56 rgb(255, 121, 198),
57 rgb(139, 233, 253),
58 rgb(255, 255, 255),
59 ],
60 [
61 rgb(40, 42, 54),
62 rgb(255, 85, 85),
63 rgb(80, 250, 123),
64 rgb(241, 250, 140),
65 rgb(189, 147, 249),
66 rgb(255, 121, 198),
67 rgb(139, 233, 253),
68 rgb(255, 255, 255),
69 ],
70 )
71 CONSOLE_HTML_FORMAT = f"""\
72 <!DOCTYPE html>
73 <head>
74 <meta charset="UTF-8">
75 <style>
76 {{stylesheet}}
77 body {{
78 color: {{foreground}};
79 background-color: {{background}};
80 max-width: {args.html_width}
81 }}
82 pre {{
83 white-space: pre-wrap; /* Since CSS 2.1 */
84 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
85 white-space: -pre-wrap; /* Opera 4-6 */
86 white-space: -o-pre-wrap; /* Opera 7 */
87 word-wrap: break-word; /* Internet Explorer 5.5+ */
88 }}
89 ::-moz-selection {{ /* Code for Firefox */
90 background: #44475a;
91 }}
92 ::selection {{
93 background: #44475a;
94 }}
95 </style>
96 </head>
97 <html>
98 <body>
99 <code>
100 <pre style="font-family:ui-monospace,'Fira Code',Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">{{code}}</pre>
101 </code>
102 </body>
103 </html>
104 """
105 console = Console(force_terminal=args.force_color, width=args.width, record=bool(args.html_filename))
106 code = sys.stdin.read() if args.path == "-" else open(args.path, "rt", encoding=args.encoding).read()
107 rst = RestructuredText(
108 code,
109 code_theme=args.code_theme,
110 guess_lexer=args.guess_lexer,
111 default_lexer=args.default_lexer,
112 show_errors=not args.hide_errors,
113 filename=args.path if args.path != "-" else "<stdin>",
114 )
115 console.print(rst, soft_wrap=args.soft_wrap)
116 if args.html_filename:
117 console.save_html(args.html_filename, theme=DRACULA_TERMINAL_THEME, code_format=CONSOLE_HTML_FORMAT)
118
119 if __name__ == "__main__":
120 main()