]> source.dussan.org Git - gitblit.git/commitdiff
Add simplified transformGFM method to MarkdownUtils
authorJames Moger <james.moger@gitblit.com>
Fri, 28 Feb 2014 20:21:03 +0000 (15:21 -0500)
committerJames Moger <james.moger@gitblit.com>
Sat, 1 Mar 2014 14:21:32 +0000 (09:21 -0500)
src/main/java/com/gitblit/utils/MarkdownUtils.java

index 11a5e340803d5b4f4a26666a326f5c38d977ebe9..2ce6f56638dff33f4f12be5563a5a7299c08b020 100644 (file)
@@ -21,11 +21,15 @@ import static org.pegdown.Extensions.SMARTYPANTS;
 import java.io.IOException;\r
 import java.io.Reader;\r
 import java.io.StringWriter;\r
+import java.text.MessageFormat;\r
 \r
 import org.apache.commons.io.IOUtils;\r
 import org.pegdown.LinkRenderer;\r
 import org.pegdown.PegDownProcessor;\r
 \r
+import com.gitblit.IStoredSettings;\r
+import com.gitblit.Keys;\r
+\r
 /**\r
  * Utility methods for transforming raw markdown text to html.\r
  *\r
@@ -96,4 +100,45 @@ public class MarkdownUtils {
                        }\r
                }\r
        }\r
+\r
+\r
+       /**\r
+        * Transforms GFM (Github Flavored Markdown) to html.\r
+        * Gitblit does not support the complete GFM specification.\r
+        *\r
+        * @param input\r
+        * @param repositoryName\r
+        * @return html\r
+        */\r
+       public static String transformGFM(IStoredSettings settings, String input, String repositoryName) {\r
+               String text = input;\r
+\r
+               // strikethrough\r
+               text = text.replaceAll("~~(.*)~~", "<s>$1</s>");\r
+               text = text.replaceAll("\\{(?:-){2}(.*)(?:-){2}}", "<s>$1</s>");\r
+\r
+               // underline\r
+               text = text.replaceAll("\\{(?:\\+){2}(.*)(?:\\+){2}}", "<u>$1</u>");\r
+\r
+               // strikethrough, replacement\r
+               text = text.replaceAll("\\{~~(.*)~>(.*)~~}", "<s>$1</s><u>$2</u>");\r
+\r
+               // highlight\r
+               text = text.replaceAll("\\{==(.*)==}", "<span class='highlight'>$1</span>");\r
+\r
+               String canonicalUrl = settings.getString(Keys.web.canonicalUrl, "https://localhost:8443");\r
+\r
+               // emphasize and link mentions\r
+               String mentionReplacement = String.format(" **<a href=\"%1s/user/$1\">@$1</a>**", canonicalUrl);\r
+               text = text.replaceAll("\\s@([A-Za-z0-9-_]+)", mentionReplacement);\r
+\r
+               // link commit shas\r
+               int shaLen = settings.getInteger(Keys.web.shortCommitIdLength, 6);\r
+               String commitPattern = MessageFormat.format("\\s([A-Fa-f0-9]'{'{0}'}')([A-Fa-f0-9]'{'{1}'}')", shaLen, 40 - shaLen);\r
+               String commitReplacement = String.format(" <a class='commit' href='%1$s/commit?r=%2$s&h=$1$2'>$1</a>", canonicalUrl, repositoryName);\r
+               text = text.replaceAll(commitPattern, commitReplacement);\r
+\r
+               String html = transformMarkdown(text);\r
+               return html;\r
+       }\r
 }\r