class RuneBlog
VERSION = "0.0.29"
Path = File.expand_path(File.join(File.dirname(__FILE__)))
DefaultData = Path + "/../data"
BlogHeader = File.read(DefaultData + "/custom/blog_header.html") rescue "not found"
BlogTrailer = File.read(DefaultData + "/custom/blog_trailer.html") rescue "not found"
end
class RuneBlog::Config
attr_reader :root, :views, :view, :sequence
def initialize(cfg_file = ".blog")
# What views are there? Deployment, etc.
# Crude - FIXME later
new_blog! unless File.exist?(cfg_file)
lines = File.readlines(cfg_file).map {|x| x.chomp }
@root = lines[0]
@view = lines[1]
dirs = Dir.entries("#@root/views/") - %w[. ..]
dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
@root = root
@views = dirs
@sequence = File.read(root + "/sequence").to_i
end
def next_sequence
@sequence += 1
File.open("#@root/sequence", "w") {|f| f.puts @sequence }
@sequence
end
def viewdir(v)
@root + "/views/#{v}/"
end
end
require 'find'
require 'yaml'
require 'rubygems'
require 'ostruct'
require 'livetext'
def clear
puts "\e[H\e[2J" # clear screen
end
def red(text)
"\e[31m#{text}\e[0m"
end
def blue(text)
"\e[34m#{text}\e[0m"
end
def bold(str)
"\e[1m#{str}\e[22m"
end
def interpolate(str)
wrap = "<<-EOS\n#{str}\nEOS"
eval wrap
end
def colored_slug(slug)
red(slug[0..3])+blue(slug[4..-1])
end
### ask
def ask(prompt, meth = :to_s)
print prompt
STDOUT.flush
STDIN.gets.chomp.send(meth)
end
### quit
def quit
puts
exit
end
### version
def version
puts "\n " + RuneBlog::VERSION
end
### new_blog!
def new_blog!
unless File.exist?(".blog")
yn = ask(red(" No .blog found. Create new blog? "))
if yn.upcase == "Y"
#-- what if data already exists?
system("cp -r #{RuneBlog::DefaultData} .")
File.open(".blog", "w") do |f|
f.puts "data"
f.puts "no_default"
end
File.open("data/VERSION", "a") {|f| f.puts "\nBlog created: " + Time.now.to_s }
end
end
end
### make_slug
def make_slug(title, seq=nil)
num = '%04d' % (seq || @config.next_sequence) # FIXME can do better
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
"#{num}-#{slug}"
end
### read_config
def read_config
# Crude - FIXME later
cfg_file = ".blog"
new_blog! unless File.exist?(cfg_file)
@config = RuneBlog::Config.new(".blog")
@view = @config.view # current view
@sequence = @config.sequence
@root = @config.root
end
### create_empty_post
def create_empty_post
@template = <<-EOS
.mixin liveblog
.liveblog_version
.title #@title
.pubdate #@date
.views #@view
Teaser goes here.
.readmore
Remainder of post goes here.
EOS
@slug = make_slug(@title)
@fname = @slug + ".lt3"
File.open("#@root/src/#@fname", "w") {|f| f.puts @template }
@fname
end
### edit_post
def edit_post(file)
system("vi #@root/src/#{file} +8 ")
end
### open_remote
def open_remote
@deploy ||= {}
unless @deploy[@view]
puts red("\n Deploy first.")
return
end
lines = @deploy[@view]
user, server, sroot, spath = *lines
system("open 'http://#{server}/#{spath}'")
end
### open_local
def open_local
system("open #{@config.viewdir(@view)}/index.html")
end
def deploy
# TBD clunky FIXME
@deploy ||= {}
deployment = @config.viewdir(@view) + "deploy"
lines = File.readlines(deployment).map {|x| x.chomp }
@deploy[@view] = lines
user, server, sroot, spath = *lines
vdir = @config.viewdir(@view)
files = ["#{vdir}/index.html"]
files += Dir.entries(vdir).grep(/^\d\d\d\d/).map {|x| "#{vdir}/#{x}" }
files.reject! {|f| File.mtime(f) < File.mtime("#{vdir}/last_deployed") }
puts "\n Files:"
files.each {|f| puts " " + f }
puts
dir = "#{sroot}/#{spath}"
cmd = "scp -r #{files.join(' ')} root@#{server}:#{dir} >/dev/null 2>&1"
print red("\n Deploying #{files.size} files... ")
# puts cmd
system(cmd)
File.write("#{vdir}/last_deployed", files)
puts red("finished.")
end
### process_post
def process_post(file)
@main ||= Livetext.new
@main.main.output = File.new("/tmp/WHOA","w")
# puts " Processing: #{Dir.pwd} :: #{file}"
path = @root + "/src/#{file}"
@meta = @main.process_file(path)
@meta.slug = make_slug(@meta.title, @config.sequence)
@meta.slug = file.sub(/.lt3$/, "")
@meta
end
### reload_post
def reload_post(file)
@main ||= Livetext.new
@main.main.output = File.new("/tmp/WHOA","w")
@meta = process_post(file)
@meta.slug = file.sub(/.lt3$/, "")
@meta
end
### posting
def posting(view, meta)
ref = "#{view}/#{meta.slug}/index.html"
<<-HTML
#{meta.pubdate}
#{meta.title}
#{meta.teaser}
Read more...