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

Proposal: Alternate Julia comment syntax #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/fileformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The basic syntax is simple:
Leading whitespace is allowed before `#`, but it will be removed when generating the
output. Since `#`-lines are treated as markdown we can not use that for regular julia
comments, for this you can instead use `## `, which will render as `# ` in the output.
Alternatively, you can use `#~` for regular julia comments, which will render as `#`
and behaves well with code cell delimiters in VSCode.

Lets look at a simple example:
```julia
Expand Down
2 changes: 2 additions & 0 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function parse(content; allow_continued = true)
end
# remove "## " and "##\n"
line = replace(replace(line, r"^(\h*)#(# .*)$" => s"\1\2"), r"^(\h*#)#$" => s"\1")
# similarly, change "#~x" to "#x" and remove "#~\n"
line = replace(replace(line, r"^(\h*)#~(.*)$" => s"\1#\2"), r"^(\h*#)~$" => s"\1")
push!(chunks[end].lines, line)
end
end
Expand Down
20 changes: 19 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ end
## Line 77
##
## Line 79
#-
#~ Line 81
#~
#~ Line 83
#-
#~ Line 85
#~
#~ Line 87
"""
expected_chunks = Chunk[
MDChunk(["" => "Line 1"]),
Expand Down Expand Up @@ -145,6 +153,8 @@ end
CodeChunk(["Line 64", " # Line 65", " Line 66", "Line 67"], false),
CodeChunk(["# Line 73", "#", "# Line 75"], false),
CodeChunk([" # Line 77", " #", " # Line 79"], false),
CodeChunk(["# Line 81", "#", "# Line 83"], false),
CodeChunk([" # Line 85", " #", " # Line 87"], false),
]
parsed_chunks = Literate.parse(content)
compare_chunks(parsed_chunks, expected_chunks)
Expand Down Expand Up @@ -201,6 +211,8 @@ content = """
Source code only #src
## # Comment
## another comment
#~ yet another comment and ...
#~... an ugly comment
#-
for i in 1:10
print(i)
Expand Down Expand Up @@ -315,6 +327,8 @@ const GITLAB_ENV = Dict(
x + 3
# # Comment
# another comment
# yet another comment and ...
#... an ugly comment

for i in 1:10
print(i)
Expand Down Expand Up @@ -547,6 +561,8 @@ end end
x * 3
# # Comment
# another comment
# yet another comment and ...
#... an ugly comment
````

````@example inputfile; continued = true
Expand Down Expand Up @@ -899,7 +915,9 @@ end end
"x * 3\\n",
"x * 3\\n",
"# # Comment\\n",
"# another comment"
"# another comment\\n",
"# yet another comment and ...\\n",
"#... an ugly comment"
],
""",

Expand Down