mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
(refs #34)Link conversion checks existence of accounts and issues.
This commit is contained in:
@@ -158,7 +158,7 @@ trait IssuesControllerBase extends ControllerBase {
|
||||
org.json4s.jackson.Serialization.write(
|
||||
Map("title" -> x.title,
|
||||
"content" -> view.Markdown.toHtml(x.content getOrElse "No description given.",
|
||||
repository, false, true, true)
|
||||
repository, false, true)
|
||||
))
|
||||
}
|
||||
} else Unauthorized
|
||||
@@ -175,7 +175,7 @@ trait IssuesControllerBase extends ControllerBase {
|
||||
contentType = formats("json")
|
||||
org.json4s.jackson.Serialization.write(
|
||||
Map("content" -> view.Markdown.toHtml(x.content,
|
||||
repository, false, true, true)
|
||||
repository, false, true)
|
||||
))
|
||||
}
|
||||
} else Unauthorized
|
||||
|
||||
@@ -27,8 +27,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
||||
contentType = "text/html"
|
||||
view.helpers.markdown(params("content"), repository,
|
||||
params("enableWikiLink").toBoolean,
|
||||
params("enableCommitLink").toBoolean,
|
||||
params("enableIssueLink").toBoolean)
|
||||
params("enableRefsLink").toBoolean)
|
||||
})
|
||||
|
||||
/**
|
||||
|
||||
26
src/main/scala/service/RequestCache.scala
Normal file
26
src/main/scala/service/RequestCache.scala
Normal file
@@ -0,0 +1,26 @@
|
||||
package service
|
||||
|
||||
import model._
|
||||
|
||||
/**
|
||||
* This service is used for a view helper mainly.
|
||||
*
|
||||
* It may be called many times in one request, so each method stores
|
||||
* its result into the cache which available during a request.
|
||||
*/
|
||||
trait RequestCache {
|
||||
|
||||
def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = {
|
||||
context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){
|
||||
new IssuesService {}.getIssue(userName, repositoryName, issueId)
|
||||
}
|
||||
}
|
||||
|
||||
def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = {
|
||||
context.cache(s"account.${userName}"){
|
||||
new AccountService {}.getAccountByUserName(userName)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package util
|
||||
|
||||
import twirl.api.Html
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import scala.util.matching.Regex
|
||||
|
||||
/**
|
||||
* Provides some usable implicit conversions.
|
||||
@@ -30,4 +30,23 @@ object Implicits {
|
||||
def &&(c2: => Column[Boolean], guard: => Boolean): Column[Boolean] = if(guard) c1 && c2 else c1
|
||||
}
|
||||
|
||||
implicit class RichString(value: String){
|
||||
def replaceBy(regex: Regex)(replace: Regex.MatchData => Option[String]): String = {
|
||||
val sb = new StringBuilder()
|
||||
var i = 0
|
||||
regex.findAllIn(value).matchData.foreach { m =>
|
||||
sb.append(value.substring(i, m.start))
|
||||
i = m.end
|
||||
replace(m) match {
|
||||
case Some(s) => sb.append(s)
|
||||
case None => sb.append(m.matched)
|
||||
}
|
||||
}
|
||||
if(i < value.length){
|
||||
sb.append(value.substring(i))
|
||||
}
|
||||
sb.toString
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/scala/view/AvatarImageProvider.scala
Normal file
26
src/main/scala/view/AvatarImageProvider.scala
Normal file
@@ -0,0 +1,26 @@
|
||||
package view
|
||||
|
||||
import service.RequestCache
|
||||
import twirl.api.Html
|
||||
import util.StringUtil
|
||||
|
||||
trait AvatarImageProvider { self: RequestCache =>
|
||||
|
||||
/**
|
||||
* Returns <img> which displays the avatar icon.
|
||||
* Looks up Gravatar if avatar icon has not been configured in user settings.
|
||||
*/
|
||||
protected def getAvatarImageHtml(userName: String, size: Int, tooltip: Boolean = false)(implicit context: app.Context): Html = {
|
||||
val src = getAccountByUserName(userName).collect { case account if(account.image.isEmpty) =>
|
||||
s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}"""
|
||||
} getOrElse {
|
||||
s"""${context.path}/${userName}/_avatar"""
|
||||
}
|
||||
if(tooltip){
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" data-toggle="tooltip" title=${userName}/>""")
|
||||
} else {
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" />""")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
33
src/main/scala/view/LinkConverter.scala
Normal file
33
src/main/scala/view/LinkConverter.scala
Normal file
@@ -0,0 +1,33 @@
|
||||
package view
|
||||
|
||||
import service.RequestCache
|
||||
import util.Implicits.RichString
|
||||
|
||||
trait LinkConverter { self: RequestCache =>
|
||||
|
||||
/**
|
||||
* Converts issue id, username and commit id to link.
|
||||
*/
|
||||
protected def convertRefsLinks(value: String, repository: service.RepositoryService.RepositoryInfo,
|
||||
issueIdPrefix: String = "#")(implicit context: app.Context): String = {
|
||||
value
|
||||
// escape HTML tags
|
||||
.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """)
|
||||
// convert issue id to link
|
||||
.replaceBy(("(^|\\W)" + issueIdPrefix + "(\\d+)(\\W|$)").r){ m =>
|
||||
if(getIssue(repository.owner, repository.name, m.group(2)).isDefined){
|
||||
Some(s"""${m.group(1)}<a href="${context.path}/${repository.owner}/${repository.name}/issues/${m.group(2)}">#${m.group(2)}</a>${m.group(3)}""")
|
||||
} else {
|
||||
Some(s"""${m.group(1)}#${m.group(2)}${m.group(3)}""")
|
||||
}
|
||||
}
|
||||
// convert @username to link
|
||||
.replaceBy("(^|\\W)@([a-zA-Z0-9\\-_]+)(\\W|$)".r){ m =>
|
||||
getAccountByUserName(m.group(2)).map { _ =>
|
||||
s"""${m.group(1)}<a href="${context.path}/${m.group(2)}">@${m.group(2)}</a>${m.group(3)}"""
|
||||
}
|
||||
}
|
||||
// convert commit id to link
|
||||
.replaceAll("(^|\\W)([a-f0-9]{40})(\\W|$)", s"""$$1<a href="${context.path}/${repository.owner}/${repository.name}/commit/$$2">$$2</a>$$3""")
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package view
|
||||
|
||||
import util.StringUtil
|
||||
import util.Implicits._
|
||||
import org.parboiled.common.StringUtils
|
||||
import org.pegdown._
|
||||
import org.pegdown.ast._
|
||||
import org.pegdown.LinkRenderer.Rendering
|
||||
import scala.collection.JavaConverters._
|
||||
import service.RequestCache
|
||||
|
||||
object Markdown {
|
||||
|
||||
@@ -13,12 +15,17 @@ object Markdown {
|
||||
* Converts Markdown of Wiki pages to HTML.
|
||||
*/
|
||||
def toHtml(markdown: String, repository: service.RepositoryService.RepositoryInfo,
|
||||
enableWikiLink: Boolean, enableCommitLink: Boolean, enableIssueLink: Boolean)(implicit context: app.Context): String = {
|
||||
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): String = {
|
||||
// escape issue id
|
||||
val source = if(enableRefsLink){
|
||||
markdown.replaceAll("(^|\\W)#([0-9]+)(\\W|$)", "$1issue:$2$3")
|
||||
} else markdown
|
||||
|
||||
val rootNode = new PegDownProcessor(
|
||||
Extensions.AUTOLINKS | Extensions.WIKILINKS | Extensions.FENCED_CODE_BLOCKS | Extensions.TABLES
|
||||
).parseMarkdown(markdown.toCharArray)
|
||||
).parseMarkdown(source.toCharArray)
|
||||
|
||||
new GitBucketHtmlSerializer(markdown, context, repository, enableWikiLink, enableCommitLink, enableIssueLink).toHtml(rootNode)
|
||||
new GitBucketHtmlSerializer(markdown, repository, enableWikiLink, enableRefsLink).toHtml(rootNode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,15 +71,13 @@ class GitBucketVerbatimSerializer extends VerbatimSerializer {
|
||||
|
||||
class GitBucketHtmlSerializer(
|
||||
markdown: String,
|
||||
context: app.Context,
|
||||
repository: service.RepositoryService.RepositoryInfo,
|
||||
enableWikiLink: Boolean,
|
||||
enableCommitLink: Boolean,
|
||||
enableIssueLink: Boolean
|
||||
) extends ToHtmlSerializer(
|
||||
enableRefsLink: Boolean
|
||||
)(implicit val context: app.Context) extends ToHtmlSerializer(
|
||||
new GitBucketLinkRender(context, repository, enableWikiLink),
|
||||
Map[String, VerbatimSerializer](VerbatimSerializer.DEFAULT -> new GitBucketVerbatimSerializer).asJava
|
||||
) {
|
||||
) with LinkConverter with RequestCache {
|
||||
|
||||
override protected def printImageTag(imageNode: SuperNode, url: String): Unit =
|
||||
printer.print("<img src=\"").print(fixUrl(url)).print("\" alt=\"").printEncoded(printChildrenToString(imageNode)).print("\"/>")
|
||||
@@ -100,10 +105,7 @@ class GitBucketHtmlSerializer(
|
||||
|
||||
override def visit(node: TextNode) {
|
||||
// convert commit id and username to link.
|
||||
val text = if(enableCommitLink) node.getText
|
||||
.replaceAll("(^|\\W)([0-9a-f]{40})(\\W|$)", s"""$$1<a href="${context.path}/${repository.owner}/${repository.name}/commit/$$2">$$2</a>$$3""")
|
||||
.replaceAll("(^|\\W)@([a-zA-Z0-9\\-_]+)(\\W|$)", s"""$$1<a href="${context.path}/$$2">@$$2</a>$$3""")
|
||||
else node.getText
|
||||
val text = if(enableRefsLink) convertRefsLinks(node.getText, repository, "issue:") else node.getText
|
||||
|
||||
if (abbreviations.isEmpty) {
|
||||
printer.print(text)
|
||||
@@ -112,15 +114,4 @@ class GitBucketHtmlSerializer(
|
||||
}
|
||||
}
|
||||
|
||||
override def visit(node: HeaderNode) {
|
||||
val text = markdown.substring(node.getStartIndex, node.getEndIndex - 1).trim
|
||||
if(enableIssueLink && text.matches("#[\\d]+")){
|
||||
// convert issue id to link
|
||||
val issueId = text.substring(1).toInt
|
||||
printer.print(s"""<a href="${context.path}/${repository.owner}/${repository.name}/issues/${issueId}">#${issueId}</a>""")
|
||||
} else {
|
||||
printTag(node, "h" + node.getLevel)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,12 +3,12 @@ import java.util.Date
|
||||
import java.text.SimpleDateFormat
|
||||
import twirl.api.Html
|
||||
import util.StringUtil
|
||||
import service.AccountService
|
||||
import service.RequestCache
|
||||
|
||||
/**
|
||||
* Provides helper methods for Twirl templates.
|
||||
*/
|
||||
object helpers {
|
||||
object helpers extends AvatarImageProvider with LinkConverter with RequestCache {
|
||||
|
||||
/**
|
||||
* Format java.util.Date to "yyyy-MM-dd HH:mm:ss".
|
||||
@@ -31,10 +31,23 @@ object helpers {
|
||||
* Converts Markdown of Wiki pages to HTML.
|
||||
*/
|
||||
def markdown(value: String, repository: service.RepositoryService.RepositoryInfo,
|
||||
enableWikiLink: Boolean, enableCommitLink: Boolean, enableIssueLink: Boolean)(implicit context: app.Context): Html = {
|
||||
Html(Markdown.toHtml(value, repository, enableWikiLink, enableCommitLink, enableIssueLink))
|
||||
enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): Html = {
|
||||
Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <img> which displays the avatar icon.
|
||||
* Looks up Gravatar if avatar icon has not been configured in user settings.
|
||||
*/
|
||||
def avatar(userName: String, size: Int, tooltip: Boolean = false)(implicit context: app.Context): Html =
|
||||
getAvatarImageHtml(userName, size, tooltip)
|
||||
|
||||
/**
|
||||
* Converts commit id, issue id and username to the link.
|
||||
*/
|
||||
def link(value: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context): Html =
|
||||
Html(convertRefsLinks(value, repository))
|
||||
|
||||
def activityMessage(message: String)(implicit context: app.Context): Html =
|
||||
Html(message
|
||||
.replaceAll("\\[issue:([^\\s]+?)/([^\\s]+?)#((\\d+))\\]" , s"""<a href="${context.path}/$$1/$$2/issues/$$3">$$1/$$2#$$3</a>""")
|
||||
@@ -66,38 +79,6 @@ object helpers {
|
||||
def assets(implicit context: app.Context): String =
|
||||
s"${context.path}/assets"
|
||||
|
||||
/**
|
||||
* Converts issue id and commit id to link.
|
||||
*/
|
||||
def link(value: String, repository: service.RepositoryService.RepositoryInfo)(implicit context: app.Context): Html =
|
||||
Html(value
|
||||
// escape HTML tags
|
||||
.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """)
|
||||
// convert issue id to link
|
||||
.replaceAll("(^|\\W)#(\\d+)(\\W|$)", s"""$$1<a href="${context.path}/${repository.owner}/${repository.name}/issues/$$2">#$$2</a>$$3""")
|
||||
// convert commit id to link
|
||||
.replaceAll("(^|\\W)([a-f0-9]{40})(\\W|$)", s"""$$1<a href="${context.path}/${repository.owner}/${repository.name}/commit/$$2">$$2</a>$$3"""))
|
||||
|
||||
|
||||
/**
|
||||
* Returns <img> which displays the avatar icon.
|
||||
* Looks up Gravatar if avatar icon has not been configured in user settings.
|
||||
*/
|
||||
def avatar(userName: String, size: Int, tooltip: Boolean = false)(implicit context: app.Context): Html = {
|
||||
val account = context.cache(s"account.${userName}"){
|
||||
new AccountService {}.getAccountByUserName(userName)
|
||||
}
|
||||
val src = account.collect { case account if(account.image.isEmpty) =>
|
||||
s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}"""
|
||||
} getOrElse {
|
||||
s"""${context.path}/${userName}/_avatar"""
|
||||
}
|
||||
if(tooltip){
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" data-toggle="tooltip" title=${userName}/>""")
|
||||
} else {
|
||||
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" />""")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implicit conversion to add mkHtml() to Seq[Html].
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
@(repository: service.RepositoryService.RepositoryInfo, content: String, enableWikiLink: Boolean,
|
||||
enableCommitLink: Boolean, enableIssueLink: Boolean, style: String = "", placeholder: String = "Leave a comment")(implicit context: app.Context)
|
||||
@(repository: service.RepositoryService.RepositoryInfo, content: String, enableWikiLink: Boolean, enableRefsLink: Boolean,
|
||||
style: String = "", placeholder: String = "Leave a comment")(implicit context: app.Context)
|
||||
@import context._
|
||||
@import view.helpers._
|
||||
<div class="tabbable">
|
||||
@@ -30,10 +30,9 @@ $(function(){
|
||||
$('#preview').click(function(){
|
||||
$('#preview-area').html('<img src="@assets/common/images/indicator.gif"> Previewing...');
|
||||
$.post('@url(repository)/_preview', {
|
||||
content : $('#content').val(),
|
||||
enableWikiLink : @enableWikiLink,
|
||||
enableCommitLink : @enableCommitLink,
|
||||
enableIssueLink : @enableIssueLink
|
||||
content : $('#content').val(),
|
||||
enableWikiLink : @enableWikiLink,
|
||||
enableRefsLink : @enableRefsLink
|
||||
}, function(data){
|
||||
$('#preview-area').html(data);
|
||||
prettyPrint();
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<hr>
|
||||
@helper.html.preview(repository, "", false, true, true, "width: 600px; height: 200px;")
|
||||
@helper.html.preview(repository, "", false, true, "width: 600px; height: 200px;")
|
||||
</div>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="issue-content" id="issueContent">
|
||||
@markdown(issue.content getOrElse "No description given.", repository, false, true, true)
|
||||
@markdown(issue.content getOrElse "No description given.", repository, false, true)
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -82,7 +82,7 @@
|
||||
</span>
|
||||
</div>
|
||||
<div class="box-content"class="issue-content" id="commentContent-@comment.commentId">
|
||||
@markdown(comment.content, repository, false, true, true)
|
||||
@markdown(comment.content, repository, false, true)
|
||||
</div>
|
||||
</div>
|
||||
@comment.action.map { action =>
|
||||
@@ -102,7 +102,7 @@
|
||||
<div class="issue-avatar-image">@avatar(loginAccount.get.userName, 48)</div>
|
||||
<div class="box issue-comment-box">
|
||||
<div class="box-content">
|
||||
@helper.html.preview(repository, "", false, true, true, "width: 680px; height: 100px;")
|
||||
@helper.html.preview(repository, "", false, true, "width: 680px; height: 100px;")
|
||||
</div>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
</div>
|
||||
@if(milestone.description.isDefined){
|
||||
<div class="milestone-description">
|
||||
@markdown(milestone.description.get, repository, false, false, false)
|
||||
@markdown(milestone.description.get, repository, false, false)
|
||||
</div>
|
||||
}
|
||||
</td>
|
||||
|
||||
@@ -27,8 +27,6 @@
|
||||
<a href="@url(repository)/commit/@latestCommit.id" class="commit-message">@link(latestCommit.summary, repository)</a>
|
||||
@if(latestCommit.description.isDefined){
|
||||
<a href="javascript:void(0)" onclick="$('#description-@latestCommit.id').toggle();" class="omit">...</a>
|
||||
}
|
||||
@if(latestCommit.description.isDefined){
|
||||
<pre id="description-@latestCommit.id" class="commit-description" style="display: none;">@link(latestCommit.description.get, repository)</pre>
|
||||
}
|
||||
</th>
|
||||
@@ -79,7 +77,7 @@
|
||||
@readme.map { content =>
|
||||
<div class="box">
|
||||
<div class="box-header">README.md</div>
|
||||
<div class="box-content">@markdown(content, repository, false, false, false)</div>
|
||||
<div class="box-content">@markdown(content, repository, false, false)</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
<form action="@url(repository)/wiki/@if(pageName == ""){_new} else {_edit}" method="POST" validate="true">
|
||||
<span id="error-pageName" class="error"></span>
|
||||
<input type="text" name="pageName" value="@pageName" style="width: 900px; font-weight: bold;" placeholder="Input a page name."/>
|
||||
@helper.html.preview(repository, page.map(_.content).getOrElse(""), true, false, false, "width: 900px; height: 400px;", "")
|
||||
@helper.html.preview(repository, page.map(_.content).getOrElse(""), true, false, "width: 900px; height: 400px;", "")
|
||||
<input type="text" name="message" value="" style="width: 900px;" placeholder="Write a small message here explaining this change. (Optional)"/>
|
||||
<input type="hidden" name="currentPageName" value="@pageName"/>
|
||||
<input type="submit" value="Save" class="btn btn-success">
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
</li>
|
||||
</ul>
|
||||
<div class="markdown-body">
|
||||
@markdown(page.content, repository, true, false, false)
|
||||
@markdown(page.content, repository, true, false)
|
||||
</div>
|
||||
<div class="small">
|
||||
<span class="muted">Last edited by @page.committer at @datetime(page.time)</span>
|
||||
|
||||
Reference in New Issue
Block a user