got pages working
This commit is contained in:
parent
f06d2e60da
commit
33dbb78137
|
@ -16,6 +16,7 @@ require 'redcloth'
|
|||
# internal requires
|
||||
require 'autoblog/site'
|
||||
require 'autoblog/post'
|
||||
require 'autoblog/page'
|
||||
|
||||
module AutoBlog
|
||||
VERSION = '1.0.0'
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
module AutoBlog
|
||||
|
||||
class Page
|
||||
attr_accessor :ext
|
||||
attr_accessor :data, :content
|
||||
|
||||
def initialize(base, dir, name)
|
||||
@base = base
|
||||
@dir = dir
|
||||
@name = name
|
||||
|
||||
self.process(name)
|
||||
self.read_yaml(base, dir, name)
|
||||
self.set_defaults
|
||||
self.transform
|
||||
end
|
||||
|
||||
def process(name)
|
||||
self.ext = File.extname(name)
|
||||
end
|
||||
|
||||
def read_yaml(base, dir, name)
|
||||
self.content = File.read(File.join(base, dir, name))
|
||||
|
||||
if self.content =~ /^(---\n.*?)\n---\n/
|
||||
self.content = self.content[($1.size + 5)..-1]
|
||||
|
||||
self.data = YAML.load($1)
|
||||
end
|
||||
end
|
||||
|
||||
def set_defaults
|
||||
self.data["layout"] ||= "default"
|
||||
end
|
||||
|
||||
def transform
|
||||
if self.ext == "textile"
|
||||
self.ext = "html"
|
||||
self.content = RedCloth.new(self.content).to_html
|
||||
end
|
||||
end
|
||||
|
||||
def add_layout(layouts)
|
||||
payload = {"page" => self.data}
|
||||
self.content = Liquid::Template.parse(self.content).render(payload)
|
||||
|
||||
layout = layouts[self.data["layout"]] || self.content
|
||||
payload = {"content" => self.content, "page" => self.data}
|
||||
|
||||
self.content = Liquid::Template.parse(layout).render(payload)
|
||||
end
|
||||
|
||||
def write(dest)
|
||||
FileUtils.mkdir_p(File.join(dest, @dir))
|
||||
|
||||
path = File.join(dest, @dir, @name)
|
||||
File.open(path, 'w') do |f|
|
||||
f.write(self.content)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -15,6 +15,7 @@ module AutoBlog
|
|||
self.read_layouts
|
||||
self.read_posts
|
||||
self.write_posts
|
||||
self.transform_pages
|
||||
end
|
||||
|
||||
def read_layouts
|
||||
|
@ -50,6 +51,23 @@ module AutoBlog
|
|||
post.write(self.dest)
|
||||
end
|
||||
end
|
||||
|
||||
def transform_pages(dir = '')
|
||||
base = File.join(self.source, dir)
|
||||
entries = Dir.entries(base)
|
||||
entries = entries.reject { |e| %w{_layouts posts}.include?(e) }
|
||||
entries = entries.reject { |e| e[0..0] == '.' }
|
||||
|
||||
entries.each do |f|
|
||||
if File.directory?(File.join(base, f))
|
||||
transform_pages(File.join(dir, f))
|
||||
else
|
||||
page = Page.new(self.source, dir, f)
|
||||
page.add_layout(self.layouts)
|
||||
page.write(self.dest)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue