forked from docsforadobe/javascript-tools-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.py
139 lines (108 loc) · 3.95 KB
/
header.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
# 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
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():
headerFile = os.path.join(script_dir, "0 - Table Of Contents/index.rst")
# open file
headers = []
with open(headerFile) as originalFile:
headers = originalFile.readlines()
headers = [x for x in headers if len(x.lstrip()) is not 0]
mainheaders = [x for x in headers if len(x) is len(x.lstrip())]
subheaders = [x.lstrip() for x in headers if len(x) is not len(x.lstrip())]
lines = []
with open(theFile) as originalFile:
lines = originalFile.readlines()
newfiles = [{"name": "index", "lines": []}]
for i, line in enumerate(lines):
converted = False
if line in mainheaders:
printAround(i, lines)
if (ask("Convert to main header?")):
newfiles.append({"name": slugify(line), "lines": []})
mainheaders.remove(line)
line = convertToHeader(line, "=")
converted = True
if not converted and line in subheaders:
printAround(i, lines)
if (ask("Convert to sub header?")):
subheaders.remove(line)
line = convertToHeader(line, "-")
newfiles[-1]["lines"].append(line)
for f in newfiles:
writeFile(f["name"], "".join(f["lines"]))
def printAround(i, lines):
for x in xrange(max(i-3, 0), i+4):
string = str(x) + " " + lines[x]
if x == i:
string = color.BOLD + color.BLUE + string + color.END
sys.stdout.write(string)
def convertToHeader(line, headerString):
label = ".. _" + slugify(line) + ":\n\n"
h = headerString * (len(line) - 1) + "\n"
return label + line + h
def writeFile(name, content):
writingdir = "header/"
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:
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'
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()