liquid_feedback_frontend
view app/main/draft/diff.lua @ 139:bc2570b97c09
fix spelling
fixed bug #330
fixed bug #330
| author | Daniel Poelzleithner <poelzi@poelzi.org> | 
|---|---|
| date | Wed Oct 06 13:52:23 2010 +0200 (2010-10-06) | 
| parents | 6a12fb7e4963 | 
| children | 5d797c6706d5 | 
 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 old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    26 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    28 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
    30 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
    31 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
    33 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    34 old_draft_file:write(old_draft_content)
    35 old_draft_file:write("\n")
    36 old_draft_file:close()
    38 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    39 new_draft_file:write(new_draft_content)
    40 new_draft_file:write("\n")
    41 new_draft_file:close()
    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 ^@")
    45 os.remove(old_draft_filename)
    46 os.remove(new_draft_filename)
    48 local last_state = "first_run"
    50 local function process_line(line)
    51   local state_char = string.sub(line, 1, 1)
    52   local state
    53   if state_char == "+" then
    54     state = "added"
    55   elseif state_char == "-" then
    56     state = "removed"
    57   elseif state_char == " " then
    58     state = "unchanged"
    59   end
    60   local state_changed = false
    61   if state ~= last_state then
    62     if last_state ~= "first_run" then
    63       slot.put("</span> ")
    64     end
    65     last_state = state
    66     state_changed = true
    67     slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
    68   end
    70   line = string.sub(line, 2, #line)
    71   if line ~= "###ENTER###" then
    72     if not state_changed then
    73       slot.put(" ")
    74     end
    75     slot.put(line)
    76   else
    77     slot.put("<br />")
    78   end
    79 end
    81 if not status then
    82   ui.field.text{ value = _"The drafts do not differ" }
    83 else
    84   ui.container{
    85     tag = "div",
    86     attr = { class = "diff" },
    87     content = function()
    88       output = output:gsub("[^\n\r]+", function(line)
    89         process_line(line)
    90       end)
    91     end
    92   }
    93 end 
