random-words 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +148 -39
  3. data/.rubocop.yml +1 -0
  4. data/CHANGELOG.md +42 -0
  5. data/Gemfile.lock +4 -1
  6. data/README.md +153 -4
  7. data/bin/randw +195 -49
  8. data/lib/random-words/array.rb +51 -0
  9. data/lib/random-words/config.rb +248 -0
  10. data/lib/random-words/generator.rb +239 -93
  11. data/lib/random-words/hash.rb +16 -0
  12. data/lib/random-words/html2markdown.rb +205 -0
  13. data/lib/random-words/lorem-markdown.rb +398 -0
  14. data/lib/random-words/number-to-word.rb +137 -0
  15. data/lib/random-words/source.rb +105 -0
  16. data/lib/random-words/string.rb +221 -47
  17. data/lib/random-words/table-cleanup.rb +210 -0
  18. data/lib/random-words/version.rb +1 -1
  19. data/lib/random-words/words/1984/adjectives.txt +103 -0
  20. data/lib/random-words/words/1984/adverbs.txt +92 -0
  21. data/lib/random-words/words/1984/articles-plural.txt +10 -0
  22. data/lib/random-words/words/1984/articles-singular.txt +10 -0
  23. data/lib/random-words/words/1984/clauses.txt +79 -0
  24. data/lib/random-words/words/1984/config.yml +4 -0
  25. data/lib/random-words/words/1984/conjunctions-coordinating.txt +20 -0
  26. data/lib/random-words/words/1984/conjunctions-subordinate.txt +29 -0
  27. data/lib/random-words/words/1984/names.txt +74 -0
  28. data/lib/random-words/words/1984/nouns-plural.txt +89 -0
  29. data/lib/random-words/words/1984/nouns-singular.txt +74 -0
  30. data/lib/random-words/words/1984/numbers.yml +5 -0
  31. data/lib/random-words/words/1984/prepositions.txt +89 -0
  32. data/lib/random-words/words/1984/terminators.txt +16 -0
  33. data/lib/random-words/words/1984/verbs-passive.txt +83 -0
  34. data/lib/random-words/words/1984/verbs-plural.txt +91 -0
  35. data/lib/random-words/words/1984/verbs-singular.txt +110 -0
  36. data/lib/random-words/words/alice/adjectives.txt +80 -0
  37. data/lib/random-words/words/alice/adverbs.txt +87 -0
  38. data/lib/random-words/words/alice/articles-plural.txt +10 -0
  39. data/lib/random-words/words/alice/articles-singular.txt +10 -0
  40. data/lib/random-words/words/alice/clauses.txt +81 -0
  41. data/lib/random-words/words/alice/config.yml +4 -0
  42. data/lib/random-words/words/alice/conjunctions-coordinating.txt +20 -0
  43. data/lib/random-words/words/alice/conjunctions-subordinate.txt +29 -0
  44. data/lib/random-words/words/alice/names.txt +74 -0
  45. data/lib/random-words/words/alice/nouns-plural.txt +95 -0
  46. data/lib/random-words/words/alice/nouns-singular.txt +82 -0
  47. data/lib/random-words/words/alice/numbers.yml +5 -0
  48. data/lib/random-words/words/alice/prepositions.txt +45 -0
  49. data/lib/random-words/words/alice/terminators.txt +16 -0
  50. data/lib/random-words/words/alice/verbs-passive.txt +495 -0
  51. data/lib/random-words/words/alice/verbs-plural.txt +115 -0
  52. data/lib/random-words/words/alice/verbs-singular.txt +97 -0
  53. data/lib/random-words/words/bacon/clauses.txt +592 -592
  54. data/lib/random-words/words/bacon/config.yml +4 -0
  55. data/lib/random-words/words/bacon/conjunctions-coordinating.txt +20 -0
  56. data/lib/random-words/words/bacon/names.txt +74 -0
  57. data/lib/random-words/words/bacon/numbers.yml +5 -0
  58. data/lib/random-words/words/bacon/prepositions.txt +45 -0
  59. data/lib/random-words/words/bacon/terminators.txt +16 -0
  60. data/lib/random-words/words/corporate/config.yml +4 -0
  61. data/lib/random-words/words/corporate/conjunctions-coordinating.txt +20 -0
  62. data/lib/random-words/words/corporate/names.txt +74 -0
  63. data/lib/random-words/words/corporate/numbers.yml +5 -0
  64. data/lib/random-words/words/corporate/prepositions.txt +45 -0
  65. data/lib/random-words/words/corporate/terminators.txt +16 -0
  66. data/lib/random-words/words/doctor/adjectives.txt +92 -0
  67. data/lib/random-words/words/doctor/adverbs.txt +92 -0
  68. data/lib/random-words/words/doctor/articles-plural.txt +10 -0
  69. data/lib/random-words/words/doctor/articles-singular.txt +10 -0
  70. data/lib/random-words/words/doctor/clauses.txt +83 -0
  71. data/lib/random-words/words/doctor/config.yml +10 -0
  72. data/lib/random-words/words/doctor/conjunctions-coordinating.txt +20 -0
  73. data/lib/random-words/words/doctor/conjunctions-subordinate.txt +29 -0
  74. data/lib/random-words/words/doctor/names.txt +74 -0
  75. data/lib/random-words/words/doctor/nouns-plural.txt +84 -0
  76. data/lib/random-words/words/doctor/nouns-singular.txt +84 -0
  77. data/lib/random-words/words/doctor/numbers.yml +5 -0
  78. data/lib/random-words/words/doctor/prepositions.txt +45 -0
  79. data/lib/random-words/words/doctor/terminators.txt +16 -0
  80. data/lib/random-words/words/doctor/verbs-passive.txt +83 -0
  81. data/lib/random-words/words/doctor/verbs-plural.txt +88 -0
  82. data/lib/random-words/words/doctor/verbs-singular.txt +83 -0
  83. data/lib/random-words/words/english/clauses.txt +592 -592
  84. data/lib/random-words/words/english/config.yml +4 -0
  85. data/lib/random-words/words/english/conjunctions-coordinating.txt +20 -0
  86. data/lib/random-words/words/english/names.txt +74 -0
  87. data/lib/random-words/words/english/numbers.yml +5 -0
  88. data/lib/random-words/words/english/prepositions.txt +45 -0
  89. data/lib/random-words/words/english/terminators.txt +16 -0
  90. data/lib/random-words/words/english/verbs-plural.txt +1 -0
  91. data/lib/random-words/words/english/verbs-singular.txt +1 -0
  92. data/lib/random-words/words/foulmouth/adjectives.txt +89 -0
  93. data/lib/random-words/words/foulmouth/adverbs.txt +97 -0
  94. data/lib/random-words/words/foulmouth/articles-plural.txt +10 -0
  95. data/lib/random-words/words/foulmouth/articles-singular.txt +10 -0
  96. data/lib/random-words/words/foulmouth/clauses.txt +74 -0
  97. data/lib/random-words/words/foulmouth/config.yml +4 -0
  98. data/lib/random-words/words/foulmouth/conjunctions-coordinating.txt +20 -0
  99. data/lib/random-words/words/foulmouth/conjunctions-subordinate.txt +29 -0
  100. data/lib/random-words/words/foulmouth/names.txt +74 -0
  101. data/lib/random-words/words/foulmouth/nouns-plural.txt +96 -0
  102. data/lib/random-words/words/foulmouth/nouns-singular.txt +99 -0
  103. data/lib/random-words/words/foulmouth/numbers.yml +5 -0
  104. data/lib/random-words/words/foulmouth/prepositions.txt +35 -0
  105. data/lib/random-words/words/foulmouth/terminators.txt +16 -0
  106. data/lib/random-words/words/foulmouth/verbs-passive.txt +143 -0
  107. data/lib/random-words/words/foulmouth/verbs-plural.txt +91 -0
  108. data/lib/random-words/words/foulmouth/verbs-singular.txt +104 -0
  109. data/lib/random-words/words/hipster/adjectives.txt +100 -0
  110. data/lib/random-words/words/hipster/adverbs.txt +89 -0
  111. data/lib/random-words/words/hipster/articles-plural.txt +10 -0
  112. data/lib/random-words/words/hipster/articles-singular.txt +10 -0
  113. data/lib/random-words/words/hipster/clauses.txt +180 -0
  114. data/lib/random-words/words/hipster/config.yml +4 -0
  115. data/lib/random-words/words/hipster/conjunctions-coordinating.txt +20 -0
  116. data/lib/random-words/words/hipster/conjunctions-subordinate.txt +29 -0
  117. data/lib/random-words/words/hipster/names.txt +74 -0
  118. data/lib/random-words/words/hipster/nouns-plural.txt +96 -0
  119. data/lib/random-words/words/hipster/nouns-singular.txt +100 -0
  120. data/lib/random-words/words/hipster/numbers.yml +5 -0
  121. data/lib/random-words/words/hipster/prepositions.txt +45 -0
  122. data/lib/random-words/words/hipster/terminators.txt +16 -0
  123. data/lib/random-words/words/hipster/verbs-passive.txt +88 -0
  124. data/lib/random-words/words/hipster/verbs-plural.txt +106 -0
  125. data/lib/random-words/words/hipster/verbs-singular.txt +114 -0
  126. data/lib/random-words/words/latin/config.yml +4 -0
  127. data/lib/random-words/words/latin/conjunctions-coordinating.txt +15 -0
  128. data/lib/random-words/words/latin/names.txt +74 -0
  129. data/lib/random-words/words/latin/numbers.yml +5 -0
  130. data/lib/random-words/words/latin/prepositions.txt +18 -0
  131. data/lib/random-words/words/latin/terminators.txt +16 -0
  132. data/lib/random-words/words/spanish/adjectives.txt +81 -0
  133. data/lib/random-words/words/spanish/adverbs.txt +87 -0
  134. data/lib/random-words/words/spanish/articles-plural.txt +4 -0
  135. data/lib/random-words/words/spanish/articles-singular.txt +4 -0
  136. data/lib/random-words/words/spanish/clauses.txt +74 -0
  137. data/lib/random-words/words/spanish/config.yml +4 -0
  138. data/lib/random-words/words/spanish/conjunctions-coordinating.txt +8 -0
  139. data/lib/random-words/words/spanish/conjunctions-subordinate.txt +16 -0
  140. data/lib/random-words/words/spanish/names.txt +74 -0
  141. data/lib/random-words/words/spanish/nouns-plural.txt +92 -0
  142. data/lib/random-words/words/spanish/nouns-singular.txt +125 -0
  143. data/lib/random-words/words/spanish/numbers.yml +5 -0
  144. data/lib/random-words/words/spanish/prepositions.txt +31 -0
  145. data/lib/random-words/words/spanish/terminators.txt +17 -0
  146. data/lib/random-words/words/spanish/verbs-passive.txt +495 -0
  147. data/lib/random-words/words/spanish/verbs-plural.txt +326 -0
  148. data/lib/random-words/words/spanish/verbs-singular.txt +351 -0
  149. data/lib/random-words/words/veggie/adjectives.txt +111 -0
  150. data/lib/random-words/words/veggie/adverbs.txt +81 -0
  151. data/lib/random-words/words/veggie/articles-plural.txt +10 -0
  152. data/lib/random-words/words/veggie/articles-singular.txt +10 -0
  153. data/lib/random-words/words/veggie/clauses.txt +57 -0
  154. data/lib/random-words/words/veggie/config.yml +4 -0
  155. data/lib/random-words/words/veggie/conjunctions-coordinating.txt +20 -0
  156. data/lib/random-words/words/veggie/conjunctions-subordinate.txt +29 -0
  157. data/lib/random-words/words/veggie/names.txt +74 -0
  158. data/lib/random-words/words/veggie/nouns-plural.txt +78 -0
  159. data/lib/random-words/words/veggie/nouns-singular.txt +105 -0
  160. data/lib/random-words/words/veggie/numbers.yml +5 -0
  161. data/lib/random-words/words/veggie/prepositions.txt +45 -0
  162. data/lib/random-words/words/veggie/terminators.txt +16 -0
  163. data/lib/random-words/words/veggie/verbs-passive.txt +56 -0
  164. data/lib/random-words/words/veggie/verbs-plural.txt +79 -0
  165. data/lib/random-words/words/veggie/verbs-singular.txt +79 -0
  166. data/lib/random-words.rb +28 -0
  167. data/random-words.gemspec +3 -1
  168. data/src/_README.md +153 -4
  169. metadata +168 -7
  170. data/lib/random-words/words/bacon/numbers.txt +0 -21
  171. data/lib/random-words/words/corporate/numbers.txt +0 -21
  172. data/lib/random-words/words/english/numbers.txt +0 -21
  173. data/lib/random-words/words/latin/numbers.txt +0 -21
