liquid_feedback_frontend
annotate app/main/draft/diff.lua @ 2:5c601807d397
Version alpha3
Dark green part of issue supporter bargraph represents all satisfied supporters, regardless of having seen the latest draft
Wiki formatting for drafts
Showing differences between two drafts of the same initiative
Display of outgoing delegation chains
Many other improvements
Dark green part of issue supporter bargraph represents all satisfied supporters, regardless of having seen the latest draft
Wiki formatting for drafts
Showing differences between two drafts of the same initiative
Display of outgoing delegation chains
Many other improvements
author | bsw |
---|---|
date | Mon Nov 23 12:00:00 2009 +0100 (2009-11-23) |
parents | |
children | 72c5e0ee7c98 |
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@2 | 6 if old_draft_id > new_draft_id then |
bsw@2 | 7 local tmp = old_draft_id |
bsw@2 | 8 old_draft_id = new_draft_id |
bsw@2 | 9 new_draft_id = tmp |
bsw@2 | 10 end |
bsw@2 | 11 |
bsw@2 | 12 local old_draft = Draft:by_id(old_draft_id) |
bsw@2 | 13 local new_draft = Draft:by_id(new_draft_id) |
bsw@2 | 14 |
bsw@2 | 15 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz"); |
bsw@2 | 16 |
bsw@2 | 17 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp") |
bsw@2 | 18 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp") |
bsw@2 | 19 |
bsw@2 | 20 local old_draft_file = assert(io.open(old_draft_filename, "w")) |
bsw@2 | 21 old_draft_file:write(old_draft.content) |
bsw@2 | 22 old_draft_file:write("\n") |
bsw@2 | 23 old_draft_file:close() |
bsw@2 | 24 |
bsw@2 | 25 local new_draft_file = assert(io.open(new_draft_filename, "w")) |
bsw@2 | 26 new_draft_file:write(new_draft.content) |
bsw@2 | 27 new_draft_file:write("\n") |
bsw@2 | 28 new_draft_file:close() |
bsw@2 | 29 |
bsw@2 | 30 local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 100000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@") |
bsw@2 | 31 |
bsw@2 | 32 os.remove(old_draft_filename) |
bsw@2 | 33 os.remove(new_draft_filename) |
bsw@2 | 34 |
bsw@2 | 35 if not status then |
bsw@2 | 36 ui.field.text{ value = _"The drafts do not differ" } |
bsw@2 | 37 else |
bsw@2 | 38 slot.put('<table class="diff">') |
bsw@2 | 39 slot.put('<tr><th width="50%">' .. _"Old draft revision" .. '</th><th width="50%">' .. _"New draft revision" .. '</th></tr>') |
bsw@2 | 40 local last_state = "unchanged" |
bsw@2 | 41 local lines = {} |
bsw@2 | 42 local removed_lines = nil |
bsw@2 | 43 output = output .. " " |
bsw@2 | 44 output = output:gsub("[^\n\r]+", function(line) |
bsw@2 | 45 local state = "unchanged" |
bsw@2 | 46 local char = line:sub(1,1) |
bsw@2 | 47 line = line:sub(2) |
bsw@2 | 48 state = "unchanged" |
bsw@2 | 49 if char == "-" then |
bsw@2 | 50 state = "-" |
bsw@2 | 51 elseif char == "+" then |
bsw@2 | 52 state = "+" |
bsw@2 | 53 end |
bsw@2 | 54 if last_state == "unchanged" then |
bsw@2 | 55 if state == "unchanged" then |
bsw@2 | 56 lines[#lines+1] = line |
bsw@2 | 57 elseif (state == "-") or (state == "+") then |
bsw@2 | 58 local text = table.concat(lines, "<br />") |
bsw@2 | 59 slot.put("<tr><td>", text, "</td><td>", text, "</td></tr>") |
bsw@2 | 60 lines = { line } |
bsw@2 | 61 end |
bsw@2 | 62 elseif last_state == "-" then |
bsw@2 | 63 if state == "-" then |
bsw@2 | 64 lines[#lines+1] = line |
bsw@2 | 65 elseif state == "+" then |
bsw@2 | 66 removed_lines = lines |
bsw@2 | 67 lines = { line } |
bsw@2 | 68 elseif state == "unchanged" then |
bsw@2 | 69 local text = table.concat(lines,"<br />") |
bsw@2 | 70 slot.put('<tr><td class="removed">', text, "</td><td></td></tr>") |
bsw@2 | 71 lines = { line } |
bsw@2 | 72 end |
bsw@2 | 73 elseif last_state == "+" then |
bsw@2 | 74 if state == "+" then |
bsw@2 | 75 lines[#lines+1] = line |
bsw@2 | 76 elseif (state == "-") or (state == "unchanged") then |
bsw@2 | 77 if removed_lines then |
bsw@2 | 78 local text = table.concat(lines, "<br />") |
bsw@2 | 79 local removed_text = table.concat(removed_lines, "<br />") |
bsw@2 | 80 slot.put('<tr><td class="removed">', removed_text, '</td><td class="added">', text, "</td></tr>") |
bsw@2 | 81 else |
bsw@2 | 82 local text = table.concat(lines, "<br />") |
bsw@2 | 83 slot.put('<tr><td></td><td class="added">', text, "</td></tr>") |
bsw@2 | 84 end |
bsw@2 | 85 removed_lines = nil |
bsw@2 | 86 lines = { line } |
bsw@2 | 87 end |
bsw@2 | 88 end |
bsw@2 | 89 last_state = state |
bsw@2 | 90 end) |
bsw@2 | 91 slot.put("</table>") |
bsw@2 | 92 end |
bsw@2 | 93 |