forked from docsforadobe/javascript-tools-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinks.py
More file actions
168 lines (136 loc) · 4.81 KB
/
links.py
File metadata and controls
168 lines (136 loc) · 4.81 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Use this to split a file based on headers
# Looks for reStructuredText headers, which looks like:
# ==============
# Example header
# ==============
# Creates files with the header contents as file name or "missing" if not found
# New files is placed in a folder called "split"
import re
import os
import sys
from slugify import slugify
targetextension = [".rst"]
script_dir = os.path.dirname(__file__)
if script_dir is "":
script_dir = "."
script, targetfile = sys.argv
# Replace with your source file
theFile = os.path.join(script_dir, targetfile)
def main():
if os.path.isdir(theFile):
for filename in os.listdir(theFile):
extension = os.path.splitext(filename)[1]
if extension in targetextension:
print color.BOLD + color.RED + filename + color.END
convert(os.path.join(theFile, filename))
print "\n" * 5
else:
convert(theFile)
def convert(theFile):
lines = ""
with open(theFile) as originalFile:
lines = originalFile.read()
modifiedlines = checklines(lines, askEachTime=False)
if modifiedlines != lines:
if not ask("Is this correct?"):
modifiedlines = checklines(lines, askEachTime=True)
if modifiedlines != lines:
filename = os.path.basename(theFile)
# Split string at extension and get first element
name = os.path.splitext(filename)[0]
writefile(name, modifiedlines)
def checklines(lines, askEachTime):
linkRegex = re.compile( r"(see\s(?:Chapter \d\d?,\s)?(\"(.+?)\.?\"(?:\son\spage\s\d\d?\d?)?))", re.I|re.S)
matches = linkRegex.findall(lines)
for match in matches:
converted = convertmatch(match)
print match[0],
print color.GREEN + " -> " + color.END,
print color.BOLD + converted + color.END
if not askEachTime or ask("Is this correct?"):
lines = lines.replace(match[0], converted)
return lines
def convertmatch(match):
refname = match[2]
ref = ":ref:`" + slugify(refname) + "`"
refnamewithquotes = match[1]
wholestring = match[0]
period = "." if wholestring[-2:-1] == "." else ""
return wholestring.replace(refnamewithquotes, ref) + period
def printAround(i, lines):
for x in xrange(max(i-3, 0), min(i+4, len(lines))):
string = lines[x]
if lines[x-1].endswith("\n"):
string = str(x) + " " + lines[x]
if x == i:
string = color.WHITE + string[:-1]
nextString = color.WHITE + lines[x+1]
highlight = color.bPURPLE + " " + color.END
string = string + highlight + nextString + color.END
elif x == i + 1:
continue
else:
string = color.GRAY + string + color.END
sys.stdout.write(string)
def writefile(name, content):
writingdir = "links/"
if not os.path.exists(os.path.join(script_dir, writingdir)):
os.makedirs(os.path.join(script_dir, writingdir))
filename = name + ".rst"
newfilepath = os.path.join(script_dir, writingdir, filename)
newfile = open(newfilepath, 'w+')
newfile.write(content)
newfile.close()
print "The modified contents has been written to " + newfilepath
class color:
BLACK = '\033[30m'
WHITE = '\033[37m'
GRAY = '\033[39m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
bBlack = '\033[40m'
bRED = '\033[41m'
bGREEN = '\033[42m'
bYELLOW = '\033[43m'
bBLUE = '\033[44m'
bPURPLE = '\033[45m'
bCYAN = '\033[46m'
bWHITE = '\033[47m'
END = '\033[0m'
def ask(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
prompt = color.PURPLE + prompt + color.END
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
main()