kramdown-prismic 0.3.1 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/kramdown/converter/prismic.rb +35 -8
- data/lib/kramdown/parser/prismic.rb +44 -13
- data/lib/kramdown-prismic/version.rb +1 -1
- data/test/converter_test.rb +79 -0
- data/test/parser_test.rb +132 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe3e038c22a918d48b71b44a5b4910910bd8b47bf18516244510ba3e2ddc5602
|
4
|
+
data.tar.gz: 6d0280c72a7efee6a1edc6b15c06068c014c1eb21c3950fbc9768a8f0942ca92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99c4e82d82ae1d57b66747991976660aff280a52f346b7d459f80b3d8b86d6829eaf2bc966b6387cd4aec1fb2fbf7819db8c40e57da28f9a2c583cb1482965e7
|
7
|
+
data.tar.gz: c7c91856d596c6f4441e333f00830facddcf744ab588bccd02f25e2ad749b700b6fcae7aeb3181c7261b3f1bce0e21f07dfa2fa504de9f8e5c9bc8a41d21be37
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## Version 0.3.5
|
4
|
+
|
5
|
+
- Convert `br` element when converting HTML documents.
|
6
|
+
|
7
|
+
## Version 0.3.4
|
8
|
+
|
9
|
+
- Convert `strong` and `em` elements when converting HTML documents.
|
10
|
+
|
11
|
+
## Version 0.3.3
|
12
|
+
|
13
|
+
- Renable converting xml comments
|
14
|
+
|
15
|
+
## Version 0.3.2
|
16
|
+
|
17
|
+
- Parse ordered and unordered lists
|
18
|
+
- Parse image with link
|
19
|
+
|
3
20
|
## Version 0.3.1
|
4
21
|
|
5
22
|
- Add binaries `html2primisc`, `markdown2prismic` and `primisc2markdown`.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Kramdown Prismic
|
1
|
+
# Kramdown Prismic ![build](https://img.shields.io/github/workflow/status/stormz/kramdown-prismic/CI) [![gem](https://img.shields.io/gem/v/kramdown-prismic)](https://rubygems.org/gems/kramdown-prismic)
|
2
2
|
|
3
3
|
A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and the other way around.
|
4
4
|
|
@@ -8,7 +8,7 @@ A useful usage is to convert markdown documents to prismic for [import purpose][
|
|
8
8
|
|
9
9
|
The converter part (kramdown to prismic) is working and fairly complete. See *Difference between markdown and rich text* below to know more about limitations.
|
10
10
|
|
11
|
-
The parser part is quite new and not feature complete
|
11
|
+
The parser part is quite new and not feature complete.
|
12
12
|
|
13
13
|
## Install
|
14
14
|
|
@@ -110,6 +110,7 @@ module Kramdown
|
|
110
110
|
end
|
111
111
|
|
112
112
|
def convert_hr(element); end
|
113
|
+
def convert_br(element); end
|
113
114
|
|
114
115
|
def convert_img(element)
|
115
116
|
{
|
@@ -148,6 +149,23 @@ module Kramdown
|
|
148
149
|
}
|
149
150
|
end
|
150
151
|
|
152
|
+
def convert_strong(element)
|
153
|
+
convert_sub_html_element(element, 'strong')
|
154
|
+
end
|
155
|
+
|
156
|
+
def convert_em(element)
|
157
|
+
convert_sub_html_element(element, 'em')
|
158
|
+
end
|
159
|
+
|
160
|
+
def convert_sub_html_element(element, type)
|
161
|
+
content = extract_content(element)
|
162
|
+
content[:spans].push({ type: type, start: 0, end: content[:text].size })
|
163
|
+
{
|
164
|
+
type: 'paragraph',
|
165
|
+
content: content
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
151
169
|
def convert_html_element(element)
|
152
170
|
if element.value == 'iframe'
|
153
171
|
{
|
@@ -187,6 +205,11 @@ module Kramdown
|
|
187
205
|
nil
|
188
206
|
end
|
189
207
|
|
208
|
+
def convert_xml_comment(_element)
|
209
|
+
warning('translating xml comment is not supported')
|
210
|
+
nil
|
211
|
+
end
|
212
|
+
|
190
213
|
def convert_raw(_element)
|
191
214
|
warning('translating raw is not supported')
|
192
215
|
nil
|
@@ -268,14 +291,18 @@ module Kramdown
|
|
268
291
|
memo[:text] += element.value
|
269
292
|
end
|
270
293
|
|
294
|
+
def extract_span_xml_comment(element, memo)
|
295
|
+
warning('translating xml comment is not supported')
|
296
|
+
end
|
297
|
+
|
271
298
|
TYPOGRAPHIC_SYMS = {
|
272
|
-
mdash: [
|
273
|
-
ndash: [
|
274
|
-
hellip: [
|
275
|
-
laquo_space: [
|
276
|
-
raquo_space: [
|
277
|
-
laquo: [
|
278
|
-
raquo: [
|
299
|
+
mdash: [Utils::Entities.entity('mdash')],
|
300
|
+
ndash: [Utils::Entities.entity('ndash')],
|
301
|
+
hellip: [Utils::Entities.entity('hellip')],
|
302
|
+
laquo_space: [Utils::Entities.entity('laquo'), Utils::Entities.entity('nbsp')],
|
303
|
+
raquo_space: [Utils::Entities.entity('nbsp'), Utils::Entities.entity('raquo')],
|
304
|
+
laquo: [Utils::Entities.entity('laquo')],
|
305
|
+
raquo: [Utils::Entities.entity('raquo')]
|
279
306
|
}.freeze
|
280
307
|
def extract_span_typographic_sym(element, memo)
|
281
308
|
value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
|
@@ -287,7 +314,7 @@ module Kramdown
|
|
287
314
|
end
|
288
315
|
|
289
316
|
def extract_span_smart_quote(element, memo)
|
290
|
-
memo[:text] +=
|
317
|
+
memo[:text] += Utils::Entities.entity(element.value.to_s).char
|
291
318
|
end
|
292
319
|
end
|
293
320
|
end
|
@@ -5,39 +5,70 @@ module Kramdown
|
|
5
5
|
class Prismic < Base
|
6
6
|
def parse
|
7
7
|
@root.options[:encoding] = 'UTF-8'
|
8
|
-
@root.children = @source.
|
9
|
-
|
10
|
-
type = 'heading' if type.match(/heading/)
|
11
|
-
element = send("parse_#{type}", block)
|
12
|
-
parse_spans(element, block)
|
13
|
-
element
|
8
|
+
@root.children = @source.reduce([]) do |memo, block|
|
9
|
+
parse_element(block, memo)
|
14
10
|
end
|
15
11
|
end
|
16
12
|
|
17
13
|
private
|
18
14
|
|
15
|
+
def parse_element(block, memo)
|
16
|
+
type = block[:type].gsub('-', '_')
|
17
|
+
type = 'heading' if type.match(/heading/)
|
18
|
+
if type == 'list_item'
|
19
|
+
parse_list(:ul, block, memo)
|
20
|
+
memo
|
21
|
+
elsif type == 'o_list_item'
|
22
|
+
parse_list(:ol, block, memo)
|
23
|
+
memo
|
24
|
+
else
|
25
|
+
element = send("parse_#{type}", block)
|
26
|
+
parse_spans(element, block)
|
27
|
+
memo << element
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
19
31
|
def parse_heading(block)
|
20
32
|
level = block[:type].match(/heading([1-6])/)[1].to_i
|
21
|
-
|
33
|
+
Element.new(:header, nil, nil, { level: level, raw_text: '' })
|
22
34
|
end
|
23
35
|
|
24
36
|
def parse_paragraph(_block)
|
25
|
-
|
37
|
+
Element.new(:p)
|
26
38
|
end
|
27
39
|
|
28
40
|
def parse_image(block)
|
29
|
-
p =
|
30
|
-
img =
|
31
|
-
|
41
|
+
p = Element.new(:p)
|
42
|
+
img = Element.new(:img, nil, { 'src' => block[:data][:origin][:url], 'alt' => block[:data][:alt] })
|
43
|
+
if block[:data][:linkTo]
|
44
|
+
a = Element.new(:a, nil, { 'href' => block[:data][:linkTo][:url] })
|
45
|
+
a.children << img
|
46
|
+
p.children << a
|
47
|
+
else
|
48
|
+
p.children << img
|
49
|
+
end
|
32
50
|
p
|
33
51
|
end
|
34
52
|
|
35
53
|
def parse_preformatted(_block)
|
36
|
-
|
54
|
+
Element.new(:blockquote)
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_list(type, block, memo)
|
58
|
+
list = memo.last
|
59
|
+
unless list && list.type == type
|
60
|
+
list = Element.new(type)
|
61
|
+
memo << list
|
62
|
+
end
|
63
|
+
li = Element.new(:li, nil, nil)
|
64
|
+
list.children << li
|
65
|
+
p = Element.new(:p, nil, nil, transparent: true)
|
66
|
+
li.children << p
|
67
|
+
parse_spans(p, block)
|
37
68
|
end
|
38
69
|
|
39
70
|
def parse_embed(block)
|
40
|
-
|
71
|
+
Element.new(:html_element, 'iframe', { src: block[:data][:embed_url] })
|
41
72
|
end
|
42
73
|
|
43
74
|
def parse_spans(element, block)
|
data/test/converter_test.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require 'minitest/autorun'
|
@@ -131,6 +132,26 @@ class KramdownPrismicConverterTest < Minitest::Test
|
|
131
132
|
assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
|
132
133
|
end
|
133
134
|
|
135
|
+
def test_convert_html_strong
|
136
|
+
expected = [
|
137
|
+
{
|
138
|
+
type: 'paragraph',
|
139
|
+
content: {
|
140
|
+
text: 'This is a paragraph',
|
141
|
+
spans: [
|
142
|
+
{
|
143
|
+
type: 'strong',
|
144
|
+
start: 0,
|
145
|
+
end: 19
|
146
|
+
}
|
147
|
+
]
|
148
|
+
}
|
149
|
+
}
|
150
|
+
]
|
151
|
+
markdown = '<strong>This is a paragraph</strong>'
|
152
|
+
assert_equal expected, Kramdown::Document.new(markdown, input: :html).to_prismic
|
153
|
+
end
|
154
|
+
|
134
155
|
def test_convert_paragraph_with_em
|
135
156
|
expected = [
|
136
157
|
{
|
@@ -151,6 +172,26 @@ class KramdownPrismicConverterTest < Minitest::Test
|
|
151
172
|
assert_equal expected, Kramdown::Document.new(markdown, input: :markdown).to_prismic
|
152
173
|
end
|
153
174
|
|
175
|
+
def test_convert_html_em
|
176
|
+
expected = [
|
177
|
+
{
|
178
|
+
type: 'paragraph',
|
179
|
+
content: {
|
180
|
+
text: 'This',
|
181
|
+
spans: [
|
182
|
+
{
|
183
|
+
type: 'em',
|
184
|
+
start: 0,
|
185
|
+
end: 4
|
186
|
+
}
|
187
|
+
]
|
188
|
+
}
|
189
|
+
}
|
190
|
+
]
|
191
|
+
markdown = '<em>This</em>'
|
192
|
+
assert_equal expected, Kramdown::Document.new(markdown, input: :html).to_prismic
|
193
|
+
end
|
194
|
+
|
154
195
|
def test_convert_list_o
|
155
196
|
expected = [
|
156
197
|
{
|
@@ -474,6 +515,20 @@ class KramdownPrismicConverterTest < Minitest::Test
|
|
474
515
|
assert_equal expected, Kramdown::Document.new(html, input: :html).to_prismic
|
475
516
|
end
|
476
517
|
|
518
|
+
def test_convert_br_in_root_element
|
519
|
+
expected = [
|
520
|
+
{
|
521
|
+
type: 'paragraph',
|
522
|
+
content: {
|
523
|
+
text: "Test\n",
|
524
|
+
spans: []
|
525
|
+
}
|
526
|
+
}
|
527
|
+
]
|
528
|
+
html = '<br><p>Test<br></p>'
|
529
|
+
assert_equal expected, Kramdown::Document.new(html, input: :html).to_prismic
|
530
|
+
end
|
531
|
+
|
477
532
|
def test_convert_html_with_no_tags
|
478
533
|
expected = [
|
479
534
|
{
|
@@ -587,6 +642,30 @@ class KramdownPrismicConverterTest < Minitest::Test
|
|
587
642
|
assert_equal 1, doc.warnings.size
|
588
643
|
end
|
589
644
|
|
645
|
+
def test_convert_xml_comment
|
646
|
+
expected = []
|
647
|
+
markdown = "<!-- Main -->"
|
648
|
+
doc = Kramdown::Document.new(markdown, input: :kramdown)
|
649
|
+
assert_equal expected, doc.to_prismic
|
650
|
+
assert_equal 1, doc.warnings.size
|
651
|
+
end
|
652
|
+
|
653
|
+
def test_convert_span_xml_comment
|
654
|
+
expected = [
|
655
|
+
{
|
656
|
+
type: 'paragraph',
|
657
|
+
content: {
|
658
|
+
text: 'test test',
|
659
|
+
spans: []
|
660
|
+
}
|
661
|
+
}
|
662
|
+
]
|
663
|
+
markdown = "test <!-- Main --> test"
|
664
|
+
doc = Kramdown::Document.new(markdown, input: :kramdown)
|
665
|
+
assert_equal expected, doc.to_prismic
|
666
|
+
assert_equal 1, doc.warnings.size
|
667
|
+
end
|
668
|
+
|
590
669
|
def test_convert_comment
|
591
670
|
expected = []
|
592
671
|
markdown = "{::comment}\nComment\n{:/comment}"
|
data/test/parser_test.rb
CHANGED
@@ -136,6 +136,114 @@ class KramdownPrismicParserTest < Minitest::Test
|
|
136
136
|
assert_equal expected, doc.to_kramdown
|
137
137
|
end
|
138
138
|
|
139
|
+
def test_parse_list_item
|
140
|
+
prismic = [
|
141
|
+
{
|
142
|
+
type: 'list-item',
|
143
|
+
content: {
|
144
|
+
text: 'Hello',
|
145
|
+
spans: []
|
146
|
+
}
|
147
|
+
},
|
148
|
+
{
|
149
|
+
type: 'list-item',
|
150
|
+
content: {
|
151
|
+
text: 'World',
|
152
|
+
spans: []
|
153
|
+
}
|
154
|
+
}
|
155
|
+
]
|
156
|
+
expected = "* Hello\n* World\n\n"
|
157
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
158
|
+
assert_equal expected, doc.to_kramdown
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_parse_list_item_and_spans
|
162
|
+
prismic = [
|
163
|
+
{
|
164
|
+
type: 'list-item',
|
165
|
+
content: {
|
166
|
+
text: 'Hello',
|
167
|
+
spans: [
|
168
|
+
{
|
169
|
+
type: 'em',
|
170
|
+
start: 0,
|
171
|
+
end: 5
|
172
|
+
}
|
173
|
+
]
|
174
|
+
}
|
175
|
+
},
|
176
|
+
{
|
177
|
+
type: 'list-item',
|
178
|
+
content: {
|
179
|
+
text: 'World',
|
180
|
+
spans: []
|
181
|
+
}
|
182
|
+
}
|
183
|
+
]
|
184
|
+
expected = "* *Hello*\n* World\n\n"
|
185
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
186
|
+
assert_equal expected, doc.to_kramdown
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_parse_o_list_item
|
190
|
+
prismic = [
|
191
|
+
{
|
192
|
+
type: 'o-list-item',
|
193
|
+
content: {
|
194
|
+
text: 'Hello',
|
195
|
+
spans: []
|
196
|
+
}
|
197
|
+
},
|
198
|
+
{
|
199
|
+
type: 'o-list-item',
|
200
|
+
content: {
|
201
|
+
text: 'World',
|
202
|
+
spans: []
|
203
|
+
}
|
204
|
+
}
|
205
|
+
]
|
206
|
+
expected = "1. Hello\n2. World\n\n"
|
207
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
208
|
+
assert_equal expected, doc.to_kramdown
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_parse_o_list_item_and_list_item
|
212
|
+
prismic = [
|
213
|
+
{
|
214
|
+
type: 'o-list-item',
|
215
|
+
content: {
|
216
|
+
text: 'Hello',
|
217
|
+
spans: []
|
218
|
+
}
|
219
|
+
},
|
220
|
+
{
|
221
|
+
type: 'o-list-item',
|
222
|
+
content: {
|
223
|
+
text: 'World',
|
224
|
+
spans: []
|
225
|
+
}
|
226
|
+
},
|
227
|
+
{
|
228
|
+
type: 'list-item',
|
229
|
+
content: {
|
230
|
+
text: 'Test',
|
231
|
+
spans: []
|
232
|
+
}
|
233
|
+
},
|
234
|
+
{
|
235
|
+
type: 'list-item',
|
236
|
+
content: {
|
237
|
+
text: 'roger',
|
238
|
+
spans: []
|
239
|
+
}
|
240
|
+
}
|
241
|
+
]
|
242
|
+
expected = "1. Hello\n2. World\n\n* Test\n* roger\n\n"
|
243
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
244
|
+
assert_equal expected, doc.to_kramdown
|
245
|
+
end
|
246
|
+
|
139
247
|
def test_parse_image
|
140
248
|
prismic = [
|
141
249
|
{
|
@@ -157,6 +265,30 @@ class KramdownPrismicParserTest < Minitest::Test
|
|
157
265
|
assert_equal expected, doc.to_kramdown
|
158
266
|
end
|
159
267
|
|
268
|
+
def test_parse_img_with_link
|
269
|
+
prismic = [
|
270
|
+
{
|
271
|
+
type: 'image',
|
272
|
+
content: {
|
273
|
+
text: '',
|
274
|
+
spans: []
|
275
|
+
},
|
276
|
+
data: {
|
277
|
+
origin: {
|
278
|
+
url: '/img.png'
|
279
|
+
},
|
280
|
+
alt: 'alt text',
|
281
|
+
linkTo: {
|
282
|
+
url: 'https://example.net/'
|
283
|
+
}
|
284
|
+
}
|
285
|
+
}
|
286
|
+
]
|
287
|
+
expected = "[![alt text](/img.png)][1]\n\n\n\n[1]: https://example.net/\n"
|
288
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
289
|
+
assert_equal expected, doc.to_kramdown
|
290
|
+
end
|
291
|
+
|
160
292
|
def test_parse_preformatted
|
161
293
|
prismic = [
|
162
294
|
{
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kramdown-prismic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- François de Metz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kramdown
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
- !ruby/object:Gem::Version
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
|
-
rubygems_version: 3.
|
110
|
+
rubygems_version: 3.1.4
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: A Kramdown converter to convert documents into prismic rich text format and
|