gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:23:15 24 4
gpt4 key购买 nike

我已经想出如何使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com