Class: MDOM::Serializer

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

Overview

Serializes an MDOM tree back to a Markdown string. Traversal is driven by node type; the context (indentation level, in-blockquote flag, list marker) flows downward so nested lists and blockquotes render correctly.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serialize(node, indent: 0, blockquote: false, marker: "-") ⇒ Object

Serialize node to Markdown. Returns a String.



9
10
11
# File 'lib/mdom/serializer.rb', line 9

def self.serialize(node, indent: 0, blockquote: false, marker: "-")
  new.serialize(node, indent: indent, blockquote: blockquote, marker: marker)
end

Instance Method Details

#serialize(node, indent: 0, blockquote: false, marker: "-") ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mdom/serializer.rb', line 13

def serialize(node, indent: 0, blockquote: false, marker: "-")
  case node.type
  when Types::DOCUMENT then serialize_document(node)
  when Types::HEADING then serialize_heading(node, indent: indent, blockquote: blockquote)
  when Types::PARAGRAPH then serialize_paragraph(node, indent: indent, blockquote: blockquote)
  when Types::LIST then serialize_list(node, indent: indent, blockquote: blockquote)
  when Types::LIST_ITEM then serialize_list_item(node, indent: indent, blockquote: blockquote, marker: marker)
  when Types::BLOCKQUOTE then serialize_blockquote(node, indent: indent)
  when Types::CODE_BLOCK then serialize_code_block(node, indent: indent, blockquote: blockquote)
  when Types::HRULE then serialize_hrule(indent: indent, blockquote: blockquote)
  else serialize_inline(node)
  end
end