Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
107 lines (94 loc) · 2.64 KB
/
Copy pathcli.py
File metadata and controls
107 lines (94 loc) · 2.64 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
CLI tool: convert a .mu Micron file to HTML or plain text.
Usage:
python -m micron2html.cli input.mu > output.html
python -m micron2html.cli input.mu -o output.html
cat page.mu | python -m micron2html.cli -
python -m micron2html.cli input.mu --format text
"""
import argparse
import os
import sys
from .converter import MicronConverter
_HTML_TEMPLATE = """\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<style>
{css}
</style>
</head>
<body>
{body}
</body>
</html>
"""
def _bundled_css() -> str:
"""Read the bundled dark-terminal stylesheet that ships with the package."""
css_path = os.path.join(os.path.dirname(__file__), "micron-meshchat.css")
try:
with open(css_path, "r", encoding="utf-8") as fh:
return fh.read()
except OSError:
return ""
def main(argv=None):
parser = argparse.ArgumentParser(
description="Convert Micron (.mu) markup to HTML or plain text"
)
parser.add_argument(
"input",
metavar="FILE",
help="Input .mu file (use - for stdin)",
)
parser.add_argument(
"-o", "--output",
metavar="FILE",
default=None,
help="Output file (default: stdout)",
)
parser.add_argument(
"-f", "--format",
choices=["html", "text"],
default="html",
help="Output format (default: html)",
)
parser.add_argument(
"--fragment",
action="store_true",
help="HTML mode only: emit a fragment instead of a full <html> page",
)
parser.add_argument(
"--node-hash",
default="",
metavar="HASH",
help="Node destination hash (used to build internal link hrefs)",
)
args = parser.parse_args(argv)
if args.input == "-":
text = sys.stdin.read()
title = "Micron Page"
else:
with open(args.input, "r", encoding="utf-8") as fh:
text = fh.read()
title = os.path.basename(args.input)
converter = MicronConverter()
if args.format == "text":
output = converter.to_text(text)
if not output.endswith("\n"):
output += "\n"
else:
body = converter.convert(text, node_hash=args.node_hash)
if args.fragment:
output = body
else:
output = _HTML_TEMPLATE.format(title=title, css=_bundled_css(), body=body)
if args.output:
with open(args.output, "w", encoding="utf-8") as fh:
fh.write(output)
else:
sys.stdout.write(output)
if __name__ == "__main__":
main()