view_diff.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from .tool.func import *
  2. def view_diff_do(first_raw_data, second_raw_data, first, second):
  3. if first_raw_data == second_raw_data:
  4. result = ''
  5. else:
  6. dmp = diff_match_patch()
  7. diff_data = dmp.diff_main(first_raw_data, second_raw_data)
  8. dmp.diff_cleanupSemantic(diff_data)
  9. diff_data += [[0, '\n']]
  10. diff_data_2 = []
  11. temp_list = []
  12. line = 1
  13. line_change = 0
  14. for for_a in diff_data:
  15. line_split = re.findall(r'(.*\n)|(.+$)', for_a[1])
  16. if line_split:
  17. for for_b in line_split:
  18. if for_b[0] != '':
  19. if for_a[0] != 0:
  20. line_change = 1
  21. temp_list += [[line, for_a[0], for_b[0].replace('\n', '')]]
  22. if line_change == 1:
  23. diff_data_2 += temp_list
  24. temp_list = []
  25. line_change = 0
  26. line += 1
  27. else:
  28. if for_a[0] != 0:
  29. line_change = 1
  30. temp_list += [[line, for_a[0], for_b[1]]]
  31. else:
  32. if for_a[0] != 0:
  33. line_change = 1
  34. temp_list += [[line, for_a[0], for_a[1]]]
  35. result = '<table style="width: 100%; white-space: pre-wrap;"><tr><td colspan="2">' + first + ' ➤ ' + second + '</td></tr>'
  36. result += '<tr><td style="width: 40px; user-select: none;">'
  37. line = 0
  38. for for_a in diff_data_2:
  39. if line == 0:
  40. line = for_a[0]
  41. result += str(line) + '</td><td>'
  42. else:
  43. if line != for_a[0]:
  44. line = for_a[0]
  45. result += '</td></tr><tr><td style="width: 40px; user-select: none;">' + str(line) + '</td><td>'
  46. if for_a[1] == 1:
  47. result += '<span class="opennamu_diff_green">' + html.escape(for_a[2]) + '</span>'
  48. elif for_a[1] == 0:
  49. result += html.escape(for_a[2])
  50. else:
  51. result += '<span class="opennamu_diff_red">' + html.escape(for_a[2]) + '</span>'
  52. result += '</td></tr></table>'
  53. return result
  54. async def view_diff(name = 'Test', num_a = 1, num_b = 1):
  55. with get_db_connect() as conn:
  56. curs = conn.cursor()
  57. first = str(num_a)
  58. second = str(num_b)
  59. if await acl_check(name, 'render') == 1:
  60. return await re_error(conn, 0)
  61. curs.execute(db_change("select title from history where title = ? and (id = ? or id = ?) and hide = 'O'"), [name, first, second])
  62. if curs.fetchall() and await acl_check(tool = 'hidel_auth') == 1:
  63. return await re_error(conn, 3)
  64. curs.execute(db_change("select data from history where id = ? and title = ?"), [first, name])
  65. first_raw_data = curs.fetchall()
  66. curs.execute(db_change("select data from history where id = ? and title = ?"), [second, name])
  67. second_raw_data = curs.fetchall()
  68. if first_raw_data and second_raw_data:
  69. first_raw_data = first_raw_data[0][0].replace('\r', '')
  70. second_raw_data = second_raw_data[0][0].replace('\r', '')
  71. result = view_diff_do(first_raw_data, second_raw_data, 'r' + first, 'r' + second)
  72. return easy_minify(flask.render_template(await skin_check(),
  73. imp = [name, await wiki_set(), await wiki_custom(conn), wiki_css(['(' + await get_lang('compare') + ')', 0])],
  74. data = result,
  75. menu = [['history/' + url_pas(name), await get_lang('return')]]
  76. ))
  77. else:
  78. return redirect(conn, '/history/' + url_pas(name))