Sha256: ca879be11d70466ed53f438df9855d755ec0e1eebd4921b61186129dba8d43fe

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'helpers-blog'
require 'runeblog'

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(meta, view_name)
    # FIXME weird logic here
    raise "RuneBlog.blog is not set!" if RuneBlog.blog.nil?
    @blog = RuneBlog.blog
    @title = meta.title
    @view = @blog.str2view(view_name)
    @num, @slug = make_slug
    @date = Time.now.strftime("%Y-%m-%d")
    template = RuneBlog::Default::PostTemplate
    @meta = meta
    html = interpolate(template)
    @draft = "#{@blog.root}/src/#@slug.lt3"
    File.write(@draft, html)
  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)
    File.write("teaser.txt", @meta.teaser)
    File.write("remainder.txt", @meta.remainder)
    template = RuneBlog::Default::TeaserTemplate
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
runeblog-0.0.82 lib/post.rb