# \[Solved\] Including source files from a repo in HUGO posts

**URL:** https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683
**Category:** support
**Created:** [March 4, 2017, 11:09am UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683 "2017-03-04T11:09:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mvelosop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mvelosop/32/2757_2.png) [@mvelosop](https://discourse.gohugo.io/u/mvelosop)
#### Post date: [March 4, 2017, 11:09am UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/1 "2017-03-04T11:09:57Z")

</div>

I’m setting up a blog as a commented code repo, and I’m including source files right from the repos on the posts.

I’ve been able to get it to a working condition and want to improve it but I’m stuck.

## What I’ve done

There’s a .gitignore’d **repos** dir inside the HUGO site root, that holds the source code repos.

There’s a **getSourceFile.html** shortcode:

```
{{ with .Get 0 }}
<pre><code>{{ readFile . }}</code>
<span class="source-footer">{{.}}</span>
</pre>
{{ end }}

```

Then, in the post I can use the shortcode like so:

```
#### Base/EntityTypeConfiguration.cs

Estas clases permiten manejar una clase de configuración por cada clase del modelo...

{{< getSourceFile "repos/EFCoreApp/src/EFCore.App/Base/EntityTypeConfiguration.cs" >}}

```

and I get this:

 ![](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/4/4919256a54aff7b8d724ad6582610fe1740af79e.png)

Which is pretty nice, since I don’t have to copy and paste code, it’s 100% up to date and I am sure it compiles.

But this is where I’m stuck!

## What I would like to do

1. Setting up the repo root in the front matter so the shortcode is simpler to use, like so:

2. Being able to pass the language as a parameter to the shortcode to use it in the highlighting feature, something like so (this does not work):

**getSourceFile.html** :

````
{{ with .Get 0 }}
```{{.Get 1}}
<pre><code>{{ readFile . }}</code>
<span class="source-footer">{{.}}</span>
</pre>
```
{{ end }}

````

Or better yet, infer it from the file extension! 😉

I think it shouldn’t be too difficult but this is my first experience with Hugo, Go and the Templates, so, Could someone help me with this, please?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [March 4, 2017, 11:14am UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/2 "2017-03-04T11:14:46Z")

</div>

As to 1), set it in site config (i.e. config.toml or similar):

```auto
[params]
repoRoot = "src/something"

```

Then pick up that value from the shortcode:

```auto
$root := .Site.Param "repoRoot"

```

---

<div class="post-metadata">

### Author: ![rwi](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@rwi](https://discourse.gohugo.io/u/rwi)
#### Post date: [March 4, 2017, 11:32am UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/3 "2017-03-04T11:32:05Z")

</div>

