betterlorem 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/betterlorem.rb +125 -0
- metadata +45 -0
data/lib/betterlorem.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# BetterLorem
|
2
|
+
#
|
3
|
+
# A better Lorem Ipsom generator
|
4
|
+
#
|
5
|
+
# Created By Caedmon Judd (caedmon@statebuilt.com)
|
6
|
+
# http://statebuilt.com
|
7
|
+
#
|
8
|
+
# Copyright 2012 State Digital. All rights reserved.
|
9
|
+
#
|
10
|
+
|
11
|
+
module BetterLorem
|
12
|
+
|
13
|
+
PUNCTUATION = ['.', ',', '!', '?', ':']
|
14
|
+
|
15
|
+
# Return Words
|
16
|
+
def self.w(count, line_ending = "<br>", surround_with_tag = "p")
|
17
|
+
loader = Loader.new
|
18
|
+
|
19
|
+
# Merge paragraphs into one line and split into words
|
20
|
+
words = loader.lines.join(' ').split(' ')
|
21
|
+
|
22
|
+
# Start at a random index in the array
|
23
|
+
start_inx = rand(words.count - count)
|
24
|
+
|
25
|
+
# Check for overrun
|
26
|
+
raise "I don't know that many words. Try a smaller value." if (start_inx + count) > words.count
|
27
|
+
|
28
|
+
# Select a random subset of words
|
29
|
+
select_words = words[start_inx, count]
|
30
|
+
|
31
|
+
# Capilalize the first word in the sentence
|
32
|
+
select_words[0].to_s.capitalize!
|
33
|
+
|
34
|
+
# Merge words into a string
|
35
|
+
return_words = select_words.join(' ')
|
36
|
+
|
37
|
+
# Correct the sentence's punctuation
|
38
|
+
correct_punctuation(return_words)
|
39
|
+
|
40
|
+
return_words = "<#{surround_with_tag}>#{return_words}</#{surround_with_tag}>#{line_ending}"
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return Paragraphs
|
44
|
+
def self.p(count, line_ending = "<br>", surround_with_tag = "p")
|
45
|
+
loader = Loader.new
|
46
|
+
|
47
|
+
# Start at a random index in the array but do not overrun array
|
48
|
+
start_inx = rand(loader.lines.count - count)
|
49
|
+
|
50
|
+
# Check for overrun
|
51
|
+
raise "I don't know that many words. Try a smaller value." if (start_inx + count) > loader.lines.count
|
52
|
+
|
53
|
+
# Build paragraphs from array
|
54
|
+
paragraphs = loader.lines[start_inx, count]
|
55
|
+
|
56
|
+
# Build final format based on parameters
|
57
|
+
paragraphs.map! do |line|
|
58
|
+
line = "<#{surround_with_tag}>#{line}</#{surround_with_tag}>#{line_ending}"
|
59
|
+
end
|
60
|
+
|
61
|
+
paragraphs.join('\n')
|
62
|
+
end
|
63
|
+
|
64
|
+
# Return Characters
|
65
|
+
def self.c(count, line_ending = "<br>", surround_with_tag = "p")
|
66
|
+
loader = Loader.new
|
67
|
+
|
68
|
+
# Merge paragraphs into one line
|
69
|
+
lines = loader.lines.join(' ')
|
70
|
+
|
71
|
+
# Start at a random index in the array
|
72
|
+
start_inx = rand(lines.length - count)
|
73
|
+
|
74
|
+
# Check for overrun
|
75
|
+
raise "I don't know that many words. Try a smaller value." if (start_inx + count) > lines.length
|
76
|
+
|
77
|
+
# Move the starting index to the beginning of a word
|
78
|
+
inx = 1
|
79
|
+
lines[start_inx, 100].scan(/./).each do |char|
|
80
|
+
if char == ' '
|
81
|
+
start_inx += inx
|
82
|
+
break
|
83
|
+
end
|
84
|
+
inx += 1
|
85
|
+
end
|
86
|
+
|
87
|
+
# Capitalize the sentence
|
88
|
+
sentence = lines[start_inx, count]
|
89
|
+
sentence[0] = sentence[0].to_s.capitalize
|
90
|
+
sentence
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
# Determine if the line ends with punctuation and correct it
|
97
|
+
def self.correct_punctuation(line)
|
98
|
+
PUNCTUATION.each do |punct|
|
99
|
+
if line[line.length - 1].to_s == punct
|
100
|
+
line[line.length - 1] = "."
|
101
|
+
return
|
102
|
+
end
|
103
|
+
end
|
104
|
+
line << "."
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# Lorem file loader
|
109
|
+
class Loader
|
110
|
+
|
111
|
+
attr_accessor :lines
|
112
|
+
|
113
|
+
def initialize
|
114
|
+
self.lines = []
|
115
|
+
File.open('lorem.txt').each_line do |line|
|
116
|
+
self.lines << clean_line(line)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Remove any bad characters from the line
|
121
|
+
def clean_line(line)
|
122
|
+
line.gsub!(/\n/, '')
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: betterlorem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Caedmon Judd
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A better Lorem Ipsom generator
|
15
|
+
email: caedmon@statebuilt.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/betterlorem.rb
|
21
|
+
homepage: http://rubygems.org/gems/betterlorem
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Better Lorem Ipsum
|
45
|
+
test_files: []
|