mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
(refs #34)Link conversion checks existence of accounts and issues.
This commit is contained in:
@@ -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].
|
||||
|
||||
Reference in New Issue
Block a user