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
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 43 44 45 46 47 48 49 50 |
# File 'lib/markdiff/differ.rb', line 16 def apply_patch(operations, node) i = 0 operations.sort_by {|operation| i += 1; [-operation.priority, i] }.each do |operation| case operation when ::Markdiff::Operations::AddChildOperation operation.target_node.add_child(operation.inserted_node) mark_li_or_tr_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_or_tr_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_or_tr_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_or_tr_as_changed(operation.target_node) if operation.target_node.name != "li" && operation.target_node.name != "tr" mark_top_level_node_as_changed(operation.target_node.parent) when ::Markdiff::Operations::RemoveOperation operation.target_node.replace(operation.inserted_node) if operation.target_node != operation.inserted_node mark_li_or_tr_as_changed(operation.target_node) mark_top_level_node_as_changed(operation.target_node) when ::Markdiff::Operations::TextDiffOperation parent = operation.target_node.parent operation.target_node.replace(operation.inserted_node) mark_li_or_tr_as_changed(parent) mark_top_level_node_as_changed(parent) end end node end |
#create_patch(before_node, after_node) ⇒ Array<Markdiff::Operations::Base>
Creates a patch from given two nodes
56 57 58 59 60 61 62 |
# File 'lib/markdiff/differ.rb', line 56 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
68 69 70 71 72 73 |
# File 'lib/markdiff/differ.rb', line 68 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 |