|
@@ -6,6 +6,7 @@ package repo
|
|
|
|
|
|
import (
|
|
import (
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
|
|
+ "strings"
|
|
|
|
|
|
"github.com/gogits/git-shell"
|
|
"github.com/gogits/git-shell"
|
|
|
|
|
|
@@ -21,56 +22,97 @@ const (
|
|
WIKI_NEW base.TplName = "repo/wiki/new"
|
|
WIKI_NEW base.TplName = "repo/wiki/new"
|
|
)
|
|
)
|
|
|
|
|
|
-func Wiki(ctx *middleware.Context) {
|
|
|
|
- ctx.Data["PageIsWiki"] = true
|
|
|
|
-
|
|
|
|
- if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
- ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
|
|
|
- ctx.HTML(200, WIKI_START)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+type PageMeta struct {
|
|
|
|
+ Name string
|
|
|
|
+ URL string
|
|
|
|
+}
|
|
|
|
|
|
|
|
+func renderWikiPage(ctx *middleware.Context, isViewPage bool) (*git.Repository, string) {
|
|
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
|
|
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.Handle(500, "OpenRepository", err)
|
|
ctx.Handle(500, "OpenRepository", err)
|
|
- return
|
|
|
|
|
|
+ return nil, ""
|
|
}
|
|
}
|
|
commit, err := wikiRepo.GetCommitOfBranch("master")
|
|
commit, err := wikiRepo.GetCommitOfBranch("master")
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.Handle(500, "GetCommitOfBranch", err)
|
|
ctx.Handle(500, "GetCommitOfBranch", err)
|
|
- return
|
|
|
|
|
|
+ return nil, ""
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Get page list.
|
|
|
|
+ if isViewPage {
|
|
|
|
+ entries, err := commit.ListEntries()
|
|
|
|
+ if err != nil {
|
|
|
|
+ ctx.Handle(500, "ListEntries", err)
|
|
|
|
+ return nil, ""
|
|
|
|
+ }
|
|
|
|
+ pages := make([]PageMeta, len(entries))
|
|
|
|
+ for i := range entries {
|
|
|
|
+ name := strings.TrimSuffix(entries[i].Name(), ".md")
|
|
|
|
+ pages[i] = PageMeta{
|
|
|
|
+ Name: name,
|
|
|
|
+ URL: models.ToWikiPageURL(name),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ctx.Data["Pages"] = pages
|
|
}
|
|
}
|
|
|
|
|
|
- page := models.ToWikiPageName(ctx.Params(":page"))
|
|
|
|
- if len(page) == 0 {
|
|
|
|
- page = "Home"
|
|
|
|
|
|
+ pageURL := ctx.Params(":page")
|
|
|
|
+ if len(pageURL) == 0 {
|
|
|
|
+ pageURL = "Home"
|
|
}
|
|
}
|
|
- ctx.Data["Title"] = page
|
|
|
|
|
|
+ ctx.Data["PageURL"] = pageURL
|
|
|
|
+
|
|
|
|
+ pageName := models.ToWikiPageName(pageURL)
|
|
|
|
+ ctx.Data["old_title"] = pageName
|
|
|
|
+ ctx.Data["Title"] = pageName
|
|
|
|
+ ctx.Data["title"] = pageName
|
|
ctx.Data["RequireHighlightJS"] = true
|
|
ctx.Data["RequireHighlightJS"] = true
|
|
|
|
|
|
- blob, err := commit.GetBlobByPath(page + ".md")
|
|
|
|
|
|
+ blob, err := commit.GetBlobByPath(pageName + ".md")
|
|
if err != nil {
|
|
if err != nil {
|
|
if git.IsErrNotExist(err) {
|
|
if git.IsErrNotExist(err) {
|
|
- ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_list")
|
|
|
|
|
|
+ ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
|
|
} else {
|
|
} else {
|
|
ctx.Handle(500, "GetBlobByPath", err)
|
|
ctx.Handle(500, "GetBlobByPath", err)
|
|
}
|
|
}
|
|
- return
|
|
|
|
|
|
+ return nil, ""
|
|
}
|
|
}
|
|
r, err := blob.Data()
|
|
r, err := blob.Data()
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.Handle(500, "Data", err)
|
|
ctx.Handle(500, "Data", err)
|
|
- return
|
|
|
|
|
|
+ return nil, ""
|
|
}
|
|
}
|
|
data, err := ioutil.ReadAll(r)
|
|
data, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.Handle(500, "ReadAll", err)
|
|
ctx.Handle(500, "ReadAll", err)
|
|
|
|
+ return nil, ""
|
|
|
|
+ }
|
|
|
|
+ if isViewPage {
|
|
|
|
+ ctx.Data["content"] = string(base.RenderMarkdown(data, ctx.Repo.RepoLink))
|
|
|
|
+ } else {
|
|
|
|
+ ctx.Data["content"] = string(data)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return wikiRepo, pageName
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func Wiki(ctx *middleware.Context) {
|
|
|
|
+ ctx.Data["PageIsWiki"] = true
|
|
|
|
+
|
|
|
|
+ if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
+ ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
|
|
|
+ ctx.HTML(200, WIKI_START)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ wikiRepo, pageName := renderWikiPage(ctx, true)
|
|
|
|
+ if ctx.Written() {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- ctx.Data["Content"] = string(base.RenderMarkdown(data, ctx.Repo.RepoLink))
|
|
|
|
|
|
|
|
// Get last change information.
|
|
// Get last change information.
|
|
- lastCommit, err := wikiRepo.GetCommitByPath(page + ".md")
|
|
|
|
|
|
+ lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
|
|
if err != nil {
|
|
if err != nil {
|
|
ctx.Handle(500, "GetCommitByPath", err)
|
|
ctx.Handle(500, "GetCommitByPath", err)
|
|
return
|
|
return
|
|
@@ -80,7 +122,7 @@ func Wiki(ctx *middleware.Context) {
|
|
ctx.HTML(200, WIKI_VIEW)
|
|
ctx.HTML(200, WIKI_VIEW)
|
|
}
|
|
}
|
|
|
|
|
|
-func WikiList(ctx *middleware.Context) {
|
|
|
|
|
|
+func WikiPages(ctx *middleware.Context) {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -107,7 +149,12 @@ func NewWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
|
|
}
|
|
}
|
|
|
|
|
|
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
|
|
if err := ctx.Repo.Repository.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
|
|
- ctx.Handle(500, "AddWikiPage", err)
|
|
|
|
|
|
+ if models.IsErrWikiAlreadyExist(err) {
|
|
|
|
+ ctx.Data["Err_Title"] = true
|
|
|
|
+ ctx.RenderWithErr(ctx.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &form)
|
|
|
|
+ } else {
|
|
|
|
+ ctx.Handle(500, "AddWikiPage", err)
|
|
|
|
+ }
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
@@ -115,5 +162,37 @@ func NewWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
|
|
}
|
|
}
|
|
|
|
|
|
func EditWiki(ctx *middleware.Context) {
|
|
func EditWiki(ctx *middleware.Context) {
|
|
- ctx.PlainText(200, []byte(ctx.Params(":page")))
|
|
|
|
|
|
+ ctx.Data["PageIsWiki"] = true
|
|
|
|
+ ctx.Data["PageIsWikiEdit"] = true
|
|
|
|
+ ctx.Data["RequireSimpleMDE"] = true
|
|
|
|
+
|
|
|
|
+ if !ctx.Repo.Repository.HasWiki() {
|
|
|
|
+ ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ renderWikiPage(ctx, false)
|
|
|
|
+ if ctx.Written() {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ctx.HTML(200, WIKI_NEW)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func EditWikiPost(ctx *middleware.Context, form auth.NewWikiForm) {
|
|
|
|
+ ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
|
|
|
|
+ ctx.Data["PageIsWiki"] = true
|
|
|
|
+ ctx.Data["RequireSimpleMDE"] = true
|
|
|
|
+
|
|
|
|
+ if ctx.HasError() {
|
|
|
|
+ ctx.HTML(200, WIKI_NEW)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := ctx.Repo.Repository.EditWikiPage(ctx.User, form.OldTitle, form.Title, form.Content, form.Message); err != nil {
|
|
|
|
+ ctx.Handle(500, "EditWikiPage", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
|
|
}
|
|
}
|