kramdown-components 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eab43cee2253b6f2cd009de176def3f27c69df88056b461a1d42547c30fff917
4
- data.tar.gz: 20c760a4faf27f89e553d5429ac8990c99c13e9cff5c0726b33630d10aea464b
3
+ metadata.gz: 88fc82100145b4d17743313e5975016be37fbee2887a415ac784d5443b42ef43
4
+ data.tar.gz: 423666e61c2b28eb8ed764a688cfb3c0ec4e260ca7932810f32dcf075ef28217
5
5
  SHA512:
6
- metadata.gz: e7a0d332581fda4f0eea4e02f163fcd9bab25181ca658baf5bb611788ef2a47b674d7d3b87b51b17680e28f76e36f2da9d5d62373c3b2b1861d84157bf022e9d
7
- data.tar.gz: 4434119b7a87d78be0ad03e49b427351066b02bb2e8261735a380d011f69cb39448bccca6eb9fdb2446ebb9afcd0bb14725ef9ee29d411027b91bdd5f1de73c7
6
+ metadata.gz: 83c176d9d2e3affcf7f33a128550fb9a8af84b2193c2a39cca6d704d790a8ddba3be314e82b84e96c6b995def86fe438ab7911a4707c4c71c8a7351c9af48e17
7
+ data.tar.gz: ef7a2f3d053ab444fb87044f0ffe26a3ca8f929b03f23b753a199f99a333def509aa9e6ff568ef317be04183d8633e76712aedd3cfbb47084c0f632da2f49b20
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Kramdown Components
2
2
 
3
- Rewrite sections of Markdown documents using HTML Custom Elements.
3
+ Rewrite and capture sections of Markdown documents using HTML Custom Elements.
4
4
 
5
- Useful for enhancing code blocks and formatted markup with more sophisticated controls while maintaining a clean separation of presentation and content.
5
+ Useful for enhancing code blocks and formatted markup with more sophisticated controls while maintaining a clean separation of presentation and content. Extract content from custom elements.
6
6
 
7
7
  ## How it Works
8
8
 
@@ -38,7 +38,7 @@ class ImageGallery < Kramdown::CustomElement; end
38
38
  Kramdown::CustomDocument.define_element("image-gallery", ImageGallery)
39
39
  ```
40
40
 
41
- ### DOM Rewriting
41
+ ### Rewriting the DOM
42
42
 
43
43
  To rewrite DOM subtrees wrapped by the component, implement the `parse_dom` method of `CustomElement` and manipulate the internal `root` and `children` elements, with instances of `Kramdown::Element` to represent each node.
44
44
 
@@ -59,7 +59,7 @@ end
59
59
  Kramdown::CustomDocument.define_element("button-list", ButtonList)
60
60
  ```
61
61
 
62
- When the `ButtonList#parse_dom` rewrite is applied, all child lists nested within the `<button-list>` component to have the content of each list item wrapped in a `<button>`.
62
+ When this `#parse_dom` hook is applied, all child lists nested within the `<button-list>` component get rewritten to have the content of each list item wrapped in a `<button>`.
63
63
 
64
64
  **Source:**
65
65
 
@@ -84,3 +84,73 @@ When the `ButtonList#parse_dom` rewrite is applied, all child lists nested withi
84
84
  </ul>
85
85
  </button-list>