> [@mvelosop](#):
>
> 1. Being able to pass the language as a parameter to the shortcode to use it in the highlighting feature, something like so (this does not work):
> 
> {{\< getSourceFile “src/EFCore.App/Base/EntityTypeConfiguration.cs” “csharp” \>}}
> 
> getSourceFile.html:
> 
> ````auto
> {{ with .Get 0 }}
> ```{{.Get 1}}
> &lt;pre&gt;&lt;code&gt;{{ readFile . }}&lt;/code&gt;
> &lt;span class="source-footer"&gt;{{.}}&lt;/span&gt;
> &lt;/pre&gt;
> ```
> {{ end }}
> 
> ````
> 
> Or better yet, infer it from the file extension! 😉

Since bep already explained the first part, I try to give you the idea why your approach for point 2 did not work.

You have used `with` to check if the `.Get 0` parameter exists. Within this block, the context (the dot - `.`) has just the filestring. So you try to access `.Get` of the string you passed in.

To do so you can either use `$.Get 1` (`$` goes to the global context - as far as I understood this) or prepare the variables before like:

````auto
$language := .Get 1;
{{ with .Get 0 }}
```{{- $language }}
<pre><code>{{ readFile . }}</code>
<span class="source-footer">{{.}}</span>
</pre>

````

{{ end }}

```auto

```

---

<div class="post-metadata">

### Author: ![rdwatters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rdwatters/32/2848_2.png) [@rdwatters](https://discourse.gohugo.io/u/rdwatters)
#### Post date: [March 4, 2017, 8:41pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/4 "2017-03-04T20:41:48Z")

</div>

Hmmm, I’ve been tweaking my own code block shortcode a lot lately, so these are some ideas:

First, you can set up a really [flexible shortcode with `.IsNamedParams`](https://gohugo.io/extras/shortcodes#single-flexible-example-vimeo-with-defaults) to allow for human error…or at least the combination of named and positional parameters. Once you start passing in more than 1 or 2 positional parameters, I find it’s easier to just called out the named parameter instead. But with the above link to flexible shortcodes, you’ll see you can do both.

> [@mvelosop](#):
>
> Or better yet, infer it from the file extension! 😉

I’m doing something similar to this, so the shortcode is called as follows:

```auto
{{% code file="/here/is/a/path/to/index.html" %}}
All my fancy code stuff"
{{% /code % }}

```

@bep’s suggestion of setting `repoRoot` in the front matter makes way more sense for your use case, but I’m using the full path to the file and writing it to the page for tutorial/instructive purposes, so it changes with each code block (again, not your use case).

Then in addition to your current shortcode templating, you can do this to grab the extension:

```auto
{{$ext := index (split (.Get "file") ".") 1 }}
=> ".html" in above example

```

Of course, this assumes you will only have a single dot `.` in the named parameter. If you want more complexity, look into [the `replaceRE` function](https://gohugo.io/templates/functions#replacere).

From `$ext` you can do the following in your shortcode template; e.g., `layouts/shortcodes/code.html`.

Keep in mind I use [client-side highlighting](https://highlightjs.org/) because I think it’s faster and more flexible:

```html
{{$ext := index (split (.Get "file") ".") 1 }}
<pre><code class="language-{{$ext}}">Whatever you want inside of this code block...</code></pre>

```

Then you can eliminate the need for…

> [@mvelosop](#):
>
> Being able to pass the language as a parameter to the shortcode to use it in the highlighting feature, something like so

HTH.

---

<div class="post-metadata">

### Author: ![mvelosop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mvelosop/32/2757_2.png) [@mvelosop](https://discourse.gohugo.io/u/mvelosop)
#### Post date: [March 5, 2017, 1:36pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/5 "2017-03-05T13:36:20Z")

</div>

Thank you guys, merging all your suggestions I could finally get just what I was expecting, you all rock!

Reading @bep solution I realized the repo root has to be in the front matter for each post, since each post should be scoped to one repo.

@rwi answer helped me understand a little more about the {{}}.

I never got to understand why the three backticks don’t work in the shortcode, even using {{% %}} from the post, but that was addressed in @rdwatters’s

And @rdwatters’s got me on the final result, including topping and everything else. I had to use findRE because I use “dots” a lot in the folder names.

So, with a bit of further reading, this is the final shortcode:

```
{{ $file := .Get 0 }}
{{ $repoFilePath := printf "%s/%s" $.Page.Params.repoName $file }}
{{ $repoFile := printf "repos/%s" $repoFilePath }}
{{ $fileExt := replace (index (findRE "(\\.)\\w+$" $file) 0) "." "" }}
<pre><code class="language-{{ $fileExt }}">{{ readFile $repoFile }}</code>
<span class="source-footer">{{ $repoFilePath }}</span>
</pre>

```

Thanks again.

---

<div class="post-metadata">

### Author: ![rdwatters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rdwatters/32/2848_2.png) [@rdwatters](https://discourse.gohugo.io/u/rdwatters)
#### Post date: [March 5, 2017, 5:02pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/6 "2017-03-05T17:02:33Z")

</div>

Glad it worked out. One thing I’m surprised by though is that `.Params.repoName’ is working for you without throwing an error. I believe you still need to call page params in all lowercase. Just throwing that out there in hopes it saves you from some head-scratch debugging next time 😄 Cheers.

---

<div class="post-metadata">

### Author: ![mvelosop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mvelosop/32/2757_2.png) [@mvelosop](https://discourse.gohugo.io/u/mvelosop)
#### Post date: [March 5, 2017, 7:35pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/7 "2017-03-05T19:35:20Z")

</div>

Yeah, I was surprised too, because I understood so in the docs, so I tried all combinations and it’s working as if it where case insensitive, at least as of 0.19.

Best.

---

<div class="post-metadata">

### Author: ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)
#### Post date: [August 3, 2017, 9:38pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/8 "2017-08-03T21:38:42Z")

</div>

The three backticks don’t work in the shortcode because the backticks are markdown and the shortcode is HTML.

---

<div class="post-metadata">

### Author: ![mvelosop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mvelosop/32/2757_2.png) [@mvelosop](https://discourse.gohugo.io/u/mvelosop)
#### Post date: [August 4, 2017, 4:00pm UTC](https://discourse.gohugo.io/t/solved-including-source-files-from-a-repo-in-hugo-posts/5683/9 "2017-08-04T16:00:01Z")

</div>

Thanks @gaetawoo!
