bsw@2: local old_draft_id = param.get("old_draft_id", atom.integer) bsw@2: local new_draft_id = param.get("new_draft_id", atom.integer) bsw@2: bsw@10: if not old_draft_id or not new_draft_id then bsw@10: slot.put( _"Please choose two versions of the draft to compare") bsw@10: return bsw@10: end bsw@10: bsw@10: if old_draft_id == new_draft_id then bsw@10: slot.put( _"Please choose two different versions of the draft to compare") bsw@10: return bsw@10: end bsw@10: bsw@2: if old_draft_id > new_draft_id then bsw@2: local tmp = old_draft_id bsw@2: old_draft_id = new_draft_id bsw@2: new_draft_id = tmp bsw@2: end bsw@2: bsw@2: local old_draft = Draft:by_id(old_draft_id) bsw@2: local new_draft = Draft:by_id(new_draft_id) bsw@2: poelzi@160: execute.view{ poelzi@160: module = "draft", poelzi@160: view = "_head", poelzi@160: params = { draft = new_draft} poelzi@160: } poelzi@159: poelzi@160: slot.put_into("title", " · " .. _"Diff") poelzi@159: poelzi@159: if app.session.member_id and not new_draft.initiative.revoked then poelzi@159: local supporter = Supporter:new_selector():add_where{"member_id = ?", app.session.member_id}:count() poelzi@159: if supporter then poelzi@159: ui.container{ poelzi@159: attr = { class = "draft_updated_info" }, poelzi@159: content = function() poelzi@159: slot.put(_"The draft of this initiative has been updated!") poelzi@159: slot.put(" ") poelzi@159: ui.link{ poelzi@159: text = _"Refresh support to current draft", poelzi@159: module = "initiative", poelzi@159: action = "add_support", poelzi@159: id = new_draft.initiative.id, poelzi@159: routing = { poelzi@159: default = { poelzi@159: mode = "redirect", poelzi@159: module = "initiative", poelzi@159: view = "show", poelzi@159: id = new_draft.initiative.id poelzi@159: } poelzi@159: } poelzi@159: } poelzi@159: end poelzi@159: } poelzi@159: end poelzi@159: end poelzi@159: bsw@95: local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n") bsw@95: local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n") bsw@95: bsw@2: local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz"); bsw@2: bsw@2: local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp") bsw@2: local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp") bsw@2: bsw@2: local old_draft_file = assert(io.open(old_draft_filename, "w")) bsw@95: old_draft_file:write(old_draft_content) bsw@2: old_draft_file:write("\n") bsw@2: old_draft_file:close() bsw@2: bsw@2: local new_draft_file = assert(io.open(new_draft_filename, "w")) bsw@95: new_draft_file:write(new_draft_content) bsw@2: new_draft_file:write("\n") bsw@2: new_draft_file:close() bsw@2: bsw@95: local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@") bsw@2: bsw@2: os.remove(old_draft_filename) bsw@2: os.remove(new_draft_filename) bsw@2: bsw@95: local last_state = "first_run" bsw@95: bsw@95: local function process_line(line) bsw@95: local state_char = string.sub(line, 1, 1) bsw@95: local state bsw@95: if state_char == "+" then bsw@95: state = "added" bsw@95: elseif state_char == "-" then bsw@95: state = "removed" bsw@95: elseif state_char == " " then bsw@95: state = "unchanged" bsw@95: end bsw@95: local state_changed = false bsw@95: if state ~= last_state then bsw@95: if last_state ~= "first_run" then bsw@95: slot.put(" ") bsw@95: end bsw@95: last_state = state bsw@95: state_changed = true bsw@95: slot.put("") bsw@95: end bsw@95: bsw@95: line = string.sub(line, 2, #line) bsw@95: if line ~= "###ENTER###" then bsw@95: if not state_changed then bsw@95: slot.put(" ") bsw@95: end bsw@95: slot.put(line) bsw@95: else bsw@95: slot.put("
") bsw@95: end bsw@95: end bsw@95: bsw@2: if not status then bsw@2: ui.field.text{ value = _"The drafts do not differ" } bsw@2: else bsw@95: ui.container{ bsw@95: tag = "div", bsw@95: attr = { class = "diff" }, bsw@95: content = function() bsw@95: output = output:gsub("[^\n\r]+", function(line) bsw@95: process_line(line) bsw@95: end) bsw@2: end bsw@95: } bsw@2: end bsw@2: