Class: Docify::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/docify/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Document

Initialize a new Document object with file path

Raises:

  • (ArgumentError)


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

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/docify/document.rb', line 4

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/docify/document.rb', line 3

def path
  @path
end

Instance Method Details

#render(format, embed_css = true) ⇒ Object

Render document by specified format

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docify/document.rb', line 15

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_to(path) ⇒ Object

Save rendered content into file



29
30
31
32
33
34
35
36
37
# File 'lib/docify/document.rb', line 29

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