Literate to generate markdown programmatically

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.

# # 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.

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

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

# 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

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.

# # 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

# Test
## Test_ThermalStandard
## Test_RenewableGen

I see, so you want something like a “self-expanding” page. With https://github.com/fredrikekre/Literate.jl/commit/e08ca0a19bd5e61dac778ddf4aaf6cef37532e48 Literate can now capture text/markdown, and with 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. You can now do the following:

$ 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
1 Like

@fredrikekre thanks, that worked