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.



43
44
45
46
47
48
49
50
# File 'lib/jekyll-spaceship/cores/processor.rb', line 43

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

.class_nameObject



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

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

.configObject



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

def self.config
end

.escape_html(content) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/jekyll-spaceship/cores/processor.rb', line 217

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



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

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

.fetch_img_data(url) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/jekyll-spaceship/cores/processor.rb', line 227

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 = Logger.new(self.class_name)
    logger.log msg
  end
end


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/jekyll-spaceship/cores/processor.rb', line 261

def self.handle_bang_link(
  content,
  url = '(https?:)?\\/\\/.*',
  title = '("(.*)".*){0,1}',
  &block
)
  # pre-handle reference-style links
  regex = /(\[(.*)\]:\s*(#{url}\s*#{title}))/
  content.scan regex do |match_data|
    match = match_data[0]
    ref_name = match_data[1]
    ref_value = match_data[2]
    content = content.gsub(match, '')
      .gsub(/\!\[(.*)\]\s*\[#{ref_name}\]/,
        "![\1](#{ref_value})")
  end

  # handle inline-style links
  regex = /(\!\[(.*)\]\(.*#{url}\s*#{title}\))/
  content.scan regex do |match_data|
    url = match_data[2]
    title = match_data[6]
    block.call(url, title)
  end
end

.make_img_tag(data) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/jekyll-spaceship/cores/processor.rb', line 244

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



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

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

.register(container, *events) ⇒ Object



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

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

Instance Method Details

#converter(name) ⇒ Object



109
110
111
# File 'lib/jekyll-spaceship/cores/processor.rb', line 109

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

#dispatch(page, container, event) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jekyll-spaceship/cores/processor.rb', line 113

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/jekyll-spaceship/cores/processor.rb', line 162

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



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

def ext
  Manager.ext @page
end

#filenameObject



35
36
37
38
39
40
41
# File 'lib/jekyll-spaceship/cores/processor.rb', line 35

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

#get_exclusion(id) ⇒ Object



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

def get_exclusion(id)
  result = nil
  match_data = id.match /<!JEKYLL@(.+)@(.+)>/
  unless match_data.nil?
    id = match_data[2].to_i
    result = {
      :id => id,
      :marker => match_data[0],
      :content => @exclusion_store[id]
    }
  end
  result
end

#initialize_exclusionsObject



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

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

#initialize_priorityObject



52
53
54
55
56
57
58
59
# File 'lib/jekyll-spaceship/cores/processor.rb', line 52

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



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

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.class_name
end

#next?Boolean

Returns:

  • (Boolean)


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

def next?
  true
end

#on_handle_html(content) ⇒ Object



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

def on_handle_html(content)
  # default handle method
  content
end

#on_handle_html_block(content, type) ⇒ Object



144
145
146
147
# File 'lib/jekyll-spaceship/cores/processor.rb', line 144

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

#on_handledObject



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

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



105
106
107
# File 'lib/jekyll-spaceship/cores/processor.rb', line 105

def output_ext
  Manager.output_ext @page
end

#post_exclude(content) ⇒ Object



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

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



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/jekyll-spaceship/cores/processor.rb', line 178

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)


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

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