From e927495a62f2ff9be2571ebbd9645f19c15f8d6d Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Sat, 3 Oct 2020 04:21:19 +0900 Subject: [PATCH] fixes #400 --- .github/workflows/ci.yml | 2 +- src/rendering/common.jl | 14 +++++--------- src/rendering/htmlformats.jl | 12 ++++++------ src/rendering/miscformats.jl | 10 +++++----- src/rendering/pandocformats.jl | 4 ++-- src/run.jl | 9 +++------ 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 941d115..0a6d998 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false # don't stop CI even when one of them fails matrix: version: - - '1.4' + - '1.5' os: - ubuntu-latest - macOS-latest diff --git a/src/rendering/common.jl b/src/rendering/common.jl index 8f39b03..b877809 100644 --- a/src/rendering/common.jl +++ b/src/rendering/common.jl @@ -44,18 +44,14 @@ function render_chunk(docformat::WeaveFormat, chunk::CodeChunk) chunk.content = render_code(docformat, chunk.content) - if !chunk.options[:eval] - return if chunk.options[:echo] - string(docformat.codestart, '\n', chunk.content, docformat.codeend) - else - "" - end - end + echo = chunk.options[:echo] + + chunk.options[:eval] || return echo ? string(docformat.codestart, chunk.content, docformat.codeend) : "" if chunk.options[:term] result = render_termchunk(docformat, chunk) else - result = if chunk.options[:echo] + result = if echo # Convert to output format and highlight (html, tex...) if needed string(docformat.codestart, chunk.content, docformat.codeend, '\n') else @@ -126,7 +122,7 @@ render_output(docformat::WeaveFormat, output) = output function render_termchunk(docformat::WeaveFormat, chunk) return if should_render(chunk) - string(docformat.termstart, chunk.output, '\n', docformat.termend, '\n') + string(docformat.termstart, chunk.output, docformat.termend) else "" end diff --git a/src/rendering/htmlformats.jl b/src/rendering/htmlformats.jl index 949e94a..d7684f0 100644 --- a/src/rendering/htmlformats.jl +++ b/src/rendering/htmlformats.jl @@ -15,8 +15,8 @@ render_termchunk(docformat::HTMLFormat, chunk) = Base.@kwdef mutable struct WeaveHTML <: HTMLFormat description = "Weave-style HTML" extension = "html" - codestart = "\n" - codeend = "\n" + codestart = '\n' + codeend = '\n' termstart = codestart termend = codeend outputstart = "
"
@@ -127,12 +127,12 @@ end
 Base.@kwdef mutable struct Pandoc2HTML <: HTMLFormat
     description = "HTML via intermediate Pandoc Markdown (requires Pandoc 2)"
     extension = "md"
-    codestart = "\n"
-    codeend = "\n"
+    codestart = '\n'
+    codeend = '\n'
     termstart = codestart
     termend = codeend
-    outputstart = "\n"
-    outputend = "\n"
+    outputstart = '\n'
+    outputend = '\n'
     mimetypes = ["image/png", "image/svg+xml", "image/jpg", "text/html", "text/markdown", "text/plain"]
     fig_ext = ".png"
     out_width = nothing
diff --git a/src/rendering/miscformats.jl b/src/rendering/miscformats.jl
index ecc7e7b..6a19365 100644
--- a/src/rendering/miscformats.jl
+++ b/src/rendering/miscformats.jl
@@ -5,7 +5,7 @@ Base.@kwdef mutable struct GitHubMarkdown <: WeaveFormat
     description = "GitHub Markdown"
     extension = "md"
     codestart = "```julia"
-    codeend = "```\n\n"
+    codeend = "```\n"
     termstart = codestart
     termend = codeend
     outputstart = "```"
@@ -51,7 +51,7 @@ Base.@kwdef mutable struct Hugo <: WeaveFormat
     description = "Hugo Markdown (using shortcodes)"
     extension = "md"
     codestart = "```julia"
-    codeend = "```\n\n"
+    codeend = "```\n"
     termstart = codestart
     termend = codeend
     outputstart = "```"
@@ -89,7 +89,7 @@ Base.@kwdef mutable struct MultiMarkdown <: WeaveFormat
     description = "MultiMarkdown"
     extension = "md"
     codestart = "```julia"
-    codeend = "```\n\n"
+    codeend = "```\n"
     termstart = codestart
     termend = codeend
     outputstart = "```"
@@ -143,7 +143,7 @@ Base.@kwdef mutable struct Rest <: WeaveFormat
     description = "reStructuredText and Sphinx"
     extension = "rst"
     codestart = ".. code-block:: julia\n"
-    codeend = "\n\n"
+    codeend = "\n"
     termstart = codestart
     termend = codeend
     outputstart = "::\n"
@@ -190,7 +190,7 @@ Base.@kwdef mutable struct AsciiDoc <: WeaveFormat
     description = "AsciiDoc"
     extension = "txt"
     codestart = "[source,julia]\n--------------------------------------"
-    codeend = "--------------------------------------\n\n"
+    codeend = "--------------------------------------\n"
     termstart = codestart
     termend = codeend
     outputstart = "--------------------------------------"
diff --git a/src/rendering/pandocformats.jl b/src/rendering/pandocformats.jl
index 0c18401..30364fe 100644
--- a/src/rendering/pandocformats.jl
+++ b/src/rendering/pandocformats.jl
@@ -37,7 +37,7 @@ Base.@kwdef mutable struct Pandoc <: PandocFormat
     description = "Pandoc Markdown"
     extension = "md"
     codestart = "~~~~{.julia}"
-    codeend = "~~~~~~~~~~~~~\n\n"
+    codeend = "~~~~~~~~~~~~~\n"
     termstart = codestart
     termend = codeend
     outputstart = "~~~~"
@@ -60,7 +60,7 @@ Base.@kwdef mutable struct Pandoc2PDF <: PandocFormat
     description = "PDF via intermediate Pandoc Markdown"
     extension = "md"
     codestart = "~~~~{.julia}"
-    codeend = "~~~~~~~~~~~~~\n\n"
+    codeend = "~~~~~~~~~~~~~\n"
     termstart = codestart
     termend = codeend
     outputstart = "~~~~"
diff --git a/src/run.jl b/src/run.jl
index 279e279..6dcc044 100644
--- a/src/run.jl
+++ b/src/run.jl
@@ -353,11 +353,9 @@ function collect_results(chunk::CodeChunk)
     content = ""
     result_chunks = CodeChunk[]
     for r in chunk.result
+        content *= r.code
         # Check if there is any output from chunk
-        if strip(r.stdout) == "" && isempty(r.figures) && strip(r.rich_output) == ""
-            content *= r.code
-        else
-            content = "\n" * content * r.code
+        if any(!isempty ∘ strip, (r.stdout, r.rich_output)) || !isempty(r.figures)
             rchunk = CodeChunk(
                 content,
                 chunk.number,
@@ -365,15 +363,14 @@ function collect_results(chunk::CodeChunk)
                 chunk.optionstring,
                 copy(chunk.options),
             )
-            content = ""
             rchunk.figures = r.figures
             rchunk.output = r.stdout
             rchunk.rich_output = r.rich_output
             push!(result_chunks, rchunk)
+            content = ""
         end
     end
     if !isempty(content)
-        startswith(content, "\n") || (content = "\n" * content)
         rchunk = CodeChunk(
             content,
             chunk.number,