markawesome 0.19.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 +31 -0
- data/README.md +41 -0
- data/lib/markawesome/counter_attributes.rb +86 -0
- data/lib/markawesome/plain_markdown_renderer.rb +11 -3
- data/lib/markawesome/quoted_tokens.rb +23 -0
- data/lib/markawesome/transformer.rb +40 -34
- data/lib/markawesome/transformers/accordion_transformer.rb +1 -1
- data/lib/markawesome/transformers/base_transformer.rb +11 -0
- data/lib/markawesome/transformers/date_transformer.rb +34 -14
- data/lib/markawesome/transformers/divider_transformer.rb +125 -0
- 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/random_content_transformer.rb +1 -1
- data/lib/markawesome/transformers/stepper_transformer.rb +201 -0
- data/lib/markawesome/transformers.rb +4 -0
- data/lib/markawesome/version.rb +1 -1
- metadata +8 -2
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,37 @@ 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
|
+
|
|
27
|
+
## [0.20.0] - 2026-10-09
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
|
|
31
|
+
- **Labeled divider (`<wa-divider>` label, Web Awesome 3.13.0+).** New `DividerTransformer` with a mirrored one-line shorthand, `--- label ---`, and a `:::wa-divider params?` alternative whose body is the label (empty for a bare divider). Leading tokens are consumed from the left: `start`/`center`/`end` → `label-placement`, `horizontal`/`vertical` → `orientation`, `icon:name` → a `<wa-icon>` before the label. The rest is the label, rendered as inline Markdown. Output: `<wa-divider orientation="…" label-placement="…"><wa-icon name="…"></wa-icon> Label</wa-divider>`, attributes only when given. Plain `---` and `--- ---` are untouched and stay `<hr>`, and so is any line made only of `|`, `:`, `-` and whitespace, which keeps pipe-less GFM table separators such as `--- | ---` intact. The plain-Markdown renderer degrades it to `---` followed by the label as its own paragraph (icon dropped), with a leading blank line so the rule can't turn a preceding line into a setext heading. Known limitation, shared with every transformer: only backtick code fences are protected, so a `--- x ---` line inside a `~~~` fence is transformed.
|
|
32
|
+
- **Display-only stepper (`<wa-stepper>` / `<wa-step>`, Web Awesome 3.14.0, experimental).** New `StepperTransformer`: a `>>>>>> params?` fence (or `:::wa-stepper params?`) wrapping a Markdown task list. Each unindented list line is a step: `[x]`/`[X]` → `completed`; leading `active`, `loading`, `disabled`, a variant (`neutral`/`brand`/`success`/`warning`/`danger`), an attention (`none`/`pulse`/`bounce`) and `icon:name` (→ `slot="icon"`) are flags; the rest is the label as inline Markdown. Lines indented two or more spaces under a step are joined into `<span slot="description">`. Steps are named `step-1…N` and the container's `active` is the step flagged `active` (last wins), else the first incomplete step, else the last. Container params: `horizontal`/`vertical`/`auto` → `orientation`, `label:"…"` → `label` (HTML-escaped). Interactivity (`clickable`, `linear`, step invokers) is intentionally omitted — the steps have no content panels to navigate between. The `>>>>>>` fence never collides with the `^>>>$` separator of details/dialog/popover/tooltip/random-content. Plain-Markdown degrade: an ordered task list keeping `[x]`/`[ ]`, the current step in bold, and descriptions as indented continuation lines.
|
|
33
|
+
- Both run **first** in the pipeline (right after code-block protection, before layouts), so a divider or stepper can sit inside layouts, cards, callouts, tabs, accordion and dialog bodies — e.g. `--- vertical vs ---` between two buttons in a `::::cluster`. Same position in `PlainMarkdownRenderer::PIPELINE` (new `:divider` / `:stepper` override keys).
|
|
34
|
+
- Manifest coverage now also guards `wa-divider` `orientation`/`label-placement`, `wa-stepper` `orientation` and `wa-step` `variant`/`attention` (`DIVIDER_ATTRIBUTES`, `STEPPER_ATTRIBUTES`, `STEP_ATTRIBUTES`).
|
|
35
|
+
|
|
36
|
+
### Internal
|
|
37
|
+
|
|
38
|
+
- **Refreshed the Web Awesome manifest fixture to 3.14.0** (September 24th, 2026; covers 3.13.0 too). `rake wa:manifest[3.14.0]` regenerated `spec/fixtures/webawesome-enums.json` and `WA_VERSION` moved to `3.14.0`. The diff is **purely additive** — three new tags (`wa-step`, `wa-stepper`, `wa-tag-input`) and a `label-placement` enum on `wa-divider` — with **zero DRIFT and zero GAP** against the enums we accept, so no transformer constant changed. `<wa-accordion>` and `<wa-random-content>` are now **stable** in Web Awesome, so the "experimental" wording was dropped from their transformer comments. Fixture remains byte-identical to `markawesome-js`'s copy. Example-site CDN pins bumped to 3.14.0 in `jekyll-webawesome` and `eleventy-plugin-webawesome`.
|
|
39
|
+
|
|
9
40
|
## [0.19.0] - 2026-08-26
|
|
10
41
|
|
|
11
42
|
### Fixed
|
data/README.md
CHANGED
|
@@ -23,11 +23,16 @@ 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 |
|
|
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>` |
|
|
28
30
|
| **Icon** | `$$$icon-name` | `:::wa-icon icon-name` | `<wa-icon name="icon-name"></wa-icon>` |
|
|
29
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>` |
|
|
30
34
|
| **Popover** | `&&¶ms?` or inline `&&&trigger >>> content&&&` | `:::wa-popover params?` | `<wa-popover>` with trigger button and content |
|
|
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>` |
|
|
31
36
|
| **Tab Group** | `++++++` | `:::wa-tabs` | `<wa-tab-group><wa-tab>content</wa-tab></wa-tab-group>` |
|
|
32
37
|
| **Tag** | `@@@brand` | `:::wa-tag brand` | `<wa-tag variant="brand">content</wa-tag>` |
|
|
33
38
|
|
|
@@ -38,6 +43,42 @@ Used as the transformation engine for the [jekyll-webawesome](https://github.com
|
|
|
38
43
|
> listener (e.g. a single global handler that removes `event.target`). The markup
|
|
39
44
|
> we emit is valid either way.
|
|
40
45
|
|
|
46
|
+
> **Labeled divider:** `--- label ---` needs text between the mirrored dashes,
|
|
47
|
+
> so a plain `---` stays an `<hr>`. Leading `start`/`center`/`end`
|
|
48
|
+
> (label placement), `horizontal`/`vertical` (orientation) and `icon:name`
|
|
49
|
+
> tokens are consumed from the left; the rest is the label, as inline Markdown.
|
|
50
|
+
> A line made only of `|`, `:` and `-` (a pipe-less table separator) is left
|
|
51
|
+
> alone. Use `:::wa-divider` when a label must start with one of those words.
|
|
52
|
+
|
|
53
|
+
> **Stepper (display-only):** each unindented list line is a step. `[x]` marks
|
|
54
|
+
> it completed; leading `active`, `loading`, `disabled`, a variant, an attention
|
|
55
|
+
> (`pulse`/`bounce`/`none`) and `icon:name` are flags; lines indented under a
|
|
56
|
+
> step become its description. The active step is the one flagged `active`,
|
|
57
|
+
> else the first incomplete step, else the last. Container params are
|
|
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.
|
|
81
|
+
|
|
41
82
|
## Layout Utilities
|
|
42
83
|
|
|
43
84
|
Layout utilities use `::::` (quadruple colon) syntax to wrap content in CSS layout containers. Inner content is not markdown-converted, so component `:::` syntax inside layouts works normally.
|
|
@@ -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
|
|
@@ -25,9 +25,9 @@ module Markawesome
|
|
|
25
25
|
# component without forking the gem.
|
|
26
26
|
#
|
|
27
27
|
# @param component [Symbol] one of :accordion, :callout, :badge, :button,
|
|
28
|
-
# :card, :carousel, :comparison, :copy_button, :details, :dialog,
|
|
29
|
-
# :image_dialog, :layout, :
|
|
30
|
-
# :tooltip.
|
|
28
|
+
# :card, :carousel, :comparison, :copy_button, :details, :dialog,
|
|
29
|
+
# :divider, :icon, :image_dialog, :layout, :lightbox, :number_ticker, :popover, :random_content,
|
|
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
|
|
33
33
|
# syntax replaced.
|
|
@@ -44,9 +44,13 @@ module Markawesome
|
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
PIPELINE = %i[
|
|
47
|
+
divider
|
|
48
|
+
stepper
|
|
49
|
+
lightbox
|
|
47
50
|
layout
|
|
48
51
|
popover
|
|
49
52
|
tooltip
|
|
53
|
+
number_ticker
|
|
50
54
|
date
|
|
51
55
|
badge
|
|
52
56
|
button
|
|
@@ -68,9 +72,13 @@ module Markawesome
|
|
|
68
72
|
].freeze
|
|
69
73
|
|
|
70
74
|
TRANSFORMER_MAP = {
|
|
75
|
+
divider: DividerTransformer,
|
|
76
|
+
stepper: StepperTransformer,
|
|
77
|
+
lightbox: LightboxTransformer,
|
|
71
78
|
layout: LayoutTransformer,
|
|
72
79
|
popover: PopoverTransformer,
|
|
73
80
|
tooltip: TooltipTransformer,
|
|
81
|
+
number_ticker: NumberTickerTransformer,
|
|
74
82
|
date: DateTransformer,
|
|
75
83
|
badge: BadgeTransformer,
|
|
76
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,48 +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
|
-
content =
|
|
14
|
-
|
|
15
|
-
content = TooltipTransformer.transform(content)
|
|
16
|
-
content = DateTransformer.transform(content)
|
|
17
|
-
content = BadgeTransformer.transform(content)
|
|
18
|
-
content = ButtonTransformer.transform(content)
|
|
19
|
-
content = CalloutTransformer.transform(content)
|
|
20
|
-
content = CardTransformer.transform(content)
|
|
21
|
-
content = CarouselTransformer.transform(content)
|
|
22
|
-
content = ComparisonTransformer.transform(content)
|
|
23
|
-
content = VideoTransformer.transform(content)
|
|
24
|
-
content = CopyButtonTransformer.transform(content)
|
|
25
|
-
content = DetailsTransformer.transform(content)
|
|
26
|
-
|
|
27
|
-
# Apply image dialog transformer BEFORE dialog transformer if enabled
|
|
50
|
+
content = BEFORE_IMAGE_DIALOG.reduce(content) { |acc, transformer| transformer.transform(acc) }
|
|
51
|
+
|
|
28
52
|
if options[:image_dialog]
|
|
29
53
|
config = options[:image_dialog].is_a?(Hash) ? options[:image_dialog] : {}
|
|
30
54
|
content = ImageDialogTransformer.transform(content, config)
|
|
31
55
|
end
|
|
32
56
|
|
|
33
|
-
content =
|
|
34
|
-
content = IconTransformer.transform(content)
|
|
35
|
-
content = TagTransformer.transform(content)
|
|
36
|
-
content = TabsTransformer.transform(content)
|
|
37
|
-
|
|
38
|
-
# Accordion runs last so item bodies may contain other already-transformed
|
|
39
|
-
# components (same reason tabs runs near the end).
|
|
40
|
-
content = AccordionTransformer.transform(content)
|
|
41
|
-
|
|
42
|
-
# Tree runs after accordion; its body is a plain nested list (never a
|
|
43
|
-
# markdown-converted body), so ordering relative to accordion is moot.
|
|
44
|
-
content = TreeTransformer.transform(content)
|
|
45
|
-
|
|
46
|
-
# Random-content runs LAST: its options routinely wrap already-transformed
|
|
47
|
-
# components (callouts/badges/cards), and dialog/details/popover/tooltip
|
|
48
|
-
# must consume their own `>>>` upstream before our `^>>>$` item split
|
|
49
|
-
# runs. Consequence: a `......` block is the outermost container and cannot
|
|
50
|
-
# be nested inside another container body (same constraint Tree documents).
|
|
51
|
-
content = RandomContentTransformer.transform(content)
|
|
57
|
+
content = AFTER_IMAGE_DIALOG.reduce(content) { |acc, transformer| transformer.transform(acc) }
|
|
52
58
|
|
|
53
59
|
CodeBlockProtector.restore(content, tokens)
|
|
54
60
|
end
|
|
@@ -23,7 +23,7 @@ module Markawesome
|
|
|
23
23
|
# - expanded -> expanded; disabled -> disabled
|
|
24
24
|
# - icon:name -> <wa-icon slot="icon" name="name"></wa-icon> as first child
|
|
25
25
|
#
|
|
26
|
-
# wa-accordion
|
|
26
|
+
# wa-accordion was added in Web Awesome 3.7 (stable since 3.13). expanded/disabled
|
|
27
27
|
# and the mode/appearance/icon-placement attributes are all static-safe.
|
|
28
28
|
class AccordionTransformer < BaseTransformer
|
|
29
29
|
CONTAINER_ATTRIBUTES = {
|
|
@@ -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)
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base_transformer'
|
|
4
|
+
require_relative '../attribute_parser'
|
|
5
|
+
require_relative '../icon_slot_parser'
|
|
6
|
+
|
|
7
|
+
module Markawesome
|
|
8
|
+
# Transforms labeled-divider syntax into <wa-divider> elements with a label in
|
|
9
|
+
# the default slot (Web Awesome 3.13.0+).
|
|
10
|
+
#
|
|
11
|
+
# Primary syntax (one line, mirrored dashes):
|
|
12
|
+
# --- or ---
|
|
13
|
+
# --- start Release notes ---
|
|
14
|
+
# --- end icon:star New ---
|
|
15
|
+
# --- icon:quote-left ---
|
|
16
|
+
# --- vertical vs ---
|
|
17
|
+
# Alternative syntax (the body is the label, and may be empty):
|
|
18
|
+
# :::wa-divider start icon:star
|
|
19
|
+
# Release **notes**
|
|
20
|
+
# :::
|
|
21
|
+
#
|
|
22
|
+
# Leading tokens (consumed from the left until the first non-token word):
|
|
23
|
+
# - start, center, end -> label-placement
|
|
24
|
+
# - horizontal, vertical -> orientation
|
|
25
|
+
# - icon:name -> <wa-icon name="name"> before the label
|
|
26
|
+
# The rest of the line is the label, rendered as inline Markdown. To start a
|
|
27
|
+
# label with one of those words, use the alternative syntax.
|
|
28
|
+
#
|
|
29
|
+
# A plain `---` (and `--- ---`) is left alone and stays an <hr>. So is a line
|
|
30
|
+
# made only of `|`, `:`, `-` and whitespace (e.g. a pipe-less GFM table
|
|
31
|
+
# separator such as `--- | ---`).
|
|
32
|
+
class DividerTransformer < BaseTransformer
|
|
33
|
+
DIVIDER_ATTRIBUTES = {
|
|
34
|
+
orientation: %w[horizontal vertical],
|
|
35
|
+
label_placement: %w[start center end]
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
# Content slot: emits <wa-icon name="..."> with no slot attribute.
|
|
39
|
+
ICON_SLOTS = {
|
|
40
|
+
default: 'content',
|
|
41
|
+
slots: %w[content],
|
|
42
|
+
slot_map: { 'content' => 'content' }
|
|
43
|
+
}.freeze
|
|
44
|
+
|
|
45
|
+
PRIMARY_REGEX = /^---[ \t]+(\S[^\n]*?)[ \t]+---[ \t]*$/
|
|
46
|
+
ALTERNATIVE_REGEX = /^:::wa-divider[ \t]*([^\n]*)\n(?:(.*?)\n)?:::[ \t]*$/m
|
|
47
|
+
TABLE_SEPARATOR_REGEX = /\A[|:\-\s]*\z/
|
|
48
|
+
|
|
49
|
+
def self.transform(content)
|
|
50
|
+
content = content.gsub(PRIMARY_REGEX) do |match|
|
|
51
|
+
inner = Regexp.last_match(1)
|
|
52
|
+
next match if inner.match?(TABLE_SEPARATOR_REGEX)
|
|
53
|
+
|
|
54
|
+
lead, label = split_leading_tokens(inner)
|
|
55
|
+
build_html(lead, label)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
content.gsub(ALTERNATIVE_REGEX) do
|
|
59
|
+
params_string = Regexp.last_match(1)
|
|
60
|
+
body = Regexp.last_match(2).to_s
|
|
61
|
+
build_html(params_string, body.strip)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Degrade to a thematic break followed by the label as its own paragraph
|
|
66
|
+
# (icon dropped). The leading blank line stops `---` from turning a
|
|
67
|
+
# preceding paragraph line into a setext heading.
|
|
68
|
+
def self.render_as_markdown(content, _options = {})
|
|
69
|
+
content = content.gsub(PRIMARY_REGEX) do |match|
|
|
70
|
+
inner = Regexp.last_match(1)
|
|
71
|
+
next match if inner.match?(TABLE_SEPARATOR_REGEX)
|
|
72
|
+
|
|
73
|
+
_lead, label = split_leading_tokens(inner)
|
|
74
|
+
markdown_break(label)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
content.gsub(ALTERNATIVE_REGEX) do
|
|
78
|
+
markdown_break(Regexp.last_match(2).to_s.strip)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
class << self
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
# Consume leading placement/orientation/icon tokens; the rest is the label.
|
|
86
|
+
def split_leading_tokens(inner)
|
|
87
|
+
tokens = inner.strip.split(/\s+/)
|
|
88
|
+
lead = []
|
|
89
|
+
lead << tokens.shift while tokens.any? && leading_token?(tokens.first)
|
|
90
|
+
[lead.join(' '), tokens.join(' ')]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def leading_token?(token)
|
|
94
|
+
token.start_with?('icon:') || DIVIDER_ATTRIBUTES.values.any? { |values| values.include?(token) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def build_html(params_string, label)
|
|
98
|
+
attributes = AttributeParser.parse(params_string, DIVIDER_ATTRIBUTES)
|
|
99
|
+
icon_result = IconSlotParser.parse(params_string, ICON_SLOTS)
|
|
100
|
+
|
|
101
|
+
attr_parts = []
|
|
102
|
+
attr_parts << "orientation=\"#{attributes[:orientation]}\"" if attributes[:orientation]
|
|
103
|
+
attr_parts << "label-placement=\"#{attributes[:label_placement]}\"" if attributes[:label_placement]
|
|
104
|
+
attrs_string = attr_parts.empty? ? '' : " #{attr_parts.join(' ')}"
|
|
105
|
+
|
|
106
|
+
icon_html = IconSlotParser.to_html(icon_result[:icons], ICON_SLOTS[:slot_map])
|
|
107
|
+
label_html = render_inline(label)
|
|
108
|
+
separator = !icon_html.empty? && !label_html.empty? ? ' ' : ''
|
|
109
|
+
|
|
110
|
+
"<wa-divider#{attrs_string}>#{icon_html}#{separator}#{label_html}</wa-divider>"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Inline Markdown: render, then drop the wrapping <p> (the badge idiom).
|
|
114
|
+
def render_inline(text)
|
|
115
|
+
return '' if text.empty?
|
|
116
|
+
|
|
117
|
+
markdown_to_html(text).strip.sub(%r{\A<p>(.*)</p>\z}m, '\1')
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def markdown_break(label)
|
|
121
|
+
label.empty? ? "\n---" : "\n---\n\n#{label}\n"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -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
|
|
@@ -4,7 +4,7 @@ require_relative 'base_transformer'
|
|
|
4
4
|
require_relative '../attribute_parser'
|
|
5
5
|
|
|
6
6
|
module Markawesome
|
|
7
|
-
# Transforms random-content syntax into Web Awesome's
|
|
7
|
+
# Transforms random-content syntax into Web Awesome's
|
|
8
8
|
# <wa-random-content> element, which shows one or more of its direct element
|
|
9
9
|
# children at random (optionally rotating them) and hides the rest — all in
|
|
10
10
|
# WA's own runtime, so the author writes zero JavaScript. Ideal for a "little
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base_transformer'
|
|
4
|
+
require_relative '../attribute_parser'
|
|
5
|
+
require_relative '../icon_slot_parser'
|
|
6
|
+
|
|
7
|
+
module Markawesome
|
|
8
|
+
# Transforms stepper syntax into a display-only <wa-stepper> / <wa-step>
|
|
9
|
+
# progress indicator, written as a Markdown task list.
|
|
10
|
+
#
|
|
11
|
+
# Primary syntax:
|
|
12
|
+
# >>>>>> vertical label:"Checkout progress"
|
|
13
|
+
# - [x] Cart
|
|
14
|
+
# - [x] Shipping
|
|
15
|
+
# Standard, 3–5 days
|
|
16
|
+
# - [ ] pulse icon:credit-card Payment
|
|
17
|
+
# - [ ] Review
|
|
18
|
+
# >>>>>>
|
|
19
|
+
# Alternative syntax: :::wa-stepper params? ...same list... :::
|
|
20
|
+
#
|
|
21
|
+
# Container params:
|
|
22
|
+
# - horizontal, vertical, auto -> orientation
|
|
23
|
+
# - label:"text" -> label (accessible name)
|
|
24
|
+
# Steps are unindented list lines (`-`, `*` or `+`):
|
|
25
|
+
# - [x] / [X] marks the step completed; [ ] or no box means not completed
|
|
26
|
+
# - leading flags: active, loading, disabled, a variant (neutral, brand,
|
|
27
|
+
# success, warning, danger), an attention (none, pulse, bounce), and
|
|
28
|
+
# icon:name (slotted into the step's icon slot)
|
|
29
|
+
# - the rest of the line is the label, rendered as inline Markdown
|
|
30
|
+
# - lines indented by two or more spaces under a step are joined into its
|
|
31
|
+
# description slot, rendered as inline Markdown
|
|
32
|
+
#
|
|
33
|
+
# Steps are named step-1..N and the container's `active` points at the step
|
|
34
|
+
# flagged `active` (last one wins), else the first incomplete step, else the
|
|
35
|
+
# last step. Interactivity (`clickable`, `linear`, step invokers) is omitted
|
|
36
|
+
# on purpose: these steps have no content panels, so there is nothing to
|
|
37
|
+
# navigate between without app JavaScript.
|
|
38
|
+
class StepperTransformer < BaseTransformer
|
|
39
|
+
STEPPER_ATTRIBUTES = {
|
|
40
|
+
orientation: %w[horizontal vertical auto]
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
STEP_ATTRIBUTES = {
|
|
44
|
+
variant: %w[neutral brand success warning danger],
|
|
45
|
+
attention: %w[none pulse bounce]
|
|
46
|
+
}.freeze
|
|
47
|
+
|
|
48
|
+
STEP_FLAGS = %w[active loading disabled].freeze
|
|
49
|
+
|
|
50
|
+
ICON_SLOTS = {
|
|
51
|
+
default: 'icon',
|
|
52
|
+
slots: %w[icon],
|
|
53
|
+
slot_map: { 'icon' => 'icon' }
|
|
54
|
+
}.freeze
|
|
55
|
+
|
|
56
|
+
PRIMARY_REGEX = /^>{6}([^\n]*)\n(.*?)\n>{6}[ \t]*$/m
|
|
57
|
+
ALTERNATIVE_REGEX = /^:::wa-stepper[ \t]*([^\n]*)\n(.*?)\n:::[ \t]*$/m
|
|
58
|
+
LABEL_REGEX = /label:(?:"([^"]*)"|'([^']*)')/
|
|
59
|
+
STEP_LINE_REGEX = /\A[-*+][ \t]+(.*)\z/
|
|
60
|
+
CONTINUATION_REGEX = /\A[ \t]{2,}(\S.*)\z/
|
|
61
|
+
CHECKBOX_REGEX = /\A\[([ xX])\](?:[ \t]+(.*))?\z/
|
|
62
|
+
|
|
63
|
+
def self.transform(content)
|
|
64
|
+
transform_proc = proc do |params_string, body|
|
|
65
|
+
label, params_string = extract_label(params_string.to_s)
|
|
66
|
+
attributes = AttributeParser.parse(params_string, STEPPER_ATTRIBUTES)
|
|
67
|
+
steps = parse_steps(body)
|
|
68
|
+
active = active_index(steps)
|
|
69
|
+
|
|
70
|
+
attr_parts = []
|
|
71
|
+
attr_parts << "orientation=\"#{attributes[:orientation]}\"" if attributes[:orientation]
|
|
72
|
+
attr_parts << "label=\"#{escape_html(label)}\"" unless label.empty?
|
|
73
|
+
attr_parts << "active=\"step-#{active + 1}\"" if active
|
|
74
|
+
attrs_string = attr_parts.empty? ? '' : " #{attr_parts.join(' ')}"
|
|
75
|
+
|
|
76
|
+
steps_html = steps.each_with_index.map { |step, index| build_step(step, index) }.join
|
|
77
|
+
"<wa-stepper#{attrs_string}>#{steps_html}</wa-stepper>"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
|
|
81
|
+
apply_multiple_patterns(content, patterns)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Degrade to an ordered task list: completed steps keep [x], the current
|
|
85
|
+
# step is bold, and a description becomes an indented continuation line.
|
|
86
|
+
def self.render_as_markdown(content, _options = {})
|
|
87
|
+
transform_proc = proc do |_params_string, body|
|
|
88
|
+
steps = parse_steps(body)
|
|
89
|
+
active = active_index(steps)
|
|
90
|
+
|
|
91
|
+
steps.each_with_index.map do |step, index|
|
|
92
|
+
marker = "#{index + 1}. "
|
|
93
|
+
label = index == active && !step[:label].empty? ? "**#{step[:label]}**" : step[:label]
|
|
94
|
+
line = "#{marker}[#{step[:completed] ? 'x' : ' '}]"
|
|
95
|
+
line += " #{label}" unless label.empty?
|
|
96
|
+
line += "\n#{' ' * marker.length}#{step[:description]}" unless step[:description].empty?
|
|
97
|
+
line
|
|
98
|
+
end.join("\n")
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
|
|
102
|
+
apply_multiple_patterns(content, patterns)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
class << self
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
# Pull out label:"…" (or label:'…') before the bare tokens are parsed, so
|
|
109
|
+
# words inside the label can't be read as orientation tokens.
|
|
110
|
+
def extract_label(params_string)
|
|
111
|
+
match = LABEL_REGEX.match(params_string)
|
|
112
|
+
return ['', params_string] unless match
|
|
113
|
+
|
|
114
|
+
[(match[1] || match[2]).strip, params_string.sub(LABEL_REGEX, ' ')]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def parse_steps(body)
|
|
118
|
+
steps = []
|
|
119
|
+
body.to_s.each_line do |line|
|
|
120
|
+
line = line.chomp
|
|
121
|
+
if (match = STEP_LINE_REGEX.match(line))
|
|
122
|
+
steps << parse_step(match[1])
|
|
123
|
+
elsif (match = CONTINUATION_REGEX.match(line)) && steps.any?
|
|
124
|
+
steps.last[:description_lines] << match[1].strip
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
steps.each { |step| step[:description] = step.delete(:description_lines).join(' ') }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def parse_step(raw)
|
|
131
|
+
completed = false
|
|
132
|
+
if (match = CHECKBOX_REGEX.match(raw.strip))
|
|
133
|
+
completed = match[1] != ' '
|
|
134
|
+
raw = match[2].to_s
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
tokens = raw.strip.split(/\s+/)
|
|
138
|
+
lead = []
|
|
139
|
+
lead << tokens.shift while tokens.any? && leading_token?(tokens.first)
|
|
140
|
+
lead_string = lead.join(' ')
|
|
141
|
+
|
|
142
|
+
{
|
|
143
|
+
completed: completed,
|
|
144
|
+
flags: lead & STEP_FLAGS,
|
|
145
|
+
attributes: AttributeParser.parse(lead_string, STEP_ATTRIBUTES),
|
|
146
|
+
icons: IconSlotParser.parse(lead_string, ICON_SLOTS)[:icons],
|
|
147
|
+
label: tokens.join(' '),
|
|
148
|
+
description_lines: []
|
|
149
|
+
}
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def leading_token?(token)
|
|
153
|
+
token.start_with?('icon:') ||
|
|
154
|
+
STEP_FLAGS.include?(token) ||
|
|
155
|
+
STEP_ATTRIBUTES.values.any? { |values| values.include?(token) }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The explicitly active step (last wins), else the first incomplete one,
|
|
159
|
+
# else the last step; nil when there are no steps.
|
|
160
|
+
def active_index(steps)
|
|
161
|
+
return nil if steps.empty?
|
|
162
|
+
|
|
163
|
+
explicit = steps.rindex { |step| step[:flags].include?('active') }
|
|
164
|
+
return explicit if explicit
|
|
165
|
+
|
|
166
|
+
steps.index { |step| !step[:completed] } || (steps.length - 1)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def build_step(step, index)
|
|
170
|
+
attr_parts = ["name=\"step-#{index + 1}\""]
|
|
171
|
+
attr_parts << 'completed' if step[:completed]
|
|
172
|
+
attr_parts << 'loading' if step[:flags].include?('loading')
|
|
173
|
+
attr_parts << 'disabled' if step[:flags].include?('disabled')
|
|
174
|
+
attr_parts << "variant=\"#{step[:attributes][:variant]}\"" if step[:attributes][:variant]
|
|
175
|
+
attr_parts << "attention=\"#{step[:attributes][:attention]}\"" if step[:attributes][:attention]
|
|
176
|
+
|
|
177
|
+
icon_html = IconSlotParser.to_html(step[:icons], ICON_SLOTS[:slot_map])
|
|
178
|
+
description = render_inline(step[:description])
|
|
179
|
+
description_html = description.empty? ? '' : "<span slot=\"description\">#{description}</span>"
|
|
180
|
+
|
|
181
|
+
"<wa-step #{attr_parts.join(' ')}>#{icon_html}#{render_inline(step[:label])}#{description_html}</wa-step>"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Inline Markdown: render, then drop the wrapping <p> (the badge idiom).
|
|
185
|
+
def render_inline(text)
|
|
186
|
+
return '' if text.empty?
|
|
187
|
+
|
|
188
|
+
markdown_to_html(text).strip.sub(%r{\A<p>(.*)</p>\z}m, '\1')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def escape_html(text)
|
|
192
|
+
text.to_s
|
|
193
|
+
.gsub('&', '&')
|
|
194
|
+
.gsub('<', '<')
|
|
195
|
+
.gsub('>', '>')
|
|
196
|
+
.gsub('"', '"')
|
|
197
|
+
.gsub("'", ''')
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
@@ -15,11 +15,15 @@ require_relative 'transformers/copy_button_transformer'
|
|
|
15
15
|
require_relative 'transformers/date_transformer'
|
|
16
16
|
require_relative 'transformers/details_transformer'
|
|
17
17
|
require_relative 'transformers/dialog_transformer'
|
|
18
|
+
require_relative 'transformers/divider_transformer'
|
|
18
19
|
require_relative 'transformers/icon_transformer'
|
|
19
20
|
require_relative 'transformers/image_dialog_transformer'
|
|
20
21
|
require_relative 'transformers/layout_transformer'
|
|
22
|
+
require_relative 'transformers/lightbox_transformer'
|
|
23
|
+
require_relative 'transformers/number_ticker_transformer'
|
|
21
24
|
require_relative 'transformers/popover_transformer'
|
|
22
25
|
require_relative 'transformers/random_content_transformer'
|
|
26
|
+
require_relative 'transformers/stepper_transformer'
|
|
23
27
|
require_relative 'transformers/tabs_transformer'
|
|
24
28
|
require_relative 'transformers/tag_transformer'
|
|
25
29
|
require_relative 'transformers/tooltip_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
|
|
@@ -96,11 +98,15 @@ files:
|
|
|
96
98
|
- lib/markawesome/transformers/date_transformer.rb
|
|
97
99
|
- lib/markawesome/transformers/details_transformer.rb
|
|
98
100
|
- lib/markawesome/transformers/dialog_transformer.rb
|
|
101
|
+
- lib/markawesome/transformers/divider_transformer.rb
|
|
99
102
|
- lib/markawesome/transformers/icon_transformer.rb
|
|
100
103
|
- lib/markawesome/transformers/image_dialog_transformer.rb
|
|
101
104
|
- lib/markawesome/transformers/layout_transformer.rb
|
|
105
|
+
- lib/markawesome/transformers/lightbox_transformer.rb
|
|
106
|
+
- lib/markawesome/transformers/number_ticker_transformer.rb
|
|
102
107
|
- lib/markawesome/transformers/popover_transformer.rb
|
|
103
108
|
- lib/markawesome/transformers/random_content_transformer.rb
|
|
109
|
+
- lib/markawesome/transformers/stepper_transformer.rb
|
|
104
110
|
- lib/markawesome/transformers/tabs_transformer.rb
|
|
105
111
|
- lib/markawesome/transformers/tag_transformer.rb
|
|
106
112
|
- lib/markawesome/transformers/tooltip_transformer.rb
|
|
@@ -130,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
130
136
|
- !ruby/object:Gem::Version
|
|
131
137
|
version: '0'
|
|
132
138
|
requirements: []
|
|
133
|
-
rubygems_version: 4.0.
|
|
139
|
+
rubygems_version: 4.0.20
|
|
134
140
|
specification_version: 4
|
|
135
141
|
summary: Framework-agnostic Markdown to Web Awesome component transformer
|
|
136
142
|
test_files: []
|