diff --git a/lib/rundoc/code_command.rb b/lib/rundoc/code_command.rb index 4a9e38a..4b1ce03 100644 --- a/lib/rundoc/code_command.rb +++ b/lib/rundoc/code_command.rb @@ -2,6 +2,18 @@ module Rundoc # Generic CodeCommand class to be inherited # class CodeCommand + + # Newlines are stripped and re-added, this tells the project that + # we're intentionally wanting an extra newline + NEWLINE = Object.new + def NEWLINE.to_s + "" + end + + def NEWLINE.empty? + false + end + attr_accessor :render_result, :render_command, :command, :contents, :keyword, :original_args diff --git a/lib/rundoc/code_command/file_command/append.rb b/lib/rundoc/code_command/file_command/append.rb index 59b68ed..8a4a88d 100644 --- a/lib/rundoc/code_command/file_command/append.rb +++ b/lib/rundoc/code_command/file_command/append.rb @@ -13,12 +13,13 @@ def to_md(env) return unless render_command? raise "must call write in its own code section" unless env[:commands].empty? - before = env[:before] - env[:before] = if @line_number - "In file `#{filename}`, on line #{@line_number} add:\n\n#{before}" + + env[:before] << if @line_number + "In file `#{filename}`, on line #{@line_number} add:" else - "At the end of `#{filename}` add:\n\n#{before}" + "At the end of `#{filename}` add:" end + env[:before] << NEWLINE nil end diff --git a/lib/rundoc/code_command/file_command/remove.rb b/lib/rundoc/code_command/file_command/remove.rb index c0fa113..3e9db58 100644 --- a/lib/rundoc/code_command/file_command/remove.rb +++ b/lib/rundoc/code_command/file_command/remove.rb @@ -8,8 +8,8 @@ def initialize(filename) def to_md(env) raise "must call write in its own code section" unless env[:commands].empty? - before = env[:before] - env[:before] = "In file `#{filename}` remove:\n\n#{before}" + env[:before] << "In file `#{filename}` remove:" + env[:before] << NEWLINE nil end diff --git a/lib/rundoc/code_command/write.rb b/lib/rundoc/code_command/write.rb index 8e49ca0..82905d6 100644 --- a/lib/rundoc/code_command/write.rb +++ b/lib/rundoc/code_command/write.rb @@ -24,8 +24,8 @@ def initialize(filename) def to_md(env) raise "must call write in its own code section" unless env[:commands].empty? - before = env[:before] - env[:before] = "In file `#{filename}` write:\n\n#{before}" + env[:before] << "In file `#{filename}` write:" + env[:before] << NEWLINE nil end diff --git a/lib/rundoc/code_section.rb b/lib/rundoc/code_section.rb index e880089..0877934 100644 --- a/lib/rundoc/code_section.rb +++ b/lib/rundoc/code_section.rb @@ -44,8 +44,10 @@ def render result = [] env = {} env[:commands] = [] - env[:before] = +"#{fence}#{lang}" - env[:after] = +"#{fence}#{AUTOGEN_WARNING}" + env[:fence_start] = +"#{fence}#{lang}" + env[:fence_end] = "#{fence}#{AUTOGEN_WARNING}" + env[:before] = [] + env[:after] = [] env[:document_path] = @document_path @stack.each do |s| @@ -71,11 +73,12 @@ def render return "" if hidden? - array = [env[:before], result, env[:after]] + array = [env[:before], env[:fence_start], result, env[:fence_end], env[:after]] array.flatten! array.compact! - array.map!(&:rstrip) + array.map! {|s| s.respond_to?(:rstrip) ? s.rstrip : s } array.reject!(&:empty?) + array.map!(&:to_s) array.join("\n") << "\n" end diff --git a/test/rundoc/code_commands/remove_contents_test.rb b/test/rundoc/code_commands/remove_contents_test.rb index f17ae58..c4fe206 100644 --- a/test/rundoc/code_commands/remove_contents_test.rb +++ b/test/rundoc/code_commands/remove_contents_test.rb @@ -29,10 +29,11 @@ def test_appends_to_a_file env = {} env[:commands] = [] - env[:before] = "```ruby" + env[:before] = [] + env[:fence_start] = "```ruby" cc.to_md(env) - assert_equal "In file `foo.rb` remove:\n\n```ruby", env[:before] + assert_equal ["In file `foo.rb` remove:", Rundoc::CodeCommand::NEWLINE], env[:before] end end end diff --git a/test/rundoc/code_section_test.rb b/test/rundoc/code_section_test.rb index f6720cb..38b4751 100644 --- a/test/rundoc/code_section_test.rb +++ b/test/rundoc/code_section_test.rb @@ -1,8 +1,6 @@ require "test_helper" class CodeSectionTest < Minitest::Test - def setup - end def test_does_not_render_if_all_contents_hidden contents = <<~RUBY