data/bin/randw CHANGED
@@ -9,12 +9,15 @@ module RandomWords
9
9
  def initialize(source = :english)
10
10
  # Create an instance of the generator
11
11
  @sentence_generator = RandomWords::Generator.new(source)
12
+ @sentence_generator.use_extended_punctuation = true
12
13
  @colors = {
13
14
  text: :yellow,
14
15
  counter: :boldcyan,
15
16
  marker: :boldgreen,
16
- bracket: :cyan
17
+ bracket: :cyan,
18
+ language: :boldmagenta
17
19
  }
20
+ @sources = @sentence_generator.sources.keys.map(&:to_s)
18
21
  end
19
22
 
20
23
  def colors
@@ -60,6 +63,7 @@ module RandomWords
60
63
  def paragraphs(length, count = 3)
61
64
  @sentence_generator.sentence_length = length
62
65
  @sentence_generator.paragraph_length = count
66
+ @sentence_generator.source = @sources.sample
63
67
 
64
68
  header_2("Random Paragraph (#{@sentence_generator.paragraph_length} #{@sentence_generator.sentence_length} sentences)")
65
69
  graf = @sentence_generator.paragraph
@@ -77,40 +81,60 @@ module RandomWords
77
81
 
78
82
  # Generate and print random combined sentences
