kramdown-prismic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6528bd7b5eab9c6cb2d3b777a4c6b24785bfbf71
4
+ data.tar.gz: 9851247bafd3d8e18030657fe4dad10c4a0ca3ef
5
+ SHA512:
6
+ metadata.gz: fc5bed50cbee721945804e15f38116561b97d873736f85308e11d0772535c8a32c4a5822165295440d8933fc1e42f7122f433aba2d2b00449cab4501214113b2
7
+ data.tar.gz: 1ea9592644dd399893061a7cc53592d7aab77153045b58ee3f2a636dc68a731c63831f5e1f934f8d8fdca45278b74e343e849da2d4b031031b6eff997ee284a1
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /vendor
2
+ /.bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kramdown-prismic (0.0.1)
5
+ kramdown (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ kramdown (1.15.0)
11
+ minitest (5.10.3)
12
+ rake (12.2.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ kramdown-prismic!
19
+ minitest (~> 5.0)
20
+ rake (~> 12.0)
21
+
22
+ BUNDLED WITH
23
+ 1.14.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Stormz
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,48 @@
1
+ # Kramdown Prismic
2
+
3
+ A Kramdown converter to convert documents into prismic rich text format.
4
+
5
+ ## Status
6
+
7
+ *Very early*, still a proof of concept.
8
+
9
+ TODO:
10
+
11
+ - [x] heading1
12
+ - [x] heading2
13
+ - [x] heading3
14
+ - [x] heading4
15
+ - [x] heading5
16
+ - [x] heading6
17
+ - [x] paragraph
18
+ - [ ] preformatted
19
+ - [x] strong
20
+ - [x] em
21
+ - [x] hyperlink
22
+ - [x] o-list-item
23
+ - [x] list-item
24
+ - [ ] Image
25
+ - [ ] Embed
26
+
27
+ ## Difference between markdown and rich text
28
+
29
+ | Markdown | Prismic |
30
+ |--------------|----------------------------|
31
+ | Blockquote | translated to preformatted |
32
+ | hr | nothing |
33
+
34
+ ## Install
35
+
36
+ Not yet available on rubygems
37
+
38
+ ## Usage
39
+
40
+ ```ruby
41
+ require 'kramdown-prismic'
42
+
43
+ Kramdown::Document.new(markdown).to_prismic
44
+ ```
45
+
46
+ ## License
47
+
48
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/kramdown-prismic/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'kramdown-prismic'
6
+ s.version = KramdownPrismic::VERSION
7
+ s.summary = "A Kramdown converter to convert documents into prismic rich text format."
8
+ s.description = "A Kramdown converter to convert documents into prismic rich text format."
9
+ s.authors = ["François de Metz"]
10
+ s.email = 'francois@2metz.fr'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- test/*`.split("\n")
14
+
15
+ s.homepage = 'https://github.com/stormz/kramdown-prismic'
16
+ s.license = 'MIT'
17
+
18
+ s.add_dependency "kramdown", "~> 1.0"
19
+ s.add_development_dependency "minitest", "~> 5.0"
20
+ s.add_development_dependency "rake", "~> 12.0"
21
+ end
@@ -0,0 +1,4 @@
1
+ require 'kramdown'
2
+
3
+ require 'kramdown/converter/prismic'
4
+ require 'kramdown-prismic/version'
@@ -0,0 +1,3 @@
1
+ module KramdownPrismic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,122 @@
1
+ require 'kramdown/converter/base'
2
+
3
+ module Kramdown
4
+ module Converter
5
+ class Prismic < Base
6
+ def convert(root)
7
+ root.children.map { |child|
8
+ convert_element(child)
9
+ }.compact.flatten
10
+ end
11
+
12
+ private
13
+
14
+ def convert_element(element)
15
+ send("convert_#{element.type}", element)
16
+ end
17
+
18
+ def convert_blank(element)
19
+ end
20
+
21
+ def convert_header(element)
22
+ {
23
+ type: "heading#{element.options[:level]}",
24
+ content: extract_content(element)
25
+ }
26
+ end
27
+
28
+ def convert_p(element)
29
+ {
30
+ type: "paragraph",
31
+ content: extract_content(element)
32
+ }
33
+ end
34
+
35
+ def convert_ol(element)
36
+ convert_list(element, 'o-list-item')
37
+ end
38
+
39
+ def convert_ul(element)
40
+ convert_list(element, 'list-item')
41
+ end
42
+
43
+ def convert_list(element, type)
44
+ element.children.map do |child|
45
+ convert_li(type, child)
46
+ end
47
+ end
48
+
49
+ def convert_li(type, element)
50
+ {
51
+ type: type,
52
+ content: extract_content(element)
53
+ }
54
+ end
55
+
56
+ def convert_codeblock(element)
57
+ {
58
+ type: 'preformatted',
59
+ content: {
60
+ text: element.value,
61
+ spans: []
62
+ }
63
+ }
64
+ end
65
+
66
+ def convert_blockquote(element)
67
+ {
68
+ type: 'preformatted',
69
+ content: extract_content(element)
70
+ }
71
+ end
72
+
73
+ def convert_hr(element)
74
+ end
75
+
76
+ def extract_content(element, memo={text: '', spans: []})
77
+ element.children.inject(memo) do |memo2, child|
78
+ send("extract_span_#{child.type}", child, memo2)
79
+ memo2
80
+ end
81
+ end
82
+
83
+ def insert_span(element, memo, span)
84
+ span[:start] = memo[:text].size
85
+ extract_content(element, memo)
86
+ span[:end] = memo[:text].size
87
+ memo[:spans] << span
88
+ memo
89
+ end
90
+
91
+ def extract_span_text(element, memo)
92
+ memo[:text] += element.value
93
+ memo
94
+ end
95
+
96
+ def extract_span_a(element, memo)
97
+ insert_span(element, memo, {
98
+ type: 'hyperlink',
99
+ data: {
100
+ url: element.attr["href"]
101
+ }
102
+ })
103
+ end
104
+
105
+ def extract_span_strong(element, memo)
106
+ insert_span(element, memo, {
107
+ type: 'strong'
108
+ })
109
+ end
110
+
111
+ def extract_span_em(element, memo)
112
+ insert_span(element, memo, {
113
+ type: 'em'
114
+ })
115
+ end
116
+
117
+ def extract_span_p(element, memo)
118
+ extract_content(element, memo)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,243 @@
1
+ require 'minitest/autorun'
2
+ require 'kramdown-prismic'
3
+
4
+ class KramdownPrismicTest < Minitest::Test
5
+ def test_convert_heading
6
+ expected = [
7
+ {
8
+ type: "heading1",
9
+ content: {
10
+ text: "This is the document title",
11
+ spans: []
12
+ }
13
+ }
14
+ ]
15
+ markdown = "# This is the document title"
16
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
17
+ end
18
+
19
+ def test_convert_heading2
20
+ expected = [
21
+ {
22
+ type: "heading2",
23
+ content: {
24
+ text: "This is a document title",
25
+ spans: []
26
+ }
27
+ }
28
+ ]
29
+ markdown = "## This is a document title"
30
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
31
+ end
32
+
33
+ def test_convert_heading_with_spans
34
+ expected = [
35
+ {
36
+ type: "heading2",
37
+ content: {
38
+ text: "This is a document title",
39
+ spans: [
40
+ {
41
+ type: "em",
42
+ start: 0,
43
+ end: 4
44
+ }
45
+ ]
46
+ }
47
+ }
48
+ ]
49
+ markdown = "## *This* is a document title"
50
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
51
+ end
52
+
53
+ def test_convert_paragraph
54
+ expected = [
55
+ {
56
+ type: "paragraph",
57
+ content: {
58
+ text: "This is a paragraph",
59
+ spans: []
60
+ }
61
+ }
62
+ ]
63
+ markdown = "This is a paragraph"
64
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
65
+ end
66
+
67
+ def test_convert_paragraph_with_spans
68
+ expected = [
69
+ {
70
+ type: "paragraph",
71
+ content: {
72
+ text: "This is a paragraph",
73
+ spans: [
74
+ {
75
+ type: "hyperlink",
76
+ start: 0,
77
+ end: 19,
78
+ data: {
79
+ url: "https://prismic.io"
80
+ }
81
+ }
82
+ ]
83
+ }
84
+ }
85
+ ]
86
+ markdown = "[This is a paragraph](https://prismic.io)"
87
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
88
+ end
89
+
90
+ def test_convert_paragraph_with_strong
91
+ expected = [
92
+ {
93
+ type: "paragraph",
94
+ content: {
95
+ text: "This is a paragraph",
96
+ spans: [
97
+ {
98
+ type: "strong",
99
+ start: 0,
100
+ end: 19
101
+ }
102
+ ]
103
+ }
104
+ }
105
+ ]
106
+ markdown = "**This is a paragraph**"
107
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
108
+ end
109
+
110
+ def test_convert_paragraph_with_strong2
111
+ expected = [
112
+ {
113
+ type: "paragraph",
114
+ content: {
115
+ text: "This is a paragraph",
116
+ spans: [
117
+ {
118
+ type: "strong",
119
+ start: 0,
120
+ end: 4
121
+ }
122
+ ]
123
+ }
124
+ }
125
+ ]
126
+ markdown = "**This** is a paragraph"
127
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
128
+ end
129
+
130
+ def test_convert_paragraph_with_em
131
+ expected = [
132
+ {
133
+ type: "paragraph",
134
+ content: {
135
+ text: "This is a paragraph",
136
+ spans: [
137
+ {
138
+ type: "em",
139
+ start: 0,
140
+ end: 4
141
+ }
142
+ ]
143
+ }
144
+ }
145
+ ]
146
+ markdown = "*This* is a paragraph"
147
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
148
+ end
149
+
150
+ def test_convert_list_o
151
+ expected = [
152
+ {
153
+ type: "o-list-item",
154
+ content: {
155
+ text: "This is a list item",
156
+ spans: [
157
+ {
158
+ type: "em",
159
+ start: 0,
160
+ end: 4
161
+ }
162
+ ]
163
+ }
164
+ },
165
+ {
166
+ type: "o-list-item",
167
+ content: {
168
+ text: "This is a second list item",
169
+ spans: []
170
+ }
171
+ }
172
+ ]
173
+ markdown = "1. *This* is a list item\n2. This is a second list item"
174
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
175
+ end
176
+
177
+ def test_convert_list_u
178
+ expected = [
179
+ {
180
+ type: "list-item",
181
+ content: {
182
+ text: "This is a list item",
183
+ spans: [
184
+ {
185
+ type: "em",
186
+ start: 0,
187
+ end: 4
188
+ }
189
+ ]
190
+ }
191
+ },
192
+ {
193
+ type: "list-item",
194
+ content: {
195
+ text: "This is a second list item",
196
+ spans: []
197
+ }
198
+ }
199
+ ]
200
+ markdown = "- *This* is a list item\n- This is a second list item"
201
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
202
+ end
203
+
204
+ def test_convert_preformatted
205
+ expected = [
206
+ {
207
+ type: "preformatted",
208
+ content: {
209
+ text: "This is a pre block\n",
210
+ spans: []
211
+ }
212
+ }
213
+ ]
214
+ markdown = " This is a pre block\n"
215
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
216
+ end
217
+
218
+ def test_convert_blockquote
219
+ expected = [
220
+ {
221
+ type: "preformatted",
222
+ content: {
223
+ text: "This is a blockquote",
224
+ spans: []
225
+ }
226
+ }
227
+ ]
228
+ markdown = "> This is a blockquote\n"
229
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
230
+ end
231
+
232
+ def test_convert_empty
233
+ expected = []
234
+ markdown = ""
235
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
236
+ end
237
+
238
+ def test_convert_hr
239
+ expected = []
240
+ markdown = "---"
241
+ assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
242
+ end
243
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kramdown-prismic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - François de Metz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-27 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: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ description: A Kramdown converter to convert documents into prismic rich text format.
56
+ email: francois@2metz.fr
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - kramdown-prismic.gemspec
68
+ - lib/kramdown-prismic.rb
69
+ - lib/kramdown-prismic/version.rb
70
+ - lib/kramdown/converter/prismic.rb
71
+ - test/kramdown-prismic_test.rb
72
+ homepage: https://github.com/stormz/kramdown-prismic
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.11
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: A Kramdown converter to convert documents into prismic rich text format.
96
+ test_files:
97
+ - test/kramdown-prismic_test.rb