python - 如何将 git diff 结果与代码行号并排输出以嵌入网页?

标签 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)

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

最佳答案

使用以下流程:

  • 将 openTag 包裹在表格列中:

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

    </td>
    
  • 将循环的结果放入表格

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

  • 每一行号都是一个li标签

引用资料

关于python - 如何将 git diff 结果与代码行号并排输出以嵌入网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421170/

相关文章:

javascript - DataTable() 没有给出正确的对象引用

git 推送到 nginx+git-http-backend : error: Cannot access URL http return code 22 fatal: git-http-push failed

python - 使用 mod_wsgi 和 WinSCP 在 Apache 服务器上部署 Flask 应用程序

python - IBM-Watson 文本转语音抛出 "403:Forbidden"错误

HTML 表格自动扩展最后一列以占据剩余宽度

javascript - 使用鼠标位置更改 D3 map 工具提示的类别

.net - Azure 网站 - 从解决方案中选择要部署的网站项目

difftool 不会显示 Git diff -cc 模式

python - 如何将 grad 方法添加到 theano Op 中?

java - 提供小型静态图像的最佳方式是什么?