class BridgetownInlineSvg::Markup

Constants

PARAM_SYNTAX

Parse the first parameter in a string, giving :

[full_match, param_name, double_quoted_val, single_quoted_val, unquoted_val]

The Regex works like :

  • first group

    - match a group of characters that is alphanumeric, _ or -.
  • second group (non-capturing OR)

    - match a double-quoted string
    - match a single-quoted string
    - match an unquoted string matching the set : [\w\.\-#]
PATH_SYNTAX

Separate file path from other attributes

Attributes

markup[R]

Public Class Methods

new(markup) click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 31
def initialize(markup)
  @markup = markup
end
parse(markup) click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 27
def self.parse(markup)
  new(markup).call
end

Public Instance Methods

call() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 35
def call
  raise_exception! unless matched

  [path, params]
end

Private Instance Methods

matched() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 43
def matched
  @matched ||= markup.strip.match(PATH_SYNTAX)
end
params() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 51
def params
  @params ||= begin
    @params = {}
    params_parse_with_liquid_match!
    params_parse_with_custom_match!
    params_set_height_if_missing!
    @params
  end
end
params_parse_with_custom_match!() click to toggle source

Scan using our regex to support id=“some-id”

# File lib/bridgetown-inline-svg/markup.rb, line 70
def params_parse_with_custom_match!
  raw_params.scan(PARAM_SYNTAX) do |key, group_2, group_3, group_4|
    @params[key.to_sym] = (group_2 || group_3 || group_4)
  end
end
params_parse_with_liquid_match!() click to toggle source

Scan for arguments using liquids regex From: github.com/Shopify/liquid/blob/57c9cf64ebc777fe5e92d4408d31a911f087eeb4/lib/liquid/tags/render.rb#L27

# File lib/bridgetown-inline-svg/markup.rb, line 63
def params_parse_with_liquid_match!
  raw_params.scan(Liquid::TagAttributes) do |key, value|
    @params[key.to_sym] = value
  end
end
params_set_height_if_missing!() click to toggle source

IE11 requires we have both width & height attributes on SVG elements

# File lib/bridgetown-inline-svg/markup.rb, line 78
def params_set_height_if_missing!
  @params[:height] = @params[:width] if @params.key?(:width) && @params[:width] != "" && !@params.key?(:height)
end
path() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 47
def path
  @path ||= matched["path"].sub(/^["']/, "").sub(/["']$/, "").strip
end
raise_exception!() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 86
    def raise_exception!
      raise SyntaxError, <<~END
        Syntax Error in tag 'svg' while parsing the following markup:
        #{markup}
        Valid syntax: svg <path> [property=value]
      END
    end
raw_params() click to toggle source
# File lib/bridgetown-inline-svg/markup.rb, line 82
def raw_params
  @raw_params = matched["params"].strip
end