Add syntax highlighting in Wiki.

This commit is contained in:
takezoe
2013-06-07 03:14:08 +09:00
parent d293493b0e
commit 5e6e69d078
3 changed files with 10 additions and 36 deletions

View File

@@ -24,45 +24,11 @@ object helpers {
def format(value: String): Html = Html(
value.replaceAll(" ", "&nbsp;").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\n", "<br>"))
/**
* Converts the issue number and the commit id to the link.
*/
private def markdownFilter(value: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context): String = {
value
.replaceAll("#([0-9]+)", "[$0](%s/%s/%s/issue/$1)".format(context.path, repository.owner, repository.name))
.replaceAll("[0-9a-f]{40}", "[$0](%s/%s/%s/commit/$0)".format(context.path, repository.owner, repository.name))
}
/**
* Converts Markdown of Wiki pages to HTML.
*/
def markdown(value: String, repository: service.RepositoryService.RepositoryInfo, wikiLink: Boolean)(implicit context: app.Context): twirl.api.Html = {
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();
}
}
}
})
Html(html)
Html(Markdown.toHtml(value, repository, wikiLink))
}
/**

View File

@@ -16,6 +16,8 @@
</div>
</div>
</div>
<link href="@path/assets/google-code-prettify/prettify.css" type="text/css" rel="stylesheet"/>
<script src="@path/assets/google-code-prettify/prettify.js"></script>
<script>
$(function(){
$('#preview').click(function(){
@@ -24,6 +26,7 @@ $(function(){
content: $('#content').val()
}, function(data){
$('#preview-area').html(data);
prettyPrint();
});
});
});

View File

@@ -24,4 +24,9 @@
<div class="small">
<span class="description">Last edited by @page.committer at @helpers.datetime(page.time)</span>
</div>
}
}
<link href="@path/assets/google-code-prettify/prettify.css" type="text/css" rel="stylesheet"/>
<script src="@path/assets/google-code-prettify/prettify.js"></script>
<script>
$(function(){ prettyPrint(); });
</script>