Module: Asciidoctor::Standoc::Utils

Included in:
Converter
Defined in:
lib/asciidoctor/standoc/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



15
16
17
18
# File 'lib/asciidoctor/standoc/utils.rb', line 15

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



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

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



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/asciidoctor/standoc/utils.rb', line 74

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



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

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

.reqt_subpart(x) ⇒ Object



88
89
90
91
# File 'lib/asciidoctor/standoc/utils.rb', line 88

def reqt_subpart(x)
  %w(specification measurement-target verification import label
     subject inherit classification title).include? x
end

.smart_render_xml(x) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/asciidoctor/standoc/utils.rb', line 41

def smart_render_xml(x)
  xstr = x.to_xml if x.respond_to? :to_xml
  xml = Nokogiri::XML(xstr)
  xml.traverse do |n|
    next unless n.text?
    n.replace(smartformat(n.text))
  end
  xml.to_xml.sub(/<\?[^>]+>/, "")
end

.smartformat(n) ⇒ Object



36
37
38
39
# File 'lib/asciidoctor/standoc/utils.rb', line 36

def smartformat(n)
  n.gsub(/ -- /, "&#8201;&#8212;&#8201;").
    gsub(/--/, "&#8212;").smart_format.gsub(/</, "&lt;").gsub(/>/, "&gt;")
end

.warning(node, msg, text) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/asciidoctor/standoc/utils.rb', line 51

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



124
125
126
127
128
129
# File 'lib/asciidoctor/standoc/utils.rb', line 124

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



94
95
96
97
# File 'lib/asciidoctor/standoc/utils.rb', line 94

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



99
100
101
# File 'lib/asciidoctor/standoc/utils.rb', line 99

def document_ns_attributes(_doc)
  nil
end

#noko(&block) ⇒ Object

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/asciidoctor/standoc/utils.rb', line 113

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/, " ")
    #l.sub(/\n$/, " ")
    l.sub(/\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>



133
134
135
136
137
138
# File 'lib/asciidoctor/standoc/utils.rb', line 133

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