79
83
  def combined_sentences(length = nil)
80
- number_of_sentences = length || 3
84
+ number_of_sentences = length || 2
85
+
81
86
  header_1('Random Combined Sentences:')
82
- @sentence_generator.sentences(number_of_sentences).each_with_index do |_sentence, _index|
83
- puts text(sentence(:medium))
87
+ @sources.count.times do |i|
88
+ @sentence_generator.source = @sources[i - 1]
89
+ @sentence_generator.sentence_length = :medium
90
+ @sentence_generator.paragraph_length = number_of_sentences
91
+ @sentence_generator.sentences(number_of_sentences)
92
+ s = @sentence_generator.sentence
93
+ puts "#{marker} #{text(s)}"
94
+ puts counter("#{s.split(/ /).count} words, #{s.length} characters")
95
+ puts colorize_text(' ' * 2, :boldwhite)
84
96
  end
85
97
  end
86
98
 
87
99
  def random_sentences
88
100
  header_1('Random Sentences')
89
- @sentence_generator.lengths.keys.each do |length|
101
+ @sentence_generator.lengths.keys.each_with_index do |length, index|
102
+ @sentence_generator.source = @sources[index]
90
103
  sentence(length)
91
104
  end
92
105
  end
93
106
 
94
107
  def random_paragraphs
