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
|
# File 'lib/prosereflect/parser.rb', line 11
def self.parse_node(data)
return nil unless data.is_a?(Hash)
type = data["type"]
text = data["text"]
attrs = data["attrs"]
marks_data = data["marks"]
node_class = case type
when "doc"
Document
when "paragraph"
Paragraph
when "text"
Text
when "table"
Table
when "table_row"
TableRow
when "table_cell"
TableCell
when "table_header"
TableHeader
when "hard_break"
HardBreak
when "heading"
Heading
when "ordered_list"
OrderedList
when "bullet_list"
BulletList
when "list_item"
ListItem
when "blockquote"
Blockquote
when "horizontal_rule"
HorizontalRule
when "image"
Image
when "code_block"
CodeBlock
when "code_block_wrapper"
CodeBlockWrapper
when "user"
User
else
Node
end
if type == "text"
node = Text.new(text: text)
else
node = node_class.create(attrs)
if data["content"].is_a?(Array)
data["content"].each do |content_data|
child_node = parse_node(content_data)
node.add_child(child_node) if child_node
end
end
end
case type
when "ordered_list"
node.start = attrs["start"].to_i if attrs && attrs["start"]
when "bullet_list"
node.bullet_style = attrs["bullet_style"] if attrs && attrs["bullet_style"]
when "blockquote"
node.citation = attrs["cite"] if attrs && attrs["cite"]
when "horizontal_rule"
if attrs
node.style = attrs["border_style"] if attrs["border_style"]
node.width = attrs["width"] if attrs["width"]
node.thickness = attrs["thickness"].to_i if attrs["thickness"]
end
when "image"
if attrs
node.src = attrs["src"] if attrs["src"]
node.alt = attrs["alt"] if attrs["alt"]
node.title = attrs["title"] if attrs["title"]
node.dimensions = [attrs["width"]&.to_i, attrs["height"]&.to_i]
end
when "table_header"
if attrs
node.scope = attrs["scope"] if attrs["scope"]
node.abbr = attrs["abbr"] if attrs["abbr"]
node.colspan = attrs["colspan"] if attrs["colspan"]
end
when "code_block"
if attrs
node.language = attrs["language"] if attrs["language"]
node.line_numbers = attrs["line_numbers"] if attrs["line_numbers"]
end
end
node.marks = marks_data if marks_data && !marks_data.empty?
node
end
|