"Literate" leaves behind extraneous #

I use “cells” in a Julia script. VS code can execute these cells with a single keystroke.

The problem is that when I generate the markdown from the script with Literate, the cell markers (##) are interpreted as markdown and their remnants are left behind.

image
As you can see it is interpreted as a comment within code.

Has anyone figured out what to do with these cell markers in markdown?

Is this something specific to VS Code, or does it happen in other environments?

Do you have an MWE?

Depending on this, I would consider opening an issue for the VS Code plugin, or Literate.jl.

If I understand correctly this should be an issue with Literate - VSCode only comes into play as it defines the syntax for cell delimiters (##) which isn’t understood by Literate.

Tangentially related - how do you get cells in VSCode? Just putting in ## New cell doesn’t seem to do anything for me, and I can’t see anything in the settings on this?

Next release of the VSCode extension will also support # %% as cell delimiters, so that should be a workaround, right?

I’m afraid that will not solve the problem. Literate will process it as

image

So, as you can see, this text is again interpreted as Julia code.

Here is a sample:

##
# ## Cross-section

# Cross-sectional properties are incorporated in the cross-section property.
# There are several rectangular cross-sections in the model: the fuselage, the
# wing, the tail. There are also three massless connectors: connections between
# the fuselage and the wing, between the wing structure and the viscoelastic
# damping layer, and between the fuselage and the tail.

using FinEtoolsFlexBeams.CrossSectionModule: CrossSectionRectangle
# Body of the frame (fuselage).
cs_body = CrossSectionRectangle(s -> 1.5*L, s -> L/2, s -> [1.0, 0.0, 1.0]; label = 1)

This cell can be evaluated with Shift+Enter.

No, this is specific to VS code. Other editors, as far as I know, do not handle “cell evaluation”.

Shoot! I figured out that placing the “cell” marker as #!md ## would filter out that extra #.
Unfortunately, this is no longer recognized as a cell marker by VS code.

Literate.jl accepts a preprocess function, you can remove the ## lines with the following:

function preprocess(content)
   return replace(content, r"^##$."ms => "")
end

This matches lines that are exactly ##, including the trailing newline (. inside the regexp and s postfix option combined with m, see perlre - Perl regular expressions - Perldoc Browser) and removes it.

Pass it to Literate by calling e.g. Literate.markdown(script, outputdir; preprocess=preprocess, kwargs...).

6 Likes