Minor cleanups

Use fmt.Fprint were appropriate instead of Fprintf.  Remove some unused
code.
This commit is contained in:
Cameron Moore 2019-11-23 08:45:04 -06:00 committed by Bjørn Erik Pedersen
parent bfb9613a14
commit 20f351ee4c
2 changed files with 7 additions and 33 deletions

View file

@ -22,7 +22,6 @@ import (
"github.com/alecthomas/chroma/formatters/html" "github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers" "github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles" "github.com/alecthomas/chroma/styles"
hl "github.com/gohugoio/hugo/markup/highlight/temphighlighting" hl "github.com/gohugoio/hugo/markup/highlight/temphighlighting"
) )
@ -76,14 +75,15 @@ func highlight(code, lang string, cfg Config) (string, error) {
formatter := html.New(options...) formatter := html.New(options...)
fmt.Fprintf(w, `<div class="highlight">`) fmt.Fprint(w, `<div class="highlight">`)
if err := formatter.Format(w, style, iterator); err != nil { if err := formatter.Format(w, style, iterator); err != nil {
return "", err return "", err
} }
fmt.Fprintf(w, `</div>`) fmt.Fprint(w, `</div>`)
return w.String(), nil return w.String(), nil
} }
func GetCodeBlockOptions() func(ctx hl.CodeBlockContext) []html.Option { func GetCodeBlockOptions() func(ctx hl.CodeBlockContext) []html.Option {
return func(ctx hl.CodeBlockContext) []html.Option { return func(ctx hl.CodeBlockContext) []html.Option {
var language string var language string
@ -99,6 +99,7 @@ func GetCodeBlockOptions() func(ctx hl.CodeBlockContext) []html.Option {
func getPreWrapper(language string) preWrapper { func getPreWrapper(language string) preWrapper {
return preWrapper{language: language} return preWrapper{language: language}
} }
func getHtmlPreWrapper(language string) html.Option { func getHtmlPreWrapper(language string) html.Option {
return html.WithPreWrapper(getPreWrapper(language)) return html.WithPreWrapper(getPreWrapper(language))
} }
@ -121,8 +122,8 @@ func (p preWrapper) Start(code bool, styleAttr string) string {
func WriteCodeTag(w io.Writer, language string) { func WriteCodeTag(w io.Writer, language string) {
fmt.Fprint(w, "<code") fmt.Fprint(w, "<code")
if language != "" { if language != "" {
fmt.Fprintf(w, " class=\"language-"+language+"\"") fmt.Fprint(w, ` class="language-`+language+`"`)
fmt.Fprintf(w, " data-lang=\""+language+"\"") fmt.Fprint(w, ` data-lang="`+language+`"`)
} }
fmt.Fprint(w, ">") fmt.Fprint(w, ">")
} }

View file

@ -8,30 +8,11 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/yuin/goldmark/util"
chromahtml "github.com/alecthomas/chroma/formatters/html" chromahtml "github.com/alecthomas/chroma/formatters/html"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/util"
) )
type preWrapper struct {
language string
}
func (p preWrapper) Start(code bool, styleAttr string) string {
w := &strings.Builder{}
fmt.Fprintf(w, "<pre%s><code", styleAttr)
if p.language != "" {
fmt.Fprintf(w, " class=\"language-"+p.language)
}
fmt.Fprint(w, ">")
return w.String()
}
func (p preWrapper) End(code bool) string {
return "</code></pre>"
}
func TestHighlighting(t *testing.T) { func TestHighlighting(t *testing.T) {
var css bytes.Buffer var css bytes.Buffer
markdown := goldmark.New( markdown := goldmark.New(
@ -156,7 +137,6 @@ Title
/* GenericSubheading */ .chroma .gu { color: #75715e }`) { /* GenericSubheading */ .chroma .gu { color: #75715e }`) {
t.Error("failed to render CSS") t.Error("failed to render CSS")
} }
} }
func TestHighlighting2(t *testing.T) { func TestHighlighting2(t *testing.T) {
@ -240,9 +220,7 @@ func TestHighlightingHlLines(t *testing.T) {
{`hl_lines=["2-3",5],linenostart=5`, []int{2, 3, 5}}, {`hl_lines=["2-3",5],linenostart=5`, []int{2, 3, 5}},
{`hl_lines=["2-3"]`, []int{2, 3}}, {`hl_lines=["2-3"]`, []int{2, 3}},
} { } {
t.Run(fmt.Sprint(i), func(t *testing.T) { t.Run(fmt.Sprint(i), func(t *testing.T) {
var buffer bytes.Buffer var buffer bytes.Buffer
codeBlock := fmt.Sprintf(`bash {%s} codeBlock := fmt.Sprintf(`bash {%s}
LINE1 LINE1
@ -269,11 +247,9 @@ LINE8
} }
}) })
} }
} }
func TestHighlightingLinenos(t *testing.T) { func TestHighlightingLinenos(t *testing.T) {
outputLineNumbersInTable := `<div class="chroma"> outputLineNumbersInTable := `<div class="chroma">
<table class="lntable"><tr><td class="lntd"> <table class="lntable"><tr><td class="lntd">
<span class="lnt">1 <span class="lnt">1
@ -297,7 +273,6 @@ LINE1
{`linenos=foo`, false, false, `<span class="ln">1</span>LINE1`}, {`linenos=foo`, false, false, `<span class="ln">1</span>LINE1`},
{`linenos=table`, false, false, outputLineNumbersInTable}, {`linenos=table`, false, false, outputLineNumbersInTable},
} { } {
t.Run(fmt.Sprint(i), func(t *testing.T) { t.Run(fmt.Sprint(i), func(t *testing.T) {
markdown := goldmark.New( markdown := goldmark.New(
goldmark.WithExtensions( goldmark.WithExtensions(
@ -328,8 +303,6 @@ LINE1
if s != test.expect { if s != test.expect {
t.Fatal("got\n", s, "\nexpected\n", test.expect) t.Fatal("got\n", s, "\nexpected\n", test.expect)
} }
}) })
} }
} }