lib/runeblog.rb in runeblog-0.0.67 vs lib/runeblog.rb in runeblog-0.0.69
- old
+ new
@@ -1,34 +1,23 @@
require 'find'
require 'yaml'
require 'livetext'
+require 'skeleton'
+require 'view'
+require 'deploy'
+require 'post'
+require 'helpers-blog'
+require 'version'
-def create_dir(dir) # FIXME move later
- cmd = "mkdir -p #{dir} >/dev/null 2>&1"
- result = system(cmd)
- raise "Can't create #{dir}" unless result
-end
+###
-def interpolate(str)
- wrap = "<<-EOS\n#{str}\nEOS"
- eval wrap
-end
-
-def error(err) # FIXME - this is duplicated
- str = "\n Error: #{err}"
- puts str
- puts err.backtrace # [0]
-end
-
class RuneBlog
- VERSION = "0.0.67"
class << self
attr_accessor :blog
end
- Path = File.expand_path(File.join(File.dirname(__FILE__)))
DefaultData = Path + "/../data/views/_default"
BlogHeaderPath = DefaultData + "/custom/blog_header.html"
BlogTrailerPath = DefaultData + "/custom/blog_trailer.html"
PostTemplatePath = DefaultData + "/custom/post_template.html"
@@ -303,185 +292,5 @@
files
end
end
-#######
-
-class RuneBlog::Deployment
- attr_reader :user, :server, :root, :path
-
- def initialize(user, server, root, path, protocol = "http")
- @blog = RuneBlog.blog
- @user, @server, @root, @path =
- user, server, root, path
- end
-
- def url
- url = "#{protocol}://#{@server}/#{@path}"
- end
-
- def deploy(files)
- reset_output
- dir = "#@root/#@path"
- result = system("ssh -c #@user@#@server mkdir #{dir}")
- list = files.join(' ')
- cmd = "scp -r #{list} root@#{server}:#{dir} >/dev/null 2>&1"
- output! "Deploying #{files.size} files...\n"
- result = system(cmd)
- raise "Problem occurred in deployment" unless result
-
- File.write("#{@blog.view.dir}/last_deployed", files)
- output! "...finished.\n"
- @out
- end
-end
-
-#######
-
-class RuneBlog::View
- attr_reader :name, :state
- attr_accessor :deploy
-
- def initialize(name)
- raise "RuneBlog.blog is not set!" if RuneBlog.blog.nil?
- @blog = RuneBlog.blog
- @name = name
- @deploy = read_config
- end
-
- def dir
- @blog.root + "/views/#@name/"
- end
-
- def index
- dir + "index.html"
- end
-
- def to_s
- @name
- end
-
- def files(recent = false)
- vdir = dir()
- files = [index()]
- others = Dir.entries(vdir).grep(/^\d\d\d\d/)
- files += others.map {|x| "#{vdir}/#{x}" }
- files.reject! {|f| recent?(f) } if recent
- files
- end
-
- def deploy
- # ?? @blog.view.deployment.deploy
- # output "Files:"
- # files.each {|f| output " #{f}\n" }
- output_newline
- list = files(true)
- @deployer.deploy(list)
- rescue => err
- error(err)
- end
-
- def recent?(file)
- File.mtime(file) < File.mtime("#{dir()}/last_deployed")
- end
-
- def read_config
- file = self.dir + "/deploy"
- lines = File.readlines(file).map(&:chomp)
- user, server, root, path, proto = *lines
- @deploy = RuneBlog::Deployment.new(user, server, root, path, proto)
- end
-
- def write_config
- file = @blog.view.dir + "/deploy"
- File.open(file) {|f| f.puts [@user, @server, @root, @path, @proto] }
- end
-end
-
-#######
-
-
-class RuneBlog::Post
-
- attr_reader :id, :title, :date, :views, :num, :slug
-
- def self.files(num, root)
- files = Find.find(root).to_a
- result = files.grep(/#{tag(num)}-/)
- result
- end
-
- def initialize(title, view_name)
- raise "RuneBlog.blog is not set!" if RuneBlog.blog.nil?
- @blog = RuneBlog.blog
- @title = title
- @view = @blog.str2view(view_name)
- @num, @slug = make_slug
- date = Time.now.strftime("%Y-%m-%d")
- template = <<-EOS.gsub(/^ */, "")
- .mixin liveblog
-
- .title #{title}
- .pubdate #{date}
- .views #@view
-
- .teaser
- Teaser goes here.
- .end
- Remainder of post goes here.
- EOS
-
- @draft = "#{@blog.root}/src/#@slug.lt3"
- File.write(@draft, template)
- end
-
- def edit
- result = system("vi #@draft +8")
- raise "Problem editing #@draft" unless result
- nil
- rescue => err
- error(err)
- end
-
- def publish
- livetext = Livetext.new(STDOUT)
- @meta = livetext.process_file(@draft, binding)
- raise "process_file returned nil" if @meta.nil?
-
- @meta.views.each do |view_name| # Create dir using slug (index.html, metadata?)
- view = @blog.str2view(view_name)
- vdir = view.dir
- dir = vdir + @slug + "/"
- Dir.mkdir(dir)
- Dir.chdir(dir) do
- create_post_subtree(vdir)
- @blog.generate_index(view)
- end
- end
- rescue => err
- p err
- err.backtrace.each {|x| puts x }
- # error(err)
- end
-
- private
-
- def create_post_subtree(vdir)
- create_dir("assets")
- File.write("metadata.yaml", @meta.to_yaml)
- template = File.read(vdir + "/custom/post_template.html")
- text = interpolate(template)
- File.write("index.html", text)
- end
-
- def make_slug(postnum = nil)
- postnum ||= @blog.next_sequence
- num = tag(postnum) # FIXME can do better
- slug = @title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
- [postnum, "#{num}-#{slug}"]
- end
-
- def tag(num)
- "#{'%04d' % num}"
- end
-end