lib/runeblog.rb in runeblog-0.0.64 vs lib/runeblog.rb in runeblog-0.0.65

- old
+ new

@@ -1,22 +1,39 @@ require 'find' require 'yaml' require 'livetext' +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.64" + VERSION = "0.0.65" Path = File.expand_path(File.join(File.dirname(__FILE__))) - DefaultData = Path + "/../data" + DefaultData = Path + "/../data/views/_default" - BlogHeaderPath = DefaultData + "/custom/blog_header.html" - BlogTrailerPath = DefaultData + "/custom/blog_trailer.html" - BlogTemplatePath = DefaultData + "/custom/blog_trailer.html" + BlogHeaderPath = DefaultData + "/custom/blog_header.html" + BlogTrailerPath = DefaultData + "/custom/blog_trailer.html" + PostTemplatePath = DefaultData + "/custom/post_template.html" - BlogHeader = File.read(BlogHeaderPath) rescue "not found" - BlogTrailer = File.read(BlogTrailerPath) rescue "not found" - BlogTemplate = File.read(BlogTemplatePath) rescue "not found" + BlogHeader = File.read(BlogHeaderPath) rescue "not found" + BlogTrailer = File.read(BlogTrailerPath) rescue "not found" + PostTemplate = File.read(PostTemplatePath) rescue "not found" attr_reader :root, :views, :sequence attr_accessor :view # FIXME def self.create_new_blog @@ -66,11 +83,11 @@ create_dir(dir + 'assets') File.open(dir + "deploy", "w") { } # FIXME File.write(dir + "custom/blog_header.html", RuneBlog::BlogHeader) File.write(dir + "custom/blog_trailer.html", RuneBlog::BlogTrailer) - File.write(dir + "custom/post_template.html", RuneBlog::BlogTemplate) + File.write(dir + "custom/post_template.html", RuneBlog::PostTemplate) File.write(dir + "last_deployed", "Initial creation") self.views << arg end def delete_view(name, force = false) @@ -101,32 +118,14 @@ result = files.grep(/#{tag}-/) result end def create_new_post(title, testing = false) - view = @view - date = Time.now.strftime("%Y-%m-%d") - @template = <<-EOS -.mixin liveblog - -.title #{title} -.pubdate #{date} -.views #{view} - -.teaser -Teaser goes here. -.end -Remainder of post goes here. -EOS - - num, @slug = make_slug(title) - @fname = @slug + ".lt3" - File.open("#@root/src/#@fname", "w") {|f| f.puts @template } - edit_initial_post(@fname) unless testing - process_post(@fname) #- FIXME handle each view - publish_post(@meta) - num + post = RuneBlog::Post.new(title, @view) + post.edit unless testing + post.publish + post.num rescue => err puts err # error(err) end def edit_initial_post(file) @@ -150,14 +149,13 @@ def change_view(view) @view = view # error checking? end def process_post(file) - @main ||= Livetext.new - @main.main.output = File.new("/tmp/WHOA","w") # FIXME srsly? path = @root + "/src/#{file}" - @meta = @main.process_file(path, binding) + livetext = Livetext.new(STDOUT) # (nil) + @meta = livetext.process_file(path, binding) raise "process_file returned nil" if @meta.nil? num, slug = self.make_slug(@meta.title, self.sequence) slug = file.sub(/.lt3$/, "") @meta.slug = slug @@ -213,20 +211,10 @@ def relink self.views.each {|view| generate_index(view) } end - def reload_post(file) - @main ||= Livetext.new - @main.main.output = File.new("/tmp/WHOA","w") # FIXME srsly? - @meta = process_post(file) - @meta.slug = file.sub(/.lt3$/, "") - @meta - rescue => err - error(err) - end - def posting(view, meta) # FIXME clean up and generalize ref = "#{view}/#{meta.slug}/index.html" <<-HTML <br> @@ -236,14 +224,16 @@ #{meta.teaser} <a href=../#{ref} style="text-decoration: none">Read more...</a> <br><br> <hr> HTML + text = File.read(self.viewdir(view) + "custom/post_template.html") + interpolate(text) end def rebuild_post(file) - reload_post(file) + @meta = process_post(file) publish_post(@meta) # FIXME ?? rescue => err error(err) end @@ -270,27 +260,10 @@ dirs = Dir.entries(dir) - %w[. ..] dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") } dirs end - def create_dir(dir) - cmd = "mkdir -p #{dir} >/dev/null 2>&1" - result = system(cmd) - raise "Can't create #{dir}" unless result - end - - def error(err) # FIXME - this is duplicated - str = "\n Error: #{err}" - puts str - puts err.backtrace # [0] - end - - def interpolate(str) - wrap = "<<-EOS\n#{str}\nEOS" - eval wrap - end - def find_src_slugs files = Dir.entries("#@root/src/").grep /\d\d\d\d.*.lt3$/ files.map! {|f| File.basename(f) } files = files.sort.reverse files @@ -300,27 +273,86 @@ ####### class RuneBlog::Post + class << self + attr_accessor :blog + end + 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) + raise "Post.blog is not set!" if RuneBlog::Post.blog.nil? + @blog = RuneBlog::Post.blog @title = title @view = view @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| # Create dir using slug (index.html, metadata?) + vdir = @blog.viewdir(view) + 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 ||= RuneBlog.next_sequence + postnum ||= @blog.next_sequence num = tag(postnum) # FIXME can do better slug = @title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') [postnum, "#{num}-#{slug}"] end