view_diff.py 3.7 KB

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