Module: BetterLorem

Defined in:
lib/betterlorem.rb

Overview

BetterLorem

A better Lorem Ipsum generator

Created By Caedmon Judd ([email protected]) statebuilt.com

Copyright 2012 State Digital. All rights reserved.

Defined Under Namespace

Classes: Loader

Constant Summary collapse

PUNCTUATION =
['.', ',', '!', '?', ':']

Class Method Summary collapse

Class Method Details

.c(count = 100, plainText = false) ⇒ Object

Return Characters



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/betterlorem.rb', line 73

def self.c(count = 100, plainText = false)
  loader = Loader.new

  # Merge paragraphs into one line
  lines = loader.lines.join(' ')

  # Start at a random index in the array
  start_inx = rand(lines.length - count)

  # Check for overrun
  raise "I don't know that many words. Try a smaller value." if (start_inx + count) > lines.length

  # Move the starting index to the beginning of a word
  inx = 1
  lines[start_inx, 100].scan(/./).each do |char|
    if char == ' '
      start_inx += inx
      break
    end
    inx += 1
  end

  # Capitalize the sentence
  sentence = lines[start_inx, count]
  sentence[0] = sentence[0].to_s.capitalize

  if plainText
    "#{sentence}\n"
  else
    "<p>#{sentence}</p>"
  end
end

.p(count = 5, plainText = false) ⇒ Object

Return Paragraphs



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/betterlorem.rb', line 48

def self.p(count = 5, plainText = false)
  loader = Loader.new

  # Start at a random index in the array but do not overrun array
  start_inx = rand(loader.lines.count - count)

  # Check for overrun
  raise "I don't know that many words. Try a smaller value." if (start_inx + count) > loader.lines.count

  # Build paragraphs from array
  paragraphs = loader.lines[start_inx, count]

  # Build final format based on parameters
  paragraphs.map! do |line|
    if plainText
      line = "#{line}\n"
    else
      line = "<p>#{line}</p>"
    end
  end

  paragraphs.join('')
end

.w(count = 15, plainText = false) ⇒ Object

Return Words



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
44
45
# File 'lib/betterlorem.rb', line 16

def self.w(count = 15, plainText = false)
  loader = Loader.new

  # Merge paragraphs into one line and split into words
  words = loader.lines.join(' ').split(' ')

  # Start at a random index in the array
  start_inx = rand(words.count - count)

  # Check for overrun
  raise "I don't know that many words. Try a smaller value." if (start_inx + count) > words.count

  # Select a random subset of words
  select_words = words[start_inx, count]

  # Capilalize the first word in the sentence
  select_words[0].to_s.capitalize!

  # Merge words into a string
  return_words = select_words.join(' ')

  # Correct the sentence's punctuation
  correct_punctuation(return_words)

  if plainText
    "#{return_words}\n"
  else
    "<p>#{return_words}</p>"
  end
end