Class: Jekyll::Spaceship::ElementProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jekyll-spaceship/processors/element-processor.rb

Constant Summary

Constants inherited from Processor

Processor::DEFAULT_PRIORITY, Processor::PRIORITY_MAP

Instance Attribute Summary

Attributes inherited from Processor

#config, #exclusions, #handled, #logger, #page, #priority, #registers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#after_exclude, #converter, #dispatch, escape_html, exclude, #ext, #filename, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, #name, #on_handle_html_block, #on_handled, #output_ext, #pre_exclude, priority, #process?, register

Constructor Details

This class inherits a constructor from Jekyll::Spaceship::Processor

Class Method Details

.configObject



9
10
11
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 9

def self.config
  { 'css' => [] }
end

Instance Method Details

#create_children(data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 106

def create_children(data)
  doc = data[:doc]
  data = data[:data]
  root = Nokogiri::HTML.fragment("")

  data = [data] unless data.kind_of? Array
  data.each do |child_data|
    node = self.create_element({
      :doc => doc,
      :data => child_data
    })
    next if node.nil?
    unless child_data['children'].nil?
      node.children = self.create_children({
        :doc => doc,
        :data => child_data['children']
      })
    end
    root.add_child node
  end
  root.children
end

#create_element(data) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 129

def create_element(data)
  doc = data[:doc]
  data = data[:data]

  return if data.nil?
  return Nokogiri::HTML.fragment(data) if data.kind_of? String
  return if data['name'].nil?

  # create node
  node = doc.create_element(data['name'])

  # set props
  data['props']&.each do |prop, val|
    if val.kind_of? Hash
      result = []
      val.each { |k, v| result.push "#{k}: #{v}" }
      val = result.join(";")
    end
    node.set_attribute(prop, val)
  end
  node
end

#handle_css_pattern(data) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 40

def handle_css_pattern(data)
  doc = data[:doc]
  element = data[:element]
  data = data[:data]

  if data.kind_of? String
    element.replace Nokogiri::HTML.fragment(data)
  elsif data.kind_of? Hash
    # set name
    element.name = data['name'] unless data['name'].nil?

    # set props
    data['props']&.each do |prop, val|
      next element.remove_attribute if val.nil?
      if val.kind_of? Array
        next if val.size != 2
        v = element[prop]
        v = '' if v.nil?
        val = v.sub(/#{val[0]}/, val[1])
      elsif val.kind_of? Hash
        result = []
        val.each { |k, v| result.push "#{k}: #{v}" }
        val = result.join(";")
      end
      element.set_attribute(prop, val)
    end

    # processing children
    return unless data.has_key?('children')
    return element.inner_html = "" if data['children'].nil?
    children = self.create_children({
      :doc => doc,
      :data => data['children']
    })

    # replace whole inner html
    unless data['children'].kind_of? Array
      return element.inner_html = children
    end

    if element.children.size.zero?
      return element.inner_html = children
    end

    index = data['children'].index(nil)
    if index.nil?
      return element.inner_html = children
    end

    # insert to the end of children
    if index == 0
      return element.children.last.after(children)
    end

    # insert to the begin of children
    rindex = data['children'].rindex { |item| !item.nil? }
    if index == rindex + 1
      return element.children.first.before children
    end

    # wrap the children
    element.children.first.before children[0..index]
    element.children.last.after children[index..children.size]
  end
end

#on_handle_html(content) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 13

def on_handle_html(content)
  return content if config['css'].size.zero?

  # use nokogiri to parse html content
  doc = Nokogiri::HTML(content)

  # handle each css pattern
  config['css'].each do |data|
    data.each do |key, val|
      key = [key] if key.kind_of? String
      key.each do |pattern|
        nodes = doc.css(pattern)
        nodes.each do |element|
          handle_css_pattern({
            :doc => doc,
            :element => element,
            :data => val
          })
        end
        self.handled = true
      end
    end
  end

  doc.to_html
end