123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // 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-shell"
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/modules/auth"
- "github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/middleware"
- )
- const (
- WIKI_START base.TplName = "repo/wiki/start"
- WIKI_VIEW base.TplName = "repo/wiki/view"
- WIKI_NEW base.TplName = "repo/wiki/new"
- WIKI_PAGES base.TplName = "repo/wiki/pages"
- )
- func MustEnableWiki(ctx *middleware.Context) {
- if !ctx.Repo.Repository.EnableWiki {
- ctx.Handle(404, "MustEnableWiki", nil)
- }
- }
- type PageMeta struct {
- Name string
- URL string
- Updated time.Time
- }
- 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 nil, ""
- }
- commit, err := wikiRepo.GetCommitOfBranch("master")
- if err != nil {
- ctx.Handle(500, "GetCommitOfBranch", err)
- return nil, ""
- }
- // Get page list.
- if isViewPage {
- entries, err := commit.ListEntries()
- if err != nil {
- ctx.Handle(500, "ListEntries", err)
- return nil, ""
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type == git.OBJECT_BLOB {
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: models.ToWikiPageURL(name),
- })
- }
- }
- ctx.Data["Pages"] = pages
- }
- pageURL := ctx.Params(":page")
- if len(pageURL) == 0 {
- pageURL = "Home"
- }
- 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(pageName + ".md")
- if err != nil {
- if git.IsErrNotExist(err) {
- ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
- } else {
- ctx.Handle(500, "GetBlobByPath", err)
- }
- return nil, ""
- }
- r, err := blob.Data()
- if err != nil {
- ctx.Handle(500, "Data", err)
- 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, ctx.Repo.Repository.ComposeMetas()))
- } 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
- }
- // Get last change information.
- lastCommit, err := wikiRepo.GetCommitByPath(pageName + ".md")
- if err != nil {
- ctx.Handle(500, "GetCommitByPath", err)
- return
- }
- ctx.Data["Author"] = lastCommit.Author
- ctx.HTML(200, WIKI_VIEW)
- }
- func WikiPages(ctx *middleware.Context) {
- ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
- ctx.Data["PageIsWiki"] = true
- if !ctx.Repo.Repository.HasWiki() {
- ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
- return
- }
- wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
- if err != nil {
- ctx.Handle(500, "OpenRepository", err)
- return
- }
- commit, err := wikiRepo.GetCommitOfBranch("master")
- if err != nil {
- ctx.Handle(500, "GetCommitOfBranch", err)
- return
- }
- entries, err := commit.ListEntries()
- if err != nil {
- ctx.Handle(500, "ListEntries", err)
- return
- }
- pages := make([]PageMeta, 0, len(entries))
- for i := range entries {
- if entries[i].Type == git.OBJECT_BLOB {
- c, err := wikiRepo.GetCommitByPath(entries[i].Name())
- if err != nil {
- ctx.Handle(500, "GetCommit", err)
- return
- }
- name := strings.TrimSuffix(entries[i].Name(), ".md")
- pages = append(pages, PageMeta{
- Name: name,
- URL: models.ToWikiPageURL(name),
- Updated: c.Author.When,
- })
- }
- }
- ctx.Data["Pages"] = pages
- ctx.HTML(200, WIKI_PAGES)
- }
- func NewWiki(ctx *middleware.Context) {
- ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
- ctx.Data["PageIsWiki"] = true
- ctx.Data["RequireSimpleMDE"] = true
- if !ctx.Repo.Repository.HasWiki() {
- ctx.Data["title"] = "Home"
- }
- ctx.HTML(200, WIKI_NEW)
- }
- func NewWikiPost(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.AddWikiPage(ctx.User, form.Title, form.Content, form.Message); err != nil {
- 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
- }
- ctx.Redirect(ctx.Repo.RepoLink + "/wiki/" + models.ToWikiPageURL(form.Title))
- }
- func EditWiki(ctx *middleware.Context) {
- 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))
- }
|