Class: Msgtrail::Site
- Inherits:
-
Object
- Object
- Msgtrail::Site
- Defined in:
- lib/msgtrail/site.rb
Instance Attribute Summary collapse
-
#blog_directory ⇒ Object
Returns the value of attribute blog_directory.
-
#config ⇒ Object
Returns the value of attribute config.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#theme_directory ⇒ Object
Returns the value of attribute theme_directory.
Instance Method Summary collapse
- #copy_files ⇒ Object
- #fetch_site_pages ⇒ Object
- #generate_articles(articles) ⇒ Object
-
#initialize(config) ⇒ Site
constructor
A new instance of Site.
- #write_article_directory_and_file(page, renderer, slug = nil) ⇒ Object
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_directory ⇒ Object
Returns the value of attribute blog_directory.
4 5 6 |
# File 'lib/msgtrail/site.rb', line 4 def blog_directory @blog_directory end |
#config ⇒ Object
Returns the value of attribute config.
4 5 6 |
# File 'lib/msgtrail/site.rb', line 4 def config @config end |
#pages ⇒ Object
Returns the value of attribute pages.
4 5 6 |
# File 'lib/msgtrail/site.rb', line 4 def pages @pages end |
#theme_directory ⇒ Object
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_files ⇒ Object
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_pages ⇒ Object
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 |