Class: Markdiff::Differ
- Inherits:
-
Object
- Object
- Markdiff::Differ
- Defined in:
- lib/markdiff/differ.rb
Instance Method Summary collapse
-
#apply_patch(operations, node) ⇒ Nokogiri::XML::Node
Apply a given patch to a given node.
-
#create_patch(before_node, after_node) ⇒ Array<Markdiff::Operations::Base>
Creates a patch from given two nodes.
-
#render(before_string, after_string) ⇒ Nokogiri::XML::Node
Utility method to do both creating and applying a patch.
Instance Method Details
#apply_patch(operations, node) ⇒ Nokogiri::XML::Node
Apply a given patch to a given node
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/markdiff/differ.rb', line 14 def apply_patch(operations, node) operations.each do |operation| case operation when ::Markdiff::Operations::AddChildOperation operation.target_node.add_child(operation.inserted_node) mark_li_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node) when ::Markdiff::Operations::AddDataBeforeHrefOperation operation.target_node["data-before-href"] = operation.target_node["href"] operation.target_node["href"] = operation.after_href mark_li_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node) when ::Markdiff::Operations::AddDataBeforeTagNameOperation operation.target_node["data-before-tag-name"] = operation.target_node.name operation.target_node.name = operation.after_tag_name mark_li_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node) when ::Markdiff::Operations::AddPreviousSiblingOperation operation.target_node.add_previous_sibling(operation.inserted_node) mark_li_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node.parent) when ::Markdiff::Operations::RemoveOperation operation.target_node.replace(operation.inserted_node) mark_li_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node) end end node end |
#create_patch(before_node, after_node) ⇒ Array<Markdiff::Operations::Base>
Creates a patch from given two nodes
48 49 50 51 52 53 54 |
# File 'lib/markdiff/differ.rb', line 48 def create_patch(before_node, after_node) if before_node.to_html == after_node.to_html [] else create_patch_from_children(before_node, after_node) end end |
#render(before_string, after_string) ⇒ Nokogiri::XML::Node
Utility method to do both creating and applying a patch
60 61 62 63 64 65 |
# File 'lib/markdiff/differ.rb', line 60 def render(before_string, after_string) before_node = ::Nokogiri::HTML.fragment(before_string) after_node = ::Nokogiri::HTML.fragment(after_string) patch = create_patch(before_node, after_node) apply_patch(patch, before_node) end |