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
}\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