Module: IsoDoc::ClassUtils

Included in:
Common
Defined in:
lib/isodoc/class_utils.rb

Instance Method Summary collapse

Instance Method Details

#case_strict(text, casing, script) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/isodoc/class_utils.rb', line 31

def case_strict(text, casing, script)
  return text unless %w(Latn Cyrl Grek Armn).include?(script)

  letters = text.chars
  case casing
  when "capital" then letters.first.upcase!
  when "lowercase" then letters.first.downcase!
  end
  letters.join
end

#case_with_markup(linkend, casing, script) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/isodoc/class_utils.rb', line 42

def case_with_markup(linkend, casing, script)
  seen = false
  xml = Nokogiri::XML("<root>#{linkend}</root>")
  xml.traverse do |b|
    next unless b.text? && !seen

    b.replace(Common::case_strict(b.text, casing, script))
    seen = true
  end
  xml.root.children.to_xml
end

#date_range(date) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/isodoc/class_utils.rb', line 3

def date_range(date)
  from = date.at(ns("./from"))
  to = date.at(ns("./to"))
  on = date.at(ns("./on"))
  return date.text unless from || on || to
  return on.text if on

  ret = "#{from.text}&ndash;"
  ret += to.text if to
  ret
end

#liquid(doc) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/isodoc/class_utils.rb', line 22

def liquid(doc)
  # unescape HTML escapes in doc
  doc = doc.split(%r<(\{%|%\})>).each_slice(4).map do |a|
    a[2] = a[2].gsub("&lt;", "<").gsub("&gt;", ">") if a.size > 2
    a.join
  end.join
  Liquid::Template.parse(doc)
end

#ns(xpath) ⇒ Object



15
16
17
18
19
20
# File 'lib/isodoc/class_utils.rb', line 15

def ns(xpath)
  xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1")
    .gsub(%r{::([a-zA-z])}, "::xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1")
    .gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*[/\]])}, "[xmlns:\\1")
end