diff options
author | Lauris BH <lauris@nix.lv> | 2019-10-31 03:06:25 +0200 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-10-31 01:06:25 +0000 |
commit | 086a46994a9f59ba06bfacdf2041817ef2f6671c (patch) | |
tree | 1a54a4b9f74d2a1c8a3827f24dec2fb77b8a3554 /modules/markup/orgmode | |
parent | 690a8ec502ff2e162b6db9cfe1f0555cf6b37323 (diff) | |
download | gitea-086a46994a9f59ba06bfacdf2041817ef2f6671c.tar.gz gitea-086a46994a9f59ba06bfacdf2041817ef2f6671c.zip |
Rewrite markdown rendering to blackfriday v2 and rewrite orgmode rendering to go-org (#8560)
* Rewrite markdown rendering to blackfriday v2.0
* Fix style
* Fix go mod with golang 1.13
* Fix blackfriday v2 import
* Inital orgmode renderer migration to go-org
* Vendor go-org dependency
* Ignore errors :/
* Update go-org to latest version
* Update test
* Fix go-org test
* Remove unneeded code
* Fix comments
* Fix markdown test
* Fix blackfriday regression rendering HTML block
Diffstat (limited to 'modules/markup/orgmode')
-rw-r--r-- | modules/markup/orgmode/orgmode.go | 100 | ||||
-rw-r--r-- | modules/markup/orgmode/orgmode_test.go | 10 |
2 files changed, 84 insertions, 26 deletions
diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index f63155201e..54188d2734 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -5,12 +5,16 @@ package markup import ( + "bytes" + "fmt" + "html" + "strings" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" - "code.gitea.io/gitea/modules/markup/markdown" + "code.gitea.io/gitea/modules/util" - "github.com/chaseadamsio/goorgeous" - "github.com/russross/blackfriday" + "github.com/niklasfasching/go-org/org" ) func init() { @@ -32,23 +36,23 @@ func (Parser) Extensions() []string { } // Render renders orgmode rawbytes to HTML -func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) (result []byte) { - defer func() { - if err := recover(); err != nil { - log.Error("Panic in orgmode.Render: %v Just returning the rawBytes", err) - result = rawBytes - } - }() - htmlFlags := blackfriday.HTML_USE_XHTML - htmlFlags |= blackfriday.HTML_SKIP_STYLE - htmlFlags |= blackfriday.HTML_OMIT_CONTENTS - renderer := &markdown.Renderer{ - Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""), - URLPrefix: urlPrefix, - IsWiki: isWiki, +func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { + htmlWriter := org.NewHTMLWriter() + + renderer := &Renderer{ + HTMLWriter: htmlWriter, + URLPrefix: urlPrefix, + IsWiki: isWiki, + } + + htmlWriter.ExtendingWriter = renderer + + res, err := org.New().Silent().Parse(bytes.NewReader(rawBytes), "").Write(renderer) + if err != nil { + log.Error("Panic in orgmode.Render: %v Just returning the rawBytes", err) + return rawBytes } - result = goorgeous.Org(rawBytes, renderer) - return + return []byte(res) } // RenderString reners orgmode string to HTML string @@ -56,7 +60,63 @@ func RenderString(rawContent string, urlPrefix string, metas map[string]string, return string(Render([]byte(rawContent), urlPrefix, metas, isWiki)) } -// Render implements markup.Parser +// Render reners orgmode string to HTML string func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { return Render(rawBytes, urlPrefix, metas, isWiki) } + +// Renderer implements org.Writer +type Renderer struct { + *org.HTMLWriter + URLPrefix string + IsWiki bool +} + +var byteMailto = []byte("mailto:") + +// WriteRegularLink renders images, links or videos +func (r *Renderer) WriteRegularLink(l org.RegularLink) { + link := []byte(html.EscapeString(l.URL)) + if l.Protocol == "file" { + link = link[len("file:"):] + } + if len(link) > 0 && !markup.IsLink(link) && + link[0] != '#' && !bytes.HasPrefix(link, byteMailto) { + lnk := string(link) + if r.IsWiki { + lnk = util.URLJoin("wiki", lnk) + } + link = []byte(util.URLJoin(r.URLPrefix, lnk)) + } + + description := string(link) + if l.Description != nil { + description = r.nodesAsString(l.Description...) + } + switch l.Kind() { + case "image": + r.WriteString(fmt.Sprintf(`<img src="%s" alt="%s" title="%s" />`, link, description, description)) + case "video": + r.WriteString(fmt.Sprintf(`<video src="%s" title="%s">%s</video>`, link, description, description)) + default: + r.WriteString(fmt.Sprintf(`<a href="%s" title="%s">%s</a>`, link, description, description)) + } +} + +func (r *Renderer) emptyClone() *Renderer { + wcopy := *(r.HTMLWriter) + wcopy.Builder = strings.Builder{} + + rcopy := *r + rcopy.HTMLWriter = &wcopy + + wcopy.ExtendingWriter = &rcopy + + return &rcopy +} + +func (r *Renderer) nodesAsString(nodes ...org.Node) string { + tmp := r.emptyClone() + org.WriteNodes(tmp, nodes...) + return tmp.String() +} diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 3846922c25..40323912b4 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -27,12 +27,12 @@ func TestRender_StandardLinks(t *testing.T) { assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } - googleRendered := `<p><a href="https://google.com/" title="https://google.com/">https://google.com/</a></p>` + googleRendered := "<p>\n<a href=\"https://google.com/\" title=\"https://google.com/\">https://google.com/</a>\n</p>" test("[[https://google.com/]]", googleRendered) lnk := util.URLJoin(AppSubURL, "WikiPage") test("[[WikiPage][WikiPage]]", - `<p><a href="`+lnk+`" title="WikiPage">WikiPage</a></p>`) + "<p>\n<a href=\""+lnk+"\" title=\"WikiPage\">WikiPage</a>\n</p>") } func TestRender_Images(t *testing.T) { @@ -45,10 +45,8 @@ func TestRender_Images(t *testing.T) { } url := "../../.images/src/02/train.jpg" - title := "Train" result := util.URLJoin(AppSubURL, url) - test( - "[[file:"+url+"]["+title+"]]", - `<p><a href="`+result+`"><img src="`+result+`" alt="`+title+`" title="`+title+`" /></a></p>`) + test("[[file:"+url+"]]", + "<p>\n<img src=\""+result+"\" alt=\""+result+"\" title=\""+result+"\" />\n</p>") } |