Module: Petrify
- Defined in:
- lib/petrify.rb,
lib/petrify/version.rb
Constant Summary collapse
- VERSION =
"0.4.4"
- @@output_dir =
'_site'
- @@working_dir =
File.join(Dir.pwd, @@output_dir)
- @@views_dir =
'views'
- @@layout_fn =
File.join(@@views_dir, 'layout.haml')
- @@log =
Logger.new($stdout)
Class Method Summary collapse
- .create_path(path_items) ⇒ Object
-
.csv(path_items, filename, data) ⇒ Object
Write a CSV file from an array of hashes (or an empty array).
-
.file(path_items, filename, data) ⇒ Object
Write an arbitrary file to output (eg a JSON file).
-
.page(path_items, template, locals = {}) ⇒ Object
Write an HTML page using a specified template and optional data.
- .setup ⇒ Object
Class Method Details
.create_path(path_items) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/petrify.rb', line 69 def self.create_path(path_items) dir = File.join(@@output_dir, path_items) FileUtils.mkdir_p(dir) @@log.debug dir dir end |
.csv(path_items, filename, data) ⇒ Object
Write a CSV file from an array of hashes (or an empty array)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/petrify.rb', line 33 def self.csv(path_items, filename, data) dir = create_path(path_items) fn = File.join(dir, filename + '.csv') if data.size > 0 csv_string = CSV.generate do |csv| csv << data.first.keys # header row data.each { |row| csv << row.values } end else # There's no data so write an empty file (no header row either) csv_string = '' end File.write(fn, csv_string) @@log.info fn end |
.file(path_items, filename, data) ⇒ Object
Write an arbitrary file to output (eg a JSON file)
52 53 54 55 56 57 |
# File 'lib/petrify.rb', line 52 def self.file(path_items, filename, data) dir = create_path(path_items) fn = File.join(dir, filename) File.write(fn, data) @@log.info fn end |
.page(path_items, template, locals = {}) ⇒ Object
Write an HTML page using a specified template and optional data
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/petrify.rb', line 17 def self.page(path_items, template, locals = {}) dir = create_path(path_items) fn = File.join(dir, 'index.html') # https://stackoverflow.com/questions/6125265/using-layouts-in-haml-files-independently-of-rails html = Haml::Engine.new(File.read(@@layout_fn)).render(Object.new, locals) do Haml::Engine.new(File.read(File.join(@@views_dir, "#{template}.haml"))).render(Object.new, locals) end File.write(fn, html) @@log.info fn # TODO - add page to sitemap.xml or sitemap.txt # https://support.google.com/webmasters/answer/183668?hl=en&ref_topic=4581190 end |
.setup ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/petrify.rb', line 59 def self.setup # Recursively delete working directory to ensure no redundant files are left behind from previous builds. # But preserve dot-files (especially .git directory) FileUtils.rm_r(Dir.glob(File.join(@@working_dir, '*'))) Dir.mkdir(@@working_dir) unless File.directory?(@@working_dir) # Copy `public` dir to output dir FileUtils.copy_entry('public', @@working_dir) end |