module Middleman::Util::UriTemplates
def apply_uri_template(template, data)
-
(String)
- normalized path
Parameters:
-
data
(Hash
) -- -
template
(Addressable::Template
) --
def apply_uri_template(template, data) ::Middleman::Util.normalize_path(::Addressable::URI.unencode(template.expand(data)).to_s) end
def date_to_params(date)
-
(Hash)
- parameters
Parameters:
-
date
(DateTime
) --
def date_to_params(date) { year: date.year.to_s, month: date.month.to_s.rjust(2, '0'), day: date.day.to_s.rjust(2, '0') } end
def extract_params(template, path)
-
path
(String
) -- -
template
(Addressable::Template
) --
def extract_params(template, path) template.extract(path, BlogTemplateProcessor) end
def safe_parameterize(str)
def safe_parameterize(str) sep = '-' # Reimplementation of http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize that preserves un-transliterate-able multibyte chars. parameterized_string = ::ActiveSupport::Inflector.transliterate(str.to_s).downcase parameterized_string.gsub!(/[^a-z0-9\-_\?]+/, sep) parameterized_string.chars.to_a.each_with_index do |char, i| next unless char == '?' && str[i].bytes.count != 1 parameterized_string[i] = str[i] end re_sep = ::Regexp.escape(sep) # No more than one of the separator in a row. parameterized_string.gsub!(/#{re_sep}{2,}/, sep) # Remove leading/trailing separator. parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/, '') parameterized_string end
def uri_template(tmpl_src)
-
(Addressable::Template)
- a URI template
Parameters:
-
tmpl_src
(String
) -- URI template source
def uri_template(tmpl_src) # Support the RFC6470 templates directly if people use them if tmpl_src.include?(':') tmpl_src = tmpl_src.gsub(/:([A-Za-z0-9]+)/, '{\1}') end ::Addressable::Template.new(::Middleman::Util.normalize_path(tmpl_src)) end