Hello, I want to make a shortcode which allows me to paste multiple snippets from single file by their id.
I.e. I want to place that file in static folder:
// START full
func NewGame() *Game {
// START root
root := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(
image.NewNineSliceColor(colornames.Gainsboro),
),
)
// END root
return &Game{
ui: &ebitenui.UI{Container: root},
}
}
// END full
And pull different snippets from it, like this:
{{< code source="static/examples/bas_con_sin.txt" language="go" id="full">}}
func NewGame() *Game {
root := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(
image.NewNineSliceColor(colornames.Gainsboro),
),
)
return &Game{
ui: &ebitenui.UI{Container: root},
}
}
And this:
{{< code source="static/examples/bas_con_sin.txt" language="go" id="root">}}
root := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(
image.NewNineSliceColor(colornames.Gainsboro),
),
)
I have written a shortcode for that, but its not removing nested indents:
{{ $file := .Get "source" }}
{{ $id := .Get "id" }}
{{ $lang := .Get "language" }}
{{ $content := readFile $file }}
{{ $snippetStart := printf "// START %s" $id }}
{{ $snippetEnd := printf "// END %s" $id }}
{{ $inSnippet := false }}
{{ $snippet := "" }}
{{ range $line := split $content "\n" }}
{{ $trimmedLine := trim $line " \t\n\r" }}
{{ if eq $trimmedLine $snippetStart }}
{{ $inSnippet = true }}
{{ continue }}
{{ end }}
{{ if eq $trimmedLine $snippetEnd }}
{{ $inSnippet = false }}
{{ continue }}
{{ end }}
{{ if $inSnippet }}
<!-- Skip any nested START/END markers inside the snippet -->
{{ if not (or (findRE "^// START " $trimmedLine) (findRE "^// END " $trimmedLine)) }}
{{ $snippet = printf "%s%s\n" $snippet $line }}
{{ end }}
{{ end }}
{{ end }}
{{ $snippet := trim $snippet "\n" }}
{{ if $snippet }}
{{ $highlighted := highlight $snippet $lang "" }}
{{ $highlighted | safeHTML }}
{{ else }}
<p>No snippet found for id "{{ $id }}"</p>
{{ end }}
I’m struggle with nested indents a bit. Does someone knows, how to remove them properly?