liquid_feedback_frontend
diff app/main/draft/diff.lua @ 95:6a12fb7e4963
Suggestion API, draft preview, word based diff, multiple fixes
- Added suggestion API
- Initiative API: Drafts optionally delivered as rendered html fragment
- Initiative API: Fixed wrong output of revoked timestamp when using JSON
- Preview added for initiative drafts
- Improved (word based) diff added
- Improved suggestion list
- Added missing sorting of initiative in vote list
- Filter state for member page initiative lists
- Fixed wrong status output in member history
- Fixed wrongly closed div in layout
- Added suggestion API
- Initiative API: Drafts optionally delivered as rendered html fragment
- Initiative API: Fixed wrong output of revoked timestamp when using JSON
- Preview added for initiative drafts
- Improved (word based) diff added
- Improved suggestion list
- Added missing sorting of initiative in vote list
- Filter state for member page initiative lists
- Fixed wrong status output in member history
- Fixed wrongly closed div in layout
author | bsw |
---|---|
date | Mon Aug 30 21:52:19 2010 +0200 (2010-08-30) |
parents | ca3a0552927f |
children | 5d797c6706d5 |
line diff
1.1 --- a/app/main/draft/diff.lua Thu Aug 19 15:37:51 2010 +0200 1.2 +++ b/app/main/draft/diff.lua Mon Aug 30 21:52:19 2010 +0200 1.3 @@ -22,92 +22,73 @@ 1.4 local old_draft = Draft:by_id(old_draft_id) 1.5 local new_draft = Draft:by_id(new_draft_id) 1.6 1.7 +local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n") 1.8 +local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n") 1.9 + 1.10 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz"); 1.11 1.12 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp") 1.13 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp") 1.14 1.15 local old_draft_file = assert(io.open(old_draft_filename, "w")) 1.16 -old_draft_file:write(old_draft.content) 1.17 +old_draft_file:write(old_draft_content) 1.18 old_draft_file:write("\n") 1.19 old_draft_file:close() 1.20 1.21 local new_draft_file = assert(io.open(new_draft_filename, "w")) 1.22 -new_draft_file:write(new_draft.content) 1.23 +new_draft_file:write(new_draft_content) 1.24 new_draft_file:write("\n") 1.25 new_draft_file:close() 1.26 1.27 -local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 100000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@") 1.28 +local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@") 1.29 1.30 os.remove(old_draft_filename) 1.31 os.remove(new_draft_filename) 1.32 1.33 +local last_state = "first_run" 1.34 + 1.35 +local function process_line(line) 1.36 + local state_char = string.sub(line, 1, 1) 1.37 + local state 1.38 + if state_char == "+" then 1.39 + state = "added" 1.40 + elseif state_char == "-" then 1.41 + state = "removed" 1.42 + elseif state_char == " " then 1.43 + state = "unchanged" 1.44 + end 1.45 + local state_changed = false 1.46 + if state ~= last_state then 1.47 + if last_state ~= "first_run" then 1.48 + slot.put("</span> ") 1.49 + end 1.50 + last_state = state 1.51 + state_changed = true 1.52 + slot.put("<span class=\"diff_" .. tostring(state) .. "\">") 1.53 + end 1.54 + 1.55 + line = string.sub(line, 2, #line) 1.56 + if line ~= "###ENTER###" then 1.57 + if not state_changed then 1.58 + slot.put(" ") 1.59 + end 1.60 + slot.put(line) 1.61 + else 1.62 + slot.put("<br />") 1.63 + end 1.64 +end 1.65 + 1.66 if not status then 1.67 ui.field.text{ value = _"The drafts do not differ" } 1.68 else 1.69 - slot.put('<table class="diff">') 1.70 - slot.put('<tr><th width="50%">' .. _"Old draft revision" .. '</th><th width="50%">' .. _"New draft revision" .. '</th></tr>') 1.71 - 1.72 - local last_state = "unchanged" 1.73 - local lines = {} 1.74 - local removed_lines = nil 1.75 - 1.76 - local function process_line(line) 1.77 - local state = "unchanged" 1.78 - local char = line:sub(1,1) 1.79 - line = line:sub(2) 1.80 - state = "unchanged" 1.81 - if char == "-" then 1.82 - state = "-" 1.83 - elseif char == "+" then 1.84 - state = "+" 1.85 - elseif char == "!" then 1.86 - state = "eof" 1.87 + ui.container{ 1.88 + tag = "div", 1.89 + attr = { class = "diff" }, 1.90 + content = function() 1.91 + output = output:gsub("[^\n\r]+", function(line) 1.92 + process_line(line) 1.93 + end) 1.94 end 1.95 - if last_state == "unchanged" then 1.96 - if state == "unchanged" then 1.97 - lines[#lines+1] = line 1.98 - elseif (state == "-") or (state == "+") or (state == "eof") then 1.99 - local text = table.concat(lines, "\n") 1.100 - slot.put("<tr><td>", encode.html_newlines(encode.html(text)), "</td><td>", encode.html_newlines(encode.html(text)), "</td></tr>") 1.101 - lines = { line } 1.102 - end 1.103 - elseif last_state == "-" then 1.104 - if state == "-" then 1.105 - lines[#lines+1] = line 1.106 - elseif state == "+" then 1.107 - removed_lines = lines 1.108 - lines = { line } 1.109 - elseif (state == "unchanged") or (state == "eof") then 1.110 - local text = table.concat(lines,"\n") 1.111 - slot.put('<tr><td class="removed">', encode.html_newlines(encode.html(text)), "</td><td></td></tr>") 1.112 - lines = { line } 1.113 - end 1.114 - elseif last_state == "+" then 1.115 - if state == "+" then 1.116 - lines[#lines+1] = line 1.117 - elseif (state == "-") or (state == "unchanged") or (state == "eof") then 1.118 - if removed_lines then 1.119 - local text = table.concat(lines, "\n") 1.120 - local removed_text = table.concat(removed_lines, "\n") 1.121 - 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>") 1.122 - else 1.123 - local text = table.concat(lines, "\n") 1.124 - slot.put('<tr><td></td><td class="added">', encode.html_newlines(encode.html(text)), "</td></tr>") 1.125 - end 1.126 - removed_lines = nil 1.127 - lines = { line } 1.128 - end 1.129 - end 1.130 - last_state = state 1.131 - end 1.132 - 1.133 - output = output .. " " 1.134 - output = output:gsub("[^\n\r]+", function(line) 1.135 - process_line(line) 1.136 - end) 1.137 - process_line("!") 1.138 - 1.139 - slot.put("</table>") 1.140 + } 1.141 end 1.142