mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
afd63bf7d5
commit
10928a4f78
7 changed files with 74 additions and 3 deletions
|
@ -14,6 +14,7 @@
|
|||
package text
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
|
||||
|
@ -45,3 +46,18 @@ func RemoveAccentsString(s string) string {
|
|||
accentTransformerPool.Put(t)
|
||||
return s
|
||||
}
|
||||
|
||||
// Chomp removes trailing newline characters from s.
|
||||
func Chomp(s string) string {
|
||||
return strings.TrimRightFunc(s, func(r rune) bool {
|
||||
return r == '\n' || r == '\r'
|
||||
})
|
||||
}
|
||||
|
||||
// Puts adds a trailing \n none found.
|
||||
func Puts(s string) string {
|
||||
if s == "" || s[len(s)-1] == '\n' {
|
||||
return s
|
||||
}
|
||||
return s + "\n"
|
||||
}
|
||||
|
|
|
@ -26,3 +26,18 @@ func TestRemoveAccents(t *testing.T) {
|
|||
c.Assert(string(RemoveAccents([]byte("Hugo Rocks!"))), qt.Equals, "Hugo Rocks!")
|
||||
c.Assert(string(RemoveAccentsString("Resumé")), qt.Equals, "Resume")
|
||||
}
|
||||
|
||||
func TestChomp(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
c.Assert(Chomp("\nA\n"), qt.Equals, "\nA")
|
||||
c.Assert(Chomp("A\r\n"), qt.Equals, "A")
|
||||
}
|
||||
|
||||
func TestPuts(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
c.Assert(Puts("A"), qt.Equals, "A\n")
|
||||
c.Assert(Puts("\nA\n"), qt.Equals, "\nA\n")
|
||||
c.Assert(Puts(""), qt.Equals, "")
|
||||
}
|
||||
|
|
|
@ -28,6 +28,9 @@ import (
|
|||
)
|
||||
|
||||
func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuilder {
|
||||
// Code fences.
|
||||
conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§§", "```")
|
||||
|
||||
data := txtar.Parse([]byte(conf.TxtarString))
|
||||
|
||||
c, ok := conf.T.(*qt.C)
|
||||
|
|
|
@ -113,3 +113,34 @@ Go Language: golang|
|
|||
"<h2 id=\"bash-code\">Bash Code</h2>\n<div class=\"highlight blue\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\"><span class=\"line\"><span class=\"ln\">32</span><span class=\"cl\"><span class=\"nb\">echo</span> <span class=\"s2\">"l1"</span><span class=\"p\">;</span>\n</span></span><span class=\"line hl\"><span class=\"ln\">33</span>",
|
||||
)
|
||||
}
|
||||
|
||||
func TestCodeChomp(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
-- content/p1.md --
|
||||
---
|
||||
title: "p1"
|
||||
---
|
||||
|
||||
§§§bash
|
||||
echo "p1";
|
||||
§§§
|
||||
-- layouts/_default/single.html --
|
||||
{{ .Content }}
|
||||
-- layouts/_default/_markup/render-codeblock.html --
|
||||
|{{ .Code | safeHTML }}|
|
||||
|
||||
`
|
||||
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: false,
|
||||
},
|
||||
).Build()
|
||||
|
||||
b.AssertFileContent("public/p1/index.html", "|echo \"p1\";|")
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
|
||||
htext "github.com/gohugoio/hugo/common/text"
|
||||
"github.com/gohugoio/hugo/markup/converter/hooks"
|
||||
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
|
||||
"github.com/gohugoio/hugo/markup/internal/attributes"
|
||||
|
@ -75,7 +76,8 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
|
|||
line := n.b.Lines().At(i)
|
||||
buff.Write(line.Value(src))
|
||||
}
|
||||
text := buff.String()
|
||||
|
||||
text := htext.Chomp(buff.String())
|
||||
|
||||
var info []byte
|
||||
if n.b.Info != nil {
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/alecthomas/chroma/lexers"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
"github.com/gohugoio/hugo/common/hugio"
|
||||
"github.com/gohugoio/hugo/common/text"
|
||||
"github.com/gohugoio/hugo/identity"
|
||||
"github.com/gohugoio/hugo/markup/converter/hooks"
|
||||
"github.com/gohugoio/hugo/markup/internal/attributes"
|
||||
|
@ -123,7 +124,9 @@ func (h chromaHighlighter) RenderCodeblock(w hugio.FlexiWriter, ctx hooks.Codebl
|
|||
return err
|
||||
}
|
||||
|
||||
return highlight(w, ctx.Code(), ctx.Lang(), attributes, cfg)
|
||||
code := text.Puts(ctx.Code())
|
||||
|
||||
return highlight(w, code, ctx.Lang(), attributes, cfg)
|
||||
}
|
||||
|
||||
var id = identity.NewPathIdentity("chroma", "highlight")
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gohugoio/hugo/common/text"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
|
||||
|
@ -119,7 +120,7 @@ func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
res := strings.TrimRight(ss, "\r\n")
|
||||
res := text.Chomp(ss)
|
||||
switch s.(type) {
|
||||
case template.HTML:
|
||||
return template.HTML(res), nil
|
||||
|
|
Loading…
Reference in a new issue