Module: Middleman::Blog::UriTemplates

Included in:
BlogData, CalendarPages, CustomPages, Paginator, TagPages, Cli::Article
Defined in:
lib/middleman-blog/uri_templates.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



40
41
42
# File 'lib/middleman-blog/uri_templates.rb', line 40

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



73
74
75
76
77
78
79
# File 'lib/middleman-blog/uri_templates.rb', line 73

def date_to_params(date)
  return {
    year: date.year.to_s,
    month: date.month.to_s.rjust(2, '0'),
    day: date.day.to_s.rjust(2, '0')
  }
end

.extract_directory_path(article_path) ⇒ Object



84
85
86
87
88
89
# File 'lib/middleman-blog/uri_templates.rb', line 84

def extract_directory_path( article_path )
  uri = Addressable::URI.parse article_path

  # Remove file extension from the article path
  directory_path = uri.path.gsub( uri.extname, '' )
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)


51
52
53
# File 'lib/middleman-blog/uri_templates.rb', line 51

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

.safe_parameterize(str) ⇒ Object

Parametrize a string preserving any multi-byte characters Reimplementation of this, preserves un-transliterate-able multibyte chars.



61
62
63
64
# File 'lib/middleman-blog/uri_templates.rb', line 61

def safe_parameterize(str)
  parameterized_string = ::ActiveSupport::Inflector.transliterate(str.to_s)
  parameterized_string.parameterize
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



23
24
25
26
27
28
29
30
# File 'lib/middleman-blog/uri_templates.rb', line 23

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