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

class_name, #converter, #dispatch, escape_html, exclude, #exclusion_regexs, #ext, fetch_img_data, #filename, #get_exclusion, handle_bang_link, #initialize, #initialize_exclusions, #initialize_priority, #initialize_register, make_img_tag, #name, #next?, #on_handle_html_block, #on_handled, #output_ext, #post_exclude, #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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 56

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 118

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
# 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
    handle_hash_element({
      :doc => doc,
      :element => element,
      :data => data,
    })
  end
end

#handle_element_placement(data) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 141

def handle_element_placement(data)
  element = data[:element]
  children = data[:children]
  data = data[:data]

  # 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)
  rindex = data['children'].rindex { |item| !item.nil? }

  if index.nil?
    element.inner_html = children
  elsif index == 0 # insert to the end of children
    element.children.last.after(children)
  elsif index == rindex + 1 # insert to the begin of children
      element.children.first.before children
  else # wrap the children
    element.children.first.before children[0..index]
    element.children.last.after children[index..children.size]
  end
end

#handle_hash_element(data) ⇒ Object



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
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll-spaceship/processors/element-processor.rb', line 79

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

  # 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']
  })

  handle_element_placement({
    :data => data,
    :element => element,
    :children => children,
  })
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