Module: TextTools

Extended by:
TextTools
Included in:
TextTools
Defined in:
lib/texttools.rb

Instance Method Summary collapse

Instance Method Details

#line_break(text, len: 80, prefix: '', first_prefix: nil, preserve_lines: false) ⇒ Object

Breaks a text into lines of a given length. If preserve_lines is set, then all line breaks are preserved; otherwise line breaks are treated as spaces. However, two consecutive line breaks are always preserved, treating them as paragraph breaks. Line breaks at the end of the text are never preserved.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/texttools.rb', line 11

def line_break(
  text, len: 80, prefix: '', first_prefix: nil, preserve_lines: false
)
  res = String.new('')

  #
  # Remove single paragraph breaks (unless preserve_lines is on). After this,
  # every paragraph break should be preserved. Also remove trailing
  # whitespace, which causes problems.
  #
  text = text.split(/\s*\n\s*\n\s*/).map { |para|
    preserve_lines ? para : para.gsub(/\s*\n\s*/, " ")
  }.join("\n\n").rstrip

  cur_prefix = first_prefix || prefix

  # Process each individual line separately.
  while (line_match = /\s*\n\s*/.match(text))
    res << one_line_break(line_match.pre_match, len, cur_prefix, prefix)
    res << line_match[0].gsub(/[^\n]/, '')
    cur_prefix = prefix
    text = line_match.post_match
  end
  res << one_line_break(text, len, cur_prefix, prefix)
  return res

end

#markdown(text, i: [ '<i>', '</i>' ], b: [ '<b>', '</b>' ]) ⇒ Object

Processes simple markdown for a given text.

Parameters:

  • i (defaults to: [ '<i>', '</i>' ])

    A two-element array of the starting and ending text for italicized content.

  • b (defaults to: [ '<b>', '</b>' ])

    A two-element array of the starting and ending text for bold content.



92
93
94
95
96
97
98
# File 'lib/texttools.rb', line 92

def markdown(text, i: [ '<i>', '</i>' ], b: [ '<b>', '</b>' ])
  return text.gsub(/(?<!\w)\*\*([^*]+)\*\*(?!\w)/) { |t|
    "#{b.first}#$1#{b.last}"
  }.gsub(/(?<!\w)\*([^*]+)\*(?!\w)/) { |t|
    "#{i.first}#$1#{i.last}"
  }
end

#one_line_break(text, len, first_prefix, prefix) ⇒ Object

Line break a text that is guaranteed not to have any line breaks within it.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/texttools.rb', line 42

def one_line_break(text, len, first_prefix, prefix)
  res = String.new('')
  cur_prefix = first_prefix
  strlen = len - cur_prefix.length
  while text.length > strlen
    if (m = /\A(.{0,#{strlen}})(\s+)/.match(text))
      res << cur_prefix + m[1] + "\n"
      text = m.post_match
    else
      res << cur_prefix + text[0, strlen] + "\n"
      text = text[strlen..-1]
    end
    cur_prefix = prefix
    strlen = len - cur_prefix.length
  end

  if text.length > 0
    res << cur_prefix + text
  else
    res.rstrip!
  end

  return res
end

#ordinal(num, legal: true) ⇒ Object

Computes the ordinal number (using digits).

Parameters:

  • legal (defaults to: true)

    Whether to use legal ordinals (2d, 3d)



105
106
107
108
109
110
111
112
113
# File 'lib/texttools.rb', line 105

def ordinal(num, legal: true)
  case num.to_s
  when /1\d\z/ then "#{num}th"
  when /1\z/ then "#{num}st"
  when /2\z/ then legal ? "#{num}d" : "#{num}nd"
  when /3\z/ then legal ? "#{num}d" : "#{num}rd"
  else "#{num}th"
  end
end

#text_join(list, comma: ", ", amp: " & ", commaamp: " & ") ⇒ Object

Joins a list of items into a textual phrase. If there are two items, then amp is used to join them. If there are three or more items, then comma is used for all but the last pair, for which commaamp is used.



73
74
75
76
77
78
79
80
81
82
# File 'lib/texttools.rb', line 73

def text_join(list, comma: ", ", amp: " & ", commaamp: " & ")
  return list unless list.is_a?(Array)
  case list.count
  when 0 then raise "Can't textjoin empty list"
  when 1 then list.first
  when 2 then list.join(amp)
  else
    list[0..-2].join(comma) + commaamp + list.last
  end
end