Sha256: b96c03fa8ebd98ed03a719d8ec83123be5b0530bd6a278cbc97c659368d68dce

Contents?: true

Size: 1.78 KB

Versions: 47

Compression:

Stored size: 1.78 KB

Contents

# rubocop:disable Naming/FileName
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

47 entries across 47 versions & 1 rubygems

Version Path
jekyll-octicons-4.3.0.pre.8fd2622e lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.6c42d500 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.955417e7 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.75bbf905 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.e8c3035f lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.30f25f97 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.a295d642 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.5704ee36 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.ab8f6d66 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.982097c9 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.b41121ca lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.8f120ac7 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.9d94633b lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.60d21a75 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.d7728f2d lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.a0d68d5d lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.2cb57f78 lib/jekyll-octicons.rb
jekyll-octicons-4.3.0.pre.542d2f63 lib/jekyll-octicons.rb
jekyll-octicons-4.2.0 lib/jekyll-octicons.rb
jekyll-octicons-4.1.0.pre.802761e0 lib/jekyll-octicons.rb