Class: Blogdoor::Builder
- Inherits:
-
Object
- Object
- Blogdoor::Builder
- Defined in:
- lib/blogdoor/builder.rb
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#ignore_patterns ⇒ Object
Returns the value of attribute ignore_patterns.
Instance Method Summary collapse
- #build(post_path) ⇒ Object
- #build_all ⇒ Object
-
#initialize(args = {}) ⇒ Builder
constructor
A new instance of Builder.
- #insert_script_tags(html) ⇒ Object
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_patterns ⇒ Object
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 = (html) html_path.open("wb") { |file| file.write(html) } @client.notify end |
#build_all ⇒ Object
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 (html) javascripts_path = Pathname.new("../javascripts").(File.dirname(__FILE__)) = [] << %(<script src="#{javascripts_path.join("livereload.js")}"></script>) html.gsub(/<\/head>/) { "#{script_tags.join("\n")}\n</head>"} end |