Class: Jekyll::Spaceship::Manager
- Inherits:
-
Object
- Object
- Jekyll::Spaceship::Manager
- Defined in:
- lib/jekyll-spaceship/cores/manager.rb
Constant Summary collapse
- @@_hooks =
{}
- @@_processors =
[]
Class Method Summary collapse
- .add(processor) ⇒ Object
- .converter(page, name) ⇒ Object
- .dispatch(page, container, event) ⇒ Object
- .dispatch_html_block(page) ⇒ Object
- .ext(page) ⇒ Object
- .hook(container, event, &block) ⇒ Object
- .is_hooked?(container, event) ⇒ Boolean
- .output_ext(page) ⇒ Object
Class Method Details
.add(processor) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 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(/^after/) 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
93 94 95 96 97 98 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 93 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 60 def self.dispatch(page, container, event) # dispatch to each processor @@_processors.each do |processor| processor.dispatch page, container, event break unless processor.next? end if event.to_s.start_with?('post') and Type.html? output_ext(page) self.dispatch_html_block(page) end # update page excerpt @@_processors.each do |processor| next unless processor.handled next unless page.is_a? Jekyll::Document break page.data['excerpt'] = Jekyll::Excerpt.new(page) end # call on_handled @@_processors.each do |processor| processor.on_handled if processor.handled break unless processor.next? end end |
.dispatch_html_block(page) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 100 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
82 83 84 85 86 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 82 def self.ext(page) return unless page.respond_to? :path ext = page.path.match(/\.[^.]+$/) ext.to_s.rstrip end |
.hook(container, event, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 29 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
54 55 56 57 58 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 54 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
88 89 90 91 |
# File 'lib/jekyll-spaceship/cores/manager.rb', line 88 def self.output_ext(page) return unless page.respond_to? :url_placeholders page.url_placeholders[:output_ext] end |