1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-07-15 01:14:21 +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:
Bjørn Erik Pedersen
2019-11-21 09:24:48 +01:00
committed by Alec Thomas
parent d3926cc0e1
commit 5921c52787
6 changed files with 49 additions and 25 deletions

View File

@ -139,7 +139,7 @@ func main() {
// Dump styles.
if cli.HTMLStyles {
formatter := html.New(html.WithClasses())
formatter := html.New(html.WithClasses(true))
err = formatter.WriteCSS(w, style)
ctx.FatalIfErrorf(err)
return
@ -174,19 +174,19 @@ func configureHTMLFormatter(ctx *kong.Context) {
options = append(options, html.ClassPrefix(cli.HTMLPrefix))
}
if !cli.HTMLInlineStyles {
options = append(options, html.WithClasses())
options = append(options, html.WithClasses(true))
}
if !cli.HTMLOnly {
options = append(options, html.Standalone())
options = append(options, html.Standalone(true))
}
if cli.HTMLLines {
options = append(options, html.WithLineNumbers())
options = append(options, html.WithLineNumbers(true))
}
if cli.HTMLLinesTable {
options = append(options, html.LineNumbersInTable())
options = append(options, html.LineNumbersInTable(true))
}
if cli.HTMLPreventSurroundingPre {
options = append(options, html.PreventSurroundingPre())
options = append(options, html.PreventSurroundingPre(true))
}
if len(cli.HTMLHighlight) > 0 {
ranges := [][2]int{}

BIN
cmd/chromad/chromad Executable file

Binary file not shown.

View File

@ -102,7 +102,7 @@ func render(req *renderRequest) (*renderResponse, error) {
buf := &strings.Builder{}
options := []html.Option{}
if req.Classes {
options = append(options, html.WithClasses(), html.Standalone())
options = append(options, html.WithClasses(true), html.Standalone(true))
}
formatter := html.New(options...)
err = formatter.Format(buf, style, tokens)

View File

@ -20,7 +20,7 @@ var (
return nil
}))
// 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)))
)

View File

@ -14,21 +14,25 @@ import (
type Option func(f *Formatter)
// 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.
func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } }
// 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.
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
// PreventSurroundingPre prevents the surrounding pre tags around the generated code
func PreventSurroundingPre() Option {
// PreventSurroundingPre prevents the surrounding pre tags around the generated code.
func PreventSurroundingPre(b bool) Option {
return func(f *Formatter) {
if b {
f.preWrapper = nopPreWrapper
} else {
f.preWrapper = defaultPreWrapper
}
}
}
@ -40,17 +44,17 @@ func WithPreWrapper(wrapper PreWrapper) Option {
}
// WithLineNumbers formats output with line numbers.
func WithLineNumbers() Option {
func WithLineNumbers(b bool) Option {
return func(f *Formatter) {
f.lineNumbers = true
f.lineNumbers = b
}
}
// LineNumbersInTable will, when combined with WithLineNumbers, separate the line numbers
// 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) {
f.lineNumbersInTable = true
f.lineNumbersInTable = b
}
}

View File

@ -58,7 +58,7 @@ func TestFormatterStyleToCSS(t *testing.T) {
if err != nil {
t.Error(err)
}
formatter := New(WithClasses())
formatter := New(WithClasses(true))
css := formatter.styleToCSS(style)
for _, s := range css {
if strings.HasPrefix(strings.TrimSpace(s), ";") {
@ -69,8 +69,8 @@ func TestFormatterStyleToCSS(t *testing.T) {
func TestClassPrefix(t *testing.T) {
wantPrefix := "some-prefix-"
withPrefix := New(WithClasses(), ClassPrefix(wantPrefix))
noPrefix := New(WithClasses())
withPrefix := New(WithClasses(true), ClassPrefix(wantPrefix))
noPrefix := New(WithClasses(true))
for st := range chroma.StandardTypes {
if noPrefix.class(st) == "" {
if got := withPrefix.class(st); got != "" {
@ -90,7 +90,7 @@ func TestClassPrefix(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")
assert.NoError(t, err)
@ -130,22 +130,22 @@ func TestWithPreWrapper(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>`)
})
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`)
})
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>`)
})
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">
<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())
}