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
- ALBUM_SHELL =
File.read(File.join(TEMPLATE_DIR, 'card-album.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.
-
.render_album(provider, data, path) ⇒ Object
The
layout=albumvertical shell — see templates/card-album.html and DESIGN-asciidoctor-beautify-uri.adoc, "Album Layout".
Class Method Details
.escape_html(text) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 26 def self.escape_html(text) text.to_s .gsub('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end |
.icon(name) ⇒ Object
22 23 24 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 22 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
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 71 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 37 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 |
.render_album(provider, data, path) ⇒ Object
The layout=album vertical shell — see templates/card-album.html
and DESIGN-asciidoctor-beautify-uri.adoc, "Album Layout". Only
called once UriBlockMacro has already confirmed a thumbnail
resolved and the provider implements code_url (Spotify-only); no
thumbnail=false/no-code fallback logic lives here.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/asciidoctor/beautify_uri/card_renderer.rb', line 78 def self.render_album(provider, data, path) = if data[:subtitle] && !data[:subtitle].empty? %(<p class="uri-card-subtitle">#{escape_html data[:subtitle]}</p>) else '' end # gsub, not sub — unlike the default shell's tokens (each used # exactly once), {{TITLE}} and {{TARGET_URL}} each appear twice # here (thumbnail alt text + title line; two link wrappers). ALBUM_SHELL .sub('{{ID_ATTR}}', data[:id] ? %( id="#{escape_html data[:id]}") : '') .sub('{{PROVIDER}}', provider.key) .sub('{{THUMBNAIL_URL}}', escape_html(data[:thumbnail_url])) .gsub('{{TITLE}}', escape_html(data[:title])) .sub('{{SUBTITLE_BLOCK}}', ) .sub('{{CODE_URL}}', escape_html(provider.code_url(path))) .gsub('{{TARGET_URL}}', escape_html(data[:target_url])) end |