lib/docify/document.rb in docify-1.0.5 vs lib/docify/document.rb in docify-1.0.6

- old
+ new

@@ -1,32 +1,59 @@ module Docify class Document + include Docify::Format + include Docify::Markup + attr_reader :path attr_reader :content + attr_reader :format # Initialize a new Document object with file path - def initialize(path) + # + # path - Input file path + # format - Markup (default: markdown) + # + 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 - # Render document by specified format - def render(format, embed_css=true) - raise ArgumentError, 'Invalid format!' unless FORMATS.include?(format) - result = Docify::Markup.render("doc.#{format}", File.read(@path)) - if embed_css - params = {:title => File.basename(@path), :content => result} - params[:css] = Docify::CSS if embed_css + # Render document content + # + # options - Render options + # + # options[:format] - Set render format (auto-detection) + # options[:html] - Render with html (default: true) + # options[:css] - Include CSS styles (default: true) + # + def render(options={}) + format = (options[:format] || detect_format(@path)).to_sym + use_html = options.key?(:html) ? options[:html] == true : true + use_css = options.key?(:css) ? options[: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) - else - @content = result end @content end - # Save rendered content into file + # Save rendered content into the file + # + # path - Output path + # def save_to(path) unless File.exists?(File.dirname(path)) raise ArgumentError, "Output path does not exist!" end if File.directory?(path) \ No newline at end of file