Class: Kramdown::CustomDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/kramdown/custom_document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ CustomDocument

Returns a new instance of CustomDocument.



18
19
20
21
22
23
24
25
26
# File 'lib/kramdown/custom_document.rb', line 18

def initialize(source)
  @source = source
  @parsed_dom = Kramdown::Document.new(@source, {
    input: "GFM",
    parse_block_html: true,
    syntax_highlighter: "rouge"
  })
  @custom_elements = extract_custom_elements
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *attr, &block) ⇒ Object



55
56
57
# File 'lib/kramdown/custom_document.rb', line 55

def method_missing(id, *attr, &block)
  @parsed_dom.send(id, attr, &block)
end

Instance Attribute Details

#custom_elementsObject (readonly)

Returns the value of attribute custom_elements.



16
17
18
# File 'lib/kramdown/custom_document.rb', line 16

def custom_elements
  @custom_elements
end

Class Method Details

.define_element(name, element = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/kramdown/custom_document.rb', line 5

def self.define_element(name, element=nil)
  if element.nil?
    element = CustomElement
  end
  tag_name = name.downcase
  Kramdown::Parser::Html::Constants::HTML_ELEMENT[tag_name] = true
  Kramdown::Parser::Html::Constants::HTML_CONTENT_MODEL[tag_name] = :block
  @@custom_elements ||= {}
  @@custom_elements[tag_name] = element
end

Instance Method Details

#extract_custom_elementsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kramdown/custom_document.rb', line 66

def extract_custom_elements
  elements = []

  @parsed_dom.root.children.each do |outer_el|
    if outer_el.type == :html_element && @@custom_elements.keys.include?(outer_el.value)

      unless outer_el.attr.key?("id")
        outer_el.attr["id"] = generate_el_id(outer_el.value)
      end

      custom_element_cls = @@custom_elements[outer_el.value]
      element = custom_element_cls.new(outer_el.attr["id"])
      element.parse_dom(outer_el)

      # codeblocks = outer_el.children.filter { |child_el| child_el.type == :codeblock }
      #
      # outer_el.children = codeblocks.map do |codeblock|
      #   wrapper = Kramdown::Element.new(
      #     :html_element,
      #     "example-script",
      #     { label: LANG_LABELS[codeblock.options[:lang]] },
      #     { content_model: :block }
      #   )
      #   wrapper.children << codeblock
      #   wrapper
      # end
      #
      # outer_el.children.first.attr[:selected] = true

      elements << element
    end
  end

  elements
end

#generate_el_id(tagname) ⇒ Object



59
60
61
62
63
64
# File 'lib/kramdown/custom_document.rb', line 59

def generate_el_id(tagname)
  alphabet = [('a'..'z')].map(&:to_a).flatten
  tag_id = (0...12).map { alphabet[rand(alphabet.length)] }.join
  tag_abbr = tagname.split("-").map { |part| part[0] }.join("")
  "#{tag_abbr}-#{tag_id}"
end

#has_js?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/kramdown/custom_document.rb', line 40

def has_js?
  !@custom_elements.empty?
end

#rootObject



28
29
30
# File 'lib/kramdown/custom_document.rb', line 28

def root
  @parsed_dom.root
end

#to_htmlObject



36
37
38
# File 'lib/kramdown/custom_document.rb', line 36

def to_html
  @parsed_dom.to_html
end

#to_jsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/kramdown/custom_document.rb', line 44

def to_js
  bundle = ["const EXAMPLE_HANDLERS = {}"]

  @custom_elements.each do |element|
    bundle << element.to_js
    bundle << "EXAMPLE_HANDLERS[\"#{element.id}\"] = #{element.name}"
  end

  bundle.join("\n\n")
end

#warningsObject



32
33
34
# File 'lib/kramdown/custom_document.rb', line 32

def warnings
  @parsed_dom.warnings
end