Module: Asciidoctor::ISO::Utils

Included in:
Converter
Defined in:
lib/asciidoctor/iso/utils.rb

Constant Summary collapse

NOKOHEAD =
<<~HERE.freeze
    <!DOCTYPE html SYSTEM
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head> <title></title> <meta charset="UTF-8" /> </head>
    <body> </body> </html>
HERE
SUBCLAUSE_XPATH =
"//clause[ancestor::clause or ancestor::annex or "\
"ancestor::introduction]".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.anchor_or_uuid(node = nil) ⇒ Object



14
15
16
17
# File 'lib/asciidoctor/iso/utils.rb', line 14

def anchor_or_uuid(node = nil)
  uuid = UUIDTools::UUID.random_create
  node.nil? || node.id.nil? || node.id.empty? ? "_" + uuid : node.id
end

.current_location(n) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/asciidoctor/iso/utils.rb', line 19

def current_location(n)
  return "Line #{n.lineno}" if n.respond_to?(:lineno) &&
    !n.lineno.nil? && !n.lineno.empty?
  return "Line #{n.line}" if n.respond_to?(:line) &&
    !n.line.nil?
  return "ID #{n.id}" if n.respond_to?(:id) && !n.id.nil?
  while !n.nil? &&
      (!n.respond_to?(:level) || n.level.positive?) &&
      (!n.respond_to?(:context) || n.context != :section)
    n = n.parent
    return "Section: #{n.title}" if n&.respond_to?(:context) &&
      n&.context == :section
  end
  "??"
end

.flatten_rawtext(node) ⇒ Object

if node contains blocks, flatten them into a single line; and extract only raw text



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/asciidoctor/iso/utils.rb', line 58

def flatten_rawtext(node)
  result = []
  if node.respond_to?(:blocks) && node.blocks?
    node.blocks.each { |b| result << flatten_rawtext(b) }
  elsif node.respond_to?(:lines)
    result = flatten_rawtext_lines(node, result)
  elsif node.respond_to?(:text)
    result << node.text.gsub(/<[^>]*>+/, "")
  else
    result << node.content.gsub(/<[^>]*>+/, "")
  end
  result.reject(&:empty?)
end

.flatten_rawtext_lines(node, result) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/iso/utils.rb', line 43

def flatten_rawtext_lines(node, result)
  node.lines.each do |x|
    if node.respond_to?(:context) && (node.context == :literal ||
        node.context == :listing)
      result << x.gsub(/</, "&lt;").gsub(/>/, "&gt;")
    else
      # strip not only HTML <tag>, and Asciidoc xrefs <<xref>>
      result << x.gsub(/<[^>]*>+/, "")
    end
  end
  result
end

.warning(node, msg, text) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/asciidoctor/iso/utils.rb', line 35

def warning(node, msg, text)
  return if @novalid
  warntext = "asciidoctor: WARNING"\
    "(#{current_location(node)}): #{msg}"
  warntext += ": #{text}" if text
  warn warntext
end

Instance Method Details

#attr_code(attributes) ⇒ Object



101
102
103
104
105
106
# File 'lib/asciidoctor/iso/utils.rb', line 101

def attr_code(attributes)
  attributes = attributes.reject { |_, val| val.nil? }.map
  attributes.map do |k, v|
    [k, (v.is_a? String) ? HTMLEntities.new.decode(v) : v]
  end.to_h
end

#convert(node, transform = nil, opts = {}) ⇒ Object



73
74
75
76
# File 'lib/asciidoctor/iso/utils.rb', line 73

def convert(node, transform = nil, opts = {})
  transform ||= node.node_name
  opts.empty? ? (send transform, node) : (send transform, node, opts)
end

#document_ns_attributes(_doc) ⇒ Object



78
79
80
# File 'lib/asciidoctor/iso/utils.rb', line 78

def document_ns_attributes(_doc)
  nil
end

#noko(&block) ⇒ Object

block for processing XML document fragments as XHTML, to allow for HTMLentities



92
93
94
95
96
97
98
99
# File 'lib/asciidoctor/iso/utils.rb', line 92

def noko(&block)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  fragment = doc.fragment("")
  ::Nokogiri::XML::Builder.with fragment, &block
  fragment.to_xml(encoding: "US-ASCII").lines.map do |l|
    l.gsub(/\s*\n/, "")
  end
end

#wrap_in_para(node, out) ⇒ Object

if the contents of node are blocks, output them to out; else, wrap them in <p>



110
111
112
113
114
115
# File 'lib/asciidoctor/iso/utils.rb', line 110

def wrap_in_para(node, out)
  if node.blocks? then out << node.content
  else
    out.p { |p| p << node.content }
  end
end