Class: Msgtrail::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/msgtrail/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



6
7
8
9
10
# File 'lib/msgtrail/site.rb', line 6

def initialize(config)
  self.config = config
  self.blog_directory = File.join(config.working_directory, config.settings.file_matter.blog_directory)
  self.theme_directory = File.join(config.working_directory, config.settings.file_matter.theme_directory)
end

Instance Attribute Details

#blog_directoryObject

Returns the value of attribute blog_directory.



4
5
6
# File 'lib/msgtrail/site.rb', line 4

def blog_directory
  @blog_directory
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/msgtrail/site.rb', line 4

def config
  @config
end

#pagesObject

Returns the value of attribute pages.



4
5
6
# File 'lib/msgtrail/site.rb', line 4

def pages
  @pages
end

#theme_directoryObject

Returns the value of attribute theme_directory.



4
5
6
# File 'lib/msgtrail/site.rb', line 4

def theme_directory
  @theme_directory
end

Instance Method Details

#copy_filesObject



66
67
68
69
70
71
72
# File 'lib/msgtrail/site.rb', line 66

def copy_files
  fp = File.join(self.theme_directory, 'files/*')
  Dir.glob(fp).each do|f|
    FileUtils.cp(f, self.blog_directory)
    puts "Copied '#{fp}' to '#{self.blog_directory}'"
  end
end

#fetch_site_pagesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/msgtrail/site.rb', line 12

def fetch_site_pages
  filepath = File.join(self.config.working_directory, self.config.settings.file_matter.site_manifest_file)
  begin
    manifest = File.read(filepath)
  rescue
    puts("Can't find site manifest '#{filepath}' (#{$!})")
    exit(2)
  end
  begin
    self.pages = MultiJson.load(manifest, symbolize_keys: true)
  rescue
    puts("Invalid JSON in '#{filepath}' (#{$!})")
    exit(2)
  end
end

#generate_articles(articles) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/msgtrail/site.rb', line 28

def generate_articles(articles)
  self.pages.each do |page|
    layout_filepath = File.join(self.theme_directory, page[:layout])
    template_filepath = File.join(self.theme_directory, page[:template])
    if page[:iterator_subject] == false
      renderer = PageRenderer.new(layout_filepath, template_filepath, self.config)
      renderer.articles = articles
      write_article_directory_and_file(page, renderer)
    else
      articles.each do |article|
        renderer = PageRenderer.new(layout_filepath, template_filepath, self.config)
        renderer.article = article
        write_article_directory_and_file(page, renderer, article[:slug])
      end
    end
  end
end

#write_article_directory_and_file(page, renderer, slug = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/msgtrail/site.rb', line 46

def write_article_directory_and_file(page, renderer, slug = nil)
  # Create directory
  article_directory = File.join(self.blog_directory, (page[:output_path] || "") % slug)
  begin
    FileUtils.mkdir_p(article_directory)
  rescue
    puts("Can't create '#{article_directory}' (#{$!})")
    exit(2)
  end
  # Create file
  index_filepath = File.join(self.blog_directory, (page[:output_path] || "") % slug, page[:output_file])
  begin
    File.write(index_filepath, renderer.render)
  rescue
    puts("Can't write '#{index_filepath}' (#{$!})")
    exit(2)
  end
  puts("Created '#{index_filepath}'")
end