Class: Monograph::Export
- Inherits:
-
Object
- Object
- Monograph::Export
- Defined in:
- lib/monograph/export.rb
Defined Under Namespace
Classes: AlreadyExists
Instance Method Summary collapse
-
#assets ⇒ Object
Return a hash of all required assets for the book.
-
#html_files ⇒ Object
Return a hash of all HTML files needed for the book.
-
#initialize(book) ⇒ Export
constructor
A new instance of Export.
-
#save(path, force = false) ⇒ Object
Actually run the export to the local file system.
Constructor Details
#initialize(book) ⇒ Export
Returns a new instance of Export.
6 7 8 |
# File 'lib/monograph/export.rb', line 6 def initialize(book) @book = book end |
Instance Method Details
#assets ⇒ Object
Return a hash of all required assets for the book. This combines all the files from the built-in template with those provided within the book itself.
The copy within the book will always win if there’s a filename conflict.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/monograph/export.rb', line 39 def assets @assets ||= begin hash = Hash.new search_paths = [] search_paths << File.join(@book.builtin_template_path, 'assets', '**', '*') search_paths << File.join(@book.path, 'assets', '**', '*') search_paths.each do |search_path| Dir[search_path].inject(hash) do |hash, path| if File.file?(path) content = File.open(path, 'rb', &:read) case path.split('.').last when 'scss' path.gsub!(/.scss\z/, '.css') content = Sass::Engine.new(content, :syntax => :scss).render end hash[path.gsub(/\A.*\/assets\//, '')] = content end hash end end hash end end |
#html_files ⇒ Object
Return a hash of all HTML files needed for the book. The hash key is the filename and the value is the full HTML document.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/monograph/export.rb', line 12 def html_files @html ||= begin erb = ERB.new(@book.template) hash = {} # export the chapters @book.chapters.inject(hash) do |hash, chapter| hash["#{chapter.permalink}.html"] = erb.result(chapter.template_context.get_binding) hash end # export other ancillary pages ['index.html', 'contents.html'].each do |page| context = BookTemplateContext.new(@book, page) page_erb = ERB.new(erb.result(context.get_binding)) hash[page] = page_erb.result(context.get_binding) end hash end end |
#save(path, force = false) ⇒ Object
Actually run the export to the local file system. This method accepts a path and an optional boolean for forcing the creation.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/monograph/export.rb', line 65 def save(path, force = false) raise AlreadyExists, "Export path already exists at #{path}" if File.exist?(path) && !force FileUtils.rm_rf(path) if File.exist?(path) && force FileUtils.mkdir_p(path) html_files.each { |filename, content| File.open(File.join(path, filename), 'wb') { |f| f.write(content) }} assets.each do |filename, content| full_path = File.join(path, filename) dir = File.dirname(full_path) FileUtils.mkdir_p(dir) File.open(full_path, 'w') { |f| f.write(content) } end end |