markawesome 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +26 -3
- data/lib/markawesome/counter_attributes.rb +86 -0
- data/lib/markawesome/plain_markdown_renderer.rb +5 -1
- data/lib/markawesome/quoted_tokens.rb +23 -0
- data/lib/markawesome/transformer.rb +40 -41
- data/lib/markawesome/transformers/base_transformer.rb +11 -0
- data/lib/markawesome/transformers/date_transformer.rb +34 -14
- data/lib/markawesome/transformers/layout_transformer.rb +8 -0
- data/lib/markawesome/transformers/lightbox_transformer.rb +165 -0
- data/lib/markawesome/transformers/number_ticker_transformer.rb +160 -0
- data/lib/markawesome/transformers.rb +2 -0
- data/lib/markawesome/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a88d3ed0946cecd2f203b0ede3c6693e8e05fe8a699761d5884d125c2a1ea70e
|
|
4
|
+
data.tar.gz: 8e9cd1f9decb28a1d240d27f3bde317953f36443a8329a785b8e41cef02f695b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 566f1d7e22b6e815bb404bf4401ae53b54ea4d39d59381ec48102e3741012ddc4523983e36bb8309ca580df643bfd5e599e2fca46676a823fd63dc34ae625f3d
|
|
7
|
+
data.tar.gz: e8f2f23d4f3b34c4e0875a9903a674597abf28a348cd8af972dfbc158a4991f8c4bdad38feac6a0d73aa5593cee4be4e5df23e581d7b35a62119a0b0127b53d1
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,24 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.21.0] - 2026-11-20
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **Lightbox gallery (`<wa-lightbox>` / `<wa-lightbox-item>`, Web Awesome 3.15.0, experimental).** New `LightboxTransformer`: a `::::lightbox params?` fence (or `:::wa-lightbox params?`) whose body lines are Markdown images. `` → an item whose `src` and thumbnail are the image; a linked image `[](href "caption")` → thumbnail `thumb`, item `src` = `href`. The caption is the link title, else the image title. An item `src` ending in `.mp4`/`.webm`/`.mov`/`.ogv`/`.m4v` (any case, ignoring `?query`/`#hash`) gets `type="video"`. Blank lines are ignored; any other line is rendered as Markdown in place. The gallery always carries `class="wa-grid"`, and `gap:`/`min:`/`align:`/`justify:` follow the `::::grid` rules via a new public `LayoutTransformer.grid_attributes`. Flags: `loop`, `thumbnails` → `with-thumbnails`, `no-nav` → `without-navigation`, `no-counter` → `without-counter`, `no-zoom` → `without-zoom`, `label:"…"`. Thumbnails are `<img loading="lazy">` square-cropped by default; `landscape` (4 / 3), `portrait` (3 / 4) or `natural` (no crop). Runs **first** in the pipeline with divider/stepper, so a gallery can sit inside a `::::grid`, card or tab, and `image_dialog: true` never wraps gallery images. Plain-Markdown degrade: the image lines as written, fence dropped (new `:lightbox` override key).
|
|
14
|
+
- **Counter (`<wa-counter>`, Web Awesome 3.15.0, experimental).** A third mode of the `[[[ … ]]]` date syntax: a `countdown` or `countup` token → `<wa-counter date="…">`, plus the `:::wa-counter <date> tokens` block (counts down unless `up`/`countup`). Tokens: `largest:`/`smallest:` (`years`…`seconds`, invalid dropped) → `largest-unit`/`smallest-unit`; `format:short|narrow`; `no-labels` → `without-labels`; `delimiter:"…"`; `label:"…"`; `lang:`/`locale:`; `finished:"…"` → `<span slot="finished">…</span>` (HTML-escaped). Fixed emission order: `date, direction, largest-unit, smallest-unit, format, without-labels, delimiter, label, lang`. `duration` (relative to page load) and `paused` (needs JS to resume) are not exposed. Plain-Markdown degrade: the raw date, as for other dates.
|
|
15
|
+
- **Number ticker (`<wa-number-ticker>`, Web Awesome 3.15.0, experimental).** New `NumberTickerTransformer`, run right before `DateTransformer`: `[[[ <number> tokens ]]]` (or `:::wa-number-ticker <number> tokens`) whose first token is `-?\d+(\.\d+)?%?`. `87%` → `type="percent" value="0.87"`, shifting the decimal point textually (no floats: `12.5%` → `0.125`, `150%` → `1.5`); a bare number with a `percent` token keeps its value. Tokens: `currency:XXX` (upcased, sets `type="currency"`), `display:symbol|narrowSymbol|code|name` → `currency-display`, `from:N` → `start-value`, `duration:MS`, `delay:MS`, `easing:VALUE`, `eager`, `no-grouping` → `without-grouping`, `decimals:N` (0–100) → `minimum-fraction-digits` + `maximum-fraction-digits`, `lang:`/`locale:`. Plain-Markdown degrade: the number as written, plus the currency code (new `:number_ticker` override key).
|
|
16
|
+
- `[[[ … ]]]` tokens are now **quote-aware**: `key:"…"` / `key:'…'` keeps a value with spaces together (new `Markawesome::QuotedTokens`). Existing date and relative-time output is unchanged.
|
|
17
|
+
- Manifest coverage now also guards `wa-lightbox-item` `type`, `wa-counter` `direction`/`format` and `wa-number-ticker` `type`/`currency-display`.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- `Transformer.process` runs its transformers from two ordered lists (`BEFORE_IMAGE_DIALOG`, `AFTER_IMAGE_DIALOG`) instead of one call per line; the order is unchanged apart from the new transformers.
|
|
22
|
+
|
|
23
|
+
### Internal
|
|
24
|
+
|
|
25
|
+
- **Refreshed the Web Awesome manifest fixture to 3.15.0** (November 17th, 2026). `rake wa:manifest[3.15.0]` regenerated `spec/fixtures/webawesome-enums.json` and `WA_VERSION` moved to `3.15.0`. The diff is **purely additive** — five new tags (`wa-code-highlighter`, `wa-counter`, `wa-lightbox`, `wa-lightbox-item`, `wa-number-ticker`) and their enums — with **zero DRIFT and zero GAP** against the enums we accept, so no transformer constant changed. `<wa-stepper>` is now **stable** in Web Awesome, so the "experimental" wording was dropped from the README. Fixture remains byte-identical to `markawesome-js`'s copy. Example-site CDN pins bumped to 3.15.0 in `jekyll-webawesome` and `eleventy-plugin-webawesome`.
|
|
26
|
+
|
|
9
27
|
## [0.20.0] - 2026-10-09
|
|
10
28
|
|
|
11
29
|
### Added
|
data/README.md
CHANGED
|
@@ -23,11 +23,14 @@ Used as the transformation engine for the [jekyll-webawesome](https://github.com
|
|
|
23
23
|
| **Carousel** | `~~~~~~` | `:::wa-carousel` | `<wa-carousel>` with carousel items |
|
|
24
24
|
| **Comparison** | `\|\|\|` or `\|\|\|25` | `:::wa-comparison` or `:::wa-comparison 25` | `<wa-comparison>` with before/after slots |
|
|
25
25
|
| **Copy Button** | `<<<placement? tooltip:full\|copy\|none?` | `:::wa-copy-button placement? tooltip:full\|copy\|none?` | `<wa-copy-button value="content" tooltip-placement="top" tooltip="copy"></wa-copy-button>` |
|
|
26
|
+
| **Counter** | `[[[ date countdown\|countup params? ]]]` | `:::wa-counter date params?` (empty body) | `<wa-counter date="2026-12-24" direction="up"></wa-counter>` |
|
|
26
27
|
| **Details** | `^^^appearance? icon-placement?` | `:::wa-details appearance? icon-placement?` | `<wa-details>content</wa-details>` |
|
|
27
28
|
| **Dialog** | `???params?` | `:::wa-dialog params?` | `<wa-dialog>` with trigger button and content |
|
|
28
29
|
| **Divider** | `--- label ---` (e.g. `--- start icon:star New ---`) | `:::wa-divider params?` (body is the label) | `<wa-divider label-placement="start"><wa-icon name="star"></wa-icon> New</wa-divider>` |
|
|
29
30
|
| **Icon** | `$$$icon-name` | `:::wa-icon icon-name` | `<wa-icon name="icon-name"></wa-icon>` |
|
|
30
31
|
| **Image Dialog** | `` | — | Wraps images in clickable `<wa-dialog>` overlays |
|
|
32
|
+
| **Lightbox** | `::::lightbox params?` wrapping image lines | `:::wa-lightbox params?` | `<wa-lightbox class="wa-grid"><wa-lightbox-item src="full.jpg" caption="…"><img …></wa-lightbox-item>…</wa-lightbox>` |
|
|
33
|
+
| **Number Ticker** | `[[[ number params? ]]]` (e.g. `[[[ 87% ]]]`) | `:::wa-number-ticker number params?` (empty body) | `<wa-number-ticker value="0.87" type="percent"></wa-number-ticker>` |
|
|
31
34
|
| **Popover** | `&&¶ms?` or inline `&&&trigger >>> content&&&` | `:::wa-popover params?` | `<wa-popover>` with trigger button and content |
|
|
32
35
|
| **Stepper** | `>>>>>> params?` wrapping a task list | `:::wa-stepper params?` | `<wa-stepper active="step-2"><wa-step name="step-1" completed>…</wa-step>…</wa-stepper>` |
|
|
33
36
|
| **Tab Group** | `++++++` | `:::wa-tabs` | `<wa-tab-group><wa-tab>content</wa-tab></wa-tab-group>` |
|
|
@@ -52,9 +55,29 @@ Used as the transformation engine for the [jekyll-webawesome](https://github.com
|
|
|
52
55
|
> (`pulse`/`bounce`/`none`) and `icon:name` are flags; lines indented under a
|
|
53
56
|
> step become its description. The active step is the one flagged `active`,
|
|
54
57
|
> else the first incomplete step, else the last. Container params are
|
|
55
|
-
> `horizontal`/`vertical`/`auto` and `label:"…"`. `wa-stepper` is
|
|
56
|
-
>
|
|
57
|
-
>
|
|
58
|
+
> `horizontal`/`vertical`/`auto` and `label:"…"`. `wa-stepper` is stable as of
|
|
59
|
+
> Web Awesome 3.15.0; its interactive features (`clickable`, `linear`) are
|
|
60
|
+
> deliberately not exposed.
|
|
61
|
+
|
|
62
|
+
> **Lightbox:** each body line is an item — ``, or a
|
|
63
|
+
> linked image `[](full "caption")` for a separate full-size file;
|
|
64
|
+
> a video extension (`.mp4`, `.webm`, `.mov`, `.ogv`, `.m4v`) makes it
|
|
65
|
+
> `type="video"`. Other lines are rendered as Markdown in place. The gallery is a
|
|
66
|
+
> `wa-grid` (`gap:`, `min:`, `align:`, `justify:`); flags are `loop`,
|
|
67
|
+
> `thumbnails`, `no-nav`, `no-counter`, `no-zoom` and `label:"…"`; thumbnails
|
|
68
|
+
> are square-cropped unless `landscape`, `portrait` or `natural`. Every item's
|
|
69
|
+
> `src` is a real link, so the gallery degrades to links without JavaScript.
|
|
70
|
+
> Experimental in Web Awesome 3.15.0.
|
|
71
|
+
|
|
72
|
+
> **Counter and number ticker:** both share the `[[[ … ]]]` date syntax. A
|
|
73
|
+
> `countdown`/`countup` token makes a `<wa-counter>` (`largest:`/`smallest:`
|
|
74
|
+
> units, `format:short|narrow`, `no-labels`, `delimiter:"…"`, `label:"…"`,
|
|
75
|
+
> `finished:"…"`, `lang:`); a leading number makes a `<wa-number-ticker>`
|
|
76
|
+
> (`87%` → `type="percent" value="0.87"`, `currency:EUR`, `display:`, `from:`,
|
|
77
|
+
> `duration:`, `delay:`, `easing:`, `eager`, `no-grouping`, `decimals:N`,
|
|
78
|
+
> `lang:`). Quote values with spaces: `key:"…"`. Like dates, both render into
|
|
79
|
+
> shadow DOM and show nothing without Web Awesome's JavaScript. Experimental in
|
|
80
|
+
> Web Awesome 3.15.0.
|
|
58
81
|
|
|
59
82
|
## Layout Utilities
|
|
60
83
|
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'quoted_tokens'
|
|
4
|
+
|
|
5
|
+
module Markawesome
|
|
6
|
+
# <wa-counter> option vocabulary and attribute builder, used by
|
|
7
|
+
# DateTransformer's counter mode (`[[[ <date> countdown|countup … ]]]` and
|
|
8
|
+
# `:::wa-counter`).
|
|
9
|
+
#
|
|
10
|
+
# Tokens (later tokens win):
|
|
11
|
+
# countdown/down, countup/up -> direction (down is the default, never emitted)
|
|
12
|
+
# largest:UNIT, smallest:UNIT -> largest-unit / smallest-unit (invalid dropped)
|
|
13
|
+
# format:short|narrow -> format (long is the default, never emitted)
|
|
14
|
+
# no-labels -> without-labels
|
|
15
|
+
# delimiter:"…", label:"…" -> delimiter / label
|
|
16
|
+
# lang:/locale: -> lang
|
|
17
|
+
# finished:"…" -> <span slot="finished">…</span>
|
|
18
|
+
module CounterAttributes
|
|
19
|
+
DIRECTIONS = %w[down up].freeze
|
|
20
|
+
FORMATS = %w[long short narrow].freeze
|
|
21
|
+
UNITS = %w[years months days hours minutes seconds].freeze
|
|
22
|
+
|
|
23
|
+
# Deterministic emission order (required for byte-for-byte parity).
|
|
24
|
+
ORDER = %w[date direction largest-unit smallest-unit format without-labels
|
|
25
|
+
delimiter label lang].freeze
|
|
26
|
+
|
|
27
|
+
module_function
|
|
28
|
+
|
|
29
|
+
# @return [Array(Array<String>, String)] [attribute parts, inner HTML]
|
|
30
|
+
def build(date, tokens)
|
|
31
|
+
attrs, finished = parse(tokens)
|
|
32
|
+
attrs['date'] = date if date
|
|
33
|
+
|
|
34
|
+
parts = ORDER.filter_map do |key|
|
|
35
|
+
next unless attrs.include?(key)
|
|
36
|
+
|
|
37
|
+
attrs[key] == true ? key : "#{key}=\"#{escape_html(attrs[key])}\""
|
|
38
|
+
end
|
|
39
|
+
inner = finished ? "<span slot=\"finished\">#{escape_html(finished)}</span>" : ''
|
|
40
|
+
[parts, inner]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [Array(Hash, String|nil)] [attrs, finished text]
|
|
44
|
+
def parse(tokens)
|
|
45
|
+
attrs = {}
|
|
46
|
+
finished = nil
|
|
47
|
+
tokens.each do |token|
|
|
48
|
+
case token
|
|
49
|
+
when 'up', 'countup' then attrs['direction'] = 'up'
|
|
50
|
+
when 'down', 'countdown' then attrs.delete('direction')
|
|
51
|
+
when 'no-labels' then attrs['without-labels'] = true
|
|
52
|
+
else
|
|
53
|
+
next unless (m = token.match(/\A([a-z-]+):(.+)\z/))
|
|
54
|
+
|
|
55
|
+
finished = apply_option(m[1], QuotedTokens.unquote(m[2]), attrs) || finished
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
[attrs, finished]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Applies one key:value option; returns the text for `finished`, else nil.
|
|
62
|
+
def apply_option(key, value, attrs)
|
|
63
|
+
case key
|
|
64
|
+
when 'largest', 'smallest'
|
|
65
|
+
attrs["#{key}-unit"] = value if UNITS.include?(value)
|
|
66
|
+
when 'format'
|
|
67
|
+
if FORMATS.include?(value)
|
|
68
|
+
value == 'long' ? attrs.delete('format') : attrs['format'] = value
|
|
69
|
+
end
|
|
70
|
+
when 'delimiter', 'label' then attrs[key] = value
|
|
71
|
+
when 'lang', 'locale' then attrs['lang'] = value
|
|
72
|
+
when 'finished' then return value
|
|
73
|
+
end
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def escape_html(text)
|
|
78
|
+
text.to_s
|
|
79
|
+
.gsub('&', '&')
|
|
80
|
+
.gsub('<', '<')
|
|
81
|
+
.gsub('>', '>')
|
|
82
|
+
.gsub('"', '"')
|
|
83
|
+
.gsub("'", ''')
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -26,7 +26,7 @@ module Markawesome
|
|
|
26
26
|
#
|
|
27
27
|
# @param component [Symbol] one of :accordion, :callout, :badge, :button,
|
|
28
28
|
# :card, :carousel, :comparison, :copy_button, :details, :dialog,
|
|
29
|
-
# :divider, :icon, :image_dialog, :layout, :popover, :random_content,
|
|
29
|
+
# :divider, :icon, :image_dialog, :layout, :lightbox, :number_ticker, :popover, :random_content,
|
|
30
30
|
# :stepper, :tabs, :tag, :tooltip, :tree.
|
|
31
31
|
# @yield [content, options] Proc that receives the full source content
|
|
32
32
|
# and the renderer options; returns the content with that component
|
|
@@ -46,9 +46,11 @@ module Markawesome
|
|
|
46
46
|
PIPELINE = %i[
|
|
47
47
|
divider
|
|
48
48
|
stepper
|
|
49
|
+
lightbox
|
|
49
50
|
layout
|
|
50
51
|
popover
|
|
51
52
|
tooltip
|
|
53
|
+
number_ticker
|
|
52
54
|
date
|
|
53
55
|
badge
|
|
54
56
|
button
|
|
@@ -72,9 +74,11 @@ module Markawesome
|
|
|
72
74
|
TRANSFORMER_MAP = {
|
|
73
75
|
divider: DividerTransformer,
|
|
74
76
|
stepper: StepperTransformer,
|
|
77
|
+
lightbox: LightboxTransformer,
|
|
75
78
|
layout: LayoutTransformer,
|
|
76
79
|
popover: PopoverTransformer,
|
|
77
80
|
tooltip: TooltipTransformer,
|
|
81
|
+
number_ticker: NumberTickerTransformer,
|
|
78
82
|
date: DateTransformer,
|
|
79
83
|
badge: BadgeTransformer,
|
|
80
84
|
button: ButtonTransformer,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Markawesome
|
|
4
|
+
# Whitespace tokenization that keeps `key:"…"` / `key:'…'` (spaces included)
|
|
5
|
+
# together as one token, plus the matching unquote. Shared by the `[[[ … ]]]`
|
|
6
|
+
# transformers (date, counter, number ticker).
|
|
7
|
+
module QuotedTokens
|
|
8
|
+
TOKEN_REGEX = /[a-z-]+:"[^"]*"|[a-z-]+:'[^']*'|\S+/
|
|
9
|
+
QUOTED_REGEX = /\A"([^"]*)"\z|\A'([^']*)'\z/
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def split(string)
|
|
14
|
+
string.to_s.scan(TOKEN_REGEX)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Strip one pair of matching surrounding quotes ("…" or '…'), if present.
|
|
18
|
+
def unquote(value)
|
|
19
|
+
match = QUOTED_REGEX.match(value)
|
|
20
|
+
match ? (match[1] || match[2]) : value
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -7,55 +7,54 @@ require_relative 'code_block_protector'
|
|
|
7
7
|
module Markawesome
|
|
8
8
|
# Main transformer that orchestrates all component transformers
|
|
9
9
|
class Transformer
|
|
10
|
+
# Runs before the optional ImageDialogTransformer. Order is load-bearing:
|
|
11
|
+
#
|
|
12
|
+
# - Divider, stepper and lightbox run FIRST. Divider/stepper content is
|
|
13
|
+
# inline-only (a label, one-line steps), and the lightbox's `::::` closer
|
|
14
|
+
# must be consumed before LayoutTransformer, so all three can sit inside
|
|
15
|
+
# layouts, cards, callouts, tabs, accordion and dialog bodies — e.g.
|
|
16
|
+
# `--- vertical vs ---` inside a `::::cluster`, a gallery in a `::::grid`.
|
|
17
|
+
# The lightbox emits <img> HTML, so ImageDialogTransformer never wraps
|
|
18
|
+
# gallery images in dialogs.
|
|
19
|
+
# - Number ticker runs right before the date transformer: both use
|
|
20
|
+
# `[[[ … ]]]`, and a leading number (never an ISO date) claims it.
|
|
21
|
+
BEFORE_IMAGE_DIALOG = [
|
|
22
|
+
DividerTransformer, StepperTransformer, LightboxTransformer,
|
|
23
|
+
LayoutTransformer, PopoverTransformer, TooltipTransformer,
|
|
24
|
+
NumberTickerTransformer, DateTransformer,
|
|
25
|
+
BadgeTransformer, ButtonTransformer, CalloutTransformer, CardTransformer,
|
|
26
|
+
CarouselTransformer, ComparisonTransformer, VideoTransformer,
|
|
27
|
+
CopyButtonTransformer, DetailsTransformer
|
|
28
|
+
].freeze
|
|
29
|
+
|
|
30
|
+
# Runs after the optional ImageDialogTransformer (which must precede the
|
|
31
|
+
# dialog transformer):
|
|
32
|
+
#
|
|
33
|
+
# - Accordion runs late so item bodies may contain other already-transformed
|
|
34
|
+
# components (same reason tabs runs near the end).
|
|
35
|
+
# - Tree runs after accordion; its body is a plain nested list (never a
|
|
36
|
+
# markdown-converted body), so ordering relative to accordion is moot.
|
|
37
|
+
# - Random-content runs LAST: its options routinely wrap already-transformed
|
|
38
|
+
# components (callouts/badges/cards), and dialog/details/popover/tooltip
|
|
39
|
+
# must consume their own `>>>` upstream before our `^>>>$` item split
|
|
40
|
+
# runs. Consequence: a `......` block is the outermost container and cannot
|
|
41
|
+
# be nested inside another container body (same constraint Tree documents).
|
|
42
|
+
AFTER_IMAGE_DIALOG = [
|
|
43
|
+
DialogTransformer, IconTransformer, TagTransformer, TabsTransformer,
|
|
44
|
+
AccordionTransformer, TreeTransformer, RandomContentTransformer
|
|
45
|
+
].freeze
|
|
46
|
+
|
|
10
47
|
def self.process(content, options = {})
|
|
11
48
|
content, tokens = CodeBlockProtector.protect(content)
|
|
12
49
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# them sit inside layouts, cards, callouts, tabs, accordion and dialog
|
|
16
|
-
# bodies — e.g. `--- vertical vs ---` inside a `::::cluster`.
|
|
17
|
-
content = DividerTransformer.transform(content)
|
|
18
|
-
content = StepperTransformer.transform(content)
|
|
19
|
-
|
|
20
|
-
content = LayoutTransformer.transform(content)
|
|
21
|
-
content = PopoverTransformer.transform(content)
|
|
22
|
-
content = TooltipTransformer.transform(content)
|
|
23
|
-
content = DateTransformer.transform(content)
|
|
24
|
-
content = BadgeTransformer.transform(content)
|
|
25
|
-
content = ButtonTransformer.transform(content)
|
|
26
|
-
content = CalloutTransformer.transform(content)
|
|
27
|
-
content = CardTransformer.transform(content)
|
|
28
|
-
content = CarouselTransformer.transform(content)
|
|
29
|
-
content = ComparisonTransformer.transform(content)
|
|
30
|
-
content = VideoTransformer.transform(content)
|
|
31
|
-
content = CopyButtonTransformer.transform(content)
|
|
32
|
-
content = DetailsTransformer.transform(content)
|
|
33
|
-
|
|
34
|
-
# Apply image dialog transformer BEFORE dialog transformer if enabled
|
|
50
|
+
content = BEFORE_IMAGE_DIALOG.reduce(content) { |acc, transformer| transformer.transform(acc) }
|
|
51
|
+
|
|
35
52
|
if options[:image_dialog]
|
|
36
53
|
config = options[:image_dialog].is_a?(Hash) ? options[:image_dialog] : {}
|
|
37
54
|
content = ImageDialogTransformer.transform(content, config)
|
|
38
55
|
end
|
|
39
56
|
|
|
40
|
-
content =
|
|
41
|
-
content = IconTransformer.transform(content)
|
|
42
|
-
content = TagTransformer.transform(content)
|
|
43
|
-
content = TabsTransformer.transform(content)
|
|
44
|
-
|
|
45
|
-
# Accordion runs last so item bodies may contain other already-transformed
|
|
46
|
-
# components (same reason tabs runs near the end).
|
|
47
|
-
content = AccordionTransformer.transform(content)
|
|
48
|
-
|
|
49
|
-
# Tree runs after accordion; its body is a plain nested list (never a
|
|
50
|
-
# markdown-converted body), so ordering relative to accordion is moot.
|
|
51
|
-
content = TreeTransformer.transform(content)
|
|
52
|
-
|
|
53
|
-
# Random-content runs LAST: its options routinely wrap already-transformed
|
|
54
|
-
# components (callouts/badges/cards), and dialog/details/popover/tooltip
|
|
55
|
-
# must consume their own `>>>` upstream before our `^>>>$` item split
|
|
56
|
-
# runs. Consequence: a `......` block is the outermost container and cannot
|
|
57
|
-
# be nested inside another container body (same constraint Tree documents).
|
|
58
|
-
content = RandomContentTransformer.transform(content)
|
|
57
|
+
content = AFTER_IMAGE_DIALOG.reduce(content) { |acc, transformer| transformer.transform(acc) }
|
|
59
58
|
|
|
60
59
|
CodeBlockProtector.restore(content, tokens)
|
|
61
60
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'English'
|
|
4
4
|
require 'kramdown'
|
|
5
|
+
require_relative '../quoted_tokens'
|
|
5
6
|
|
|
6
7
|
module Markawesome
|
|
7
8
|
# Base class for all Web Awesome component transformers
|
|
@@ -19,6 +20,16 @@ module Markawesome
|
|
|
19
20
|
Kramdown::Document.new(content).to_html
|
|
20
21
|
end
|
|
21
22
|
|
|
23
|
+
# Split a params string into tokens, keeping `key:"a b"` together.
|
|
24
|
+
def quoted_tokens(string)
|
|
25
|
+
QuotedTokens.split(string)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Strip one pair of matching surrounding quotes ("…" or '…'), if present.
|
|
29
|
+
def unquote(value)
|
|
30
|
+
QuotedTokens.unquote(value)
|
|
31
|
+
end
|
|
32
|
+
|
|
22
33
|
# Helper method to apply multiple regex patterns with the same transformation logic
|
|
23
34
|
# @param content [String] The content to transform
|
|
24
35
|
# @param patterns [Array<Hash>] Array of pattern hashes with :regex and :block
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base_transformer'
|
|
4
|
+
require_relative '../counter_attributes'
|
|
4
5
|
|
|
5
6
|
module Markawesome
|
|
6
|
-
# Transforms declarative timestamp syntax into Web Awesome's
|
|
7
|
+
# Transforms declarative timestamp syntax into Web Awesome's three date
|
|
7
8
|
# components:
|
|
8
9
|
# <wa-format-date> — an absolute, locale-formatted date ("June 26, 2026")
|
|
9
10
|
# <wa-relative-time> — a relative phrase ("3 days ago"), optionally ticking
|
|
11
|
+
# <wa-counter> — a live countdown to / count up from a date
|
|
10
12
|
#
|
|
11
13
|
# These are pure declarative wrappers over the browser's Intl.DateTimeFormat /
|
|
12
14
|
# Intl.RelativeTimeFormat: the date value is baked into the markup at build
|
|
@@ -15,11 +17,16 @@ module Markawesome
|
|
|
15
17
|
#
|
|
16
18
|
# Inline (primary): [[[ <date> <tokens> ]]]
|
|
17
19
|
# Block (alternative): :::wa-format-date <date> <tokens> / :::wa-relative-time …
|
|
18
|
-
# followed by a closing ::: (empty body)
|
|
20
|
+
# / :::wa-counter …, followed by a closing ::: (empty body)
|
|
19
21
|
#
|
|
20
|
-
# Mode: absolute (<wa-format-date>) is the default;
|
|
21
|
-
#
|
|
22
|
-
#
|
|
22
|
+
# Mode: absolute (<wa-format-date>) is the default; in the inline form a bare
|
|
23
|
+
# `countdown` / `countup` token switches to <wa-counter>, else a bare
|
|
24
|
+
# `relative` token switches to <wa-relative-time>. The block selector name
|
|
25
|
+
# chooses the mode directly (a counter counts down unless `up`/`countup`).
|
|
26
|
+
#
|
|
27
|
+
# Tokens are whitespace-separated, except that `key:"…"` / `key:'…'` keeps a
|
|
28
|
+
# quoted value (spaces included) together — used by the counter's
|
|
29
|
+
# `finished:`, `label:` and `delimiter:`.
|
|
23
30
|
#
|
|
24
31
|
# Static-site caveat: both components render generated text into shadow DOM
|
|
25
32
|
# with no light-DOM fallback — with Web Awesome's JS disabled they show
|
|
@@ -29,7 +36,7 @@ module Markawesome
|
|
|
29
36
|
# Inline: content excludes `]`, non-greedy, multiple-per-line, single-line.
|
|
30
37
|
INLINE_REGEX = /\[\[\[[ \t]*([^\]\r\n]+?)[ \t]*\]\]\]/
|
|
31
38
|
# Block: selector name picks the mode; an empty body, closed by `:::`.
|
|
32
|
-
ALTERNATIVE_REGEX = /^:::wa-(format-date|relative-time)[ \t]*([^\n]*)\n:::$/
|
|
39
|
+
ALTERNATIVE_REGEX = /^:::wa-(format-date|relative-time|counter)[ \t]*([^\n]*)\n:::$/
|
|
33
40
|
|
|
34
41
|
# A token is the date when it is an ISO 8601 date or datetime (datetimes use
|
|
35
42
|
# the `T` separator — a space would break whitespace tokenization).
|
|
@@ -77,11 +84,16 @@ module Markawesome
|
|
|
77
84
|
RELATIVE_FORMATS = %w[long short narrow].freeze
|
|
78
85
|
RELATIVE_NUMERICS = %w[auto always].freeze
|
|
79
86
|
|
|
87
|
+
# Bare tokens that switch the inline form to <wa-counter>; its options live
|
|
88
|
+
# in CounterAttributes.
|
|
89
|
+
COUNTER_MODE_TOKENS = %w[countdown countup].freeze
|
|
90
|
+
BLOCK_MODES = { 'format-date' => :absolute, 'relative-time' => :relative, 'counter' => :counter }.freeze
|
|
91
|
+
|
|
80
92
|
def self.transform(content)
|
|
81
93
|
patterns = [
|
|
82
94
|
{ regex: INLINE_REGEX, block: proc { |_m, md| render_tokens(md[1], nil) } },
|
|
83
95
|
{ regex: ALTERNATIVE_REGEX,
|
|
84
|
-
block: proc { |_m, md| render_tokens(md[2], md[1]
|
|
96
|
+
block: proc { |_m, md| render_tokens(md[2], BLOCK_MODES[md[1]]) } }
|
|
85
97
|
]
|
|
86
98
|
apply_multiple_patterns(content, patterns)
|
|
87
99
|
end
|
|
@@ -100,11 +112,19 @@ module Markawesome
|
|
|
100
112
|
private
|
|
101
113
|
|
|
102
114
|
def render_tokens(token_string, mode_override)
|
|
103
|
-
tokens = token_string
|
|
115
|
+
tokens = quoted_tokens(token_string)
|
|
104
116
|
date, tokens = split_date(tokens)
|
|
105
|
-
|
|
117
|
+
case mode_override || inline_mode(tokens)
|
|
118
|
+
when :counter then build_element('wa-counter', *CounterAttributes.build(date, tokens))
|
|
119
|
+
when :relative then build_relative_time(date, tokens)
|
|
120
|
+
else build_format_date(date, tokens)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def inline_mode(tokens)
|
|
125
|
+
return :counter if tokens.intersect?(COUNTER_MODE_TOKENS)
|
|
106
126
|
|
|
107
|
-
|
|
127
|
+
tokens.include?('relative') ? :relative : :absolute
|
|
108
128
|
end
|
|
109
129
|
|
|
110
130
|
# Pull the first date/datetime token out, leaving the option tokens.
|
|
@@ -116,7 +136,7 @@ module Markawesome
|
|
|
116
136
|
end
|
|
117
137
|
|
|
118
138
|
def extract_date(token_string)
|
|
119
|
-
tokens = token_string
|
|
139
|
+
tokens = quoted_tokens(token_string)
|
|
120
140
|
tokens.find { |token| token.match?(DATE_TOKEN_REGEX) }.to_s
|
|
121
141
|
end
|
|
122
142
|
|
|
@@ -194,10 +214,10 @@ module Markawesome
|
|
|
194
214
|
opts
|
|
195
215
|
end
|
|
196
216
|
|
|
197
|
-
def build_element(tag, parts)
|
|
198
|
-
return "<#{tag}
|
|
217
|
+
def build_element(tag, parts, inner = '')
|
|
218
|
+
return "<#{tag}>#{inner}</#{tag}>" if parts.empty?
|
|
199
219
|
|
|
200
|
-
"<#{tag} #{parts.join(' ')}
|
|
220
|
+
"<#{tag} #{parts.join(' ')}>#{inner}</#{tag}>"
|
|
201
221
|
end
|
|
202
222
|
|
|
203
223
|
def escape_html(text)
|
|
@@ -60,6 +60,14 @@ module Markawesome
|
|
|
60
60
|
apply_multiple_patterns(content, patterns)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
# Grid classes and inline styles for a params string, using the same rules
|
|
64
|
+
# as `::::grid` (gap:, align:, justify:, min:). Unknown tokens are ignored.
|
|
65
|
+
# Shared with LightboxTransformer, whose gallery is itself a wa-grid.
|
|
66
|
+
# @return [Array(Array<String>, Array<String>)] [classes, styles]
|
|
67
|
+
def self.grid_attributes(params_string)
|
|
68
|
+
build_attributes('grid', params_string)
|
|
69
|
+
end
|
|
70
|
+
|
|
63
71
|
class << self
|
|
64
72
|
private
|
|
65
73
|
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base_transformer'
|
|
4
|
+
require_relative 'layout_transformer'
|
|
5
|
+
|
|
6
|
+
module Markawesome
|
|
7
|
+
# Transforms a fenced list of Markdown images into a <wa-lightbox> gallery:
|
|
8
|
+
# a grid of thumbnails that open a full-screen viewer.
|
|
9
|
+
#
|
|
10
|
+
# Primary syntax:
|
|
11
|
+
# ::::lightbox loop thumbnails min:10rem gap:s label:"Lisbon trip"
|
|
12
|
+
# 
|
|
13
|
+
# [](bridge-full.jpg "25 de Abril")
|
|
14
|
+
# [](drone.mp4 "From above")
|
|
15
|
+
# ::::
|
|
16
|
+
# Alternative syntax: :::wa-lightbox params? ...same body... :::
|
|
17
|
+
#
|
|
18
|
+
# Items (one per line):
|
|
19
|
+
#  -> item src = thumbnail = src
|
|
20
|
+
# [](href "caption") -> thumbnail = thumb, item src = href
|
|
21
|
+
# The caption is the link title, else the image title. An item src ending in
|
|
22
|
+
# a video extension (.mp4 .webm .mov .ogv .m4v, ignoring ?query/#hash) gets
|
|
23
|
+
# type="video". Blank lines are ignored; any other line is rendered as
|
|
24
|
+
# Markdown and kept in place (never silently dropped).
|
|
25
|
+
#
|
|
26
|
+
# Params:
|
|
27
|
+
# loop, thumbnails (-> with-thumbnails), no-nav (-> without-navigation),
|
|
28
|
+
# no-counter (-> without-counter), no-zoom (-> without-zoom), label:"…"
|
|
29
|
+
# square (default), landscape (4 / 3), portrait (3 / 4), natural (no crop)
|
|
30
|
+
# gap:, min:, align:, justify: — the ::::grid rules (the gallery is a wa-grid)
|
|
31
|
+
#
|
|
32
|
+
# Runs first in the pipeline (with divider/stepper) so its `::::` closer is
|
|
33
|
+
# consumed before LayoutTransformer — a lightbox can sit inside a ::::grid,
|
|
34
|
+
# a card or a tab — and so the optional ImageDialogTransformer never sees the
|
|
35
|
+
# gallery's images (they are already <img> HTML).
|
|
36
|
+
class LightboxTransformer < BaseTransformer
|
|
37
|
+
# wa-lightbox-item `type` values: image is implicit (WA's default), video is
|
|
38
|
+
# emitted for video sources.
|
|
39
|
+
LIGHTBOX_ITEM_TYPES = %w[image video].freeze
|
|
40
|
+
|
|
41
|
+
FLAGS = {
|
|
42
|
+
'loop' => 'loop',
|
|
43
|
+
'thumbnails' => 'with-thumbnails',
|
|
44
|
+
'no-nav' => 'without-navigation',
|
|
45
|
+
'no-counter' => 'without-counter',
|
|
46
|
+
'no-zoom' => 'without-zoom'
|
|
47
|
+
}.freeze
|
|
48
|
+
|
|
49
|
+
# Emission order for boolean attributes (after class, style, label).
|
|
50
|
+
FLAG_ORDER = %w[loop with-thumbnails without-navigation without-counter without-zoom].freeze
|
|
51
|
+
|
|
52
|
+
THUMBNAIL_RADIUS = 'border-radius: var(--wa-border-radius-m)'
|
|
53
|
+
THUMBNAIL_STYLES = {
|
|
54
|
+
'square' => "width: 100%; aspect-ratio: 1; object-fit: cover; #{THUMBNAIL_RADIUS}",
|
|
55
|
+
'landscape' => "width: 100%; aspect-ratio: 4 / 3; object-fit: cover; #{THUMBNAIL_RADIUS}",
|
|
56
|
+
'portrait' => "width: 100%; aspect-ratio: 3 / 4; object-fit: cover; #{THUMBNAIL_RADIUS}",
|
|
57
|
+
'natural' => "width: 100%; height: auto; #{THUMBNAIL_RADIUS}"
|
|
58
|
+
}.freeze
|
|
59
|
+
|
|
60
|
+
PRIMARY_REGEX = /^::::lightbox(?:[ \t]+([^\n]*))?\n(.*?)\n::::[ \t]*$/m
|
|
61
|
+
ALTERNATIVE_REGEX = /^:::wa-lightbox(?:[ \t]+([^\n]*))?\n(.*?)\n:::[ \t]*$/m
|
|
62
|
+
LABEL_REGEX = /label:(?:"([^"]*)"|'([^']*)')/
|
|
63
|
+
IMAGE_REGEX = /\A!\[([^\]]*)\]\(([^)\s]+)(?:[ \t]+"([^"]*)")?\)\z/
|
|
64
|
+
LINKED_IMAGE_REGEX =
|
|
65
|
+
/\A\[!\[([^\]]*)\]\(([^)\s]+)(?:[ \t]+"([^"]*)")?\)\]\(([^)\s]+)(?:[ \t]+"([^"]*)")?\)\z/
|
|
66
|
+
VIDEO_REGEX = /\.(?:mp4|webm|mov|ogv|m4v)(?:[?#].*)?\z/i
|
|
67
|
+
|
|
68
|
+
def self.transform(content)
|
|
69
|
+
transform_proc = proc do |params_string, body|
|
|
70
|
+
build_lightbox(params_string.to_s, body)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
|
|
74
|
+
apply_multiple_patterns(content, patterns)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Plain-Markdown degrade: the image lines are already valid GFM, so keep the
|
|
78
|
+
# body as written and drop the fence.
|
|
79
|
+
def self.render_as_markdown(content, _options = {})
|
|
80
|
+
transform_proc = proc { |_params_string, body| body.to_s }
|
|
81
|
+
patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
|
|
82
|
+
apply_multiple_patterns(content, patterns)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
class << self
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def build_lightbox(params_string, body)
|
|
89
|
+
label, params_string = extract_label(params_string)
|
|
90
|
+
tokens = params_string.split
|
|
91
|
+
classes, styles = LayoutTransformer.grid_attributes(params_string)
|
|
92
|
+
shape = tokens.reverse.find { |token| THUMBNAIL_STYLES.include?(token) } || 'square'
|
|
93
|
+
flags = tokens.filter_map { |token| FLAGS[token] }
|
|
94
|
+
|
|
95
|
+
attr_parts = ["class=\"#{escape_html(classes.join(' '))}\""]
|
|
96
|
+
attr_parts << "style=\"#{escape_html(styles.join('; '))}\"" unless styles.empty?
|
|
97
|
+
attr_parts << "label=\"#{escape_html(label)}\"" unless label.empty?
|
|
98
|
+
FLAG_ORDER.each { |flag| attr_parts << flag if flags.include?(flag) }
|
|
99
|
+
|
|
100
|
+
"<wa-lightbox #{attr_parts.join(' ')}>#{build_children(body, shape)}</wa-lightbox>"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Pull out label:"…" (or label:'…') before the bare tokens are read, so
|
|
104
|
+
# words inside the label can't be taken as flags.
|
|
105
|
+
def extract_label(params_string)
|
|
106
|
+
match = LABEL_REGEX.match(params_string)
|
|
107
|
+
return ['', params_string] unless match
|
|
108
|
+
|
|
109
|
+
[(match[1] || match[2]).strip, params_string.sub(LABEL_REGEX, ' ')]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Image lines become items; runs of other lines are rendered as Markdown
|
|
113
|
+
# in place. Blank lines only separate those runs.
|
|
114
|
+
def build_children(body, shape)
|
|
115
|
+
children = []
|
|
116
|
+
pending = []
|
|
117
|
+
flush = lambda do
|
|
118
|
+
text = pending.join("\n").strip
|
|
119
|
+
children << markdown_to_html(text).strip unless text.empty?
|
|
120
|
+
pending.clear
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
body.to_s.each_line do |line|
|
|
124
|
+
item = parse_item(line.strip)
|
|
125
|
+
if item
|
|
126
|
+
flush.call
|
|
127
|
+
children << build_item(item, shape)
|
|
128
|
+
else
|
|
129
|
+
pending << line.chomp
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
flush.call
|
|
133
|
+
children.join
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def parse_item(line)
|
|
137
|
+
if (match = LINKED_IMAGE_REGEX.match(line))
|
|
138
|
+
{ alt: match[1], thumb: match[2], src: match[4], caption: (match[5] || match[3]).to_s }
|
|
139
|
+
elsif (match = IMAGE_REGEX.match(line))
|
|
140
|
+
{ alt: match[1], thumb: match[2], src: match[2], caption: match[3].to_s }
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def build_item(item, shape)
|
|
145
|
+
item_parts = []
|
|
146
|
+
item_parts << 'type="video"' if item[:src].match?(VIDEO_REGEX)
|
|
147
|
+
item_parts << "src=\"#{escape_html(item[:src])}\""
|
|
148
|
+
item_parts << "caption=\"#{escape_html(item[:caption])}\"" unless item[:caption].empty?
|
|
149
|
+
|
|
150
|
+
img = "<img src=\"#{escape_html(item[:thumb])}\" alt=\"#{escape_html(item[:alt])}\" " \
|
|
151
|
+
"loading=\"lazy\" style=\"#{THUMBNAIL_STYLES[shape]}\">"
|
|
152
|
+
"<wa-lightbox-item #{item_parts.join(' ')}>#{img}</wa-lightbox-item>"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def escape_html(text)
|
|
156
|
+
text.to_s
|
|
157
|
+
.gsub('&', '&')
|
|
158
|
+
.gsub('<', '<')
|
|
159
|
+
.gsub('>', '>')
|
|
160
|
+
.gsub('"', '"')
|
|
161
|
+
.gsub("'", ''')
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base_transformer'
|
|
4
|
+
|
|
5
|
+
module Markawesome
|
|
6
|
+
# Transforms a number in the `[[[ … ]]]` syntax into a <wa-number-ticker>,
|
|
7
|
+
# which counts up to the value when it scrolls into view and formats it with
|
|
8
|
+
# Intl.NumberFormat.
|
|
9
|
+
#
|
|
10
|
+
# Inline (primary): [[[ <number> <tokens> ]]]
|
|
11
|
+
# Raised [[[ 18452.75 currency:EUR ]]] so far
|
|
12
|
+
# [[[ 87% ]]] satisfaction
|
|
13
|
+
# [[[ 12000 from:10000 duration:1500 ]]] users
|
|
14
|
+
# Block (alternative): :::wa-number-ticker <number> <tokens>
|
|
15
|
+
# followed by a closing ::: (empty body)
|
|
16
|
+
#
|
|
17
|
+
# The first token must be a number (-?digits, optional .digits, optional %).
|
|
18
|
+
# An ISO date never matches, so dates still reach DateTransformer, which runs
|
|
19
|
+
# right after this transformer.
|
|
20
|
+
#
|
|
21
|
+
# `87%` means type="percent" value="0.87": the decimal point is shifted two
|
|
22
|
+
# places textually (no floats). A bare number plus a `percent` token is
|
|
23
|
+
# type="percent" with the value as written.
|
|
24
|
+
#
|
|
25
|
+
# Tokens (later tokens win):
|
|
26
|
+
# currency:XXX -> type="currency" currency="XXX" (3 letters, upcased)
|
|
27
|
+
# percent, decimal -> type
|
|
28
|
+
# display:VALUE -> currency-display (symbol narrowSymbol code name)
|
|
29
|
+
# from:N -> start-value
|
|
30
|
+
# duration:MS, delay:MS -> integers
|
|
31
|
+
# easing:VALUE -> any CSS easing (quote it when it has spaces)
|
|
32
|
+
# eager -> eager
|
|
33
|
+
# no-grouping -> without-grouping
|
|
34
|
+
# decimals:N -> minimum- and maximum-fraction-digits (0–100)
|
|
35
|
+
# lang:/locale: -> lang
|
|
36
|
+
#
|
|
37
|
+
# Static-site caveat: the number renders into shadow DOM; without Web
|
|
38
|
+
# Awesome's JS nothing shows — same as <wa-format-date>.
|
|
39
|
+
class NumberTickerTransformer < BaseTransformer
|
|
40
|
+
TICKER_TYPES = %w[currency decimal percent].freeze
|
|
41
|
+
CURRENCY_DISPLAYS = %w[symbol narrowSymbol code name].freeze
|
|
42
|
+
|
|
43
|
+
NUMBER = '-?\d+(?:\.\d+)?%?'
|
|
44
|
+
INLINE_REGEX = /\[\[\[[ \t]*(#{NUMBER})(?:[ \t]+([^\]\r\n]*?))?[ \t]*\]\]\]/
|
|
45
|
+
ALTERNATIVE_REGEX = /^:::wa-number-ticker[ \t]+(#{NUMBER})(?:[ \t]+([^\n]*?))?[ \t]*\n:::$/
|
|
46
|
+
|
|
47
|
+
PLAIN_NUMBER_REGEX = /\A-?\d+(?:\.\d+)?\z/
|
|
48
|
+
INTEGER_REGEX = /\A\d+\z/
|
|
49
|
+
|
|
50
|
+
# Deterministic emission order (required for byte-for-byte parity).
|
|
51
|
+
ORDER = %w[value start-value duration delay easing eager type without-grouping currency
|
|
52
|
+
currency-display minimum-fraction-digits maximum-fraction-digits lang].freeze
|
|
53
|
+
|
|
54
|
+
def self.transform(content)
|
|
55
|
+
patterns = [INLINE_REGEX, ALTERNATIVE_REGEX].map do |regex|
|
|
56
|
+
{ regex: regex, block: proc { |_m, md| build_ticker(md[1], md[2]) } }
|
|
57
|
+
end
|
|
58
|
+
apply_multiple_patterns(content, patterns)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Plain-Markdown degrade: the number as written, plus the currency code.
|
|
62
|
+
def self.render_as_markdown(content, _options = {})
|
|
63
|
+
patterns = [INLINE_REGEX, ALTERNATIVE_REGEX].map do |regex|
|
|
64
|
+
{ regex: regex, block: proc { |_m, md| plain_number(md[1], md[2]) } }
|
|
65
|
+
end
|
|
66
|
+
apply_multiple_patterns(content, patterns)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class << self
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def build_ticker(number, token_string)
|
|
73
|
+
attrs = {}
|
|
74
|
+
if number.end_with?('%')
|
|
75
|
+
attrs['value'] = shift_percent(number.chomp('%'))
|
|
76
|
+
attrs['type'] = 'percent'
|
|
77
|
+
else
|
|
78
|
+
attrs['value'] = number
|
|
79
|
+
end
|
|
80
|
+
quoted_tokens(token_string).each { |token| apply_token(token, attrs) }
|
|
81
|
+
attrs.delete('type') if attrs['type'] == 'decimal'
|
|
82
|
+
|
|
83
|
+
parts = ORDER.filter_map do |key|
|
|
84
|
+
next unless attrs.include?(key)
|
|
85
|
+
|
|
86
|
+
attrs[key] == true ? key : "#{key}=\"#{escape_html(attrs[key])}\""
|
|
87
|
+
end
|
|
88
|
+
"<wa-number-ticker #{parts.join(' ')}></wa-number-ticker>"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def apply_token(token, attrs)
|
|
92
|
+
case token
|
|
93
|
+
when 'eager' then attrs['eager'] = true
|
|
94
|
+
when 'no-grouping' then attrs['without-grouping'] = true
|
|
95
|
+
when 'percent', 'decimal' then attrs['type'] = token
|
|
96
|
+
else
|
|
97
|
+
m = token.match(/\A([a-z-]+):(.+)\z/)
|
|
98
|
+
apply_option(m[1], unquote(m[2]), attrs) if m
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def apply_option(key, value, attrs)
|
|
103
|
+
case key
|
|
104
|
+
when 'currency'
|
|
105
|
+
if value.match?(/\A[A-Za-z]{3}\z/)
|
|
106
|
+
attrs['currency'] = value.upcase
|
|
107
|
+
attrs['type'] = 'currency'
|
|
108
|
+
end
|
|
109
|
+
when 'display' then attrs['currency-display'] = value if CURRENCY_DISPLAYS.include?(value)
|
|
110
|
+
when 'from' then attrs['start-value'] = value if value.match?(PLAIN_NUMBER_REGEX)
|
|
111
|
+
when 'duration', 'delay' then attrs[key] = value if value.match?(INTEGER_REGEX)
|
|
112
|
+
when 'easing' then attrs['easing'] = value
|
|
113
|
+
when 'decimals' then apply_decimals(value, attrs)
|
|
114
|
+
when 'lang', 'locale' then attrs['lang'] = value
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def apply_decimals(value, attrs)
|
|
119
|
+
return unless value.match?(INTEGER_REGEX) && value.to_i <= 100
|
|
120
|
+
|
|
121
|
+
attrs['minimum-fraction-digits'] = attrs['maximum-fraction-digits'] = value.to_i.to_s
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Divide a decimal string by 100 by moving the point two places left,
|
|
125
|
+
# textually: "87" -> "0.87", "12.5" -> "0.125", "150" -> "1.5", "5" -> "0.05".
|
|
126
|
+
def shift_percent(number)
|
|
127
|
+
negative = number.start_with?('-')
|
|
128
|
+
int_part, frac_part = number.delete_prefix('-').split('.', 2)
|
|
129
|
+
digits = "#{int_part}#{frac_part}"
|
|
130
|
+
point = int_part.length - 2
|
|
131
|
+
if point < 1
|
|
132
|
+
digits = ('0' * (1 - point)) + digits
|
|
133
|
+
point = 1
|
|
134
|
+
end
|
|
135
|
+
whole = digits[0...point].sub(/\A0+(?=\d)/, '')
|
|
136
|
+
fraction = digits[point..].sub(/0+\z/, '')
|
|
137
|
+
result = fraction.empty? ? whole : "#{whole}.#{fraction}"
|
|
138
|
+
negative && result != '0' ? "-#{result}" : result
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def plain_number(number, token_string)
|
|
142
|
+
currency = nil
|
|
143
|
+
quoted_tokens(token_string).each do |token|
|
|
144
|
+
m = token.match(/\Acurrency:([A-Za-z]{3})\z/)
|
|
145
|
+
currency = m[1].upcase if m
|
|
146
|
+
end
|
|
147
|
+
currency ? "#{number} #{currency}" : number
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def escape_html(text)
|
|
151
|
+
text.to_s
|
|
152
|
+
.gsub('&', '&')
|
|
153
|
+
.gsub('<', '<')
|
|
154
|
+
.gsub('>', '>')
|
|
155
|
+
.gsub('"', '"')
|
|
156
|
+
.gsub("'", ''')
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -19,6 +19,8 @@ require_relative 'transformers/divider_transformer'
|
|
|
19
19
|
require_relative 'transformers/icon_transformer'
|
|
20
20
|
require_relative 'transformers/image_dialog_transformer'
|
|
21
21
|
require_relative 'transformers/layout_transformer'
|
|
22
|
+
require_relative 'transformers/lightbox_transformer'
|
|
23
|
+
require_relative 'transformers/number_ticker_transformer'
|
|
22
24
|
require_relative 'transformers/popover_transformer'
|
|
23
25
|
require_relative 'transformers/random_content_transformer'
|
|
24
26
|
require_relative 'transformers/stepper_transformer'
|
data/lib/markawesome/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: markawesome
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.21.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Janne Waren
|
|
@@ -79,9 +79,11 @@ files:
|
|
|
79
79
|
- lib/markawesome.rb
|
|
80
80
|
- lib/markawesome/attribute_parser.rb
|
|
81
81
|
- lib/markawesome/code_block_protector.rb
|
|
82
|
+
- lib/markawesome/counter_attributes.rb
|
|
82
83
|
- lib/markawesome/icon_attributes.rb
|
|
83
84
|
- lib/markawesome/icon_slot_parser.rb
|
|
84
85
|
- lib/markawesome/plain_markdown_renderer.rb
|
|
86
|
+
- lib/markawesome/quoted_tokens.rb
|
|
85
87
|
- lib/markawesome/transformer.rb
|
|
86
88
|
- lib/markawesome/transformers.rb
|
|
87
89
|
- lib/markawesome/transformers/accordion_transformer.rb
|
|
@@ -100,6 +102,8 @@ files:
|
|
|
100
102
|
- lib/markawesome/transformers/icon_transformer.rb
|
|
101
103
|
- lib/markawesome/transformers/image_dialog_transformer.rb
|
|
102
104
|
- lib/markawesome/transformers/layout_transformer.rb
|
|
105
|
+
- lib/markawesome/transformers/lightbox_transformer.rb
|
|
106
|
+
- lib/markawesome/transformers/number_ticker_transformer.rb
|
|
103
107
|
- lib/markawesome/transformers/popover_transformer.rb
|
|
104
108
|
- lib/markawesome/transformers/random_content_transformer.rb
|
|
105
109
|
- lib/markawesome/transformers/stepper_transformer.rb
|