Module: Asciidoctor::Html::Utils

Defined in:
lib/asciidoctor/html/utils.rb

Overview

Utilities shared by multiple elements

Class Method Summary collapse

Class Method Details

.display_sectnum(node, level = 1) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/asciidoctor/html/utils.rb', line 80

def self.display_sectnum(node, level = 1)
  document = node.document
  sectnum = level > 1 ? node.numeral : node.sectnum(".", false)
  if document.attr?("chapnum") && (chapnum = document.attr("chapnum")).to_i.positive? && level == 1
    sectnum = "#{chapnum}.#{sectnum}"
  end
  %(<span class="title-mark">#{sectnum}</span>)
end

.display_title(node) ⇒ Object



34
35
36
37
38
# File 'lib/asciidoctor/html/utils.rb', line 34

def self.display_title(node)
  prefix = display_title_prefix node
  suffix = display_title_suffix node
  show_title?(node) ? %(<h6 class="block-title">#{prefix}#{node.title}#{suffix}</h6>\n) : ""
end

.display_title_prefix(node) ⇒ Object



47
48
49
50
51
# File 'lib/asciidoctor/html/utils.rb', line 47

def self.display_title_prefix(node)
  prefix = node.attr?("title-prefix") ? node.attr("title-prefix") : ""
  prefix = %(<span class="title-prefix">#{prefix}</span>) if node.title? && !node.title.empty? && !prefix.empty?
  prefix
end

.display_title_suffix(node) ⇒ Object



40
41
42
43
44
45
# File 'lib/asciidoctor/html/utils.rb', line 40

def self.display_title_suffix(node)
  return "" unless node.attr?("title-suffix")

  suffix = node.attr "title-suffix"
  %(<span class="title-suffix">#{node.apply_subs suffix}</span>)
end

.dyn_id_class_attr_str(node, classes = nil) ⇒ Object

Don’t include an id if the element will be wrapped by a title, since the wrapper should have the id.



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

def self.dyn_id_class_attr_str(node, classes = nil)
  id = node.title? ? nil : node.id
  id_class_attr_str id, classes
end

.id_class_attr_str(id, classes = nil) ⇒ Object



7
8
9
10
11
# File 'lib/asciidoctor/html/utils.rb', line 7

def self.id_class_attr_str(id, classes = nil)
  id_attr = id ? %( id="#{id}") : ""
  class_attr = classes ? %( class="#{classes}") : ""
  "#{id_attr}#{class_attr}"
end

.id_class_sel_comment(id, classes) ⇒ Object



26
27
28
# File 'lib/asciidoctor/html/utils.rb', line 26

def self.id_class_sel_comment(id, classes)
  id || (classes && !classes.empty?) ? " <!-- #{id_class_sel_str id, classes} -->" : ""
end

.id_class_sel_str(id, classes) ⇒ Object



20
21
22
23
24
# File 'lib/asciidoctor/html/utils.rb', line 20

def self.id_class_sel_str(id, classes)
  result = ""
  result += "##{id}" if id
  result + ".#{classes.tr "\s", "."}" if classes
end

.popover_button(content, content_id, classes = nil) ⇒ Object



74
75
76
77
78
# File 'lib/asciidoctor/html/utils.rb', line 74

def self.popover_button(content, content_id, classes = nil)
  extra_classes = classes ? " #{classes}" : ""
  attrs = %( tabindex="0" role="button" class="btn-po#{extra_classes}" data-contentid="#{content_id}")
  %(<a#{attrs}>#{content}</a>)
end

.show_title?(node) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/asciidoctor/html/utils.rb', line 30

def self.show_title?(node)
  node.attr?("showcaption") || node.title?
end

.wrap_id_classes(content, id, classes, tag_name = :div) ⇒ Object



53
54
55
56
# File 'lib/asciidoctor/html/utils.rb', line 53

def self.wrap_id_classes(content, id, classes, tag_name = :div)
  id_class = id_class_attr_str id, classes
  %(<#{tag_name}#{id_class}>\n#{content}\n</#{tag_name}>#{id_class_sel_comment id, classes}\n)
end

.wrap_id_classes_with_title(content, node, id, classes) ⇒ Object



70
71
72
# File 'lib/asciidoctor/html/utils.rb', line 70

def self.wrap_id_classes_with_title(content, node, id, classes)
  show_title?(node) ? wrap_id_classes(display_title(node) + content, id, classes) : content
end

.wrap_node(content, node, tag_name = :div) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/asciidoctor/html/utils.rb', line 58

def self.wrap_node(content, node, tag_name = :div)
  base_class = node.context
  mod = node.style
  mod_class = "#{base_class}-#{mod}" if mod && mod != base_class.to_s
  classes = [base_class, mod_class, node.role].compact.map(&:to_s).uniq.join(" ").strip
  wrap_id_classes content, node.id, classes, tag_name
end

.wrap_node_with_title(content, node, tag_name = :div) ⇒ Object



66
67
68
# File 'lib/asciidoctor/html/utils.rb', line 66

def self.wrap_node_with_title(content, node, tag_name = :div)
  show_title?(node) ? wrap_node(display_title(node) + content, node, tag_name) : content
end