Refactor and use first node start + quote + last node end for the context
This commit is contained in:
@@ -25,36 +25,23 @@ class Legislation::Annotation < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def store_context
|
def store_context
|
||||||
html = draft_version.body_html
|
begin
|
||||||
doc = Nokogiri::HTML(html)
|
html = draft_version.body_html
|
||||||
|
doc = Nokogiri::HTML(html)
|
||||||
|
|
||||||
selector_start = "/html/body/#{range_start}"
|
selector_start = "/html/body/#{range_start}"
|
||||||
el_start = doc.at_xpath(selector_start)
|
el_start = doc.at_xpath(selector_start)
|
||||||
|
|
||||||
selector_end = "/html/body/#{range_end}"
|
selector_end = "/html/body/#{range_end}"
|
||||||
el_end = doc.at_xpath(selector_end)
|
el_end = doc.at_xpath(selector_end)
|
||||||
|
|
||||||
context_text = ""
|
remainder_el_start = el_start.text[0 .. range_start_offset-1]
|
||||||
|
remainder_el_end = el_end.text[range_end_offset .. -1]
|
||||||
|
|
||||||
if el_start.path == el_end.path
|
self.context = "#{remainder_el_start}<span class=annotator-hl>#{quote}</span>#{remainder_el_end}"
|
||||||
context_text = el_start.text
|
rescue
|
||||||
quote_range = range_start_offset .. range_end_offset-1
|
"<span class=annotator-hl>#{quote}</span>"
|
||||||
else
|
|
||||||
i = el_start
|
|
||||||
|
|
||||||
while i.path != el_end.path
|
|
||||||
context_text << i.text
|
|
||||||
i = i.next_element
|
|
||||||
end
|
|
||||||
|
|
||||||
context_text << el_end.text
|
|
||||||
|
|
||||||
end_of_range = (el_end.text.size - range_end_offset) * -1
|
|
||||||
quote_range = range_start_offset .. end_of_range-1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context_text[quote_range] = "<span class=annotator-hl>#{quote}</span>"
|
|
||||||
self.context = context_text
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_first_comment
|
def create_first_comment
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<div class="comment-section">
|
<div class="comment-section">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="small-12 medium-10 column legislation-comment">
|
<div class="small-12 medium-10 column legislation-comment">
|
||||||
<%= @annotation.context.html_safe || @annotation.quote %>
|
<%= @annotation.context.try(:html_safe).presence || @annotation.quote %>
|
||||||
</div>
|
</div>
|
||||||
<div class="small-12 medium-2 column legislation-comment">
|
<div class="small-12 medium-2 column legislation-comment">
|
||||||
<span class="pull-right">
|
<span class="pull-right">
|
||||||
|
|||||||
Reference in New Issue
Block a user