Skip to content

Instantly share code, notes, and snippets.

@nvarun
Created August 31, 2014 17:56
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/a79352260a7d2f60c7dc to your computer and use it in GitHub Desktop.
Save nvarun/a79352260a7d2f60c7dc 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("^\*{5,}$"),
'OLD': re.compile("^\*{3} [0-9]+"),
'NEW': re.compile("^\-{3} [0-9]+"),
}
trHTMLCode = \
"""<tr>
<td>
%s
</td>
<td>
%s
</td>
</tr>"""
codeChunkDict = {'OLD': [], 'NEW': []}
oldIndex = newIndex = -1
###----------------------------------------------------------------------
## 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 codeChunkDict['OLD'] or codeChunkDict['NEW']:
print trHTMLCode % ('<br />\n'.join(codeChunkDict['OLD']), '<br />\n'.join(codeChunkDict['NEW']))
codeChunkDict = {'OLD': [], 'NEW': []}
else:
matchObj = re.match(regexDict['OLD'], line)
if matchObj:
newIndex, oldIndex = 0, int(line.split(' ')[1].split(',')[0])
else:
matchObj = re.match(regexDict['NEW'], line)
if matchObj:
oldIndex, newIndex = 0, int(line.split(' ')[1].split(',')[0])
elif oldIndex:
codeChunkDict['OLD'].append(fn__applyStyle(line))
elif newIndex:
codeChunkDict['NEW'].append(fn__applyStyle(line))
if codeChunkDict['OLD'] or codeChunkDict['NEW']:
print trHTMLCode % ('<br />\n'.join(codeChunkDict['OLD']), '<br />\n'.join(codeChunkDict['NEW']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment