kramdown-prismic 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +81 -20
- data/lib/kramdown-prismic.rb +1 -0
- data/lib/kramdown-prismic/version.rb +1 -1
- data/lib/kramdown/parser/prismic.rb +97 -0
- data/test/{kramdown-prismic_test.rb → converter_test.rb} +1 -1
- data/test/parser_test.rb +185 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab0262b0f760d68d07f7950d9890a58cf61744f9
|
4
|
+
data.tar.gz: c787b185094ed5295b4c5bd33ca0c4b00945180f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ced9a6393fd4ef11ec73bee999ed963c8613eac549cfc5355e7685eb2b99e10dda273984247f505751d4f085068a272e1c18eab9e9e3f75dcdc217b2d89bfc6
|
7
|
+
data.tar.gz: 712aaf67c347bcf0b7310b7972a0acd4534d644c0f7eacb5e25159a4f4d9124c658db7ee9cd66f6a8c43ac75ffc6e391d2c9779be66c383a6c949b81d0ab7eae
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,16 +1,88 @@
|
|
1
1
|
# Kramdown Prismic
|
2
2
|
|
3
|
-
A Kramdown converter to convert documents into prismic rich text format.
|
3
|
+
A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and back.
|
4
|
+
|
5
|
+
A useful usage is to convert markdown documents to prismic for import purpose. Then you can even convert prismic format back to markdown.
|
4
6
|
|
5
7
|
## Status
|
6
8
|
|
7
|
-
|
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
|
+
|
11
|
+
The parser part is quite new and not feature complete (missing list item for instance).
|
12
|
+
|
13
|
+
## Install
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'kramdown-prismic', '~> 0.1'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Convert kramdown documents to Prismic
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'kramdown-prismic'
|
25
|
+
|
26
|
+
kramdown = '# Hello world'
|
27
|
+
Kramdown::Document.new(kramdown).to_prismic
|
28
|
+
```
|
29
|
+
|
30
|
+
### Convert markdown documents to Prismic
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'kramdown-prismic'
|
34
|
+
|
35
|
+
markdown = '# Hello world'
|
36
|
+
Kramdown::Document.new(markdown, input: :markdown).to_prismic
|
37
|
+
```
|
38
|
+
|
39
|
+
### Convert HTML documents to Prismic
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'kramdown-prismic'
|
43
|
+
|
44
|
+
html = '<h1>Hello world</h1>'
|
45
|
+
Kramdown::Document.new(html, input: :html).to_prismic
|
46
|
+
```
|
47
|
+
|
48
|
+
### Convert Primic to markdown
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'kramdown-prismic'
|
52
|
+
prismic = [
|
53
|
+
{
|
54
|
+
type: "heading1",
|
55
|
+
content: {
|
56
|
+
text: "This is the document title",
|
57
|
+
spans: []
|
58
|
+
}
|
59
|
+
}
|
60
|
+
]
|
61
|
+
Kramdown::Document.new(prismic, input: :prismic).to_kramdown
|
62
|
+
```
|
63
|
+
|
64
|
+
### Lookup for warnings
|
65
|
+
|
66
|
+
If there is some elements that cannot be converted (see the status table), a warning will be emitted.
|
67
|
+
|
68
|
+
For instance, html elements in the markdown is not supported:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require 'kramdown-prismic'
|
72
|
+
|
73
|
+
markdown = '<h1>Hello world</h1>'
|
74
|
+
result = Kramdown::Document.new(markdown, input: :markdown)
|
75
|
+
result.to_prismic
|
76
|
+
p result.warnings
|
77
|
+
```
|
8
78
|
|
9
79
|
### Difference between markdown and rich text
|
10
80
|
|
81
|
+
Some elements cannot be converted, due to some Prismic limitations. The table below explain the difference and limitations of the current converter:
|
82
|
+
|
11
83
|
| Markdown | Prismic |
|
12
84
|
|------------------|----------------------------|
|
13
|
-
|
|
85
|
+
| blockquote | converted to preformatted |
|
14
86
|
| hr | nothing |
|
15
87
|
| img | moved to the top level |
|
16
88
|
| nested list | moved to the top level |
|
@@ -25,9 +97,9 @@ Most of the content is translated. See the table below to see the difference:
|
|
25
97
|
| td | not supported |
|
26
98
|
| math | not supported |
|
27
99
|
| footnote | not supported |
|
28
|
-
| entity |
|
29
|
-
| typographic_sym |
|
30
|
-
| smart_quote |
|
100
|
+
| entity | converted to unicode |
|
101
|
+
| typographic_sym | converted to unicode |
|
102
|
+
| smart_quote | converted to unicode |
|
31
103
|
| abbreviation | not supported |
|
32
104
|
| html_element | not supported |
|
33
105
|
| xml_comment | not supported |
|
@@ -35,20 +107,9 @@ Most of the content is translated. See the table below to see the difference:
|
|
35
107
|
| comment | not supported |
|
36
108
|
| raw | not supported |
|
37
109
|
|
38
|
-
## Install
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
gem 'kramdown-prismic', '~> 0.1'
|
42
|
-
```
|
43
|
-
|
44
|
-
## Usage
|
45
|
-
|
46
|
-
```ruby
|
47
|
-
require 'kramdown-prismic'
|
48
|
-
|
49
|
-
Kramdown::Document.new(markdown).to_prismic
|
50
|
-
```
|
51
|
-
|
52
110
|
## License
|
53
111
|
|
54
112
|
MIT
|
113
|
+
|
114
|
+
[Kramdown]: https://kramdown.gettalong.org/
|
115
|
+
[prismic]: https://prismic.io/
|
data/lib/kramdown-prismic.rb
CHANGED
@@ -0,0 +1,97 @@
|
|
1
|
+
module Kramdown
|
2
|
+
module Parser
|
3
|
+
class Prismic < Base
|
4
|
+
def parse
|
5
|
+
@root.options[:encoding] = 'UTF-8'
|
6
|
+
@root.children = @source.map do |block|
|
7
|
+
type = block[:type]
|
8
|
+
if type.match(/heading/)
|
9
|
+
type = 'heading'
|
10
|
+
end
|
11
|
+
element = send("parse_#{type}", block)
|
12
|
+
parse_spans(element, block)
|
13
|
+
element
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def parse_heading(block)
|
20
|
+
level = block[:type].match(/heading([1-6])/)[1].to_i
|
21
|
+
Kramdown::Element.new(:header, nil, nil, {level: level, raw_text: ''})
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_paragraph(block)
|
25
|
+
Kramdown::Element.new(:p)
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_image(block)
|
29
|
+
p = Kramdown::Element.new(:p)
|
30
|
+
img = Kramdown::Element.new(:img, nil, {"src" => block[:data][:origin][:url], "alt" => block[:data][:alt]})
|
31
|
+
p.children << img
|
32
|
+
p
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_preformatted(block)
|
36
|
+
Kramdown::Element.new(:blockquote)
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_spans(element, block)
|
40
|
+
stack = []
|
41
|
+
|
42
|
+
(block[:content][:text].size + 1).times do |index|
|
43
|
+
|
44
|
+
starting_spans = find_starting_spans_for(block, index)
|
45
|
+
ending_spans = find_ending_spans_for(block, index)
|
46
|
+
|
47
|
+
ending_spans.each do |ending_span|
|
48
|
+
el = stack.pop
|
49
|
+
if stack.empty?
|
50
|
+
element.children << el
|
51
|
+
else
|
52
|
+
stack[-1].children << el
|
53
|
+
end
|
54
|
+
end
|
55
|
+
starting_spans.each do |starting_span|
|
56
|
+
stack << if starting_span[:type] == "hyperlink"
|
57
|
+
Element.new(:a, nil, {'href' => starting_span[:data][:url]})
|
58
|
+
else
|
59
|
+
Element.new(starting_span[:type].to_sym)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
char = block[:content][:text][index]
|
64
|
+
next if char.nil?
|
65
|
+
current_text = if stack.empty?
|
66
|
+
element.children.last
|
67
|
+
else
|
68
|
+
stack[-1].children.last
|
69
|
+
end
|
70
|
+
if current_text.nil? || current_text.type != :text
|
71
|
+
current_text = Element.new(:text, "")
|
72
|
+
if stack.empty?
|
73
|
+
element.children << current_text
|
74
|
+
else
|
75
|
+
stack[-1].children << current_text
|
76
|
+
end
|
77
|
+
end
|
78
|
+
current_text.value += char
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def find_starting_spans_for(block, index)
|
83
|
+
block[:content][:spans].find_all do |span|
|
84
|
+
span[:start] == index
|
85
|
+
end.sort_by do |span|
|
86
|
+
-span[:end]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def find_ending_spans_for(block, index)
|
91
|
+
block[:content][:spans].find_all do |span|
|
92
|
+
span[:end] == index
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/test/parser_test.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'kramdown-prismic'
|
3
|
+
|
4
|
+
class KramdownPrismicParserTest < Minitest::Test
|
5
|
+
def test_parse_heading
|
6
|
+
prismic = [
|
7
|
+
{
|
8
|
+
type: "heading1",
|
9
|
+
content: {
|
10
|
+
text: "This is the document title",
|
11
|
+
spans: []
|
12
|
+
}
|
13
|
+
}
|
14
|
+
]
|
15
|
+
expected = "# This is the document title\n\n"
|
16
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
17
|
+
assert_equal expected, doc.to_kramdown
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_parse_heading2
|
21
|
+
prismic = [
|
22
|
+
{
|
23
|
+
type: "heading2",
|
24
|
+
content: {
|
25
|
+
text: "This is an h2 title",
|
26
|
+
spans: []
|
27
|
+
}
|
28
|
+
}
|
29
|
+
]
|
30
|
+
expected = "## This is an h2 title\n\n"
|
31
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
32
|
+
assert_equal expected, doc.to_kramdown
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_parse_paragraph
|
36
|
+
prismic = [
|
37
|
+
{
|
38
|
+
type: "paragraph",
|
39
|
+
content: {
|
40
|
+
text: "This is a paragraph",
|
41
|
+
spans: []
|
42
|
+
}
|
43
|
+
}
|
44
|
+
]
|
45
|
+
expected = "This is a paragraph\n\n"
|
46
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
47
|
+
assert_equal expected, doc.to_kramdown
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_parse_paragraph_with_spans
|
51
|
+
prismic = [
|
52
|
+
{
|
53
|
+
type: "paragraph",
|
54
|
+
content: {
|
55
|
+
text: "This is a paragraph",
|
56
|
+
spans: [
|
57
|
+
{
|
58
|
+
type: "em",
|
59
|
+
start: 0,
|
60
|
+
end: 4
|
61
|
+
}
|
62
|
+
]
|
63
|
+
}
|
64
|
+
}
|
65
|
+
]
|
66
|
+
expected = "*This* is a paragraph\n\n"
|
67
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
68
|
+
assert_equal expected, doc.to_kramdown
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_parse_paragraph_with_multiple_spans
|
72
|
+
prismic = [
|
73
|
+
{
|
74
|
+
type: "paragraph",
|
75
|
+
content: {
|
76
|
+
text: "This is a paragraph",
|
77
|
+
spans: [
|
78
|
+
{
|
79
|
+
type: "em",
|
80
|
+
start: 0,
|
81
|
+
end: 4
|
82
|
+
},
|
83
|
+
{
|
84
|
+
type: "strong",
|
85
|
+
start: 5,
|
86
|
+
end: 7
|
87
|
+
}
|
88
|
+
]
|
89
|
+
}
|
90
|
+
}
|
91
|
+
]
|
92
|
+
expected = "*This* **is** a paragraph\n\n"
|
93
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
94
|
+
assert_equal expected, doc.to_kramdown
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_parse_paragraph_with_link
|
98
|
+
prismic = [
|
99
|
+
{
|
100
|
+
type: "paragraph",
|
101
|
+
content: {
|
102
|
+
text: "This is a paragraph",
|
103
|
+
spans: [
|
104
|
+
{
|
105
|
+
type: "hyperlink",
|
106
|
+
start: 0,
|
107
|
+
end: 19,
|
108
|
+
data: {
|
109
|
+
url: "https://prismic.io"
|
110
|
+
}
|
111
|
+
}
|
112
|
+
]
|
113
|
+
}
|
114
|
+
}
|
115
|
+
]
|
116
|
+
expected = "[This is a paragraph][1]\n\n\n\n[1]: https://prismic.io\n"
|
117
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
118
|
+
assert_equal expected, doc.to_kramdown
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_parse_paragraph_with_nested_spans
|
122
|
+
prismic = [
|
123
|
+
{
|
124
|
+
type: "paragraph",
|
125
|
+
content: {
|
126
|
+
text: "This is a paragraph",
|
127
|
+
spans: [
|
128
|
+
{
|
129
|
+
type: "em",
|
130
|
+
start: 0,
|
131
|
+
end: 4
|
132
|
+
},
|
133
|
+
{
|
134
|
+
type: "hyperlink",
|
135
|
+
start: 0,
|
136
|
+
end: 19,
|
137
|
+
data: {
|
138
|
+
url: "https://prismic.io"
|
139
|
+
}
|
140
|
+
}
|
141
|
+
]
|
142
|
+
}
|
143
|
+
}
|
144
|
+
]
|
145
|
+
expected = "[*This* is a paragraph][1]\n\n\n\n[1]: https://prismic.io\n"
|
146
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
147
|
+
assert_equal expected, doc.to_kramdown
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_parse_image
|
151
|
+
prismic = [
|
152
|
+
{
|
153
|
+
type: "image",
|
154
|
+
content: {
|
155
|
+
text: "",
|
156
|
+
spans: []
|
157
|
+
},
|
158
|
+
data: {
|
159
|
+
origin: {
|
160
|
+
url: '/img.png'
|
161
|
+
},
|
162
|
+
alt: 'alt text'
|
163
|
+
}
|
164
|
+
}
|
165
|
+
]
|
166
|
+
expected = "![alt text](/img.png)\n\n"
|
167
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
168
|
+
assert_equal expected, doc.to_kramdown
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_parse_preformatted
|
172
|
+
prismic = [
|
173
|
+
{
|
174
|
+
type: "preformatted",
|
175
|
+
content: {
|
176
|
+
text: "This is a pre block\n",
|
177
|
+
spans: []
|
178
|
+
}
|
179
|
+
}
|
180
|
+
]
|
181
|
+
expected = "> This is a pre block \n\n"
|
182
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
183
|
+
assert_equal expected, doc.to_kramdown
|
184
|
+
end
|
185
|
+
end
|
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.
|
4
|
+
version: 0.2.0
|
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:
|
11
|
+
date: 2018-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kramdown
|
@@ -70,7 +70,9 @@ files:
|
|
70
70
|
- lib/kramdown-prismic.rb
|
71
71
|
- lib/kramdown-prismic/version.rb
|
72
72
|
- lib/kramdown/converter/prismic.rb
|
73
|
-
-
|
73
|
+
- lib/kramdown/parser/prismic.rb
|
74
|
+
- test/converter_test.rb
|
75
|
+
- test/parser_test.rb
|
74
76
|
homepage: https://github.com/stormz/kramdown-prismic
|
75
77
|
licenses:
|
76
78
|
- MIT
|
@@ -96,4 +98,5 @@ signing_key:
|
|
96
98
|
specification_version: 4
|
97
99
|
summary: A Kramdown converter to convert documents into prismic rich text format.
|
98
100
|
test_files:
|
99
|
-
- test/
|
101
|
+
- test/converter_test.rb
|
102
|
+
- test/parser_test.rb
|