| | |
| | | import java.text.MessageFormat; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import org.apache.wicket.Application; |
| | | import org.apache.wicket.Localizer; |
| | |
| | | * |
| | | */ |
| | | public class GitBlitDiffFormatter extends DiffFormatter { |
| | | |
| | | /** Regex pattern identifying trailing whitespace. */ |
| | | private static final Pattern trailingWhitespace = Pattern.compile("(\\s+?)\r?\n?$"); |
| | | |
| | | /** |
| | | * gitblit.properties key for the per-file limit on the number of diff lines. |
| | |
| | | os.write("<td class='diff-cell context2'>".getBytes()); |
| | | break; |
| | | } |
| | | String line = text.getString(cur); |
| | | line = StringUtils.escapeForHtml(line, false); |
| | | os.write(encode(line)); |
| | | os.write(encode(codeLineToHtml(prefix, text.getString(cur)))); |
| | | os.write("</td></tr>\n".getBytes()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Convert the given code line to HTML. |
| | | * |
| | | * @param prefix |
| | | * the diff prefix (+/-) indicating whether the line was added or removed. |
| | | * @param line |
| | | * the line to format as HTML |
| | | * @return the HTML-formatted line, safe for inserting as is into HTML. |
| | | */ |
| | | private String codeLineToHtml(final char prefix, final String line) { |
| | | if ((prefix == '+' || prefix == '-')) { |
| | | // Highlight trailing whitespace on deleted/added lines. |
| | | Matcher matcher = trailingWhitespace.matcher(line); |
| | | if (matcher.find()) { |
| | | StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), false)); |
| | | result.append("<span class='trailingws-").append(prefix == '+' ? "add" : "sub").append("'>"); |
| | | result.append(StringUtils.escapeForHtml(matcher.group(1), false)); |
| | | result.append("</span>"); |
| | | return result.toString(); |
| | | } |
| | | } |
| | | return StringUtils.escapeForHtml(line, false); |
| | | } |
| | | |
| | | /** |
| | |
| | | sb.append("</tbody></table></div>\n"); |
| | | inFile = false; |
| | | } |
| | | |
| | | line = StringUtils.escapeForHtml(line, false); |
| | | sb.append(MessageFormat.format("<div class='header'><div class=\"diffHeader\" id=\"{0}\"><i class=\"icon-file\"></i> ", line)).append(line) |
| | | .append("</div></div>"); |
| | | sb.append("<div class=\"diff\">"); |
| | |
| | | } else { |
| | | sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">"); |
| | | } |
| | | line = StringUtils.escapeForHtml(line.substring(1), false); |
| | | } |
| | | sb.append(line); |
| | | if (gitLinkDiff) { |
| | |
| | | String path = StringUtils.escapeForHtml(s.getKey(), false); |
| | | String comment = s.getValue(); |
| | | if (comment != null) { |
| | | sb.append("<span id='" + path + "'>" + path + ' ' + StringUtils.escapeForHtml(comment, false) + "</span>"); |
| | | sb.append("<span id=\"" + path + "\">" + path + ' ' + StringUtils.escapeForHtml(comment, false) + "</span>"); |
| | | } else { |
| | | sb.append("<span id='" + path + "'>" + path + "</span>"); |
| | | sb.append("<span id=\"" + path + "\">" + path + "</span>"); |
| | | } |
| | | first = false; |
| | | } |