Hi. I am in the process of migrating my web site from WordPress to Hugo. I am looking for a way to inline content from a local static file for highlighting in post content. I am really new to Hugo and still learning everyday.
In my wordpress posts, I used the Crayon Syntax Highlighter plugin in order to insert highlighted code examples. This plugin allowed me to refer to a local file path instead of embedding the actual code within the post itself. In other words, if the code had to be changed, I did not had to edit all posts that referred the file, locate the inline code and make the changes.
I am looking for a way to do something similar with Hugo.
I tried to use a dummy shortcode to read a file and output its content as markdown :
{{
$path := .Get "file" }}{{
$content := readFile $path }}{{
print $content }}
and in my post, I tried multiple ways to call the shortcode:
# Test 1 - code fence with cpp (comments)
```cpp
int a = 0;
{{< inline_static_file file="/static/wp-content/uploads/2015/01/guess.cpp" >}}
string c;
```
# Test 2
```cpp
int a = 0;
/* {{< inline_static_file file="/static/wp-content/uploads/2015/01/guess.cpp" >}} */
string c;
```
# Test 3
{{< highlight cpp "linenos=table,hl_lines=8 15-17,linenostart=199" >}}
{{< inline_static_file file="/static/wp-content/uploads/2015/01/guess.cpp" >}}
{{< /highlight >}}
# Test 4
```
{{< inline_static_file file="/static/wp-content/uploads/2015/01/guess.cpp" >}}
```
None of the tests above seems to have worked.
- In test #1, the shortcode did not output anything. I only see the c++ code from the post:
int a = 0;
andstring c;
. - In tests #2, now the c++ content is printed as a multi-line comment. If i remove the
/* */
, I end up like in test #1. - In test #3 the c++ code seems to have been converted to HTML. The highlight section shows literal
<
and"
. The double quotes were converted to their matching HTML entities as if it was the actual content ofguess.cpp
. - In test #4, I see the content of the file as expected but it is not highlighted as c++. If I add
cpp
to identify the code as c++, I end up like in test #1 (no content printed).
I think I do not understand Hugoβs parsing pipeline. From my point of view, Hugo reads my post, execute all shortcodes (which outputs markdown content) and then the whole βresolvedβ post is sent as markdown to the theme (or templates) be converted to HTML. From my viewpoint, I would like my shortcode to be similar to a c++ #include
directive.
If that helps, I am using hugo v0.87.0-B0C541E4 windows/amd64 and hugo-future-imperfect-slim theme.
I apologize if this question was already asked. I have been struggling with this problem for a while now and my google-fu about this subject (and searching this forum) has not given me any results.