Convert the issue number and the commit id to the link.

This commit is contained in:
takezoe
2013-06-02 02:30:50 +09:00
parent 0cdb7fd5c7
commit 8f9b459123
4 changed files with 31 additions and 26 deletions

View File

@@ -160,7 +160,7 @@ trait WikiControllerBase extends ControllerBase { self: WikiService =>
val repository = params("repository") val repository = params("repository")
val content = params("content") val content = params("content")
contentType = "text/html" contentType = "text/html"
view.helpers.markdown(content, JGitUtil.getRepositoryInfo(owner, repository, servletContext)) view.helpers.markdown(content, JGitUtil.getRepositoryInfo(owner, repository, servletContext), true)
} }
/** /**

View File

@@ -23,37 +23,42 @@ object helpers {
value.replaceAll(" ", "&nbsp;").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\n", "<br>")) value.replaceAll(" ", "&nbsp;").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\n", "<br>"))
/** /**
* Converts Markdown of Wiki pages to HTML. This method supports Wiki links. * Converts the issue number and the commit id to the link.
*/ */
def markdown(value: String, repository: app.RepositoryInfo)(implicit context: app.Context): twirl.api.Html = { private def markdownFilter(value: String, repository: app.RepositoryInfo)(implicit context: app.Context): String = {
import org.pegdown._ value
val html = new PegDownProcessor(Extensions.AUTOLINKS|Extensions.WIKILINKS|Extensions.FENCED_CODE_BLOCKS) .replaceAll("#([0-9]+)", "[$0](%s/%s/%s/issue/$1)".format(context.path, repository.owner, repository.name))
.markdownToHtml(value, new LinkRenderer(){ .replaceAll("[0-9a-z]{10,40}", "[$0](%s/%s/%s/commit/$0)".format(context.path, repository.owner, repository.name))
override def render(node: WikiLinkNode): Rendering = {
try {
val text = node.getText
val (label, page) = if(text.contains('|')){
val i = text.indexOf('|')
(text.substring(0, i), text.substring(i + 1))
} else {
(text, text)
}
val url = "%s/%s/%s/wiki/%s".format(context.path, repository.owner, repository.name,
java.net.URLEncoder.encode(page.replace(' ', '-'), "UTF-8"))
new Rendering(url, label);
} catch {
case e: java.io.UnsupportedEncodingException => throw new IllegalStateException();
}
}
})
twirl.api.Html(html)
} }
/** /**
* Converts Markdown to HTML. This method does not support Wiki links. * Converts Markdown of Wiki pages to HTML.
*/ */
def markdown(value: String): twirl.api.Html = { def markdown(value: String, repository: app.RepositoryInfo, wikiLink: Boolean)(implicit context: app.Context): twirl.api.Html = {
val html = new PegDownProcessor(Extensions.FENCED_CODE_BLOCKS).markdownToHtml(value, new LinkRenderer()) import org.pegdown._
val html = new PegDownProcessor(Extensions.AUTOLINKS|Extensions.WIKILINKS|Extensions.FENCED_CODE_BLOCKS)
.markdownToHtml(markdownFilter(value, repository), new LinkRenderer(){
override def render(node: WikiLinkNode): Rendering = {
if(wikiLink){
super.render(node)
} else {
try {
val text = node.getText
val (label, page) = if(text.contains('|')){
val i = text.indexOf('|')
(text.substring(0, i), text.substring(i + 1))
} else {
(text, text)
}
val url = "%s/%s/%s/wiki/%s".format(context.path, repository.owner, repository.name,
java.net.URLEncoder.encode(page.replace(' ', '-'), "UTF-8"))
new Rendering(url, label);
} catch {
case e: java.io.UnsupportedEncodingException => throw new IllegalStateException();
}
}
}
})
twirl.api.Html(html) twirl.api.Html(html)
} }

View File

@@ -55,7 +55,7 @@
@readme.map { content => @readme.map { content =>
<table class="table table-bordered"> <table class="table table-bordered">
<tr> <tr>
<td>@helpers.markdown(content)</td> <td>@helpers.markdown(content, repository, false)</td>
</tr> </tr>
</table> </table>
} }

View File

@@ -17,7 +17,7 @@
</li> </li>
</ul> </ul>
<div class="markdown-body"> <div class="markdown-body">
@helpers.markdown(page.content, repository) @helpers.markdown(page.content, repository, true)
</div> </div>
<div class="small"> <div class="small">
<span class="description">Last edited by @page.committer at @helpers.datetime(page.time)</span> <span class="description">Last edited by @page.committer at @helpers.datetime(page.time)</span>