From 87183b36336f51dd913c17ca3a5d569739f38b34 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Tue, 19 Sep 2017 13:14:29 +1000 Subject: [PATCH] Add HTML formatter option for setting the tab width. --- cmd/chroma/main.go | 3 ++- formatters/html/html.go | 12 ++++++++++++ lexer.go | 3 --- lexers/glsl.go | 4 ++-- lexers/xorg.go | 8 ++++---- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/chroma/main.go b/cmd/chroma/main.go index 170744e..49ec3d8 100644 --- a/cmd/chroma/main.go +++ b/cmd/chroma/main.go @@ -36,6 +36,7 @@ var ( htmlStylesFlag = kingpin.Flag("html-styles", "Output HTML CSS styles.").Bool() htmlOnlyFlag = kingpin.Flag("html-only", "Output HTML fragment.").Bool() htmlInlineStyleFlag = kingpin.Flag("html-inline-styles", "Output HTML with inline styles (no classes).").Bool() + htmlTabWidthFlag = kingpin.Flag("html-tab-width", "Set the HTML tab width.").Default("8").Int() filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles() ) @@ -73,7 +74,7 @@ command, for Go. *formatterFlag = "html" } if *formatterFlag == "html" { - options := []html.Option{} + options := []html.Option{html.TabWidth(*htmlTabWidthFlag)} if *htmlPrefixFlag != "" { options = append(options, html.ClassPrefix(*htmlPrefixFlag)) } diff --git a/formatters/html/html.go b/formatters/html/html.go index 39b0752..7a77034 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -22,6 +22,9 @@ func ClassPrefix(prefix string) Option { return func(h *HTMLFormatter) { h.prefi // WithClasses emits HTML using CSS classes, rather than inline styles. func WithClasses() Option { return func(h *HTMLFormatter) { h.classes = true } } +// TabWidth sets the number of characters for a tab. Defaults to 8. +func TabWidth(width int) Option { return func(h *HTMLFormatter) { h.tabWidth = width } } + // New HTML formatter. func New(options ...Option) *HTMLFormatter { h := &HTMLFormatter{} @@ -35,6 +38,7 @@ type HTMLFormatter struct { standalone bool prefix string classes bool + tabWidth int } func (h *HTMLFormatter) Format(w io.Writer, style *chroma.Style) (func(*chroma.Token), error) { @@ -44,6 +48,13 @@ func (h *HTMLFormatter) Format(w io.Writer, style *chroma.Style) (func(*chroma.T return h.formatWithoutClasses(w, style) } +func (h *HTMLFormatter) tabWidthStyle() string { + if h.tabWidth != 0 && h.tabWidth != 8 { + return fmt.Sprintf("; -moz-tab-size: %[1]d; -o-tab-size: %[1]d; tab-size: %[1]d", h.tabWidth) + } + return "" +} + func (h *HTMLFormatter) formatWithoutClasses(w io.Writer, style *chroma.Style) (func(*chroma.Token), error) { classes := h.typeStyles(style) bg := compressStyle(classes[chroma.Background]) @@ -172,6 +183,7 @@ func (h *HTMLFormatter) typeStyles(style *chroma.Style) map[chroma.TokenType]str styles := h.class(e) classes[t] = strings.Join(styles, "; ") } + classes[chroma.Background] += h.tabWidthStyle() return classes } diff --git a/lexer.go b/lexer.go index ab98488..8bbc2c7 100644 --- a/lexer.go +++ b/lexer.go @@ -55,9 +55,6 @@ type Config struct { // If given and greater than 0, expand tabs in the input. // TabSize int - - // Whether to track how long rules take to process. - TimeRules bool } // Token output to formatter. diff --git a/lexers/glsl.go b/lexers/glsl.go index 7a6a7ec..4a40242 100644 --- a/lexers/glsl.go +++ b/lexers/glsl.go @@ -4,8 +4,8 @@ import ( . "github.com/alecthomas/chroma" // nolint ) -// Glsl lexer. -var Glsl = Register(MustNewLexer( +// GLSL lexer. +var GLSL = Register(MustNewLexer( &Config{ Name: "GLSL", Aliases: []string{"glsl"}, diff --git a/lexers/xorg.go b/lexers/xorg.go index 698b3f7..4e8d6ea 100644 --- a/lexers/xorg.go +++ b/lexers/xorg.go @@ -14,11 +14,11 @@ var Xorg = Register(MustNewLexer( }, Rules{ "root": { - {`\s+`, Text, nil}, + {`\s+`, TextWhitespace, nil}, {`#.*$`, Comment, nil}, - {`((|Sub)Section)(\s+)("\w+")`, ByGroups(LiteralStringEscape, LiteralStringEscape, Text, LiteralStringEscape), nil}, - {`(End(|Sub)Section)`, LiteralStringEscape, nil}, - {`(\w+)(\s+)([^\n#]+)`, ByGroups(NameBuiltin, Text, NameConstant), nil}, + {`((|Sub)Section)(\s+)("\w+")`, ByGroups(KeywordNamespace, LiteralStringEscape, TextWhitespace, LiteralStringEscape), nil}, + {`(End(|Sub)Section)`, KeywordNamespace, nil}, + {`(\w+)(\s+)([^\n#]+)`, ByGroups(NameKeyword, TextWhitespace, LiteralString), nil}, }, }, ))