mirror of
https://github.com/alecthomas/chroma.git
synced 2025-01-12 01:22:30 +02:00
Add HTML formatter option for setting the tab width.
This commit is contained in:
parent
631fc87d6e
commit
87183b3633
@ -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))
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
3
lexer.go
3
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.
|
||||
|
@ -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"},
|
||||
|
@ -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},
|
||||
},
|
||||
},
|
||||
))
|
||||
|
Loading…
Reference in New Issue
Block a user