Skip to content

Instantly share code, notes, and snippets.

@nvarun
Created August 31, 2014 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvarun/8be62aed535c6d72c225 to your computer and use it in GitHub Desktop.
Save nvarun/8be62aed535c6d72c225 to your computer and use it in GitHub Desktop.
import re, sys, string, os
###----------------------------------------------------------------------
## Define Function(s)
###----------------------------------------------------------------------
def fn__applyStyle(lineOfCode):
indexZero = lineOfCode[0]
lineOfCode = string.replace(lineOfCode[1:], ' ', ' ')
lineOfCode = string.replace(string.replace(lineOfCode, '<', '&lt;'), '>', '&gt;')
spanHTMLDict = {
'+': "<span style='color: green'>%s</span>",
'-': "<span style='color: red'>%s</span>",
'!': "<span style='color: blue'>%s</span>",
'x': "%s"
}
if indexZero not in ['+', '-', '!']:
indexZero = 'x'
return " %s" % (spanHTMLDict[indexZero] % lineOfCode)
###----------------------------------------------------------------------
## Validate Script Argument(s)
###----------------------------------------------------------------------
if len(sys.argv) != 2:
print 'Usage: %s <diff-output>' % sys.argv[0]
sys.exit(1)
if not os.path.exists(sys.argv[1]):
print 'Invalid Arguments: %s' % sys.argv[1]
sys.exit(2)
###----------------------------------------------------------------------
## Define Global Variable(s)
###----------------------------------------------------------------------
inputFile = sys.argv[1]
regexDict = {
'HEADER': re.compile("^@@ ([+-][0-9]+(,[0-9]+)? ?){1,2} @@$"),
'ADD': re.compile("^\+"),
'DEL': re.compile("^\-")
}
trHTMLCode = \
"""<tr>
<td>
%s
</td>
</tr>"""
codeChunkList = []
###----------------------------------------------------------------------
## Begin Program
###----------------------------------------------------------------------
with open(inputFile, 'r') as fileObj:
### Using Slice To Ignore First 2 Lines
for line in fileObj.read().splitlines()[2:]:
matchObj = re.match(regexDict['HEADER'], line)
if matchObj:
if codeChunkList:
print trHTMLCode % '<br />\n'.join(codeChunkList)
codeChunkList = []
else:
matchObj = re.match(regexDict['ADD'], line)
if matchObj:
codeChunkList.append(fn__applyStyle(line))
else:
matchObj = re.match(regexDict['DEL'], line)
if matchObj:
codeChunkList.append(fn__applyStyle(line))
else:
codeChunkList.append(fn__applyStyle(line))
if codeChunkList:
print trHTMLCode % '<br />\n'.join(codeChunkList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment