From 9b125f53c880784357cb2e2ce3807ddfdb0b79de Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Fri, 4 Sep 2020 16:14:35 +0200 Subject: [PATCH] Added optional hashify_block_comments filter before parsing. Config is on by default. --- src/Literate.jl | 43 ++++++++++++++++++++++++++++++++++++++++++- test/runtests.jl | 8 ++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Literate.jl b/src/Literate.jl index 4d118d0..ec9de03 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -216,6 +216,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing) cfg["documenter"] = true cfg["credit"] = true cfg["keep_comments"] = false + cfg["hashify_block_comments"] = true cfg["execute"] = type === :md ? false : true cfg["codefence"] = get(user_config, "documenter", true) && !get(user_config, "execute", cfg["execute"]) ? ("```@example $(get(user_config, "name", cfg["name"]))" => "```") : ("```julia" => "```") @@ -298,6 +299,40 @@ See the manual section about [Configuration](@ref) for more information. """ const DEFAULT_CONFIGURATION=nothing # Dummy const for documentation + +# Turn block comments starting in the first column into "normal" hash comments +function hashify_block_comments(input) + lines_in = collect(eachline(IOBuffer(input))) + lines_out=IOBuffer() + line_number=0 + in_block_comment_region=false + for line in lines_in + line_number+=1 + if occursin(r"^#=", line) + if in_block_comment_region + error("line $(line_number): already in block comment region\n$(line)") + end + println(lines_out,replace(line,r"^#=" => "#")) + in_block_comment_region=true + elseif occursin(r"^=#", line) + if !in_block_comment_region + error("line $(line_number): not in block comment region\n$(line)") + end + println(lines_out,replace(line,r"^=#" => "#")) + in_block_comment_region=false + else + if in_block_comment_region + println(lines_out,"# "*line) + else + println(lines_out,line) + end + end + end + return String(take!(lines_out)) +end + + + function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) # Create configuration by merging default and userdefined config = create_configuration(inputfile; user_config=user_config, @@ -323,9 +358,15 @@ function preprocessor(inputfile, outputdir; user_config, user_kwargs, type) # read content content = read(inputfile, String) + # run custom pre-processing from user content = config["preprocess"](content) - + + # hashify block comments + if config["hashify_block_comments"] + content=hashify_block_comments(content) + end + # run some Documenter specific things for markdown output if type === :md && config["documenter"]::Bool # change the Edit on GitHub link diff --git a/test/runtests.jl b/test/runtests.jl index b054736..25263a6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -108,6 +108,9 @@ end ## Line 77 ## ## Line 79 + #= + Line 81 + =# """ expected_chunks = Chunk[ MDChunk(["" => "Line 1"]), @@ -145,8 +148,9 @@ end CodeChunk(["Line 64", " # Line 65", " Line 66", "Line 67"], false), CodeChunk(["# Line 73", "#", "# Line 75"], false), CodeChunk([" # Line 77", " #", " # Line 79"], false), - ] - parsed_chunks = Literate.parse(content) + MDChunk(["" => "Line 81"]), + ] + parsed_chunks = Literate.parse(Literate.hashify_block_comments(content)) compare_chunks(parsed_chunks, expected_chunks) # test leading/trailing whitespace removal