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
38
39
40
41
42
43
# File 'lib/texttools.rb', line 11

def line_break(
  text, len: 80, prefix: '', first_prefix: nil, preserve_lines: false
)
  res = ''
  text = text.split(/\s*\n\s*\n\s*/).map { |para|
    preserve_lines ? para : para.gsub(/\s*\n\s*/, " ")
  }.join("\n\n")

  cur_prefix = first_prefix || prefix
  strlen = len - cur_prefix.length
  while text.length > strlen
    if (m = /\A([^\n]{0,#{strlen}})(\s+)/.match(text))
      res << cur_prefix + m[1]
      res << (m[2].include?("\n") ? m[2].gsub(/[^\n]/, '') : "\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 there's no text left, then there were trailing spaces and the final \n
  # is superfluous.
  if text.length > 0
    res << cur_prefix + text
  else
    res.rstrip!
  end

  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.



70
71
72
73
74
75
76
# File 'lib/texttools.rb', line 70

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

#ordinal(num, legal: true) ⇒ Object

Computes the ordinal number (using digits).

Parameters:

  • legal (defaults to: true)

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



83
84
85
86
87
88
89
90
91
# File 'lib/texttools.rb', line 83

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.



51
52
53
54
55
56
57
58
59
60
# File 'lib/texttools.rb', line 51

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