liquid_feedback_frontend
view app/main/draft/diff.lua @ 44:b2af739019ba
Fixed two spelling issues in Esperanto translation.
| author | Dinu Gherman | 
|---|---|
| date | Mon Mar 08 22:58:57 2010 +0100 (2010-03-08) | 
| parents | 72c5e0ee7c98 | 
| children | 06a6e5846536 | 
 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>')
    50   local last_state = "unchanged"
    51   local lines = {}
    52   local removed_lines = nil
    53   output = output .. " "
    54   output = output:gsub("[^\n\r]+", function(line)
    55     local state = "unchanged"
    56     local char = line:sub(1,1)
    57     line = line:sub(2)
    58     state = "unchanged"
    59     if char == "-" then
    60       state = "-"
    61     elseif char == "+" then
    62       state = "+"
    63     end
    64     if last_state == "unchanged" then
    65       if state == "unchanged" then
    66         lines[#lines+1] = line
    67       elseif (state == "-") or (state == "+") then
    68         local text = table.concat(lines, "<br />")
    69         slot.put("<tr><td>", text, "</td><td>", text, "</td></tr>")
    70         lines = { line }
    71       end
    72     elseif last_state == "-" then
    73       if state == "-" then
    74         lines[#lines+1] = line
    75       elseif state == "+" then
    76         removed_lines = lines
    77         lines = { line }
    78       elseif state == "unchanged" then
    79         local text = table.concat(lines,"<br />")
    80         slot.put('<tr><td class="removed">', text, "</td><td></td></tr>")
    81         lines = { line }
    82       end
    83     elseif last_state == "+" then
    84       if state == "+" then
    85         lines[#lines+1] = line
    86       elseif (state == "-") or (state == "unchanged") then
    87         if removed_lines then
    88           local text = table.concat(lines, "<br />")
    89           local removed_text = table.concat(removed_lines, "<br />")
    90           slot.put('<tr><td class="removed">', removed_text, '</td><td class="added">', text, "</td></tr>")
    91         else
    92           local text = table.concat(lines, "<br />")
    93           slot.put('<tr><td></td><td class="added">', text, "</td></tr>")
    94         end
    95         removed_lines = nil
    96         lines = { line }
    97       end
    98     end
    99     last_state = state
   100   end)
   101   slot.put("</table>")
   102 end 
