Permalink
Browse files

moving stuff around

  • Loading branch information...
1 parent 7fe6bd4 commit 619fa9f526a8316cc01a7b64864c5cd000910b37 Sven Fuchs committed Mar 16, 2010
Showing with 424 additions and 231 deletions.
  1. 0 lib/{slick → }/core_ext/hash/symbolize_keys.rb
  2. +5 −0 lib/core_ext/string/camelize.rb
  3. +25 −0 lib/core_ext/string/constantize.rb
  4. 0 lib/{slick → }/core_ext/string/humanize.rb
  5. +2 −2 lib/{slick → }/core_ext/string/titleize.rb
  6. 0 lib/{slick → }/core_ext/string/underscore.rb
  7. +4 −5 lib/slick.rb
  8. +0 −3 lib/slick/blog.rb
  9. +12 −0 lib/slick/builder.rb
  10. +1 −2 lib/slick/{data/file_system → builder}/blog.rb
  11. +20 −0 lib/slick/builder/content.rb
  12. +1 −1 lib/slick/{ → builder}/page.rb
  13. +38 −0 lib/slick/builder/section.rb
  14. +0 −3 lib/slick/content.rb
  15. +0 −3 lib/slick/data.rb
  16. +0 −7 lib/slick/data/file_system.rb
  17. +0 −28 lib/slick/data/file_system/content.rb
  18. +0 −45 lib/slick/data/file_system/path.rb
  19. +0 −57 lib/slick/data/file_system/section.rb
  20. +27 −0 lib/slick/model.rb
  21. +63 −0 lib/slick/model/base.rb
  22. +5 −0 lib/slick/model/blog.rb
  23. +15 −0 lib/slick/model/content.rb
  24. +2 −1 lib/slick/{data/file_system → model}/page.rb
  25. +36 −0 lib/slick/model/section.rb
  26. +0 −3 lib/slick/section.rb
  27. +0 −2 lib/slick/site.rb
  28. +1 −0 lib/slick/views/blog/index.html.erb
  29. +1 −0 lib/slick/views/blog/show.html.erb
  30. +1 −0 lib/slick/views/page/index.html.erb
  31. +1 −0 public/2010/3/15/ripper2_ruby__modify_and_recompile_your_ruby_code.html
  32. +1 −0 public/2010/3/15/using_ruby_1_9_ripper.html
  33. +1 −0 public/articles_page.html
  34. +1 −0 public/index.html
  35. +8 −0 test/all.rb
  36. +36 −0 test/builder_test.rb
  37. +0 −65 test/file_system_test.rb
  38. +3 −2 test/fixtures/home/articles.html
  39. +2 −2 test/fixtures/home/articles/child.html
  40. +27 −0 test/model/base_test.rb
  41. +11 −0 test/model/section_test.rb
  42. +57 −0 test/model_test.rb
  43. +2 −0 test/test_helper.rb
  44. +15 −0 test/view_test.rb
@@ -0,0 +1,5 @@
+class String
+ def camelize
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ end
+end unless ''.respond_to?(:camelize)
@@ -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
@@ -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
View
@@ -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
View
@@ -1,3 +0,0 @@
-class Slick::Blog
-end
-
View
@@ -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
@@ -1,6 +1,5 @@
-module Slick::Data::FileSystem
+module Slick::Builder
class Blog < Section
end
end
-
@@ -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
+
@@ -1,4 +1,4 @@
-module Slick
+module Slick::Builder
class Page < Section
end
end
@@ -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
+
@@ -1,3 +0,0 @@
-class Slick::Content
-end
-
View
@@ -1,3 +0,0 @@
-module Slick::Data
- autoload :FileSystem, 'slick/data/file_system'
-end
@@ -1,7 +0,0 @@
-module Slick::Data::FileSystem
- autoload :Blog, 'slick/data/file_system/blog'
- autoload :Content, 'slick/data/file_system/content'
- autoload :Page, 'slick/data/file_system/page'
- autoload :Path, 'slick/data/file_system/path'
- autoload :Section, 'slick/data/file_system/section'
-end
@@ -1,28 +0,0 @@
-require 'yaml'
-require 'slick/core_ext/hash/symbolize_keys'
-require 'slick/core_ext/string/titleize'
-
-module Slick::Data::FileSystem
- class Content < Path
- YAML_PREAMBLE = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
-
- def title_from_path
- title = basename.gsub(extname, '').titleize
- end
-
- def read
- super.merge(file? ? parse(::File.read(path).strip) : {})
- 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
- end
-end
-
-
@@ -1,45 +0,0 @@
-module Slick::Data::FileSystem
- class Path
- attr_accessor :path, :attributes
-
- def initialize(path)
- @path = path
- end
-
- def read
- { :title => title_from_path, :created_at => ctime, :updated_at => mtime }
- end
-
- def file?
- File.file?(path)
- end
-
- def directory?
- File.directory?(directory)
- end
-
- def directory
- file? ? path.gsub(File.extname(path), '') : path
- end
-
- def file?
- File.file?(path)
- end
-
- def basename
- File.basename(path)
- end
-
- def extname
- File.extname(path)
- end
-
- def ctime
- File.ctime(path)
- end
-
- def mtime
- File.mtime(path)
- end
- end
-end
@@ -1,57 +0,0 @@
-module Slick::Data::FileSystem
- class Section < Content
- TYPES = {
- '_articles' => Page,
- '_posts' => Blog
- }
-
- class << self
- def build_from(path)
- section = 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
-
- def read
- data = super
- data[:type] = type
- data[:contents] = build_contents if type_dir? # would happen in Blog and Page
- data[:children] = build_children if directory?
- data
- end
-
- def type
- self.class.name.split('::').last.underscore
- end
-
- def type_dir
- directory? && (subdir = TYPES.invert[self.class]) ? directory + '/' + subdir : nil
- end
-
- def type_dir?
- type_dir && File.directory?(type_dir)
- end
-
- def build_contents
- Dir["#{type_dir}/**/*"].map { |path| Content.new(path).read }
- end
-
- def build_children
- child_paths.map { |path| Section.build_from(path).read }
- end
-
- def child_paths
- paths = Dir["#{directory}/[^_]*"].sort.reverse
- paths.inject([paths.shift]) do |paths, path|
- # i.e. skip articles/ if we already have articles.html
- paths.unshift path unless paths.first == path + File.extname(paths.first)
- paths
- end
- end
- end
-end
View
@@ -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
@@ -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
@@ -0,0 +1,5 @@
+module Slick::Model
+ class Blog < Section
+ end
+end
+
Oops, something went wrong.

0 comments on commit 619fa9f

Please sign in to comment.