Skip to content

Commit

Permalink
Merge 1be7426 into 59184b1
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenenglish committed May 18, 2022
2 parents 59184b1 + 1be7426 commit 41830e8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ def render_ocr_snippets(options = {})
end

# link to the book viewer, using page number or image index
# @document = SolrDocument (page object)
# @image_pid_list = an ordered Array of image pids for the book object
# @book_id = pid of book object
# @param document [SolrDocument] page object
# @param image_pid_list [Array] ordered Array of image pids for the book object
# @param book_id [String] ARK id of book object
def render_page_link(document, image_pid_list, book_id)
index_of_doc = image_pid_list.index(document.id)
page_num = document[blacklight_config.page_num_field.to_sym]
# TODO: UV doesn't seem to allow passing query string, as in "h=#{url_encode(params[:ocr_q])}"
viewer_path = "#{book_viewer_path(book_id)}#?&cv=#{index_of_doc}"
viewer_path += "&h=#{url_encode(params[:ocr_q])}" if params[:ocr_q].present?
link_to page_num ? "Page #{page_num}" : "Image #{index_of_doc + 1}",
"#{book_viewer_path(book_id)}#?&cv=#{index_of_doc}",
class: 'book_page_link',
rel: 'nofollow'
viewer_path, class: 'book_page_link', rel: 'nofollow'
end
end
end
49 changes: 49 additions & 0 deletions app/models/blacklight_iiif_search/iiif_search_annotation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

# corresponds to IIIF Annotation resource
module BlacklightIiifSearch
class IiifSearchAnnotation
include IIIF::Presentation
include BlacklightIiifSearch::AnnotationBehavior

attr_reader :document, :query, :hl_index, :snippet, :controller,
:parent_document, :hl_term_index

##
# @param document [SolrDocument]
# @param query [String]
# @param hl_index [Integer]
# @param snippet [String]
# @param controller [CatalogController]
# @param parent_document [SolrDocument]
# @param hl_term_index [Integer] index of highlighted term within snippet
# rubocop:disable Metrics/ParameterLists
def initialize(document, query, hl_index, snippet, controller, parent_document, hl_term_index)
@document = document
@query = query
@hl_index = hl_index
@snippet = snippet
@controller = controller
@parent_document = parent_document
@hl_term_index = hl_term_index
end
# rubocop:enable Metrics/ParameterLists

##
# @return [IIIF::Presentation::Annotation]
def as_hash
annotation = IIIF::Presentation::Annotation.new('@id' => annotation_id)
annotation.resource = text_resource_for_annotation if snippet
annotation['on'] = canvas_uri_for_annotation
annotation
end

##
# @return [IIIF::Presentation::Resource]
def text_resource_for_annotation
clean_snippet = ActionView::Base.full_sanitizer.sanitize(snippet)
IIIF::Presentation::Resource.new('@type' => 'cnt:ContentAsText',
'chars' => clean_snippet)
end
end
end
21 changes: 12 additions & 9 deletions app/models/blacklight_iiif_search/iiif_search_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,26 @@ def resources
annotation = IiifSearchAnnotation.new(document,
solr_response.params['q'],
0, nil, controller,
@parent_document)
@parent_document, 0)
@resources << annotation.as_hash
hit[:annotations] << annotation.annotation_id
else
hl_hash.each_value do |hl_array|
hl_array.each_with_index do |hl, hl_index|
# iterate over highlighted segments,
term_counts = {}
hl_index = 0
hl_array.each do |hl|
# iterate over highlighted segments (assume that <em></em> is the highlight tag)
# using highlighted text as "query" passed to IiifSearchAnnotation.new
# assume that <em></em> is the highlight tag
hl.scan(/<em>[\S]*<\/em>/)&.map { |v| v.gsub(/<[\/]?em>/, '') }&.each do |hl_string|
highlights = hl.scan(/<em>[\S]*<\/em>/)&.map { |v| v.gsub(/<[\/]?em>/, '') }
highlights.each do |hl_term|
@total += 1
annotation = IiifSearchAnnotation.new(document,
hl_string,
hl_index, hl, controller,
@parent_document)
term_counts[hl_term] ||= 1
annotation = IiifSearchAnnotation.new(document, hl_term, hl_index, hl, controller,
@parent_document, term_counts[hl_term] - 1)
@resources << annotation.as_hash
hit[:annotations] << annotation.annotation_id
hl_index += 1
term_counts[hl_term] += 1
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def coordinates

coords_json = fetch_and_parse_coords
if coords_json && coords_json['words']
matches = coords_json['words'].select { |k, _v| k.casecmp?(query) }
matches = coords_json['words'].select { |k, _v| k.gsub(/[\.,:;?!)]\z/, '') == query }
return default unless matches

# when multi-word query, hl_index may not exist for term, so use the highest index available
term_coords_array = nil
hl_index.downto(0) do |h_i|
hl_term_index.downto(0) do |h_i|
next if term_coords_array

term_coords_array = matches.values.flatten(1)[h_i]
Expand Down
1 change: 1 addition & 0 deletions app/views/image_viewer/_uv_viewer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
rangeId: urlDataProvider.get('rid', 0),
rotation: Number(urlDataProvider.get('r', 0)),
xywh: urlDataProvider.get('xywh', ''),
highlight: urlDataProvider.get('h', ''),
embedded: true,
locales: formattedLocales
}, urlDataProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
canvasIndex: Number(urlDataProvider.get('cv', 0)),
rotation: Number(urlDataProvider.get('r', 0)),
xywh: urlDataProvider.get('xywh', ''),
highlight: urlDataProvider.get('h', ''),
embedded: true,
locales: formattedLocales
}, urlDataProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:iiif_search_annotation) do
BlacklightIiifSearch::IiifSearchAnnotation.new(page_doc, query_term,
0, nil, controller,
parent_doc)
parent_doc, 0)
end
let(:test_request) { ActionDispatch::TestRequest.new({}) }

Expand Down

0 comments on commit 41830e8

Please sign in to comment.