]> source.dussan.org Git - gitblit.git/commitdiff
Implement configurable tab length support for blob views 53/253/1
authorJames Moger <james.moger@gitblit.com>
Fri, 22 May 2015 17:39:17 +0000 (13:39 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 22 May 2015 17:39:17 +0000 (13:39 -0400)
13 files changed:
src/main/distrib/data/defaults.properties
src/main/java/com/gitblit/service/LuceneService.java
src/main/java/com/gitblit/utils/DiffUtils.java
src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
src/main/java/com/gitblit/utils/StringUtils.java
src/main/java/com/gitblit/wicket/pages/BlamePage.java
src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
src/main/java/com/gitblit/wicket/pages/BlobPage.java
src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
src/main/java/com/gitblit/wicket/pages/ComparePage.java
src/main/java/com/gitblit/wicket/pages/ReviewProposalPage.java
src/test/java/com/gitblit/tests/DiffUtilsTest.java
src/test/java/com/gitblit/tests/StringUtilsTest.java

index 59fa39966f5c947c61185b79213011bd98e1e543..c17506894554026f6dc18cf544e5730cb88ae83f 100644 (file)
@@ -784,6 +784,11 @@ web.includePersonalRepositories = false
 # BASEFOLDER
 web.projectsFile = ${baseFolder}/projects.conf
 
+# Defines the tab length for all blob views
+#
+# SINCE 1.7.0
+web.tabLength = 4
+
 # Either the full path to a user config file (users.conf)
 # OR a fully qualified class name that implements the IUserService interface.
 #
index 3f7635f4b887684ab3d810a2e5b22aef8be1a2f3..798edb0a13091438fea5c60c570197fefee760a1 100644 (file)
@@ -1104,6 +1104,7 @@ public class LuceneService implements Runnable {
                        content = "";\r
                }\r
 \r
+               int tabLength = storedSettings.getInteger(Keys.web.tabLength, 4);\r
                int fragmentLength = SearchObjectType.commit == result.type ? 512 : 150;\r
 \r
                QueryScorer scorer = new QueryScorer(query, "content");\r
@@ -1126,7 +1127,7 @@ public class LuceneService implements Runnable {
                        if (fragment.length() > fragmentLength) {\r
                                fragment = fragment.substring(0, fragmentLength) + "...";\r
                        }\r
-                       return "<pre class=\"text\">" + StringUtils.escapeForHtml(fragment, true) + "</pre>";\r
+                       return "<pre class=\"text\">" + StringUtils.escapeForHtml(fragment, true, tabLength) + "</pre>";\r
                }\r
 \r
                // make sure we have unique fragments\r
index 09b8f8012e7358272fb804186ff928bfcd5dcd00..cdebec1b53dfed8fcf158df14fc716953bb69048 100644 (file)
@@ -229,11 +229,12 @@ public class DiffUtils {
         * @param commit\r
         * @param comparator\r
         * @param outputType\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,\r
-                       DiffComparator comparator, DiffOutputType outputType) {\r
-               return getDiff(repository, null, commit, null, comparator, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType, int tabLength) {\r
+               return getDiff(repository, null, commit, null, comparator, outputType, tabLength);\r
        }\r
 \r
        /**\r
@@ -246,11 +247,12 @@ public class DiffUtils {
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
         *            May be {@code null}, resulting in the default behavior.\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,\r
-                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, null, commit, null, comparator, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {\r
+               return getDiff(repository, null, commit, null, comparator, outputType, handler, tabLength);\r
        }\r
 \r
 \r
@@ -263,11 +265,12 @@ public class DiffUtils {
         * @param path\r
         * @param comparator\r
         * @param outputType\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,\r
-                       DiffComparator comparator, DiffOutputType outputType) {\r
-               return getDiff(repository, null, commit, path, comparator, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType, int tabLength) {\r
+               return getDiff(repository, null, commit, path, comparator, outputType, tabLength);\r
        }\r
 \r
        /**\r
@@ -282,11 +285,12 @@ public class DiffUtils {
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
         *            May be {@code null}, resulting in the default behavior.\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,\r
-                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, null, commit, path, comparator, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {\r
+               return getDiff(repository, null, commit, path, comparator, outputType, handler, tabLength);\r
        }\r
 \r
        /**\r
@@ -297,11 +301,13 @@ public class DiffUtils {
         * @param commit\r
         * @param comparator\r
         * @param outputType\r
+        * @param tabLength\r
+        *\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       DiffComparator comparator, DiffOutputType outputType) {\r
-               return getDiff(repository, baseCommit, commit, null, comparator, outputType);\r
+                       DiffComparator comparator, DiffOutputType outputType, int tabLength) {\r
+               return getDiff(repository, baseCommit, commit, null, comparator, outputType, tabLength);\r
        }\r
 \r
        /**\r
@@ -315,11 +321,12 @@ public class DiffUtils {
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
         *            May be {@code null}, resulting in the default behavior.\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {\r
-               return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler);\r
+                       DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {\r
+               return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler, tabLength);\r
        }\r
 \r
        /**\r
@@ -335,11 +342,12 @@ public class DiffUtils {
         *            or folder. if unspecified, the diff is for the entire commit.\r
         * @param outputType\r
         * @param diffComparator\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,\r
-                       String path, DiffComparator diffComparator, DiffOutputType outputType) {\r
-               return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null);\r
+                       String path, DiffComparator diffComparator, DiffOutputType outputType, int tabLength) {\r
+               return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null, tabLength);\r
        }\r
 \r
        /**\r
@@ -358,10 +366,11 @@ public class DiffUtils {
         * @param handler\r
         *            to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.\r
         *            May be {@code null}, resulting in the default behavior.\r
+        * @param tabLength\r
         * @return the diff\r
         */\r
        public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path,\r
-                       DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler) {\r
+                       DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) {\r
                DiffStat stat = null;\r
                String diff = null;\r
                try {\r
@@ -370,7 +379,7 @@ public class DiffUtils {
                        DiffFormatter df;\r
                        switch (outputType) {\r
                        case HTML:\r
-                               df = new GitBlitDiffFormatter(commit.getName(), path, handler);\r
+                               df = new GitBlitDiffFormatter(commit.getName(), path, handler, tabLength);\r
                                break;\r
                        case PLAIN:\r
                        default:\r
index 35549d4e6e77c92a8ab2ea778e2c2f63e9adcbda..86b7ca2eda3fa744e8bde7f314643e0e161a6ca3 100644 (file)
@@ -127,6 +127,8 @@ public class GitBlitDiffFormatter extends DiffFormatter {
        /** If {@link #truncated}, contains all entries skipped. */
        private final List<DiffEntry> skipped = new ArrayList<DiffEntry>();
 
+       private int tabLength;
+
        /**
         * A {@link ResettableByteArrayOutputStream} that intercept the "Binary files differ" message produced
         * by the super implementation. Unfortunately the super implementation has far too many things private;
@@ -162,11 +164,12 @@ public class GitBlitDiffFormatter extends DiffFormatter {
 
        }
 
-       public GitBlitDiffFormatter(String commitId, String path, BinaryDiffHandler handler) {
+       public GitBlitDiffFormatter(String commitId, String path, BinaryDiffHandler handler, int tabLength) {
                super(new DiffOutputStream());
                this.os = (DiffOutputStream) getOutputStream();
                this.os.setFormatter(this, handler);
                this.diffStat = new DiffStat(commitId);
+               this.tabLength = tabLength;
                // If we have a full commitdiff, install maxima to avoid generating a super-long diff listing that
                // will only tax the browser too much.
                maxDiffLinesPerFile = path != null ? -1 : getLimit(DIFF_LIMIT_PER_FILE_KEY, 500, DIFF_LIMIT_PER_FILE);
@@ -453,14 +456,14 @@ public class GitBlitDiffFormatter extends DiffFormatter {
                        // 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()), CONVERT_TABS));
+                               StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), CONVERT_TABS, tabLength));
                                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, CONVERT_TABS);
+               return StringUtils.escapeForHtml(line, CONVERT_TABS, tabLength);
        }
 
        /**
@@ -493,7 +496,7 @@ public class GitBlitDiffFormatter extends DiffFormatter {
                                        } else {
                                                sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">");
                                        }
-                                       line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS);
+                                       line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS, tabLength);
                                }
                                sb.append(line);
                                if (gitLinkDiff) {
index 087de5430df4b0cfce7d6fc7dabb7b7b5b555d13..643c52c3a84e3da01d3e7a874a19f277794c8eb5 100644 (file)
@@ -79,6 +79,19 @@ public class StringUtils {
         * @return plain text escaped for html\r
         */\r
        public static String escapeForHtml(String inStr, boolean changeSpace) {\r
+               return escapeForHtml(inStr, changeSpace, 4);\r
+       }\r
+\r
+       /**\r
+        * Prepare text for html presentation. Replace sensitive characters with\r
+        * html entities.\r
+        *\r
+        * @param inStr\r
+        * @param changeSpace\r
+        * @param tabLength\r
+        * @return plain text escaped for html\r
+        */\r
+       public static String escapeForHtml(String inStr, boolean changeSpace, int tabLength) {\r
                StringBuilder retStr = new StringBuilder();\r
                int i = 0;\r
                while (i < inStr.length()) {\r
@@ -93,7 +106,9 @@ public class StringUtils {
                        } else if (changeSpace && inStr.charAt(i) == ' ') {\r
                                retStr.append("&nbsp;");\r
                        } else if (changeSpace && inStr.charAt(i) == '\t') {\r
-                               retStr.append(" &nbsp; &nbsp;");\r
+                               for (int j = 0; j < tabLength; j++) {\r
+                                       retStr.append("&nbsp;");\r
+                               }\r
                        } else {\r
                                retStr.append(inStr.charAt(i));\r
                        }\r
index 3c850f2929d4965b6077892bd51bb302e8c15eb9..e45bbbc816ff9f8eda99f58daa2c9376479d6fb6 100644 (file)
@@ -154,6 +154,7 @@ public class BlamePage extends RepositoryPage {
 \r
                add(new Label("missingBlob").setVisible(false));\r
 \r
+               final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);\r
                List<AnnotatedLine> lines = DiffUtils.blame(getRepository(), blobPath, objectId);\r
                final Map<?, String> colorMap = initializeColors(activeBlameType, lines);\r
                ListDataProvider<AnnotatedLine> blameDp = new ListDataProvider<AnnotatedLine>(lines);\r
@@ -212,7 +213,7 @@ public class BlamePage extends RepositoryPage {
                                        color = colorMap.get(entry.commitId);\r
                                        break;\r
                                }\r
-                               Component data = new Label("data", StringUtils.escapeForHtml(entry.data, true)).setEscapeModelStrings(false);\r
+                               Component data = new Label("data", StringUtils.escapeForHtml(entry.data, true, tabLength)).setEscapeModelStrings(false);\r
                                data.add(new SimpleAttributeModifier("style", "background-color: " + color + ";"));\r
                                item.add(data);\r
                        }\r
index 187e4600d4e49310f9eb3d82a0420d6d9551c800..adf815e6db701a2d438375b56e17d018c3fbf867 100644 (file)
@@ -57,7 +57,7 @@ public class BlobDiffPage extends RepositoryPage {
                        RevCommit parent = commit.getParentCount() == 0 ? null : commit.getParent(0);\r
                        ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,\r
                                        parent.getName(), commit.getName(), imageExtensions);\r
-                       diff = DiffUtils.getDiff(r, commit, blobPath, diffComparator, DiffOutputType.HTML, handler).content;\r
+                       diff = DiffUtils.getDiff(r, commit, blobPath, diffComparator, DiffOutputType.HTML, handler, 3).content;\r
                        if (handler.getImgDiffCount() > 0) {\r
                                addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
                        }\r
@@ -68,7 +68,7 @@ public class BlobDiffPage extends RepositoryPage {
                        RevCommit baseCommit = JGitUtils.getCommit(r, baseObjectId);\r
                        ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,\r
                                        baseCommit.getName(), commit.getName(), imageExtensions);\r
-                       diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, diffComparator, DiffOutputType.HTML, handler).content;\r
+                       diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, diffComparator, DiffOutputType.HTML, handler, 3).content;\r
                        if (handler.getImgDiffCount() > 0) {\r
                                addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
                        }\r
index df30e4ccdc60e3186db57537d9287977efd0dafe..1ef8f227b8e6b969b819d8720e4044367b1627ce 100644 (file)
@@ -195,7 +195,8 @@ public class BlobPage extends RepositoryPage {
                } else {\r
                        sb.append("<pre class=\"plainprint\">");\r
                }\r
-               lines = StringUtils.escapeForHtml(source, true).split("\n");\r
+               final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);\r
+               lines = StringUtils.escapeForHtml(source, true, tabLength).split("\n");\r
 \r
                sb.append("<table width=\"100%\"><tbody>");\r
 \r
index 95580ed154aa23693661acae3b2cdeb6a218c510..3754f3eff0afd4d865d46b49bfe1555e3506645c 100644 (file)
@@ -87,8 +87,8 @@ public class CommitDiffPage extends RepositoryPage {
                final List<String> imageExtensions = app().settings().getStrings(Keys.web.imageExtensions);
                final ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
                                parents.isEmpty() ? null : parents.get(0), commit.getName(), imageExtensions);
-
-               final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, diffComparator, DiffOutputType.HTML, handler);
+               final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);
+               final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, diffComparator, DiffOutputType.HTML, handler, tabLength);
                if (handler.getImgDiffCount() > 0) {
                        addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
                }
index d17104466abf9b193fcf586f2a0cc2f86672dd76..7e7ac2f5db4bb65d85c6af4bddd7422e3e7c1f51 100644 (file)
@@ -119,9 +119,9 @@ public class ComparePage extends RepositoryPage {
                        final List<String> imageExtensions = app().settings().getStrings(Keys.web.imageExtensions);
                        final ImageDiffHandler handler = new ImageDiffHandler(this, repositoryName,
                                        fromCommit.getName(), toCommit.getName(), imageExtensions);
-
                        final DiffComparator diffComparator = WicketUtils.getDiffComparator(params);
-                       final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, diffComparator, DiffOutputType.HTML, handler);
+                       final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);
+                       final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, diffComparator, DiffOutputType.HTML, handler, tabLength);
                        if (handler.getImgDiffCount() > 0) {
                                addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
                        }
