liquid_feedback_frontend
view app/main/draft/diff.lua @ 60:7bc629bc1c20
Optionally show configuration specific head over issues in public access mode
| author | bsw | 
|---|---|
| date | Thu Apr 22 16:00:49 2010 +0200 (2010-04-22) | 
| parents | ca3a0552927f | 
| children | 6a12fb7e4963 | 
 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 not old_draft_id or not new_draft_id then
     7   slot.put( _"Please choose two versions of the draft to compare")
     8   return
     9 end
    11 if old_draft_id == new_draft_id then
    12   slot.put( _"Please choose two different versions of the draft to compare")
    13   return
    14 end
    16 if old_draft_id > new_draft_id then
    17   local tmp = old_draft_id
    18   old_draft_id = new_draft_id
    19   new_draft_id = tmp
    20 end
    22 local old_draft = Draft:by_id(old_draft_id)
    23 local new_draft = Draft:by_id(new_draft_id)
    25 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
    27 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
    28 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
    30 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    31 old_draft_file:write(old_draft.content)
    32 old_draft_file:write("\n")
    33 old_draft_file:close()
    35 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    36 new_draft_file:write(new_draft.content)
    37 new_draft_file:write("\n")
    38 new_draft_file:close()
    40 local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 100000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
    42 os.remove(old_draft_filename)
    43 os.remove(new_draft_filename)
    45 if not status then
    46   ui.field.text{ value = _"The drafts do not differ" }
    47 else
    48   slot.put('<table class="diff">')
    49   slot.put('<tr><th width="50%">' .. _"Old draft revision" .. '</th><th width="50%">' .. _"New draft revision" .. '</th></tr>')
    51   local last_state = "unchanged"
    52   local lines = {}
    53   local removed_lines = nil
    55   local function process_line(line)
    56     local state = "unchanged"
    57     local char = line:sub(1,1)
    58     line = line:sub(2)
    59     state = "unchanged"
    60     if char == "-" then
    61       state = "-"
    62     elseif char == "+" then
    63       state = "+"
    64     elseif char == "!" then
    65       state = "eof"
    66     end
    67     if last_state == "unchanged" then
    68       if state == "unchanged" then
    69         lines[#lines+1] = line
    70       elseif (state == "-") or (state == "+") or (state == "eof") then
    71         local text = table.concat(lines, "\n")
    72         slot.put("<tr><td>", encode.html_newlines(encode.html(text)), "</td><td>", encode.html_newlines(encode.html(text)), "</td></tr>")
    73         lines = { line }
    74       end
    75     elseif last_state == "-" then
    76       if state == "-" then
    77         lines[#lines+1] = line
    78       elseif state == "+" then
    79         removed_lines = lines
    80         lines = { line }
    81       elseif (state == "unchanged") or (state == "eof") then
    82         local text = table.concat(lines,"\n")
    83         slot.put('<tr><td class="removed">', encode.html_newlines(encode.html(text)), "</td><td></td></tr>")
    84         lines = { line }
    85       end
    86     elseif last_state == "+" then
    87       if state == "+" then
    88         lines[#lines+1] = line
    89       elseif (state == "-") or (state == "unchanged") or (state == "eof") then
    90         if removed_lines then
    91           local text = table.concat(lines, "\n")
    92           local removed_text = table.concat(removed_lines, "\n")
    93           slot.put('<tr><td class="removed">', encode.html_newlines(encode.html(removed_text)), '</td><td class="added">', encode.html_newlines(encode.html(text)), "</td></tr>")
    94         else
    95           local text = table.concat(lines, "\n")
    96           slot.put('<tr><td></td><td class="added">', encode.html_newlines(encode.html(text)), "</td></tr>")
    97         end
    98         removed_lines = nil
    99         lines = { line }
   100       end
   101     end
   102     last_state = state
   103   end
   105   output = output .. " "
   106   output = output:gsub("[^\n\r]+", function(line)
   107     process_line(line)
   108   end)
   109   process_line("!")
   111   slot.put("</table>")
   112 end 
