Class: PDoc::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pdoc/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
18
# File 'lib/pdoc/parser.rb', line 14

def initialize(string)
  @string = string
  @parser = DocumentationParser.new
  @percentage = 0
end

Instance Method Details

#completion_percentageObject



41
42
43
44
45
46
47
48
# File 'lib/pdoc/parser.rb', line 41

def completion_percentage
  if @parser.index
    ratio = @parser.index.to_f / @parser.input.size.to_f
    percentage = (ratio * 100).floor
    @percentage = percentage if percentage > @percentage
  end
  "#{@percentage}%"
end

#finished?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pdoc/parser.rb', line 50

def finished?
  @finished
end

#parseObject

Parses the preprocessed string. Returns an instance of Documentation::Doc

Raises:



22
23
24
25
26
27
28
# File 'lib/pdoc/parser.rb', line 22

def parse
  @finished = false
  result = @parser.parse(pre_process)
  @finished = true
  raise ParseError, @parser unless result
  result
end

#pre_processObject

Preprocess the string before parsing. Converts “rn” to “n” and avoids edge case by wrapping the string in line breaks.



33
34
35
36
37
38
39
# File 'lib/pdoc/parser.rb', line 33

def pre_process
  string = @string.gsub(/\r\n/, "\n")
  string = string.split("\n").map do |line|
    line.gsub(/\s+$/, '')
  end.join("\n")
  "\n#{string}\n"
end