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.



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

def initialize()
  self.initialize_priority
  self.initialize_register
  self.initialize_exclusions
  @logger = Logger.new(self.name)
  @config = Config.store(self.filename, self.class.config)
  @handled_files = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#exclusionsObject (readonly)

Returns the value of attribute exclusions.



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

def exclusions
  @exclusions
end

#handledObject

Returns the value of attribute handled.



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

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.



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

def priority
  @priority
end

#registersObject (readonly)

Returns the value of attribute registers.



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

def registers
  @registers
end

Class Method Details

.configObject



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

def self.config
end

.escape_html(content) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/jekyll-spaceship/cores/processor.rb', line 193

def self.escape_html(content)
  # escape link
  content.scan(/((https?:)?\/\/\S+\?[a-zA-Z0-9%\-_=\.&;]+)/) do |result|
    result = result[0]
    link = result.gsub('&', '&')
    content = content.gsub(result, link)
  end
  content
end

.exclude(*types) ⇒ Object



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

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

.fetch_img_data(url) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jekyll-spaceship/cores/processor.rb', line 203

def self.fetch_img_data(url)
  begin
    res = Net::HTTP.get_response URI(url)
    raise res.body unless res.is_a?(Net::HTTPSuccess)
    content_type = res.header['Content-Type']
    raise 'Unknown content type!' if content_type.nil?
    content_body = res.body.force_encoding('UTF-8')
    return {
      'type' => content_type,
      'body' => content_body
    }
  rescue StandardError => msg
    logger.log msg
  end
end

.make_img_tag(data) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/jekyll-spaceship/cores/processor.rb', line 219

def self.make_img_tag(data)
  css_class = data['class']
  type = data['type']
  body = data['body']
  if type == 'url'
    "<img class=\"#{css_class}\" src=\"#{body}\">"
  elsif type.include?('svg')
    body.gsub(/\<\?xml.*?\?>/, '')
      .gsub(/<!--[^\0]*?-->/, '')
      .sub(/<svg /, "<svg class=\"#{css_class}\" ")
  else
    body = Base64.encode64(body)
    body = "data:#{type};base64, #{body}"
    "<img class=\"#{css_class}\" src=\"#{body}\">"
  end
end

.priority(value) ⇒ Object



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

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

.register(container, *events) ⇒ Object



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

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

Instance Method Details

#converter(name) ⇒ Object



101
102
103
# File 'lib/jekyll-spaceship/cores/processor.rb', line 101

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

#dispatch(page, container, event) ⇒ Object



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
131
132
133
134
# File 'lib/jekyll-spaceship/cores/processor.rb', line 105

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.post_exclude @page.content
    end
  else
    if Type.html? output_ext
      method = 'on_handle_html'
    elsif Type.css? output_ext
      method = 'on_handle_css'
    end
    if self.respond_to? method
      @page.output = self.send method, @page.output
      if Type.html? output_ext
        @page.output = self.class.escape_html(@page.output)
      end
    end
  end
end

#exclusion_regexsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jekyll-spaceship/cores/processor.rb', line 154

def exclusion_regexs()
  regexs = []
  @exclusions.each do |type|
    regex = nil
    if type == :code
      regex = /(((?<!\\)`+)\s*(\w*)((?:.|\n)*?)\2)/
    elsif type == :math
      regex = /(((?<!\\)\${1,2})[^\n]*?\1)/
    elsif type == :liquid_filter
      regex = /((?<!\\)((\{\{[^\n]*?\}\})|(\{%[^\n]*?%\})))/
    end
    regexs.push regex unless regex.nil?
  end
  regexs
end

#extObject



93
94
95
# File 'lib/jekyll-spaceship/cores/processor.rb', line 93

def ext
  Manager.ext @page
end

#filenameObject



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

def filename
  self.name
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1-\2')
    .gsub(/([a-z\d])([A-Z])/,'\1-\2')
    .tr("_", "-")
    .downcase
end

#initialize_exclusionsObject



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

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

#initialize_priorityObject



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

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



57
58
59
60
61
62
63
64
# File 'lib/jekyll-spaceship/cores/processor.rb', line 57

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

#nameObject



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

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

#on_handle_html(content) ⇒ Object



141
142
143
144
# File 'lib/jekyll-spaceship/cores/processor.rb', line 141

def on_handle_html(content)
  # default handle method
  content
end

#on_handle_html_block(content, type) ⇒ Object



136
137
138
139
# File 'lib/jekyll-spaceship/cores/processor.rb', line 136

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

#on_handledObject



146
147
148
149
150
151
152
# File 'lib/jekyll-spaceship/cores/processor.rb', line 146

def on_handled
  source = page.site.source
  file = page.path.sub(/^#{source}\//, '')
  return if @handled_files.has_key? file
  @handled_files[file] = true
  logger.log file
end

#output_extObject



97
98
99
# File 'lib/jekyll-spaceship/cores/processor.rb', line 97

def output_ext
  Manager.output_ext @page
end

#post_exclude(content) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/jekyll-spaceship/cores/processor.rb', line 183

def post_exclude(content)
  while @exclusion_store.size > 0
    match = @exclusion_store.pop
    id = @exclusion_store.size
    content = content.sub("<!JEKYLL@#{object_id}@#{id}>", match)
  end
  @exclusion_store = []
  content
end

#pre_exclude(content, regexs = self.exclusion_regexs()) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/jekyll-spaceship/cores/processor.rb', line 170

def pre_exclude(content, regexs = self.exclusion_regexs())
  @exclusion_store = []
  regexs.each do |regex|
    content.scan(regex) do |match_data|
      match = match_data[0]
      id = @exclusion_store.size
      content = content.sub(match, "<!JEKYLL@#{object_id}@#{id}>")
      @exclusion_store.push match
    end
  end
  content
end

#process?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/jekyll-spaceship/cores/processor.rb', line 89

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