Class: Rblines::Redlines
- Inherits:
-
Object
- Object
- Rblines::Redlines
- Defined in:
- lib/rblines/redlines.rb
Overview
The Redlines class is used to compare two texts and generate a markdown output highlighting the differences between them.
Constant Summary collapse
- MD_STYLES =
{ "none" => {"ins" => %w[ins ins], "del" => %w[del del]}, "red" => { "ins" => ['span style="color:red;font-weight:700;"', "span"], "del" => ['span style="color:red;font-weight:700;text-decoration:line-through;"', "span"] } }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
-
#test ⇒ Object
Returns the value of attribute test.
Instance Method Summary collapse
- #compare(test = nil, output = "markdown", options = {}) ⇒ Object
- #handle_add_action(result, group, md_styles) ⇒ Object
- #handle_delete_action(result, group, md_styles) ⇒ Object
- #handle_equal_action(result, group) ⇒ Object
- #handle_replace_action(result, group, md_styles) ⇒ Object
-
#initialize(source, test = nil, **options) ⇒ Redlines
constructor
A new instance of Redlines.
- #opcodes ⇒ Object
- #output_markdown ⇒ Object
Constructor Details
#initialize(source, test = nil, **options) ⇒ Redlines
Returns a new instance of Redlines.
86 87 88 89 90 |
# File 'lib/rblines/redlines.rb', line 86 def initialize(source, test = nil, **) self.source = source self. = self.test = test if test end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
73 74 75 |
# File 'lib/rblines/redlines.rb', line 73 def @options end |
#source ⇒ Object
Returns the value of attribute source.
74 75 76 |
# File 'lib/rblines/redlines.rb', line 74 def source @source end |
#test ⇒ Object
Returns the value of attribute test.
74 75 76 |
# File 'lib/rblines/redlines.rb', line 74 def test @test end |
Instance Method Details
#compare(test = nil, output = "markdown", options = {}) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rblines/redlines.rb', line 141 def compare(test = nil, output = "markdown", = {}) self.test = test if test raise "No test string was provided when the function was called, or during initialisation." if self.test.nil? self..merge!() return unless output == "markdown" output_markdown end |
#handle_add_action(result, group, md_styles) ⇒ Object
124 125 126 127 128 |
# File 'lib/rblines/redlines.rb', line 124 def handle_add_action(result, group, md_styles) temp_str = group.map(&:new_element).join.split("¶ ") temp_str.each { |split| result.push("<#{md_styles["ins"][0]}>#{split}</#{md_styles["ins"][1]}>", "\n\n") } result.pop if temp_str.length.positive? end |
#handle_delete_action(result, group, md_styles) ⇒ Object
130 131 132 |
# File 'lib/rblines/redlines.rb', line 130 def handle_delete_action(result, group, md_styles) result.push("<#{md_styles["del"][0]}>#{group.map(&:old_element).join}</#{md_styles["del"][1]}>") end |
#handle_equal_action(result, group) ⇒ Object
120 121 122 |
# File 'lib/rblines/redlines.rb', line 120 def handle_equal_action(result, group) result.push(group.map(&:old_element).join.gsub("¶ ", "\n\n")) end |
#handle_replace_action(result, group, md_styles) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/rblines/redlines.rb', line 134 def handle_replace_action(result, group, md_styles) result.push("<#{md_styles["del"][0]}>#{group.map(&:old_element).join}</#{md_styles["del"][1]}>") temp_str = group.map(&:new_element).join.split("¶ ") temp_str.each { |split| result.push("<#{md_styles["ins"][0]}>#{split}</#{md_styles["ins"][1]}>", "\n\n") } result.pop if temp_str.length.positive? end |
#opcodes ⇒ Object
92 93 94 95 96 |
# File 'lib/rblines/redlines.rb', line 92 def opcodes raise "No test string was provided when the function was called, or during initialisation." if @seq2.nil? Diff::LCS.sdiff(@seq1, @seq2) end |
#output_markdown ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rblines/redlines.rb', line 98 def output_markdown result = [] style = MD_STYLES[[:markdown_style] || "red"] grouped_opcodes = opcodes.chunk_while { |a, b| a.action == b.action }.to_a grouped_opcodes.each do |group| group_action = group[0].action case group_action when "=" handle_equal_action(result, group) when "+" handle_add_action(result, group, style) when "-" handle_delete_action(result, group, style) when "!" handle_replace_action(result, group, style) end end result.join end |