如何将git diff结果与代码行号并排输出到网页中?

时间:2015-05-24 07:38:51

标签: python html git git-diff

我已经想出如何使用html显示行号和彩色差异以嵌入电子邮件或网页,但我不知道如何去做并排的差异。

这是我的小蛋糕,以获得基于颜色HTML的git diff与行号:

openTag = """<span style='font-size:1.0em; color: """
openTagEnd = ";font-family: courier, arial, helvetica, sans-serif;'>"
nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;'

def _traditional_diff(linesfromDiff, openTag, openTagEnd, nbsp):
    lines = []  
    line_num = 0

    def updateLine(line_num, color, line):
        tabs = line.count('\t')
        lines.append("%s:%s#%s%s%s%s</span><br>" % 
        ((repr(line_num), openTag, color, openTagEnd, nbsp*tabs, line)))
        return lines

    for line in linesfromDiff:
        if (line.startswith('diff ') or
                line.startswith('index ') or
                line.startswith('--- ')):
            color = "10EDF5"
            updateLine(line_num, color, line)
            continue

        if line.startswith('-'):
            color = "ff0000"
            updateLine(line_num, color, line)
            continue


        if line.startswith('+++ '):
            color = "07CB14"
            updateLine(line_num, color, line)
            continue

        if line.startswith('@@ '):
            _, old_nr, new_nr, _ = line.split(' ', 3)
            line_num = int(new_nr.split(',')[0])
            color = "5753BE"
            updateLine(line_num, color, line)
            continue

        if line.startswith('+'):
            color = "007900"
            updateLine(line_num, color, line)

        if line.startswith('+') or line.startswith(' '):
            line_num += 1

    return ''.join(lines)

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

使用以下过程:

  • 将openTag包装在表格列中:

    <td>
    
  • nbsp替换为列的结束标记:

    </td>
    
  • 将循环结果放在表格中

    <table><tr>%s</tr></table>
    
  • 在ol标签中包裹每一行

  • 每个行号都是li标签

<强>参考

相关问题