Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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