mirror of
https://github.com/alecthomas/chroma.git
synced 2025-11-25 22:32:32 +02:00
Add HTML formatter option for setting the tab width.
This commit is contained in:
@@ -36,6 +36,7 @@ var (
|
|||||||
htmlStylesFlag = kingpin.Flag("html-styles", "Output HTML CSS styles.").Bool()
|
htmlStylesFlag = kingpin.Flag("html-styles", "Output HTML CSS styles.").Bool()
|
||||||
htmlOnlyFlag = kingpin.Flag("html-only", "Output HTML fragment.").Bool()
|
htmlOnlyFlag = kingpin.Flag("html-only", "Output HTML fragment.").Bool()
|
||||||
htmlInlineStyleFlag = kingpin.Flag("html-inline-styles", "Output HTML with inline styles (no classes).").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()
|
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
|
||||||
)
|
)
|
||||||
@@ -73,7 +74,7 @@ command, for Go.
|
|||||||
*formatterFlag = "html"
|
*formatterFlag = "html"
|
||||||
}
|
}
|
||||||
if *formatterFlag == "html" {
|
if *formatterFlag == "html" {
|
||||||
options := []html.Option{}
|
options := []html.Option{html.TabWidth(*htmlTabWidthFlag)}
|
||||||
if *htmlPrefixFlag != "" {
|
if *htmlPrefixFlag != "" {
|
||||||
options = append(options, html.ClassPrefix(*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.
|
// WithClasses emits HTML using CSS classes, rather than inline styles.
|
||||||
func WithClasses() Option { return func(h *HTMLFormatter) { h.classes = true } }
|
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.
|
// New HTML formatter.
|
||||||
func New(options ...Option) *HTMLFormatter {
|
func New(options ...Option) *HTMLFormatter {
|
||||||
h := &HTMLFormatter{}
|
h := &HTMLFormatter{}
|
||||||
@@ -35,6 +38,7 @@ type HTMLFormatter struct {
|
|||||||
standalone bool
|
standalone bool
|
||||||
prefix string
|
prefix string
|
||||||
classes bool
|
classes bool
|
||||||
|
tabWidth int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTMLFormatter) Format(w io.Writer, style *chroma.Style) (func(*chroma.Token), error) {
|
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)
|
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) {
|
func (h *HTMLFormatter) formatWithoutClasses(w io.Writer, style *chroma.Style) (func(*chroma.Token), error) {
|
||||||
classes := h.typeStyles(style)
|
classes := h.typeStyles(style)
|
||||||
bg := compressStyle(classes[chroma.Background])
|
bg := compressStyle(classes[chroma.Background])
|
||||||
@@ -172,6 +183,7 @@ func (h *HTMLFormatter) typeStyles(style *chroma.Style) map[chroma.TokenType]str
|
|||||||
styles := h.class(e)
|
styles := h.class(e)
|
||||||
classes[t] = strings.Join(styles, "; ")
|
classes[t] = strings.Join(styles, "; ")
|
||||||
}
|
}
|
||||||
|
classes[chroma.Background] += h.tabWidthStyle()
|
||||||
return classes
|
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.
|
// If given and greater than 0, expand tabs in the input.
|
||||||
// TabSize int
|
// TabSize int
|
||||||
|
|
||||||
// Whether to track how long rules take to process.
|
|
||||||
TimeRules bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token output to formatter.
|
// Token output to formatter.
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
. "github.com/alecthomas/chroma" // nolint
|
. "github.com/alecthomas/chroma" // nolint
|
||||||
)
|
)
|
||||||
|
|
||||||
// Glsl lexer.
|
// GLSL lexer.
|
||||||
var Glsl = Register(MustNewLexer(
|
var GLSL = Register(MustNewLexer(
|
||||||
&Config{
|
&Config{
|
||||||
Name: "GLSL",
|
Name: "GLSL",
|
||||||
Aliases: []string{"glsl"},
|
Aliases: []string{"glsl"},
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ var Xorg = Register(MustNewLexer(
|
|||||||
},
|
},
|
||||||
Rules{
|
Rules{
|
||||||
"root": {
|
"root": {
|
||||||
{`\s+`, Text, nil},
|
{`\s+`, TextWhitespace, nil},
|
||||||
{`#.*$`, Comment, nil},
|
{`#.*$`, Comment, nil},
|
||||||
{`((|Sub)Section)(\s+)("\w+")`, ByGroups(LiteralStringEscape, LiteralStringEscape, Text, LiteralStringEscape), nil},
|
{`((|Sub)Section)(\s+)("\w+")`, ByGroups(KeywordNamespace, LiteralStringEscape, TextWhitespace, LiteralStringEscape), nil},
|
||||||
{`(End(|Sub)Section)`, LiteralStringEscape, nil},
|
{`(End(|Sub)Section)`, KeywordNamespace, nil},
|
||||||
{`(\w+)(\s+)([^\n#]+)`, ByGroups(NameBuiltin, Text, NameConstant), nil},
|
{`(\w+)(\s+)([^\n#]+)`, ByGroups(NameKeyword, TextWhitespace, LiteralString), nil},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user