Class: Slamdown::Parsing::ParsedNodeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/slamdown/parsing/parsed_node_adapter.rb

Overview

Projects Kramdown elements into immutable Slamdown nodes while removing only dependency-specific representation details from the parser boundary.

Constant Summary collapse

SEMANTIC_KINDS =
{
	:p => :paragraph,
	:header => :heading,
	:blockquote => :blockquote,
	:codeblock => :code_block,
	:ul => :unordered_list,
	:ol => :ordered_list,
	:li => :list_item,
	:dl => :definition_list,
	:dt => :definition_term,
	:dd => :definition_description,
	:hr => :horizontal_rule,
	:table => :table,
	:thead => :table_head,
	:tbody => :table_body,
	:tfoot => :table_foot,
	:tr => :table_row,
	:td => :table_cell,
	:a => :link,
	:br => :line_break,
	:img => :image,
	:codespan => :code_span,
	:em => :emphasis,
	:strong => :strong,
	:math => :math
}.freeze
NON_ELEMENT_KINDS =
{
	:root => :document,
	:text => :text,
	:raw => :raw,
	:xml_comment => :comment,
	:xml_pi => :processing_instruction,
	:entity => :entity,
	:typographic_sym => :typographic_symbol,
	:smart_quote => :smart_quote
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adapt(root) ⇒ Object



47
48
49
# File 'lib/slamdown/parsing/parsed_node_adapter.rb', line 47

def adapt(root)
	new.adapt_node(root)
end

Instance Method Details

#adapt_node(element, parent_type: nil, final_root_child: false) ⇒ Object

Recursively copies only fields that belong to Slamdown's parser contract.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/slamdown/parsing/parsed_node_adapter.rb', line 53

def adapt_node(element, parent_type: nil, final_root_child: false)
	source_name = explicit_source_name(element)
	children = element.children.each_with_index.map do |child, index|
		adapt_node(
			child,
			:parent_type => element.type,
			:final_root_child => element.type == :root && index == element.children.length - 1
		)
	end.freeze

	Node.new(
		:kind => NON_ELEMENT_KINDS.fetch(element.type, :element),
		:source_name => source_name,
		:semantic_kind => SEMANTIC_KINDS[element.type],
		:category => ::Kramdown::Element.category(element),
		:content_model => element.options[:content_model],
		:value => adapted_value(element, parent_type, final_root_child),
		:attributes => adapted_attributes(element),
		:children => children,
		:properties => adapted_properties(element),
		:source_form => adapted_source_form(element),
		:synthetic => synthetic_element?(element, source_name)
	)
end