kramdown-prismic 0.2.1 → 0.3.2

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: bb835641c9e7f2e9e600e0307e3bfd6e9eb46e5d83ae257c505c659c20865931
4
- data.tar.gz: cba5da2b2d81fff052b24d7921872ec8e9607b726bdbe210ff516b6cf7b6810a
3
+ metadata.gz: 1ee09d3336016df7164dd139beccbdf493e4fa5c057b2c3d6480d1db7c352da4
4
+ data.tar.gz: 2b14fca6c580875817413ec64d3f5fc015ef00544ddf7a4aff6bfe0edb8e160c
5
5
  SHA512:
6
- metadata.gz: 9b4e02f18a5dbf5c51f46cc729c8dd2018d1eec8809790e0492ee22f7886627e5bc77d20dabde755f44883f1c062cd7a7dda33ba8acbe9c4dac165218f23ca70
7
- data.tar.gz: 5428075b5c8eac073a1729feac6803a378ac55ba330540c46c230f78b5e965caeb95b26ea60be9f059611d182cb0f6c764c2817f9b03c53b1dcde03ec923d86b
6
+ metadata.gz: 4a3b05a335d537eedcd4ab32f45dfd7fd28f258c7557a7efe087a8e98b77d1b65922c012c9b5737939d7ea6d1651117061d3158aad626c510408091abb4d210a
7
+ data.tar.gz: cf73a15a0961791104991790e2f46ee031d80f0265fdaca1e14601822747b43a815af87a7e3c74bb0a8e60774ec847b6e46e6b0bd0eca5a46b2bffe962ede4fe
@@ -2,12 +2,33 @@ name: CI
2
2
 
3
3
  on: [push, pull_request]
4
4
  jobs:
5
- test:
5
+ test-kramdown2:
6
6
  strategy:
7
7
  fail-fast: false
8
8
  matrix:
9
9
  os: [ubuntu-latest]
10
- ruby: [2.5, 2.6, 2.7]
10
+ ruby: [2.6, 2.7, "3.0"]
11
+ gemfile: [ kramdown2 ]
12
+ env:
13
+ BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}.gemfile
14
+ runs-on: ${{ matrix.os }}
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - run: bundle exec rake test
22
+
23
+ test-kramdown1:
24
+ strategy:
25
+ fail-fast: false
26
+ matrix:
27
+ os: [ubuntu-latest]
28
+ ruby: [2.6, 2.7]
29
+ gemfile: [ kramdown1 ]
30
+ env:
31
+ BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}.gemfile
11
32
  runs-on: ${{ matrix.os }}
12
33
  steps:
13
34
  - uses: actions/checkout@v2
