123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- // Copyright 2015 The Gogs Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
- package repo
- import (
- "io/ioutil"
- "strings"
- "time"
- "github.com/gogits/git-module"
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/pkg/form"
- "github.com/gogits/gogs/pkg/markup"
- )
- const (
- WIKI_START = "repo/wiki/start"
- WIKI_VIEW = "repo/wiki/view"
- WIKI_NEW = "repo/wiki/new"
- WIKI_PAGES = "repo/wiki/pages"
- )
- func MustEnableWiki(c *context.Context) {
- if !c.Repo.Repository.EnableWiki {
- c.Handle(404, "MustEnableWiki", nil)
- return
- }
- if c.Repo.Repository.EnableExternalWiki {
- c.Redirect(c.Repo.Repository.ExternalWikiURL)
- return
- }
- }
- type PageMeta struct {
- Name string
- URL string
- Updated time.Time
- }
- func renderWikiPage(c *context.Context, isViewPage bool) (*git.Repository, string) {
- wikiRepo, err := git.OpenRepository(c.Repo.Repository.WikiPath())
- if err != nil {
- c.Handle(500, "OpenRepository", err)
- return nil, ""
- }
- commit, err := wikiRepo.GetBranchCommit("master")
- if err != nil {
- c.Handle(500, "GetBranchCommit", err)
- return nil, ""
- }
- // Get page list.
- if isViewPage {
- entries, err := commit.ListEntries()
- if err != nil {
- c.Handle(500, "ListEntries", err)
- return nil, ""
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: models.ToWikiPageURL(name),
- })
- }
- }
- c.Data["Pages"] = pages
- }
- pageURL := c.Params(":page")
- if len(pageURL) == 0 {
- pageURL = "Home"
- }
- c.Data["PageURL"] = pageURL
- pageName := models.ToWikiPageName(pageURL)
- c.Data["old_title"] = pageName
- c.Data["Title"] = pageName
- c.Data["title"] = pageName
- c.Data["RequireHighlightJS"] = true
- blob, err := commit.GetBlobByPath(pageName + ".md")
- if err != nil {
- if git.IsErrNotExist(err) {
- c.Redirect(c.Repo.RepoLink + "/wiki/_pages")
- } else {
- c.Handle(500, "GetBlobByPath", err)
- }
- return nil, ""
- }
- r, err := blob.Data()
- if err != nil {
- c.Handle(500, "Data", err)
- return nil, ""
- }
- data, err := ioutil.ReadAll(r)
- if err != nil {
- c.Handle(500, "ReadAll", err)
- return nil, ""
- }
- if isViewPage {
- c.Data["content"] = string(markup.Markdown(data, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
- } else {
- c.Data["content"] = string(data)
- }
- return wikiRepo, pageName
- }
- func Wiki(c *context.Context) {
- c.Data["PageIsWiki"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Data["Title"] = c.Tr("repo.wiki")
- c.HTML(200, WIKI_START)
- return
- }
- wikiRepo, pageName := renderWikiPage(c, true)
- if c.Written() {
- return
- }
- // Get last change information.
- lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
- if err != nil {
- c.Handle(500, "GetCommitByPath", err)
- return
- }
- c.Data["Author"] = lastCommit.Author
- c.HTML(200, WIKI_VIEW)
- }
- func WikiPages(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.wiki.pages")
- c.Data["PageIsWiki"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Redirect(c.Repo.RepoLink + "/wiki")
- return
- }
- wikiRepo, err := git.OpenRepository(c.Repo.Repository.WikiPath())
- if err != nil {
- c.Handle(500, "OpenRepository", err)
- return
- }
- commit, err := wikiRepo.GetBranchCommit("master")
- if err != nil {
- c.Handle(500, "GetBranchCommit", err)
- return
- }
- entries, err := commit.ListEntries()
- if err != nil {
- c.Handle(500, "ListEntries", err)
- return
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type == git.OBJECT_BLOB && strings.HasSuffix(entries[i].Name(), ".md") {
- commit, err := wikiRepo.GetCommitByPath(entries[i].Name())
- if err != nil {
- c.ServerError("GetCommitByPath", err)
- return
- }
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: models.ToWikiPageURL(name),
- Updated: commit.Author.When,
- })
- }
- }
- c.Data["Pages"] = pages
- c.HTML(200, WIKI_PAGES)
- }
- func NewWiki(c *context.Context) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Data["title"] = "Home"
- }
- c.HTML(200, WIKI_NEW)
- }
- func NewWikiPost(c *context.Context, f form.NewWiki) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if c.HasError() {
- c.HTML(200, WIKI_NEW)
- return
- }
- if err := c.Repo.Repository.AddWikiPage(c.User, f.Title, f.Content, f.Message); err != nil {
- if models.IsErrWikiAlreadyExist(err) {
- c.Data["Err_Title"] = true
- c.RenderWithErr(c.Tr("repo.wiki.page_already_exists"), WIKI_NEW, &f)
- } else {
- c.Handle(500, "AddWikiPage", err)
- }
- return
- }
- c.Redirect(c.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(models.ToWikiPageName(f.Title)))
- }
- func EditWiki(c *context.Context) {
- c.Data["PageIsWiki"] = true
- c.Data["PageIsWikiEdit"] = true
- c.Data["RequireSimpleMDE"] = true
- if !c.Repo.Repository.HasWiki() {
- c.Redirect(c.Repo.RepoLink + "/wiki")
- return
- }
- renderWikiPage(c, false)
- if c.Written() {
- return
- }
- c.HTML(200, WIKI_NEW)
- }
- func EditWikiPost(c *context.Context, f form.NewWiki) {
- c.Data["Title"] = c.Tr("repo.wiki.new_page")
- c.Data["PageIsWiki"] = true
- c.Data["RequireSimpleMDE"] = true
- if c.HasError() {
- c.HTML(200, WIKI_NEW)
- return
- }
- if err := c.Repo.Repository.EditWikiPage(c.User, f.OldTitle, f.Title, f.Content, f.Message); err != nil {
- c.Handle(500, "EditWikiPage", err)
- return
- }
- c.Redirect(c.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(models.ToWikiPageName(f.Title)))
- }
- func DeleteWikiPagePost(c *context.Context) {
- pageURL := c.Params(":page")
- if len(pageURL) == 0 {
- pageURL = "Home"
- }
- pageName := models.ToWikiPageName(pageURL)
- if err := c.Repo.Repository.DeleteWikiPage(c.User, pageName); err != nil {
- c.Handle(500, "DeleteWikiPage", err)
- return
- }
- c.JSON(200, map[string]interface{}{
- "redirect": c.Repo.RepoLink + "/wiki/",
- })
- }
|