bsw@2: slot.put_into("title", _"Diff") bsw@2: 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: 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@2: 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@2: new_draft_file:write(new_draft.content) bsw@2: new_draft_file:write("\n") bsw@2: new_draft_file:close() bsw@2: bsw@2: local output, err, status = os.pfilter(nil, "sh", "-c", "diff -U 100000 '" .. 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@2: if not status then bsw@2: ui.field.text{ value = _"The drafts do not differ" } bsw@2: else bsw@2: slot.put('') bsw@2: slot.put('') bsw@39: bsw@2: local last_state = "unchanged" bsw@2: local lines = {} bsw@2: local removed_lines = nil bsw@39: bsw@39: local function process_line(line) bsw@2: local state = "unchanged" bsw@2: local char = line:sub(1,1) bsw@2: line = line:sub(2) bsw@2: state = "unchanged" bsw@2: if char == "-" then bsw@2: state = "-" bsw@2: elseif char == "+" then bsw@2: state = "+" bsw@39: elseif char == "!" then bsw@39: state = "eof" bsw@2: end bsw@2: if last_state == "unchanged" then bsw@2: if state == "unchanged" then bsw@2: lines[#lines+1] = line bsw@39: elseif (state == "-") or (state == "+") or (state == "eof") then bsw@39: local text = table.concat(lines, "\n") bsw@40: slot.put("") bsw@2: lines = { line } bsw@2: end bsw@2: elseif last_state == "-" then bsw@2: if state == "-" then bsw@2: lines[#lines+1] = line bsw@2: elseif state == "+" then bsw@2: removed_lines = lines bsw@2: lines = { line } bsw@39: elseif (state == "unchanged") or (state == "eof") then bsw@39: local text = table.concat(lines,"\n") bsw@39: slot.put('") bsw@2: lines = { line } bsw@2: end bsw@2: elseif last_state == "+" then bsw@2: if state == "+" then bsw@2: lines[#lines+1] = line bsw@39: elseif (state == "-") or (state == "unchanged") or (state == "eof") then bsw@2: if removed_lines then bsw@39: local text = table.concat(lines, "\n") bsw@39: local removed_text = table.concat(removed_lines, "\n") bsw@39: slot.put('") bsw@2: else bsw@39: local text = table.concat(lines, "\n") bsw@39: slot.put('") bsw@2: end bsw@2: removed_lines = nil bsw@2: lines = { line } bsw@2: end bsw@2: end bsw@2: last_state = state bsw@39: end bsw@39: bsw@39: output = output .. " " bsw@39: output = output:gsub("[^\n\r]+", function(line) bsw@39: process_line(line) bsw@2: end) bsw@39: process_line("!") bsw@39: bsw@2: slot.put("
' .. _"Old draft revision" .. '' .. _"New draft revision" .. '
", encode.html_newlines(encode.html(text)), "", encode.html_newlines(encode.html(text)), "
', encode.html_newlines(encode.html(text)), "
', encode.html_newlines(encode.html(removed_text)), '', encode.html_newlines(encode.html(text)), "
', encode.html_newlines(encode.html(text)), "
") bsw@2: end bsw@2: