Including program code in pages?

After a long break from website creation, I am planning one that will have program source code snippets and listings on many of its pages. Would Hugo be suitable for this?

I have created HTML pages by hand in the past, but have no experience of Markdown or Go.

Whilst reading through the online docs I am a little concerned that Hugo might modify some of the code as it processes the pages. For example, it seems that Blackfriday still converts 1/2, 1/4 and 3/4 even with fractions set to false in the configuration.

I want code such as “PRINT 1/2” to remain intact.

Is there a way to ensure that certain text remains as written?

Can Hugo “include” other text files in a page without modifying the content?

If you want code to remain intact, use code blocks…either with the triple back tick or with the built in highlight shortcode…

1 Like

I don’t need the code to be highlighted, and there isn’t likely to be a suitable language definition if I did (the code will be various BASIC dialects).
I just need the code to remain unmodified.
Is there a way to include other text files with Hugo? If not, I may be better off using another generator.

Yes Hugo can include text without modifying it. Check out the readFile function. Triple back ticks won’t give you syntax highlighting unless you run it through pygments on the server or through a client side highlighter like highlight.js. Triple back ticks in content are just standard markdown for <code> and <pre>. As far as output, Hugo Dan write to any text-based format…

Maybe I’m not understanding how Hugo works, but don’t functions like readFile only work inside template files? I was hoping to pull in content from a text file in the middle of a markdown file, as that would make life easier whenever the code changed.

I think shortcodes can use readFile. And then, that shortcode is usable in markdown.

I haven’t tried, but a shortcode like this should be possible to create:

{{< includeText "/path/to/textFile" />}}

_layouts/shortcodes/includeText:

<pre>
  <code>
    {{ readFile ( .Get 0 ) }}
  </code>
</pre>
1 Like