Class: Jekyll::Spaceship::Processor

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

Constant Summary collapse

@@_registers =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jekyll-spaceship/cores/processor.rb', line 7

def initialize()
  @@_registers.each do |_register|
    container = _register.first
    events = _register.last.uniq
    events = events.select do |event|
      next true if event.match?(/^post/)
      next !events.any?(event.to_s.gsub(/^pre/, 'post').to_sym)
    end
    events.each do |event|
      register container, event
    end
  end
  @@_registers.clear
end

Class Method Details

.register(container, *events) ⇒ Object



27
28
29
# File 'lib/jekyll-spaceship/cores/processor.rb', line 27

def self.register(container, *events)
  @@_registers << [container, events]
end

Instance Method Details

#content_tag(post) ⇒ Object



22
23
24
25
# File 'lib/jekyll-spaceship/cores/processor.rb', line 22

def (post)
  # pre-handle content
   = "<content_tag id=\"#{post.url}\" />"
end

#register(container, event, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
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
# File 'lib/jekyll-spaceship/cores/processor.rb', line 31

def register(container, event, &block)
  # define handle proc
  handle = ->(post) {
    method = "on_#{container}_#{event}"
    self.send method, post if self.respond_to? method
    block.call(post) if block
  }

  if event.to_s.start_with?("after")
    Jekyll::Hooks.register container, event do |post|
      handle.call post
    end
  elsif event.to_s.start_with?("post")
    Jekyll::Hooks.register container, event do |post|
      # remove content tags
      tag = self.(post)
      post.content = post.content.gsub(tag, "")

      handle.call post

      # replace output content
      post.output = post.output.gsub(/#{tag}.*#{tag}/m, post.content)
    end

    # auto add pre-event
    register container, event.to_s.sub("post", "pre").to_sym
  elsif event.to_s.start_with?("pre")
    Jekyll::Hooks.register container, event do |post|
      # wrap post content with tags
      tag = self.(post)

      handle.call post

      post.content = "#{tag}\n#{post.content}\n#{tag}"
    end
  end
end