mirror of
https://github.com/alecthomas/chroma.git
synced 2025-07-17 01:22:22 +02:00
Add bool argument to WithClasses, WithLineNumbers etc.
This allows the boolean options to be reconfigured, e.g: ```go options := getOptions() options = append(options, html.WithLineNumbers(true)) ``` Fixes #301
This commit is contained in:
committed by
Alec Thomas
parent
d3926cc0e1
commit
5921c52787
@ -139,7 +139,7 @@ func main() {
|
|||||||
|
|
||||||
// Dump styles.
|
// Dump styles.
|
||||||
if cli.HTMLStyles {
|
if cli.HTMLStyles {
|
||||||
formatter := html.New(html.WithClasses())
|
formatter := html.New(html.WithClasses(true))
|
||||||
err = formatter.WriteCSS(w, style)
|
err = formatter.WriteCSS(w, style)
|
||||||
ctx.FatalIfErrorf(err)
|
ctx.FatalIfErrorf(err)
|
||||||
return
|
return
|
||||||
@ -174,19 +174,19 @@ func configureHTMLFormatter(ctx *kong.Context) {
|
|||||||
options = append(options, html.ClassPrefix(cli.HTMLPrefix))
|
options = append(options, html.ClassPrefix(cli.HTMLPrefix))
|
||||||
}
|
}
|
||||||
if !cli.HTMLInlineStyles {
|
if !cli.HTMLInlineStyles {
|
||||||
options = append(options, html.WithClasses())
|
options = append(options, html.WithClasses(true))
|
||||||
}
|
}
|
||||||
if !cli.HTMLOnly {
|
if !cli.HTMLOnly {
|
||||||
options = append(options, html.Standalone())
|
options = append(options, html.Standalone(true))
|
||||||
}
|
}
|
||||||
if cli.HTMLLines {
|
if cli.HTMLLines {
|
||||||
options = append(options, html.WithLineNumbers())
|
options = append(options, html.WithLineNumbers(true))
|
||||||
}
|
}
|
||||||
if cli.HTMLLinesTable {
|
if cli.HTMLLinesTable {
|
||||||
options = append(options, html.LineNumbersInTable())
|
options = append(options, html.LineNumbersInTable(true))
|
||||||
}
|
}
|
||||||
if cli.HTMLPreventSurroundingPre {
|
if cli.HTMLPreventSurroundingPre {
|
||||||
options = append(options, html.PreventSurroundingPre())
|
options = append(options, html.PreventSurroundingPre(true))
|
||||||
}
|
}
|
||||||
if len(cli.HTMLHighlight) > 0 {
|
if len(cli.HTMLHighlight) > 0 {
|
||||||
ranges := [][2]int{}
|
ranges := [][2]int{}
|
||||||
|
BIN
cmd/chromad/chromad
Executable file
BIN
cmd/chromad/chromad
Executable file
Binary file not shown.
@ -102,7 +102,7 @@ func render(req *renderRequest) (*renderResponse, error) {
|
|||||||
buf := &strings.Builder{}
|
buf := &strings.Builder{}
|
||||||
options := []html.Option{}
|
options := []html.Option{}
|
||||||
if req.Classes {
|
if req.Classes {
|
||||||
options = append(options, html.WithClasses(), html.Standalone())
|
options = append(options, html.WithClasses(true), html.Standalone(true))
|
||||||
}
|
}
|
||||||
formatter := html.New(options...)
|
formatter := html.New(options...)
|
||||||
err = formatter.Format(buf, style, tokens)
|
err = formatter.Format(buf, style, tokens)
|
||||||
|
@ -20,7 +20,7 @@ var (
|
|||||||
return nil
|
return nil
|
||||||
}))
|
}))
|
||||||
// Default HTML formatter outputs self-contained HTML.
|
// Default HTML formatter outputs self-contained HTML.
|
||||||
htmlFull = Register("html", html.New(html.Standalone(), html.WithClasses())) // nolint
|
htmlFull = Register("html", html.New(html.Standalone(true), html.WithClasses(true))) // nolint
|
||||||
SVG = Register("svg", svg.New(svg.EmbedFont("Liberation Mono", svg.FontLiberationMono, svg.WOFF)))
|
SVG = Register("svg", svg.New(svg.EmbedFont("Liberation Mono", svg.FontLiberationMono, svg.WOFF)))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,21 +14,25 @@ import (
|
|||||||
type Option func(f *Formatter)
|
type Option func(f *Formatter)
|
||||||
|
|
||||||
// Standalone configures the HTML formatter for generating a standalone HTML document.
|
// Standalone configures the HTML formatter for generating a standalone HTML document.
|
||||||
func Standalone() Option { return func(f *Formatter) { f.standalone = true } }
|
func Standalone(b bool) Option { return func(f *Formatter) { f.standalone = b } }
|
||||||
|
|
||||||
// ClassPrefix sets the CSS class prefix.
|
// ClassPrefix sets the CSS class prefix.
|
||||||
func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } }
|
func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } }
|
||||||
|
|
||||||
// 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(f *Formatter) { f.Classes = true } }
|
func WithClasses(b bool) Option { return func(f *Formatter) { f.Classes = b } }
|
||||||
|
|
||||||
// TabWidth sets the number of characters for a tab. Defaults to 8.
|
// TabWidth sets the number of characters for a tab. Defaults to 8.
|
||||||
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
|
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
|
||||||
|
|
||||||
// PreventSurroundingPre prevents the surrounding pre tags around the generated code
|
// PreventSurroundingPre prevents the surrounding pre tags around the generated code.
|
||||||
func PreventSurroundingPre() Option {
|
func PreventSurroundingPre(b bool) Option {
|
||||||
return func(f *Formatter) {
|
return func(f *Formatter) {
|
||||||
f.preWrapper = nopPreWrapper
|
if b {
|
||||||
|
f.preWrapper = nopPreWrapper
|
||||||
|
} else {
|
||||||
|
f.preWrapper = defaultPreWrapper
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,17 +44,17 @@ func WithPreWrapper(wrapper PreWrapper) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithLineNumbers formats output with line numbers.
|
// WithLineNumbers formats output with line numbers.
|
||||||
func WithLineNumbers() Option {
|
func WithLineNumbers(b bool) Option {
|
||||||
return func(f *Formatter) {
|
return func(f *Formatter) {
|
||||||
f.lineNumbers = true
|
f.lineNumbers = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LineNumbersInTable will, when combined with WithLineNumbers, separate the line numbers
|
// LineNumbersInTable will, when combined with WithLineNumbers, separate the line numbers
|
||||||
// and code in table td's, which make them copy-and-paste friendly.
|
// and code in table td's, which make them copy-and-paste friendly.
|
||||||
func LineNumbersInTable() Option {
|
func LineNumbersInTable(b bool) Option {
|
||||||
return func(f *Formatter) {
|
return func(f *Formatter) {
|
||||||
f.lineNumbersInTable = true
|
f.lineNumbersInTable = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ func TestFormatterStyleToCSS(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
formatter := New(WithClasses())
|
formatter := New(WithClasses(true))
|
||||||
css := formatter.styleToCSS(style)
|
css := formatter.styleToCSS(style)
|
||||||
for _, s := range css {
|
for _, s := range css {
|
||||||
if strings.HasPrefix(strings.TrimSpace(s), ";") {
|
if strings.HasPrefix(strings.TrimSpace(s), ";") {
|
||||||
@ -69,8 +69,8 @@ func TestFormatterStyleToCSS(t *testing.T) {
|
|||||||
|
|
||||||
func TestClassPrefix(t *testing.T) {
|
func TestClassPrefix(t *testing.T) {
|
||||||
wantPrefix := "some-prefix-"
|
wantPrefix := "some-prefix-"
|
||||||
withPrefix := New(WithClasses(), ClassPrefix(wantPrefix))
|
withPrefix := New(WithClasses(true), ClassPrefix(wantPrefix))
|
||||||
noPrefix := New(WithClasses())
|
noPrefix := New(WithClasses(true))
|
||||||
for st := range chroma.StandardTypes {
|
for st := range chroma.StandardTypes {
|
||||||
if noPrefix.class(st) == "" {
|
if noPrefix.class(st) == "" {
|
||||||
if got := withPrefix.class(st); got != "" {
|
if got := withPrefix.class(st); got != "" {
|
||||||
@ -90,7 +90,7 @@ func TestClassPrefix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTableLineNumberNewlines(t *testing.T) {
|
func TestTableLineNumberNewlines(t *testing.T) {
|
||||||
f := New(WithClasses(), WithLineNumbers(), LineNumbersInTable())
|
f := New(WithClasses(true), WithLineNumbers(true), LineNumbersInTable(true))
|
||||||
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
|
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
@ -130,22 +130,22 @@ func TestWithPreWrapper(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Regular", func(t *testing.T) {
|
t.Run("Regular", func(t *testing.T) {
|
||||||
s := format(New(WithClasses()))
|
s := format(New(WithClasses(true)))
|
||||||
assert.Equal(t, s, `<pre class="chroma"><span class="nb">echo</span> FOO</pre>`)
|
assert.Equal(t, s, `<pre class="chroma"><span class="nb">echo</span> FOO</pre>`)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PreventSurroundingPre", func(t *testing.T) {
|
t.Run("PreventSurroundingPre", func(t *testing.T) {
|
||||||
s := format(New(PreventSurroundingPre(), WithClasses()))
|
s := format(New(PreventSurroundingPre(true), WithClasses(true)))
|
||||||
assert.Equal(t, s, `<span class="nb">echo</span> FOO`)
|
assert.Equal(t, s, `<span class="nb">echo</span> FOO`)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Wrapper", func(t *testing.T) {
|
t.Run("Wrapper", func(t *testing.T) {
|
||||||
s := format(New(WithPreWrapper(wrapper), WithClasses()))
|
s := format(New(WithPreWrapper(wrapper), WithClasses(true)))
|
||||||
assert.Equal(t, s, `<foo class="chroma" id="code-true"><span class="nb">echo</span> FOO</foo>`)
|
assert.Equal(t, s, `<foo class="chroma" id="code-true"><span class="nb">echo</span> FOO</foo>`)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Wrapper, LineNumbersInTable", func(t *testing.T) {
|
t.Run("Wrapper, LineNumbersInTable", func(t *testing.T) {
|
||||||
s := format(New(WithPreWrapper(wrapper), WithClasses(), WithLineNumbers(), LineNumbersInTable()))
|
s := format(New(WithPreWrapper(wrapper), WithClasses(true), WithLineNumbers(true), LineNumbersInTable(true)))
|
||||||
|
|
||||||
assert.Equal(t, s, `<div class="chroma">
|
assert.Equal(t, s, `<div class="chroma">
|
||||||
<table class="lntable"><tr><td class="lntd">
|
<table class="lntable"><tr><td class="lntd">
|
||||||
@ -157,3 +157,23 @@ func TestWithPreWrapper(t *testing.T) {
|
|||||||
`)
|
`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReconfigureOptions(t *testing.T) {
|
||||||
|
options := []Option{
|
||||||
|
WithClasses(true),
|
||||||
|
WithLineNumbers(true),
|
||||||
|
}
|
||||||
|
|
||||||
|
options = append(options, WithLineNumbers(false))
|
||||||
|
|
||||||
|
f := New(options...)
|
||||||
|
|
||||||
|
it, err := lexers.Get("bash").Tokenise(nil, "echo FOO")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err = f.Format(&buf, styles.Fallback, it)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `<pre class="chroma"><span class="nb">echo</span> FOO</pre>`, buf.String())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user