Module: Middleman::Util::UriTemplates

Defined in:
lib/middleman-core/util.rb

Overview

Handy methods for dealing with URI templates. Mix into whatever class.

Class Method Summary collapse

Class Method Details

.apply_uri_template(template, data) ⇒ String

Apply a URI template with the given data, producing a normalized Middleman path.

Parameters:

  • template (Addressable::Template)
  • data (Hash)

Returns:

  • (String)

    normalized path



560
561
562
# File 'lib/middleman-core/util.rb', line 560

def apply_uri_template(template, data)
  ::Middleman::Util.normalize_path Addressable::URI.unencode(template.expand(data)).to_s
end

.date_to_params(date) ⇒ Hash

Convert a date into a hash of components to strings suitable for using in a URL template.

Parameters:

  • date (DateTime)

Returns:

  • (Hash)

    parameters



599
600
601
602
603
604
605
# File 'lib/middleman-core/util.rb', line 599

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

.extract_params(template, path) ⇒ Object

Use a template to extract parameters from a path, and validate some special (date) keys. Returns nil if the special keys don't match.

Parameters:

  • template (Addressable::Template)
  • path (String)


569
570
571
# File 'lib/middleman-core/util.rb', line 569

def extract_params(template, path)
  template.extract(path, BlogTemplateProcessor)
end

.safe_parameterize(str) ⇒ Object

Parameterize a string preserving any multibyte characters



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/middleman-core/util.rb', line 574

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

.uri_template(tmpl_src) ⇒ Addressable::Template

Given a URI template string, make an Addressable::Template This supports the legacy middleman-blog/Sinatra style :colon URI templates as well as RFC6570 templates.

Parameters:

  • tmpl_src (String)

    URI template source

Returns:

  • (Addressable::Template)

    a URI template



545
546
547
548
549
550
551
552
# File 'lib/middleman-core/util.rb', line 545

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