diff options
author | Unknwon <u@gogs.io> | 2015-11-27 01:50:38 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-11-27 01:50:38 -0500 |
commit | e42fcb033d4a7ee44fe397be3e933d7031b7d8f7 (patch) | |
tree | a203fff39c92f947c4585a746e2345b3ebc67b3c /routers/repo/wiki.go | |
parent | 392f3ee21016476fb9794f78882d6c447acb8449 (diff) | |
download | gitea-e42fcb033d4a7ee44fe397be3e933d7031b7d8f7.tar.gz gitea-e42fcb033d4a7ee44fe397be3e933d7031b7d8f7.zip |
wiki: finish edit
Diffstat (limited to 'routers/repo/wiki.go')
-rw-r--r-- | routers/repo/wiki.go | 125 |
1 files changed, 102 insertions, 23 deletions
diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index 4ea41b347e..146254043d 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -6,6 +6,7 @@ package repo import ( "io/ioutil" + "strings" "github.com/gogits/git-shell" @@ -21,56 +22,97 @@ const ( 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()) if err != nil { ctx.Handle(500, "OpenRepository", err) - return + return nil, "" } commit, err := wikiRepo.GetCommitOfBranch("master") if err != nil { 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 - blob, err := commit.GetBlobByPath(page + ".md") + blob, err := commit.GetBlobByPath(pageName + ".md") if err != nil { if git.IsErrNotExist(err) { - ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_list") + ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages") } else { ctx.Handle(500, "GetBlobByPath", err) } - return + return nil, "" } r, err := blob.Data() if err != nil { ctx.Handle(500, "Data", err) - return + return nil, "" } data, err := ioutil.ReadAll(r) if err != nil { 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 } - ctx.Data["Content"] = string(base.RenderMarkdown(data, ctx.Repo.RepoLink)) // Get last change information. - lastCommit, err := wikiRepo.GetCommitByPath(page + ".md") + lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md") if err != nil { ctx.Handle(500, "GetCommitByPath", err) return @@ -80,7 +122,7 @@ func Wiki(ctx *middleware.Context) { 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 { - 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 } @@ -115,5 +162,37 @@ func NewWikiPost(ctx *middleware.Context, form auth.NewWikiForm) { } 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)) } |