kramdown-prismic 0.2.0 → 0.3.1

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
- SHA1:
3
- metadata.gz: ab0262b0f760d68d07f7950d9890a58cf61744f9
4
- data.tar.gz: c787b185094ed5295b4c5bd33ca0c4b00945180f
2
+ SHA256:
3
+ metadata.gz: e34361d43f02e8ee666fb78685a040128617accc25d44221ed0f98e45fcc7eae
4
+ data.tar.gz: 58ceb9f450d5d719b3698dcc37d417d27f6ecff3a8873812426a086bb6296695
5
5
  SHA512:
6
- metadata.gz: 4ced9a6393fd4ef11ec73bee999ed963c8613eac549cfc5355e7685eb2b99e10dda273984247f505751d4f085068a272e1c18eab9e9e3f75dcdc217b2d89bfc6
7
- data.tar.gz: 712aaf67c347bcf0b7310b7972a0acd4534d644c0f7eacb5e25159a4f4d9124c658db7ee9cd66f6a8c43ac75ffc6e391d2c9779be66c383a6c949b81d0ab7eae
6
+ metadata.gz: fdd15d2fe0bb82ab3528f818579efe067f82274727c4f59ec262e899ca44224aa18b3c766f73f7c82396a8d803c93d9d92cfd864c4c24587a2743297cc60e0e1
7
+ data.tar.gz: 4cc36e4dbba12aaad5318267b268773a7010481959f9a4df4ddafaa3fc912cb9bb8830f4ac7fc8709e0c7b38c6ba2fc6a84a3436754c5d044ec4a7ab5be4646e
@@ -0,0 +1,39 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+ jobs:
5
+ test-kramdown2:
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ os: [ubuntu-latest]
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
32
+ runs-on: ${{ matrix.os }}
33
+ steps:
34
+ - uses: actions/checkout@v2
35
+ - uses: ruby/setup-ruby@v1
36
+ with:
37
+ ruby-version: ${{ matrix.ruby }}
38
+ bundler-cache: true
39
+ - run: bundle exec rake test
data/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## Version 0.3.1
4
+
5
+ - Add binaries `html2primisc`, `markdown2prismic` and `primisc2markdown`.
6
+ - Add parsed embed support
7
+
8
+ ## Version 0.3.0
9
+
10
+ - Relax the kramdown dependency to allows the version 2.
11
+ - XML elements support have been removed.
12
+
13
+ ## Version 0.2.2
14
+
15
+ - Convert links with only an image inside. See #1.
16
+
17
+ ## Version 0.2.1
18
+
19
+ - Parse embed elements
20
+ - Convert inline code
21
+
22
+ ## Version 0.2.0
23
+
24
+ - Add parser to convert prismic to markdown
25
+
26
+ ## Version 0.1.2
27
+
28
+ - fix output with empty paragraph
29
+
30
+ ## Version 0.1.1
31
+
32
+ - Fix json of the image
33
+
34
+ ## Version 0.1.0
35
+
36
+ - 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.0)
5
- kramdown (~> 1.0)
4
+ kramdown-prismic (0.3.0)
5
+ kramdown (>= 1, < 3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- kramdown (1.15.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
- 1.14.6
25
+ 1.17.2
data/LICENSE CHANGED
@@ -1,6 +1,7 @@
1
1
  MIT License
2
2
 
3
3
  Copyright (c) 2017 Stormz
4
+ Copyright (c) 2021 François de Metz
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,8 +1,8 @@
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
 
@@ -18,7 +18,23 @@ gem 'kramdown-prismic', '~> 0.1'
18
18
 
19
19
  ## Usage
20
20
 
21
- ### Convert kramdown documents to Prismic
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
+
37
+ **Convert kramdown documents to Prismic**
22
38
 
23
39
  ```ruby
24
40
  require 'kramdown-prismic'
@@ -27,7 +43,7 @@ kramdown = '# Hello world'
27
43
  Kramdown::Document.new(kramdown).to_prismic
28
44
  ```
29
45
 
30
- ### Convert markdown documents to Prismic
46
+ **Convert markdown documents to Prismic**
31
47
 
32
48
  ```ruby
33
49
  require 'kramdown-prismic'
@@ -36,7 +52,7 @@ markdown = '# Hello world'
36
52
  Kramdown::Document.new(markdown, input: :markdown).to_prismic
37
53
  ```
38
54
 
39
- ### Convert HTML documents to Prismic
55
+ **Convert HTML documents to Prismic**
40
56
 
41
57
  ```ruby
42
58
  require 'kramdown-prismic'
@@ -45,7 +61,7 @@ html = '<h1>Hello world</h1>'
45
61
  Kramdown::Document.new(html, input: :html).to_prismic
46
62
  ```
47
63
 
48
- ### Convert Primic to markdown
64
+ **Convert Prismic to markdown**
49
65
 
50
66
  ```ruby
51
67
  require 'kramdown-prismic'
@@ -86,6 +102,9 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
86
102
  | hr | nothing |
87
103
  | img | moved to the top level |
88
104
  | nested list | moved to the top level |
105
+ | entity | converted to unicode |
106
+ | typographic_sym | converted to unicode |
107
+ | smart_quote | converted to unicode |
89
108
  | dl | not supported |
90
109
  | dt | not supported |
91
110
  | dd | not supported |
@@ -97,9 +116,6 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
97
116
  | td | not supported |
98
117
  | math | not supported |
99
118
  | footnote | not supported |
100
- | entity | converted to unicode |
101
- | typographic_sym | converted to unicode |
102
- | smart_quote | converted to unicode |
103
119
  | abbreviation | not supported |
104
120
  | html_element | not supported |
105
121
  | xml_comment | not supported |
@@ -107,9 +123,20 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
107
123
  | comment | not supported |
108
124
  | raw | not supported |
109
125
 
126
+ ## Develop
127
+
128
+ Install dependencies:
129
+
130
+ bundle install
131
+
132
+ Run tests:
133
+
134
+ bundle exec rake test
135
+
110
136
  ## License
111
137
 
112
138
  MIT
113
139
 
114
140
  [Kramdown]: https://kramdown.gettalong.org/
115
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.0"
4
+ VERSION = '0.3.1'
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']
120
124
  },
121
- alt: element.attr["alt"]
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']
142
+ },
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,15 +246,20 @@ 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
 
215
- def extract_span_html_element(element, memo)
253
+ def extract_span_codespan(element, memo)
254
+ warning('translating inline code is not supported')
255
+ memo[:text] += element.value
256
+ end
257
+
258
+ def extract_span_html_element(_element, _memo)
216
259
  warning('translating html elements is not supported')
217
260
  end
218
261
 
219
- def extract_span_footnote(element, memo)
262
+ def extract_span_footnote(_element, _memo)
220
263
  warning('translating footnote is not supported')
221
264
  end
222
265
 
@@ -233,9 +276,9 @@ module Kramdown
233
276
  raquo_space: [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')],
234
277
  laquo: [::Kramdown::Utils::Entities.entity('laquo')],
235
278
  raquo: [::Kramdown::Utils::Entities.entity('raquo')]
236
- }
279
+ }.freeze
237
280
  def extract_span_typographic_sym(element, memo)
238
- value = TYPOGRAPHIC_SYMS[element.value].map {|e| e.char }.join('')
281
+ value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
239
282
  memo[:text] += value
240
283
  end
241
284