Module: Coradoc::Html::Theme::ModernRenderer::Serializers::DocumentSerializer

Defined in:
lib/coradoc/html/theme/modern/serializers/document_serializer.rb

Overview

Serialize Coradoc CoreModel to Vue-compatible data structure

This module converts the Coradoc CoreModel into a JSON-serializable hash structure that can be consumed by Vue.js components.

IMPORTANT: This serializer ONLY handles CoreModel types. All format-specific documents (AsciiDoc, Markdown, etc.) should be transformed to CoreModel before using this serializer.

Class Method Summary collapse

Class Method Details

.build_toc_data_from_core_model(children, level = 1) ⇒ Array

Build TOC data from CoreModel children

Parameters:

  • children (Array)

    Children to process

  • level (Integer) (defaults to: 1)

    Current level

Returns:

  • (Array)

    TOC entries



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 330

def build_toc_data_from_core_model(children, level = 1)
  return [] unless children

  children.each_with_object([]) do |child, result|
    next unless child.is_a?(Coradoc::CoreModel::StructuralElement)

    entry = {
      id: child.id || generate_uid(child),
      title: child.title.to_s,
      level: level,
      children: build_toc_data_from_core_model(child.children, level + 1)
    }

    result << entry
  end
end

.serialize(document) ⇒ Hash

Serialize document to Vue-compatible format

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    Document to serialize

Returns:

  • (Hash)

    Serialized document data

Raises:

  • (ArgumentError)

    if document is not a CoreModel::StructuralElement



23
24
25
26
27
28
29
30
31
32
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 23

def serialize(document)
  unless document.is_a?(Coradoc::CoreModel::StructuralElement)
    raise ArgumentError,
          "Expected CoreModel::StructuralElement, got #{document.class}. " \
          'Transform your document to CoreModel first using the appropriate ' \
          'format transformer (e.g., ToCoreModel for your source format).'
  end

  serialize_core_model_document(document)
end

.serialize_block_content(block) ⇒ Array

Serialize block content

Parameters:

  • block (Coradoc::CoreModel::Block)

    Block to serialize

Returns:

  • (Array)

    Serialized content



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 154

def serialize_block_content(block)
  return [] unless block.content

  case block.content
  when Array
    block.content.map { |el| serialize_core_model_element(el) }.compact
  when Coradoc::CoreModel::InlineElement
    [serialize_core_model_inline(block.content)]
  else
    [{ type: 'text', content: block.content.to_s }]
  end
end

.serialize_core_model_admonition(admonition) ⇒ Hash

Serialize CoreModel admonition

Parameters:

  • admonition (Coradoc::CoreModel::AnnotationBlock)

    Admonition to serialize

Returns:

  • (Hash)

    Serialized admonition



283
284
285
286
287
288
289
290
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 283

def serialize_core_model_admonition(admonition)
  {
    id: generate_uid(admonition),
    type: 'admonition',
    style: admonition.annotation_type || :note,
    content: admonition.content ? [{ type: 'text', content: admonition.content.to_s }] : []
  }
end

.serialize_core_model_block(block) ⇒ Hash

Serialize CoreModel block

Parameters:

  • block (Coradoc::CoreModel::Block)

    Block to serialize

Returns:

  • (Hash)

    Serialized block



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 130

def serialize_core_model_block(block)
  semantic = block.block_semantic_type&.to_sym
  block_type = case semantic
               when :paragraph then 'paragraph'
               when :source_code then 'source'
               when :quote, :verse then 'quote'
               when :example then 'example'
               else 'block'
               end

  {
    id: block.id || generate_uid(block),
    type: block_type,
    block_type: block_type,
    title: block.title,
    content: serialize_block_content(block),
    language: block.language
  }
end

.serialize_core_model_children(children) ⇒ Array

Serialize CoreModel children

Parameters:

  • children (Array, nil)

    Children to serialize

Returns:

  • (Array)

    Serialized children



77
78
79
80
81
82
83
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 77

def serialize_core_model_children(children)
  return [] unless children

  children.map do |child|
    serialize_core_model_element(child)
  end.compact
end

.serialize_core_model_document(document) ⇒ Hash

Serialize CoreModel::StructuralElement document

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    Document to serialize

Returns:

  • (Hash)

    Serialized document data



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 38

def serialize_core_model_document(document)
  {
    id: document.id || generate_uid(document),
    type: 'document',
    header: serialize_core_model_header(document),
    attributes: {},
    sections: serialize_core_model_children(document.children),
    toc: build_toc_data_from_core_model(document.children),
    metadata: {
      title: document.title || 'Untitled Document',
      author: nil,
      revision: nil
    }
  }
end

.serialize_core_model_element(element) ⇒ Hash

Serialize individual CoreModel element

Parameters:

  • element (Object)

    Element to serialize

Returns:

  • (Hash)

    Serialized element



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 89

def serialize_core_model_element(element)
  case element
  when Coradoc::CoreModel::StructuralElement
    serialize_core_model_section(element)
  when Coradoc::CoreModel::Block
    serialize_core_model_block(element)
  when Coradoc::CoreModel::ListBlock
    serialize_core_model_list(element)
  when Coradoc::CoreModel::Table
    serialize_core_model_table(element)
  when Coradoc::CoreModel::Image
    serialize_core_model_image(element)
  when Coradoc::CoreModel::AnnotationBlock
    serialize_core_model_admonition(element)
  when Coradoc::CoreModel::InlineElement
    serialize_core_model_inline(element)
  else
    serialize_generic_element(element)
  end
end

.serialize_core_model_header(document) ⇒ Hash?

Serialize CoreModel header (extracted from title)

