# Literate to generate markdown programmatically

**URL:** <https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888>\
**Category:** General Usage\
**Tags:** markdown, literate-programming\
**Created:** [August 13, 2020, 4:40pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888 "2020-08-13T16:40:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)\
**Post date:** [August 13, 2020, 4:40pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/1 "2020-08-13T16:40:46Z")

</div>

I want to generate Markdown entries programmatically and I am using Literate.jl (maybe that is not the best tool for that, recommendations are also welcome).

This is a MWE of what I am trying to do.

```nohighlight
# # Test

import Markdown #src
function foo() #src
    for t in [1, 2] #src
        println(Markdown.parse("## test_$(t)")) #src
    end #src
end #src
foo(); #src

```

I would expect to see entries for the `## test_1` and `## test_2` in the resulting markdown but nothing appears. The only way to make it show is by not adding the `#src` blocks at the end but then the function is also printed in the markdown.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 13, 2020, 5:53pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/2 "2020-08-13T17:53:21Z")

</div>

I don’t quite understand what you are trying to do. Can you post the output you are looking for?

---

<div class="post-metadata">

**Author:** ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)\
**Post date:** [August 13, 2020, 6:02pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/3 "2020-08-13T18:02:33Z")

</div>

In this MWE all I should see is a markdown file with this entry.

```markdown
# Test

## test_1
## test_2

```

The idea is to have code that can write Markdown sections based on a template and inputs. The MWE template is super simple just to explain

---

<div class="post-metadata">

**Author:** ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)\
**Post date:** [August 13, 2020, 6:34pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/4 "2020-08-13T18:34:14Z")

</div>

This is another MWE of the Literate file I am trying to create. I am just adding the code to write the header as an example. I want to write a more complex Markdown text.

```nohighlight
# # Test

import Markdown #src
using PowerSystems #src
types = [ThermalStandard, RenewableGen] #src
for t in types #src
    print(stdout, "## Test_$(t)" #src
    ) #src
end; #src

```

I would expect or like to see something like this as the output

```markdown
# Test
## Test_ThermalStandard
## Test_RenewableGen

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 14, 2020, 10:17pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/5 "2020-08-14T22:17:14Z")

</div>

I see, so you want something like a “self-expanding” page. With [https://github.com/fredrikekre/Literate.jl/commit/e08ca0a19bd5e61dac778ddf4aaf6cef37532e48](https://github.com/fredrikekre/Literate.jl/commit/e08ca0a19bd5e61dac778ddf4aaf6cef37532e48) Literate can now capture `text/markdown`, and with [https://github.com/fredrikekre/Literate.jl/commit/6d1aec90b13c6ad888be0fdc77583e9c525b5dc1](https://github.com/fredrikekre/Literate.jl/commit/6d1aec90b13c6ad888be0fdc77583e9c525b5dc1) Literate can scrub the lines _after_ execution (instead of _before_ execution as is done with the `#src` token).

These features are available in version 2.6.0: [https://github.com/JuliaRegistries/General/pull/19552](https://github.com/JuliaRegistries/General/pull/19552). You can now do the following:

```julia
$ cat script.jl
# # Test

struct MD str end #hide
Base.show(io::IO, ::MIME"text/markdown", md::MD) = print(io, md.str) #hide
function f() #hide
    io = IOBuffer() #hide
    for i in 1:2 #hide
        println(io, "## test_$(i)") #hide
    end #hide
    return MD(String(take!(io))) #hide
end #hide
f() #hide

$ julia -e 'import Literate; Literate.markdown("script.jl"; documenter=false, execute=true)'
[ Info: generating markdown page from `/tmp/tmp.mLoi8uvI4A/script.jl`
[ Info: writing result to `/tmp/tmp.mLoi8uvI4A/script.md`

$ cat script.md 
# Test

## test_1
## test_2

```

---

<div class="post-metadata">

**Author:** ![jdlara-berkeley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdlara-berkeley/32/4096_2.png) [@jdlara-berkeley](https://discourse.julialang.org/u/jdlara-berkeley)\
**Post date:** [August 17, 2020, 7:44pm UTC](https://discourse.julialang.org/t/literate-to-generate-markdown-programmatically/44888/6 "2020-08-17T19:44:14Z")

</div>

@fredrikekre thanks, that worked
