class Staticpress::Content::Base
Attributes
params[R]
template_types[R]
Public Class Methods
find_by_url_path(url_path)
click to toggle source
# File lib/staticpress/content/base.rb, line 202 def self.find_by_url_path(url_path) params = Staticpress::Route.extract_params config.routes[type], url_path new params if params end
new(params = {})
click to toggle source
# File lib/staticpress/content/base.rb, line 13 def initialize(params = {}) clean_params = params.select { |key, value| value }.map do |key, value| cast_value = case Staticpress::Route::REGEX_STUBS[key].klass when :integer then Integer value rescue value when :symbol then value.to_sym else value end [ key, cast_value ] end @params = optional_param_defaults.merge Hash[clean_params] @template_types = [] end
theme()
click to toggle source
# File lib/staticpress/content/base.rb, line 207 def self.theme Staticpress::Theme.theme end
type()
click to toggle source
# File lib/staticpress/content/base.rb, line 211 def self.type name.split('::').last.downcase end
Public Instance Methods
==(other)
click to toggle source
# File lib/staticpress/content/base.rb, line 28 def ==(other) other.respond_to?(:params) && (params == other.params) && other.respond_to?(:url_path) && (url_path == other.url_path) end
content()
click to toggle source
# File lib/staticpress/content/base.rb, line 33 def content return @content if @content regex_frontmatter = /^-{3}${1}(?<frontmatter>.*)^-{3}${1}/m regex_text = /(?<text>.*)/m regex = /(#{regex_frontmatter})?#{regex_text}/ c = template_path_content @content = if Tilt.mappings.include?(template_path.extname[1..-1]) c.match regex else { :text => c } end end
content_type()
click to toggle source
# File lib/staticpress/content/base.rb, line 49 def content_type Rack::Mime.mime_type output_path.extname end
exist?()
click to toggle source
# File lib/staticpress/content/base.rb, line 53 def exist? template_path.file? end
full_title()
click to toggle source
# File lib/staticpress/content/base.rb, line 57 def full_title [ title, config.title ].join config.title_separators.site end
layout()
click to toggle source
# File lib/staticpress/content/base.rb, line 64 def layout if meta.layout || config.markup_templates.include?(template_path.extname[1..-1]) Tilt.new theme.layout_for(*preferred_layout_names).to_s end end
markup_template?()
click to toggle source
# File lib/staticpress/content/base.rb, line 70 def markup_template? config.markup_templates.include?(template_path.extname[1..-1]) end
meta()
click to toggle source
# File lib/staticpress/content/base.rb, line 74 def meta Staticpress::Metadata.new(content[:frontmatter] ? YAML.load(content[:frontmatter]) : {}) rescue Psych::SyntaxError => e warn "Could not parse frontmatter for #{template_path}", content[:frontmatter] raise e end
optional_param_defaults()
click to toggle source
# File lib/staticpress/content/base.rb, line 81 def optional_param_defaults {} end
output_path()
click to toggle source
# File lib/staticpress/content/base.rb, line 85 def output_path base = Staticpress.blog_path + config.destination_path + url_path.sub(/^\//, '') # FIXME need a better check if (markup_template? && config.index_file && (Pathname(config.index_file).extname != base.extname)) base + config.index_file else base end end
published?()
click to toggle source
# File lib/staticpress/content/base.rb, line 96 def published? published = meta.published if published.is_a? Time published <= Time.utc else published.nil? ? true : published end end
raw()
click to toggle source
# File lib/staticpress/content/base.rb, line 105 def raw content[:text].strip end
render(locals = {})
click to toggle source
# File lib/staticpress/content/base.rb, line 109 def render(locals = {}) if l = layout l.render template_context, locals do render_partial locals end else render_partial locals end end
render_partial(locals = {})
click to toggle source
# File lib/staticpress/content/base.rb, line 119 def render_partial(locals = {}) template_types.inject(raw) do |result, template_type| template = Tilt.new(template_type.to_s, template_engine_options(template_type)) { result } template.render template_context, locals end end
save()
click to toggle source
# File lib/staticpress/content/base.rb, line 126 def save save! unless output_path.file? end
save!()
click to toggle source
# File lib/staticpress/content/base.rb, line 130 def save! if settings.verbose width = config.routes.to_hash.keys.max_by { |sym| sym.to_s.length }.to_s.length type = self.class.type.rjust width path = output_path.to_s.sub Staticpress.blog_path.to_s + '/', '' puts "#{type} #{path}" end FileUtils.mkdir_p output_path.dirname output_path.open('w') { |f| f.write render } end
template_context()
click to toggle source
# File lib/staticpress/content/base.rb, line 142 def template_context Staticpress::ViewHelpers.new self end
template_engine_options(template_type)
click to toggle source
# File lib/staticpress/content/base.rb, line 146 def template_engine_options(template_type) (config.template_engine_options[template_type] || {}).to_hash end
template_extension()
click to toggle source
# File lib/staticpress/content/base.rb, line 150 def template_extension template_types.empty? ? '' : ".#{template_types.reverse.join('.')}" end
template_path_content()
click to toggle source
# File lib/staticpress/content/base.rb, line 154 def template_path_content exist? ? template_path.read : '' end
theme()
click to toggle source
# File lib/staticpress/content/base.rb, line 158 def theme self.class.theme end
title()
click to toggle source
# File lib/staticpress/content/base.rb, line 162 def title if meta.title meta.title else titleize(url_path) end end
to_s()
click to toggle source
# File lib/staticpress/content/base.rb, line 170 def to_s "#<#{self.class} url_path=#{url_path}, params=#{Hash[params.sort]}>" end
url_path()
click to toggle source
# File lib/staticpress/content/base.rb, line 174 def url_path # grab url pattern for content type pattern = config.routes[self.class.type].clone # regex to find optional segment in pattern # NOTE cannot find more than one optional segment per pattern optional_segment_finder = /\((?<optional_segment>.+)\)\?/ # replace optional segment in pattern with result of block # optional segments should have one keys pattern.gsub! optional_segment_finder do |optional_segment| # http://www.rubular.com/r/LiOup53CI1 # http://www.rubular.com/r/TE7E9ZdtKF # grab the key from optional segment optional_key = /:(?<optional_key>[0-9a-z_]+)/.match(optional_segment)[:optional_key].to_sym # if params has the optional key and params optional key is not the key's default, remove optional segment indicators # otherwise return nil to remove optional segment entirely optional_segment_finder.match(optional_segment)[:optional_segment] if params[optional_key] && (params[optional_key] != optional_param_defaults[optional_key]) end # actually do conversion from pattern to url path Staticpress::Route::REGEX_STUBS.keys.inject(pattern) do |p, key| p.gsub /:#{key}/, params[key].to_s end end