2
Runs through a reST file looking for old-style literals, and helps replace them
3
with new-style references.
10
refre = re.compile(r'``([^`\s]+?)``')
41
def fixliterals(fname):
42
data = open(fname).read()
46
storage = shelve.open("/tmp/literals_to_xref.shelve")
47
lastvalues = storage.get("lastvalues", {})
49
for m in refre.finditer(data):
51
new.append(data[last:m.start()])
54
line_start = data.rfind("\n", 0, m.start())
55
line_end = data.find("\n", m.end())
56
prev_start = data.rfind("\n", 0, line_start)
57
next_end = data.find("\n", line_end + 1)
59
# Skip always-skip stuff
60
if m.group(1) in ALWAYS_SKIP:
61
new.append(m.group(0))
64
# skip when the next line is a title
65
next_line = data[m.end():next_end].strip()
66
if next_line[0] in "!-/:-@[-`{-~" and \
67
all(c == next_line[0] for c in next_line):
68
new.append(m.group(0))
71
sys.stdout.write("\n" + "-" * 80 + "\n")
72
sys.stdout.write(data[prev_start + 1:m.start()])
73
sys.stdout.write(colorize(m.group(0), fg="red"))
74
sys.stdout.write(data[m.end():next_end])
75
sys.stdout.write("\n\n")
78
while replace_type is None:
79
replace_type = raw_input(
80
colorize("Replace role: ", fg="yellow")).strip().lower()
81
if replace_type and replace_type not in ROLES:
84
if replace_type == "":
85
new.append(m.group(0))
88
if replace_type == "skip":
89
new.append(m.group(0))
90
ALWAYS_SKIP.append(m.group(1))
93
default = lastvalues.get(m.group(1), m.group(1))
94
if default.endswith("()") and \
95
replace_type in ("class", "func", "meth"):
96
default = default[:-2]
97
replace_value = raw_input(
98
colorize("Text <target> [", fg="yellow") + default + \
99
colorize("]: ", fg="yellow")).strip()
100
if not replace_value:
101
replace_value = default
102
new.append(":%s:`%s`" % (replace_type, replace_value))
103
lastvalues[m.group(1)] = replace_value
105
new.append(data[last:])
106
open(fname, "w").write("".join(new))
108
storage["lastvalues"] = lastvalues
112
def colorize(text='', opts=(), **kwargs):
114
Returns your text, enclosed in ANSI graphics codes.
116
Depends on the keyword arguments 'fg' and 'bg', and the contents of
119
Returns the RESET code if no parameters are given.
122
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
130
'noreset' - string will not be auto-terminated with the RESET code
133
colorize('hello', fg='red', bg='blue', opts=('blink',))
135
colorize('goodbye', opts=('underscore',))
136
print colorize('first line', fg='red', opts=('noreset',))
137
print 'this should be red too'
138
print colorize('and so should this')
139
print 'this should not be red'
141
color_names = ('black', 'red', 'green', 'yellow',
142
'blue', 'magenta', 'cyan', 'white')
143
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
144
background = dict([(color_names[x], '4%s' % x) for x in range(8)])
147
opt_dict = {'bold': '1',
155
if text == '' and len(opts) == 1 and opts[0] == 'reset':
156
return '\x1b[%sm' % RESET
157
for k, v in kwargs.iteritems():
159
code_list.append(foreground[v])
161
code_list.append(background[v])
164
code_list.append(opt_dict[o])
165
if 'noreset' not in opts:
166
text = text + '\x1b[%sm' % RESET
167
return ('\x1b[%sm' % ';'.join(code_list)) + text
169
if __name__ == '__main__':
171
fixliterals(sys.argv[1])
172
except (KeyboardInterrupt, SystemExit):