Skip to content

Instantly share code, notes, and snippets.

@mcfiredrill
Created October 12, 2012 19:15
Show Gist options
  • Save mcfiredrill/3880932 to your computer and use it in GitHub Desktop.
Save mcfiredrill/3880932 to your computer and use it in GitHub Desktop.
my crappy blog engine - being replaced by tumblr i guess ¯\_(ツ)_/¯
# encoding: utf-8
require 'yaml'
require 'find'
require 'mini_magick'
# slurp up .erb files in blog/
# parse the yaml frontmatter
# generate views in views/blog/
# populate the articles array
# article
# - url
# - title
# - date
# - tags[]
#articles = []
class BlogArticle
SUMMARY_SEPARATOR="READMORE"
MAX_IMG_WIDTH=260
attr_reader :date, :title, :tags, :page, :url, :img, :thumb
def initialize(file)
f = File.read(file)
f.force_encoding("UTF-8")
#puts "the encoding: #{f.encoding}"
#puts "is this invalid?!#{f}"
#puts "is this invalid?!#{f.split("---\n")[1]}"
y = YAML.load(f.split("---\n")[1])
#puts y
@page = f.split("---\n")[2]
# parse yaml
@title = y["title"]
@date = Date.parse y["date"]
@page = f.split("---\n")[2]
@tags = []
y["tags"].to_s.delete(' ').split(',').each do |t|
@tags << t
end
@img = y["img"]
# make thumbnail
if @img
#puts "@img: #{@img}"
image = MiniMagick::Image.open("./public/#{@img}")
if image[:width] > MAX_IMG_WIDTH
thumb_filename = "/img/thumb/#{File.basename(@img, File.extname(@img))}_thumb#{File.extname(@img)}"
#puts thumb_filename
if !File.exists?("./public/#{thumb_filename}")
#TODO thumbnail img
percent = ((MAX_IMG_WIDTH.to_f / image[:width].to_f) * 100).to_i
#puts "percent: #{percent}"
image.resize "#{percent}%"
image.write "./public/#{thumb_filename}"
end
@thumb = thumb_filename
else
@thumb = @img
end
end
@url = "blog/#{@date.year}/#{File.basename(file, ".*")}"
end
def body
@page.gsub(/#{SUMMARY_SEPARATOR}/, '')
end
def summary
return @page.split(SUMMARY_SEPARATOR).first
end
def parse_yaml_frontmatter(frontmatter)
yaml = YAML.load(frontmatter)
@title = yaml["title"]
@date = Date.parse yaml["date"]
@tags = []
yaml["tags"].to_s.delete(' ').split(',').each do |t|
@tags << t
end
@img = yaml["img"]
end
def make_thumbnail
if @img
#puts "@img: #{@img}"
image = MiniMagick::Image.open("./public/#{@img}")
if image[:width] > MAX_IMG_WIDTH
thumb_filename = "/img/thumb/#{File.basename(@img, File.extname(@img))}_thumb#{File.extname(@img)}"
#puts thumb_filename
if !File.exists?("./public/#{thumb_filename}")
#TODO thumbnail img
percent = ((MAX_IMG_WIDTH.to_f / image[:width].to_f) * 100).to_i
#puts "percent: #{percent}"
image.resize "#{percent}%"
image.write "./public/#{thumb_filename}"
end
@thumb = thumb_filename
else
@thumb = @img
end
end
end
end
def slurp_files(dir)
articles = []
#puts "dir: #{dir}"
Find.find(dir) do |f|
if f =~ /.*\.erb$/
articles << BlogArticle.new(f)
end
end
return articles
end
def find_article(url, articles)
articles.each do |a|
if a.url == url
return a
end
end
return nil
end
class Blog
attr_accessor :articles
def initialize(dir)
@articles = []
#puts "dir: #{dir}"
Find.find(dir) do |f|
if f =~ /.*\.erb$/
@articles << BlogArticle.new(f)
end
end
end
def find_article(url)
@articles.each do |a|
if a.url == url
return a
end
end
return nil
end
end
#articles = slurp_files("#{File.dirname(__FILE__)}/views/blog/")
#puts articles
#puts "ok"
require './blog'
describe BlogArticle do
before do
@blog_article = BlogArticle.new("./spec/blogs/firedrill_xbit1set.erb")
end
it "parses the yaml frontmatter" do
frontmatter =<<EOS
---
title: Firedrill's set from X-Bit 1
date: 2012/04/23
img: ./spec/firedrillxbit.jpg
tags: liveset
---
EOS
@blog_article.parse_yaml_frontmatter(frontmatter)
@blog_article.title.should == "Firedrill's set from X-Bit 1"
@blog_article.date.should == Date.parse("2012/04/23")
puts @blog_article.img
File.exists?(@blog_article.img).should be_true
@blog_article.tags.should == ["liveset"]
end
it "creates a thumbnail from an image" do
@blog_article.make_thumbnail
end
it "returns the body of the article" do
@blog_article.body
end
it "returns the summary of the article" do
@blog_article.summary
end
it "returns the url of the article" do
@blog_article.url
end
end
describe Blog do
before do
@blog = Blog.new("./spec/blogs")
end
it "parses the blog files into an array of BlogArticles" do
@blog.articles
end
it "finds an BlogArticle by url" do
@blog.find_article(@blog.articles.first.url)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment