view_diff.py 3.9 KB

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