Module: Whatsa::Format

Included in:
Article, CLI, Scraper, Section
Defined in:
lib/whatsa/format.rb

Instance Method Summary collapse

Instance Method Details

#bulletize_lines(string) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/whatsa/format.rb', line 16

def bulletize_lines(string)
  if string.index("\n")
    list = string.gsub(/^/, "- ")
    word_wrap(list, "- ".length)
  else
    string
  end
end

#heading_to_title(string) ⇒ Object



8
9
10
# File 'lib/whatsa/format.rb', line 8

def heading_to_title(string)
  string.gsub('[edit]', '').strip
end

#remove_citation_markers(string) ⇒ Object



12
13
14
# File 'lib/whatsa/format.rb', line 12

def remove_citation_markers(string)
  string.gsub(/\[(\d+|citation needed)\]/, "")
end

#url_friendly(string) ⇒ Object



4
5
6
# File 'lib/whatsa/format.rb', line 4

def url_friendly(string)
  string.gsub(/[^A-z0-9\(\)]+/, '+').gsub(/(\A\+|\+\z)/, '')
end

#word_wrap(text, indent = 0) ⇒ Object

setting an indent will indent the lines AFTER the first line of a paragraph



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/whatsa/format.rb', line 26

def word_wrap(text, indent=0)
  chars = text.split(//)
  unless text.length < 80
    count = 1
    last_space = 80
    chars.each_with_index do |char, index|
      count += 1
      last_space = index if char.match(/ /)
      if char == "\n"
        count = indent
      elsif count == 80
        chars[last_space] = "\n#{" " * indent}"
        count = indent + index - last_space
      end
    end
  end
  chars.join
end