Class: Docify::Document
- Inherits:
-
Object
- Object
- Docify::Document
- Defined in:
- lib/docify/document.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path) ⇒ Document
constructor
Initialize a new Document object with file path.
-
#render(format, embed_css = true) ⇒ Object
Render document by specified format.
-
#save_to(path) ⇒ Object
Save rendered content into file.
Constructor Details
#initialize(path) ⇒ Document
Initialize a new Document object with file path
7 8 9 10 11 12 |
# File 'lib/docify/document.rb', line 7 def initialize(path) raise ArgumentError, "File [#{path}] does not exist!" unless File.exists?(path) raise ArgumentError, "File required!" unless File.file?(path) @path = path @content = "" end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/docify/document.rb', line 4 def content @content end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/docify/document.rb', line 3 def path @path end |
Instance Method Details
#render(format, embed_css = true) ⇒ Object
Render document by specified format
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/docify/document.rb', line 15 def render(format, =true) raise ArgumentError, 'Invalid format!' unless FORMATS.include?(format) result = Docify::Markup.render("doc.#{format}", File.read(@path)) if params = {:title => File.basename(@path), :content => result} params[:css] = Docify::CSS if @content = Docify::Template.new(Docify::TEMPLATE).render(params) else @content = result end @content end |
#save_to(path) ⇒ Object
Save rendered content into file
29 30 31 32 33 34 35 36 37 |
# File 'lib/docify/document.rb', line 29 def save_to(path) unless File.exists?(File.dirname(path)) raise ArgumentError, "Output path does not exist!" end if File.directory?(path) raise ArgumentError, "Output path should be a file!" end File.open(path, 'w') { |f| f.write(@content) } end |