Skip to content

Commit 6ebdf91

Browse files
committed
templates/repo: fix README.ipynb not rendered (gogs#4367)
1 parent 91cd350 commit 6ebdf91

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

gogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/gogits/gogs/pkg/setting"
1717
)
1818

19-
const APP_VER = "0.11.7.0407"
19+
const APP_VER = "0.11.8.0407"
2020

2121
func init() {
2222
setting.AppVer = APP_VER

pkg/markup/markup.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func IsReadmeFile(name string) bool {
2323
return strings.HasPrefix(strings.ToLower(name), "readme")
2424
}
2525

26+
// IsIPythonNotebook reports whether name looks like a IPython notebook based on its extension.
27+
func IsIPythonNotebook(name string) bool {
28+
return strings.HasSuffix(name, ".ipynb")
29+
}
30+
2631
const (
2732
ISSUE_NAME_STYLE_NUMERIC = "numeric"
2833
ISSUE_NAME_STYLE_ALPHANUMERIC = "alphanumeric"

routers/repo/view.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ const (
3333
FORKS = "repo/forks"
3434
)
3535

36-
func renderDirectory(ctx *context.Context, treeLink string) {
37-
tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
36+
func renderDirectory(c *context.Context, treeLink string) {
37+
tree, err := c.Repo.Commit.SubTree(c.Repo.TreePath)
3838
if err != nil {
39-
ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
39+
c.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
4040
return
4141
}
4242

4343
entries, err := tree.ListEntries()
4444
if err != nil {
45-
ctx.Handle(500, "ListEntries", err)
45+
c.ServerError("ListEntries", err)
4646
return
4747
}
4848
entries.Sort()
4949

50-
ctx.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(ctx.Repo.Commit, ctx.Repo.TreePath, setting.Repository.CommitsFetchConcurrency)
50+
c.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(c.Repo.Commit, c.Repo.TreePath, setting.Repository.CommitsFetchConcurrency)
5151
if err != nil {
52-
ctx.Handle(500, "GetCommitsInfo", err)
52+
c.ServerError("GetCommitsInfoWithCustomConcurrency", err)
5353
return
5454
}
5555

@@ -65,13 +65,13 @@ func renderDirectory(ctx *context.Context, treeLink string) {
6565
}
6666

6767
if readmeFile != nil {
68-
ctx.Data["RawFileLink"] = ""
69-
ctx.Data["ReadmeInList"] = true
70-
ctx.Data["ReadmeExist"] = true
68+
c.Data["RawFileLink"] = ""
69+
c.Data["ReadmeInList"] = true
70+
c.Data["ReadmeExist"] = true
7171

7272
dataRc, err := readmeFile.Data()
7373
if err != nil {
74-
ctx.Handle(500, "Data", err)
74+
c.ServerError("readmeFile.Data", err)
7575
return
7676
}
7777

@@ -80,38 +80,41 @@ func renderDirectory(ctx *context.Context, treeLink string) {
8080
buf = buf[:n]
8181

8282
isTextFile := tool.IsTextFile(buf)
83-
ctx.Data["IsTextFile"] = isTextFile
84-
ctx.Data["FileName"] = readmeFile.Name()
83+
c.Data["IsTextFile"] = isTextFile
84+
c.Data["FileName"] = readmeFile.Name()
8585
if isTextFile {
8686
d, _ := ioutil.ReadAll(dataRc)
8787
buf = append(buf, d...)
8888
switch {
8989
case markup.IsMarkdownFile(readmeFile.Name()):
90-
ctx.Data["IsMarkdown"] = true
91-
buf = markup.Markdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
90+
c.Data["IsMarkdown"] = true
91+
buf = markup.Markdown(buf, treeLink, c.Repo.Repository.ComposeMetas())
92+
case markup.IsIPythonNotebook(readmeFile.Name()):
93+
c.Data["IsIPythonNotebook"] = true
94+
c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name())
9295
default:
9396
buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
9497
}
95-
ctx.Data["FileContent"] = string(buf)
98+
c.Data["FileContent"] = string(buf)
9699
}
97100
}
98101

99102
// Show latest commit info of repository in table header,
100103
// or of directory if not in root directory.
101-
latestCommit := ctx.Repo.Commit
102-
if len(ctx.Repo.TreePath) > 0 {
103-
latestCommit, err = ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
104+
latestCommit := c.Repo.Commit
105+
if len(c.Repo.TreePath) > 0 {
106+
latestCommit, err = c.Repo.Commit.GetCommitByPath(c.Repo.TreePath)
104107
if err != nil {
105-
ctx.Handle(500, "GetCommitByPath", err)
108+
c.ServerError("GetCommitByPath", err)
106109
return
107110
}
108111
}
109-
ctx.Data["LatestCommit"] = latestCommit
110-
ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
112+
c.Data["LatestCommit"] = latestCommit
113+
c.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
111114

112-
if ctx.Repo.CanEnableEditor() {
113-
ctx.Data["CanAddFile"] = true
114-
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
115+
if c.Repo.CanEnableEditor() {
116+
c.Data["CanAddFile"] = true
117+
c.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
115118
}
116119
}
117120

@@ -157,7 +160,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
157160
ctx.Data["IsMarkdown"] = isMarkdown
158161
ctx.Data["ReadmeExist"] = isMarkdown && markup.IsReadmeFile(blob.Name())
159162

160-
ctx.Data["IsIPythonNotebook"] = strings.HasSuffix(blob.Name(), ".ipynb")
163+
ctx.Data["IsIPythonNotebook"] = markup.IsIPythonNotebook(blob.Name())
161164

162165
if isMarkdown {
163166
ctx.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))

templates/.VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.11.7.0407
1+
0.11.8.0407

templates/base/head.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<link rel="stylesheet" href="{{AppSubURL}}/assets/octicons-4.3.0/octicons.min.css">
4848

4949
<!-- notebook.js for rendering ipython notebooks and marked.js for rendering markdown in notebooks -->
50-
{{if .IsIPythonNotebook }}
50+
{{if .IsIPythonNotebook}}
5151
<script src="{{AppSubURL}}/plugins/notebookjs-0.2.6/notebook.min.js"></script>
5252
<script src="{{AppSubURL}}/plugins/marked-0.3.6/marked.min.js"></script>
5353
{{end}}

templates/repo/view_file.tmpl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,29 @@
3636
{{end}}
3737
</h4>
3838
<div class="ui attached table segment">
39-
<div id="{{if .IsIPythonNotebook}}ipython-notebook{{end}}" class="file-view {{if .IsMarkdown}}markdown{{else if .ReadmeInList}}plain-text{{else if .IsIPythonNotebook}}ipython-notebook1{{else if and .IsTextFile}}code-view{{end}} has-emoji">
40-
{{if or .IsMarkdown .ReadmeInList}}
39+
<div id="{{if .IsIPythonNotebook}}ipython-notebook{{end}}" class="file-view {{if .IsMarkdown}}markdown{{else if .IsIPythonNotebook}}ipython-notebook{{else if .ReadmeInList}}plain-text{{else if and .IsTextFile}}code-view{{end}} has-emoji">
40+
{{if .IsMarkdown}}
4141
{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
4242
{{else if .IsIPythonNotebook}}
43-
{{if .FileContent}}
44-
<script>
45-
var rendered = null;
46-
$.getJSON("{{.RawFileLink}}", null, function(notebook_json) {
47-
var notebook = nb.parse(notebook_json);
48-
rendered = notebook.render();
49-
$("#ipython-notebook").append(rendered);
50-
$("#ipython-notebook code").each(function(i, block) {
51-
$(block).addClass("py").addClass("python");
52-
hljs.highlightBlock(block);
53-
});
43+
<script>
44+
var rendered = null;
45+
console.log("fuck")
46+
$.getJSON("{{.RawFileLink}}", null, function(notebook_json) {
47+
var notebook = nb.parse(notebook_json);
48+
rendered = notebook.render();
49+
$("#ipython-notebook").append(rendered);
50+
$("#ipython-notebook code").each(function(i, block) {
51+
$(block).addClass("py").addClass("python");
52+
hljs.highlightBlock(block);
53+
});
5454

55-
$("#ipython-notebook .nb-markdown-cell").each(function(i, markdown) {
56-
$(markdown).html(marked($(markdown).html()));
57-
});
55+
$("#ipython-notebook .nb-markdown-cell").each(function(i, markdown) {
56+
$(markdown).html(marked($(markdown).html()));
5857
});
59-
</script>
60-
{{end}}
58+
});
59+
</script>
60+
{{else if .ReadmeInList}}
61+
{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
6162
{{else if not .IsTextFile}}
6263
<div class="view-raw ui center">
6364
{{if .IsImageFile}}

0 commit comments

Comments
 (0)