rev |
line source |
bsw@2
|
1 slot.put_into("title", _"Diff")
|
bsw@2
|
2
|
bsw@2
|
3 local old_draft_id = param.get("old_draft_id", atom.integer)
|
bsw@2
|
4 local new_draft_id = param.get("new_draft_id", atom.integer)
|
bsw@2
|
5
|
bsw@10
|
6 if not old_draft_id or not new_draft_id then
|
bsw@10
|
7 slot.put( _"Please choose two versions of the draft to compare")
|
bsw@10
|
8 return
|
bsw@10
|
9 end
|
bsw@10
|
10
|
bsw@10
|
11 if old_draft_id == new_draft_id then
|
bsw@10
|
12 slot.put( _"Please choose two different versions of the draft to compare")
|
bsw@10
|
13 return
|
bsw@10
|
14 end
|
bsw@10
|
15
|
bsw@2
|
16 if old_draft_id > new_draft_id then
|
bsw@2
|
17 local tmp = old_draft_id
|
bsw@2
|
18 old_draft_id = new_draft_id
|
bsw@2
|
19 new_draft_id = tmp
|
bsw@2
|
20 end
|
bsw@2
|
21
|
bsw@2
|
22 local old_draft = Draft:by_id(old_draft_id)
|
bsw@2
|
23 local new_draft = Draft:by_id(new_draft_id)
|
bsw@2
|
24
|
bsw@95
|
25 local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
|
bsw@95
|
26 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
|
bsw@95
|
27
|
bsw@2
|
28 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
|
bsw@2
|
29
|
bsw@2
|
30 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
|
bsw@2
|
31 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
|
bsw@2
|
32
|
bsw@2
|
33 local old_draft_file = assert(io.open(old_draft_filename, "w"))
|
bsw@95
|
34 old_draft_file:write(old_draft_content)
|
bsw@2
|
35 old_draft_file:write("\n")
|
bsw@2
|
36 old_draft_file:close()
|
bsw@2
|
37
|
bsw@2
|
38 local new_draft_file = assert(io.open(new_draft_filename, "w"))
|
bsw@95
|
39 new_draft_file:write(new_draft_content)
|
bsw@2
|
40 new_draft_file:write("\n")
|
bsw@2
|
41 new_draft_file:close()
|
bsw@2
|
42
|
bsw@95
|
43 local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
|
bsw@2
|
44
|
bsw@2
|
45 os.remove(old_draft_filename)
|
bsw@2
|
46 os.remove(new_draft_filename)
|
bsw@2
|
47
|
bsw@95
|
48 local last_state = "first_run"
|
bsw@95
|
49
|
bsw@95
|
50 local function process_line(line)
|
bsw@95
|
51 local state_char = string.sub(line, 1, 1)
|
bsw@95
|
52 local state
|
bsw@95
|
53 if state_char == "+" then
|
bsw@95
|
54 state = "added"
|
bsw@95
|
55 elseif state_char == "-" then
|
bsw@95
|
56 state = "removed"
|
bsw@95
|
57 elseif state_char == " " then
|
bsw@95
|
58 state = "unchanged"
|
bsw@95
|
59 end
|
bsw@95
|
60 local state_changed = false
|
bsw@95
|
61 if state ~= last_state then
|
bsw@95
|
62 if last_state ~= "first_run" then
|
bsw@95
|
63 slot.put("</span> ")
|
bsw@95
|
64 end
|
bsw@95
|
65 last_state = state
|
bsw@95
|
66 state_changed = true
|
bsw@95
|
67 slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
|
bsw@95
|
68 end
|
bsw@95
|
69
|
bsw@95
|
70 line = string.sub(line, 2, #line)
|
bsw@95
|
71 if line ~= "###ENTER###" then
|
bsw@95
|
72 if not state_changed then
|
bsw@95
|
73 slot.put(" ")
|
bsw@95
|
74 end
|
bsw@95
|
75 slot.put(line)
|
bsw@95
|
76 else
|
bsw@95
|
77 slot.put("<br />")
|
bsw@95
|
78 end
|
bsw@95
|
79 end
|
bsw@95
|
80
|
bsw@2
|
81 if not status then
|
bsw@2
|
82 ui.field.text{ value = _"The drafts do not differ" }
|
bsw@2
|
83 else
|
bsw@95
|
84 ui.container{
|
bsw@95
|
85 tag = "div",
|
bsw@95
|
86 attr = { class = "diff" },
|
bsw@95
|
87 content = function()
|
bsw@95
|
88 output = output:gsub("[^\n\r]+", function(line)
|
bsw@95
|
89 process_line(line)
|
bsw@95
|
90 end)
|
bsw@2
|
91 end
|
bsw@95
|
92 }
|
bsw@2
|
93 end
|
bsw@2
|
94
|