Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation into md-strings #180

Open
mauro3 opened this issue Dec 22, 2021 · 2 comments
Open

Interpolation into md-strings #180

mauro3 opened this issue Dec 22, 2021 · 2 comments

Comments

@mauro3
Copy link

mauro3 commented Dec 22, 2021

It would be cool, if interpolation into md-strings worked. For example:

result_of_some_calc = 3

md"""
The result of this tricky calculation is $result_of_some_calc
"""

currently produces via Literate.notebook("tmp.jl", execute=true, mdstrings=true)

image

I think it would be ok and best if interpolation only works with md-strings and not with general comments.

@nluetts
Copy link

nluetts commented Jan 4, 2022

I was also looking for such an option, similar to what Weave provides with inline bode blocks (https://weavejl.mpastell.com/stable/usage/#inline-code). What you can do now is to use a postprocessing function that replaces the interpolation syntax with the value. However, you cannot directly access the global variables of your script, so you have to store them somewhere and read them back in in the postprocessing functions. I did this with a temporary YAML file. It was clunky at best 😉 Would be great to have a better, build-in solution.

@jebej
Copy link

jebej commented Jun 15, 2022

I needed to include contents from a file in order to display the definition of a function, I am using the following preprocessing function:

function displaycode(str)
    regexp = r"# DISPLAYCODE\(\"(?<filename>.*\.jl)\",(?<startline>\d+),(?<endline>\d+)\)"
    repfun = (s) -> begin
        r = match(regexp, s)
        lines = readlines(r[:filename])[parse(Int,r[:startline]):parse(Int,r[:endline])]
        """
        # ```julia
        # $(join(lines, "\n# "))
        # ```
        """
    end
    replace(str, regexp=>repfun)
end

It replaces a line in a markdown chunk like this one (you must specify start and end line numbers to include):

# Here is the function definition:
# DISPLAYCODE("src/yourfile.jl",27,46)

@fredrikekre fredrikekre changed the title Interploation into md-strings Interpolation into md-strings Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants