diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-04-20 06:25:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-19 18:25:08 -0400 |
commit | 9d99f6ab19ac3f97af3ca126720e9075c127a652 (patch) | |
tree | b817b4582a871f83b91ad7977fe772fc3501c1e8 /modules/markup/orgmode | |
parent | c9cc6698d2172625854cd063301e63602204a2a1 (diff) | |
download | gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.tar.gz gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.zip |
Refactor renders (#15175)
* Refactor renders
* Some performance optimization
* Fix comment
* Transform reader
* Fix csv test
* Fix test
* Fix tests
* Improve optimaziation
* Fix test
* Fix test
* Detect file encoding with reader
* Improve optimaziation
* reduce memory usage
* improve code
* fix build
* Fix test
* Fix for go1.15
* Fix render
* Fix comment
* Fix lint
* Fix test
* Don't use NormalEOF when unnecessary
* revert change on util.go
* Apply suggestions from code review
Co-authored-by: zeripath <art27@cantab.net>
* rename function
* Take NormalEOF back
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules/markup/orgmode')
-rw-r--r-- | modules/markup/orgmode/orgmode.go | 60 | ||||
-rw-r--r-- | modules/markup/orgmode/orgmode_test.go | 11 |
2 files changed, 41 insertions, 30 deletions
diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index b445b76956..96e67f90cf 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -8,9 +8,9 @@ import ( "bytes" "fmt" "html" + "io" "strings" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/util" @@ -18,58 +18,62 @@ import ( ) func init() { - markup.RegisterParser(Parser{}) + markup.RegisterRenderer(Renderer{}) } -// Parser implements markup.Parser for orgmode -type Parser struct { +// Renderer implements markup.Renderer for orgmode +type Renderer struct { } -// Name implements markup.Parser -func (Parser) Name() string { +// Name implements markup.Renderer +func (Renderer) Name() string { return "orgmode" } -// NeedPostProcess implements markup.Parser -func (Parser) NeedPostProcess() bool { return true } +// NeedPostProcess implements markup.Renderer +func (Renderer) NeedPostProcess() bool { return true } -// Extensions implements markup.Parser -func (Parser) Extensions() []string { +// Extensions implements markup.Renderer +func (Renderer) Extensions() []string { return []string{".org"} } // Render renders orgmode rawbytes to HTML -func Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { +func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error { htmlWriter := org.NewHTMLWriter() - renderer := &Renderer{ + w := &Writer{ HTMLWriter: htmlWriter, - URLPrefix: urlPrefix, - IsWiki: isWiki, + URLPrefix: ctx.URLPrefix, + IsWiki: ctx.IsWiki, } - htmlWriter.ExtendingWriter = renderer + htmlWriter.ExtendingWriter = w - res, err := org.New().Silent().Parse(bytes.NewReader(rawBytes), "").Write(renderer) + res, err := org.New().Silent().Parse(input, "").Write(w) if err != nil { - log.Error("Panic in orgmode.Render: %v Just returning the rawBytes", err) - return rawBytes + return fmt.Errorf("orgmode.Render failed: %v", err) } - return []byte(res) + _, err = io.Copy(output, strings.NewReader(res)) + return err } -// RenderString reners orgmode string to HTML string -func RenderString(rawContent string, urlPrefix string, metas map[string]string, isWiki bool) string { - return string(Render([]byte(rawContent), urlPrefix, metas, isWiki)) +// RenderString renders orgmode string to HTML string +func RenderString(ctx *markup.RenderContext, content string) (string, error) { + var buf strings.Builder + if err := Render(ctx, strings.NewReader(content), &buf); err != nil { + return "", err + } + return buf.String(), nil } -// 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) +// Render renders orgmode string to HTML string +func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error { + return Render(ctx, input, output) } -// Renderer implements org.Writer -type Renderer struct { +// Writer implements org.Writer +type Writer struct { *org.HTMLWriter URLPrefix string IsWiki bool @@ -78,7 +82,7 @@ type Renderer struct { var byteMailto = []byte("mailto:") // WriteRegularLink renders images, links or videos -func (r *Renderer) WriteRegularLink(l org.RegularLink) { +func (r *Writer) WriteRegularLink(l org.RegularLink) { link := []byte(html.EscapeString(l.URL)) if l.Protocol == "file" { link = link[len("file:"):] diff --git a/modules/markup/orgmode/orgmode_test.go b/modules/markup/orgmode/orgmode_test.go index 020a3f592a..da89326e9e 100644 --- a/modules/markup/orgmode/orgmode_test.go +++ b/modules/markup/orgmode/orgmode_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -23,7 +24,10 @@ func TestRender_StandardLinks(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - buffer := RenderString(input, setting.AppSubURL, nil, false) + buffer, err := RenderString(&markup.RenderContext{ + URLPrefix: setting.AppSubURL, + }, input) + assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } @@ -40,7 +44,10 @@ func TestRender_Images(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - buffer := RenderString(input, setting.AppSubURL, nil, false) + buffer, err := RenderString(&markup.RenderContext{ + URLPrefix: setting.AppSubURL, + }, input) + assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) } |