Class: Yarrow::Web::Generator
- Inherits:
-
Object
- Object
- Yarrow::Web::Generator
- Defined in:
- lib/yarrow/web/generator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #generate(manifest) ⇒ Object
- #generate_sitemap(manifest) ⇒ Object
-
#initialize(config) ⇒ Generator
constructor
A new instance of Generator.
- #write_document(document) ⇒ Object
- #write_output_file(url, content) ⇒ Object
Constructor Details
#initialize(config) ⇒ Generator
Returns a new instance of Generator.
6 7 8 9 10 11 |
# File 'lib/yarrow/web/generator.rb', line 6 def initialize(config) @config = config # Config is here but we haven’t decided on schema for web publishing fields # so hard-code the needed values as part of this prototype @default_host = "http://example.com" end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/yarrow/web/generator.rb', line 4 def config @config end |
Instance Method Details
#generate(manifest) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/yarrow/web/generator.rb', line 13 def generate(manifest) Parallel.each(manifest.documents) do |document| write_document(document) end #generate_sitemap(manifest) end |
#generate_sitemap(manifest) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/yarrow/web/generator.rb', line 44 def generate_sitemap(manifest) # SitemapGenerator::Sitemap.default_host = @default_host # SitemapGenerator::Sitemap.public_path = @public_dir # SitemapGenerator::Sitemap.create do # manifest.documents.each do |document| # # Options: :changefreq, :lastmod, :priority, :expires # #add(document.url, lastmod: document.published_at) # add(document.url) # end # end end |
#write_document(document) ⇒ Object
20 21 22 23 |
# File 'lib/yarrow/web/generator.rb', line 20 def write_document(document) template = Template.for_document(document) write_output_file(document.url, template.render(document)) end |
#write_output_file(url, content) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/yarrow/web/generator.rb', line 25 def write_output_file(url, content) # If the target path is a directory, # generate a default index filename. if url[url.length-1] == '/' url = "#{url}index" end # Construct full path to file in output dir path = @config.output_dir.join(url.delete_prefix("/")).sub_ext('.html') # Create directory path if it doesn’t exist FileUtils.mkdir_p(path.dirname) # Write out the file File.open(path.to_s, 'w+:UTF-8') do |file| file.puts(content) end end |