Module: Asciidoctor::Html::Utils
- Defined in:
- lib/asciidoctor/html/utils.rb
Overview
Utilities shared by multiple elements
Class Method Summary collapse
-
.display_footnotes(node, content) ⇒ Object
index_filter: Hash(index => bool).
- .display_sectnum(node, level = 1) ⇒ Object
- .display_title(node) ⇒ Object
- .display_title_prefix(node) ⇒ Object
- .display_title_suffix(node) ⇒ Object
-
.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.
- .heading(node, title, discrete: false) ⇒ Object
- .id_class_attr_str(id, classes = nil) ⇒ Object
- .id_class_sel_comment(id, classes) ⇒ Object
- .id_class_sel_str(id, classes) ⇒ Object
- .line_number_attr(line_number, live: false) ⇒ Object
- .popover_button(content, content_id, classes = nil) ⇒ Object
- .reftext(node) ⇒ Object
- .show_error(message) ⇒ Object
- .show_title?(node) ⇒ Boolean
- .wrap_id_classes(content, id, classes, tag_name = :div) ⇒ Object
- .wrap_id_classes_with_title(content, node, id, classes) ⇒ Object
- .wrap_live(content, live_attr) ⇒ Object
- .wrap_node(content, node, tag_name = :div) ⇒ Object
- .wrap_node_with_title(content, node, tag_name = :div) ⇒ Object
Class Method Details
.display_footnotes(node, content) ⇒ Object
index_filter: Hash(index => bool)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/asciidoctor/html/utils.rb', line 126 def self.display_footnotes(node, content) footnotes = [] content.scan(/data-contentid="_footnotedef_(\d)"/) do |matches| index = matches&.first&.to_i fn = node.document.footnotes.select { |fn| fn.index == index }.first next unless fn footnotes << <<~HTML <div class="fn-row"> <div class="fn-mark">#{fn.index}</div> <div class="footnote" id="_footnotedef_#{fn.index}">#{fn.text}</div> </div> HTML end return if footnotes.empty? <<~HTML <div class="fn-wrapper"> <div class="footnote-separator"></div> <div class="footnotes"> #{footnotes.join "\n"} </div> <!-- .footnotes --> </div> <!-- .fn-wrapper --> HTML end |
.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) ? %(<div class="block-title">#{prefix}#{node.title}#{suffix}</div>\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 |
.heading(node, title, discrete: false) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/asciidoctor/html/utils.rb', line 107 def self.heading(node, title, discrete: false) level = node.level tag_level = [level + 1, 6].min tag_name = %(h#{tag_level}) classes = %( class="#{node.role}") if discrete && node.role %(<#{tag_name}#{classes}>#{title}</#{tag_name}>\n) 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 |
.line_number_attr(line_number, live: false) ⇒ Object
103 104 105 |
# File 'lib/asciidoctor/html/utils.rb', line 103 def self.line_number_attr(line_number, live: false) live ? %( data-line-number="#{line_number}") : "" end |
.popover_button(content, content_id, classes = nil) ⇒ Object
74 75 76 77 78 |
# File 'lib/asciidoctor/html/utils.rb', line 74 def self.(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 |
.reftext(node) ⇒ Object
121 122 123 |
# File 'lib/asciidoctor/html/utils.rb', line 121 def self.reftext(node) node.reftext || (node.title unless node.inline?) || "[#{node.id}]" if node.id end |
.show_error(message) ⇒ Object
115 116 117 118 119 |
# File 'lib/asciidoctor/html/utils.rb', line 115 def self.show_error() <<~HTML <p class="border border-danger p-2">#{}</p> HTML end |
.show_title?(node) ⇒ 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_live(content, live_attr) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/asciidoctor/html/utils.rb', line 89 def self.wrap_live(content, live_attr) return content unless live_attr /\A(?<default>normal|faded|covered)-(?<live>faded|covered)\Z/ =~ live_attr live_class = %(live-#{live || "faded"}) default_class = %(live-default-#{default || "normal"}) <<~HTML <div class="live #{default_class} #{live_class}" data-reset="#{default_class}"> <div class="live-select"><i class="bi bi-eye"></i></div> #{content}</div> <!-- .live --> HTML 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 |