mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-14 09:25:49 +01:00
Switch markdown processor to markedj from pegdown
This commit is contained in:
@@ -50,7 +50,8 @@ object MyBuild extends Build {
|
|||||||
"org.json4s" %% "json4s-jackson" % "3.2.11",
|
"org.json4s" %% "json4s-jackson" % "3.2.11",
|
||||||
"jp.sf.amateras" %% "scalatra-forms" % "0.1.0",
|
"jp.sf.amateras" %% "scalatra-forms" % "0.1.0",
|
||||||
"commons-io" % "commons-io" % "2.4",
|
"commons-io" % "commons-io" % "2.4",
|
||||||
"org.pegdown" % "pegdown" % "1.5.0",
|
// "org.pegdown" % "pegdown" % "1.5.0",
|
||||||
|
"io.github.gitbucket" % "markedj" % "1.0.0",
|
||||||
"org.apache.commons" % "commons-compress" % "1.9",
|
"org.apache.commons" % "commons-compress" % "1.9",
|
||||||
"org.apache.commons" % "commons-email" % "1.3.3",
|
"org.apache.commons" % "commons-email" % "1.3.3",
|
||||||
"org.apache.httpcomponents" % "httpclient" % "4.3.6",
|
"org.apache.httpcomponents" % "httpclient" % "4.3.6",
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import java.util.regex.Pattern
|
|||||||
import gitbucket.core.controller.Context
|
import gitbucket.core.controller.Context
|
||||||
import gitbucket.core.service.{RepositoryService, RequestCache, WikiService}
|
import gitbucket.core.service.{RepositoryService, RequestCache, WikiService}
|
||||||
import gitbucket.core.util.StringUtil
|
import gitbucket.core.util.StringUtil
|
||||||
import org.parboiled.common.StringUtils
|
import io.github.gitbucket.markedj._
|
||||||
import org.pegdown.LinkRenderer.Rendering
|
//import org.parboiled.common.StringUtils
|
||||||
import org.pegdown._
|
//import org.pegdown.LinkRenderer.Rendering
|
||||||
import org.pegdown.ast._
|
//import org.pegdown._
|
||||||
|
//import org.pegdown.ast._
|
||||||
|
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
|
|
||||||
@@ -35,241 +36,241 @@ object Markdown {
|
|||||||
enableTaskList: Boolean = false,
|
enableTaskList: Boolean = false,
|
||||||
hasWritePermission: Boolean = false,
|
hasWritePermission: Boolean = false,
|
||||||
pages: List[String] = Nil)(implicit context: Context): String = {
|
pages: List[String] = Nil)(implicit context: Context): String = {
|
||||||
|
Marked.marked(markdown, new Options())
|
||||||
// escape issue id
|
// // escape issue id
|
||||||
val s = if(enableRefsLink){
|
// val s = if(enableRefsLink){
|
||||||
markdown.replaceAll("(?<=(\\W|^))#(\\d+)(?=(\\W|$))", "issue:$2")
|
// markdown.replaceAll("(?<=(\\W|^))#(\\d+)(?=(\\W|$))", "issue:$2")
|
||||||
} else markdown
|
// } else markdown
|
||||||
|
//
|
||||||
// escape task list
|
// // escape task list
|
||||||
val source = if(enableTaskList){
|
// val source = if(enableTaskList){
|
||||||
GitBucketHtmlSerializer.escapeTaskList(s)
|
// GitBucketHtmlSerializer.escapeTaskList(s)
|
||||||
} else s
|
// } else s
|
||||||
|
//
|
||||||
val rootNode = new PegDownProcessor(
|
// val rootNode = new PegDownProcessor(
|
||||||
Extensions.AUTOLINKS | Extensions.WIKILINKS | Extensions.FENCED_CODE_BLOCKS |
|
// Extensions.AUTOLINKS | Extensions.WIKILINKS | Extensions.FENCED_CODE_BLOCKS |
|
||||||
Extensions.TABLES | Extensions.HARDWRAPS | Extensions.SUPPRESS_ALL_HTML | Extensions.STRIKETHROUGH
|
// Extensions.TABLES | Extensions.HARDWRAPS | Extensions.SUPPRESS_ALL_HTML | Extensions.STRIKETHROUGH
|
||||||
).parseMarkdown(source.toCharArray)
|
// ).parseMarkdown(source.toCharArray)
|
||||||
|
//
|
||||||
new GitBucketHtmlSerializer(
|
// new GitBucketHtmlSerializer(
|
||||||
markdown, repository, enableWikiLink, enableRefsLink, enableAnchor, enableTaskList,
|
// markdown, repository, enableWikiLink, enableRefsLink, enableAnchor, enableTaskList,
|
||||||
hasWritePermission, pages
|
// hasWritePermission, pages
|
||||||
).toHtml(rootNode)
|
// ).toHtml(rootNode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GitBucketLinkRender(
|
//class GitBucketLinkRender(
|
||||||
context: Context,
|
// context: Context,
|
||||||
repository: RepositoryService.RepositoryInfo,
|
// repository: RepositoryService.RepositoryInfo,
|
||||||
enableWikiLink: Boolean,
|
// enableWikiLink: Boolean,
|
||||||
pages: List[String]) extends LinkRenderer with WikiService {
|
// pages: List[String]) extends LinkRenderer with WikiService {
|
||||||
|
//
|
||||||
override def render(node: WikiLinkNode): Rendering = {
|
// override def render(node: WikiLinkNode): Rendering = {
|
||||||
if(enableWikiLink){
|
// if(enableWikiLink){
|
||||||
try {
|
// try {
|
||||||
val text = node.getText
|
// val text = node.getText
|
||||||
val (label, page) = if(text.contains('|')){
|
// val (label, page) = if(text.contains('|')){
|
||||||
val i = text.indexOf('|')
|
// val i = text.indexOf('|')
|
||||||
(text.substring(0, i), text.substring(i + 1))
|
// (text.substring(0, i), text.substring(i + 1))
|
||||||
} else {
|
// } else {
|
||||||
(text, text)
|
// (text, text)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
|
// val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
|
||||||
|
//
|
||||||
if(pages.contains(page)){
|
// if(pages.contains(page)){
|
||||||
new Rendering(url, label)
|
// new Rendering(url, label)
|
||||||
} else {
|
// } else {
|
||||||
new Rendering(url, label).withAttribute("class", "absent")
|
// new Rendering(url, label).withAttribute("class", "absent")
|
||||||
}
|
// }
|
||||||
} catch {
|
// } catch {
|
||||||
case e: java.io.UnsupportedEncodingException => throw new IllegalStateException
|
// case e: java.io.UnsupportedEncodingException => throw new IllegalStateException
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
super.render(node)
|
// super.render(node)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
class GitBucketVerbatimSerializer extends VerbatimSerializer {
|
//class GitBucketVerbatimSerializer extends VerbatimSerializer {
|
||||||
def serialize(node: VerbatimNode, printer: Printer): Unit = {
|
// def serialize(node: VerbatimNode, printer: Printer): Unit = {
|
||||||
printer.println.print("<pre")
|
// printer.println.print("<pre")
|
||||||
if (!StringUtils.isEmpty(node.getType)) {
|
// if (!StringUtils.isEmpty(node.getType)) {
|
||||||
printer.print(" class=").print('"').print("prettyprint ").print(node.getType).print('"')
|
// printer.print(" class=").print('"').print("prettyprint ").print(node.getType).print('"')
|
||||||
}
|
// }
|
||||||
printer.print(">")
|
// printer.print(">")
|
||||||
var text: String = node.getText
|
// var text: String = node.getText
|
||||||
while (text.charAt(0) == '\n') {
|
// while (text.charAt(0) == '\n') {
|
||||||
printer.print("<br/>")
|
// printer.print("<br/>")
|
||||||
text = text.substring(1)
|
// text = text.substring(1)
|
||||||
}
|
// }
|
||||||
printer.printEncoded(text)
|
// printer.printEncoded(text)
|
||||||
printer.print("</pre>")
|
// printer.print("</pre>")
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
class GitBucketHtmlSerializer(
|
//class GitBucketHtmlSerializer(
|
||||||
markdown: String,
|
// markdown: String,
|
||||||
repository: RepositoryService.RepositoryInfo,
|
// repository: RepositoryService.RepositoryInfo,
|
||||||
enableWikiLink: Boolean,
|
// enableWikiLink: Boolean,
|
||||||
enableRefsLink: Boolean,
|
// enableRefsLink: Boolean,
|
||||||
enableAnchor: Boolean,
|
// enableAnchor: Boolean,
|
||||||
enableTaskList: Boolean,
|
// enableTaskList: Boolean,
|
||||||
hasWritePermission: Boolean,
|
// hasWritePermission: Boolean,
|
||||||
pages: List[String]
|
// pages: List[String]
|
||||||
)(implicit val context: Context) extends ToHtmlSerializer(
|
// )(implicit val context: Context) extends ToHtmlSerializer(
|
||||||
new GitBucketLinkRender(context, repository, enableWikiLink, pages),
|
// new GitBucketLinkRender(context, repository, enableWikiLink, pages),
|
||||||
Map[String, VerbatimSerializer](VerbatimSerializer.DEFAULT -> new GitBucketVerbatimSerializer).asJava
|
// Map[String, VerbatimSerializer](VerbatimSerializer.DEFAULT -> new GitBucketVerbatimSerializer).asJava
|
||||||
) with LinkConverter with RequestCache {
|
// ) with LinkConverter with RequestCache {
|
||||||
|
//
|
||||||
override protected def printImageTag(rendering: LinkRenderer.Rendering): Unit = {
|
// override protected def printImageTag(rendering: LinkRenderer.Rendering): Unit = {
|
||||||
printer.print("<a target=\"_blank\" href=\"").print(fixUrl(rendering.href, true)).print("\">")
|
// printer.print("<a target=\"_blank\" href=\"").print(fixUrl(rendering.href, true)).print("\">")
|
||||||
.print("<img src=\"").print(fixUrl(rendering.href, true)).print("\" alt=\"").printEncoded(rendering.text).print("\"/></a>")
|
// .print("<img src=\"").print(fixUrl(rendering.href, true)).print("\" alt=\"").printEncoded(rendering.text).print("\"/></a>")
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override protected def printLink(rendering: LinkRenderer.Rendering): Unit = {
|
// override protected def printLink(rendering: LinkRenderer.Rendering): Unit = {
|
||||||
printer.print('<').print('a')
|
// printer.print('<').print('a')
|
||||||
printAttribute("href", fixUrl(rendering.href))
|
// printAttribute("href", fixUrl(rendering.href))
|
||||||
for (attr <- rendering.attributes.asScala) {
|
// for (attr <- rendering.attributes.asScala) {
|
||||||
printAttribute(attr.name, attr.value)
|
// printAttribute(attr.name, attr.value)
|
||||||
}
|
// }
|
||||||
printer.print('>').print(rendering.text).print("</a>")
|
// printer.print('>').print(rendering.text).print("</a>")
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private def fixUrl(url: String, isImage: Boolean = false): String = {
|
// private def fixUrl(url: String, isImage: Boolean = false): String = {
|
||||||
if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){
|
// if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){
|
||||||
url
|
// url
|
||||||
} else if(url.startsWith("#")){
|
// } else if(url.startsWith("#")){
|
||||||
("#" + GitBucketHtmlSerializer.generateAnchorName(url.substring(1)))
|
// ("#" + GitBucketHtmlSerializer.generateAnchorName(url.substring(1)))
|
||||||
} else if(!enableWikiLink){
|
// } else if(!enableWikiLink){
|
||||||
if(context.currentPath.contains("/blob/")){
|
// if(context.currentPath.contains("/blob/")){
|
||||||
url + (if(isImage) "?raw=true" else "")
|
// url + (if(isImage) "?raw=true" else "")
|
||||||
} else if(context.currentPath.contains("/tree/")){
|
// } else if(context.currentPath.contains("/tree/")){
|
||||||
val paths = context.currentPath.split("/")
|
// val paths = context.currentPath.split("/")
|
||||||
val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch
|
// val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch
|
||||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||||
} else {
|
// } else {
|
||||||
val paths = context.currentPath.split("/")
|
// val paths = context.currentPath.split("/")
|
||||||
val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch
|
// val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch
|
||||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
|
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private def printAttribute(name: String, value: String): Unit = {
|
// private def printAttribute(name: String, value: String): Unit = {
|
||||||
printer.print(' ').print(name).print('=').print('"').print(value).print('"')
|
// printer.print(' ').print(name).print('=').print('"').print(value).print('"')
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private def printHeaderTag(node: HeaderNode): Unit = {
|
// private def printHeaderTag(node: HeaderNode): Unit = {
|
||||||
val tag = s"h${node.getLevel}"
|
// val tag = s"h${node.getLevel}"
|
||||||
val child = node.getChildren.asScala.headOption
|
// val child = node.getChildren.asScala.headOption
|
||||||
val anchorName = child match {
|
// val anchorName = child match {
|
||||||
case Some(x: AnchorLinkNode) => x.getName
|
// case Some(x: AnchorLinkNode) => x.getName
|
||||||
case Some(x: TextNode) => x.getText
|
// case Some(x: TextNode) => x.getText
|
||||||
case _ => GitBucketHtmlSerializer.generateAnchorName(extractText(node)) // TODO
|
// case _ => GitBucketHtmlSerializer.generateAnchorName(extractText(node)) // TODO
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
printer.print(s"""<$tag class="markdown-head">""")
|
// printer.print(s"""<$tag class="markdown-head">""")
|
||||||
if(enableAnchor){
|
// if(enableAnchor){
|
||||||
printer.print(s"""<a class="markdown-anchor-link" href="#$anchorName"></a>""")
|
// printer.print(s"""<a class="markdown-anchor-link" href="#$anchorName"></a>""")
|
||||||
printer.print(s"""<a class="markdown-anchor" name="$anchorName"></a>""")
|
// printer.print(s"""<a class="markdown-anchor" name="$anchorName"></a>""")
|
||||||
}
|
// }
|
||||||
child match {
|
// child match {
|
||||||
case Some(x: AnchorLinkNode) => printer.print(x.getText)
|
// case Some(x: AnchorLinkNode) => printer.print(x.getText)
|
||||||
case _ => visitChildren(node)
|
// case _ => visitChildren(node)
|
||||||
}
|
// }
|
||||||
printer.print(s"</$tag>")
|
// printer.print(s"</$tag>")
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private def extractText(node: Node): String = {
|
// private def extractText(node: Node): String = {
|
||||||
val sb = new StringBuilder()
|
// val sb = new StringBuilder()
|
||||||
node.getChildren.asScala.map {
|
// node.getChildren.asScala.map {
|
||||||
case x: TextNode => sb.append(x.getText)
|
// case x: TextNode => sb.append(x.getText)
|
||||||
case x: Node => sb.append(extractText(x))
|
// case x: Node => sb.append(extractText(x))
|
||||||
}
|
// }
|
||||||
sb.toString()
|
// sb.toString()
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: HeaderNode): Unit = {
|
// override def visit(node: HeaderNode): Unit = {
|
||||||
printHeaderTag(node)
|
// printHeaderTag(node)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: TextNode): Unit = {
|
// override def visit(node: TextNode): Unit = {
|
||||||
// convert commit id and username to link.
|
// // convert commit id and username to link.
|
||||||
val t = if(enableRefsLink) convertRefsLinks(node.getText, repository, "issue:") else node.getText
|
// val t = if(enableRefsLink) convertRefsLinks(node.getText, repository, "issue:") else node.getText
|
||||||
|
//
|
||||||
// convert task list to checkbox.
|
// // convert task list to checkbox.
|
||||||
val text = if(enableTaskList) GitBucketHtmlSerializer.convertCheckBox(t, hasWritePermission) else t
|
// val text = if(enableTaskList) GitBucketHtmlSerializer.convertCheckBox(t, hasWritePermission) else t
|
||||||
|
//
|
||||||
if (abbreviations.isEmpty) {
|
// if (abbreviations.isEmpty) {
|
||||||
printer.print(text)
|
// printer.print(text)
|
||||||
} else {
|
// } else {
|
||||||
printWithAbbreviations(text)
|
// printWithAbbreviations(text)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: VerbatimNode) {
|
// override def visit(node: VerbatimNode) {
|
||||||
val printer = new Printer()
|
// val printer = new Printer()
|
||||||
val serializer = verbatimSerializers.get(VerbatimSerializer.DEFAULT)
|
// val serializer = verbatimSerializers.get(VerbatimSerializer.DEFAULT)
|
||||||
serializer.serialize(node, printer)
|
// serializer.serialize(node, printer)
|
||||||
val html = printer.getString
|
// val html = printer.getString
|
||||||
|
//
|
||||||
// convert commit id and username to link.
|
// // convert commit id and username to link.
|
||||||
val t = if(enableRefsLink) convertRefsLinks(html, repository, "issue:", escapeHtml = false) else html
|
// val t = if(enableRefsLink) convertRefsLinks(html, repository, "issue:", escapeHtml = false) else html
|
||||||
|
//
|
||||||
this.printer.print(t)
|
// this.printer.print(t)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: BulletListNode): Unit = {
|
// override def visit(node: BulletListNode): Unit = {
|
||||||
if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) {
|
// if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) {
|
||||||
printer.println().print("""<ul class="task-list">""").indent(+2)
|
// printer.println().print("""<ul class="task-list">""").indent(+2)
|
||||||
visitChildren(node)
|
// visitChildren(node)
|
||||||
printer.indent(-2).println().print("</ul>")
|
// printer.indent(-2).println().print("</ul>")
|
||||||
} else {
|
// } else {
|
||||||
printIndentedTag(node, "ul")
|
// printIndentedTag(node, "ul")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: ListItemNode): Unit = {
|
// override def visit(node: ListItemNode): Unit = {
|
||||||
if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) {
|
// if (printChildrenToString(node).contains("""class="task-list-item-checkbox" """)) {
|
||||||
printer.println()
|
// printer.println()
|
||||||
printer.print("""<li class="task-list-item">""")
|
// printer.print("""<li class="task-list-item">""")
|
||||||
visitChildren(node)
|
// visitChildren(node)
|
||||||
printer.print("</li>")
|
// printer.print("</li>")
|
||||||
} else {
|
// } else {
|
||||||
printer.println()
|
// printer.println()
|
||||||
printTag(node, "li")
|
// printTag(node, "li")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
override def visit(node: ExpLinkNode) {
|
// override def visit(node: ExpLinkNode) {
|
||||||
printLink(linkRenderer.render(node, printLinkChildrenToString(node)))
|
// printLink(linkRenderer.render(node, printLinkChildrenToString(node)))
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
def printLinkChildrenToString(node: SuperNode) = {
|
// def printLinkChildrenToString(node: SuperNode) = {
|
||||||
val priorPrinter = printer
|
// val priorPrinter = printer
|
||||||
printer = new Printer()
|
// printer = new Printer()
|
||||||
visitLinkChildren(node)
|
// visitLinkChildren(node)
|
||||||
val result = printer.getString()
|
// val result = printer.getString()
|
||||||
printer = priorPrinter
|
// printer = priorPrinter
|
||||||
result
|
// result
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
def visitLinkChildren(node: SuperNode) {
|
// def visitLinkChildren(node: SuperNode) {
|
||||||
import scala.collection.JavaConversions._
|
// import scala.collection.JavaConversions._
|
||||||
node.getChildren.foreach(child => child match {
|
// node.getChildren.foreach(child => child match {
|
||||||
case node: ExpImageNode => visitLinkChild(node)
|
// case node: ExpImageNode => visitLinkChild(node)
|
||||||
case node: SuperNode => visitLinkChildren(node)
|
// case node: SuperNode => visitLinkChildren(node)
|
||||||
case _ => child.accept(this)
|
// case _ => child.accept(this)
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
def visitLinkChild(node: ExpImageNode) {
|
// def visitLinkChild(node: ExpImageNode) {
|
||||||
printer.print("<img src=\"").print(fixUrl(node.url, true)).print("\" alt=\"").printEncoded(printChildrenToString(node)).print("\"/>")
|
// printer.print("<img src=\"").print(fixUrl(node.url, true)).print("\" alt=\"").printEncoded(printChildrenToString(node)).print("\"/>")
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
object GitBucketHtmlSerializer {
|
object GitBucketHtmlSerializer {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user