Sha256: 131e5d968590d0216b6b631d8adc91e9f0c2fa60ea2ea5d1016c103104733ea5

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

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
    #
    # 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 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)
      end
      @content
    end
    
    # 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)
        raise ArgumentError, "Output path should be a file!"
      end
      File.open(path, 'w') { |f| f.write(@content) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
docify-1.0.7 lib/docify/document.rb
docify-1.0.6 lib/docify/document.rb