Sha256: 0237c23dd3aadc8712b92ba34d33bc71575c1dcb40cb3c9dbf944bc80ccc9d80

Contents?: true

Size: 1.75 KB

Versions: 803

Compression:

Stored size: 1.75 KB

Contents

require "octicons"
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?
      ::Octicons::Octicon.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

803 entries across 803 versions & 2 rubygems

Version Path
jekyll-octicons-0.0.0.pre.58d4279 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.6e1d684 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.4ed1570 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.7c18aeb lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.fe1b03f lib/jekyll-octicons.rb
jekyll-octicons-15.0.0 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.1b074ad lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.77e02f9 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.8f3b54e lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.42407ca lib/jekyll-octicons.rb
jekyll-octicons-14.2.2 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.e102a2f lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.5d5e869 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.5055845 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.81b2c05 lib/jekyll-octicons.rb
jekyll-octicons-14.2.1 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.4dce537 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.a0bcda1 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.cd8c4c4 lib/jekyll-octicons.rb
jekyll-octicons-0.0.0.pre.443d06d lib/jekyll-octicons.rb