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

Literate does not strip notebook references when source spans multiple lines #224

Closed
kbarros opened this issue Aug 29, 2023 · 2 comments · Fixed by #233
Closed

Literate does not strip notebook references when source spans multiple lines #224

kbarros opened this issue Aug 29, 2023 · 2 comments · Fixed by #233

Comments

@kbarros
Copy link
Contributor

kbarros commented Aug 29, 2023

For a notebook output, Literate will strip references for an input like this:

# Try this [Structure Factors with Classical Dynamics](@ref) tutorial.

but not this:

# This is the subject of our [Structure Factors with Classical
# Dynamics](@ref) tutorial.

The latter can appear when using a "line wrap" feature of an editor.

Perhaps this issue can be resolved if these regex's are allowed to span multiple lines: https://github.com/fredrikekre/Literate.jl/blob/master/src/Literate.jl#L212-L214

Specifically, can there be a trailing s on these three regex's?

        push!(repls, r"\[(.*?)\]\(@ref\)"s => s"\1")     # [foo](@ref) => foo
        push!(repls, r"\[(.*?)\]\(@ref .*?\)"s => s"\1") # [foo](@ref bar) => foo
        push!(repls, r"\[(.*?)\]\(@id .*?\)"s => s"\1")  # [foo](@id bar) => foo
@fredrikekre
Copy link
Owner

For regex 2 and 3 it make sense, I think. Do you want to submit a PR? You can probably find some tests for these replacements that you can augment too.

@kbarros
Copy link
Contributor Author

kbarros commented Jan 3, 2024

After some investigation, I would propose these three modified regexes:

repls = []
push!(repls, r"\[([^]]+?)\]\(@ref\)"s => s"\1")     # [foo](@ref) => foo
push!(repls, r"\[([^]]+?)\]\(@ref .*?\)"s => s"\1") # [foo](@ref bar) => foo
push!(repls, r"\[([^]]+?)\]\(@id .*?\)"s => s"\1")  # [foo](@id bar) => foo

In addition to including the trailing s (which allows the match to cross between lines, https://perldoc.perl.org/perlre#Modifiers) I have also replaced (.*?) with ([^]]+?), which excludes the character ] from the matched ID within brackets.

These have the following behavior:

content = """
    # # [Example](@id example-id)
    # [foo](@ref), [bar](@ref bbaarr)
    x = 1
    """
for repl in repls
    content = replace(content, repl)
end
@assert content == """
    # # Example
    # foo, bar
    x = 1
    """

content = """
    # # [Example](@id example-id)
    #
    # [This is a long example which
    # wraps a line](@ref)
    #
    # [In this case the](@ref reference also
    # wraps a line)
    #    
    # x = 1
    """
for repl in repls
    content = replace(content, repl)
end
@assert content == """
    # # Example
    #
    # This is a long example which
    # wraps a line
    #
    # In this case the
    #    
    # x = 1
    """

@kbarros kbarros changed the title Literate does note strip notebook references when source spans multiple lines Literate does not strip notebook references when source spans multiple lines Jan 3, 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

Successfully merging a pull request may close this issue.

2 participants