HTMLWriter: Change code block markup to resemble hugo chroma output

The highlight function from hugo already wraps the highlighted source code in
div > pre > code

e.g.

<div class="highlight">
<pre
style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<code class="language-sh" data-lang="sh">echo hello world</code>
</pre>
</div>

So now we always delegate all that to the highlight function
This commit is contained in:
Niklas Fasching 2018-12-03 00:50:44 +01:00
parent d5665fb21c
commit 09a8437b59
2 changed files with 7 additions and 17 deletions

View file

@ -34,7 +34,7 @@ var listTags = map[string][]string{
func NewHTMLWriter() *HTMLWriter { func NewHTMLWriter() *HTMLWriter {
return &HTMLWriter{ return &HTMLWriter{
HighlightCodeBlock: func(source, lang string) string { HighlightCodeBlock: func(source, lang string) string {
return fmt.Sprintf("<pre>%s</pre>", html.EscapeString(source)) return fmt.Sprintf(`<div class="highlight"><pre>%s</pre></div>`, html.EscapeString(source))
}, },
} }
} }
@ -112,9 +112,7 @@ func (w *HTMLWriter) writeBlock(b Block) {
for _, n := range b.Children { for _, n := range b.Children {
lines = append(lines, n.(Line).Children[0].(Text).Content) lines = append(lines, n.(Line).Children[0].(Text).Content)
} }
w.WriteString(fmt.Sprintf(`<code class="src src-%s">`, lang) + "\n") w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang) + "\n")
w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang))
w.WriteString("\n</code>\n")
case "EXAMPLE": case "EXAMPLE":
w.WriteString(`<pre class="example">\n`) w.WriteString(`<pre class="example">\n`)
w.writeNodes(b.Children...) w.writeNodes(b.Children...)

View file

@ -63,9 +63,7 @@ and some lines of text
<p> <p>
and another subitem and another subitem
</p> </p>
<code class="src src-sh"> <div class="highlight"><pre>echo with a block</pre></div>
<pre>echo with a block</pre>
</code>
</li> </li>
<li> <li>
<p> <p>
@ -166,14 +164,10 @@ auto link, i.e. not inside <code class="verbatim">\[[square brackets]\]</code> <
</li> </li>
</ul> </ul>
<h2>blocks</h2> <h2>blocks</h2>
<code class="src src-bash"> <div class="highlight"><pre>echo a bash source block</pre></div>
<pre>echo a bash source block</pre> <div class="highlight"><pre>a source block without a language
</code>
<code class="src src-text">
<pre>a source block without a language
and a second line and a second line
and a third one</pre> and a third one</pre></div>
</code>
<pre class="example">\nan example block <pre class="example">\nan example block
with multiple lines with multiple lines
</pre> </pre>
@ -280,9 +274,7 @@ and other elements
<p> <p>
like blocks like blocks
</p> </p>
<code class="src src-text"> <div class="highlight"><pre>other non-plain</pre></div>
<pre>other non-plain</pre>
</code>
</li> </li>
<li> <li>
<p> <p>