class Snippr::Processor::Functions

Public Instance Methods

process(content, opts = {}) click to toggle source
# File lib/snippr/processor/functions.rb, line 10
def process(content, opts = {})
  content.scan(/\{(.*?):(.*?)\}/) do |match|
    command, func_options = match
    options = opts.merge(hashify(func_options))
    command = "cmd_#{command}"
    content = send(command, content, options, func_options) if respond_to?(command, true)
  end
  content
end

Private Instance Methods

cmd_snip(unprocessed_content, opts, original_options) click to toggle source

expand another snip {snip:path/to/snippet}

# File lib/snippr/processor/functions.rb, line 24
def cmd_snip(unprocessed_content, opts, original_options)
  path = opts[:default].split("/")
  path = recursive_include_from_path(path, opts[:_parent]) if path.first.in? [".", ".."]
  snip_content = Snippr::Snip.new(*path + [opts]).content
  unprocessed_content.gsub("{snip:#{original_options}}", snip_content)
end
hashify(func_options="") click to toggle source

home home,var=1 home,var1=“1” home,var1=“1,2,3”

# File lib/snippr/processor/functions.rb, line 35
def hashify(func_options="")
  options = {}

  # replace comma temporarily
  func_options.scan(/(["'])?(.*?)(\1){1}/).each do |delimiter, value, _|
    func_options.gsub!("#{delimiter}#{value}#{delimiter}", "#{delimiter}#{value.gsub(",", "@@comma@@")}#{delimiter}")
  end

  # Split on comma which is secure now
  func_options.split(",").each do |option|
    opt_key, opt_value = option.split("=")
    unless opt_value
      opt_value = opt_key
      opt_key = :default
    end
    options[opt_key.to_sym] = opt_value.match(/^["']?(.*?)["']?$/)[1].gsub("@@comma@@", ",")
  end
  options
end
recursive_include_from_path(path, parent) click to toggle source
# File lib/snippr/processor/functions.rb, line 55
def recursive_include_from_path(path, parent)
  target_pathname = Pathname.new(parent.pathname + path.join(File::SEPARATOR))
  if target_pathname.to_s =~ /^#{Snippr.path}/
    target_pathname.to_s.gsub(/^#{Snippr.path}\//, "").split(File::SEPARATOR)
  else
    path
  end
end