Class: PrettyDoc::Resource::Source

Inherits:
PrettyDoc::Resource show all
Defined in:
lib/pretty_doc/resources/source.rb

Overview

Source Resource

Instance Attribute Summary collapse

Attributes inherited from PrettyDoc::Resource

#file

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, converter, options) ⇒ Source

Returns a new instance of Source.



9
10
11
12
13
14
15
16
# File 'lib/pretty_doc/resources/source.rb', line 9

def initialize(file, converter, options)
  self.file = file
  extname = File.extname(file)
  self.basename = File.basename(file, extname)
  converter.content = File.read(self.file)
  converter.options = options
  self.content = converter.as_html
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



7
8
9
# File 'lib/pretty_doc/resources/source.rb', line 7

def basename
  @basename
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/pretty_doc/resources/source.rb', line 7

def content
  @content
end

Class Method Details

.build(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pretty_doc/resources/source.rb', line 27

def self.build(options)
  puts 'Converting files ...'
  output_dir = options.output
  options.files.each do |file|
    extname = File.extname(file)
    converter_class = PrettyDoc::Converter.descendants.find do |klass|
      klass.perfer_exts.include?(extname)
    end
    if converter_class
      converter = converter_class.new
      result = new(file, converter, options)
      result.write(output_dir, options)
    else
      puts "No corresponding converter found for file `#{file}`"
    end
  end
end

Instance Method Details

#write(target_dir, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/pretty_doc/resources/source.rb', line 18

def write(target_dir, options = {})
  template = options[:template]
  result = template.render(content: content)
  target = File.join(target_dir, "#{basename}.html")
  File.open(target, 'w') { |f| f << result }
  template.write_assets(target_dir)
  puts "Create File: #{target}"
end