Module: Jekyll::Geolexica::Filters

Defined in:
lib/jekyll/geolexica/filters.rb

Constant Summary collapse

URN_REFERENCE_REGEX =
/{{(urn:[^,}]*),?([^,}]*),?([^}]*)?}}/.freeze
REFERENCE_REGEX =
/(?:&lt;|<){2}((?!:&gt;:&gt;).*?)(?:&gt;|>){2}/.freeze
IMAGE_REGEX =
/(?:<|&lt;){2}(fig_.*?)(?:>|&gt;){2}/.freeze
ABBREVIATION_TYPES =
%w[
  truncation
  acronym
  initialism
].freeze

Instance Method Summary collapse

Instance Method Details

#abbreviation?(term) ⇒ Boolean

check if the given term is an abbreviation or not

Returns:

  • (Boolean)


192
193
194
# File 'lib/jekyll/geolexica/filters.rb', line 192

def abbreviation?(term)
  ABBREVIATION_TYPES.include?(term["type"])
end

#add_images(text) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jekyll/geolexica/filters.rb', line 104

def add_images(text)
  return text if text.nil?

  images = []

  text.gsub!(IMAGE_REGEX) do
    image_name = Regexp.last_match[1]
     = [image_name]

    images << image_tag(image_name, , width: "100%")

    ["clause"]
  end

  text + images.join("\n\n")
end

#concepts_url(base_url) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/jekyll/geolexica/filters.rb', line 31

def concepts_url(base_url)
  return if !base_url || base_url.empty?

  if base_url.end_with?("/")
    "#{base_url}concepts/"
  else
    "#{base_url}/concepts/"
  end
end

#deprecated?(term) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/jekyll/geolexica/filters.rb', line 200

def deprecated?(term)
  term["normative_status"] == "deprecated"
end

#display_authoritative_source(input) ⇒ String

Renders authoritative source hash as HTML.

TODO Maybe support string inputs.

Parameters:

  • input (Hash)

    authoritative source hash.

Returns:

  • (String)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll/geolexica/filters.rb', line 13

def display_authoritative_source(input)
  ref, clause, link = input["origin"].values_at("ref", "clause", "link") rescue nil

  return "" if ref.nil? && link.nil?

  ref_caption = escape_once(ref || link)
  ref_part = link ? %[<a href="#{link}">#{ref_caption}</a>] : ref_caption

  clause_part = clause && escape_once(clause)

  source = [ref_part, clause_part].compact.join(", ")

  modification = input["modification"]
  return source unless modification

  "#{source}, #{input["status"] || "modified"} -- #{modification}"
end

#display_terminological_data(term) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jekyll/geolexica/filters.rb', line 145

def display_terminological_data(term)
  result = []

  result << "&lt;#{term['usage_info']}&gt;" if term["usage_info"]
  result << extract_grammar_info(term)
  result << term["geographical_area"]&.upcase

  result.unshift(",") if result.compact.size.positive?

  result.compact.join(" ")
end

#extract_concept_id(url) ⇒ Object



41
42
43
44
45
# File 'lib/jekyll/geolexica/filters.rb', line 41

def extract_concept_id(url)
  return if !url || url.empty?

  url.split("/").last
end

#extract_grammar_info(term) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jekyll/geolexica/filters.rb', line 157

def extract_grammar_info(term)
  return unless term["grammar_info"]

  grammar_info = []

  term["grammar_info"].each do |info|
    grammar_info << info["gender"]&.join(", ")
    grammar_info << info["number"]&.join(", ")
    grammar_info << extract_parts_of_speech(info)
  end

  grammar_info.join(" ")
end

#extract_parts_of_speech(grammar_info) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jekyll/geolexica/filters.rb', line 171

def extract_parts_of_speech(grammar_info)
  parts_of_speech = grammar_info.dup || {}

  %w[number gender].each do |key|
    parts_of_speech.delete(key)
  end

  parts_of_speech
    .select { |_key, value| value }
    .keys
    .compact
    .join(", ")
end

#get_all_authoritative_sources(sources) ⇒ Object



208
209
210
# File 'lib/jekyll/geolexica/filters.rb', line 208

def get_all_authoritative_sources(sources)
  sources&.select { |source| source["type"] == "authoritative" }
end

#get_authoritative(sources) ⇒ Object



204
205
206
# File 'lib/jekyll/geolexica/filters.rb', line 204

def get_authoritative(sources)
  sources&.find { |source| source["type"] == "authoritative" }
end

#image_tag(image_name, metadata, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jekyll/geolexica/filters.rb', line 121

def image_tag(image_name, , options = {})
  options_str = options.map do |name, value|
    %(#{name} = "#{value}")
  end.join(" ")

  title = "#{['clause']}#{['caption']}"

  <<~TEMPLATE
    <img src="/concepts/images/#{image_name}.png" #{options_str}>
    <div style="font-weight: bold;">#{title}</div>
  TEMPLATE
end

#images_metadataObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/jekyll/geolexica/filters.rb', line 134

def 
  site = @context.registers[:site]
  glossary_path = site.config["geolexica"]["glossary_path"]
  return {} if glossary_path.nil? || glossary_path.empty?

  @images_metadata ||= YAML.safe_load_file(
    File.expand_path("#{glossary_path}/images_metadata.yaml", site.source),
    permitted_classes: [Time],
  )
end


96
97
98
99
100
# File 'lib/jekyll/geolexica/filters.rb', line 96

def link_tag(link, text)
  return text if link.nil? || link.empty?

  "<a href=\"#{link}\">#{text}<\/a>"
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll/geolexica/filters.rb', line 80

def link_tag_from_ref(ref)
  return ref if @context.registers[:site].data["bibliography"].nil?

  bib_ref = @context.registers[:site].data["bibliography"][ref]

  if bib_ref["user_defined"]
    link = bib_ref["link"]
    docidentifier = bib_ref["reference"]
  else
    link = bib_ref["link"].detect { |l| l["type"] == "src" }["content"]["uri_string"]
    docidentifier = bib_ref["docidentifier"].detect { |d| d["primary"] }["id"]
  end

  link_tag(link, docidentifier)
end


73
74
75
76
77
78
# File 'lib/jekyll/geolexica/filters.rb', line 73

def link_tag_from_urn(urn, term_referenced, term_to_show)
  clause = urn.split(":").last
  term_to_show = term_to_show.empty? ? term_referenced : term_to_show

  link_tag("/concepts/#{clause}", term_to_show)
end

#preferred?(term) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/jekyll/geolexica/filters.rb', line 196

def preferred?(term)
  term["normative_status"] == "preferred"
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll/geolexica/filters.rb', line 50

def resolve_reference_to_links(text)
  return text if text.nil?

  text.gsub!(URN_REFERENCE_REGEX) do |reference|
    urn = Regexp.last_match[1]

    if !urn || urn.empty?
      reference
    else
      link_tag_from_urn(urn, Regexp.last_match[2], Regexp.last_match[3])
    end
  end

  text.gsub!(REFERENCE_REGEX) do |reference|
    ref = Regexp.last_match[1]
    return reference if ref.start_with?("fig")

    link_tag_from_ref(ref.split(",").first)
  end

  text
end