diff --git a/formatters/html/html.go b/formatters/html/html.go index 982f7651d..0a45d8714 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -38,6 +38,8 @@ func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width // PreventSurroundingPre prevents the surrounding pre tags around the generated code. func PreventSurroundingPre(b bool) Option { return func(f *Formatter) { + f.preventSurroundingPre = b + if b { f.preWrapper = nopPreWrapper } else { @@ -46,6 +48,29 @@ func PreventSurroundingPre(b bool) Option { } } +// InlineCode creates inline code wrapped in a code tag. +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(``, styleAttr) + } + + return `` + }, + end: func(code bool) string { + if code { + return `` + } + + return `` + }, + } + } +} + // WithPreWrapper allows control of the surrounding pre tags. func WithPreWrapper(wrapper PreWrapper) Option { return func(f *Formatter) { @@ -163,20 +188,22 @@ var ( // Formatter that generates HTML. type Formatter struct { - standalone bool - prefix string - Classes bool // Exported field to detect when classes are being used - allClasses bool - customCSS map[chroma.TokenType]string - preWrapper PreWrapper - tabWidth int - wrapLongLines bool - lineNumbers bool - lineNumbersInTable bool - linkableLineNumbers bool - lineNumbersIDPrefix string - highlightRanges highlightRanges - baseLineNumber int + standalone bool + prefix string + Classes bool // Exported field to detect when classes are being used + allClasses bool + customCSS map[chroma.TokenType]string + preWrapper PreWrapper + inlineCode bool + preventSurroundingPre bool + tabWidth int + wrapLongLines bool + lineNumbers bool + lineNumbersInTable bool + linkableLineNumbers bool + lineNumbersIDPrefix string + highlightRanges highlightRanges + baseLineNumber int } type highlightRanges [][2]int @@ -257,26 +284,29 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma. highlightIndex++ } - // Start of Line - fmt.Fprint(w, ``) } else { - fmt.Fprintf(w, ` style="%s %s"`, css[chroma.Line], css[chroma.LineHighlight]) + fmt.Fprintf(w, "%s>", f.styleAttr(css, chroma.Line)) } - fmt.Fprint(w, `>`) - } else { - fmt.Fprintf(w, "%s>", f.styleAttr(css, chroma.Line)) - } - // Line number - if f.lineNumbers && !wrapInTable { - fmt.Fprintf(w, "%s", f.styleAttr(css, chroma.LineNumbers), f.lineIDAttribute(line), f.lineTitleWithLinkIfNeeded(lineDigits, line)) - } + // Line number + if f.lineNumbers && !wrapInTable { + fmt.Fprintf(w, "%s", f.styleAttr(css, chroma.LineNumbers), f.lineIDAttribute(line), f.lineTitleWithLinkIfNeeded(lineDigits, line)) + } - fmt.Fprintf(w, ``, f.styleAttr(css, chroma.CodeLine)) + fmt.Fprintf(w, ``, f.styleAttr(css, chroma.CodeLine)) + } for _, token := range tokens { html := html.EscapeString(token.String()) @@ -287,11 +317,12 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma. fmt.Fprint(w, html) } - fmt.Fprint(w, ``) // End of CodeLine + if !(f.preventSurroundingPre || f.inlineCode) { + fmt.Fprint(w, ``) // End of CodeLine - fmt.Fprint(w, ``) // End of Line + fmt.Fprint(w, ``) // End of Line + } } - fmt.Fprintf(w, f.preWrapper.End(true)) if wrapInTable { diff --git a/formatters/html/html_test.go b/formatters/html/html_test.go index 06a182939..d86eb7791 100644 --- a/formatters/html/html_test.go +++ b/formatters/html/html_test.go @@ -296,7 +296,17 @@ func TestWithPreWrapper(t *testing.T) { t.Run("PreventSurroundingPre", func(t *testing.T) { s := format(New(PreventSurroundingPre(true), WithClasses(true))) - assert.Equal(t, s, `echo FOO`) + assert.Equal(t, s, `echo FOO`) + }) + + t.Run("InlineCode", func(t *testing.T) { + s := format(New(InlineCode(true), WithClasses(true))) + assert.Equal(t, s, `echo FOO`) + }) + + t.Run("InlineCode, inline styles", func(t *testing.T) { + s := format(New(InlineCode(true))) + assert.Regexp(t, `echo FOO`, s) }) t.Run("Wrapper", func(t *testing.T) {