fredrikekre / Literate.jl

Simple package for literate programming in Julia
https://fredrikekre.github.io/Literate.jl
Other
536 stars 62 forks source link

Hide code lines with `#hide` when executing Markdown #188

Closed devmotion closed 2 years ago

devmotion commented 2 years ago

This PR fixes a surprising behaviour that seemed a bug to me, both intuitively and after reading the documentation. Feel free to close the PR if it is intended and there is another way to achieve the same behaviour :slightly_smiling_face:

For different reasons, I would like to execute a Documenter-flavored Markdown file generated with Literate with Literate instead of Documenter. However, as with Documenter, I would like to suppress the output of lines that end with #hide in the final file. To my surprise, currently this does not work:

julia> using Literate

julia> write("literate_script.jl",
       """
       a = 2 + 2
       print("a: ", a); nothing #hide
       """);

julia> Literate.markdown("literate_script.jl"; execute=true, credit=false);
[ Info: generating markdown page from ~/literate_script.jl
[ Info: writing result to ~/literate_script.md

julia> print(read("literate_script.md", String))
```@meta
EditURL = "<unknown>/literate_script.jl"
a = 2 + 2
print("a: ", a); nothing #hide
a: 4
Including this file in a Documenter setup doesn't help either since Documenter does not remove lines in `julia` codeblocks (only in `@example` AFAIK).

With this PR the line is removed from the resulting Markdown file but, of course, still executed. Hence including it in my documentation yields the same output as if I would have executed the Markdown file with Documenter instead of Literate:
`````julia
julia> using Literate

julia> write("literate_script.jl",
       """
       a = 2 + 2
       print("a: ", a); nothing #hide
       """);

julia> Literate.markdown("literate_script.jl"; execute=true, credit=false);
[ Info: generating markdown page from ~/literate_script.jl
[ Info: writing result to ~/literate_script.md

julia> print(read("literate_script.md", String))
```@meta
EditURL = "<unknown>/literate_script.jl"
a = 2 + 2
a: 4


@racinmat noticed this inconsistency in https://github.com/fredrikekre/Literate.jl/issues/166#issuecomment-979987878 as well. I checked that the example there is fixed as well, with this PR the code block is shown as
![image](https://user-images.githubusercontent.com/26102/153487397-301b156c-ab75-4336-82f0-2455acd51d89.png)
devmotion commented 2 years ago

What's your opinion @fredrikekre? Do you think the PR is a good idea?

fredrikekre commented 2 years ago

Yea this seems fine, it is a bit difficult to understand all the combinations of options at this point, sadly.

Can you add a note in the CHANGELOG.md?

fredrikekre commented 2 years ago

Thanks!