95
108
  header_1('Random Paragraphs')
96
- @sentence_generator.lengths.keys.each do |length|
109
+ @sentence_generator.lengths.keys.each_with_index do |length, index|
110
+ @sentence_generator.source = @sources[index]
97
111
  paragraphs(length, 3)
98
112
  end
99
113
  end
100
114
 
101
115
  # Generate and print specified number of words
102
116
  def random_words
103
- number_of_words = 10
117
+ number_of_words = []
118
+ @sources.count.times do |i|
119
+ puts i
120
+ number_of_words << (i * i) * 5 + 10
121
+ end
122
+
104
123
  header_1("#{number_of_words} Random Words")
105
- s = @sentence_generator.words(number_of_words)
106
- puts "#{marker} #{text(s)} "
107
- puts counter("#{s.split(/ /).count} words, #{s.length} characters")
124
+ @sources.each_with_index do |source, index|
125
+ header_2("#{number_of_words[index]} Random Words")
126
+ @sentence_generator.source = source
127
+ s = @sentence_generator.words(number_of_words[index])
128
+ puts "#{marker} #{text(s)} "
129
+ puts counter("#{s.split(/ /).count} words, #{s.length} characters")
130
+ end
108
131
  end
109
132
 
110
133
  # Generate and print specified number of characters
111
134
  def random_characters
112
135
  header_1('Random Characters (exact length)')
113
- [20, 50, 120, 200].each do |i|
136
+ [20, 50, 120, 200, 500].each_with_index do |i, index|
137
+ @sentence_generator.source = @sources[index]
114
138
  chars = @sentence_generator.characters(i, whole_words: true)
115
139
  puts "#{marker} #{colorize_text("#{i}:", :boldwhite)} #{text(chars)} #{counter(chars.length)}"
116
140
  end
@@ -118,8 +142,10 @@ module RandomWords
118
142
  # Generate and print specified number of characters
119
143
  max_characters = [15, 25, 53, 110, 600]
120
144
  min_characters = [10, 20, 50, 100, 500]
145
+
121
146
  header_1('Random Characters (length range)')
122
147
  max_characters.count.times do |i|
148
+ @sentence_generator.source = @sources[i]
123
149
  chars = @sentence_generator.characters(min_characters[i], max_characters[i], whole_words: true)
124
150
  range = "#{marker} #{colorize_text("[#{min_characters[i]}-#{max_characters[i]}]: ", :boldwhite)}"
125
151
  puts "#{range}#{text(chars)} #{counter(chars.length)}"
@@ -134,9 +160,16 @@ module RandomWords
134
160
  colorize_text(text, @colors[:text])
135
161
  end
136
162
 
163
+ def language
164
+ "#{colorize_text("(",
165
+ @colors[:bracket])}#{colorize_text("#{@sentence_generator.source}", @colors[:language])}#{colorize_text(")",
166
+ @colors[:bracket])}"
167
+
168
+ end
169
+
137
170
  def counter(length)
138
- "#{colorize_text('[',
139
- @colors[:bracket])}#{colorize_text("#{length}", @colors[:counter])}#{colorize_text(']', @colors[:bracket])}"
171
+ "#{colorize_text("[",
172
+ @colors[:bracket])}#{colorize_text("#{length}", @colors[:counter])}#{colorize_text("]", @colors[:bracket])} #{language}"
140
173
  end
141
174
 
142
175
  def print_test
@@ -149,6 +182,8 @@ module RandomWords
149
182
  end
150
183
  end
151
184
 
185
+ # Default options for the script
186
+
152
187
  @options = {
153
188
  source: :latin,
154
189
  length: :medium,
@@ -157,30 +192,20 @@ end
157
192
  method: nil,
158
193
  debug: false,
159
194
  whitespace: true,
160
- separator: ' '
195
+ separator: ' ',
196
+ use_extended_punctuation: false
161
197
  }
162
198
 
163
199
  OptionParser.new do |opts|
164
200
  opts.banner = 'Usage: randw [options]'
165
201
 
166
- opts.separator 'Options:'
202
+ opts.separator 'OPTIONS:'
167
203
 
168
- opts.on('-S', '--source [SOURCE]', 'Specify the source language (default: latin)') do |source|
169
- @options[:source] = case source
170
- when /^e/
171
- :english
172
- when /^l/
173
- :latin
174
- when /^(co|pro)/
175
- :corporate
176
- when /^(me|ba|ca)/
177
- :bacon
178
- else
179
- :latin
180
- end
204
+ opts.on('-S', '--source SOURCE', 'Specify the source language (default: latin)') do |source|
205
+ @options[:source] = source.to_source
181
206
  end
182
207
 
183
- opts.on('-l', '--length [short|medium|long|very_long]', 'Specify the length of the sentence') do |length|
208
+ opts.on('-l', '--length LENGTH', 'Specify the length of the sentence [short|medium|long|very_long]') do |length|
184
209
  @options[:length] = case length
185
210
  when /^s/
186
211
  :short
@@ -193,43 +218,83 @@ OptionParser.new do |opts|
193
218
  end
194
219
  end
195
220
 
