Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions formatters/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,29 @@ func PreventSurroundingPre(b bool) Option {
func InlineCode(b bool) Option {
return func(f *Formatter) {
f.inlineCode = b
f.preWrapper = preWrapper{
start: func(code bool, styleAttr string) string {
if code {
return fmt.Sprintf(`<code%s>`, styleAttr)
}

return ``
},
end: func(code bool) string {
if code {
return `</code>`
}

return ``
},
if b {
f.preWrapper = preWrapper{
start: func(code bool, styleAttr string) string {
if code {
return fmt.Sprintf(`<code%s>`, styleAttr)
}

return ``
},
end: func(code bool) string {
if code {
return `</code>`
}

return ``
},
}
} else {
if f.preventSurroundingPre {
f.preWrapper = nopPreWrapper
} else {
f.preWrapper = defaultPreWrapper
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions formatters/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,21 @@ func TestWithPreWrapper(t *testing.T) {
assert.Equal(t, s, `<code class="chroma"><span class="nb">echo</span> FOO</code>`)
})

t.Run("InlineCode(false)", func(t *testing.T) {
s := format(New(InlineCode(false), WithClasses(true)))
assert.Equal(t, s, `<pre class="chroma"><code><span class="line"><span class="cl"><span class="nb">echo</span> FOO</span></span></code></pre>`)
})

t.Run("InlineCode(false) toggled off", func(t *testing.T) {
s := format(New(InlineCode(true), InlineCode(false), WithClasses(true)))
assert.Equal(t, s, `<pre class="chroma"><code><span class="line"><span class="cl"><span class="nb">echo</span> FOO</span></span></code></pre>`)
})

t.Run("InlineCode(false) with PreventSurroundingPre", func(t *testing.T) {
s := format(New(PreventSurroundingPre(true), InlineCode(false), WithClasses(true)))
assert.Equal(t, s, `<span class="nb">echo</span> FOO`)
})

t.Run("InlineCode, inline styles", func(t *testing.T) {
s := format(New(InlineCode(true)))
assert.True(t, regexp.MustCompile(`<code style=".+?"><span style=".+?">echo</span> FOO</code>`).MatchString(s))
Expand Down