mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-02 19:45:57 +01:00
Initial support for rendering asciidoc files.
This commit is contained in:
@@ -251,7 +251,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private val readmeFiles = Seq("readme.md", "readme.markdown")
|
private val readmeFiles = view.helpers.renderableSuffixes.map(suffix => s"readme${suffix}")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides HTML of the file list.
|
* Provides HTML of the file list.
|
||||||
|
|||||||
30
src/main/scala/view/Asciidoc.scala
Normal file
30
src/main/scala/view/Asciidoc.scala
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package view
|
||||||
|
|
||||||
|
import util.StringUtil
|
||||||
|
import util.ControlUtil._
|
||||||
|
import util.Directory._
|
||||||
|
import org.parboiled.common.StringUtils
|
||||||
|
import org.pegdown._
|
||||||
|
import org.pegdown.ast._
|
||||||
|
import org.pegdown.LinkRenderer.Rendering
|
||||||
|
import java.text.Normalizer
|
||||||
|
import java.util.Locale
|
||||||
|
import scala.collection.JavaConverters._
|
||||||
|
import service.{ RequestCache, WikiService }
|
||||||
|
import org.asciidoctor.{ Asciidoctor, OptionsBuilder, SafeMode }
|
||||||
|
|
||||||
|
object Asciidoc {
|
||||||
|
|
||||||
|
private[this] lazy val asciidoctor = Asciidoctor.Factory.create()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts Markdown of Wiki pages to HTML.
|
||||||
|
*/
|
||||||
|
def toHtml(asciidoc: String, repository: service.RepositoryService.RepositoryInfo,
|
||||||
|
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): String = {
|
||||||
|
val options = OptionsBuilder.options()
|
||||||
|
options.safe(SafeMode.SECURE)
|
||||||
|
asciidoctor.render(asciidoc, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -27,6 +27,16 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
|
|||||||
def plural(count: Int, singular: String, plural: String = ""): String =
|
def plural(count: Int, singular: String, plural: String = ""): String =
|
||||||
if(count == 1) singular else if(plural.isEmpty) singular + "s" else plural
|
if(count == 1) singular else if(plural.isEmpty) singular + "s" else plural
|
||||||
|
|
||||||
|
private[this] val renderersBySuffix: Seq[(String, (String, service.RepositoryService.RepositoryInfo, Boolean, Boolean, app.Context) => Html)] =
|
||||||
|
Seq(
|
||||||
|
".md" -> ((fileContent, repository, enableWikiLink, enableRefsLink, context) => markdown(fileContent, repository, enableWikiLink, enableRefsLink)(context)),
|
||||||
|
".markdown" -> ((fileContent, repository, enableWikiLink, enableRefsLink, context) => markdown(fileContent, repository, enableWikiLink, enableRefsLink)(context)),
|
||||||
|
".adoc" -> ((fileContent, repository, enableWikiLink, enableRefsLink, context) => asciidoc(fileContent, repository, enableWikiLink, enableRefsLink)(context)),
|
||||||
|
".asciidoc" -> ((fileContent, repository, enableWikiLink, enableRefsLink, context) => asciidoc(fileContent, repository, enableWikiLink, enableRefsLink)(context))
|
||||||
|
)
|
||||||
|
|
||||||
|
def renderableSuffixes: Seq[String] = renderersBySuffix.map(_._1)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts Markdown of Wiki pages to HTML.
|
* Converts Markdown of Wiki pages to HTML.
|
||||||
*/
|
*/
|
||||||
@@ -34,6 +44,21 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
|
|||||||
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): Html =
|
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): Html =
|
||||||
Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink))
|
Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink))
|
||||||
|
|
||||||
|
def renderMarkup(fileName: String, fileContent: String,
|
||||||
|
repository: service.RepositoryService.RepositoryInfo,
|
||||||
|
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): Html = {
|
||||||
|
|
||||||
|
val fileNameLower = fileName.toLowerCase
|
||||||
|
renderersBySuffix.find { case (suffix, _) => fileNameLower.endsWith(suffix) } match {
|
||||||
|
case Some((_, handler)) => handler(fileContent, repository, enableWikiLink, enableRefsLink, context)
|
||||||
|
case None => Html("UNSUPPORTED MARKUP TYPE")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def asciidoc(value: String, repository: service.RepositoryService.RepositoryInfo,
|
||||||
|
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): Html =
|
||||||
|
Html(Asciidoc.toHtml(value, repository, enableWikiLink, enableRefsLink))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns <img> which displays the avatar icon for the given user name.
|
* Returns <img> which displays the avatar icon for the given user name.
|
||||||
* This method looks up Gravatar if avatar icon has not been configured in user settings.
|
* This method looks up Gravatar if avatar icon has not been configured in user settings.
|
||||||
|
|||||||
@@ -80,7 +80,7 @@
|
|||||||
@readme.map { case(file, content) =>
|
@readme.map { case(file, content) =>
|
||||||
<div id="readme" class="box">
|
<div id="readme" class="box">
|
||||||
<div class="box-header">@file.name</div>
|
<div class="box-header">@file.name</div>
|
||||||
<div class="box-content markdown-body">@markdown(content, repository, false, false)</div>
|
<div class="box-content markdown-body">@renderMarkup(file.name, content, repository, false, false)</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user