-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
76 lines (54 loc) · 1.76 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""CLI Application
Cat a pandoc json string.
"""
from __future__ import annotations
import argparse
import re
from io import StringIO
import pypandoc
from rich.console import Console
from rich.markdown import Markdown
def pandoc2ansi(file: str, width: int = 79) -> str:
string = StringIO()
console = Console(file=string, color_system="truecolor", safe_box=False, width=width)
pypandoc.ensure_pandoc_installed()
markdown = Markdown(pypandoc.convert_file(file, "md"))
console.print(markdown)
return string.getvalue()
def stripAnsi(string: str) -> str:
"""Strip ansi codes from a given string.
Args:
----
string (str): string to strip codes from
Returns:
-------
str: plaintext, utf-8 string (safe for writing to files)
"""
return re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])").sub("", string)
def pandoc2plain(file: str, width: int = 79) -> str:
return stripAnsi(pandoc2ansi(file, width))
def handle(args: argparse.Namespace) -> None:
"""Handle the args and output to the terminal.
Args:
----
args (argparse.Namespace): Args
"""
# Open file and convert to JSON
try:
pypandoc.convert_text("#test", "json", format="md") # type: ignore
except OSError:
pypandoc.download_pandoc() # type: ignore
width = args.width or 79
# Print to console
functions = pandoc2ansi, pandoc2plain
print(functions[args.to_plain](args.file, width))
def cli() -> None:
"""Parse args from the command line."""
parser = argparse.ArgumentParser(description="Print md")
parser.add_argument("file", help="file to render")
parser.add_argument("--width", help="terminal width", action="store", type=int)
parser.add_argument("--to-plain", help="convert to plaintext", action="store_true")
args = parser.parse_args()
handle(args)
if __name__ == "__main__":
cli()