196
- opts.on('--graf-length [NUMBER]', 'Specify the number of sentences in a paragraph') do |length|
221
+ opts.on('--graf-length NUMBER', 'Specify the number of sentences in a paragraph') do |length|
197
222
  @options[:paragraph_length] = length.to_i
198
223
  end
199
224
 
200
- opts.on('-s', '--sentences [NUMBER]', 'Generate random sentences') do |number|
225
+ opts.on('--[no-]extended', 'Specify whether to use extended punctuation in generated text') do |value|
226
+ @options[:use_extended_punctuation] = value
227
+ end
228
+
229
+ opts.separator 'GENERATORS:'
230
+
231
+ opts.on('-s', '--sentences [NUMBER]', 'Generate NUMBER of random sentences (default: 3)') do |number|
201
232
  @options[:method] = :sentences
202
- @options[:count] = number.to_i
233
+ @options[:count] = number&.to_i || 3
203
234
  end
204
235
 
205
- opts.on('-p', '--paragraphs [NUMBER]', 'Generate random paragraphs') do |number|
236
+
237
+ opts.on('-p', '--paragraphs [NUMBER]', 'Generate NUMBER of random paragraphs (default: 3)') do |number|
206
238
  @options[:method] = :paragraphs
207
- @options[:count] = number
239
+ @options[:count] = number&.to_i || 3
208
240
  end
209
241
 
210
- opts.on('-w', '--words [NUMBER]', 'Generate random words') do |number|
242
+ opts.on('-w', '--words [NUMBER]', 'Generate NUMBER of random words (default: 10)') do |number|
211
243
  @options[:method] = :words
212
- @options[:count] = number
244
+ @options[:count] = number&.to_i || 10
213
245
  end
214
246
 
215
- opts.on('-c', '--characters [NUMBER]', 'Generate random characters') do |number|
247
+ opts.on('-c', '--characters [NUMBER]', 'Generate random characters of length (default: 100)') do |number|
216
248
  @options[:method] = :characters
217
- @options[:count] = number
249
+ @options[:count] = number&.to_i || 100
218
250
  end
219
251
 
220
- opts.on('--password', 'Generate a random password') do
252
+ opts.on('-m', '--markdown [SETTINGS]', 'Generate random markdown text, comma separated string like "latin,10,all"') do |settings|
253
+ @options[:method] = :markdown
254
+ @options[:markdown_settings] = settings || 'latin,10,all'
255
+ end
256
+
257
+ opts.on('-H', '--html [SETTINGS]', 'Generate random html text, comma separated string like "latin,10,all"') do |settings|
258
+ @options[:method] = :html
259
+ @options[:markdown_settings] = settings || 'latin,10,all'
260
+ end
261
+
262
+ opts.on('--password [LENGTH]', 'Generate a random password of LENGTH (default: 20)') do |len|
221
263
  @options[:method] = :password
222
- @options[:count] = 20
264
+ @options[:count] = len&.to_i || 20
223
265
  end
224
266
 
225
- opts.on('--separator [CHAR]', 'Specify the separator character for the password') do |separator|
267
+ opts.separator 'PASSWORD OPTIONS:'
268
+
269
+ opts.on('--separator CHAR', 'Specify the separator character for the password (default: " ")') do |separator|
226
270
  @options[:separator] = separator
227
271
  end
228
272
 
229
- opts.on('-n', '--no-whitespace', 'Specify whether to remove whitespace in generated text (characters only)') do
230
- @options[:whitespace] = false
273
+ opts.on('-n', '--[no-]whitespace', 'Specify whether to remove whitespace in generated text (characters only)') do |value|
274
+ @options[:whitespace] = value
275
+ end
276
+
277
+ opts.separator 'DICTIONARIES:'
278
+
279
+ opts.on('--list-dictionaries', 'List available dictionaries') do
280
+ @rw = RandomWords::Generator.new(:latin)
281
+
282
+ @rw.sources.each do |k, v|
283
+ puts "#{v.name}: #{v.description}"
284
+ puts "triggers: (#{v.names.join(", ")})"
285
+ puts
286
+ end
287
+ Process.exit 0
288
+ end
289
+
290
+ opts.on('--create-dictionary [NAME]', 'Create a new dictionary') do |name|
291
+ @rw = RandomWords::Generator.new(:latin)
292
+ @rw.create_dictionary(name)
293
+ Process.exit 0
231
294
  end
232
295
 
296
+ opts.separator 'OTHER OPTIONS:'
297
+
233
298
  opts.on('-d', '--debug', 'Enable debug mode, displays sentence/word/character counts') do
234
299
  @options[:debug] = true
235
300
  end
@@ -261,9 +326,83 @@ def debug(msg)
261
326
  warn RandomWords::Terminal.new.colorize_text(" [#{msg}]", :cyan)
262
327
  end
263
328
 
