kramdown-components 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d12fd7ba367f8b2ff86e9e6d313781b88757b37c456a94654ade598c0b47df41
4
+ data.tar.gz: 7a02bd99bce593452e2d3f510718b474a1acc894c730d66e1f9eba07ecd3e0bd
5
+ SHA512:
6
+ metadata.gz: 6db57afee5989ac53ab2cd7b1668e9f110fd88e1da20195f724a218c4d59530487ed8bd095267441e9805b046a321530118c53382bed55783df63fc5e7bbbf39
7
+ data.tar.gz: e86b0cd86885dce1175a6384b03f037f39d32cc962e1d5ad176f37604e38008a0f884f125f150fc119f6fd2f1ab34fe4234ac532b6fbe549bbdb0e2cdc4b5589
data/.gitignore ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kramdown-components (0.0.1)
5
+ kramdown (~> 2.4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.5.0)
11
+ kramdown (2.4.0)
12
+ rexml
13
+ rexml (3.2.5)
14
+ rspec (3.11.0)
15
+ rspec-core (~> 3.11.0)
16
+ rspec-expectations (~> 3.11.0)
17
+ rspec-mocks (~> 3.11.0)
18
+ rspec-core (3.11.0)
19
+ rspec-support (~> 3.11.0)
20
+ rspec-expectations (3.11.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-mocks (3.11.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.11.0)
26
+ rspec-support (3.11.0)
27
+
28
+ PLATFORMS
29
+ x86_64-darwin-17
30
+
31
+ DEPENDENCIES
32
+ bundler
33
+ kramdown-components!
34
+ rspec
35
+
36
+ BUNDLED WITH
37
+ 2.2.9
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Mark Rickerby
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Kramdown Components
2
+
3
+ Rewrite sections of Markdown documents using HTML Custom Elements.
4
+
5
+ Useful for enhancing code blocks and formatted markup with more sophisticated controls while maintaining a clean separation of presentation and content.
6
+
7
+ ## How it Works
8
+
9
+ Register new components for your documents. Use the custom elements as block level wrappers in your text markup. Mix and match HTML and Markdown syntax. Generate nested DOM trees from a single top level element.
10
+
11
+ This library is only responsible for converting HTML elements at compile time. To actually render and control the element lifecycle when your document is viewed in a browser, you will need to attach the appropriate JavaScript.
12
+
13
+ ## Usage
14
+
15
+ To use the enhanced documents with components, create a `CustomDocument` instance. This supports the same interface as the base `Kramdown::Document`.
16
+
17
+ ```ruby
18
+ require "kramdown-components"
19
+
20
+ doc = Kramdown::CustomDocument("# Title")
21
+
22
+ puts doc.to_html
23
+ ```
24
+
25
+ ### Registration Phase
26
+
27
+ ```rb
28
+
29
+ ```
30
+
31
+ ### Compile Phase
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "kramdown-components"
3
+ spec.version = "0.0.1"
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."
7
+ spec.authors = ["Mark Rickerby"]
8
+ spec.email = "me@maetl.net"
9
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
10
+ spec.add_runtime_dependency "kramdown", '~> 2.4.0'
11
+ spec.add_development_dependency "bundler"
12
+ spec.add_development_dependency "rspec"
13
+ #spec.add_development_dependency "rspec-html"
14
+ spec.homepage = "https://github.com/maetl/kramdown-components"
15
+ spec.license = "MIT"
16
+ end
@@ -0,0 +1,102 @@
1
+ require "kramdown"
2
+
3
+ module Kramdown
4
+ class CustomDocument
5
+ def self.define_element(name, element=nil)
6
+ if element.nil?
7
+ element = CustomElement
8
+ end
9
+ tag_name = name.downcase
10
+ Kramdown::Parser::Html::Constants::HTML_ELEMENT[tag_name] = true
11
+ Kramdown::Parser::Html::Constants::HTML_CONTENT_MODEL[tag_name] = :block
12
+ @@custom_elements ||= {}
13
+ @@custom_elements[tag_name] = element
14
+ end
15
+
16
+ attr_reader :custom_elements
17
+
18
+ def initialize(source)
19
+ @source = source
20
+ @parsed_dom = Kramdown::Document.new(@source, {
21
+ input: "GFM",
22
+ parse_block_html: true,
23
+ syntax_highlighter: "rouge"
24
+ })
25
+ @custom_elements = extract_custom_elements
26
+ end
27
+
28
+ def root
29
+ @parsed_dom.root
30
+ end
31
+
32
+ def warnings
33
+ @parsed_dom.warnings
34
+ end
35
+
36
+ def to_html
37
+ @parsed_dom.to_html
38
+ end
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
+ def method_missing(id, *attr, &block)
56
+ @parsed_dom.send(id, attr, &block)
57
+ end
58
+
59
+ def generate_el_id(tagname)
60
+ alphabet = [('a'..'z')].map(&:to_a).flatten
61
+ tag_id = (0...12).map { alphabet[rand(alphabet.length)] }.join
62
+ tag_abbr = tagname.split("-").map { |part| part[0] }.join("")
63
+ "#{tag_abbr}-#{tag_id}"
64
+ end
65
+
66
+ def extract_custom_elements
67
+ elements = []
68
+
69
+ @parsed_dom.root.children.each do |outer_el|
70
+ if outer_el.type == :html_element && @@custom_elements.keys.include?(outer_el.value)
71
+
72
+ unless outer_el.attr.key?("id")
73
+ outer_el.attr["id"] = generate_el_id(outer_el.value)
74
+ end
75
+
76
+ custom_element_cls = @@custom_elements[outer_el.value]
77
+ element = custom_element_cls.new(outer_el.attr["id"], outer_el)
78
+ element.parse_dom
79
+
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
+ elements << element
96
+ end
97
+ end
98
+
99
+ elements
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,13 @@
1
+ module Kramdown
2
+ class CustomElement
3
+ attr_reader :id, :outer_el
4
+
5
+ def initialize(id, outer_el)
6
+ @id = id
7
+ @outer_el = outer_el
8
+ end
9
+
10
+ def parse_dom
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "kramdown/custom_document"
2
+ require_relative "kramdown/custom_element"
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kramdown-components
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Rickerby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Mix and match HTML and Markdown syntax. Generate nested DOM trees from
56
+ a single top level element.
57
+ email: me@maetl.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.md
67
+ - kramdown-components.gemspec
68
+ - lib/kramdown-components.rb
69
+ - lib/kramdown/custom_document.rb
70
+ - lib/kramdown/custom_element.rb
71
+ homepage: https://github.com/maetl/kramdown-components
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.1.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Rewrite sections of Markdown documents using HTML Custom Elements.
94
+ test_files: []