2008-08-24 06:25:40 +08:00
|
|
|
"""
|
|
|
|
Runs through a reST file looking for old-style literals, and helps replace them
|
|
|
|
with new-style references.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import shelve
|
2015-02-10 02:19:34 +08:00
|
|
|
import sys
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
refre = re.compile(r'``([^`\s]+?)``')
|
|
|
|
|
|
|
|
ROLES = (
|
|
|
|
'attr',
|
|
|
|
'class',
|
|
|
|
"djadmin",
|
|
|
|
'data',
|
|
|
|
'exc',
|
|
|
|
'file',
|
|
|
|
'func',
|
|
|
|
'lookup',
|
|
|
|
'meth',
|
2013-10-27 09:27:42 +08:00
|
|
|
'mod',
|
2008-08-24 06:25:40 +08:00
|
|
|
"djadminopt",
|
|
|
|
"ref",
|
|
|
|
"setting",
|
|
|
|
"term",
|
|
|
|
"tfilter",
|
|
|
|
"ttag",
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
# special
|
|
|
|
"skip"
|
|
|
|
)
|
|
|
|
|
|
|
|
ALWAYS_SKIP = [
|
|
|
|
"NULL",
|
|
|
|
"True",
|
|
|
|
"False",
|
|
|
|
]
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
def fixliterals(fname):
|
2012-05-05 20:01:38 +08:00
|
|
|
with open(fname) as fp:
|
|
|
|
data = fp.read()
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
last = 0
|
|
|
|
new = []
|
|
|
|
storage = shelve.open("/tmp/literals_to_xref.shelve")
|
|
|
|
lastvalues = storage.get("lastvalues", {})
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
for m in refre.finditer(data):
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
new.append(data[last:m.start()])
|
|
|
|
last = m.end()
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
line_start = data.rfind("\n", 0, m.start())
|
|
|
|
line_end = data.find("\n", m.end())
|
|
|
|
prev_start = data.rfind("\n", 0, line_start)
|
|
|
|
next_end = data.find("\n", line_end + 1)
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
# Skip always-skip stuff
|
|
|
|
if m.group(1) in ALWAYS_SKIP:
|
|
|
|
new.append(m.group(0))
|
|
|
|
continue
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
# skip when the next line is a title
|
|
|
|
next_line = data[m.end():next_end].strip()
|
|
|
|
if next_line[0] in "!-/:-@[-`{-~" and all(c == next_line[0] for c in next_line):
|
|
|
|
new.append(m.group(0))
|
|
|
|
continue
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2013-11-04 02:08:55 +08:00
|
|
|
sys.stdout.write("\n" + "-" * 80 + "\n")
|
|
|
|
sys.stdout.write(data[prev_start + 1:m.start()])
|
2008-08-24 06:25:40 +08:00
|
|
|
sys.stdout.write(colorize(m.group(0), fg="red"))
|
|
|
|
sys.stdout.write(data[m.end():next_end])
|
|
|
|
sys.stdout.write("\n\n")
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
replace_type = None
|
|
|
|
while replace_type is None:
|
|
|
|
replace_type = raw_input(
|
|
|
|
colorize("Replace role: ", fg="yellow")
|
|
|
|
).strip().lower()
|
|
|
|
if replace_type and replace_type not in ROLES:
|
|
|
|
replace_type = None
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
if replace_type == "":
|
|
|
|
new.append(m.group(0))
|
|
|
|
continue
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
if replace_type == "skip":
|
|
|
|
new.append(m.group(0))
|
|
|
|
ALWAYS_SKIP.append(m.group(1))
|
|
|
|
continue
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
default = lastvalues.get(m.group(1), m.group(1))
|
|
|
|
if default.endswith("()") and replace_type in ("class", "func", "meth"):
|
2013-10-11 04:42:30 +08:00
|
|
|
default = default[:-2]
|
2008-08-24 06:25:40 +08:00
|
|
|
replace_value = raw_input(
|
|
|
|
colorize("Text <target> [", fg="yellow") + default + colorize("]: ", fg="yellow")
|
|
|
|
).strip()
|
2013-10-11 04:42:30 +08:00
|
|
|
if not replace_value:
|
2008-08-24 06:25:40 +08:00
|
|
|
replace_value = default
|
|
|
|
new.append(":%s:`%s`" % (replace_type, replace_value))
|
|
|
|
lastvalues[m.group(1)] = replace_value
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
new.append(data[last:])
|
2012-05-05 20:01:38 +08:00
|
|
|
with open(fname, "w") as fp:
|
|
|
|
fp.write("".join(new))
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
storage["lastvalues"] = lastvalues
|
|
|
|
storage.close()
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
#
|
|
|
|
# The following is taken from django.utils.termcolors and is copied here to
|
2013-03-29 01:16:53 +08:00
|
|
|
# avoid the dependency.
|
2008-08-24 06:25:40 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def colorize(text='', opts=(), **kwargs):
|
|
|
|
"""
|
|
|
|
Returns your text, enclosed in ANSI graphics codes.
|
|
|
|
|
|
|
|
Depends on the keyword arguments 'fg' and 'bg', and the contents of
|
|
|
|
the opts tuple/list.
|
|
|
|
|
|
|
|
Returns the RESET code if no parameters are given.
|
|
|
|
|
|
|
|
Valid colors:
|
|
|
|
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
|
|
|
|
|
|
|
|
Valid options:
|
|
|
|
'bold'
|
|
|
|
'underscore'
|
|
|
|
'blink'
|
|
|
|
'reverse'
|
|
|
|
'conceal'
|
|
|
|
'noreset' - string will not be auto-terminated with the RESET code
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
colorize('hello', fg='red', bg='blue', opts=('blink',))
|
|
|
|
colorize()
|
|
|
|
colorize('goodbye', opts=('underscore',))
|
2012-04-29 00:02:01 +08:00
|
|
|
print(colorize('first line', fg='red', opts=('noreset',)))
|
|
|
|
print('this should be red too')
|
|
|
|
print(colorize('and so should this'))
|
|
|
|
print('this should not be red')
|
2008-08-24 06:25:40 +08:00
|
|
|
"""
|
|
|
|
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
|
2014-12-07 05:00:09 +08:00
|
|
|
foreground = {color_names[x]: '3%s' % x for x in range(8)}
|
|
|
|
background = {color_names[x]: '4%s' % x for x in range(8)}
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
RESET = '0'
|
|
|
|
opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'}
|
|
|
|
|
|
|
|
text = str(text)
|
|
|
|
code_list = []
|
|
|
|
if text == '' and len(opts) == 1 and opts[0] == 'reset':
|
|
|
|
return '\x1b[%sm' % RESET
|
|
|
|
for k, v in kwargs.iteritems():
|
|
|
|
if k == 'fg':
|
|
|
|
code_list.append(foreground[v])
|
|
|
|
elif k == 'bg':
|
|
|
|
code_list.append(background[v])
|
|
|
|
for o in opts:
|
|
|
|
if o in opt_dict:
|
|
|
|
code_list.append(opt_dict[o])
|
|
|
|
if 'noreset' not in opts:
|
|
|
|
text = text + '\x1b[%sm' % RESET
|
|
|
|
return ('\x1b[%sm' % ';'.join(code_list)) + text
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
fixliterals(sys.argv[1])
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
2012-04-29 00:02:01 +08:00
|
|
|
print('')
|