index 8aec9e6333096bb32354b099cdfbd8517705b01f..ceca1ec22c49bc5372afde5a1c4052646c5c60d1 100644 (file)
@@ -76,8 +76,9 @@ public class ReviewProposalPage extends RootSubPage {
                sb.append(asParam(p, proposal.name, "exclude", ""));\r
                sb.append(asParam(p, proposal.name, "include", ""));\r
 \r
+               final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);\r
                add(new Label("definition", StringUtils.breakLinesForHtml(StringUtils.escapeForHtml(sb\r
-                               .toString().trim(), true))).setEscapeModelStrings(false));\r
+                               .toString().trim(), true, tabLength))).setEscapeModelStrings(false));\r
 \r
                List<RepositoryModel> repositories = new ArrayList<RepositoryModel>(\r
                                proposal.repositories.values());\r
index c73e4783dc6953f5facf323512be0aec5dd2924c..e8e839a512145ed3fd74279b63b4a08a5d750cdb 100644 (file)
@@ -41,7 +41,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getCommitDiff(repository, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getCommitDiff(repository, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r
@@ -55,7 +55,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                                "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r
@@ -67,7 +67,7 @@ public class DiffUtilsTest extends GitblitUnitTest {
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
                RevCommit commit = JGitUtils.getCommit(repository,\r
                                "1d0c2933a4ae69c362f76797d42d6bd182d05176");\r
-               String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN).content;\r
+               String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffComparator.SHOW_WHITESPACE, DiffOutputType.PLAIN, 3).content;\r
                repository.close();\r
                assertTrue(diff != null && diff.length() > 0);\r
                String expected = "-            system.out.println(\"Hello World\");\n+         System.out.println(\"Hello World\"";\r
index 0fd42aad6688236a1a50c39e52adfcd720bc7c8f..7176b88c7dd98950efcb14a0a4ea71efe93232bd 100644 (file)
@@ -50,7 +50,7 @@ public class StringUtilsTest extends GitblitUnitTest {
        public void testEscapeForHtml() throws Exception {\r
                String input = "& < > \" \t";\r
                String outputNoChange = "&amp; &lt; &gt; &quot; \t";\r
-               String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp; &nbsp; &nbsp;";\r
+               String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";\r
                assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));\r
                assertEquals(outputChange, StringUtils.escapeForHtml(input, true));\r
        }\r