Class: Jekyll::Spaceship::Processor

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

Constant Summary collapse

DEFAULT_PRIORITY =
20
PRIORITY_MAP =
{
  :lowest  => 0,
  :low     => 10,
  :normal  => 20,
  :high    => 30,
  :highest => 40,
}.freeze
@@_registers =
[]
@@_exclusions =
[]
@@_priority =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



30
31
32
33
34
35
# File 'lib/jekyll-spaceship/cores/processor.rb', line 30

def initialize()
  self.initialize_priority
  self.initialize_register
  self.initialize_exclusions
  @logger = Logger.new(self.name)
end

Instance Attribute Details

#exclusionsObject (readonly)

Returns the value of attribute exclusions.



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

def exclusions
  @exclusions
end

#handledObject

Returns the value of attribute handled.



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

def handled
  @handled
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/jekyll-spaceship/cores/processor.rb', line 20

def logger
  @logger
end

#pageObject (readonly)

Returns the value of attribute page.



19
20
21
# File 'lib/jekyll-spaceship/cores/processor.rb', line 19

def page
  @page
end

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
end

#registersObject (readonly)

Returns the value of attribute registers.



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

def registers
  @registers
end

Class Method Details

.exclude(*types) ⇒ Object



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

def self.exclude(*types)
  @@_exclusions = types
end

.priority(value) ⇒ Object



63
64
65
# File 'lib/jekyll-spaceship/cores/processor.rb', line 63

def self.priority(value)
  @@_priority = value.to_sym
end

.register(container, *events) ⇒ Object



67
68
69
# File 'lib/jekyll-spaceship/cores/processor.rb', line 67

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

Instance Method Details

#after_exclude(content) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/jekyll-spaceship/cores/processor.rb', line 152

def after_exclude(content)
  while @exclusion_store.size > 0
    match = @exclusion_store.pop
    id = @exclusion_store.size
    content = content.gsub("[//]: JEKYLL_EXCLUDE_##{id}", match)
  end
  @exclusion_store = []
  content
end

#converter(name) ⇒ Object



87
88
89
# File 'lib/jekyll-spaceship/cores/processor.rb', line 87

def converter(name)
  Manager.converter @page, name
end

#dispatch(page, container, event) ⇒ Object



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/processor.rb', line 91

def dispatch(page, container, event)
  @page = page
  @handled = false
  return unless self.process?
  method = "on_#{container}_#{event}"
  self.send method, @page if self.respond_to? method
  method = ''
  if event.to_s.start_with?('pre')
    if Type.markdown? ext
      method = 'on_handle_markdown'
    end
    if self.respond_to? method
      @page.content = self.pre_exclude @page.content
      @page.content = self.send method, @page.content
      @page.content = self.after_exclude @page.content
    end
  else
    if Type.html? output_ext
      method = 'on_handle_html'
    elsif css? output_ext
      method = 'on_handle_css'
    end
    if self.respond_to? method
      @page.output = self.send method, @page.output
    end
  end
end

#extObject



79
80
81
# File 'lib/jekyll-spaceship/cores/processor.rb', line 79

def ext
  Manager.ext @page
end

#initialize_exclusionsObject



55
56
57
58
59
60
61
# File 'lib/jekyll-spaceship/cores/processor.rb', line 55

def initialize_exclusions
  if @@_exclusions.size.zero?
    self.class.exclude :code, :block_quotes
  end
  @exclusions = @@_exclusions.uniq
  @@_exclusions.clear
end

#initialize_priorityObject



37
38
39
40
41
42
43
44
# File 'lib/jekyll-spaceship/cores/processor.rb', line 37

def initialize_priority
  @priority = @@_priority
  unless @priority.nil? or @priority.is_a? Numeric
    @priority = PRIORITY_MAP[@priority.to_sym]
  end
  @priority = DEFAULT_PRIORITY if @priority.nil?
  @@_priority = nil
end

#initialize_registerObject



46
47
48
49
50
51
52
53
# File 'lib/jekyll-spaceship/cores/processor.rb', line 46

def initialize_register
  if @@_registers.size.zero?
    self.class.register :pages, :pre_render, :post_render
    self.class.register :posts, :pre_render, :post_render
  end
  @registers = Array.new @@_registers
  @@_registers.clear
end

#nameObject



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

def name
  self.class.name.split('::').last
end

#on_handle_html(content) ⇒ Object



124
125
126
127
# File 'lib/jekyll-spaceship/cores/processor.rb', line 124

def on_handle_html(content)
  # default handle method
  content
end

#on_handle_html_block(content, type) ⇒ Object



119
120
121
122
# File 'lib/jekyll-spaceship/cores/processor.rb', line 119

def on_handle_html_block(content, type)
  # default handle method
  content
end

#on_handledObject



129
130
131
132
# File 'lib/jekyll-spaceship/cores/processor.rb', line 129

def on_handled
  file = page.path.gsub(/.*_posts\//, '')
  logger.log file
end

#output_extObject



83
84
85
# File 'lib/jekyll-spaceship/cores/processor.rb', line 83

def output_ext
  Manager.output_ext @page
end

#pre_exclude(content) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jekyll-spaceship/cores/processor.rb', line 134

def pre_exclude(content)
  @exclusion_store = []
  @exclusions.each do |type|
    regex = nil
    if type == :code
      regex = /(`{3}\s*(\w*)((?:.|\n)*?)`{3})/
    end
    next if regex.nil?
    content.scan(regex) do |match_data|
      match = match_data[0]
      id = @exclusion_store.size
      content = content.gsub(match, "[//]: JEKYLL_EXCLUDE_##{id}")
      @exclusion_store.push match
    end
  end
  content
end

#process?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/jekyll-spaceship/cores/processor.rb', line 75

def process?
  Type.html?(output_ext) or Type.markdown?(ext)
end