Module: YARD::Amp::ParsingHelpers

Included in:
LegacyAmpCommandHandler, LegacyCommandHandler, ModernAmpCommandHandler, ModernCommandHandler
Defined in:
lib/yard-amp/parsing_helpers.rb

Instance Method Summary collapse

Instance Method Details

#clean_string(str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yard-amp/parsing_helpers.rb', line 3

def clean_string(str)
  if str[0,2] == ":\"" && str[-1,1] == "\""
    str[2..-2]
  elsif str[0,1] == ":"
    str[1..-1]
  elsif str[0,1] =~ /['"]/ && str[-1,1] == str[0,1]
    str[1..-2]
  elsif str[0,3] =~ /%q\{/i && str[-1,1] == "}"
    str[3..-2]
  elsif str[0,3] == "<<-"
    prefix = str[3..-1] =~ /[\S]+/
    if prefix
      prefix = $&
      # check if it starts and ends with it
      if str[-1 * prefix.size..-1] == str[3...prefix.size+3]
        return str[3 + prefix.size..(-1 * prefix.size - 2)]
      end
    end
    str
  else
    str
  end
end

#construct_docstring(klass) ⇒ Object



27
28
29
# File 'lib/yard-amp/parsing_helpers.rb', line 27

def construct_docstring(klass)
  klass.docstring = "== #{klass[:amp_data][:desc]}\n\n#{klass[:amp_data][:help]}\n#{klass[:amp_data][:docstring]}"
end

#parse_hash(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yard-amp/parsing_helpers.rb', line 31

def parse_hash(node)
  node.children.inject({}) do |hash, pair|
    unless pair.type == :assoc
      raise YARD::Parser::UndocumentableError, %Q{expected an :assoc in a hash but got #{pair.type}}
    end
    key = clean_string pair[0].source
    val = clean_string pair[1].source
    hash[key] = val
    hash
  end
end

#split_by_comma_smart(string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yard-amp/parsing_helpers.rb', line 43

def split_by_comma_smart(string)
  result = []
  in_quote = false
  last_spot = 0
  string.split(//).each_with_index do |char, idx|
    if char =~ /['"]/
      in_quote = !in_quote
    elsif char == "," && !in_quote
      result << string[last_spot..(idx - 1)]
      last_spot = idx + 1
    end
  end
  unless last_spot == string.size - 1
    result << string[last_spot..-1]
  end
  result
end