Skip to content

Commit

Permalink
A blog, you say?
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed Sep 2, 2011
1 parent 9e584d4 commit 23992b3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/static_ish.rb
Expand Up @@ -12,7 +12,9 @@ module StaticIsh


module Pages module Pages
autoload :Base, 'static_ish/pages/base' # Base page implementation autoload :Base, 'static_ish/pages/base' # Base page implementation
autoload :Blog, 'static_ish/pages/blog'
autoload :Page, 'static_ish/pages/page' # Default page type autoload :Page, 'static_ish/pages/page' # Default page type
autoload :Post, 'static_ish/pages/post'
end end


module Parts module Parts
Expand All @@ -22,5 +24,4 @@ module Parts
autoload :Markdown, 'static_ish/parts/markdown' autoload :Markdown, 'static_ish/parts/markdown'
autoload :Textile, 'static_ish/parts/textile' autoload :Textile, 'static_ish/parts/textile'
end end

end end
50 changes: 34 additions & 16 deletions lib/static_ish/pages/base.rb
Expand Up @@ -4,6 +4,8 @@ class Base
attr_reader :site # site to which this page belongs attr_reader :site # site to which this page belongs
attr_reader :path # absolute FS path to this page attr_reader :path # absolute FS path to this page
attr_reader :parts # array of page parts attr_reader :parts # array of page parts

attr_accessor :url # page URL
attr_accessor :parent # parent page attr_accessor :parent # parent page


def [](k); @preamble[k.to_sym]; end def [](k); @preamble[k.to_sym]; end
Expand All @@ -12,17 +14,7 @@ def []=(k,v); @preamble[k.to_sym] = v; end
def initialize(site, path, preamble = {}, parts = []) def initialize(site, path, preamble = {}, parts = [])
preamble = preamble.inject({}) { |h,(k,v)| h[k.to_sym] = v; h } preamble = preamble.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
@site, @path, @preamble, @parts = site, path, preamble, parts @site, @path, @preamble, @parts = site, path, preamble, parts

@children = {}
relative_path = path[@site.public_root.length..-1]
p relative_path
end

def url
if parent.nil?
url_component
else
File.join(parent.url, url_component)
end
end end


def type def type
Expand All @@ -37,13 +29,39 @@ def self.preamble_reader(key, default = nil)
define_method(key) { self[key] || default } define_method(key) { self[key] || default }
end end


preamble_reader :layout preamble_reader :layout, nil
preamble_reader :title preamble_reader :title, ''
preamble_reader :subtitle preamble_reader :subtitle, ''

def find_page(url_cons)
(url_cons == '') ? self : find_child_page(url_cons)
end

def find_child_page(url_cons)
nxt, *cons = url_cons.split('/')
@children[nxt] ||= load_child_page(nxt)
@children[nxt] ? @children[nxt].find_page(cons.join('')) : nil
end


def find_page(path) private
self
# load a page at a path relative to current page and set the parent/URL
def load_child_page(path_cons)
load_child_from_relative_path(File.join(path_cons, 'index.page'), path_cons)
end end

def load_child_from_relative_path(relative_path, url_component)
child_path = File.join(File.dirname(path), relative_path)
if File.readable?(child_path)
child = site.load_page(child_path)
child.parent = self
child.url = url + url_component + '/'
child
else
nil
end
end

end end
end end
end end
37 changes: 37 additions & 0 deletions lib/static_ish/pages/blog.rb
@@ -0,0 +1,37 @@
module StaticIsh
module Pages
class Blog < Base
def find_child_page(url_cons)
if url_cons =~ /^\d{4}\/\d{2}\/...$/

else
nil
end
end

def posts(options = {})

end

private

def post_paths
unless defined?(@post_paths)
@post_paths = Dir.glob("#{File.dirname(path)}/*/*/*.page").inject([]) do |ary, p|
p = p.gsub(File.dirname(path) + '/', '')
if p =~ /^\d{4}\/\d{1,2}\/\d{1,2}-([a-z0-9_-]+)\.page$/i
chunks = p.split('/')
chunks[1] = "0#{chunks[1]}" if chunks[1] =~ /^\d$/
chunks[2] = "0#{chunks[2]}" if chunks[2] =~ /^\d-/
p = chunks.join('/')
end
ary
end
@post_paths.sort!
@post_paths.reverse!
end
@post_paths
end
end
end
end
7 changes: 7 additions & 0 deletions lib/static_ish/pages/post.rb
@@ -0,0 +1,7 @@
module StaticIsh
module Pages
class Post < Base

end
end
end
4 changes: 3 additions & 1 deletion lib/static_ish/registry.rb
Expand Up @@ -6,7 +6,9 @@ def initialize(site)
@site = site @site = site


@page_types = { @page_types = {
:page => '::StaticIsh::Pages::Page' :page => '::StaticIsh::Pages::Page',
:blog => '::StaticIsh::Pages::Blog',
:post => '::StaticIsh::Pages::Post'
} }


@part_types = { @part_types = {
Expand Down
13 changes: 9 additions & 4 deletions lib/static_ish/site.rb
Expand Up @@ -7,6 +7,7 @@ class Site
attr_reader :home # home page attr_reader :home # home page


def [](path) def [](path)
path = path.gsub(/(^\/+|\/+$)/, '').gsub(/\/+/, '/')
home.find_page(path) home.find_page(path)
end end


Expand All @@ -19,16 +20,17 @@ def initialize(root)
end end


def home def home
@home ||= load_page(File.join(public_root, 'index.page')) unless @home
@home = load_page(File.join(public_root, 'index.page'))
@home.url = '/'
end
@home
end end


def reload! def reload!
@home = nil @home = nil
end end


private
DEFAULT_PREAMBLE = {'layout' => 'main', 'type' => 'page'}

def load_page(path) def load_page(path)
@parts, @part, @preamble, @buffer = [], nil, DEFAULT_PREAMBLE.dup, '' @parts, @part, @preamble, @buffer = [], nil, DEFAULT_PREAMBLE.dup, ''


Expand All @@ -48,6 +50,9 @@ def load_page(path)
page page
end end


private
DEFAULT_PREAMBLE = {'layout' => 'main', 'type' => 'page'}

def commit_part! def commit_part!
if @part.nil? if @part.nil?
@buffer.strip! @buffer.strip!
Expand Down

0 comments on commit 23992b3

Please sign in to comment.