Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions app/presenters/v1/resources_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ def translations
.translations
.approved
.one_verse
.includes(:language)
.order(priority: :asc, id: :asc)

process_results(list)
process_results(list).select { |resource| translation_resource_usable?(resource) }
end

def word_translations
Expand All @@ -24,9 +25,10 @@ def tafsirs
list = ResourceContent
.tafsirs
.approved
.includes(:language)
.order(priority: :asc, id: :asc)

process_results(list)
process_results(list).select { |resource| tafsir_resource_usable?(resource) }
end

def languages
Expand Down Expand Up @@ -71,6 +73,42 @@ def process_results(records, resource_type: ResourceContent)
eager_load_best_names(records, resource_type: resource_type)
end

def translation_resource_usable?(resource)
slug = resource.slug.to_s.strip
return false if slug.blank?

language_code = resource.language&.iso_code.to_s.strip.downcase
return false if language_code.blank?

slug_matches_language?(slug, language_code)
end

def tafsir_resource_usable?(resource)
slug = resource.slug.to_s.strip
return false if slug.blank?

language_code = resource.language&.iso_code.to_s.strip.downcase
return false if language_code.blank?
return false if resource.records_count.to_i <= 0

slug_matches_language?(slug, language_code)
end

def slug_matches_language?(slug, language_code)
normalized_slug = slug.to_s.downcase.strip
return false if normalized_slug.blank?
return false if language_code.blank?

if normalized_slug.start_with?('quran.')
return normalized_slug.include?(".#{language_code}.")
end

prefix = normalized_slug[/\A([a-z]{2,3})(?:[-_\.])/, 1]
return true if prefix.blank?

prefix == language_code
end

def apply_language_filter(resources, resource_type:)
filter_language_code = params[:language].presence

Expand Down
13 changes: 11 additions & 2 deletions app/views/api/v1/audio/recitations/_ayah_recitation.json.props
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
include_audio_files = local_assigns[:include_audio_files] || false
audio_hosts = []

json.extract! ayah_recitation, :id, :name, :relative_path, :segments_count
json.has_segments ayah_recitation.segments_count > 0
json.supports_segment_timing ayah_recitation.segments_count.to_i > 0
json.style ayah_recitation.recitation_style&.name
json.qirat ayah_recitation.qirat_type&.name

if include_audio_files
json.audio_files do
ayah_recitation.audio_files.order('verse_id ASC').each do |audio_file|
audio_url = audio_file.audio_url.to_s
audio_host = URI.parse(audio_url).host rescue nil
audio_hosts << audio_host if audio_host.present?
json.set! audio_file.verse_key.to_s, {
file_size: audio_file.file_size,
duration: audio_file.duration,
audio_url: audio_file.audio_url,
audio_url: audio_url,
audio_host: audio_host,
segments_count: audio_file.segments_count
}
end
end
end

json.audio_hosts audio_hosts.uniq
json.audio_hosts_mixed audio_hosts.uniq.size > 1
end
14 changes: 12 additions & 2 deletions app/views/api/v1/tafsirs/_tafsir.json.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tafsir = local_assigns[:tafsir]
# Get resource_content from local_assigns, presenter, or tafsir association
resource_content = local_assigns[:resource_content] || tafsir.get_resource_content

verse_keys = []
Expand All @@ -25,4 +24,15 @@ json.translated_name do
json.locale translated_name_obj[:locale]
end

json.text tafsir.text.to_s
full_text = tafsir.text.to_s
offset = params[:text_offset].to_i
offset = 0 if offset.negative?
max_chars = params[:max_chars].to_i
max_chars = 12000 if max_chars <= 0
text_chunk = full_text.slice(offset, max_chars).to_s

json.text text_chunk
json.text_offset offset
json.text_chunk_size max_chars
json.text_total_length full_text.length
json.text_has_more((offset + text_chunk.length) < full_text.length)
21 changes: 20 additions & 1 deletion app/views/api/v1/translations/_translation.json.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
translation = local_assigns[:translation]
resource_content = local_assigns[:resource_content] || translation.get_resource_content
raw_text = translation.text.to_s
fragment = Nokogiri::HTML::DocumentFragment.parse(raw_text)
footnotes = []

fragment.css('a[foot_note], sup[foot_note]').each do |node|
footnote_id = node['foot_note'].to_s.strip
marker = node.text.to_s.gsub(/\s+/, '')
next if footnote_id.blank?

footnotes << {
id: footnote_id.to_i,
marker: marker
}
end

fragment.css('a').remove
fragment.css('sup').remove
clean_text = fragment.text.to_s.gsub(/\s+/, ' ').strip

json.id translation.id
json.verse_key translation.verse_key
Expand All @@ -16,5 +34,6 @@ json.translated_name do
json.locale translated_name_obj[:locale]
end

json.text translation.text.to_s
json.text clean_text
json.footnotes footnotes

25 changes: 23 additions & 2 deletions app/views/api/v1/verses/_translation.json.props
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
json.extract! translation, :id, :text
json.name translation.resource_name
raw_text = translation.text.to_s
fragment = Nokogiri::HTML::DocumentFragment.parse(raw_text)
footnotes = []

fragment.css('a[foot_note], sup[foot_note]').each do |node|
footnote_id = node['foot_note'].to_s.strip
marker = node.text.to_s.gsub(/\s+/, '')
next if footnote_id.blank?

footnotes << {
id: footnote_id.to_i,
marker: marker
}
end

fragment.css('a').remove
fragment.css('sup').remove
clean_text = fragment.text.to_s.gsub(/\s+/, ' ').strip

json.id translation.id
json.text clean_text
json.name translation.resource_name
json.footnotes footnotes
Loading