lib/jekyll-inline-svg.rb in jekyll-inline-svg-1.0.1 vs lib/jekyll-inline-svg.rb in jekyll-inline-svg-1.1.0

- old
+ new

@@ -21,11 +21,11 @@ # For interpoaltion, look for liquid variables VARIABLE = /\{\{\s*([\w]+\.?[\w]*)\s*\}\}/i #Separate file path from other attributes PATH_SYNTAX = %r! - ^(?<path>[^\s"']+|"[^"]*"|'[^']*') + ^(?<path>[^\s"']+|"[^"]+"|'[^']+') (?<params>.*) !x # parse the first parameter in a string, giving : # [full_match, param_name, double_quoted_val, single_quoted_val, unquoted_val] @@ -39,13 +39,13 @@ PARAM_SYNTAX= %r! ([\w-]+)\s*=\s* (?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.\-#]+)) !x - def initialize(tag_name, input, tokens) + def initialize(tag_name, markup, tokens) super - @svg, @params = JekyllInlineSvg.parse_params(input) + @svg, @params = JekyllInlineSvg.parse_params(markup) end #lookup Liquid variables from markup in context def interpolate(markup, context) markup.scan VARIABLE do |variable| @@ -68,15 +68,22 @@ end return params end #Parse parameters. Returns : [svg_path, parameters] # Does not interpret variables as it's done at render time - def self.parse_params(input) - matched = input.strip.match(PATH_SYNTAX) - path = matched["path"].gsub("\"","").gsub("'","").strip - markup = matched["params"].strip - return path, markup + def self.parse_params(markup) + matched = markup.strip.match(PATH_SYNTAX) + if !matched + raise SyntaxError, <<~END + Syntax Error in tag 'highlight' while parsing the following markup: + #{markup} + Valid syntax: svg <path> [property=value] + END + end + path = matched["path"].sub(%r!^["']!,"").sub(%r!["']$!,"").strip + params = matched["params"].strip + return path, params end def fmt(params) r = params.to_a.select{|v| v[1] != ""}.map {|v| %!#{v[0]}="#{v[1]}"!} r.join(" ") end @@ -91,14 +98,25 @@ end end mod.set(params) return mod end + def add_file_to_dependency(site, path, context) + if context.registers[:page] && context.registers[:page].key("path") + site.regenerator.add_dependency( + site.in_source_dir(context.registers[:page]["path"]), + path + ) + end + end + def render(context) #global site variable site = context.registers[:site] #check if given name is a variable. Otherwise use it as a file name svg_file = Jekyll.sanitized_path(site.source, interpolate(@svg,context)) + return unless svg_file + add_file_to_dependency(site,svg_file, context) #replace variables with their current value params = split_params(@params,context) #because ie11 require to have a height AND a width if params.key? "width" and ! params.key? "height" params["height"] = params["width"]