Skip to content

Commit

Permalink
Fix indentation in highlight shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 30, 2022
1 parent 9e904d7 commit 8a82a40
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
23 changes: 22 additions & 1 deletion hugolib/shortcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type ShortcodeWithPage struct {
// this ordinal will represent the position of this shortcode in the page content.
Ordinal int

// Indentation before the opening shortcode.
indentation string

// pos is the position in bytes in the source file. Used for error logging.
posInit sync.Once
posOffset int
Expand All @@ -70,6 +73,24 @@ type ShortcodeWithPage struct {
scratch *maps.Scratch
}

// InnerDeindented returns the (potentially de-indented) inner content of the shortcode.
func (scp *ShortcodeWithPage) InnerDeindented() template.HTML {
if scp.indentation == "" {
return scp.Inner
}
var sb strings.Builder
text.VisitLinesAfter(string(scp.Inner), func(s string) {
//fmt.Printf("%s=>%s", util.VisualizeSpaces([]byte(scp.indentation)), util.VisualizeSpaces([]byte(s)))
if strings.HasPrefix(s, scp.indentation) {
sb.WriteString(strings.TrimPrefix(s, scp.indentation))
} else {
sb.WriteString(s)
}
})
//fmt.Println(string(util.VisualizeSpaces([]byte(sb.String()))))
return template.HTML(sb.String())
}

// Position returns this shortcode's detailed position. Note that this information
// may be expensive to calculate, so only use this in error situations.
func (scp *ShortcodeWithPage) Position() text.Position {
Expand Down Expand Up @@ -326,7 +347,7 @@ func renderShortcode(
hasVariants = hasVariants || more
}

data := &ShortcodeWithPage{Ordinal: sc.ordinal, posOffset: sc.pos, Params: sc.params, Page: newPageForShortcode(p), Parent: parent, Name: sc.name}
data := &ShortcodeWithPage{Ordinal: sc.ordinal, posOffset: sc.pos, indentation: sc.indentation, Params: sc.params, Page: newPageForShortcode(p), Parent: parent, Name: sc.name}
if sc.params != nil {
data.IsNamedParams = reflect.TypeOf(sc.params).Kind() == reflect.Map
}
Expand Down
45 changes: 45 additions & 0 deletions hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,48 @@ echo "foo";
b.AssertFileContent("public/p1/index.html", "<pre><code>echo &quot;foo&quot;;\n</code></pre>")

}

func TestShortcodeHighlightIndent(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
[markup]
[markup.highlight]
codeFences = true
noClasses = false
-- content/p1.md --
---
title: "p1"
---
## Indent 5 Spaces
{{< highlight bash >}}
line 1;
line 2;
line 3;
{{< /highlight >}}
-- layouts/_default/single.html --
{{ .Content }}
`

b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
Running: true,
},
).Build()

fc := b.FileContent("public/p1/index.html")

fmt.Println(fc)

b.AssertFileContent("public/p1/index.html", `
`)

}
2 changes: 1 addition & 1 deletion tpl/tplimpl/embedded/templates/shortcodes/highlight.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}
{{ if len .Params | eq 2 }}{{ highlight (trim .InnerDeindented "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .InnerDeindented "\n\r") (.Get 0) "" }}{{ end }}
2 changes: 1 addition & 1 deletion tpl/tplimpl/template_ast_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (c *templateContext) collectInner(n *parse.CommandNode) {
idents = nt.Ident
}

if c.hasIdent(idents, "Inner") {
if c.hasIdent(idents, "Inner") || c.hasIdent(idents, "InnerDeindented") {
c.t.parseInfo.IsInner = true
break
}
Expand Down

0 comments on commit 8a82a40

Please sign in to comment.