Module: Woody::Compiler

Included in:
Woody
Defined in:
lib/woody/compiler.rb

Overview

Handles functions related to compiling the Woody site

Instance Method Summary collapse

Instance Method Details

#compile(options = nil) ⇒ Object

Compiles the Woody site



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/woody/compiler.rb', line 8

def compile(options = nil)
  puts "Compiling..."
  meta = YAML.load_file(dir("content/metadata.yml"))
		
# instantiate the metadata hash so shit doesn't explode in our faces
  meta = Hash.new if meta == false

  posts      = Array.new
  filesfound = Array.new
  Dir.glob(dir('content/*.mp3')) do |file|
    filename = undir(file)[8..-1]
    unless meta == false or meta[filename].nil?
      # Episode metadata already stored
      posts      << Episode.new_from_meta(self, filename, meta[filename])
      filesfound << filename
    else
      # No episode metadata stored for this yet
      unless options.nil? or options.no_add == false # Seemingly backwards, I know...
        (meta, posts, filesfound, filename)
      else
        puts "Warning: found media file #{filename} but no metadata. Will not be published."
      end
    end
  end

  # Process blog posts
  Dir.glob(dir('content/posts/*')) do |file|
    filename = undir(file)[8..-1]
    data = Preamble.load(file)
    m = data[0]
    body = data[1]

    posts << Post.new(self, filename, m['title'], m['subtitle'], body, Date.parse(m['date'].to_s), m['tags'])
  end

  posts.sort_by! do |post|
    post.date
  end.reverse!

  # Check for files in meta but not found
  unless meta.empty?
    meta.each do |file|
      next if filesfound.include? file[0]
      puts "Warning: found metadata for file #{file[0]}, but file itself is missing. Will not be published."
    end
  end

  # Make sure necessary directories exist
  FileUtils.mkdir_p(dir 'output/assets') unless File.directory?(dir 'output/assets')
  FileUtils.mkdir_p(dir 'output/assets/mp3') unless File.directory?(dir 'output/assets/mp3')
  FileUtils.mkdir_p(dir 'output/post') unless File.directory?(dir 'output/post')

  # Copy over (TODO: and process) MP3 files
  posts.each do |post|
    next unless post.has_file?
    copy_file_to_output dir(File.join("content", post.filename)), File.join("assets/mp3", post.compiledname)
  end

  # Copy over assets
  copy_assets

  # Update index.html
  layout = File.read(dir 'templates/layout.html')
  layout = Erubis::Eruby.new(layout)

  index_compiled = layout.result(config: @config) do
    index = Erubis::Eruby.new(File.read(dir 'templates/index.html'))
    index.result(config: @config, posts: posts) do |post|
      ep = Erubis::Eruby.new(File.read(dir 'templates/post.html'))
      ep.result(config: @config, posts: posts, post: post)
    end
  end
  write_output_file('index.html') {|f| f.write(index_compiled) }

  # Update post pages
  posts.each do |post|
    layout = File.read(dir 'templates/layout.html')
    layout = Erubis::Eruby.new(layout)
    post_compiled = layout.result(config: @config) do
      ep = Erubis::Eruby.new(File.read(dir 'templates/post.html'))
      ep.result(config: @config, posts: posts, post: post)
    end
    write_output_file(post.path!) {|f| f.write(post_compiled) }
  end

  # Copy over iTunes.png
  begin
    copy_file_to_output "content/iTunes.png", "assets/iTunes.png"
  rescue Errno::ENOENT
    puts "Warning: content/iTunes.png missing!"
  end

  # Update (iTunes) RSS
  @config['itunes']['explicit'] = "no" if @config['itunes']['explicit'].nil?
  feedxml = File.read File.join($source_root, "feed.xml") # Use feed.xml template in gem, not in site's template folder.
  feed = Erubis::Eruby.new(feedxml)
  feed_compiled = feed.result(config: @config, posts: posts)
  write_output_file("feed.xml") {|f| f.write(feed_compiled) }


  # Purge left over files
  purge_output

  # Remove all empty directories from the output
  Dir[dir 'output/**/*'].select { |d| File.directory? d }.select { |d| (Dir.entries(d) - %w[ . .. ]).empty? }.each { |d| Dir.rmdir d }
end