Sha256: 6220311d9646171983dbdf21c02977a65c40fffe5efd72b8f51a8818f2982116

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Jekyll
  class SmileyGenerator < Generator
    def generate(site)
      smiley_config = site.config["smiley"]
      return if not smiley_config or not smiley_config["enabled"]
      
      dir = smiley_config["dir"] || "smileys"

      smileys = {}

      begin
        Dir.foreach(dir) do |filename|
          if filename.chars.first != "." 
            name = File.basename(filename, File.extname(filename))
            smiley = {
              "name" => name,
              "img" => File.join("/", dir, filename),
              "slug" => ":#{name}:"
            }
            smileys[name] = smiley
          end
        end
      rescue Exception => e
        puts e
      end
      site.config["smileys"] = smileys
    end
  end

  module SmileyFilter
    def smiley(message)
      site = @context.registers[:site].config

      smileys = site["smileys"]
      return message if smileys.nil? or smileys.empty?

      message.gsub!(/:([a-z]+):/) do |match|
        smiley = smileys[$1]
        if smiley
          "![#{$1}](#{smiley["img"]})"
        end
      end
      message
    end
  end
end

Liquid::Template.register_filter(Jekyll::SmileyFilter)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-smiley-0.1.4 lib/jekyll-smiley.rb
jekyll-smiley-0.1.3 lib/jekyll-smiley.rb