kramdown-prismic 0.2.2 → 0.3.3

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: 3229f319815e16e39608a957bb9f596e623a6ef605be3955c46723de2aedbb98
4
- data.tar.gz: '08f9b62d41e583e99d51373ddb875352abec6226f2a584c6f9d21b61c15ed26f'
3
+ metadata.gz: ba6d3ad52704c02e2ec102cc69ca216f02c464ba6cde85c2f28024c66448a985
4
+ data.tar.gz: 64fdd7184a0184c10b72acb00d2109f1b3512fc53392327a5f537d091d5796ce
5
5
  SHA512:
6
- metadata.gz: 8c3bf867920d85af049006027d70f156cca4c0863cd8c40784eb473121153652acc368906e1100fabdb90d36a04f73c9a966090c8a68e7d55fc8c55f0e3610e7
7
- data.tar.gz: e4daf1cfe1886f809f0234d7a181b22e677be78401f06c4f4657d89a519e61b13d56ce1dcabcad11ed13bbe782f359c3214c5650da098b8b72c65fccdeb230b4
6
+ metadata.gz: ea6f89410664900030c17478ed4cce6fb6a499974ef775f421fd1b720e7f28e123526eb1893889f42d8964928a1c2139387f2dae408a3ea19dffc50805430918
7
+ data.tar.gz: 95c6df198bfdacb61f24606575d4fd5d517b1285750a4dd463f15cc0416513759aec67183eb3820c64c221d5154acbb826e0a74b7e9f797ddfff7cbb2bbae165
@@ -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,45 @@
1
+ # Changelog
2
+
3
+ ## Version 0.3.3
4
+
5
+ - Renable converting xml comments
6
+
7
+ ## Version 0.3.2
8
+
9
+ - Parse ordered and unordered lists
10
+ - Parse image with link
11
+
12
+ ## Version 0.3.1
13
+
14
+ - Add binaries `html2primisc`, `markdown2prismic` and `primisc2markdown`.
15
+ - Add parsed embed support
16
+
17
+ ## Version 0.3.0
18
+
19
+ - Relax the kramdown dependency to allows the version 2.
20
+ - XML elements support have been removed.
21
+
22
+ ## Version 0.2.2
23
+
24
+ - Convert links with only an image inside. See #1.
25
+
26
+ ## Version 0.2.1
27
+
28
+ - Parse embed elements
29
+ - Convert inline code
30
+
31
+ ## Version 0.2.0
32
+
33
+ - Add parser to convert prismic to markdown
34
+
35
+ ## Version 0.1.2
36
+
37
+ - fix output with empty paragraph
38
+
39
+ ## Version 0.1.1
40
+
41
+ - Fix json of the image
42
+
43
+ ## Version 0.1.0
44
+
45
+ - 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.2)
5
- kramdown (~> 1.0)
4
+ kramdown-prismic (0.3.3)
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
13
  rake (13.0.6)
14
+ rexml (3.2.5)
13
15
 
14
16
  PLATFORMS
15
17
  ruby
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", "~> 13.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.2"
4
+ VERSION = '0.3.3'
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,22 +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 == :a && element.children.size === 1 && element.children.first.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
36
37
  elements << element
37
38
  elsif element.type == :img
38
39
  elements << element
39
- 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
40
43
  elsif element.type == :ul
41
44
  warning('nested list moved to the top level')
42
45
  elements << element
@@ -45,7 +48,6 @@ module Kramdown
45
48
  memo << element
46
49
  extract_non_nestable_elements(element, elements)
47
50
  end
48
- memo
49
51
  end
50
52
  end
51
53
 
@@ -61,9 +63,10 @@ module Kramdown
61
63
  end
62
64
 
63
65
  def convert_p(element)
64
- return nil if element.children.size == 0
66
+ return nil if element.children.size.zero?
67
+
65
68
  {
66
- type: "paragraph",
69
+ type: 'paragraph',
67
70
  content: extract_content(element)
68
71
  }
69
72
  end
@@ -106,8 +109,7 @@ module Kramdown
106
109
  }
107
110
  end
108
111
 
109
- def convert_hr(element)
110
- end
112
+ def convert_hr(element); end
111
113
 
112
114
  def convert_img(element)
113
115
  {
@@ -118,9 +120,9 @@ module Kramdown
118
120
  },
119
121
  data: {
120
122
  origin: {
121
- url: element.attr["src"]
123
+ url: element.attr['src']
122
124
  },
123
- alt: element.attr["alt"]
125
+ alt: element.attr['alt']
124
126
  }
125
127
  }
126
128
  end
@@ -147,49 +149,67 @@ module Kramdown
147
149
  end
148
150
 
149
151
  def convert_html_element(element)
150
- warning('translating html elements is not supported')
151
- 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
152
168
  end
153
169
 
154
- def convert_table(element)
170
+ def convert_table(_element)
155
171
  warning('translating table is not supported')
