Module: IsoDoc::XrefGen::Util

Included in:
IsoDoc::Xref, Anchor
Defined in:
lib/isodoc/xref/xref_util.rb

Constant Summary collapse

SECTIONS_XPATH =
"//foreword | //introduction | //acknowledgements | " \
"//preface/abstract | " \
"//preface/terms | //preface/definitions | //preface/references | " \
"//preface/clause | //sections/terms | //annex | " \
"//sections/clause | //sections/definitions | " \
"//bibliography/references | //bibliography/clause".freeze
CHILD_SECTIONS =
"./clause | ./appendix | ./terms | ./definitions | " \
"./references".freeze

Instance Method Summary collapse

Instance Method Details

#blank?(text) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/isodoc/xref/xref_util.rb', line 18

def blank?(text)
  text.nil? || text.empty?
end

#child_asset_path(asset) ⇒ Object



38
39
40
41
42
# File 'lib/isodoc/xref/xref_util.rb', line 38

def child_asset_path(asset)
  "./*[not(self::xmlns:clause) and not(self::xmlns:appendix) and " \
  "not(self::xmlns:terms) and not(self::xmlns:definitions)]//xmlns:X | " \
  "./xmlns:X".gsub("X", asset)
end

#child_sectionsObject



47
48
49
# File 'lib/isodoc/xref/xref_util.rb', line 47

def child_sections
  CHILD_SECTIONS
end

#delim_wrap(delim, klass = "fmt-autonum-delim") ⇒ Object



67
68
69
70
# File 'lib/isodoc/xref/xref_util.rb', line 67

def delim_wrap(delim, klass = "fmt-autonum-delim")
  delim.blank? and return ""
  "<span class='#{klass}'>#{delim}</span>"
end

#hiersemx(parent, parentlabel, counter, element, sep: nil) ⇒ Object

assume parent is already semantically annotated with semx



62
63
64
65
# File 'lib/isodoc/xref/xref_util.rb', line 62

def hiersemx(parent, parentlabel, counter, element, sep: nil)
  sep ||= hier_separator(markup: true)
  "#{semx(parent, parentlabel)}#{sep}#{semx(element, counter.print)}"
end

#labelled_autonum(label, autonum) ⇒ Object



84
85
86
87
# File 'lib/isodoc/xref/xref_util.rb', line 84

def labelled_autonum(label, autonum)
  label.blank? and return autonum
  l10n("<span class='fmt-element-name'>#{label}</span> #{autonum}")
end

#noblank(xpath) ⇒ Object



22
23
24
# File 'lib/isodoc/xref/xref_util.rb', line 22

def noblank(xpath)
  xpath.reject { |n| blank?(n["id"]) }
end

#sections_xpathObject



34
35
36
# File 'lib/isodoc/xref/xref_util.rb', line 34

def sections_xpath
  SECTIONS_XPATH
end

#semx(node, label, element = "autonum") ⇒ Object

if hierarchically marked up node in label already, leave alone, else wrap in semx



53
54
55
56
57
58
59
# File 'lib/isodoc/xref/xref_util.rb', line 53

def semx(node, label, element = "autonum")
  label = label.to_s
  id = node["id"] || node[:id]
  /<semx element='[^']+' source='#{id}'/.match?(label) and return label
  l = stripsemx(label)
  %(<semx element='#{element}' source='#{id}'>#{l}</semx>)
end

#stripsemx(elem) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/isodoc/xref/xref_util.rb', line 72

def stripsemx(elem)
  elem.nil? and return elem
  xml = Nokogiri::XML::DocumentFragment.parse(elem)
  xml.traverse do |x|
    x.name == "semx" ||
      (x.name == "span" && /^fmt-/.match?(x["class"])) and
      x.replace(x.children)
  end
  xml.to_xml(indent: 0, encoding: "UTF-8",
             save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
end