Skip to content

Commit d80ba20

Browse files
committed
gist website embed link
You can paste GitBucket Gist Code in your website.
1 parent 4782746 commit d80ba20

File tree

4 files changed

+126
-15
lines changed

4 files changed

+126
-15
lines changed

src/main/scala/gitbucket/gist/controller/GistController.scala

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import gitbucket.gist.util._
1919
import gitbucket.gist.util.GistUtils._
2020
import gitbucket.gist.util.Configurations._
2121
import gitbucket.gist.html
22+
import gitbucket.gist.js
2223

2324
import org.apache.commons.io.FileUtils
2425
import org.eclipse.jgit.api.Git
2526
import org.eclipse.jgit.lib._
2627
import org.scalatra.Ok
2728
import play.twirl.api.Html
29+
import play.twirl.api.JavaScript
2830

2931
class GistController extends GistControllerBase with GistService with GistCommentService with AccountService
3032
with GistEditorAuthenticator with UsersAuthenticator
@@ -415,7 +417,12 @@ trait GistControllerBase extends ControllerBase {
415417
////////////////////////////////////////////////////////////////////////////////
416418

417419

418-
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master"): Any = {
420+
private def _gist(userName: String, repoName: Option[String] = None, revision: String = "master", isEmbed: Boolean = false): Any = {
421+
422+
if( repoName.isDefined && repoName.get.endsWith(".js")) {
423+
return _gist(userName, Some(repoName.get.substring(0,repoName.get.length()-3)), revision, true)
424+
}
425+
419426
repoName match {
420427
case None => {
421428
val page = params.get("page") match {
@@ -443,11 +450,14 @@ trait GistControllerBase extends ControllerBase {
443450
case Some(gist) =>
444451
if(gist.mode == "PRIVATE"){
445452
context.loginAccount match {
446-
case Some(x) if(x.userName == userName) => _gistDetail(gist, userName, repoName, revision)
453+
case Some(x) if(x.userName == userName) =>
454+
if(isEmbed) _embedJs(gist, userName, repoName, revision)
455+
else _gistDetail(gist, userName, repoName, revision)
447456
case _ => Unauthorized()
448457
}
449458
} else {
450-
_gistDetail(gist, userName, repoName, revision)
459+
if(isEmbed) _embedJs(gist, userName, repoName, revision)
460+
else _gistDetail(gist, userName, repoName, revision)
451461
}
452462
case None =>
453463
NotFound()
@@ -456,6 +466,18 @@ trait GistControllerBase extends ControllerBase {
456466
}
457467
}
458468

469+
private def _embedJs(gist: Gist, userName: String, repoName: String, revision: String): JavaScript = {
470+
val originUserName = gist.originUserName.getOrElse(userName)
471+
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
472+
473+
js.detail(
474+
gist,
475+
GistRepositoryURL(gist, baseUrl, context.settings),
476+
revision,
477+
getGistFiles(userName, repoName, revision)
478+
)
479+
}
480+
459481
private def _gistDetail(gist: Gist, userName: String, repoName: String, revision: String): Html = {
460482
val originUserName = gist.originUserName.getOrElse(userName)
461483
val originRepoName = gist.originRepositoryName.getOrElse(repoName)

src/main/scala/gitbucket/gist/util/GistUtils.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ object GistUtils {
4747
case class GistRepositoryURL(gist: Gist, baseUrl: String, settings: SystemSettings){
4848

4949
def httpUrl: String = s"${baseUrl}/git/gist/${gist.userName}/${gist.repositoryName}.git"
50+
51+
def embedUrl: String = s"${baseUrl}/gist/${gist.userName}/${gist.repositoryName}.js"
5052

5153
def sshUrl(loginUser: String): String = {
5254
val host = """^https?://(.+?)(:\d+)?/""".r.findFirstMatchIn(httpUrl).get.group(1)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@(gist: gitbucket.gist.model.Gist,
2+
repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL,
3+
revision: String,
4+
files: Seq[(String, String)]
5+
)(implicit context: gitbucket.core.controller.Context)
6+
@import gitbucket.core.view.helpers
7+
@import play.twirl.api.Html
8+
@bootstrapCSS={<link href="@helpers.assets("/vendors/bootstrap-3.3.6/css/bootstrap.css")" rel="stylesheet">}
9+
@prettifyCSS={<link href="@helpers.assets("/vendors/google-code-prettify/prettify.css")" rel="stylesheet">}
10+
@gistCSS={<link href="@context.path/plugin-assets/gist/style.css" rel="stylesheet">}
11+
@gitbucketCSS={<link href="@helpers.assets("/common/css/gitbucket.css")" rel="stylesheet">}
12+
@adminLTECSS={<link href="@helpers.assets("/vendors/AdminLTE-2.3.8/css/AdminLTE.min.css")" rel="stylesheet">}
13+
jqueryScript = document.createElement('script');
14+
jqueryScript.src = '@helpers.assets("/vendors/jquery/jquery-1.12.2.min.js")';
15+
document.head.appendChild(jqueryScript);
16+
prettifyScript = document.createElement('script');
17+
prettifyScript.src = '@helpers.assets("/vendors/google-code-prettify/prettify.js")';
18+
prettifyScript.onload = function() { prettyPrint(); $('pre:has(> span.pln)').hide(); };
19+
document.head.appendChild(prettifyScript);
20+
fireScript = document.createElement('script');
21+
fireScript.setAttribute('type','text/javascript');
22+
fireScript.text = '$(document).load(function() { prettyPrint(); });'
23+
24+
var _html = (function () {/*4f85e035-2513-453b-b435-33f0a12b2339
25+
<div class="content-wrapper main-center">
26+
<div class="content body">
27+
<div style="overflow: hidden;">
28+
<div style="margin-bottom: 10px;">
29+
@gist.description
30+
</div>
31+
@files.map { case (fileName, content) =>
32+
<div class="panel panel-default">
33+
<div class="panel-heading strong" style="padding: 6px; line-height: 30px;">
34+
@fileName
35+
<div class="pull-right">
36+
<a href="@context.path/gist/@gist.userName/@gist.repositoryName/raw/@revision/@fileName" class="btn btn-sm btn-default">Raw</a>
37+
</div>
38+
</div>
39+
@if(helpers.isRenderable(fileName)){
40+
<div class="panel-body markdown-body" style="padding-left: 16px; padding-right: 16px;">
41+
@helpers.renderMarkup(List(fileName), content, "master", gist.toRepositoryInfo, false, false, true)
42+
</div>
43+
} else {
44+
<div class="panel-body">
45+
<pre class="prettyprint linenums blob">@content.toString.replaceAll("<","&lt;").replaceAll(">","&gt;")<pre>
46+
</div>
47+
}
48+
</div>
49+
}
50+
</div>
51+
</div>
52+
</div>
53+
4f85e035-2513-453b-b435-33f0a12b2339*/}).toString().replace(/(\n)/g, '').split('4f85e035-2513-453b-b435-33f0a12b2339')[1];
54+
55+
document.write('@bootstrapCSS');
56+
document.write('@prettifyCSS');
57+
document.write('@gistCSS');
58+
document.write('@gitbucketCSS');
59+
document.write('@adminLTECSS');
60+
document.write(_html.replace(/\\r\\n/g,"\n").replace(/\\/g,""));

src/main/twirl/gitbucket/gist/menu.scala.html

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
gist: gitbucket.gist.model.Gist,
33
repositoryUrl: gitbucket.gist.util.GistUtils.GistRepositoryURL)(implicit context: gitbucket.core.controller.Context)
44
@import gitbucket.core.view.helpers
5-
5+
@embedScript=@{"<script>"+repositoryUrl.embedUrl+"</script>"}
66
@menuitem(name: String, path: String, label: String, count: Int = 0) = {
77
<li @if(active == name){class="active"}>
88
<a href="@context.path/gist/@path">
@@ -32,33 +32,60 @@
3232
"Forks")
3333
}
3434
</ul>
35-
<div class="small">
36-
<strong id="repository-url-proto">HTTP</strong> <span class="mute">clone URL</span>
37-
</div>
35+
<div class="pull-right pc">
36+
<div class="input-group" style="margin-bottom: 0px;">
37+
<div class="btn-group input-group-btn">
38+
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
39+
<span id="repository-url-proto">HTTP</span> <span class="caret"></span>
40+
</button>
41+
<ul class="dropdown-menu">
42+
<li>
43+
<a href="javascript:void(0);" id="repository-url-embed">
44+
<strong>Embed</strong><br>
45+
Embed this gist in your website.
46+
</a>
47+
</li>
48+
<li>
49+
<a href="javascript:void(0);" id="repository-url-http">
50+
<strong>HTTP</strong><br>
51+
Clone with Git using the repository's web address.
52+
</a>
53+
</li>
54+
@if(context.settings.ssh && context.loginAccount.isDefined){
55+
<li>
56+
<a href="javascript:void(0);" id="repository-url-ssh">
57+
<strong>SSH</strong><br>
58+
Clone with an SSH key and passphrase from your GitBucket Settings.
59+
</a>
60+
</li>
61+
}
62+
</ul>
63+
</div>
3864
@gitbucket.core.helper.html.copy("repository-url", "repository-url-copy", repositoryUrl.httpUrl){
3965
<input type="text" value="@repositoryUrl.httpUrl" id="repository-url" class="form-control input-sm" readonly>
4066
}
41-
@if(context.settings.ssh && context.loginAccount.isDefined){
42-
<div class="small">
43-
<span class="mute">You can clone <a href="javascript:void(0);" id="repository-url-http">HTTP</a> or <a href="javascript:void(0);" id="repository-url-ssh">SSH</a>.</span>
44-
</div>
45-
}
67+
</div>
4668
<div style="margin-top: 10px;">
4769
<a href="@context.path/gist/@{gist.userName}/@{gist.repositoryName}/download/@{gist.repositoryName}.zip"
48-
class="btn btn-default btn-block"><i class="icon-download-alt"></i>Download ZIP</a>
70+
class="btn btn-default btn-block"><i class="octicon octicon-cloud-download"></i>Download ZIP</a>
4971
</div>
5072
</div>
51-
@if(context.settings.ssh && context.loginAccount.isDefined){
73+
</div>
5274
<script>
5375
$(function(){
76+
$('#repository-url-embed').click(function(){
77+
$('#repository-url').val('@embedScript'.replace(/&lt;/g,"<").replace(/&gt;/g,">"));
78+
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
79+
});
5480
$('#repository-url-http').click(function(){
5581
$('#repository-url').val('@repositoryUrl.httpUrl');
5682
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
5783
});
84+
@if(context.settings.ssh && context.loginAccount.isDefined){
5885
$('#repository-url-ssh').click(function(){
5986
$('#repository-url').val('@repositoryUrl.sshUrl(context.loginAccount.get.userName)');
6087
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
6188
});
89+
}
6290
});
6391
</script>
64-
}

0 commit comments

Comments
 (0)