1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-21 21:17:50 +02:00

Add option to preserve all HTML classes.

Fixes .
This commit is contained in:
Alec Thomas 2020-04-13 07:36:37 +10:00
parent 937aba1514
commit cc53faa932
2 changed files with 12 additions and 2 deletions
cmd/chroma
formatters/html

@ -53,6 +53,7 @@ command, for Go.
HTML bool `help:"Enable HTML mode (equivalent to '--formatter html')."` HTML bool `help:"Enable HTML mode (equivalent to '--formatter html')."`
HTMLPrefix string `help:"HTML CSS class prefix." placeholder:"PREFIX"` HTMLPrefix string `help:"HTML CSS class prefix." placeholder:"PREFIX"`
HTMLStyles bool `help:"Output HTML CSS styles."` HTMLStyles bool `help:"Output HTML CSS styles."`
HTMLAllStyles bool `help:"Output all HTML CSS styles, including redundant ones."`
HTMLOnly bool `help:"Output HTML fragment."` HTMLOnly bool `help:"Output HTML fragment."`
HTMLInlineStyles bool `help:"Output HTML with inline styles (no classes)."` HTMLInlineStyles bool `help:"Output HTML with inline styles (no classes)."`
HTMLTabWidth int `help:"Set the HTML tab width." default:"8"` HTMLTabWidth int `help:"Set the HTML tab width." default:"8"`
@ -139,7 +140,11 @@ func main() {
// Dump styles. // Dump styles.
if cli.HTMLStyles { if cli.HTMLStyles {
formatter := html.New(html.WithClasses(true)) options := []html.Option{html.WithClasses(true)}
if cli.HTMLAllStyles {
options = append(options, html.WithAllClasses(true))
}
formatter := html.New(options...)
err = formatter.WriteCSS(w, style) err = formatter.WriteCSS(w, style)
ctx.FatalIfErrorf(err) ctx.FatalIfErrorf(err)
return return
@ -170,6 +175,7 @@ func configureHTMLFormatter(ctx *kong.Context) {
html.TabWidth(cli.HTMLTabWidth), html.TabWidth(cli.HTMLTabWidth),
html.BaseLineNumber(cli.HTMLBaseLine), html.BaseLineNumber(cli.HTMLBaseLine),
html.ClassPrefix(cli.HTMLPrefix), html.ClassPrefix(cli.HTMLPrefix),
html.WithAllClasses(cli.HTMLAllStyles),
html.WithClasses(!cli.HTMLInlineStyles), html.WithClasses(!cli.HTMLInlineStyles),
html.Standalone(!cli.HTMLOnly), html.Standalone(!cli.HTMLOnly),
html.WithLineNumbers(cli.HTMLLines), html.WithLineNumbers(cli.HTMLLines),

@ -22,6 +22,9 @@ func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix =
// WithClasses emits HTML using CSS classes, rather than inline styles. // WithClasses emits HTML using CSS classes, rather than inline styles.
func WithClasses(b bool) Option { return func(f *Formatter) { f.Classes = b } } func WithClasses(b bool) Option { return func(f *Formatter) { f.Classes = b } }
// WithAllClasses disables an optimisation that omits redundant CSS classes.
func WithAllClasses(b bool) Option { return func(f *Formatter) { f.allClasses = b } }
// TabWidth sets the number of characters for a tab. Defaults to 8. // TabWidth sets the number of characters for a tab. Defaults to 8.
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } } func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
@ -141,6 +144,7 @@ type Formatter struct {
standalone bool standalone bool
prefix string prefix string
Classes bool // Exported field to detect when classes are being used Classes bool // Exported field to detect when classes are being used
allClasses bool
preWrapper PreWrapper preWrapper PreWrapper
tabWidth int tabWidth int
lineNumbers bool lineNumbers bool
@ -379,7 +383,7 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string
if t != chroma.Background { if t != chroma.Background {
entry = entry.Sub(bg) entry = entry.Sub(bg)
} }
if entry.IsZero() { if !f.allClasses && entry.IsZero() {
continue continue
} }
classes[t] = StyleEntryToCSS(entry) classes[t] = StyleEntryToCSS(entry)