Skip to content

Commit

Permalink
Add bool argument to WithClasses, WithLineNumbers etc.
Browse files Browse the repository at this point in the history
This allows the boolean options to be reconfigured, e.g:

```go
options := getOptions()
options = append(options, html.WithLineNumbers(true))
```

Fixes #301
  • Loading branch information
bep authored and alecthomas committed Nov 21, 2019
1 parent d3926cc commit 5921c52
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
12 changes: 6 additions & 6 deletions cmd/chroma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand Down
Binary file added cmd/chromad/chromad
Binary file not shown.
2 changes: 1 addition & 1 deletion cmd/chromad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion formatters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)

Expand Down
22 changes: 13 additions & 9 deletions formatters/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
f.preWrapper = nopPreWrapper
if b {
f.preWrapper = nopPreWrapper
} else {
f.preWrapper = defaultPreWrapper
}
}
}

Expand All @@ -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
}
}

Expand Down
36 changes: 28 additions & 8 deletions formatters/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), ";") {
Expand All @@ -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 != "" {
Expand All @@ -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)

Expand Down Expand Up @@ -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">
Expand All @@ -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())
}

0 comments on commit 5921c52

Please sign in to comment.