Class: Coradoc::Markdown::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/markdown/serializer.rb

Overview

Serializer for Markdown Document models.

This serializer converts Document model objects back into Markdown text format.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serialize(document, options = {}) ⇒ String

Serialize a document model to Markdown string

Parameters:

  • document (Coradoc::Markdown::Base)

    The document or element to serialize

  • options (Hash) (defaults to: {})

    Serialization options

Returns:

  • (String)

    The Markdown output



16
17
18
# File 'lib/coradoc/markdown/serializer.rb', line 16

def self.serialize(document, options = {})
  new.serialize(document, options)
end

Instance Method Details

#serialize(element, _options = {}) ⇒ String

Serialize a document model to Markdown string

Parameters:

Returns:

  • (String)

    The Markdown output



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
# File 'lib/coradoc/markdown/serializer.rb', line 25

def serialize(element, _options = {})
  case element
  when Document
    serialize_document(element)
  when Heading
    serialize_heading(element)
  when Paragraph
    serialize_paragraph(element)
  when List
    serialize_list(element)
  when CodeBlock
    serialize_code_block(element)
  when Blockquote
    serialize_blockquote(element)
  when Link
    serialize_link(element)
  when Image
    serialize_image(element)
  when HorizontalRule
    serialize_horizontal_rule(element)
  when Table
    serialize_table(element)
  when Emphasis
    serialize_emphasis(element)
  when Strong
    serialize_strong(element)
  when Code
    serialize_code(element)
  when DefinitionList
    serialize_definition_list(element)
  when Footnote
    serialize_footnote(element)
  when FootnoteReference
    serialize_footnote_reference(element)
  when Abbreviation
    serialize_abbreviation(element)
  when Strikethrough
    element.to_md
  when Highlight
    element.to_md
  when AttributeList
    element.to_md
  when Math
    element.to_md
  when Extension
    element.to_md
  when String
    element
  else
    raise ArgumentError,
          "Unknown element type for serialization: #{element.class}. " \
          'Expected a known Markdown model type.'
  end
end