From 6ebdf91b327313a0d258ee473b5da5314377e348 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 7 Apr 2017 22:33:19 -0400 Subject: [PATCH] templates/repo: fix README.ipynb not rendered (#4367) --- gogs.go | 2 +- pkg/markup/markup.go | 5 ++++ routers/repo/view.go | 53 ++++++++++++++++++----------------- templates/.VERSION | 2 +- templates/base/head.tmpl | 2 +- templates/repo/view_file.tmpl | 37 ++++++++++++------------ 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/gogs.go b/gogs.go index 8654f7612..97dd03013 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/pkg/setting" ) -const APP_VER = "0.11.7.0407" +const APP_VER = "0.11.8.0407" func init() { setting.AppVer = APP_VER diff --git a/pkg/markup/markup.go b/pkg/markup/markup.go index 2484a7f9d..6173be391 100644 --- a/pkg/markup/markup.go +++ b/pkg/markup/markup.go @@ -23,6 +23,11 @@ func IsReadmeFile(name string) bool { return strings.HasPrefix(strings.ToLower(name), "readme") } +// IsIPythonNotebook reports whether name looks like a IPython notebook based on its extension. +func IsIPythonNotebook(name string) bool { + return strings.HasSuffix(name, ".ipynb") +} + const ( ISSUE_NAME_STYLE_NUMERIC = "numeric" ISSUE_NAME_STYLE_ALPHANUMERIC = "alphanumeric" diff --git a/routers/repo/view.go b/routers/repo/view.go index 0efa8ba20..f970bae76 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -33,23 +33,23 @@ const ( FORKS = "repo/forks" ) -func renderDirectory(ctx *context.Context, treeLink string) { - tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) +func renderDirectory(c *context.Context, treeLink string) { + tree, err := c.Repo.Commit.SubTree(c.Repo.TreePath) if err != nil { - ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) + c.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) return } entries, err := tree.ListEntries() if err != nil { - ctx.Handle(500, "ListEntries", err) + c.ServerError("ListEntries", err) return } entries.Sort() - ctx.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(ctx.Repo.Commit, ctx.Repo.TreePath, setting.Repository.CommitsFetchConcurrency) + c.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(c.Repo.Commit, c.Repo.TreePath, setting.Repository.CommitsFetchConcurrency) if err != nil { - ctx.Handle(500, "GetCommitsInfo", err) + c.ServerError("GetCommitsInfoWithCustomConcurrency", err) return } @@ -65,13 +65,13 @@ func renderDirectory(ctx *context.Context, treeLink string) { } if readmeFile != nil { - ctx.Data["RawFileLink"] = "" - ctx.Data["ReadmeInList"] = true - ctx.Data["ReadmeExist"] = true + c.Data["RawFileLink"] = "" + c.Data["ReadmeInList"] = true + c.Data["ReadmeExist"] = true dataRc, err := readmeFile.Data() if err != nil { - ctx.Handle(500, "Data", err) + c.ServerError("readmeFile.Data", err) return } @@ -80,38 +80,41 @@ func renderDirectory(ctx *context.Context, treeLink string) { buf = buf[:n] isTextFile := tool.IsTextFile(buf) - ctx.Data["IsTextFile"] = isTextFile - ctx.Data["FileName"] = readmeFile.Name() + c.Data["IsTextFile"] = isTextFile + c.Data["FileName"] = readmeFile.Name() if isTextFile { d, _ := ioutil.ReadAll(dataRc) buf = append(buf, d...) switch { case markup.IsMarkdownFile(readmeFile.Name()): - ctx.Data["IsMarkdown"] = true - buf = markup.Markdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas()) + c.Data["IsMarkdown"] = true + buf = markup.Markdown(buf, treeLink, c.Repo.Repository.ComposeMetas()) + case markup.IsIPythonNotebook(readmeFile.Name()): + c.Data["IsIPythonNotebook"] = true + c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name()) default: buf = bytes.Replace(buf, []byte("\n"), []byte(`
`), -1) } - ctx.Data["FileContent"] = string(buf) + c.Data["FileContent"] = string(buf) } } // Show latest commit info of repository in table header, // or of directory if not in root directory. - latestCommit := ctx.Repo.Commit - if len(ctx.Repo.TreePath) > 0 { - latestCommit, err = ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath) + latestCommit := c.Repo.Commit + if len(c.Repo.TreePath) > 0 { + latestCommit, err = c.Repo.Commit.GetCommitByPath(c.Repo.TreePath) if err != nil { - ctx.Handle(500, "GetCommitByPath", err) + c.ServerError("GetCommitByPath", err) return } } - ctx.Data["LatestCommit"] = latestCommit - ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) + c.Data["LatestCommit"] = latestCommit + c.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) - if ctx.Repo.CanEnableEditor() { - ctx.Data["CanAddFile"] = true - ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled + if c.Repo.CanEnableEditor() { + c.Data["CanAddFile"] = true + c.Data["CanUploadFile"] = setting.Repository.Upload.Enabled } } @@ -157,7 +160,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["IsMarkdown"] = isMarkdown ctx.Data["ReadmeExist"] = isMarkdown && markup.IsReadmeFile(blob.Name()) - ctx.Data["IsIPythonNotebook"] = strings.HasSuffix(blob.Name(), ".ipynb") + ctx.Data["IsIPythonNotebook"] = markup.IsIPythonNotebook(blob.Name()) if isMarkdown { ctx.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) diff --git a/templates/.VERSION b/templates/.VERSION index d00fae397..caa02f193 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.11.7.0407 \ No newline at end of file +0.11.8.0407 \ No newline at end of file diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 71551ac7a..1f967f0ed 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -47,7 +47,7 @@ - {{if .IsIPythonNotebook }} + {{if .IsIPythonNotebook}} {{end}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 92bd7139f..b8d6c1e48 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -36,28 +36,29 @@ {{end}}
-
- {{if or .IsMarkdown .ReadmeInList}} +
+ {{if .IsMarkdown}} {{if .FileContent}}{{.FileContent | Str2html}}{{end}} {{else if .IsIPythonNotebook}} - {{if .FileContent}} - - {{end}} + }); + + {{else if .ReadmeInList}} + {{if .FileContent}}{{.FileContent | Str2html}}{{end}} {{else if not .IsTextFile}}
{{if .IsImageFile}}