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)


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

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

#add_images(text) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll/geolexica/filters.rb', line 112

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



35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/geolexica/filters.rb', line 35

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)


211
212
213
# File 'lib/jekyll/geolexica/filters.rb', line 211

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
30
31
32
33
# File 'lib/jekyll/geolexica/filters.rb', line 13

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

  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



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

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



45
46
47
48
49
# File 'lib/jekyll/geolexica/filters.rb', line 45

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

  url.split("/").last
end

#extract_grammar_info(term) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/jekyll/geolexica/filters.rb', line 168

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



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/jekyll/geolexica/filters.rb', line 182

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



219
220
221
# File 'lib/jekyll/geolexica/filters.rb', line 219

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

#get_authoritative(sources) ⇒ Object



215
216
217
# File 'lib/jekyll/geolexica/filters.rb', line 215

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

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



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jekyll/geolexica/filters.rb', line 129

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



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

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

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


104
105
106
107
108
# File 'lib/jekyll/geolexica/filters.rb', line 104

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

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


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jekyll/geolexica/filters.rb', line 84

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 do |l|
      l["type"] == "src"
    end["content"]["uri_string"]
    docidentifier = bib_ref["docidentifier"].detect do |d|
      d["primary"]
    end["id"]
  end

  link_tag(link, docidentifier)
end


77
78
79
80
81
82
# File 'lib/jekyll/geolexica/filters.rb', line 77

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)


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

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


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jekyll/geolexica/filters.rb', line 54

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