orgmode.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package markup
  7. import (
  8. "path/filepath"
  9. "strings"
  10. "github.com/chaseadamsio/goorgeous"
  11. log "gopkg.in/clog.v1"
  12. )
  13. var orgModeExtensions = []string{".org"}
  14. // IsOrgModeFile reports whether name looks like a Org-mode file based on its extension.
  15. func IsOrgModeFile(name string) bool {
  16. extension := strings.ToLower(filepath.Ext(name))
  17. for _, ext := range orgModeExtensions {
  18. if strings.ToLower(ext) == extension {
  19. return true
  20. }
  21. }
  22. return false
  23. }
  24. // RawOrgMode renders content in Org-mode syntax to HTML without handling special links.
  25. func RawOrgMode(body []byte, urlPrefix string) (result []byte) {
  26. // TODO: remove recover code once the third-party package is stable
  27. defer func() {
  28. if err := recover(); err != nil {
  29. result = body
  30. log.Warn("PANIC (RawOrgMode): %v", err)
  31. }
  32. }()
  33. return goorgeous.OrgCommon(body)
  34. }
  35. // OrgMode takes a string or []byte and renders to HTML in Org-mode syntax with special links.
  36. func OrgMode(input interface{}, urlPrefix string, metas map[string]string) []byte {
  37. return Render(OrgModeMP, input, urlPrefix, metas)
  38. }