Module: MarkdownServer::Plugins::BibleCitations::Helpers

Defined in:
lib/markdown_server/plugins/bible_citations/helpers.rb

Constant Summary collapse

DICTIONARY_TTL =

1 hour

3600
BLB_BASE =

── Blue Letter Bible ─────────────────────────────────────────────────

"https://www.blueletterbible.org"
@@strongs_cache =

Server-side Strong’s dictionary cache

nil
@@strongs_cache_url =
nil
@@strongs_fetched_at =
nil

Instance Method Summary collapse

Instance Method Details

#blueletterbible_html(html, url) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 104

def blueletterbible_html(html, url)
  info_html = extract_blb_info(html, url)
  infl_html = extract_blb_inflections(html, info_html[:word])
  usage_html = extract_blb_usage(html)
  conc_html = extract_blb_concordance(html)

  info_html[:table] + infl_html + usage_html + conc_html
end

#inject_pronunciation_icon(html, meta) ⇒ Object

── Pronunciation Icons ───────────────────────────────────────────────



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 140

def inject_pronunciation_icon(html, meta)
  return html unless meta.is_a?(Hash) && meta["strongs"] && meta["language"]
  strongs = meta["strongs"].to_s.strip
  lang = meta["language"].to_s.strip.downcase
  return html unless strongs.match?(/\A[GH]\d+\z/i) && %w[hebrew greek].include?(lang)

  sn = strongs.upcase
  blb_audio_url = "/pronunciation?strongs=#{sn}"
  blb_btn = audio_button(blb_audio_url, "Blue Letter Bible", lazy_fetch: true, data_strongs: sn)

  sl_lang = lang == "hebrew" ? "hebrew" : "greek"
  sl_num = sn.sub(/\A[HG]/, "")
  sl_url = "https://www.studylight.org/multi-media/audio/lexicons/eng/#{sl_lang}.html?n=#{sl_num}"
  sl_btn = audio_button(sl_url, "StudyLight")

  html.sub(%r{</h2>}) { " #{blb_btn}#{sl_btn}</h2>" }
end

#inject_strongs_urls(html) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 78

def inject_strongs_urls(html)
  return html unless settings.dictionary_url
  html = html.gsub(/<span\s+class="subst"([^>]*data-strongs="([^"]+)"[^>]*)>/) do
    attrs, strongs = $1, $2
    popup_url = strongs_popup_url(strongs)
    if popup_url && !attrs.include?("data-popup-url")
      %(<span class="subst" data-popup-url="#{h(popup_url)}"#{attrs}>)
    else
      $&
    end
  end
  html.gsub(/<span\s+class="word"([^>]*data-strongs="([^"]+)"[^>]*)>/) do
    attrs, strongs = $1, $2
    dict_url = strongs_map[strongs.strip.upcase]
    if dict_url && !attrs.include?("data-popup-url")
      %(<span class="word" data-popup-url="#{h(dict_url)}"#{attrs}>)
    else
      $&
    end
  end
end

#markdownr_html(html) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 128

def markdownr_html(html)
  content = html[/<div[^>]+class="md-content"[^>]*>([\s\S]*?)<\/div>\s*(?:<div\s+class="frontmatter"|<\/div>\s*<\/div>|\z)/im, 1]
  return page_html(html) unless content && content.length > 10

  fm = html[/<div\s+class="frontmatter">([\s\S]*?)<\/div>\s*<\/div>/im, 0] || ""
  result = content.strip
  result += "\n#{fm}" unless fm.empty?
  result.length > 15_000 ? result[0, 15_000] : result
end

#scrip_html(html, source_url = nil) ⇒ Object

── Scripture Pages ───────────────────────────────────────────────────



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 115

def scrip_html(html, source_url = nil)
  content = html[/<div\s+class="passage-text[^"]*"[^>]*>([\s\S]*)<\/div>\s*(?:<nav|<script|<style|\z)/im, 1] || ""
  return page_html(html, source_url) if content.empty?

  content = content.gsub(/<sup class="verse-number">(\d+)[^<]*<\/sup>/) do
    %(<sup class="verse-number" data-verse="#{$1}">#{$1} </sup>)
  end

  css_blocks = inline_external_css(html, source_url)
  content = inject_strongs_urls(content)
  css_blocks + '<div style="font-family:Georgia,serif;line-height:1.7">' + content + "</div>"
end

#strongs_mapObject

── Strong’s Dictionary ───────────────────────────────────────────────



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 15

def strongs_map
  url = settings.dictionary_url
  return {} unless url

  now = Time.now
  if @@strongs_cache && @@strongs_cache_url == url && @@strongs_fetched_at
    if url.match?(/\Ahttps?:\/\//i)
      return @@strongs_cache if (now - @@strongs_fetched_at) < DICTIONARY_TTL
    else
      path = File.expand_path(url, root_dir)
      mtime = File.mtime(path) rescue nil
      return @@strongs_cache if mtime && mtime <= @@strongs_fetched_at
    end
  end

  begin
    raw = if url.match?(/\Ahttps?:\/\//i)
      uri = URI.parse(url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = (uri.scheme == "https")
      http.open_timeout = MarkdownServer::Helpers::FetchHelpers::FETCH_TIMEOUT
      http.read_timeout = 10
      req = Net::HTTP::Get.new(uri.request_uri)
      req["Accept"] = "application/json"
      resp = http.request(req)
      return @@strongs_cache || {} unless resp.is_a?(Net::HTTPSuccess)
      resp.body
    else
      path = File.expand_path(url, root_dir)
      return @@strongs_cache || {} unless File.exist?(path)
      File.read(path, encoding: "utf-8")
    end

    data = JSON.parse(raw)
    url_tpl = data["url"] || ""
    map = {}
    %w[greek hebrew].each do |lang|
      stems = data.dig("stems", lang)
      next unless stems.is_a?(Hash)
      stems.each_value do |entry|
        sn = entry["strongs"].to_s.strip.upcase
        next if sn.empty?
        map[sn] = url_tpl.gsub("{filename}", entry["filename"].to_s)
      end
    end
    @@strongs_cache = map
    @@strongs_cache_url = url
    @@strongs_fetched_at = now
    map
  rescue StandardError
    @@strongs_cache || {}
  end
end

#strongs_popup_url(strongs) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/markdown_server/plugins/bible_citations/helpers.rb', line 69

def strongs_popup_url(strongs)
  sn = strongs.to_s.strip.upcase
  return nil unless sn.match?(/\A[GH]\d+\z/)
  url = strongs_map[sn]
  return url if url
  prefix = sn.start_with?("H") ? "wlc" : "tr"
  "https://www.blueletterbible.org/lexicon/#{sn.downcase}/nasb20/#{prefix}/0-1/"
end