class JekyllPost

Public Class Methods

new(title, body, atts, repo, imgdir, pstdir, meta) click to toggle source
# File lib/jekyllpost.rb, line 5
def initialize(title, body, atts, repo, imgdir, pstdir, meta)
  time = Time.now
  postday = time.strftime("%Y-%m-%d")
  posttime = time.strftime("%H:%M:%S")
  daydir = time.strftime("%Y/%m/%d")
  imgdir = "#{imgdir}/#{daydir}"
  path = make_slug(title,postday)
  post_filename = "#{repo}/#{pstdir}/#{path}"
  
  content = replace_images(body, atts, imgdir)
  create_imgdir(repo,imgdir)
  save_attachments(repo, imgdir, atts)
  create_post(post_filename, meta, title, postday, posttime, atts, imgdir, content)
end

Public Instance Methods

create_imgdir(repo,imgdir) click to toggle source
# File lib/jekyllpost.rb, line 71
def create_imgdir(repo,imgdir)
  
  fullpath = "#{repo}/#{imgdir}"
  
  unless Dir.exists?(fullpath)
    FileUtils.mkdir_p(fullpath)
  end
end
create_post(post_filename, meta, title, postday, posttime, atts, imgdir, content) click to toggle source
# File lib/jekyllpost.rb, line 20
def create_post(post_filename, meta, title, postday, posttime, atts, imgdir, content)
  open(post_filename, 'w') do |post|
    post << "---\n"
    post << "layout: #{meta[:layout]}\n"
    post << "title: #{title}\n"
    post << "date: #{postday} #{posttime}\n"
    post << "categories: #{meta[:categories]}\n"
    unless atts.empty?
      key,val = atts[:image0][:filename]
      post << "image: /#{imgdir}/#{key}\n"
    end
    post << "---\n"
    post << "#{content}"
  end
end
make_slug(title,time) click to toggle source
# File lib/jekyllpost.rb, line 64
def make_slug(title,time)
  title = title.downcase.gsub(/\W+/, ' ')
  title = title.split(" ").join("-")
  title += ".md"
  path = "#{time}-#{title}"
end
replace_images(post, atts, imgdir) click to toggle source
# File lib/jekyllpost.rb, line 50
def replace_images(post, atts, imgdir)
  atts.each do |img,vals|
    srchimg = "![](#{vals[:filename]})"
    unless post.nil?
      post = post.gsub(srchimg,"![](/#{imgdir}/#{vals[:filename]})")
      # if vals[:cid] == ""
      #   newimg = "![](/#{imgdir}/#{vals[:filename]})\n\n"
      #   post << newimg
      # end
    end
  end
  post
end
save_attachments(repo, imgdir, atts) click to toggle source
# File lib/jekyllpost.rb, line 36
def save_attachments(repo, imgdir, atts)
  atts.each do |img,att|
    if (att[:type].start_with?('image/'))
      # extracting images for example...
      filename = att[:filename]
      begin
        File.open("#{repo}/#{imgdir}/#{filename}", "w+b", 0644) {|f| f.write att[:content]}
      rescue => e
        puts "Unable to save data for #{filename} because #{e.message}"
      end
    end
  end
end