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, firstonly: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/isodoc/class_utils.rb', line 28

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

  seen = false
  text.split(/(\s+)/).map do |w|
    letters = w.chars
    case_strict1(letters, casing) if !seen || !firstonly
    seen ||= /\S/.match?(w)
    letters.join
  end.join
end

#case_strict1(letters, casing) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/isodoc/class_utils.rb', line 40

def case_strict1(letters, casing)
  return letters if letters.empty?

  case casing
  when "capital" then letters.first.upcase!
  when "lowercase" then letters.first.downcase!
  when "allcaps" then letters.map(&:upcase!)
  end
end

#case_with_markup(linkend, casing, script, firstonly: true) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/isodoc/class_utils.rb', line 55

def case_with_markup(linkend, casing, script, firstonly: true)
  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,
                                  firstonly: firstonly))
    seen = true if firstonly
  end
  to_xml(xml.root.children)
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}&#x2013;"
  ret += to.text if to
  ret
end

#liquid(doc) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/isodoc/class_utils.rb', line 19

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

#nearest_block_parent(node) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/isodoc/class_utils.rb', line 68

def nearest_block_parent(node)
  until %w(p title td th name formula li dt dd sourcecode pre quote
           note example target clause annex term appendix bibdata)
      .include?(node.name)
    node = node.parent
  end
  node
end

#ns(xpath) ⇒ Object



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

def ns(xpath)
  Metanorma::Utils::ns(xpath)
end

#start_of_sentence(node) ⇒ Object

node is at the start of sentence in a Metanorma XML context



78
79
80
81
82
83
84
85
# File 'lib/isodoc/class_utils.rb', line 78

def start_of_sentence(node)
  prec = [] # all text nodes before node
  nearest_block_parent(node).traverse do |x|
    x == node and break
    x.text? and prec << x
  end
  prec.empty? || /(?!<[^.].)\.\s+$/.match(prec.map(&:text).join)
end

#to_xml(node) ⇒ Object



50
51
52
53
# File 'lib/isodoc/class_utils.rb', line 50

def to_xml(node)
  node&.to_xml(encoding: "UTF-8", indent: 0,
               save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
end