@@ -38,6 +38,8 @@ func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width
38
38
// PreventSurroundingPre prevents the surrounding pre tags around the generated code.
39
39
func PreventSurroundingPre (b bool ) Option {
40
40
return func (f * Formatter ) {
41
+ f .preventSurroundingPre = b
42
+
41
43
if b {
42
44
f .preWrapper = nopPreWrapper
43
45
} else {
@@ -46,6 +48,29 @@ func PreventSurroundingPre(b bool) Option {
46
48
}
47
49
}
48
50
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
+
49
74
// WithPreWrapper allows control of the surrounding pre tags.
50
75
func WithPreWrapper (wrapper PreWrapper ) Option {
51
76
return func (f * Formatter ) {
@@ -163,20 +188,22 @@ var (
163
188
164
189
// Formatter that generates HTML.
165
190
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
180
207
}
181
208
182
209
type highlightRanges [][2 ]int
@@ -257,26 +284,29 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.
257
284
highlightIndex ++
258
285
}
259
286
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 , `>` )
266
299
} else {
267
- fmt .Fprintf (w , ` style= "%s %s"` , css [ chroma . Line ], css [ chroma .LineHighlight ] )
300
+ fmt .Fprintf (w , "%s>" , f . styleAttr ( css , chroma .Line ) )
268
301
}
269
- fmt .Fprint (w , `>` )
270
- } else {
271
- fmt .Fprintf (w , "%s>" , f .styleAttr (css , chroma .Line ))
272
- }
273
302
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
+ }
278
307
279
- fmt .Fprintf (w , `<span%s>` , f .styleAttr (css , chroma .CodeLine ))
308
+ fmt .Fprintf (w , `<span%s>` , f .styleAttr (css , chroma .CodeLine ))
309
+ }
280
310
281
311
for _ , token := range tokens {
282
312
html := html .EscapeString (token .String ())
@@ -287,11 +317,12 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.
287
317
fmt .Fprint (w , html )
288
318
}
289
319
290
- fmt .Fprint (w , `</span>` ) // End of CodeLine
320
+ if ! (f .preventSurroundingPre || f .inlineCode ) {
321
+ fmt .Fprint (w , `</span>` ) // End of CodeLine
291
322
292
- fmt .Fprint (w , `</span>` ) // End of Line
323
+ fmt .Fprint (w , `</span>` ) // End of Line
324
+ }
293
325
}
294
-
295
326
fmt .Fprintf (w , f .preWrapper .End (true ))
296
327
297
328
if wrapInTable {
0 commit comments