Skip to content

Commit d18e8a4

Browse files
CIAvashalecthomas
authored andcommitted
Add InlineCode option for inline code blocks
- Add an `InlineCode` option for inline code blocks - When `PreventSurroundingPre` option is enabled, do not wrap the code by `Line` and `CodeLine` - Write and update related tests
1 parent 530c45d commit d18e8a4

File tree

2 files changed

+75
-34
lines changed

2 files changed

+75
-34
lines changed

formatters/html/html.go

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width
3838
// PreventSurroundingPre prevents the surrounding pre tags around the generated code.
3939
func PreventSurroundingPre(b bool) Option {
4040
return func(f *Formatter) {
41+
f.preventSurroundingPre = b
42+
4143
if b {
4244
f.preWrapper = nopPreWrapper
4345
} else {
@@ -46,6 +48,29 @@ func PreventSurroundingPre(b bool) Option {
4648
}
4749
}
4850

51+
// InlineCode creates inline code wrapped in a code tag.
52+
func InlineCode(b bool) Option {
53+
return func(f *Formatter) {
54+
f.inlineCode = b
55+
f.preWrapper = preWrapper{
56+
start: func(code bool, styleAttr string) string {
57+
if code {
58+
return fmt.Sprintf(`<code%s>`, styleAttr)
59+
}
60+
61+
return ``
62+
},
63+
end: func(code bool) string {
64+
if code {
65+
return `</code>`
66+
}
67+
68+
return ``
69+
},
70+
}
71+
}
72+
}
73+
4974
// WithPreWrapper allows control of the surrounding pre tags.
5075
func WithPreWrapper(wrapper PreWrapper) Option {
5176
return func(f *Formatter) {
@@ -163,20 +188,22 @@ var (
163188

164189
// Formatter that generates HTML.
165190
type Formatter struct {
166-
standalone bool
167-
prefix string
168-
Classes bool // Exported field to detect when classes are being used
169-
allClasses bool
170-
customCSS map[chroma.TokenType]string
171-
preWrapper PreWrapper
172-
tabWidth int
173-
wrapLongLines bool
174-
lineNumbers bool
175-
lineNumbersInTable bool
176-
linkableLineNumbers bool
177-
lineNumbersIDPrefix string
178-
highlightRanges highlightRanges
179-
baseLineNumber int
191+
standalone bool
192+
prefix string
193+
Classes bool // Exported field to detect when classes are being used
194+
allClasses bool
195+
customCSS map[chroma.TokenType]string
196+
preWrapper PreWrapper
197+
inlineCode bool
198+
preventSurroundingPre bool
199+
tabWidth int
200+
wrapLongLines bool
201+
lineNumbers bool
202+
lineNumbersInTable bool
203+
linkableLineNumbers bool
204+
lineNumbersIDPrefix string
205+
highlightRanges highlightRanges
206+
baseLineNumber int
180207
}
181208

182209
type highlightRanges [][2]int
@@ -257,26 +284,29 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.
257284
highlightIndex++
258285
}
259286

260-
// Start of Line
261-
fmt.Fprint(w, `<span`)
262-
if highlight {
263-
// Line + LineHighlight
264-
if f.Classes {
265-
fmt.Fprintf(w, ` class="%s %s"`, f.class(chroma.Line), f.class(chroma.LineHighlight))
287+
if !(f.preventSurroundingPre || f.inlineCode) {
288+
// Start of Line
289+
fmt.Fprint(w, `<span`)
290+
291+
if highlight {
292+
// Line + LineHighlight
293+
if f.Classes {
294+
fmt.Fprintf(w, ` class="%s %s"`, f.class(chroma.Line), f.class(chroma.LineHighlight))
295+
} else {
296+
fmt.Fprintf(w, ` style="%s %s"`, css[chroma.Line], css[chroma.LineHighlight])
297+
}
298+
fmt.Fprint(w, `>`)
266299
} else {
267-
fmt.Fprintf(w, ` style="%s %s"`, css[chroma.Line], css[chroma.LineHighlight])
300+
fmt.Fprintf(w, "%s>", f.styleAttr(css, chroma.Line))
268301
}
269-
fmt.Fprint(w, `>`)
270-
} else {
271-
fmt.Fprintf(w, "%s>", f.styleAttr(css, chroma.Line))
272-
}
273302

274-
// Line number
275-
if f.lineNumbers && !wrapInTable {
276-
fmt.Fprintf(w, "<span%s%s>%s</span>", f.styleAttr(css, chroma.LineNumbers), f.lineIDAttribute(line), f.lineTitleWithLinkIfNeeded(lineDigits, line))
277-
}
303+
// Line number
304+
if f.lineNumbers && !wrapInTable {
305+
fmt.Fprintf(w, "<span%s%s>%s</span>", f.styleAttr(css, chroma.LineNumbers), f.lineIDAttribute(line), f.lineTitleWithLinkIfNeeded(lineDigits, line))
306+
}
278307

279-
fmt.Fprintf(w, `<span%s>`, f.styleAttr(css, chroma.CodeLine))
308+
fmt.Fprintf(w, `<span%s>`, f.styleAttr(css, chroma.CodeLine))
309+
}
280310

281311
for _, token := range tokens {
282312
html := html.EscapeString(token.String())
@@ -287,11 +317,12 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.
287317
fmt.Fprint(w, html)
288318
}
289319

290-
fmt.Fprint(w, `</span>`) // End of CodeLine
320+
if !(f.preventSurroundingPre || f.inlineCode) {
321+
fmt.Fprint(w, `</span>`) // End of CodeLine
291322

292-
fmt.Fprint(w, `</span>`) // End of Line
323+
fmt.Fprint(w, `</span>`) // End of Line
324+
}
293325
}
294-
295326
fmt.Fprintf(w, f.preWrapper.End(true))
296327

297328
if wrapInTable {

formatters/html/html_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,17 @@ func TestWithPreWrapper(t *testing.T) {
296296

297297
t.Run("PreventSurroundingPre", func(t *testing.T) {
298298
s := format(New(PreventSurroundingPre(true), WithClasses(true)))
299-
assert.Equal(t, s, `<span class="line"><span class="cl"><span class="nb">echo</span> FOO</span></span>`)
299+
assert.Equal(t, s, `<span class="nb">echo</span> FOO`)
300+
})
301+
302+
t.Run("InlineCode", func(t *testing.T) {
303+
s := format(New(InlineCode(true), WithClasses(true)))
304+
assert.Equal(t, s, `<code class="chroma"><span class="nb">echo</span> FOO</code>`)
305+
})
306+
307+
t.Run("InlineCode, inline styles", func(t *testing.T) {
308+
s := format(New(InlineCode(true)))
309+
assert.Regexp(t, `<code style=".+?"><span style=".+?">echo</span> FOO</code>`, s)
300310
})
301311

302312
t.Run("Wrapper", func(t *testing.T) {

0 commit comments

Comments
 (0)