liquid_feedback_frontend
view app/main/draft/diff.lua @ 804:b690dfa23f95
Backed out changeset c0a0a3747a29 (wrong timestamp in commit)
| author | jbe | 
|---|---|
| date | Sat Jun 30 02:41:47 2012 +0200 (2012-06-30) | 
| parents | f49cdfb72567 | 
| children | 3b74187efaa3 | 
 line source
     1 local old_draft_id = param.get("old_draft_id", atom.integer)
     2 local new_draft_id = param.get("new_draft_id", atom.integer)
     4 if not old_draft_id or not new_draft_id then
     5   slot.put( _"Please choose two versions of the draft to compare")
     6   return
     7 end
     9 if old_draft_id == new_draft_id then
    10   slot.put( _"Please choose two different versions of the draft to compare")
    11   return
    12 end
    14 if old_draft_id > new_draft_id then
    15   local tmp = old_draft_id
    16   old_draft_id = new_draft_id
    17   new_draft_id = tmp
    18 end
    20 local old_draft = Draft:by_id(old_draft_id)
    21 local new_draft = Draft:by_id(new_draft_id)
    23 execute.view{
    24   module = "draft",
    25   view = "_head",
    26   params = { draft = new_draft}
    27 }
    29 ui.title(_"Diff")
    31 if app.session.member_id and not new_draft.initiative.revoked then
    32   local supporter = Supporter:new_selector():add_where{"member_id = ?", app.session.member_id}:count()
    33   if supporter then
    34     ui.container{
    35       attr = { class = "draft_updated_info" },
    36       content = function()
    37         slot.put(_"The draft of this initiative has been updated!")
    38         slot.put(" ")
    39         ui.link{
    40           text   = _"Refresh support to current draft",
    41           module = "initiative",
    42           action = "add_support",
    43           id     = new_draft.initiative.id,
    44           routing = {
    45             default = {
    46               mode = "redirect",
    47               module = "initiative",
    48               view = "show",
    49               id = new_draft.initiative.id
    50             }
    51           }
    52         }
    53       end
    54     }
    55   end
    56 end
    58 local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    59 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    61 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
    63 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
    64 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
    66 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    67 old_draft_file:write(old_draft_content)
    68 old_draft_file:write("\n")
    69 old_draft_file:close()
    71 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    72 new_draft_file:write(new_draft_content)
    73 new_draft_file:write("\n")
    74 new_draft_file:close()
    76 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
    78 os.remove(old_draft_filename)
    79 os.remove(new_draft_filename)
    81 local last_state = "first_run"
    83 local function process_line(line)
    84   local state_char = string.sub(line, 1, 1)
    85   local state
    86   if state_char == "+" then
    87     state = "added"
    88   elseif state_char == "-" then
    89     state = "removed"
    90   elseif state_char == " " then
    91     state = "unchanged"
    92   end
    93   local state_changed = false
    94   if state ~= last_state then
    95     if last_state ~= "first_run" then
    96       slot.put("</span> ")
    97     end
    98     last_state = state
    99     state_changed = true
   100     slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
   101   end
   103   line = string.sub(line, 2, #line)
   104   if line ~= "###ENTER###" then
   105     if not state_changed then
   106       slot.put(" ")
   107     end
   108     slot.put(line)
   109   else
   110     slot.put("<br />")
   111   end
   112 end
   114 if not status then
   115   ui.field.text{ value = _"The drafts do not differ" }
   116 else
   117   ui.container{
   118     tag = "div",
   119     attr = { class = "diff" },
   120     content = function()
   121       output = output:gsub("[^\n\r]+", function(line)
   122         process_line(line)
   123       end)
   124     end
   125   }
   126 end 
