Sha256: 074fdc4290788921b730c9ec40e3b23d4b11e0624215dd31f93638f887296e7c

Contents?: true

Size: 1.75 KB

Versions: 92

Compression:

Stored size: 1.75 KB

Contents

require "octicons_v2"
require "jekyll-octicons/version"
require "liquid"
require "jekyll/liquid_extensions"

module Jekyll
  class Octicons < Liquid::Tag
    include Jekyll::LiquidExtensions

    # Syntax for the octicon symbol
    Syntax = /\A(#{Liquid::VariableSignature}+)/

    # For interpoaltion, look for liquid variables
    Variable = /\{\{\s*([\w]+\.?[\w]*)\s*\}\}/i

    # Copied from Liquid::TagAttributes to allow dashes in tag names:
    #
    #   {% octicon alert area-label:"Hello World!" %}
    #
    TagAttributes = /([\w-]+)\s*\:\s*(#{Liquid::QuotedFragment})/o

    def initialize(tag_name, markup, options)
      super
      @markup = markup

      # If there's interpoaltion going on, we need to do this in render
      prepare(markup) unless match = markup.match(Variable)
    end

    def render(context)
      prepare(interpolate(@markup, context)) if match = @markup.match(Variable)

      return nil if @symbol.nil?
      ::OcticonsV2::OcticonV2.new(@symbol, @options).to_svg
    end

    private

    def interpolate(markup, context)
      markup.scan Variable do |variable|
        markup = markup.gsub(Variable, lookup_variable(context, variable.first))
      end
      markup
    end

    def prepare(markup)
      @symbol = symbol(markup)
      @options = string_to_hash(markup)
    end

    def symbol(markup)
      if match = markup.match(Syntax)
        match[1]
      end
    end

    # Create a ruby hash from a string passed by the jekyll tag
    def string_to_hash(markup)
      options = {}

      if match = markup.match(Syntax)
        markup.scan(TagAttributes) do |key, value|
          options[key.to_sym] = value.gsub(/\A"|"\z/, "")
        end
      end

      options
    end
  end
end

Liquid::Template.register_tag("octicon", Jekyll::Octicons)

Version data entries

92 entries across 92 versions & 1 rubygems

Version Path
jekyll-octicons_v2-0.0.0.pre.dcc6a36 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.387b0bb lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.ef56016 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.0ea8199 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.fb17b1e lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.0823bfd lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.0a10848 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.77f2e00 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.01b17b3 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.519e9d5 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.1cb16f5 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.53dc419 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.1a093dc lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.1623cf0 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.1527811 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.a58f4df lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.26e8c87 lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.e3d382c lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.a58c58c lib/jekyll-octicons.rb
jekyll-octicons_v2-0.0.0.pre.f6a1df0 lib/jekyll-octicons.rb