Class: Staticpress::Content::Page

Inherits:
Base
  • Object
show all
Extended by:
ResourceContent, StaticContent
Includes:
ResourceContent, StaticContent
Defined in:
lib/staticpress/content/page.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#params, #template_types

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceContent

find_supported_extensions, gather_resources_from, load_resource

Methods included from StaticContent

layout, parse_slug, render_partial, supported_extensions

Methods inherited from Base

#==, #content, #content_type, #exist?, find_by_url_path, #full_title, #layout, #markup_template?, #meta, #optional_param_defaults, #output_path, #published?, #raw, #render, #render_partial, #save!, #template_context, #template_engine_options, #template_extension, #template_path_content, #theme, theme, #title, #to_s, type, #url_path

Methods included from Helpers

#config, #extensionless_basename, #extensionless_path, #hash_from_array, #hash_from_match_data, #paginate, #settings, #spider_map, #titleize

Constructor Details

#initialize(params = {}) ⇒ Page

Returns a new instance of Page.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/staticpress/content/page.rb', line 12

def initialize(params = {})
  super

  source_path = Staticpress.blog_path + config.source_path

  @full_slug = params[:slug]
  @template_types = find_supported_extensions(source_path + full_slug)

  unless template_path.file?
    @full_slug = [
      params[:slug],
      extensionless_basename(Pathname(config.index_file))
    ].reject(&:empty?).join('/')
    @template_types = find_supported_extensions(source_path + full_slug)
  end
end

Instance Attribute Details

#full_slugObject (readonly)

Returns the value of attribute full_slug.



10
11
12
# File 'lib/staticpress/content/page.rb', line 10

def full_slug
  @full_slug
end

Class Method Details

.allObject



45
46
47
48
49
50
51
52
53
# File 'lib/staticpress/content/page.rb', line 45

def self.all
  all_but_posts = if (posts_dir = Staticpress.blog_path + config.posts_source_path).directory?
    (Staticpress.blog_path + config.source_path).children - [ posts_dir ]
  else
    (Staticpress.blog_path + config.source_path).children
  end

  gather_resources_from all_but_posts
end

.create(format, title, path = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/staticpress/content/page.rb', line 59

def self.create(format, title, path = nil)
  name = title.gsub(/ /, '-').downcase

  filename = "#{name}.#{format}"
  destination = Staticpress.blog_path + config.source_path + (path ? path : '').sub(/^\//, '') + filename

  FileUtils.mkdir_p destination.dirname
  destination.open('w') { |f| f.write template }
end

.extract_slug(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/staticpress/content/page.rb', line 69

def self.extract_slug(path)
  base_path = Staticpress.blog_path + config.source_path
  supported = find_supported_extensions(path)
  extension = supported.empty? ? '' : "\\.#{supported.reverse.join('\.')}"

  if match = /^#{base_path}\/(?<slug>.+)#{extension}$/.match(path.to_s)
    basename = extensionless_basename Pathname(config.index_file)
    match[:slug].sub(/.*(\/?#{basename})$/, '')
  end
end

.find_by_path(path) ⇒ Object



80
81
82
# File 'lib/staticpress/content/page.rb', line 80

def self.find_by_path(path)
  new :slug => extract_slug(path) if path.file?
end

.publishedObject



55
56
57
# File 'lib/staticpress/content/page.rb', line 55

def self.published
  all.select &:published?
end

.templateObject



84
85
86
87
88
# File 'lib/staticpress/content/page.rb', line 84

def self.template
  <<-TEMPLATE
in page
  TEMPLATE
end

Instance Method Details

#preferred_layout_namesObject



37
38
39
# File 'lib/staticpress/content/page.rb', line 37

def preferred_layout_names
  [meta.layout, :page]
end

#saveObject



29
30
31
# File 'lib/staticpress/content/page.rb', line 29

def save
  save!
end

#static?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/staticpress/content/page.rb', line 33

def static?
  (Staticpress.blog_path + config.source_path + params[:slug]).file?
end

#template_pathObject



41
42
43
# File 'lib/staticpress/content/page.rb', line 41

def template_path
  Staticpress.blog_path + config.source_path + "#{full_slug}#{template_extension}"
end