156
172
  nil
157
173
  end
158
174
 
159
- def convert_dl(element)
175
+ def convert_dl(_element)
160
176
  warning('translating dl is not supported')
161
177
  nil
162
178
  end
163
179
 
164
- def convert_math(element)
180
+ def convert_math(_element)
165
181
  warning('translating math is not supported')
166
182
  nil
167
183
  end
168
184
 
169
- def convert_comment(element)
185
+ def convert_comment(_element)
170
186
  warning('translating comment is not supported')
171
187
  nil
172
188
  end
173
189
 
174
- def convert_xml_comment(element)
175
- warning('translating xml_comment is not supported')
190
+ def convert_xml_comment(_element)
191
+ warning('translating xml comment is not supported')
176
192
  nil
177
193
  end
178
194
 
179
- def convert_xml_pi(element)
180
- warning('translating xml_pi is not supported')
195
+ def convert_raw(_element)
196
+ warning('translating raw is not supported')
181
197
  nil
182
198
  end
183
199
 
184
- def convert_raw(element)
185
- warning('translating raw is not supported')
186
- nil
200
+ def convert_text(element)
201
+ {
202
+ type: 'paragraph',
203
+ content: {
204
+ text: element.value,
205
+ spans: []
206
+ }
207
+ }
187
208
  end
188
209
 
189
- def extract_content(element, memo={text: '', spans: []})
190
- element.children.inject(memo) do |memo2, child|
210
+ def extract_content(element, memo = { text: '', spans: [] })
211
+ element.children.each_with_object(memo) do |child, memo2|
191
212
  send("extract_span_#{child.type}", child, memo2)
192
- memo2
193
213
  end
194
214
  end
195
215
 
@@ -210,7 +230,7 @@ module Kramdown
210
230
  insert_span(element, memo, {
211
231
  type: 'hyperlink',
212
232
  data: {
213
- url: element.attr["href"]
233
+ url: element.attr['href']
214
234
  }
215
235
  })
216
236
  end
@@ -231,7 +251,7 @@ module Kramdown
231
251
  extract_content(element, memo)
232
252
  end
233
253
 
234
- def extract_span_br(element, memo)
254
+ def extract_span_br(_element, memo)
235
255
  memo[:text] += "\n"
236
256
  end
237
257
 
@@ -240,11 +260,11 @@ module Kramdown
240
260
  memo[:text] += element.value
241
261
  end
242
262
 
243
- def extract_span_html_element(element, memo)
263
+ def extract_span_html_element(_element, _memo)
244
264
  warning('translating html elements is not supported')
245
265
  end
246
266
 
247
- def extract_span_footnote(element, memo)
267
+ def extract_span_footnote(_element, _memo)
248
268
  warning('translating footnote is not supported')
249
269
  end
250
270
 
@@ -253,17 +273,21 @@ module Kramdown
253
273
  memo[:text] += element.value
254
274
  end
255
275
 
276
+ def extract_span_xml_comment(element, memo)
277
+ warning('translating xml comment is not supported')
278
+ end
279
+
256
280
  TYPOGRAPHIC_SYMS = {
257
- mdash: [::Kramdown::Utils::Entities.entity('mdash')],
258
- ndash: [::Kramdown::Utils::Entities.entity('ndash')],
259
- hellip: [::Kramdown::Utils::Entities.entity('hellip')],
260
- laquo_space: [::Kramdown::Utils::Entities.entity('laquo'), ::Kramdown::Utils::Entities.entity('nbsp')],
261
- raquo_space: [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')],
262
- laquo: [::Kramdown::Utils::Entities.entity('laquo')],
263
- raquo: [::Kramdown::Utils::Entities.entity('raquo')]
264
- }
281
+ mdash: [Utils::Entities.entity('mdash')],
282
+ ndash: [Utils::Entities.entity('ndash')],
283
+ hellip: [Utils::Entities.entity('hellip')],
284
+ laquo_space: [Utils::Entities.entity('laquo'), Utils::Entities.entity('nbsp')],
285
+ raquo_space: [Utils::Entities.entity('nbsp'), Utils::Entities.entity('raquo')],
286
+ laquo: [Utils::Entities.entity('laquo')],
287
+ raquo: [Utils::Entities.entity('raquo')]
288
+ }.freeze
265
289
  def extract_span_typographic_sym(element, memo)
266
- value = TYPOGRAPHIC_SYMS[element.value].map {|e| e.char }.join('')
290
+ value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
267
291
  memo[:text] += value
268
292
  end
269
293
 
@@ -272,7 +296,7 @@ module Kramdown
272
296
  end
273
297
 
274
298
  def extract_span_smart_quote(element, memo)
275
- memo[:text] += ::Kramdown::Utils::Entities.entity(element.value.to_s).char
299
+ memo[:text] += Utils::Entities.entity(element.value.to_s).char
276
300
  end
277
301
  end
278
302
  end