-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathparseRstNarrative.py
More file actions
126 lines (97 loc) · 4.67 KB
/
parseRstNarrative.py
File metadata and controls
126 lines (97 loc) · 4.67 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
# FILE ....... parseRstNarrative.py
# PURPOSE .... This script parses a .cpp file and creates an .rst file in a
# narrative format. Code comments in the .cpp become standard text,
# while actual code becomes a verbatim block in the rst. The target
# use case is to transform a unit or integrated test into a quick user
# tutorial by adding longer comments in the implementation.
# CREATED .... Sep 2018
import sys, string
# ... DETECT RST BLOCK START
def detectStart(rawLine):
startDetected = ("BEGIN_RST_NARRATIVE" in rawLine)
if (startDetected):
splitLine = rawLine.split() # split using whitespace as separator
outputFileName = splitLine[-1] # -1 = last string element
else:
outputFileName = ''
return (startDetected, outputFileName)
# ... DETECT RST BLOCK FINISH
def detectFinish(rawLine):
finishDetected = ("END_RST_NARRATIVE" in rawLine)
return finishDetected
# ... MAIN PARSER
def main():
# confirm number onf input arguments
assert len(sys.argv) == 2, "Script was expecting one input argument"
# open desired input file
inputFileName = str(sys.argv[1])
print "Attempting to parse file:", inputFileName
inputFile = open(inputFileName, "r")
# define state variables
activeRstBlock = False
activeVerbatimBlock = False
# loop over file line by line and apply parsing logic
for rawLine in inputFile:
# if not activeRstBlock, look for a start flag. once one is found, open
# a new RST file with the desired output filename. we also save an
# uncommented version of the code in rawCode so we can print a clean
# version at the end to allow users to easily see the overall code flow.
if (not activeRstBlock):
(startDetected, outputFileName) = detectStart(rawLine)
if (startDetected):
activeRstBlock = True
print "Creating new RST file:", outputFileName
outputFile = open(outputFileName, "w")
codeCount = 0
rawCode = ""
# we are in an activeRstBlock state. either we will encounter a finish flag
# or a line we want to write to output
else:
finishDetected = detectFinish(rawLine)
# if we find an end flag, we first print an uncommented version of the
# code, close the file, and switch back to an inactiveRstBlock state
if (finishDetected):
activeRstBlock = False
outputFile.write("\n**Clean Code:**\n\n")
outputFile.write(".. code:: cpp\n\n")
outputFile.write(rawCode)
outputFile.close()
# if we get here, this means we have a line we want to write into the
# output RST file. just need to decide if it is code or comment.
else:
splitLine = rawLine.strip().split()
emptyLine = (len(splitLine) == 0)
commentLine = (not emptyLine and "//" in splitLine[0])
# handle empty lines
if (emptyLine):
outputFile.write("\n")
if (activeVerbatimBlock):
rawCode += "\n"
# handle comment lines, stripping off //.
# a comment line also means that we can't be in an active
# verbatim code block
elif (commentLine):
if (activeVerbatimBlock):
outputFile.write("\n")
activeVerbatimBlock = False
cleanLine = rawLine.strip().replace("//", "").strip()
outputFile.write(cleanLine)
outputFile.write("\n")
# handle code lines. if this is the first code line after a
# comment block, we need to start the verbatim block. also, rst
# code blocks do not preserve leading white space, and therefore
# the code indentation between blocks cannot be preserved. as a
# quick workaround, we begin the code line with a line number. we
# should explore other strategies down the road.
else:
if (not activeVerbatimBlock):
outputFile.write("\n.. code:: cpp \n\n")
activeVerbatimBlock = True
outputFile.write("\t" + str(codeCount).zfill(3) + ") " + rawLine)
rawCode += "\t" + rawLine
codeCount += 1
# close file before quitting
inputFile.close()
print "Completed parsing file:", inputFileName
# ... EXECUTE MAIN
main()