86
86
  ```
87
+
88
+ ## Extracting Content from the DOM
89
+
90
+ It’s also possible to use `#parse_dom` to extract content from the document, with or without rewriting.
91
+
92
+ ```ruby
93
+ class Color
94
+ def self.from_hex(value)
95
+ r, g, b = value.match(/^#(..)(..)(..)$/).captures.map(&:hex)
96
+ new(r, g, b)
97
+ end
98
+
99
+ attr_reader :r, :g, :b
100
+
101
+ def initialize(r, g, b)
102
+ @r = r
103
+ @g = g
104
+ @b = b
105
+ end
106
+
107
+ def to_css
108
+ "rgb(#{r}, #{g}, #{b})"
109
+ end
110
+ end
111
+
112
+ class ColorSwatch < Kramdown::CustomElement
113
+ def parse_dom(root)
114
+ ul = root.children.find { |child_el| child_el.type == :ul }
115
+ @colors = ul.children.map do |li|
116
+ li.children.first.children.first.value
117
+ end
118
+ end
119
+
120
+ def to_a
121
+ @colors.map { |hex| Color.from_hex(hex) }
122
+ end
123
+ end
124
+
125
+ Kramdown::CustomDocument.define_element("color-swatch", ColorSwatch)
126
+ ```
127
+
128
+ **Source:**
129
+
130
+ ```md
131
+ Using the [HoneyGB palette](https://lospec.com/palette-list/honeygb):
132
+
133
+ <color-swatch>
134
+
135
+ - #3e3a42
136
+ - #877286
137
+ - #f0b695
138
+ - #e9f5da
139
+
140
+ </color-swatch>
141
+ ```
142
+
143
+ **Result:**
144
+
145
+ ```ruby
146
+ document = Kramdown::CustomDocument(source)
147
+
148
+ # Get a reference to the parsed `ColorSwatch` instance
149
+ color_swatch = document.custom_elements.first
150
+
151
+ # Use the custom `#to_a` method to get the first extracted color
152
+ color = color_swatch.to_a.first
153
+
154
+ # Render the color value as an RGB string
155
+ color.to_css
156
+ ```
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "kramdown-components"
3
- spec.version = "0.1.1"
3
+ spec.version = "0.2.0"
4
4
  spec.platform = Gem::Platform::RUBY
5
- spec.summary = "Rewrite sections of Markdown documents using HTML Custom Elements."
6
- spec.description = "Mix and match HTML and Markdown syntax. Generate nested DOM trees from a single top level element."
5
+ spec.summary = "Rewrite and capture sections of Markdown documents using HTML Custom Elements."
6
+ spec.description = "Mix and match HTML and Markdown syntax. Generate nested DOM trees from a single top level element. Extract content from custom elements."
7
7
  spec.authors = ["Mark Rickerby"]
8
8
  spec.email = "me@maetl.net"
9
9
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -13,15 +13,15 @@ module Kramdown
13
13
  @@custom_elements[tag_name] = element
14
14
  end
15
15
 
16
+ DEFAULTS = {
17
+ parse_block_html: true
18
+ }
19
+
16
20
  attr_reader :custom_elements
17
21
 
18
- def initialize(source)
22
+ def initialize(source, options={})
19
23
  @source = source
20
- @parsed_dom = Kramdown::Document.new(@source, {
21
- input: "GFM",
22
- parse_block_html: true,
23
- syntax_highlighter: "rouge"
24
- })
24
+ @parsed_dom = Kramdown::Document.new(@source, DEFAULTS.merge(options))
25
25
  @custom_elements = extract_custom_elements
26
26
  end
27
27
 
@@ -37,25 +37,12 @@ module Kramdown
37
37
  @parsed_dom.to_html
38
38
  end
39
39
 
40
- def has_js?
41
- !@custom_elements.empty?
42
- end
43
-
44
- def to_js
45
- bundle = ["const EXAMPLE_HANDLERS = {}"]
46
-
47
- @custom_elements.each do |element|
48
- bundle << element.to_js
49
- bundle << "EXAMPLE_HANDLERS[\"#{element.id}\"] = #{element.name}"
50
- end
51
-
52
- bundle.join("\n\n")
53
- end
54
-
55
40
  def method_missing(id, *attr, &block)
56
41
  @parsed_dom.send(id, attr, &block)
57
42
  end
58
43
 
44
+ private
45
+
59
46
  def generate_el_id(tagname)
60
47
  alphabet = [('a'..'z')].map(&:to_a).flatten
61
48
  tag_id = (0...12).map { alphabet[rand(alphabet.length)] }.join
@@ -77,21 +64,6 @@ module Kramdown
77
64
  element = custom_element_cls.new(outer_el.attr["id"])
78
65
  element.parse_dom(outer_el)
79
66
 
80
- # codeblocks = outer_el.children.filter { |child_el| child_el.type == :codeblock }
81
- #
82
- # outer_el.children = codeblocks.map do |codeblock|
83
- # wrapper = Kramdown::Element.new(
84
- # :html_element,
85
- # "example-script",
86
- # { label: LANG_LABELS[codeblock.options[:lang]] },
87
- # { content_model: :block }
88
- # )
89
- # wrapper.children << codeblock
90
- # wrapper
91
- # end
92
- #
93
- # outer_el.children.first.attr[:selected] = true
94
-
95
67
  elements << element
96
68
  end
97
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kramdown-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.11.0
55
55
  description: Mix and match HTML and Markdown syntax. Generate nested DOM trees from
56
- a single top level element.
56
+ a single top level element. Extract content from custom elements.
57
57
  email: me@maetl.net
58
58
  executables: []
59
59
  extensions: []
@@ -90,5 +90,5 @@ requirements: []
90
90
  rubygems_version: 3.1.2
91
91
  signing_key:
92
92
  specification_version: 4
93
- summary: Rewrite sections of Markdown documents using HTML Custom Elements.
93
+ summary: Rewrite and capture sections of Markdown documents using HTML Custom Elements.
94
94
  test_files: []