From d863dde6c653700fb540d7e4dd6605c7186f59da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?=
%s
", inlineCodeAttrs(lang), gohtml.EscapeString(code)))
+ } else {
+ preWrapper := getPreWrapper(lang, w)
+ fmt.Fprint(w, preWrapper.Start(true, ""))
+ fmt.Fprint(w, gohtml.EscapeString(code))
+ fmt.Fprint(w, preWrapper.End(true))
+ }
+ return 0, 0, nil
}
style := styles.Get(cfg.Style)
@@ -194,20 +203,51 @@ func highlight(fw hugio.FlexiWriter, code, lang string, attributes []attributes.
return 0, 0, err
}
+ if !cfg.Hl_inline {
+ writeDivStart(w, attributes)
+ }
+
options := cfg.ToHTMLOptions()
- preWrapper := getPreWrapper(lang, w)
- options = append(options, html.WithPreWrapper(preWrapper))
+ var wrapper html.PreWrapper
+
+ if cfg.Hl_inline {
+ wrapper = startEnd{
+ start: func(code bool, styleAttr string) string {
+ if code {
+ return fmt.Sprintf(``, inlineCodeAttrs(lang))
+ }
+ return ``
+ },
+ end: func(code bool) string {
+ if code {
+ return `
`
+ }
+
+ return ``
+ },
+ }
+
+ } else {
+ wrapper = getPreWrapper(lang, w)
+ }
+
+ options = append(options, html.WithPreWrapper(wrapper))
formatter := html.New(options...)
- writeDivStart(w, attributes)
-
if err := formatter.Format(w, style, iterator); err != nil {
return 0, 0, err
}
- writeDivEnd(w)
- return preWrapper.low, preWrapper.high, nil
+ if !cfg.Hl_inline {
+ writeDivEnd(w)
+ }
+
+ if p, ok := wrapper.(*preWrapper); ok {
+ return p.low, p.high, nil
+ }
+
+ return 0, 0, nil
}
func getPreWrapper(language string, writeCounter *byteCountFlexiWriter) *preWrapper {
@@ -232,6 +272,12 @@ func (p *preWrapper) Start(code bool, styleAttr string) string {
return w.String()
}
+func inlineCodeAttrs(lang string) string {
+ if lang == "" {
+ }
+ return fmt.Sprintf(` class="code-inline language-%s"`, lang)
+}
+
func WritePreStart(w io.Writer, language, styleAttr string) {
fmt.Fprintf(w, ``, styleAttr)
fmt.Fprint(w, "
}}(message "this highlight shortcode"){{< /highlight >}}:End.
+Inline Unknown:{{< highlight foo "hl_inline=true" >}}(message "this highlight shortcode"){{< /highlight >}}:End.
+
+## Inline in code block
+
+Not sure if this makes sense, but add a test for it:
+
+§§§bash {hl_inline=true}
+(message "highlight me")
+§§§
+
+## HighlightCodeBlock in hook
+
+§§§html
+(message "highlight me 2")
+§§§
+
+## Unknown lexer
+
+§§§foo {hl_inline=true}
+(message "highlight me 3")
+§§§
+
+
+-- layouts/_default/_markup/render-codeblock-html.html --
+{{ $opts := dict "hl_inline" true }}
+{{ $result := transform.HighlightCodeBlock . $opts }}
+HighlightCodeBlock: Wrapped:{{ $result.Wrapped }}|Inner:{{ $result.Inner }}
+-- layouts/_default/single.html --
+{{ .Content }}
+`
+
+ b := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ NeedsOsFS: false,
+ },
+ ).Build()
+
+ b.AssertFileContent("public/p1/index.html",
+ "Inline:
(message "this highlight shortcode")
:End.",
+ "Inline Unknown:(message "this highlight shortcode")
:End.",
+ "Not sure if this makes sense, but add a test for it:
(message "highlight me")\n
",
+ "HighlightCodeBlock: Wrapped:(message "highlight me 2")
|Inner:(message "highlight me 2")
",
+ "(message "highlight me 3")\n
",
+ )
+}
diff --git a/markup/internal/attributes/attributes.go b/markup/internal/attributes/attributes.go
index edf857822..688740983 100644
--- a/markup/internal/attributes/attributes.go
+++ b/markup/internal/attributes/attributes.go
@@ -30,6 +30,7 @@ var chromaHightlightProcessingAttributes = map[string]bool{
"anchorLineNos": true,
"guessSyntax": true,
"hl_Lines": true,
+ "hl_inline": true,
"lineAnchors": true,
"lineNos": true,
"lineNoStart": true,