orgmode.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2017 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 markup
  5. import (
  6. "path/filepath"
  7. "strings"
  8. log "gopkg.in/clog.v1"
  9. "github.com/chaseadamsio/goorgeous"
  10. )
  11. var orgModeExtensions = []string{".org"}
  12. // IsOrgModeFile reports whether name looks like a Org-mode file based on its extension.
  13. func IsOrgModeFile(name string) bool {
  14. extension := strings.ToLower(filepath.Ext(name))
  15. for _, ext := range orgModeExtensions {
  16. if strings.ToLower(ext) == extension {
  17. return true
  18. }
  19. }
  20. return false
  21. }
  22. // RawOrgMode renders content in Org-mode syntax to HTML without handling special links.
  23. func RawOrgMode(body []byte, urlPrefix string) (result []byte) {
  24. // TODO: remove recover code once the third-party package is stable
  25. defer func() {
  26. if err := recover(); err != nil {
  27. result = body
  28. log.Warn("PANIC (RawOrgMode): %v", err)
  29. }
  30. }()
  31. return goorgeous.OrgCommon(body)
  32. }
  33. // OrgMode takes a string or []byte and renders to HTML in Org-mode syntax with special links.
  34. func OrgMode(input interface{}, urlPrefix string, metas map[string]string) []byte {
  35. return Render(ORG_MODE, input, urlPrefix, metas)
  36. }