Skip to content

Commit

Permalink
Add option to preserve all HTML classes.
Browse files Browse the repository at this point in the history
Fixes #346.
  • Loading branch information
alecthomas committed Apr 12, 2020
1 parent 937aba1 commit cc53faa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/chroma/main.go
Expand Up @@ -53,6 +53,7 @@ command, for Go.
HTML bool `help:"Enable HTML mode (equivalent to '--formatter html')."`
HTMLPrefix string `help:"HTML CSS class prefix." placeholder:"PREFIX"`
HTMLStyles bool `help:"Output HTML CSS styles."`
HTMLAllStyles bool `help:"Output all HTML CSS styles, including redundant ones."`
HTMLOnly bool `help:"Output HTML fragment."`
HTMLInlineStyles bool `help:"Output HTML with inline styles (no classes)."`
HTMLTabWidth int `help:"Set the HTML tab width." default:"8"`
Expand Down Expand Up @@ -139,7 +140,11 @@ func main() {

// Dump styles.
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)
ctx.FatalIfErrorf(err)
return
Expand Down Expand Up @@ -170,6 +175,7 @@ func configureHTMLFormatter(ctx *kong.Context) {
html.TabWidth(cli.HTMLTabWidth),
html.BaseLineNumber(cli.HTMLBaseLine),
html.ClassPrefix(cli.HTMLPrefix),
html.WithAllClasses(cli.HTMLAllStyles),
html.WithClasses(!cli.HTMLInlineStyles),
html.Standalone(!cli.HTMLOnly),
html.WithLineNumbers(cli.HTMLLines),
Expand Down
6 changes: 5 additions & 1 deletion formatters/html/html.go
Expand Up @@ -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.
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.
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }

Expand Down Expand Up @@ -141,6 +144,7 @@ type Formatter struct {
standalone bool
prefix string
Classes bool // Exported field to detect when classes are being used
allClasses bool
preWrapper PreWrapper
tabWidth int
lineNumbers bool
Expand Down Expand Up @@ -379,7 +383,7 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string
if t != chroma.Background {
entry = entry.Sub(bg)
}
if entry.IsZero() {
if !f.allClasses && entry.IsZero() {
continue
}
classes[t] = StyleEntryToCSS(entry)
Expand Down

0 comments on commit cc53faa

Please sign in to comment.