lib/runeblog.rb in runeblog-0.0.81 vs lib/runeblog.rb in runeblog-0.0.82
- old
+ new
@@ -1,9 +1,12 @@
+require 'pp'
+
require 'find'
require 'yaml' # get rid of YAML later
require 'livetext'
require 'skeleton'
+require 'helpers-blog'
require 'default'
require 'view'
require 'deploy'
require 'post'
require 'version'
@@ -18,30 +21,29 @@
attr_reader :root, :views, :sequence
attr_accessor :view # overridden
def self.create_new_blog(dir = "data")
- File.write(".blog", "#{dir}\nno_default\n") # .blog lives above subtree
+ x = OpenStruct.new
+ x.root, x.current_view, x.editor = dir, "no_default", "vi"
+ write_config(x, ".blog")
create_dir(dir)
Dir.chdir(dir) do
create_dir("views")
create_dir("assets")
create_dir("src")
File.write("sequence", 0)
File.write("VERSION", "#{RuneBlog::VERSION}\nBlog created: " + Time.now.to_s )
end
end
- def get_config(file)
- lines = File.readlines(file).map(&:chomp)
- root, view_name = *lines
- end
-
def initialize(cfg_file = ".blog") # assumes existing blog
# Crude - FIXME later - What views are there? Deployment, etc.
self.class.blog = self # Weird. Like a singleton - dumbass circular dependency?
- @root, view_name = get_config(cfg_file)
+ obj = read_config(cfg_file)
+ vals = obj.to_h.values_at(:root, :current_view, :editor)
+ @root, view_name, @editor = *vals
@views = subdirs("#@root/views/").sort.map {|name| RuneBlog::View.new(name) }
@view = str2view(view_name)
@sequence = get_sequence
end
@@ -59,16 +61,17 @@
def view=(arg)
case arg
when RuneBlog::View
@view = arg
+ raise "Problem here?"
@view.read_config
when String
new_view = str2view(arg)
raise "Can't find view #{arg}" if new_view.nil?
@view = new_view
- @view.read_config
+ @view.deployer = read_config(@view.dir + "/deploy")
else
raise "#{arg.inspect} was not a View or a String"
end
end
@@ -121,11 +124,11 @@
def view_files
vdir = @blog.viewdir(@view)
# meh
files = ["#{vdir}/index.html"]
- files += Dir.entries(vdir).grep(/^\d\d\d\d/).map {|x| "#{vdir}/#{x}" }
+ files += Dir.entries(vdir).grep(/^\d{4}/).map {|x| "#{vdir}/#{x}" }
# Huh?
files.reject! {|f| File.mtime(f) < File.mtime("#{vdir}/last_deployed") }
end
def files_by_id(id)
@@ -146,11 +149,11 @@
rescue => err
puts err # error(err)
end
def edit_initial_post(file)
- result = system("vi #@root/src/#{file} +8 ")
+ result = system("#@editor #@root/src/#{file} +8")
raise "Problem editing #@root/src/#{file}" unless result
nil
rescue => err
error(err)
end
@@ -165,11 +168,13 @@
dir = "#@root/src"
drafts = Dir.entries(dir).grep(/^\d{4}.*/)
end
def change_view(view)
- File.write(".blog", "#@root\n#{view}\n")
+ x = OpenStruct.new
+ x.root, x.current_view, x.editor = @root, view, @editor # dumb - FIXME later
+ write_config(x, ".blog")
self.view = view # error checking?
end
def process_post(file)
path = @root + "/src/#{file}"
@@ -208,11 +213,11 @@
end
def generate_index(view)
# Gather all posts, create list
vdir = "#@root/views/#{view}"
- posts = Dir.entries(vdir).grep /^\d\d\d\d/
+ posts = Dir.entries(vdir).grep /^\d{4}/
posts = posts.sort.reverse
# Add view header/trailer
head = tail = nil
Dir.chdir(vdir) do
@@ -260,12 +265,15 @@
end
def remove_post(num)
list = files_by_id(num)
return nil if list.empty?
- result = system("rm -rf #{list.join(' ')}")
- error_cant_delete(files) unless result
+ dest = list.map {|f| f.sub(/(?<num>\d{4}-)/, "_\\k<num>") }
+ list.each.with_index do |src, i|
+ cmd = "mv #{src} #{dest[i]} 2>/dev/null"
+ system(cmd)
+ end
# FIXME - update index/etc
true
end
def delete_draft(num)
@@ -286,18 +294,19 @@
end
private
def subdirs(dir)
+raise Exception.new("hell") if dir == "/views/"
dirs = Dir.entries(dir) - %w[. ..]
dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
dirs
rescue => err
p err
end
def find_src_slugs
- files = Dir.entries("#@root/src/").grep /\d\d\d\d.*.lt3$/
+ files = Dir.entries("#@root/src/").grep /\d{4}.*.lt3$/
files.map! {|f| File.basename(f) }
files = files.sort.reverse
files
end