class Ruhoh::UrlSlug
Public Class Methods
new(opts)
click to toggle source
# File lib/ruhoh/url_slug.rb, line 3 def initialize(opts) @page_data = opts[:page_data] @format = opts[:format] @pointer = @page_data["pointer"] end
Public Instance Methods
category()
click to toggle source
Category is only the first one if multiple categories exist.
# File lib/ruhoh/url_slug.rb, line 85 def category string = Array(@page_data['categories'])[0] return '' if string.to_s.empty? string.split('/').map { |c| Ruhoh::StringFormat.clean_slug_and_escape(c) }.join('/') end
data()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 45 def data result = @page_data result = result.merge(date_data) if uses_date? result.merge({ "filename" => filename, "path" => path, "relative_path" => relative_path, "categories" => category, }) end
date_data()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 57 def date_data date = Time.parse(@page_data['date'].to_s) { "year" => date.strftime("%Y"), "month" => date.strftime("%m"), "day" => date.strftime("%d"), "i_day" => date.strftime("%d").to_i.to_s, "i_month" => date.strftime("%m").to_i.to_s, } rescue ArgumentError, TypeError Ruhoh.log.error( "ArgumentError:" + " The file '#{ @pointer["realpath"] }' has a permalink '#{ @format }'" + " which is date dependant but the date '#{ @page_data['date'] }' could not be parsed." + " Ensure the date's format is: 'YYYY-MM-DD'" ) end
dynamic()
click to toggle source
@return the dynamic URL with token substitution.
# File lib/ruhoh/url_slug.rb, line 22 def dynamic cache = data result = @format .gsub(/:[^\/\.]+/) { |a| cache[$&.gsub(':', '')] } .gsub('//', '/') .split('/') # this is ugly but I'm out of ideas. Help! last = result.pop if uses_extension? last = last .split('.') .map{ |a| Ruhoh::StringFormat.clean_slug_and_escape(a) } .join('.') else last = Ruhoh::StringFormat.clean_slug_and_escape(last) end result .map{ |a| Ruhoh::StringFormat.clean_slug_and_escape(a) } .join('/') + "/#{ last }" end
ext()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 80 def ext File.extname(@page_data['id']) end
filename()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 76 def filename File.basename(@page_data['id'], ext) end
generate()
click to toggle source
@return URL Slug based on the given data and format.
# File lib/ruhoh/url_slug.rb, line 10 def generate url = @format.include?(':') ? dynamic : literal url = process_url_extension(url) url.empty? ? '/' : url end
literal()
click to toggle source
@return the literal URL without token substitution.
# File lib/ruhoh/url_slug.rb, line 17 def literal @format.gsub(/^\//, '').split('/').map {|p| CGI::escape(p) }.join('/') end
path()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 99 def path File.join(@pointer["resource"], relative_path) end
relative_path()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 94 def relative_path string = File.dirname(@page_data['id']) (string == ".") ? "" : string end
Private Instance Methods
process_url_extension(url)
click to toggle source
The url extension depends on multiple factors: user-config : preserve any extension set by the user in the format. converters : Automatically change convertable extensions to .html
Non-convertable file-extensions should 'pass-through'
‘pretty’ : Automatically prettify urls (omit .html) unless user disabled.
# File lib/ruhoh/url_slug.rb, line 129 def process_url_extension(url) return url if uses_extension? url += Ruhoh::Converter.extensions.include?(ext) ? '.html' : ext # Prettify by default @page_data['permalink_ext'] ? url : url.gsub(/index|index.html$/, '').gsub(/\.html$/, '') end
uses_date?()
click to toggle source
# File lib/ruhoh/url_slug.rb, line 105 def uses_date? result = false %w{ :year :month :day :i_day :i_month }.each do |token| if @format.include?(token) result = true break end end result end
uses_extension?()
click to toggle source
Is an extension explicitly defined?
# File lib/ruhoh/url_slug.rb, line 118 def uses_extension? @format =~ /\.[^\.]+$/ end