Class: Slamdown::Parsing::Html::SourceAwareElementConverter

Inherits:
Kramdown::Parser::Html::ElementConverter
  • Object
show all
Defined in:
lib/slamdown/parsing/html.rb

Overview

Extends only converter instances selected by Slamdown. Ordinary Kramdown parsing continues to instantiate its original converter class.

Constant Summary collapse

CODE_SOURCE_NAMES =
%w[code kbd pre samp tt var].freeze

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ SourceAwareElementConverter

Returns a new instance of SourceAwareElementConverter.



15
16
17
18
19
20
# File 'lib/slamdown/parsing/html.rb', line 15

def initialize(root)
	@raw_elements = {}
	@raw_list_children = {}
	capture_raw_element(root)
	super
end

Instance Method Details

#process(element, do_conversion = true, preserve_text = false, parent = nil) ⇒ Object

Captures the source name and contains a failure of Kramdown's optional native conversion to the affected element.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slamdown/parsing/html.rb', line 23

def process(element, do_conversion = true, preserve_text = false, parent = nil)
	source_name = element.value if element.type == :html_element
	raw_element = @raw_elements[element.object_id] if source_name
	result = begin
		super
	rescue StandardError
		raise unless source_name && do_conversion && @raw_elements.key?(element.object_id)

		restore_raw_element(element)
		process_html_element(element, false, preserve_text)
	end
	element.options[:source_name] = source_name if source_name
	restore_list_text(element) if ["ol", "ul"].include?(source_name)
	attach_code_source(element, raw_element) if CODE_SOURCE_NAMES.include?(source_name)
	result
end