Module: Asciidoctor::BeautifyUri::CardRenderer
- Defined in:
- lib/asciidoctor/beautify_uri/card_renderer.rb
Overview
Fills the shared, language-agnostic templates/card.html shell, and the per-provider icon fragments beside it, for the HTML5/DocBook backends. The exact same template file is used by the npm package's JS renderer, see js/lib/card_renderer.js, so any layout change belongs in that one file, not duplicated in code here.
Constant Summary collapse
- TEMPLATE_DIR =
File.('../../../templates', __dir__)
- SHELL =
Strip the leading documentation comment (its prose repeats the same {TOKEN} spellings as the real markup below it) before this becomes the substitution target, so each token is replaced exactly once. Read explicitly as UTF-8: card.html's own doc comment carries real Unicode text, and Ruby's default external encoding on a minimal CI image (no locale configured) is US-ASCII, which raised "invalid byte sequence in US-ASCII" on the very first .sub call below, confirmed live in CI though never reproduced locally, where the shell's own locale is already UTF-8.
File.read(File.join(TEMPLATE_DIR, 'card.html'), encoding: 'UTF-8').sub(/\A<!--.*?-->\s*/m, '').freeze
Class Method Summary collapse
- .escape_html(text) ⇒ Object
- .icon(name) ⇒ Object
-
.render(provider, data, thumbnail: true) ⇒ Object
data - Hash with :title, :subtitle (nullable), :thumbnail_url (nullable), :target_url provider - provider module (must respond to .key, .link_text, .icon_block) thumbnail - Boolean, whether to render the thumbnail column.
Class Method Details
.escape_html(text) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 25 def self.escape_html(text) text.to_s .gsub('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end |
.icon(name) ⇒ Object
21 22 23 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 21 def self.icon(name) File.read(File.join(TEMPLATE_DIR, 'icons', "#{name}.svg"), encoding: 'UTF-8') end |
.render(provider, data, thumbnail: true) ⇒ Object
data - Hash with :title, :subtitle (nullable), :thumbnail_url (nullable), :target_url provider - provider module (must respond to .key, .link_text, .icon_block) thumbnail - Boolean, whether to render the thumbnail column
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 36 def self.render(provider, data, thumbnail: true) thumb_url = thumbnail ? data[:thumbnail_url] : nil # No photographic thumbnail — thumbnail=false, or the provider # simply didn't resolve one — falls back to the provider's own # brand mark at full size in that column, rather than leaving it # empty. The small icon beside the link line is then redundant # (the same mark already anchors the card at a much larger size) # and dropped. if thumb_url thumbnail_block = %(<img class="uri-card-thumb" src="#{escape_html thumb_url}" alt="#{escape_html data[:title]}">) provider_icon_block = provider.icon_block else thumbnail_block = %(<span class="uri-card-thumb-logo">#{icon provider.key}</span>) provider_icon_block = '' end = if data[:subtitle] && !data[:subtitle].empty? %(<p class="uri-card-subtitle">#{escape_html data[:subtitle]}</p>) else '' end SHELL .sub('{{ID_ATTR}}', data[:id] ? %( id="#{escape_html data[:id]}") : '') .sub('{{PROVIDER}}', provider.key) .sub('{{GRID_COLUMNS}}', 'auto auto') .sub('{{THUMBNAIL_BLOCK}}', thumbnail_block) .sub('{{TITLE}}', escape_html(data[:title])) .sub('{{META_LINE_BLOCK}}', ) .sub('{{PROVIDER_ICON_BLOCK}}', provider_icon_block) .sub('{{PROVIDER_LINK_TEXT}}', escape_html(provider.link_text)) .sub('{{TARGET_URL}}', escape_html(data[:target_url])) end |