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 | " \
"//executivesummary | //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
SUBCLAUSES =
"./clause | ./references | ./term  | ./terms | ./definitions".freeze
FIRST_LVL_REQ_RULE =
"[not(ancestor::permission or ancestor::requirement or ancestor::recommendation)]\n".freeze
FIRST_LVL_REQ =
".//permission\#{FIRST_LVL_REQ_RULE} | .//requirement\#{FIRST_LVL_REQ_RULE} | .//recommendation\#{FIRST_LVL_REQ_RULE}\n".freeze
REQ_CHILDREN =
"./permission | ./requirement | ./recommendation\n".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



81
82
83
84
85
# File 'lib/isodoc/xref/xref_util.rb', line 81

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



90
91
92
# File 'lib/isodoc/xref/xref_util.rb', line 90

def child_sections
  CHILD_SECTIONS
end

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



137
138
139
140
# File 'lib/isodoc/xref/xref_util.rb', line 137

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

#first_lvl_reqObject



109
110
111
# File 'lib/isodoc/xref/xref_util.rb', line 109

def first_lvl_req
  FIRST_LVL_REQ
end

#hier_separator(markup: false) ⇒ Object



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

def hier_separator(markup: false)
  h = hiersep
  h.blank? || !markup or h = delim_wrap(h)
  h
end

#hierfigsepObject



30
31
32
# File 'lib/isodoc/xref/xref_util.rb', line 30

def hierfigsep
  "-"
end

#hierreqtsepObject



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

def hierreqtsep
  "-"
end

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

assume parent is already semantically annotated with semx



132
133
134
135
# File 'lib/isodoc/xref/xref_util.rb', line 132

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

#hiersepObject



26
27
28
# File 'lib/isodoc/xref/xref_util.rb', line 26

def hiersep
  "."
end

#increment_label(elems, node, counter, increment: true) ⇒ Object



159
160
161
162
163
# File 'lib/isodoc/xref/xref_util.rb', line 159

def increment_label(elems, node, counter, increment: true)
  elems.size == 1 && !node["number"] and return ""
  counter.increment(node) if increment
  counter.print
end

#labelled_autonum(label, autonum) ⇒ Object



154
155
156
157
# File 'lib/isodoc/xref/xref_util.rb', line 154

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

#nodeSet(clauses) ⇒ Object



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

def nodeSet(clauses)
  case clauses
  when Nokogiri::XML::Node
    [clauses]
  when Nokogiri::XML::NodeSet
    clauses
  end
end

#req_childrenObject



117
118
119
# File 'lib/isodoc/xref/xref_util.rb', line 117

def req_children
  REQ_CHILDREN
end

#sections_xpathObject



77
78
79
# File 'lib/isodoc/xref/xref_util.rb', line 77

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



123
124
125
126
127
128
129
# File 'lib/isodoc/xref/xref_util.rb', line 123

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



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/isodoc/xref/xref_util.rb', line 142

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

#subclausesObject



97
98
99
# File 'lib/isodoc/xref/xref_util.rb', line 97

def subclauses
  SUBCLAUSES
end

#subfigure_delimObject



56
57
58
# File 'lib/isodoc/xref/xref_util.rb', line 56

def subfigure_delim
  ""
end

#subfigure_separator(markup: false) ⇒ Object



44
45
46
47
48
# File 'lib/isodoc/xref/xref_util.rb', line 44

def subfigure_separator(markup: false)
  h = hierfigsep
  h.blank? || !markup or h = delim_wrap(h)
  h
end

#subreqt_separator(markup: false) ⇒ Object



50
51
52
53
54
# File 'lib/isodoc/xref/xref_util.rb', line 50

def subreqt_separator(markup: false)
  h = hierreqtsep
  h.blank? || !markup or h = delim_wrap(h)
  h
end