Class: Docify::Document

Inherits:
Object
  • Object
show all
Includes:
Format, Markup
Defined in:
lib/docify/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Markup

#markdown, #rdoc, #textile

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)

Raises:

  • (ArgumentError)


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

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/docify/document.rb', line 7

def content
  @content
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/docify/document.rb', line 8

def format
  @format
end

#pathObject (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

Render document content

options - Render options

options - Set render format (auto-detection) options - Render with html (default: true) options - Include CSS styles (default: true)



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(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_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