Skip to content

Commit

Permalink
Prevent panic in Org mode HighlightCodeBlock (go-gitea#17140)
Browse files Browse the repository at this point in the history
Backport go-gitea#17140

When rendering source in org mode there is a mistake in the highlight code that
causes a panic.

This PR fixes this.

Fix go-gitea#17139

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Sep 24, 2021
1 parent 7ce938b commit 9a37156
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
27 changes: 16 additions & 11 deletions modules/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ func Code(fileName, code string) string {
if len(code) > sizeLimit {
return code
}
formatter := html.New(html.WithClasses(true),
html.WithLineNumbers(false),
html.PreventSurroundingPre(true),
)
if formatter == nil {
log.Error("Couldn't create chroma formatter")
return code
}

htmlbuf := bytes.Buffer{}
htmlw := bufio.NewWriter(&htmlbuf)

var lexer chroma.Lexer
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
Expand All @@ -97,6 +86,22 @@ func Code(fileName, code string) string {
}
cache.Add(fileName, lexer)
}
return CodeFromLexer(lexer, code)
}

// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
func CodeFromLexer(lexer chroma.Lexer, code string) string {
formatter := html.New(html.WithClasses(true),
html.WithLineNumbers(false),
html.PreventSurroundingPre(true),
)
if formatter == nil {
log.Error("Couldn't create chroma formatter")
return code
}

htmlbuf := bytes.Buffer{}
htmlw := bufio.NewWriter(&htmlbuf)

iterator, err := lexer.Tokenise(nil, string(code))
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion modules/markup/orgmode/orgmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -51,6 +52,12 @@ func (Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
htmlWriter := org.NewHTMLWriter()
htmlWriter.HighlightCodeBlock = func(source, lang string, inline bool) string {
defer func() {
if err := recover(); err != nil {
log.Error("Panic in HighlightCodeBlock: %v\n%s", err, log.Stack(2))
panic(err)
}
}()
var w strings.Builder
if _, err := w.WriteString(`<pre>`); err != nil {
return ""
Expand Down Expand Up @@ -80,7 +87,7 @@ func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
}
lexer = chroma.Coalesce(lexer)

if _, err := w.WriteString(highlight.Code(lexer.Config().Filenames[0], source)); err != nil {
if _, err := w.WriteString(highlight.CodeFromLexer(lexer, source)); err != nil {
return ""
}
}
Expand Down
26 changes: 26 additions & 0 deletions modules/markup/orgmode/orgmode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func TestRender_Images(t *testing.T) {
test("[[file:"+url+"]]",
"<p><img src=\""+result+"\" alt=\""+result+"\" title=\""+result+"\" /></p>")
}

func TestRender_Source(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL

test := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{
URLPrefix: setting.AppSubURL,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}

test(`#+begin_src go
// HelloWorld prints "Hello World"
func HelloWorld() {
fmt.Println("Hello World")
}
#+end_src
`, `<div class="src src-go">
<pre><code class="chroma language-go"><span class="c1">// HelloWorld prints &#34;Hello World&#34;
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
<span class="p">}</span></code></pre>
</div>`)
}

0 comments on commit 9a37156

Please sign in to comment.