Skip to content

Commit

Permalink
moving stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Mar 17, 2010
1 parent 7fe6bd4 commit 619fa9f
Show file tree
Hide file tree
Showing 44 changed files with 424 additions and 231 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions lib/core_ext/string/camelize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class String
def camelize
to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
end unless ''.respond_to?(:camelize)
25 changes: 25 additions & 0 deletions lib/core_ext/string/constantize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class String
if Module.method(:const_get).arity == 1
def constantize
names = split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
else
def constantize
names = split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_get(name, false) || constant.const_missing(name)
end
constant
end
end
end
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'slick/core_ext/string/humanize'
require 'slick/core_ext/string/underscore'
require 'core_ext/string/humanize'
require 'core_ext/string/underscore'

class String
def titleize
Expand Down
File renamed without changes.
9 changes: 4 additions & 5 deletions lib/slick.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'action_view'

module Slick
autoload :Blog, 'slick/blog'
autoload :Content, 'slick/content'
autoload :Builder, 'slick/builder'
autoload :Data, 'slick/data'
autoload :Page, 'slick/page'
autoload :Section, 'slick/section'
autoload :Site, 'slick/site'
autoload :Model, 'slick/model'
end
3 changes: 0 additions & 3 deletions lib/slick/blog.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/slick/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Slick::Builder
autoload :Blog, 'slick/builder/blog'
autoload :Content, 'slick/builder/content'
autoload :Page, 'slick/builder/page'
autoload :Section, 'slick/builder/section'

class << self
def create(parent, object, data)
const_get(object.class.name.split('::').last).new(data, parent)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Slick::Data::FileSystem
module Slick::Builder
class Blog < Section
end
end


20 changes: 20 additions & 0 deletions lib/slick/builder/content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'fileutils'

module Slick::Builder
class Content
def view
@@view ||= ActionView::Base.new([File.expand_path('../../views', __FILE__)])
end

def document_root
@@document_root ||= File.expand_path('../../../../public', __FILE__)
end

def write(file_name, html)
path = [document_root, path, file_name].compact.join('/')
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w+') { |f| f.write(html) }
end
end
end

2 changes: 1 addition & 1 deletion lib/slick/page.rb → lib/slick/builder/page.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Slick
module Slick::Builder
class Page < Section
end
end
38 changes: 38 additions & 0 deletions lib/slick/builder/section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Slick::Builder
class Section < Content
attr_reader :data, :site, :section, :parent

def initialize(data, parent = nil)
@parent = parent
@data = data
@site, @section = data.values_at(:site, :section)
end

def root?
parent.nil?
end

def path # TODO should be in the model, we don't have routing, so keep it simple
root? ? 'index' : [parent.root? ? nil : parent.path, section.slug].compact.join('/')
end

def build
render_index
section.contents.each { |content| render_content(content) }
section.children.each { |child| Slick::Builder.create(self, child, data.merge(:section => child)).build }
end

def render_index
write("#{path}.html", render('index', data))
end

def render_content(content)
write("#{content.permalink}.html", render('show', data.merge(:content => content)))
end

def render(template, locals)
view.render(:file => "#{section.type}/#{template}", :locals => locals)
end
end
end

3 changes: 0 additions & 3 deletions lib/slick/content.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/slick/data.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/slick/data/file_system.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/slick/data/file_system/content.rb

This file was deleted.

45 changes: 0 additions & 45 deletions lib/slick/data/file_system/path.rb

This file was deleted.

57 changes: 0 additions & 57 deletions lib/slick/data/file_system/section.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/slick/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'core_ext/string/camelize'

module Slick::Model
autoload :Base, 'slick/model/base'
autoload :Blog, 'slick/model/blog'
autoload :Content, 'slick/model/content'
autoload :Page, 'slick/model/page'
autoload :Section, 'slick/model/section'
autoload :Site, 'slick/model/site'

TYPES = {
'_articles' => Page,
'_posts' => Blog
}

class << self
def build(path)
determine_type(path).new(path)
end

def determine_type(path)
subdirs = Dir["#{path}/_*"].map { |path| File.basename(path) }
subdirs.each { |subdir| type = TYPES[subdir] and return type }
Page
end
end
end
63 changes: 63 additions & 0 deletions lib/slick/model/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'pathname'

module Slick::Model
class Base < Pathname
YAML_PREAMBLE = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m

attr_writer :attributes

def attributes
@attributes ||= read
end

def type
self.class.name.split('::').last.underscore
end

def slug # TODO
title.underscore.gsub(/[\W]/, '_')
end

def title_from_path
title = File.basename(self.to_s).gsub(extname, '').titleize
end

def dirname
directory? ? self : Pathname.new(self.to_s.gsub(extname, ''))
end

alias :created_at :ctime
alias :updated_at :mtime

def published_at
attributes.key?(:published_at) ? attributes[:published_at] : ctime
end

def read
attributes = { :title => title_from_path }
attributes.merge!(parse(::File.read(self.to_s).strip)) if file?
attributes
end

def parse(content)
parse_yaml_preamble(content).merge(:content => content)
end

def parse_yaml_preamble(content)
content.gsub!(YAML_PREAMBLE, '')
$1 ? YAML.load($1).symbolize_keys : {}
end

def respond_to?(name)
attributes.key?(name)
end

def method_missing(name, *args, &block)
respond_to?(name) ? attributes[name] : super
end

def inspect
super.gsub(/>/, " #{attributes.inspect}")
end
end
end
5 changes: 5 additions & 0 deletions lib/slick/model/blog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Slick::Model
class Blog < Section
end
end

Loading

0 comments on commit 619fa9f

Please sign in to comment.