Class: Blogdoor::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/blogdoor/builder.rb

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#ignore_patternsObject

Returns the value of attribute ignore_patterns.



7
8
9
# File 'lib/blogdoor/builder.rb', line 7

def ignore_patterns
  @ignore_patterns
end

Instance Method Details

#build(post_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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_allObject



21
22
23
24
25
26
# 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) ⇒ Object



42
43
44
45
46
47
# 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