data/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ # Changelog
2
+
3
+ ## Version 0.3.2
4
+
5
+ - Parse ordered and unordered lists
6
+ - Parse image with link
7
+
8
+ ## Version 0.3.1
9
+
10
+ - Add binaries `html2primisc`, `markdown2prismic` and `primisc2markdown`.
11
+ - Add parsed embed support
12
+
13
+ ## Version 0.3.0
14
+
15
+ - Relax the kramdown dependency to allows the version 2.
16
+ - XML elements support have been removed.
17
+
18
+ ## Version 0.2.2
19
+
20
+ - Convert links with only an image inside. See #1.
21
+
22
+ ## Version 0.2.1
23
+
24
+ - Parse embed elements
25
+ - Convert inline code
26
+
27
+ ## Version 0.2.0
28
+
29
+ - Add parser to convert prismic to markdown
30
+
31
+ ## Version 0.1.2
32
+
33
+ - fix output with empty paragraph
34
+
35
+ ## Version 0.1.1
36
+
37
+ - Fix json of the image
38
+
39
+ ## Version 0.1.0
40
+
41
+ - Initial version
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
data/Gemfile.lock CHANGED
@@ -1,15 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kramdown-prismic (0.2.1)
5
- kramdown (~> 1.0)
4
+ kramdown-prismic (0.3.2)
5
+ kramdown (>= 1, < 3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- kramdown (1.17.0)
10
+ kramdown (2.3.1)
11
+ rexml
11
12
  minitest (5.10.3)
12
- rake (12.2.1)
13
+ rake (13.0.6)
14
+ rexml (3.2.5)
13
15
 
14
16
  PLATFORMS
15
17
  ruby
@@ -17,7 +19,7 @@ PLATFORMS
17
19
  DEPENDENCIES
18
20
  kramdown-prismic!
19
21
  minitest (~> 5.0)
20
- rake (~> 12.0)
22
+ rake (~> 13.0)
21
23
 
22
24
  BUNDLED WITH
23
25
  1.17.2
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # Kramdown Prismic
2
2
 
3
- A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and back.
3
+ A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and the other way around.
4
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.
5
+ A useful usage is to convert markdown documents to prismic for [import purpose][prismic-import]. Then you can even convert prismic format back to markdown.
6
6
 
7
7
  ## Status
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 (missing list item for instance).
11
+ The parser part is quite new and not feature complete.
12
12
 
13
13
  ## Install
14
14
 
@@ -18,6 +18,22 @@ gem 'kramdown-prismic', '~> 0.1'
18
18
 
19
19
  ## Usage
20
20
 
21
+ ### With executables
22
+
23
+ *markdown2prismic*:
24
+
25
+ markdown2prismic $'# My Title\n\nHello world'
26
+
27
+ *html2prismic*:
28
+
29
+ html2prismic '<h1>My Title</h1><p>Hello world</p>'
30
+
31
+ *prismic2markdown*:
32
+
33
+ prismic2markdown '[{"type":"heading1","content":{"text":"My Title","spans":[]}},{"type":"paragraph","content":{"text":"Test","spans":[]}}]'
34
+
35
+ ### As a library
36
+
21
37
  **Convert kramdown documents to Prismic**
22
38
 
23
39
  ```ruby
@@ -123,3 +139,4 @@ MIT
123
139
 
124
140
  [Kramdown]: https://kramdown.gettalong.org/
125
141
  [prismic]: https://prismic.io/
142
+ [prismic-import]: https://prismic.io/docs/core-concepts/how-to-import-content
data/Rakefile CHANGED
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake/testtask'
2
4
 
3
5
  Rake::TestTask.new do |t|
4
- t.libs << "test"
6
+ t.libs << 'test'
5
7
  t.test_files = FileList['test/*_test.rb']
6
8
  t.verbose = true
7
9
  end
8
-
data/bin/html2prismic ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kramdown-prismic'
4
+ require 'json'
5
+
6
+ print Kramdown::Document.new(ARGV[0], input: :html).to_prismic.to_json.to_s
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kramdown-prismic'
4
+ require 'json'
5
+
6
+ print Kramdown::Document.new(ARGV[0], input: :markdown).to_prismic.to_json.to_s
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kramdown-prismic'
4
+ require 'json'
5
+
6
+ source = JSON.parse(ARGV[0], symbolize_names: true)
7
+
8
+ print Kramdown::Document.new(source, input: :prismic).to_kramdown
@@ -1,21 +1,25 @@
1
- # encoding: utf-8
2
- require File.expand_path('../lib/kramdown-prismic/version', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('lib/kramdown-prismic/version', __dir__)
3
4
 
4
5
  Gem::Specification.new do |s|
5
6
  s.name = 'kramdown-prismic'
6
7
  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"]
8
+ s.summary = 'A Kramdown converter to convert documents into prismic rich text format and the other way around.'
9
+ s.description = 'A Kramdown converter to convert documents into prismic rich text format and the other way around.'
10
+ s.authors = ['François de Metz']
10
11
  s.email = 'francois@2metz.fr'
11
12
 
13
+ s.executables << 'markdown2prismic'
14
+ s.executables << 'html2prismic'
15
+ s.executables << 'prismic2markdown'
12
16
  s.files = `git ls-files`.split("\n")
13
17
  s.test_files = `git ls-files -- test/*`.split("\n")
14
18
 
15
19
  s.homepage = 'https://github.com/stormz/kramdown-prismic'
16
20
  s.license = 'MIT'
17
21
 
18
- s.add_dependency "kramdown", "~> 1.0"
19
- s.add_development_dependency "minitest", "~> 5.0"
20
- s.add_development_dependency "rake", "~> 12.0"
22
+ s.add_dependency 'kramdown', '>= 1', '< 3'
23
+ s.add_development_dependency 'minitest', '~> 5.0'
24
+ s.add_development_dependency 'rake', '~> 13.0'
21
25
  end
data/kramdown1.gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+ gem 'kramdown', '~> 1.0'
data/kramdown2.gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+ gem 'kramdown', '~> 2.0'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'kramdown'
2
4
 
3
5
  require 'kramdown/parser/prismic'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module KramdownPrismic
2
- VERSION = "0.2.1"
4
+ VERSION = '0.3.2'
3
5
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'kramdown/converter/base'
2
4
 
3
5
  module Kramdown
4
6
  module Converter
5
7
  class Prismic < Base
6
8
  def convert(root)
7
- cleanup_ast(root).map { |child|
9
+ cleanup_ast(root).map do |child|
8
10
  convert_element(child)
9
- }.compact.flatten
11
+ end.compact.flatten
10
12
  end
11
13
 
12
14
  private
@@ -21,20 +23,23 @@ module Kramdown
21
23
  end
22
24
 
23
25
  def remove_blanks(root)
24
- root.children = root.children.inject([]) do |memo, child|
26
+ root.children = root.children.each_with_object([]) do |child, memo|
25
27
  unless child.type == :blank
26
28
  remove_blanks(child)
27
29
  memo << child
28
30
  end
29
- memo
30
31
  end
31
32
  end
32
33
 
33
34
  def extract_non_nestable_elements(child, elements)
34
- child.children = child.children.inject([]) do |memo, element|
35
- if element.type == :img
35
+ child.children = child.children.each_with_object([]) do |element, memo|
36
+ if element.type == :a && element.children.size == 1 && element.children.first.type == :img
37
+ elements << element
38
+ elsif element.type == :img
36
39
  elements << element
37
- warning('images inside content will be moved to the top level and may be rendered differently') if child.children.size > 1
40
+ if child.children.size > 1
41
+ warning('images inside content will be moved to the top level and may be rendered differently')
42
+ end
38
43
  elsif element.type == :ul
39
44
  warning('nested list moved to the top level')
40
45
  elements << element
@@ -43,7 +48,6 @@ module Kramdown
43
48
  memo << element
44
49
  extract_non_nestable_elements(element, elements)
45
50
  end
46
- memo
47
51
  end
48
52
  end
49
53
 
@@ -59,9 +63,10 @@ module Kramdown
59
63
  end
60
64
 
61
65
  def convert_p(element)
62
- return nil if element.children.size == 0
66
+ return nil if element.children.size.zero?
67
+
63
68
  {
64
- type: "paragraph",
69
+ type: 'paragraph',
65
70
  content: extract_content(element)
66
71
  }
67
72
  end
@@ -104,8 +109,7 @@ module Kramdown
104
109
  }
105
110
  end
106
111
 
107
- def convert_hr(element)
108
- end
112
+ def convert_hr(element); end
109
113
 
110
114
  def convert_img(element)
111
115
  {
@@ -116,57 +120,91 @@ module Kramdown
116
120
  },
117
121
  data: {
118
122
  origin: {
119
- url: element.attr["src"]
123
+ url: element.attr['src']
124
+ },
125
+ alt: element.attr['alt']
126
+ }
127
+ }
128
+ end
129
+
130
+ # This can only apply when an link with an image inside has been detected
131
+ def convert_a(element)
132
+ image = element.children.first
133
+ {
134
+ type: 'image',
135
+ content: {
136
+ text: '',
137
+ spans: []
138
+ },
139
+ data: {
140
+ origin: {
141
+ url: image.attr['src']
120
142
  },
121
- alt: element.attr["alt"]
143
+ alt: image.attr['alt'],
144
+ linkTo: {
145
+ url: element.attr['href']
146
+ }
122
147
  }
123
148
  }
124
149
  end
125
150
 
126
151
  def convert_html_element(element)
127
- warning('translating html elements is not supported')
128
- nil
152
+ if element.value == 'iframe'
153
+ {
154
+ content: {
155
+ spans: [],
156
+ text: ''
157
+ },
158
+ type: 'embed',
159
+ data: {
160
+ embed_url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
161
+ type: 'link'
162
+ }
163
+ }
164
+ else
165
+ warning('translating html elements is not supported')
166
+ nil
167
+ end
129
168
  end
130
169
 
131
- def convert_table(element)
170
+ def convert_table(_element)
132
171
  warning('translating table is not supported')
133
172
  nil
134
173
  end
135
174
 
136
- def convert_dl(element)
175
+ def convert_dl(_element)
137
176
  warning('translating dl is not supported')
138
177
  nil
139
178
  end
140
179
 
141
- def convert_math(element)
180
+ def convert_math(_element)
142
181
  warning('translating math is not supported')
143
182
  nil
144
183
  end
145
184
 
146
- def convert_comment(element)
185
+ def convert_comment(_element)
147
186
  warning('translating comment is not supported')
148
187
  nil
149
188
  end
150
189
 
151
- def convert_xml_comment(element)
152
- warning('translating xml_comment is not supported')
153
- nil
154
- end
155
-
156
- def convert_xml_pi(element)
157
- warning('translating xml_pi is not supported')
190
+ def convert_raw(_element)
191
+ warning('translating raw is not supported')
158
192
  nil
159
193
  end
160
194
 
161
- def convert_raw(element)
162
- warning('translating raw is not supported')
163
- nil
195
+ def convert_text(element)
196
+ {
197
+ type: 'paragraph',
198
+ content: {
199
+ text: element.value,
200
+ spans: []
201
+ }
202
+ }
164
203
  end
165
204
 
166
- def extract_content(element, memo={text: '', spans: []})
167
- element.children.inject(memo) do |memo2, child|
205
+ def extract_content(element, memo = { text: '', spans: [] })
206
+ element.children.each_with_object(memo) do |child, memo2|
168
207
  send("extract_span_#{child.type}", child, memo2)
169
- memo2
170
208
  end
171
209
  end
172
210
 
@@ -187,7 +225,7 @@ module Kramdown
187
225
  insert_span(element, memo, {
188
226
  type: 'hyperlink',
189
227
  data: {
190
- url: element.attr["href"]
228
+ url: element.attr['href']
191
229
  }
192
230
  })
193
231
  end
@@ -208,7 +246,7 @@ module Kramdown
208
246
  extract_content(element, memo)
209
247
  end
210
248
 
211
- def extract_span_br(element, memo)
249
+ def extract_span_br(_element, memo)
212
250
  memo[:text] += "\n"
213
251
  end
214
252
 
@@ -217,11 +255,11 @@ module Kramdown
217
255
  memo[:text] += element.value
218
256
  end
219
257
 
220
- def extract_span_html_element(element, memo)
258
+ def extract_span_html_element(_element, _memo)
221
259
  warning('translating html elements is not supported')
222
260
  end
223
261
 
224
- def extract_span_footnote(element, memo)
262
+ def extract_span_footnote(_element, _memo)
225
263
  warning('translating footnote is not supported')
226
264
  end
227
265
 
@@ -231,16 +269,16 @@ module Kramdown
231
269
  end
232
270
 
233
271
  TYPOGRAPHIC_SYMS = {
234
- mdash: [::Kramdown::Utils::Entities.entity('mdash')],
235
- ndash: [::Kramdown::Utils::Entities.entity('ndash')],
236
- hellip: [::Kramdown::Utils::Entities.entity('hellip')],
237
- laquo_space: [::Kramdown::Utils::Entities.entity('laquo'), ::Kramdown::Utils::Entities.entity('nbsp')],
238
- raquo_space: [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')],
239
- laquo: [::Kramdown::Utils::Entities.entity('laquo')],
240
- raquo: [::Kramdown::Utils::Entities.entity('raquo')]
241
- }
272
+ mdash: [Utils::Entities.entity('mdash')],
273
+ ndash: [Utils::Entities.entity('ndash')],
274
+ hellip: [Utils::Entities.entity('hellip')],
275
+ laquo_space: [Utils::Entities.entity('laquo'), Utils::Entities.entity('nbsp')],
276
+ raquo_space: [Utils::Entities.entity('nbsp'), Utils::Entities.entity('raquo')],
277
+ laquo: [Utils::Entities.entity('laquo')],
278
+ raquo: [Utils::Entities.entity('raquo')]
279
+ }.freeze
242
280
  def extract_span_typographic_sym(element, memo)
243
- value = TYPOGRAPHIC_SYMS[element.value].map {|e| e.char }.join('')
281
+ value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
244
282
  memo[:text] += value
245
283
  end
246
284
 
@@ -249,7 +287,7 @@ module Kramdown
249
287
  end
250
288
 
251
289
  def extract_span_smart_quote(element, memo)
252
- memo[:text] += ::Kramdown::Utils::Entities.entity(element.value.to_s).char
290
+ memo[:text] += Utils::Entities.entity(element.value.to_s).char
253
291
  end
254
292
  end
255
293
  end