class Blogdoor::Builder

Attributes

ignore_patterns[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/blogdoor/builder.rb, line 9
def initialize(args = {})
  @root_path = Pathname.new(args[:root_path] || ".")
  @builds_path = @root_path.join("builds")

  layout_path = Pathname.new(args[:layout_path] || "./layout.erb")
  @layout = ERB.new(layout_path.read)
  @converter = Converter.new
  @client = Client.new

  @ignore_patterns = (args[:ignore_patterns] || []).map { |pattern| /#{pattern}/ }
end

Public Instance Methods

build(post_path) click to toggle source
# File lib/blogdoor/builder.rb, line 28
def build(post_path)
  return if @ignore_patterns.any? { |pattern| post_path.to_s =~ pattern }

  filename = post_path.basename(".md")
  html_path = @builds_path.join("#{filename}.html")

  content = @converter.convert(post_path.read)
  context = Context.new({ title: filename, created_at: post_path.mtime, content: content })
  html = @layout.result(context.to_binding)
  html = insert_script_tags(html)
  html_path.open("wb") { |file| file.write(html) }
  @client.notify
end
build_all() click to toggle source
# File lib/blogdoor/builder.rb, line 21
def build_all
  @builds_path.mkdir unless @builds_path.exist?
  Pathname.glob("#{@root_path.to_s}/**/*.md") do |post_path|
    build(post_path)
  end
end
insert_script_tags(html) click to toggle source
# File lib/blogdoor/builder.rb, line 42
def insert_script_tags(html)
  javascripts_path = Pathname.new("../javascripts").expand_path(File.dirname(__FILE__))
  script_tags = []
  script_tags << %(<script src="#{javascripts_path.join("livereload.js")}"></script>)
  html.gsub(/<\/head>/) { "#{script_tags.join("\n")}\n</head>"}
end