# Easiest way to embed code from source files

**URL:** <https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662>\
**Category:** support\
**Created:** [January 19, 2022, 11:11pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662 "2022-01-19T23:11:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![katexochen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/katexochen/32/14493_2.png) [@katexochen](https://discourse.gohugo.io/u/katexochen)\
**Post date:** [January 19, 2022, 11:11pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/1 "2022-01-19T23:11:03Z")

</div>

I want to automatically embed code from `.go` files highlighted in my post (markdown or html, doesn’t matter). What is the easiest way to do so with Hugo?

Here are some more details of what I’m planning:

- I want to have a folder of source code files, like `example1.go`.
- I want to store metadata to these files in a JSON file, like  
`{ "name": "example1", "author": "me", "output": "hello world" }`.  
It doesn’t matter if its one big JSON file or multiple.
- I want to create a post that contains the metadata and the code. One post will always have exactly one code example and one example is only embedded into one post.

I think I know how to read the metadata and include it into the post, but how could I include the code?

Thanks for helping!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 20, 2022, 12:46am UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/2 "2022-01-20T00:46:22Z")

</div>

Please clarify:

1. Will one post ever display multiple examples?
2. Will one example ever be displayed on multiple posts?
3. Does the metadata file contain information for all examples, or will there be a unique metadata file for each example, or for each page of example(s)?

---

<div class="post-metadata">

**Author:** ![katexochen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/katexochen/32/14493_2.png) [@katexochen](https://discourse.gohugo.io/u/katexochen)\
**Post date:** [January 20, 2022, 7:13am UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/4 "2022-01-20T07:13:30Z")

</div>

I’ve added the information you asked for to my question.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 21, 2022, 6:40pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/5 "2022-01-21T18:40:54Z")

</div>

Here’s an example:

```bash
git clone --single-branch -b hugo-forum-topic-36662 https://github.com/jmooring/hugo-testing hugo-forum-topic-36662
cd hugo-forum-topic-36662
hugo server

```

structure

```nohighlight
content/
└── examples
    ├── example-1.md
    ├── example-2.md
    └── example-3.md
data/
└── examples.json
assets/
└── examples
    ├── example-1.go
    ├── example-2.py
    └── example-3.js
layouts/
├── _default
│ ├── baseof.html
│ ├── home.html
│ ├── list.html
│ └── single.html
└── examples
    └── single.html

```

And this is the code in layouts/examples/single.html that ties it all together

```auto
{{ $key := path.Base .TranslationKey }}

{{ with resources.GetMatch (printf "examples/%s.*" $key) }}
  <h2>Code</h2>
  {{ $lang := path.Ext .Name | strings.TrimLeft "." }}
  {{ $options := slice "lineNos=table" "style=native" }}
  {{ transform.Highlight .Content $lang (delimit $options ",") }}
{{ end }}

{{ with index site.Data.examples $key }}
  <h2>Data</h2>
  Author: {{ .author }}<br>
  Output: {{ .output }}<br>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [January 21, 2022, 6:53pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/6 "2022-01-21T18:53:12Z")

</div>

> [@jmooring](#):
>
> `hugo-forum-topic-36662`
> 
> ```auto
> {{ $key := path.Base .TranslationKey }}
> 
> ```

I had not seen `.TranslationKey` being used this way. Is there a specific reason to use that? I would have just used `{{ $key := .File.BaseFileName }}`. So I am curious to know if using `.TranslationKey` is just a preference or it does something better.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [January 21, 2022, 6:55pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/7 "2022-01-21T18:55:04Z")

</div>

I’m trying to think forward, when pages aren’t necessarily backed by files. Might as well get into the habit…

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [January 23, 2022, 6:55pm UTC](https://discourse.gohugo.io/t/easiest-way-to-embed-code-from-source-files/36662/8 "2022-01-23T18:55:25Z")

</div>

Closed on January 23, 2022, 6:55pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
