Skip to content

Commit 2c51474

Browse files
authored
Merge pull request #42 from tkgdsg/publish_code
gist website embed link
2 parents 4782746 + 5109f96 commit 2c51474

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

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

Lines changed: 25 additions & 0 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
@@ -68,6 +70,17 @@ trait GistControllerBase extends ControllerBase {
6870
_gist(params("userName"), Some(params("repoName")))
6971
}
7072

73+
get("/gist/:userName/:repoName.js"){
74+
val userName = params("userName")
75+
val repoName = params("repoName")
76+
getGist(userName, repoName) match {
77+
case Some(gist) =>
78+
_embedJs(gist, userName, repoName, "master")
79+
case None =>
80+
NotFound()
81+
}
82+
}
83+
7184
get("/gist/:userName/:repoName/:revision"){
7285
_gist(params("userName"), Some(params("repoName")), params("revision"))
7386
}
@@ -456,6 +469,18 @@ trait GistControllerBase extends ControllerBase {
456469
}
457470
}
458471

472+
private def _embedJs(gist: Gist, userName: String, repoName: String, revision: String): JavaScript = {
473+
val originUserName = gist.originUserName.getOrElse(userName)
474+
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
475+
476+
js.detail(
477+
gist,
478+
GistRepositoryURL(gist, baseUrl, context.settings),
479+
revision,
480+
getGistFiles(userName, repoName, revision)
481+
)
482+
}
483+
459484
private def _gistDetail(gist: Gist, userName: String, repoName: String, revision: String): Html = {
460485
val originUserName = gist.originUserName.getOrElse(userName)
461486
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
jqueryScript = document.createElement('script');
8+
jqueryScript.src = '@helpers.assets("/vendors/jquery/jquery-1.12.2.min.js")';
9+
document.head.appendChild(jqueryScript);
10+
prettifyScript = document.createElement('script');
11+
prettifyScript.src = '@helpers.assets("/vendors/google-code-prettify/prettify.js")';
12+
prettifyScript.onload = function() { prettyPrint(); $('pre:has(> span.pln)').hide(); };
13+
document.head.appendChild(prettifyScript);
14+
fireScript = document.createElement('script');
15+
fireScript.setAttribute('type','text/javascript');
16+
fireScript.text = '$(document).load(function() { prettyPrint(); });'
17+
18+
var _html = (function () {/*4f85e035-2513-453b-b435-33f0a12b2339
19+
<div class="content-wrapper main-center">
20+
<div class="content body">
21+
<div style="overflow: hidden;">
22+
<div style="margin-bottom: 10px;">
23+
@gist.description
24+
</div>
25+
@files.map { case (fileName, content) =>
26+
<div class="panel panel-default">
27+
<div class="panel-heading strong" style="padding: 6px; line-height: 30px;">
28+
@fileName
29+
<div class="pull-right">
30+
<a href="@context.path/gist/@gist.userName/@gist.repositoryName/raw/@revision/@fileName" class="btn btn-sm btn-default">Raw</a>
31+
</div>
32+
</div>
33+
@if(helpers.isRenderable(fileName)){
34+
<div class="panel-body markdown-body" style="padding-left: 16px; padding-right: 16px;">
35+
@helpers.renderMarkup(List(fileName), content, "master", gist.toRepositoryInfo, false, false, true)
36+
</div>
37+
} else {
38+
<div class="panel-body">
39+
<pre class="prettyprint linenums blob">@content.toString.replaceAll("<","&lt;").replaceAll(">","&gt;")<pre>
40+
</div>
41+
}
42+
</div>
43+
}
44+
</div>
45+
</div>
46+
</div>
47+
4f85e035-2513-453b-b435-33f0a12b2339*/}).toString().replace(/(\n)/g, '').split('4f85e035-2513-453b-b435-33f0a12b2339')[1];
48+
49+
document.write('<link href="@helpers.assets("/vendors/bootstrap-3.3.6/css/bootstrap.css")" rel="stylesheet">');
50+
document.write('<link href="@helpers.assets("/vendors/google-code-prettify/prettify.css")" rel="stylesheet">');
51+
document.write('<link href="@context.path/plugin-assets/gist/style.css" rel="stylesheet">');
52+
document.write('<link href="@helpers.assets("/common/css/gitbucket.css")" rel="stylesheet">');
53+
document.write('<link href="@helpers.assets("/vendors/AdminLTE-2.3.8/css/AdminLTE.min.css")" rel="stylesheet">');
54+
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 snippet 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)