Parameters:

  • document (Coradoc::CoreModel::StructuralElement)

    Document

Returns:

  • (Hash, nil)

    Serialized header



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 58

def serialize_core_model_header(document)
  return nil unless document.title

  {
    id: generate_uid(document),
    type: 'header',
    title: {
      type: 'title',
      text: document.title.to_s,
      level: 1
    },
    author: nil
  }
end

.serialize_core_model_image(image) ⇒ Hash

Serialize CoreModel image

Parameters:

  • image (Coradoc::CoreModel::Image)

    Image to serialize

Returns:

  • (Hash)

    Serialized image



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 267

def serialize_core_model_image(image)
  {
    type: 'image',
    src: image.src,
    alt: image.alt,
    title: nil,
    width: image.width,
    height: image.height,
    inline: image.inline
  }
end

.serialize_core_model_inline(element) ⇒ Hash

Serialize CoreModel inline element

Parameters:

  • element (Coradoc::CoreModel::InlineElement)

    Inline element to serialize

Returns:

  • (Hash)

    Serialized inline element



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
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 296

def serialize_core_model_inline(element)
  element_type = case element.inline_type
                 when 'bold' then 'bold'
                 when 'italic' then 'italic'
                 when 'monospace' then 'monospace'
                 when 'link' then 'link'
                 when 'xref' then 'xref'
                 when 'highlight' then 'highlight'
                 when 'strikethrough' then 'strikethrough'
                 when 'underline' then 'underline'
                 when 'subscript' then 'subscript'
                 when 'superscript' then 'superscript'
                 else 'inline'
                 end

  result = {
    type: element_type,
    content: element.text.to_s
  }

  # Add link-specific attributes
  result[:href] = element.target if element.inline_type == 'link' && element.target

  # Add cross-ref specific attributes
  result[:target] = element.target if element.inline_type == 'xref' && element.target

  result
end

.serialize_core_model_list(list) ⇒ Hash

Serialize CoreModel list

Parameters:

  • list (Coradoc::CoreModel::ListBlock)

    List to serialize

Returns:

  • (Hash)

    Serialized list



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 171

def serialize_core_model_list(list)
  list_type = case list.marker_type
              when 'ordered', '1' then 'ordered'
              when 'unordered', '*', '-' then 'unordered'
              when 'definition' then 'definition'
              else 'unordered'
              end

  {
    id: generate_uid(list),
    type: 'list',
    list_type: list_type,
    items: (list.items || []).map do |item|
      serialize_list_item(item)
    end
  }
end

.serialize_core_model_section(section) ⇒ Hash

Serialize CoreModel section

Parameters:

  • section (Coradoc::CoreModel::StructuralElement)

    Section to serialize

Returns:

  • (Hash)

    Serialized section



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 114

def serialize_core_model_section(section)
  level = section.level || 1
  {
    id: section.id || generate_uid(section),
    type: 'section',
    title: section.title ? { type: 'title', text: section.title.to_s, level: level } : nil,
    level: level,
    content: [],
    sections: serialize_core_model_children(section.children)
  }
end

.serialize_core_model_table(table) ⇒ Hash

Serialize CoreModel table

Parameters:

  • table (Coradoc::CoreModel::Table)

    Table to serialize

Returns:

  • (Hash)

    Serialized table



216
217
218
219
220
221
222
223
224
225
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 216

def serialize_core_model_table(table)
  {
    id: table.id || generate_uid(table),
    type: 'table',
    title: nil,
    rows: (table.rows || []).map do |row|
      serialize_table_row(row)
    end
  }
end

.serialize_generic_element(element) ⇒ Hash

Serialize generic element (fallback for unknown types)

Parameters:

  • element (Object)

    Element to serialize

Returns:

  • (Hash)

    Serialized element



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 351

def serialize_generic_element(element)
  text_content = case element
                 when String
                   element
                 else
                   str = element.to_s
                   str.include?('#<') ? '' : str
                 end

  {
    type: 'generic',
    class: element.class.name,
    content: text_content
  }
end

.serialize_list_item(item) ⇒ Hash

Serialize list item

Parameters:

  • item (Coradoc::CoreModel::ListItem)

    List item to serialize

Returns:

  • (Hash)

    Serialized list item



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 193

def serialize_list_item(item)
  content = if item.content
              case item.content
              when Array
                item.content.map { |el| serialize_core_model_element(el) }.compact
              else
                [{ type: 'text', content: item.content.to_s }]
              end
            else
              []
            end

  {
    id: generate_uid(item),
    type: 'list_item',
    content: content
  }
end

.serialize_table_cell(cell) ⇒ Hash

Serialize table cell

Parameters:

  • cell (Coradoc::CoreModel::TableCell)

    Cell to serialize

Returns:

  • (Hash)

    Serialized cell



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 245

def serialize_table_cell(cell)
  content = if cell.content
              case cell.content
              when Array
                cell.content.map { |el| serialize_core_model_element(el) }.compact
              else
                cell.content.to_s
              end
            else
              ''
            end

  {
    type: 'table_cell',
    content: content
  }
end

.serialize_table_row(row) ⇒ Hash

Serialize table row

Parameters:

  • row (Coradoc::CoreModel::TableRow)

    Row to serialize

Returns:

  • (Hash)

    Serialized row



231
232
233
234
235
236
237
238
239
# File 'lib/coradoc/html/theme/modern/serializers/document_serializer.rb', line 231

def serialize_table_row(row)
  {
    type: 'table_row',
    header: row.is_a?(Coradoc::CoreModel::TableRow) && row.header,
    cells: (row.cells || []).map do |cell|
      serialize_table_cell(cell)
    end
  }
end