# Extracting code excerpts automatically

**URL:** <https://discourse.julialang.org/t/extracting-code-excerpts-automatically/16313>\
**Category:** General Usage\
**Tags:** literate-programming, weave\
**Created:** [October 14, 2018, 4:54pm UTC](https://discourse.julialang.org/t/extracting-code-excerpts-automatically/16313 "2018-10-14T16:54:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)\
**Post date:** [October 14, 2018, 4:54pm UTC](https://discourse.julialang.org/t/extracting-code-excerpts-automatically/16313/1 "2018-10-14T16:54:21Z")

</div>

I have a self-referential problem: I code programs for a book and like to keep nice and long jupyter notebooks for teaching and developing. From these I would like to automatically extract all files needed as listings for the book. I have created a version that works:

```julia
fname = "Export"

function export_code_segments(fname)
    run(`jupyter nbconvert --to script $(fname).ipynb`);

    open(fname*".jl") do file_in
        file_content = read(file_in, String)
        split_contents = split(file_content,"###EXPORT CODE")

        for code_segment in split_contents 
            code_listing = split(code_segment,"FROM HERE ###")
            if (code_listing[1][1:9] == " TO FILE ")
                segment_name = strip(code_listing[1][9:end]);
                segment_content = code_listing[2];
                open(segment_name, "w") do file_out
                    write(file_out, strip(segment_content));
                end
            end
        end
    end
end

###EXPORT CODE TO FILE myfun.jl FROM HERE ### 
for t=1:10
    a = a + t;
end
println(a)
###EXPORT CODE UNTIL HERE ###

export_code_segments(fname)
open("myfun.jl") do file
    for i in enumerate(eachline(file)) #for ln in eachline(ss)
        println("$(i[1]): $(i[2])")
    end
end

```

BUT is there a “proper way” to do this? I am sure more people thought about such problems, right?

Thanks!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 14, 2018, 4:58pm UTC](https://discourse.julialang.org/t/extracting-code-excerpts-automatically/16313/2 "2018-10-14T16:58:15Z")

</div>

> **[GitHub - fredrikekre/Literate.jl: Simple package for literate programming in...](https://github.com/fredrikekre/Literate.jl)**
>
> Simple package for literate programming in Julia. Contribute to fredrikekre/Literate.jl development by creating an account on GitHub.

> [@ANN: Literate.jl](https://discourse.julialang.org/t/ann-literate-jl/10651):
>
> Hello there, I am happy to present [Literate.jl](https://github.com/fredrikekre/Literate.jl), a lightweight package for [literate programming](https://en.wikipedia.org/wiki/Literate_programming). The main purpose is to facilitate writing Julia examples/tutorials that can be included in your package documentation. I believe that one of the best way to showcase a package is to present well documented examples. However, people are different, and we all prefer different ways of trying out a new package. Some people want to RTFM, others want to explore the package interactively in a notebook, and…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 14, 2018, 5:12pm UTC](https://discourse.julialang.org/t/extracting-code-excerpts-automatically/16313/3 "2018-10-14T17:12:08Z")

</div>

Similar to Literate.jl there is also [Weave.jl](https://github.com/mpastell/Weave.jl)  
In particular [convert\_doc](http://weavejl.mpastell.com/stable/notebooks/#Weave.convert_doc-Tuple%7BString,String%7D)
