Module: YARD::Templates::Helpers::TextHelper

Defined in:
lib/yard/templates/helpers/text_helper.rb

Overview

Helper methods for text template formats.

Instance Method Summary collapse

Instance Method Details

#align_right(text, spacer = ' ', col = 72) ⇒ String

Returns aligns text to the right.

Returns:

  • (String)

    aligns text to the right



37
38
39
40
# File 'lib/yard/templates/helpers/text_helper.rb', line 37

def align_right(text, spacer = ' ', col = 72)
  text = text[0, col - 4] + '...' if (col - 1 - text.length) < 0
  spacer * (col - 1 - text.length) + " " + text
end

#h(text) ⇒ String

Returns escapes text.

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yard/templates/helpers/text_helper.rb', line 7

def h(text)
  out = ""
  text = text.split(/\n/)
  text.each_with_index do |line, i|
    out << 
    case line
    when /^\s*$/; "\n\n"
    when /^\s+\S/, /^=/; line + "\n"
    else; line + (text[i + 1] =~ /^\s+\S/ ? "\n" : " ")
    end
  end
  out
end

#hr(col = 72, sep = "-") ⇒ String

Returns a horizontal rule for output

Returns:

  • (String)

    returns a horizontal rule for output



43
44
45
# File 'lib/yard/templates/helpers/text_helper.rb', line 43

def hr(col = 72, sep = "-")
  sep * col
end

#indent(text, len = 4) ⇒ String

Returns indents text by len characters.

Returns:

  • (String)

    indents text by len characters.



27
28
29
# File 'lib/yard/templates/helpers/text_helper.rb', line 27

def indent(text, len = 4)
  text.gsub(/^/, ' ' * len)
end

#signature(meth) ⇒ String

Returns the formatted signature for a method.

Returns:

  • (String)

    the formatted signature for a method



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yard/templates/helpers/text_helper.rb', line 48

def signature(meth)
  # use first overload tag if it has a return type and method itself does not
  if !meth.tag(:return) && meth.tag(:overload) && meth.tag(:overload).tag(:return)
    meth = meth.tag(:overload)
  end

  type = options[:default_return] || ""
  if meth.tag(:return) && meth.tag(:return).types
    types = meth.tags(:return).map {|t| t.types ? t.types : [] }.flatten.uniq
    first = types.first
    if types.size == 2 && types.last == 'nil'
      type = first + '?'
    elsif types.size == 2 && types.last =~ /^(Array)?<#{Regexp.quote types.first}>$/
      type = first + '+'
    elsif types.size > 2
      type = [first, '...'].join(', ')
    elsif types == ['void'] && options[:hide_void_return]
      type = ""
    else
      type = types.join(", ")
    end
  end
  type = "(#{type})" if type.include?(',')
  type = " -> #{type} " unless type.empty?
  scope = meth.scope == :class ? "#{meth.namespace.name}." : "#{meth.namespace.name.to_s.downcase}."
  name = meth.name
  blk = format_block(meth)
  args = format_args(meth)
  extras = []
  extras_text = ''
  if rw = meth.namespace.attributes[meth.scope][meth.name]
    attname = [rw[:read] ? 'read' : nil, rw[:write] ? 'write' : nil].compact
    attname = attname.size == 1 ? attname.join('') + 'only' : nil
    extras << attname if attname
  end
  extras << meth.visibility if meth.visibility != :public
  extras_text = '(' + extras.join(", ") + ')' unless extras.empty?
  title = "%s%s%s %s%s%s" % [scope, name, args, blk, type, extras_text]
  title.gsub(/\s+/, ' ')
end

#title_align_right(text, col = 72) ⇒ String

Returns aligns a title to the right.

Returns:

  • (String)

    aligns a title to the right



32
33
34
# File 'lib/yard/templates/helpers/text_helper.rb', line 32

def title_align_right(text, col = 72)
  align_right(text, '-', col)
end

#wrap(text, col = 72) ⇒ String

Returns wraps text at col columns.

Returns:

  • (String)

    wraps text at col columns.



22
23
24
# File 'lib/yard/templates/helpers/text_helper.rb', line 22

def wrap(text, col = 72)
  text.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n") 
end