-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathrest.py
134 lines (118 loc) · 4.17 KB
/
rest.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
from operator import itemgetter
def match(node, tag, attr=None, val=None):
if not isinstance(node, tuple):
return False
if node[0] != tag:
return False
if attr is not None:
attrs = dict(node[1])
if attr not in attrs:
return False
if attrs[attr] != val:
return False
return True
def find(node, tag, attr=None, val=None):
if isinstance(node, tuple):
node = node[2]
if not isinstance(node, list):
msg = "child not found"
raise ValueError(msg)
for child in node:
if match(child, tag, attr, val):
return child
msg = "child not found"
raise ValueError(msg)
children = itemgetter(2)
def text(node):
return children(node)[0]
def _(s):
return s # TODO
def rest(root, f=sys.stdout):
def write(text):
f.write(text)
def show(item, italic=False, bold=False):
if isinstance(item, str):
spc = "" # if item[-1] == '\n' else ' '
fmt = "**" if bold else ("*" if italic else "")
write("%s%s%s%s" % (fmt, item, fmt, spc))
elif match(item, "b"):
for i in children(item):
show(i, italic, True)
elif match(item, "i") or match(item, "em"):
for i in children(item):
show(i, True, bold)
html = find(root, "html")
title = text(find(find(html, "head"), "title"))
rule = "=" * len(title)
write("%s\n" % rule)
write("%s\n" % title)
write("%s\n" % rule)
write("\n")
write(".. figure:: grass_logo.png\n")
write(" :align: center\n")
write(" :alt: GRASS logo\n")
write("\n")
body = find(html, "body")
section = None
for child in children(body):
if match(child, "h2"):
section = text(child)
rule = "-" * len(section)
write("%s\n%s\n" % (section, rule))
elif section == _("NAME"):
if match(child, "em"):
name = text(find(child, "b"))
write("**%s**" % name)
section = "desc"
elif section == "desc" and isinstance(child, str) and child[:4] == " - ":
write(" - ")
write(child[4:])
if child[-1] != "\n":
write("\n")
write("\n")
section = None
elif section == _("KEYWORDS"):
write(child.strip())
write("\n\n")
section = None
elif section == _("SYNOPSIS"):
if match(child, "div", "id", "name"):
name = text(find(child, "b"))
write("**%s**\n\n" % name)
write("**%s** help\n\n" % name)
elif match(child, "div", "id", "synopsis"):
for item in children(child):
show(item)
write("\n")
elif match(child, "div", "id", "flags"):
header = text(find(child, "h3"))
rule = "=" * len(header)
write("%s\n%s\n" % (header, rule))
flags = find(child, "dl")
for flag in children(flags):
if match(flag, "dt"):
item = text(find(flag, "b"))
write("**%s**\n" % item)
elif match(flag, "dd"):
write(" %s\n" % text(flag))
write("\n\n")
elif match(child, "div", "id", "parameters"):
header = text(find(child, "h3"))
rule = "=" * len(header)
write("%s\n%s\n" % (header, rule))
params = find(child, "dl")
for param in children(params):
if match(param, "dt"):
name = text(children(param)[0])
write("**%s** = " % name)
for item in children(param)[2:]:
show(item)
write("\n\n")
elif match(param, "dd"):
write("\t")
for item in children(param):
show(item)
write("\n\n")
write("\n")
return