slate_serializer 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/slate_serializer/html.rb +35 -15
- data/lib/slate_serializer/plain.rb +5 -11
- data/lib/slate_serializer/version.rb +1 -1
- data/slate_serializer.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 516354941eb7b361c8a0caecde0f1c2fbb632e4a0eddbd9357b8c837ee0bfcf9
|
4
|
+
data.tar.gz: 201cf1578c19867963fdd6e01dd4ea035bb30ab382ec665e252ee30fc6ff2f83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bb3f0f0661a972f8611f3fd789bcb724301d5dad998dd90524fc1816d86f3dd26e6b28ebc78b03cd0f4d94bcd536b24ef5af1b84024baf818cec5189b8366ef
|
7
|
+
data.tar.gz: 91e690457e2c9f62f33c5bae950a04e2f5494ecbcd605544e7207c0a2a6fd3c2df01e7693ac5fd10a3ad36936262d082273086d9d0419163284d476e6c52161b
|
@@ -40,7 +40,7 @@ module SlateSerializer
|
|
40
40
|
# @option options [Array] :inline_elemnts List of inline types
|
41
41
|
# @option options [Array] :mark_elemnts List of mark types
|
42
42
|
def deserializer(html, options = {})
|
43
|
-
return
|
43
|
+
return empty_state if html.nil?
|
44
44
|
|
45
45
|
self.elements = options[:elements] || ELEMENTS
|
46
46
|
self.block_elements = options[:block_elements] || BLOCK_ELEMENTS
|
@@ -54,6 +54,7 @@ module SlateSerializer
|
|
54
54
|
|
55
55
|
{
|
56
56
|
document: {
|
57
|
+
object: 'document',
|
57
58
|
nodes: nodes
|
58
59
|
}
|
59
60
|
}
|
@@ -66,7 +67,7 @@ module SlateSerializer
|
|
66
67
|
def element_to_node(element)
|
67
68
|
type = convert_name_to_type(element)
|
68
69
|
|
69
|
-
nodes = element.children.
|
70
|
+
nodes = element.children.flat_map do |child|
|
70
71
|
next if child.text.strip == '' && child.type == 'img'
|
71
72
|
|
72
73
|
if block?(child)
|
@@ -76,7 +77,7 @@ module SlateSerializer
|
|
76
77
|
else
|
77
78
|
next if child.text.strip == ''
|
78
79
|
|
79
|
-
|
80
|
+
element_to_texts(child)
|
80
81
|
end
|
81
82
|
end.compact
|
82
83
|
|
@@ -90,8 +91,8 @@ module SlateSerializer
|
|
90
91
|
|
91
92
|
def element_to_inline(element)
|
92
93
|
type = convert_name_to_type(element)
|
93
|
-
nodes = element.children.
|
94
|
-
|
94
|
+
nodes = element.children.flat_map do |child|
|
95
|
+
element_to_texts(child)
|
95
96
|
end
|
96
97
|
|
97
98
|
{
|
@@ -102,29 +103,26 @@ module SlateSerializer
|
|
102
103
|
}
|
103
104
|
end
|
104
105
|
|
105
|
-
def
|
106
|
-
|
106
|
+
def element_to_texts(element)
|
107
|
+
nodes = []
|
107
108
|
mark = convert_name_to_mark(element.name)
|
108
109
|
|
109
110
|
if element.class == Nokogiri::XML::Element
|
110
111
|
element.children.each do |child|
|
111
|
-
|
112
|
+
nodes << element_to_text(child, mark)
|
112
113
|
end
|
113
114
|
else
|
114
|
-
|
115
|
+
nodes << element_to_text(element)
|
115
116
|
end
|
116
117
|
|
117
|
-
|
118
|
-
leaves: leaves,
|
119
|
-
object: 'text'
|
120
|
-
}
|
118
|
+
nodes
|
121
119
|
end
|
122
120
|
|
123
|
-
def
|
121
|
+
def element_to_text(element, mark = nil)
|
124
122
|
marks = [mark, convert_name_to_mark(element.name)].compact
|
125
123
|
{
|
126
124
|
marks: marks,
|
127
|
-
object: '
|
125
|
+
object: 'text',
|
128
126
|
text: element.text
|
129
127
|
}
|
130
128
|
end
|
@@ -153,6 +151,28 @@ module SlateSerializer
|
|
153
151
|
def inline?(element)
|
154
152
|
inline_elements.include?(element.name)
|
155
153
|
end
|
154
|
+
|
155
|
+
def empty_state
|
156
|
+
{
|
157
|
+
document: {
|
158
|
+
object: 'document',
|
159
|
+
nodes: [
|
160
|
+
{
|
161
|
+
data: {},
|
162
|
+
object: 'block',
|
163
|
+
type: 'paragraph',
|
164
|
+
nodes: [
|
165
|
+
{
|
166
|
+
marks: [],
|
167
|
+
object: 'text',
|
168
|
+
text: ''
|
169
|
+
}
|
170
|
+
]
|
171
|
+
}
|
172
|
+
]
|
173
|
+
}
|
174
|
+
}
|
175
|
+
end
|
156
176
|
end
|
157
177
|
end
|
158
178
|
end
|
@@ -7,7 +7,7 @@ module SlateSerializer
|
|
7
7
|
# @param text format [String] the text
|
8
8
|
# return [Hash] Slate document
|
9
9
|
def deserializer(text)
|
10
|
-
|
10
|
+
text = '' if text.nil?
|
11
11
|
|
12
12
|
lines = split_text_into_lines(text)
|
13
13
|
{
|
@@ -47,7 +47,7 @@ module SlateSerializer
|
|
47
47
|
lines.shift(index + 1)
|
48
48
|
end
|
49
49
|
|
50
|
-
blocks.reject { |block| block == '' }
|
50
|
+
blocks.length == 1 ? blocks : blocks.reject { |block| block == '' }
|
51
51
|
end
|
52
52
|
|
53
53
|
def convert_lines_into_nodes(lines)
|
@@ -58,14 +58,8 @@ module SlateSerializer
|
|
58
58
|
data: {},
|
59
59
|
nodes: [
|
60
60
|
object: 'text',
|
61
|
-
|
62
|
-
|
63
|
-
{
|
64
|
-
object: 'leaf',
|
65
|
-
text: line,
|
66
|
-
marks: []
|
67
|
-
}
|
68
|
-
]
|
61
|
+
text: line,
|
62
|
+
marks: []
|
69
63
|
]
|
70
64
|
}
|
71
65
|
end
|
@@ -75,7 +69,7 @@ module SlateSerializer
|
|
75
69
|
if node[:object] == 'document' || node[:object] == 'block'
|
76
70
|
node[:nodes].map { |n| serialize_node(n, options) }.join(options[:delimiter])
|
77
71
|
else
|
78
|
-
node[:
|
72
|
+
node[:text]
|
79
73
|
end
|
80
74
|
end
|
81
75
|
end
|
data/slate_serializer.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.version = SlateSerializer::VERSION
|
8
8
|
spec.authors = ['Wesley Stam']
|
9
9
|
spec.email = ['wesley@stam.me']
|
10
|
-
spec.license =
|
10
|
+
spec.license = 'MIT'
|
11
11
|
|
12
12
|
spec.summary = 'Serializer for Slate documents written in Ruby'
|
13
13
|
spec.homepage = 'https://github.com/wesleystam/slate_serializer'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slate_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wesley Stam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|