本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

63 lines
1.7KB

  1. // Copyright 2017 The Gitea 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. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/markup"
  8. "code.gitea.io/gitea/modules/markup/markdown"
  9. "github.com/chaseadamsio/goorgeous"
  10. "github.com/russross/blackfriday"
  11. )
  12. func init() {
  13. markup.RegisterParser(Parser{})
  14. }
  15. // Parser implements markup.Parser for orgmode
  16. type Parser struct {
  17. }
  18. // Name implements markup.Parser
  19. func (Parser) Name() string {
  20. return "orgmode"
  21. }
  22. // Extensions implements markup.Parser
  23. func (Parser) Extensions() []string {
  24. return []string{".org"}
  25. }
  26. // Render renders orgmode rawbytes to HTML
  27. func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) (result []byte) {
  28. defer func() {
  29. if err := recover(); err != nil {
  30. log.Error("Panic in orgmode.Render: %v Just returning the rawBytes", err)
  31. result = rawBytes
  32. }
  33. }()
  34. htmlFlags := blackfriday.HTML_USE_XHTML
  35. htmlFlags |= blackfriday.HTML_SKIP_STYLE
  36. htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
  37. renderer := &markdown.Renderer{
  38. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  39. URLPrefix: urlPrefix,
  40. IsWiki: isWiki,
  41. }
  42. result = goorgeous.Org(rawBytes, renderer)
  43. return
  44. }
  45. // RenderString reners orgmode string to HTML string
  46. func RenderString(rawContent string, urlPrefix string, metas map[string]string, isWiki bool) string {
  47. return string(Render([]byte(rawContent), urlPrefix, metas, isWiki))
  48. }
  49. // Render implements markup.Parser
  50. func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
  51. return Render(rawBytes, urlPrefix, metas, isWiki)
  52. }
上海开阖软件有限公司 沪ICP备12045867号-1