Sha256: 05375f70f6cfc4b2bbcbd5ed86592b31ed1f02c642a048528c5a9bfd8a12d797

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module Docify
  class Document
    attr_reader :path
    attr_reader :content
    
    # Initialize a new Document object with file path
    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
    
    # 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
        @content = Docify::Template.new(Docify::TEMPLATE).render(params)
      else
        @content = result
      end
      @content
    end
    
    # Save rendered content into file
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docify-1.0.5 lib/docify/document.rb
docify-1.0.4 lib/docify/document.rb
docify-1.0.3 lib/docify/document.rb