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.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, format = :markdown) ⇒ Document
constructor
Initialize a new Document object with file path.
-
#render(options = {}) ⇒ Object
Render document content.
-
#save_to(path) ⇒ Object
Save rendered content into the file.
Methods included from Markup
Methods included from Format
#detect_format, #valid_format?
Constructor Details
#initialize(path, format = :markdown) ⇒ Document
Initialize a new Document object with file path
path - Input file path format - Markup (default: markdown)
15 16 17 18 19 20 21 |
# File 'lib/docify/document.rb', line 15 def initialize(path, format=:markdown) raise ArgumentError, "File [#{path}] does not exist!" unless File.exists?(path) raise ArgumentError, "File required!" unless File.file?(path) @path = path @format = detect_format(path) @content = "" end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
7 8 9 |
# File 'lib/docify/document.rb', line 7 def content @content end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
8 9 10 |
# File 'lib/docify/document.rb', line 8 def format @format end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/docify/document.rb', line 6 def path @path end |
Instance Method Details
#render(options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/docify/document.rb', line 31 def render(={}) format = ([:format] || detect_format(@path)).to_sym use_html = .key?(:html) ? [:html] == true : true use_css = .key?(:css) ? [:css] == true : true unless valid_format?(format) raise ArgumentError, "Invalid format: #{format}" end @content = Docify::Markup.send(format, File.read(@path)) if use_html == true params = { :title => File.basename(@path), :content => @content, } params[:css] = Docify::CSS if use_css == true @content = Docify::Template.new(Docify::TEMPLATE).render(params) end @content end |
#save_to(path) ⇒ Object
Save rendered content into the file
path - Output path
56 57 58 59 60 61 62 63 64 |
# File 'lib/docify/document.rb', line 56 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 |