class Jekyll::Aplayer::Processor

Constants

DEFAULT_PRIORITY
PRIORITY_MAP

Attributes

config[R]
exclusions[R]
handled[RW]
logger[R]
page[R]
priority[R]
registers[R]

Public Class Methods

class_name() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 38
def self.class_name
  self.name.split('::').last
end
config() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 50
def self.config
  {}
end
escape_html(content) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 295
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) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 118
def self.exclude(*types)
  @@_exclusions = types
end
new() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 66
def initialize()
  self.initialize_priority
  self.initialize_register
  self.initialize_exclusions
  @logger = Logger.new(self.name)
  @@_site_config = {}.deep_merge!(Config.site_config()).deep_merge!(self.config)
  @handled_files = {}
end
priority(value) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 87
def self.priority(value)
  @@_priority = value.to_sym
end
register(container, *events) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 103
def self.register(container, *events)
  @@_registers << [container, events]
end

Public Instance Methods

converter(name) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 161
def converter(name)
  Manager.converter @page, name
end
dispatch(page, container, event) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 165
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_regexs() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 122
def exclusion_regexs()
  regexs = []
  @exclusions.each do |type|
    regex = nil
    if type == :code
      regex = /(((?<!\\)`{4,})\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
ext() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 58
def ext
  Manager.ext @page
end
filename() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 42
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_exclusions() click to toggle source

Exclusions

# File lib/jekyll-aplayer/cores/processor.rb, line 110
def initialize_exclusions
  if @@_exclusions.size.zero?
    self.class.exclude :code, :math, :liquid_filter
  end
  @exclusions = @@_exclusions.uniq
  @@_exclusions.clear
end
initialize_priority() click to toggle source

Priority

# File lib/jekyll-aplayer/cores/processor.rb, line 78
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_register() click to toggle source

Register

# File lib/jekyll-aplayer/cores/processor.rb, line 94
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
name() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 34
def name
  self.class.class_name
end
on_handle_html(content) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 244
def on_handle_html(content)
  # Do not handle html if no aplayer placeholder.
  return content unless Generator.has_aplayer_placeholder?(content, :html_aplayer)

  # use nokogiri to parse html.
  doc = Nokogiri::HTML(content)

  # Prepare doms.
  head = doc.at('head')
  body = doc.at('body')
  return content if head.nil? or body.nil?

  # Inject assets into head.
  @@_site_config['assets'].each do |type, value|
    value.each do |asset|
      Generator.asset_inject(:"#{type}", asset, head)
    end
  end

  # Parse aplayer doc, and inset aplayer instances into body.
  doc.css('aplayer').each do |elem|
    elem['id'] = SecureRandom.uuid if !elem.key?('id') or elem['id'].empty?

    config = {}.deep_merge!(
      Config.get(elem['id']) ? Config.get(elem['id']) : @@_site_config
    ).deep_merge!(
      elem.keys.map { |key| [key, elem[key]] }.to_h
    )

    # Store each aplayers' config.
    Config.store(elem['id'], config)

    # Generate aplayer instance.
    body.add_child(
      Generator.generate_aplayer_instance(elem['id'], Config.normalize?(config))
    ) if !body.to_html.include?(Generator.machine_id(elem['id']))
  end

  self.handled = true

  doc.to_html
end
on_handle_html_block(content, type) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 239
def on_handle_html_block(content, type)
  # default handle method
  content
end
on_handle_markdown(content) click to toggle source

Handle content.

# File lib/jekyll-aplayer/cores/processor.rb, line 200
def on_handle_markdown(content)
  # Do not handle markdown if no aplayer placeholder.
  return content unless Generator.has_aplayer_placeholder?(content, :code_aplayer)

  # pre-handle aplayer code block in markdown.
  content.scan(Generator.get_aplayer_regex(:code_aplayer)) do |match_data|
    # Generate customize config.
    config = {}
      .deep_merge!(@@_site_config)
      .deep_merge!(
        HANSON.parse(match_data[3]).map {
          |data|

          # Convert string to real datatype.
          method = 'to_' + Type.datatype?(data[0])
          data[1] = data[1].send(method) if data[1].respond_to?(method)

          data
        }.to_h
      )

    # Skip if the processor not match.
    next unless config['processor'].strip == self.name.downcase

    # Replace aplayer placeholder.
    uuid = config.key?('id') ? config['id'] : SecureRandom.uuid;

    content = content.gsub(match_data[0], Nokogiri::HTML::Document.new.create_element('aplayer', '', {
      'id' => uuid,
      'class' => config['class'],
    }).to_html)

    # Store the each aplayers' config.
    Config.store(uuid, config)
  end

  content
end
on_handled() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 287
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_ext() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 62
def output_ext
  Manager.output_ext @page
end
post_exclude(content) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 151
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()) click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 138
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?() click to toggle source
# File lib/jekyll-aplayer/cores/processor.rb, line 54
def process?
  return true if Type.html?(output_ext) or Type.markdown?(output_ext)
end