wiki.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. "github.com/gogits/git-shell"
  11. )
  12. // ToWikiPageName formats a string to corresponding wiki URL name.
  13. func ToWikiPageName(name string) string {
  14. return strings.Replace(name, " ", "-", -1)
  15. }
  16. // WikiPath returns wiki data path by given user and repository name.
  17. func WikiPath(userName, repoName string) string {
  18. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  19. }
  20. func (repo *Repository) WikiPath() string {
  21. return WikiPath(repo.MustOwner().Name, repo.Name)
  22. }
  23. // HasWiki returns true if repository has wiki.
  24. func (repo *Repository) HasWiki() bool {
  25. return com.IsDir(repo.WikiPath())
  26. }
  27. // InitWiki initializes a wiki for repository,
  28. // it does nothing when repository already has wiki.
  29. func (repo *Repository) InitWiki() error {
  30. if repo.HasWiki() {
  31. return nil
  32. }
  33. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  34. return fmt.Errorf("InitRepository: %v", err)
  35. }
  36. return nil
  37. }
  38. // AddWikiPage adds new page to repository wiki.
  39. func (repo *Repository) AddWikiPage(title, content, message string) (err error) {
  40. if err = repo.InitWiki(); err != nil {
  41. return fmt.Errorf("InitWiki: %v", err)
  42. }
  43. return nil
  44. }