-
Notifications
You must be signed in to change notification settings - Fork 11
/
prismic.rb
331 lines (285 loc) · 8.52 KB
/
prismic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# frozen_string_literal: true
require 'kramdown/converter/base'
module Kramdown
module Converter
class Prismic < Base
def convert(root)
cleanup_ast(root).map do |child|
convert_element(child)
end.compact.flatten
end
private
def cleanup_ast(root)
remove_blanks(root)
root.children.map do |child|
elements = []
extract_non_nestable_elements(child, elements)
[child, elements]
end.flatten.compact
end
def remove_blanks(root)
root.children = root.children.each_with_object([]) do |child, memo|
unless child.type == :blank
remove_blanks(child)
memo << child
end
end
end
def extract_non_nestable_elements(child, elements)
child.children = child.children.each_with_object([]) do |element, memo|
if element.type == :a && element.children.size == 1 && element.children.first.type == :img
elements << element
elsif element.type == :img
elements << element
if child.children.size > 1
warning('images inside content will be moved to the top level and may be rendered differently')
end
elsif [:ul, :ol].include?(element.type)
warning('nested list moved to the top level')
elements << element
extract_non_nestable_elements(element, elements)
elsif element.type == :header
warning('header moved to the top level')
elements << element
extract_non_nestable_elements(element, elements)
else
memo << element
extract_non_nestable_elements(element, elements)
end
end
end
def convert_element(element)
send("convert_#{element.type}", element)
end
def convert_header(element)
{
type: "heading#{element.options[:level]}",
content: extract_content(element)
}
end
def convert_p(element)
return nil if element.children.size.zero?
{
type: 'paragraph',
content: extract_content(element)
}
end
def convert_ol(element)
convert_list(element, 'o-list-item')
end
def convert_ul(element)
convert_list(element, 'list-item')
end
def convert_list(element, type)
element.children.map do |child|
convert_li(type, child)
end
end
def convert_li(type, element)
{
type: type,
content: extract_content(element)
}
end
def convert_codeblock(element)
{
type: 'preformatted',
content: {
text: element.value,
spans: []
}
}
end
def convert_blockquote(element)
{
type: 'preformatted',
content: extract_content(element)
}
end
def convert_hr(element); end
def convert_br(element); end
def convert_img(element)
{
type: 'image',
content: {
text: '',
spans: []
},
data: {
origin: {
url: element.attr['src']
},
alt: element.attr['alt']
}
}
end
# This can only apply when an link with an image inside has been detected
def convert_a(element)
image = element.children.first
{
type: 'image',
content: {
text: '',
spans: []
},
data: {
origin: {
url: image.attr['src']
},
alt: image.attr['alt'],
linkTo: {
url: element.attr['href']
}
}
}
end
def convert_strong(element)
convert_sub_html_element(element, 'strong')
end
def convert_em(element)
convert_sub_html_element(element, 'em')
end
def convert_sub_html_element(element, type)
content = extract_content(element)
content[:spans].push({ type: type, start: 0, end: content[:text].size })
{
type: 'paragraph',
content: content
}
end
def convert_html_element(element)
if element.value == 'iframe'
{
content: {
spans: [],
text: ''
},
type: 'embed',
data: {
embed_url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
type: 'link'
}
}
else
warning('translating html elements is not supported')
nil
end
end
def convert_table(_element)
warning('translating table is not supported')
nil
end
def convert_dl(_element)
warning('translating dl is not supported')
nil
end
def convert_math(_element)
warning('translating math is not supported')
nil
end
def convert_comment(_element)
warning('translating comment is not supported')
nil
end
def convert_xml_comment(_element)
warning('translating xml comment is not supported')
nil
end
def convert_raw(_element)
warning('translating raw is not supported')
nil
end
def convert_text(element)
{
type: 'paragraph',
content: {
text: element.value,
spans: []
}
}
end
def extract_content(element, memo = { text: '', spans: [] })
element.children.each_with_object(memo) do |child, memo2|
send("extract_span_#{child.type}", child, memo2)
end
end
def insert_span(element, memo, span)
span[:start] = memo[:text].size
extract_content(element, memo)
span[:end] = memo[:text].size
memo[:spans] << span
memo
end
def extract_span_text(element, memo)
memo[:text] += element.value
memo
end
def extract_span_a(element, memo)
target = element.attr['target']
insert_span(element, memo, {
type: 'hyperlink',
data: {
url: element.attr['href'],
**(target ? { target: target } : {}),
}
})
end
def extract_span_strong(element, memo)
insert_span(element, memo, {
type: 'strong'
})
end
def extract_span_em(element, memo)
insert_span(element, memo, {
type: 'em'
})
end
def extract_span_p(element, memo)
extract_content(element, memo)
end
def extract_span_br(_element, memo)
memo[:text] += "\n"
end
def extract_span_codespan(element, memo)
warning('translating inline code is not supported')
memo[:text] += element.value
end
def extract_span_html_element(element, memo)
if respond_to?("extract_span_#{element.value}", true)
send("extract_span_#{element.value}", element, memo)
else
warning("translating html element '#{element.value}' is not supported")
end
end
def extract_span_footnote(_element, _memo)
warning('translating footnote is not supported')
end
def extract_span_abbreviation(element, memo)
warning('translating abbreviation is not supported')
memo[:text] += element.value
end
def extract_span_xml_comment(element, memo)
warning('translating xml comment is not supported')
end
TYPOGRAPHIC_SYMS = {
mdash: [Utils::Entities.entity('mdash')],
ndash: [Utils::Entities.entity('ndash')],
hellip: [Utils::Entities.entity('hellip')],
laquo_space: [Utils::Entities.entity('laquo'), Utils::Entities.entity('nbsp')],
raquo_space: [Utils::Entities.entity('nbsp'), Utils::Entities.entity('raquo')],
laquo: [Utils::Entities.entity('laquo')],
raquo: [Utils::Entities.entity('raquo')]
}.freeze
def extract_span_typographic_sym(element, memo)
value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
memo[:text] += value
end
def extract_span_entity(element, memo)
memo[:text] += element.value.char
end
def extract_span_smart_quote(element, memo)
memo[:text] += Utils::Entities.entity(element.value.to_s).char
end
end
end
end