-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto-archive.py
executable file
·84 lines (66 loc) · 2.6 KB
/
to-archive.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
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import os
import argparse
import docutils
import pathlib
import docutils.nodes
import docutils.parsers.rst
import docutils.utils
class Visitor(docutils.nodes.NodeVisitor):
def __init__(self, doc, debug):
super().__init__(doc)
self.debug = debug
def visit_reference(self, node: docutils.nodes.Node) -> None:
if self.debug:
print("--> reference: ", node.pformat())
print(node.children)
print(node.parent.children)
def depart_reference(self, node: docutils.nodes.Node) -> None:
if self.debug:
print("<--- reference")
def visit_Text(self, node: docutils.nodes.Node) -> None:
node_text = node.astext()
if "http" in node_text:
# look for markdown; if we hit this, we've gotta look at siblings too
if "](" in node_text:
all_sibs = node.parent.children
this_index = all_sibs.index(node)
left, right = all_sibs[this_index - 1], all_sibs[this_index + 1]
print(f"markdown link nodes: {left}{node_text}{right}")
else:
print(f"raw link node: {node}")
def unknown_visit(self, node: docutils.nodes.Node) -> None:
visit_method = node.__class__.__name__
if self.debug:
try:
print(f"self.{visit_method}")
except AttributeError:
print(node)
def parse_rst(text: str) -> docutils.nodes.document:
parser = docutils.parsers.rst.Parser()
components = (docutils.parsers.rst.Parser,)
settings = docutils.frontend.OptionParser(
components=components
).get_default_values()
document = docutils.utils.new_document("<rst-doc>", settings=settings)
parser.parse(text, document)
return document
def fix_markup(path: pathlib.Path, debug: bool = False) -> None:
print(path)
doc = parse_rst(path.read_text(encoding="utf-8"))
visitor = Visitor(doc, debug)
doc.walk(visitor)
if __name__ == "__main__":
parser = argparse.ArgumentParser("fixer")
parser.add_argument("--debug", action="store_true", default=False)
parser.add_argument("--basedir", "-d", default="content")
parser.add_argument("single_file", nargs="?")
opts = parser.parse_args()
if opts.single_file:
fix_markup(pathlib.Path(opts.single_file), opts.debug)
else:
for dirpath, dirnames, filenames in os.walk(opts.base_dir):
for name in filenames:
if not name.endswith(".rst"):
continue
fix_markup(pathlib.Path(dirpath) / name, opts.debug)