-
Notifications
You must be signed in to change notification settings - Fork 0
/
undoc.py
executable file
·153 lines (119 loc) · 4.31 KB
/
undoc.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
# Copyright 2014 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
# Pulls test cases out of code fences in .md/.rs files
import os
import sys
def translate_test_case(out_file_name, markdown_tags, lines):
is_definitely_rust = True
useless_tags = [
'ignore', 'console', 'text', 'notation',
'gram', 'ebnf', 'sh', 'precedence',
'field', 'tuple', 'literals',
'linked-failure', 'c', 'md', 'keyword',
'cpp', 'notrust', 'bash', 'ruby',
'toml', 'shell', 'txt', 'should-fail',
'js', 'javascript', 'html', 'make',
'json', 'py'
]
# TODO: Do stuff with these
usefull_tags = [ 'no_run', 'test_harness', 'should_panic' ]
# Configure translation from tags
for tag in markdown_tags:
if tag == 'rust':
pass
elif tag in useless_tags:
is_definitely_rust = False
elif tag in usefull_tags:
is_definitely_rust = False
else:
# idk!
print "NEW TAG DISCOVERED! [" + tag + "]"
is_definitely_rust = False
# Configure translation from source
has_main = False
for line in lines:
if "fn main" in line:
has_main = True
if not is_definitely_rust:
return
if os.path.exists(out_file_name):
print "%s exists!" % out_file_name
return
with open(out_file_name, 'w') as f:
if not has_main:
f.write("fn main() {\n")
for line in lines:
line = line.rstrip()
if line.startswith("# "):
line = line[2:len(line)]
if not has_main:
# Indent to make pretty
f.write(" ")
f.write(line + "\n")
if not has_main:
f.write("}\n")
def undoc(filename, outdir, test_name):
if not os.path.isfile(filename):
print "'%s' is not a file" % filename
return False
if not os.path.isdir(outdir):
print "'%s' is not a dir" % outdir
return False
test_num = 0
with open(filename) as f:
in_code_block = False
markdown_tags = []
lines = []
for line in f:
if line.strip().startswith("/// ") or line.strip().startswith("//! "):
line = line.strip()[4 : len(line)]
if line.strip().startswith("///") or line.strip().startswith("//!"):
line = line.strip()[3 : len(line)]
if line.startswith("```") or line.startswith("~~~"):
in_code_block = not in_code_block
if in_code_block:
assert not markdown_tags
assert not lines
tags = line
tags = tags.replace("`", "")
tags = tags.replace("~", "")
tags = tags.strip()
tags = tags.translate(None, "{}.")
if len(tags) > 0:
tags = tags.split(",")
if len(tags) == 1:
tags = tags[0].split(" ")
else:
tags = []
markdown_tags = tags
else:
out_file_name = outdir + "/" + test_name + "_" + str(test_num).zfill(4) + ".rs"
translate_test_case(out_file_name, markdown_tags, lines)
markdown_tags = []
lines = []
test_num += 1
continue
if in_code_block:
lines = lines + [line]
pass
return True
if __name__ == '__main__':
if len(sys.argv) < 3:
print "usage: undoc.py filename outdir"
sys.exit(1)
filename = sys.argv[1]
outdir = sys.argv[2]
test_name = os.path.basename(filename)
test_name = test_name.replace(".", "_")
if undoc(filename, outdir, test_name):
sys.exit(0)
else:
sys.exit(1)