Skip to content

Commit

Permalink
Merge pull request #42 from zombocom/schneems/refactor-before
Browse files Browse the repository at this point in the history
Refactor env[:before] to hold an array
  • Loading branch information
schneems authored May 3, 2024
2 parents 39b48b1 + a4d7269 commit bcc8b31
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
12 changes: 12 additions & 0 deletions lib/rundoc/code_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/rundoc/code_command/file_command/append.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/rundoc/code_command/file_command/remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/rundoc/code_command/write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions lib/rundoc/code_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/rundoc/code_commands/remove_contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions test/rundoc/code_section_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bcc8b31

Please sign in to comment.