329
+ def markdown_settings(settings)
330
+ base_config = RandomWords::Config.new(:latin).config
331
+
332
+ defaults = {
333
+ source: base_config[:source].is_a?(String) ? base_config[:source].to_source : base_config[:source],
334
+ grafs: 10,
335
+ sentences: base_config[:paragraph_length],
336
+ length: base_config[:sentence_length],
337
+ decorate: true,
338
+ link: false,
339
+ ul: false,
340
+ ol: false,
341
+ dl: false,
342
+ bq: false,
343
+ code: false,
344
+ mark: false,
345
+ headers: false,
346
+ table: false
347
+ }
348
+ sources = []
349
+ RandomWords::Generator.new(:latin).sources.each { |k, v| sources.concat(v.names.map(&:to_s)) }
350
+
351
+ markdown_options = defaults.dup
352
+ settings = settings.split(%r{[,/.\|]}).map(&:strip)
353
+ settings.each do |setting|
354
+ case setting
355
+ when /^a(ll)?/
356
+ new_options = {
357
+ decorate: true,
358
+ link: true,
359
+ ul: true,
360
+ ol: true,
361
+ dl: true,
362
+ bq: true,
363
+ code: true,
364
+ mark: true,
365
+ headers: true,
366
+ image: true,
367
+ table: true
368
+ }
369
+ markdown_options.merge!(new_options)
370
+ when Regexp.union(sources)
371
+ markdown_options[:source] = setting.to_source
372
+ when /^\d+$/
373
+ markdown_options[:grafs] = setting.to_i
374
+ when /^dec/
375
+ markdown_options[:decorate] = true
376
+ when /^link/
377
+ markdown_options[:link] = true
378
+ when /^ul/
379
+ markdown_options[:ul] = true
380
+ when /^ol/
381
+ markdown_options[:ol] = true
382
+ when /^dl/
383
+ markdown_options[:dl] = true
384
+ when /^bq/
385
+ markdown_options[:bq] = true
386
+ when /^co/
387
+ markdown_options[:code] = true
388
+ when /^ma/
389
+ markdown_options[:mark] = true
390
+ when /^h/
391
+ markdown_options[:headers] = true
392
+ when /^t(a|bl)/
393
+ markdown_options[:table] = true
394
+ when /^[smlv]/
395
+ markdown_options[:length] = setting.to_length
396
+ end
397
+ end
398
+
399
+ markdown_options
400
+ end
401
+
264
402
  @rw = RandomWords::Generator.new(@options[:source])
265
403
  @rw.sentence_length = @options[:length] || :medium
266
404
  @rw.paragraph_length = @options[:paragraph_length] || 3
405
+ @rw.use_extended_punctuation = @options[:use_extended_punctuation] || false
267
406
  @options[:count] = @options[:count].to_i
268
407
 
269
408
  case @options[:method]
@@ -273,13 +412,13 @@ when :sentences
273
412
  words: 0,
274
413
  characters: 0
275
414
  }
276
- @options[:count].times do
277
- s = @rw.sentence
415
+ # @options[:count].times do
416
+ s = @rw.sentences(@options[:count]).join(' ')
278
417
  total[:words] += s.split(/ /).count
279
418
  total[:characters] += s.length
280
419
  print s
281
420
  debug "#{s.split(/ /).count}w, #{s.length}c"
282
- end
421
+ # end
283
422
  debug "Total: #{total[:sentences]}s, #{total[:words]}w, #{total[:characters]}c"
284
423
  when :paragraphs
