Class: Prosereflect::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/parser.rb

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



27
28
29
30
31
# File 'lib/prosereflect/parser.rb', line 27

def self.parse(data)
  return nil unless data.is_a?(Hash)

  parse_node(data)
end

.parse_document(data) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/prosereflect/parser.rb', line 127

def self.parse_document(data)
  raise ArgumentError, 'Input cannot be nil' if data.nil?
  raise ArgumentError, "Input must be a hash, got #{data.class}" unless data.is_a?(Hash)

  document = parse_node(data)

  unless document.is_a?(Document)
    # If the result isn't a Document, create one and add the node as content
    doc = Document.create
    doc.add_child(document) if document
    document = doc
  end

  document
end

.parse_node(data) ⇒ Object



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
# File 'lib/prosereflect/parser.rb', line 33

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']

  # Find the right class based on type
  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 'user'
                 User
               else
                 Node
               end

  if type == 'text'
    node = Text.new(text: text)
  else
    node = node_class.create(attrs)

    # Process content recursively
    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

  # Handle special attributes for specific node types
  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
  end

  node.marks = marks_data if marks_data && !marks_data.empty?

  node
end