Skip to content

Commit

Permalink
Add HTML formatter option for setting the tab width.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Sep 19, 2017
1 parent 631fc87 commit 87183b3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cmd/chroma/main.go
Expand Up @@ -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()
)
Expand Down Expand Up @@ -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))
}
Expand Down
12 changes: 12 additions & 0 deletions formatters/html/html.go
Expand Up @@ -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{}
Expand All @@ -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) {
Expand All @@ -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])
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 0 additions & 3 deletions lexer.go
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lexers/glsl.go
Expand Up @@ -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"},
Expand Down
8 changes: 4 additions & 4 deletions lexers/xorg.go
Expand Up @@ -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},
},
},
))

0 comments on commit 87183b3

Please sign in to comment.