Class: Jekyll::Spaceship::Manager

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

Constant Summary collapse

@@_hooks =
{}
@@_processors =
[]

Class Method Summary collapse

Class Method Details

.add(processor) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll-spaceship/cores/manager.rb', line 11

def self.add(processor)
  # register for listening event
  processor.registers.each do |_register|
    container = _register.first
    events = _register.last.uniq
    events = events.select do |event|
      next true if event.match(/^post/)
      next events.index(event.to_s.gsub(/^pre/, 'post').to_sym).nil?
    end
    events.each do |event|
      self.hook container, event
    end
  end
  @@_processors.push(processor)
  @@_processors = @@_processors.sort { |a, b| b.priority <=> a.priority }
end

.converter(page, name) ⇒ Object



80
81
82
83
84
85
# File 'lib/jekyll-spaceship/cores/manager.rb', line 80

def self.converter(page, name)
  page.site.converters.each do |converter|
    class_name = converter.class.to_s.downcase
    return converter if class_name.end_with?(name.downcase)
  end
end

.dispatch(page, container, event) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll-spaceship/cores/manager.rb', line 59

def self.dispatch(page, container, event)
  @@_processors.each do |processor|
    processor.dispatch page, container, event
  end
  if event.to_s.start_with?('post') and Type.html? output_ext(page)
    self.dispatch_html_block(page)
  end
  @@_processors.each do |processor|
    processor.on_handled if processor.handled
  end
end

.dispatch_html_block(page) ⇒ Object



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
117
# File 'lib/jekyll-spaceship/cores/manager.rb', line 87

def self.dispatch_html_block(page)
  doc = Nokogiri::HTML(page.output)
  doc.css('script').each do |node|
    type = Type.html_block_type node['type']
    content = node.content
    next if type.nil?

    # dispatch to on_handle_html_block
    @@_processors.each do |processor|
      next unless processor.process?
      content = processor.on_handle_html_block content, type
      # dispatch to type handlers
      method = "on_handle_#{type}"
      next unless processor.respond_to? method
      content = processor.pre_exclude content
      content = processor.send method, content
      content = processor.after_exclude content
    end

    cvter = self.converter page, type
    content = cvter.convert content unless cvter.nil?

    # dispatch to on_handle_html
    @@_processors.each do |processor|
      next unless processor.process?
      content = processor.on_handle_html content
    end
    node.replace Nokogiri::HTML.fragment content
  end
  page.output = Processor.escape_html doc.to_html
end

.ext(page) ⇒ Object



71
72
73
74
# File 'lib/jekyll-spaceship/cores/manager.rb', line 71

def self.ext(page)
  ext = page.path.match(/\.[^.]+$/)
  ext.to_s.rstrip
end

.hook(container, event, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll-spaceship/cores/manager.rb', line 28

def self.hook(container, event, &block)
  return if not is_hooked? container, event

  handler = ->(page) {
    self.dispatch page, container, event
    block.call if block
  }

  if event.to_s.start_with?('after')
    Jekyll::Hooks.register container, event do |page|
      handler.call page
    end
  elsif event.to_s.start_with?('post')
    Jekyll::Hooks.register container, event do |page|
      handler.call page
    end
    # auto add pre-event
    self.hook container, event.to_s.sub('post', 'pre').to_sym
  elsif event.to_s.start_with?('pre')
    Jekyll::Hooks.register container, event do |page|
      handler.call page
    end
  end
end

.is_hooked?(container, event) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/jekyll-spaceship/cores/manager.rb', line 53

def self.is_hooked?(container, event)
  hook_name = "#{container}_#{event}".to_sym
  return false if @@_hooks.has_key? hook_name
  @@_hooks[hook_name] = true
end

.output_ext(page) ⇒ Object



76
77
78
# File 'lib/jekyll-spaceship/cores/manager.rb', line 76

def self.output_ext(page)
  page.url_placeholders[:output_ext]
end