liquid_feedback_frontend
view 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 | 
 line source
     1 slot.put_into("title", _"Diff")
     3 local old_draft_id = param.get("old_draft_id", atom.integer)
     4 local new_draft_id = param.get("new_draft_id", atom.integer)
     6 if old_draft_id > new_draft_id then
     7   local tmp = old_draft_id
     8   old_draft_id = new_draft_id
     9   new_draft_id = tmp
    10 end
    12 local old_draft = Draft:by_id(old_draft_id)
    13 local new_draft = Draft:by_id(new_draft_id)
    15 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
    17 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
    18 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
    20 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    21 old_draft_file:write(old_draft.content)
    22 old_draft_file:write("\n")
    23 old_draft_file:close()
    25 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    26 new_draft_file:write(new_draft.content)
    27 new_draft_file:write("\n")
    28 new_draft_file:close()
    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 ^@")
    32 os.remove(old_draft_filename)
    33 os.remove(new_draft_filename)
    35 if not status then
    36   ui.field.text{ value = _"The drafts do not differ" }
    37 else
    38   slot.put('<table class="diff">')
    39   slot.put('<tr><th width="50%">' .. _"Old draft revision" .. '</th><th width="50%">' .. _"New draft revision" .. '</th></tr>')
    40   local last_state = "unchanged"
    41   local lines = {}
    42   local removed_lines = nil
    43   output = output .. " "
    44   output = output:gsub("[^\n\r]+", function(line)
    45     local state = "unchanged"
    46     local char = line:sub(1,1)
    47     line = line:sub(2)
    48     state = "unchanged"
    49     if char == "-" then
    50       state = "-"
    51     elseif char == "+" then
    52       state = "+"
    53     end
    54     if last_state == "unchanged" then
    55       if state == "unchanged" then
    56         lines[#lines+1] = line
    57       elseif (state == "-") or (state == "+") then
    58         local text = table.concat(lines, "<br />")
    59         slot.put("<tr><td>", text, "</td><td>", text, "</td></tr>")
    60         lines = { line }
    61       end
    62     elseif last_state == "-" then
    63       if state == "-" then
    64         lines[#lines+1] = line
    65       elseif state == "+" then
    66         removed_lines = lines
    67         lines = { line }
    68       elseif state == "unchanged" then
    69         local text = table.concat(lines,"<br />")
    70         slot.put('<tr><td class="removed">', text, "</td><td></td></tr>")
    71         lines = { line }
    72       end
    73     elseif last_state == "+" then
    74       if state == "+" then
    75         lines[#lines+1] = line
    76       elseif (state == "-") or (state == "unchanged") then
    77         if removed_lines then
    78           local text = table.concat(lines, "<br />")
    79           local removed_text = table.concat(removed_lines, "<br />")
    80           slot.put('<tr><td class="removed">', removed_text, '</td><td class="added">', text, "</td></tr>")
    81         else
    82           local text = table.concat(lines, "<br />")
    83           slot.put('<tr><td></td><td class="added">', text, "</td></tr>")
    84         end
    85         removed_lines = nil
    86         lines = { line }
    87       end
    88     end
    89     last_state = state
    90   end)
    91   slot.put("</table>")
    92 end 
