summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/utils/HtmlBuilder.java
diff options
context:
space:
mode:
authorTom <tw201207@gmail.com>2014-11-11 07:52:15 +0100
committerTom <tw201207@gmail.com>2014-11-11 08:00:30 +0100
commit7dd99fe7474604f314c01bcd4123eb7cbacfb583 (patch)
tree84f3b0da388cdc79c2b31e6fab5619fc1617e455 /src/main/java/com/gitblit/utils/HtmlBuilder.java
parent8cd4feca58b55f311a543c744777e930c4f4b34a (diff)
downloadgitblit-7dd99fe7474604f314c01bcd4123eb7cbacfb583.tar.gz
gitblit-7dd99fe7474604f314c01bcd4123eb7cbacfb583.zip
Image diffs
Ticket 88: https://dev.gitblit.com/tickets/gitblit.git/88 Based on Lea Verou's pure CSS slider: http://lea.verou.me/2014/07/image-comparison-slider-with-pure-css/ * Add a callback interface, pass it through DiffUtils to the GitBlitDiffFormatter. Is needed because the rendering needs access to the repositoryName and other things that are known only at higher levels. * New class ImageDiffHandler responsible for rendering an image diff. Called for all binary diffs, doesn't do anything if it's not an image. HTML is generated via JSoup: no worries about forgetting to close a tag, not about HTML escaping, nor about XSS. * The 3 diff pages set up such an ImageDIffHandler and pass it along. * CSS changes: from Lea Verou, with some minor improvements. I think in the long run there'll be no way around rewriting the HTML diff formatter from scratch, not using the standard JGit DiffFormatter at all.
Diffstat (limited to 'src/main/java/com/gitblit/utils/HtmlBuilder.java')
-rw-r--r--src/main/java/com/gitblit/utils/HtmlBuilder.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/com/gitblit/utils/HtmlBuilder.java b/src/main/java/com/gitblit/utils/HtmlBuilder.java
new file mode 100644
index 00000000..6208ea82
--- /dev/null
+++ b/src/main/java/com/gitblit/utils/HtmlBuilder.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2014 Tom <tw201207@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gitblit.utils;
+
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+/**
+ * Simple helper class to hide some common setup needed to use JSoup as an HTML generator.
+ * A {@link HtmlBuilder} has a root element that can be manipulated in all the usual JSoup
+ * ways (but not added to some {@link Document}); to generate HTML for that root element,
+ * the builder's {@link #toString()} method can be used. (Or one can invoke toString()
+ * directly on the root element.) By default, a HTML builder does not pretty-print the HTML.
+ *
+ * @author Tom <tw201207@gmail.com>
+ */
+public class HtmlBuilder {
+
+ private final Document shell;
+ private final Element root;
+
+ /**
+ * Creates a new HTML builder with a root element with the given {@code tagName}.
+ *
+ * @param tagName
+ * of the {@link Element} to set as the root element.
+ */
+ public HtmlBuilder(String tagName) {
+ this(new Element(Tag.valueOf(tagName), ""));
+ }
+
+ /**
+ * Creates a new HTML builder for the given element.
+ *
+ * @param element
+ * to set as the root element of this HTML builder.
+ */
+ public HtmlBuilder(Element element) {
+ shell = Document.createShell("");
+ shell.outputSettings().prettyPrint(false);
+ shell.body().appendChild(element);
+ root = element;
+ }
+
+ /** @return the root element of this HTML builder */
+ public Element getRoot() {
+ return root;
+ }
+
+ /** @return the root element of this HTML builder */
+ public Element root() {
+ return root;
+ }
+
+ /** @return whether this HTML builder will pretty-print the HTML generated by {@link #toString()} */
+ public boolean prettyPrint() {
+ return shell.outputSettings().prettyPrint();
+ }
+
+ /**
+ * Sets whether this HTML builder will produce pretty-printed HTML in its {@link #toString()} method.
+ *
+ * @param pretty
+ * whether to pretty-print
+ * @return the HTML builder itself
+ */
+ public HtmlBuilder prettyPrint(boolean pretty) {
+ shell.outputSettings().prettyPrint(pretty);
+ return this;
+ }
+
+ /** @return the HTML for the root element. */
+ @Override
+ public String toString() {
+ return root.toString();
+ }
+
+ /** @return the {@link Document} used as generation shell. */
+ protected Document getShell() {
+ return shell;
+ }
+}