Folks,
I’ve had a private short-code built on top of an early dev build of Hugo v.13 for a long time. I now find myself in a position where I need to upgrade to the current version of Hugo, but need to keep this short code working. Fairly obviously I have a huge amount of material that relies on this short code.
My new short code, drops the pygements based highlighting approach and replaces it with an alternative highlighter that uses the highlight tool.
With Hugo v13. this was simple to do. I just added the helper/new-highlight.go
file and hooked the new short code into the list of internal short codes in tpl/template_embeded.go
by adding an entry into the EmbedShortcodes()
method.
Now it seems that to do the same thing I need to start hacking both versions of
BlockCode
the block code method in helpers/content_render.go
.
What I need to do is change the code from this:
func (renderer *HugoHtmlRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) { if viper.GetBool("PygmentsCodeFences") { opts := viper.GetString("PygmentsOptions") str := html.UnescapeString(string(text)) out.WriteString(Highlight(str, lang, opts)) } else { renderer.Renderer.BlockCode(out, text, lang) } }
to this
func (renderer *HugoHtmlRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) { if viper.GetBool("PygmentsCodeFences") { opts := viper.GetString("PygmentsOptions") str := html.UnescapeString(string(text)) out.WriteString(Highlight(str, lang, opts)) // the new bit } if viper.GetBool("UseHighlight") { // use my alternative highlight scheme str := html.UnescapeString(string(text)) out.WriteString(Hilight(str, lang, lexer, lineNumbers)) // see below for an explanation of the extra options } else { renderer.Renderer.BlockCode(out, text, lang) } }
That’s the easy bit. The hard part is going to be more involved.
The problem is the parameters that are being passed about.
My short code and hence the new short codes function takes 4 parameters.
The code to highlight - as a string
The lexer to use - again as a string
The style name - again as a string
Line numbers on/off - also as a string.
Each of these varies on a per call basis. So the information passed to BlockCode
is insufficient. Also I cannot set these as “options” in the config for the same reasons.
To give you an idea of the scale of the problem it is not uncommon for me to have a web page containing code snippets of three or more different languages, each with their own highlighting and line number options.
So…Has anyone any idea how to attack this problem? It seems to me that the old approach will no longer work. A simple grep for Highlight
as well as my experimentation seems to confirm this.
Owen