Class: DoubleDoc::DocExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/double_doc/doc_extractor.rb

Constant Summary collapse

TYPES =
{
'rb' => /\s*##\s?(.*)$/
}.freeze

Class Method Summary collapse

Class Method Details

.extract(source, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/double_doc/doc_extractor.rb', line 7

def self.extract(source, options = {})
  case source
  when String
    extract_from_lines(source.split("\n"), options)
  when File
    if type = File.extname(source.path)
      type = type[1..-1]
    end
    type ||= 'rb'

    extract_from_lines(source.readlines, options.merge(:type => type))
  when Array
    extract_from_lines(source, options)
  else
    raise "can't extract docs from #{source}"
  end
end

.extract_from_lines(lines, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/double_doc/doc_extractor.rb', line 25

def self.extract_from_lines(lines, options)
  doc = []
  extractor = TYPES[options[:type]]

  add_empty_line = false
  lines.each do |line|
    if match = line.match(extractor)
      if add_empty_line
        doc << ''
        add_empty_line = false
      end
      doc << match[1].rstrip
    else
      add_empty_line = !doc.empty?
    end
  end

  return '' if doc.empty? || doc.all?(&:empty?)

  doc << ''

  return doc.join("\n")
end