285
424
  total = {
@@ -305,13 +444,20 @@ when :characters
305
444
  c = @rw.characters(@options[:count], whitespace: @options[:whitespace])
306
445
  print c
307
446
  debug "#{c.split(/ /).count}w, #{c.length}c"
447
+ when :markdown, :html
448
+ settings = markdown_settings(@options[:markdown_settings])
449
+ @rw.source = settings[:source]
450
+ @rw.sentence_length = settings[:length] || :medium
451
+ @rw.paragraph_length = settings[:sentences] || 5
452
+ @rw.use_extended_punctuation = settings[:use_extended_punctuation] || false
453
+ puts @rw.html(settings)
308
454
  when :password
309
455
  p = @rw.characters(20, whitespace: @options[:whitespace])
310
456
 
311
- m = p.match(/[oleat]/i)
457
+ m = p.match(/[oleats]/i)
312
458
  if m
313
459
  num_char = m.begin(0)
314
- char = m[0].tr('oleat', '01347')
460
+ char = m[0].tr('oleats', '013475')
315
461
  else
316
462
  num_char = p.length
317
463
  char = rand(10).to_s
@@ -319,7 +465,7 @@ when :password
319
465
  p[num_char] = char
320
466
 
321
467
  cap_char = rand(20)
322
- cap_char = rand(20) while p[cap_char] == ' '
468
+ cap_char = rand(20) while p[cap_char] !~ /[a-z]/
323
469
  p[cap_char] = p[cap_char].upcase
324
470
 
325
471
  tail_char = p =~ /[^a-z0-9]/i ? '' : %w[! @ # $ % ^ & * ( ) _ + - = ; : < > , . ? /].sample
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RandomWords
4
+ # Array helpers for RandomWords
5
+ # This module extends the Array class with additional methods for manipulating arrays.
6
+ class ::Array
7
+ # Split a terminators array into terminators and
8
+ # extended punctuation based on a line without a comma
9
+ # @return [Array<Array<String>>]
10
+ # An array containing two arrays: the first for terminators
11
+ # and the second for extended punctuation
12
+ def split_terminators
13
+ terminators = []
14
+ extended_punctuation = []
15
+ terminators_ended = false
16
+ each do |line|
17
+ if line.include?(',') && !terminators_ended
18
+ terminators << line.split(',').map(&:strip)
19
+ elsif terminators_ended
20
+ extended_punctuation << line.split(',').map(&:strip)
21
+ else
22
+ terminators_ended = true
23
+ end
24
+ end
25
+ terminators.delete_if { |line| line[1].empty? }
26
+ extended_punctuation.delete_if { |line| line[1].empty? }
27
+ [terminators, extended_punctuation]
28
+ end
29
+
30
+ # Split a names list into first and last names
31
+ # Splits the names at blank line into two arrays: first names and last names.
32
+ # @return [Array<Array<String>, Array<String>>]
33
+ def split_names
34
+ first_names = []
35
+ last_names = []
36
+ first_names_ended = false
37
+ each do |line|
38
+ if line.strip =~ /^[\w-]+$/ && !first_names_ended
39
+ first_names << line
40
+ elsif first_names_ended
41
+ last_names << line
42
+ else
43
+ first_names_ended = true
44
+ end
45
+ end
46
+ first_names.delete_if(&:empty?)
47
+ last_names.delete_if(&:empty?)
48
+ [first_names, last_names]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ module RandomWords
5
+ # Configuration
6
+ class Config
7
+ # Language config files
8
+ CONFIG_FILES = %w[
9
+ config
10
+ numbers
11
+ ].freeze
12
+
13
+ # Parts of speech
14
+ SPEECH_PARTS = %w[
15
+ adjectives
16
+ adverbs
17
+ articles-plural
18
+ articles-singular
19
+ clauses
20
+ conjunctions-coordinating
21
+ conjunctions-subordinate
22
+ nouns-plural
23
+ nouns-singular
24
+ prepositions
25
+ terminators
26
+ verbs-passive
27
+ verbs-plural
28
+ verbs-singular
29
+ ].freeze
30
+
31
+ # Dictionary for source
32
+ attr_reader :dictionary
33
+
34
+ # Source directory for languages
35
+ attr_reader :source_dir
36
+
37
+ # Initialize the config with the given language
38
+ # @param lang [Symbol] The language to use
39
+ # @raise [RuntimeError] if no dictionary is found for the given language
40
+ def initialize(lang)
41
+ @lang = lang.to_s
42
+ FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
43
+
44
+ @source_dir = user_dictionary_exist? ? user_lang_dir : builtin_lang_dir
45
+
46
+ raise "No dictionary found for #{@lang}" unless @source_dir
47
+
48
+ rw_source = RandomWords::Source.new(@lang, @source_dir)
49
+
50
+ @dictionary = rw_source.dictionary
51
+
52
+ end
53
+
54
+ # Tests if a uer dictionary exists
55
+ # @return [Boolean] true if the user dictionary exists, false otherwise
56
+ # @raise [RuntimeError] if the user dictionary is incomplete
57
+ def user_dictionary_exist?
58
+ if user_lang_dir
59
+ raise "User dictionary for #{@lang} is incomplete. Please run create_user_dictionary." unless all_parts_of_speech?(user_lang_dir, @lang)
60
+
61
+ true
62
+ else
63
+ false
64
+ end
65
+ end
66
+
67
+ # The user language directory path
68
+ # @return [String] The path to the user language directory
69
+ def user_lang_dir
70
+ File.join(config_dir, 'words', @lang) if File.exist?(File.join(config_dir, 'words', @lang))
71
+ end
72
+
73
+ # The builtin language directory path
74
+ # @param lang [String] The language to use
75
+ # @param basedir [String] The base directory to use
76
+ # @return [String, nil] The path to the builtin language directory or nil if not found
77
+ def builtin_lang_dir(lang = nil, basedir: nil)
78
+ lang ||= @lang
79
+ basedir ||= __dir__
80
+ dir = File.join(basedir, 'words', lang)
81
+ return dir if File.directory?(dir)
82
+
83
+ warn "No dictionary found for #{lang}"
84
+ nil
85
+ end
86
+
87
+ # Check if all parts of speech files exist in the given directory
88
+ # @param dir [String] The directory to check
89
+ # @param lang [String] The language to check
90
+ # @return [Boolean] true if all parts of speech files exist, false otherwise
91
+ def all_parts_of_speech?(dir, lang = nil)
92
+ lang ||= @lang
93
+ dir ||= @source_dir
94
+ exists = true
95
+ SPEECH_PARTS.each do |part|
96
+ unless File.exist?(File.join(dir, "#{part}.txt"))
97
+ warn "Missing #{File.join(dir, "#{part}.txt")} for #{lang}"
98
+ exists = false
99
+ break
100
+ end
101
+ end
102
+ CONFIG_FILES.each do |file|
103
+ unless File.exist?(File.join(dir, "#{file}.yml"))
104
+ warn "Missing #{File.join(dir, "#{file}.yml")} for #{lang}"
105
+ exists = false
106
+ end
107
+ end
108
+ exists
109
+ end
110
+
111
+ # Create a user dictionary for the given language
112
+ # @param lang [String] The language to create the dictionary for
113
+ # @return [Symbol, nil] The language symbol if successful, nil otherwise
114
+ def create_user_dictionary(lang = nil)
115
+ return lang.to_sym if File.directory?(File.join(config_dir, 'words', lang)) && all_parts_of_speech?(File.join(config_dir, 'words', lang), lang)
116
+
117
+ lang_dir = File.join(config_dir, 'words', lang)
118
+
119
+ FileUtils.mkdir_p(lang_dir) unless File.directory?(lang_dir)
120
+ SPEECH_PARTS.each do |part|
121
+ source_file = File.join(builtin_lang_dir('english'), "#{part}.txt")
122
+
123
+ target_file = File.join(lang_dir, "#{part}.txt")
124
+ unless File.exist?(target_file)
125
+ FileUtils.cp(source_file, target_file)
126
+ warn "Created #{part}.txt"
127
+ end
128
+ end
129
+
130
+ # Copy numbers.yml file from the builtin directory
131
+ source_file = File.join(builtin_lang_dir('english'), 'numbers.yml')
132
+ target_file = File.join(lang_dir, 'numbers.yml')
133
+ unless File.exist?(target_file)
134
+ FileUtils.cp(source_file, target_file)
135
+ warn "Created numbers.yml"
136
+ end
137
+
138
+ # Create the config.yml file if it doesn't exist
139
+ target_file = File.join(lang_dir, "config.yml")
140
+
141
+ unless File.exist?(target_file)
142
+ config = {
143
+ "name" => lang,
144
+ "triggers" => [lang],
145
+ "description" => "User dictionary for #{lang}",
146
+ }
147
+ File.write(target_file, config.to_yaml)
148
+ warn "Created #{target_file}"
149
+ end
150
+
151
+ if all_parts_of_speech?(lang_dir, lang) || (RandomWords.testing && !RandomWords.tested.include?('create_user_dictionary'))
152
+ RandomWords.tested << 'create_user_dictionary'
153
+ warn "Created #{lang} in #{lang_dir}"
154
+ lang.to_sym
155
+ end
156
+ end
157
+
158
+ # List all sources available, builtin and custom
159
+ # @return [Hash] A hash of source names and their corresponding RandomWords::Source objects
160
+ def sources
161
+ return @sources if @sources
162
+
163
+ @sources = {}
164
+ Dir[File.join(__dir__, "words", "*")].each do |dir|
165
+ next unless File.directory?(dir)
166
+ name = File.basename(dir)
167
+ @sources[name] = RandomWords::Source.new(name, dir)
168
+ end
169
+ Dir[File.join(config_dir, "words", "*")].each do |dir|
170
+ next unless File.directory?(dir)
171
+ name = File.basename(dir)
172
+ @sources[name] = RandomWords::Source.new(name, dir)
173
+ end
174
+ @sources
175
+ end
176
+
177
+ # The base configuration
178
+ def config
179
+ @config ||= base_config
180
+ end
181
+
182
+ # private
183
+
184
+ # The user configuration directory path
185
+ # @return [String] The path to the config directory
186
+ def config_dir
187
+ @config_dir ||= File.expand_path(File.join(Dir.home, '.config', 'random-words'))
188
+ end
189
+
190
+ # Set the user configuration directory path
191
+ # @param dir [String] The path to the config directory
192
+ # @return [String] The path to the config directory
193
+ def config_dir=(dir)
194
+ @config_dir = File.expand_path(dir)
195
+ end
196
+
197
+ # Convert a config file's options to regular config
198
+ # @param configuration [Hash] The configuration hash
199
+ # @return [Hash] The converted configuration hash
200
+ def handle_config(configuration)
201
+ {
202
+ source: configuration[:source].to_source || :latin,
203
+ sentence_length: configuration[:length].to_length || :medium,
204
+ paragraph_length: configuration[:paragraph_length].to_i || 5,
205
+ }
206
+ end
207
+
208
+ # Return base configuration
209
+ # @return [Hash] The base configuration
210
+ def base_config
211
+ config_file = File.join(config_dir, 'config.yml')
212
+ if File.exist?(config_file)
213
+ config = YAML.load_file(config_file).symbolize_keys
214
+ return handle_config(config)
215
+ end
216
+ # If the config file doesn't exist, create it
217
+ # and return the default configuration
218
+ create_base_config(config_file)
219
+ config = YAML.load_file(config_file).symbolize_keys
220
+ handle_config(config)
221
+ end
222
+
223
+ # Look for a config.yml file in the config directory
224
+ # @return [String, nil] The path to the config.yml file or nil if not found
225
+ def config_file
226
+ config_file = File.join(config_dir, 'config.yml')
227
+ return config_file if File.exist?(config_file)
228
+
229
+ create_base_config(config_file)
230
+ config_file
231
+ end
232
+
233
+ # Create a base config.yml file if it doesn't exist
234
+ # @param config_file [String] The path to the config.yml file
235
+ # @return [String] The path to the config.yml file
236
+ def create_base_config(config_file)
237
+ FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
238
+ config = {
239
+ "source" => "latin",
240
+ "length" => "medium",
241
+ "paragraph_length" => 5,
242
+ }
243
+ File.write(config_file, config.to_yaml)
244
+ warn "Created #{config_file}"
245
+ config_file
246
+ end
247
+ end
248
+ end