random-words 1.0.11 → 1.0.13

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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +267 -20
  3. data/.rubocop_todo.yml +0 -313
  4. data/CHANGELOG.md +36 -0
  5. data/bin/randw +54 -194
  6. data/generator.cgi +155 -0
  7. data/lib/random-words/array.rb +0 -2
  8. data/lib/random-words/boolean.rb +1 -1
  9. data/lib/random-words/config.rb +6 -6
  10. data/lib/random-words/generator.rb +60 -32
  11. data/lib/random-words/html2markdown.rb +80 -20
  12. data/lib/random-words/{lorem-markdown.rb → lorem_html.rb} +123 -56
  13. data/lib/random-words/{number-to-word.rb → number_to_word.rb} +5 -5
  14. data/lib/random-words/numeric.rb +2 -2
  15. data/lib/random-words/string.rb +10 -3
  16. data/lib/random-words/terminal.rb +185 -0
  17. data/lib/random-words/version.rb +1 -1
  18. data/lib/random-words/words/corporate/clauses.txt +100 -100
  19. data/lib/random-words/words/walken/adjectives.txt +139 -0
  20. data/lib/random-words/words/walken/adverbs.txt +164 -0
  21. data/lib/random-words/words/walken/articles-plural.txt +10 -0
  22. data/lib/random-words/words/walken/articles-singular.txt +10 -0
  23. data/lib/random-words/words/walken/clauses.txt +200 -0
  24. data/lib/random-words/words/walken/config.yml +4 -0
  25. data/lib/random-words/words/walken/conjunctions-coordinating.txt +20 -0
  26. data/lib/random-words/words/walken/conjunctions-subordinate.txt +28 -0
  27. data/lib/random-words/words/walken/names.txt +101 -0
  28. data/lib/random-words/words/walken/nouns-plural.txt +330 -0
  29. data/lib/random-words/words/walken/nouns-singular.txt +500 -0
  30. data/lib/random-words/words/walken/numbers.yml +5 -0
  31. data/lib/random-words/words/walken/phrases.txt +45 -0
  32. data/lib/random-words/words/walken/prepositions.txt +45 -0
  33. data/lib/random-words/words/walken/terminators.txt +17 -0
  34. data/lib/random-words/words/walken/verbs-passive.txt +370 -0
  35. data/lib/random-words/words/walken/verbs-plural.txt +294 -0
  36. data/lib/random-words/words/walken/verbs-singular.txt +262 -0
  37. data/lib/random-words.rb +3 -2
  38. metadata +24 -4
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ # !/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # Random Sentence Generator
@@ -92,7 +92,7 @@ module RandomWords
92
92
  end
93
93
 
94
94
  # Display debug message
95
- def debug(msg)
95
+ def dbg(msg)
96
96
  return unless @debug
97
97
 
98
98
  "%#{msg}%"
@@ -110,7 +110,10 @@ module RandomWords
110
110
  # Define number of sentences in paragraphs
111
111
  # @param length [Integer] The number of sentences in the paragraph
112
112
  def paragraph_length=(length)
113
- raise ArgumentError, 'Paragraph length must be a positive integer' unless length.is_a?(Integer) && length.positive?
113
+ unless length.is_a?(Integer) && length.positive?
114
+ raise ArgumentError,
115
+ 'Paragraph length must be a positive integer'
116
+ end
114
117
 
115
118
  @paragraph_length = length
116
119
  end
@@ -200,7 +203,8 @@ module RandomWords
200
203
  end
201
204
 
202
205
  def use_extended_punctuation=(use_extended_punctuation)
203
- raise ArgumentError, 'use_extended_punctuation must be a boolean' unless [true, false].include?(use_extended_punctuation)
206
+ raise ArgumentError, 'use_extended_punctuation must be a boolean' unless [true,
207
+ false].include?(use_extended_punctuation)
204
208
 
205
209
  @use_extended_punctuation = use_extended_punctuation
206
210
  @terminators.concat(@config.dictionary[:extended_punctuation]) if use_extended_punctuation
@@ -237,7 +241,7 @@ module RandomWords
237
241
  result = SENTENCE_PARTS.cycle.take(number).map { |part| send(part.to_sym) }.take(number)
238
242
  result.map do |word|
239
243
  word.split(/ /).last
240
- end.join(' ').compress
244
+ end.join(' ').article_agree.compress
241
245
  end
242
246
 
243
247
  # Generate a series of random words up to a specified length
@@ -250,7 +254,7 @@ module RandomWords
250
254
  # characters(50) # Generates a string with at least 50 characters
251
255
  # characters(50, 100) # Generates a string with between 50 and 100 characters
252
256
  # characters(50, whole_words: false) # Generates a string with 50 characters allowing word truncation
253
- def characters(min, max = nil, whole_words: true, whitespace: true, dead_switch: 0)
257
+ def characters(min, max = nil, whole_words: true, whitespace: true, article: true, dead_switch: 0)
254
258
  result = ''
255
259
  max ||= min
256
260
  raise ArgumentError, 'Infinite loop detected' if dead_switch > 40
@@ -258,7 +262,7 @@ module RandomWords
258
262
  whole_words = false if dead_switch > 15
259
263
 
260
264
  space = whitespace ? ' ' : ''
261
- current_part = 0
265
+ current_part = article ? 0 : 1
262
266
  while result.length < max && result.length < min
263
267
  word = send(SENTENCE_PARTS[current_part].to_sym)
264
268
  word.gsub!(/ +/, '') unless whitespace
@@ -338,7 +342,6 @@ module RandomWords
338
342
  code_langs[Random.rand(code_langs.count)]
339
343
  end
340
344
 
341
- # rubocop:disable Layout/LineLength
342
345
  # Return random code snippet
343
346
  # @param lang [Symbol] The language of the code snippet
344
347
  # @return [String] A randomly generated code snippet
@@ -356,27 +359,52 @@ module RandomWords
356
359
  lang ||= code_lang
357
360
  code_snippets[lang.to_sym]
358
361
  end
359
- # rubocop:enable Layout/LineLength
360
362
 
361
363
  # Generate random markdown
362
364
  # @param settings [Hash] Settings for generating markdown
363
365
  # @return [String] A randomly generated markdown string
364
366
  def markdown(settings = {})
365
- input = RandomWords::LoremMarkdown.new(settings).output
366
- RandomWords::HTML2Markdown.new(input).markdown
367
+ input = RandomWords::LoremHTML.new(settings)
368
+ meta = {}
369
+ if settings[:meta_type]
370
+ meta[:type] = settings[:meta_type]
371
+ meta[:title] = input.title if input.title
372
+ meta[:style] = settings[:style] || 'style.css'
373
+ meta[:date] = Time.now.strftime('%Y-%m-%d %H:%M:%S')
374
+ end
375
+ RandomWords::HTML2Markdown.new(input, nil, meta).to_s
367
376
  end
368
377
 
369
378
  # Generate random HTML
370
379
  # @param settings [Hash] Settings for generating HTML
371
380
  # @return [String] A randomly generated HTML string
372
381
  def html(settings = {})
373
- RandomWords::LoremMarkdown.new(settings).output
382
+ html = RandomWords::LoremHTML.new(settings)
383
+ if settings[:complete]
384
+ style = settings[:style] || 'style.css'
385
+ <<~EOOUTPUT
386
+ <!DOCTYPE html>
387
+ <html lang="en">
388
+ <head>
389
+ \t<meta charset="UTF-8">
390
+ \t<meta name="viewport" content="width=device-width, initial-scale=1.0">
391
+ \t<title>#{html.title}</title>
392
+ \t<link rel="stylesheet" href="#{style}">
393
+ </head>
394
+ <body>
395
+ #{html.output.indent("\t")}
396
+ </body>
397
+ </html>
398
+ EOOUTPUT
399
+ else
400
+ html.output
401
+ end
374
402
  end
375
403
 
376
404
  # Generate a random name
377
405
  # @return [String] A randomly generated name
378
406
  def name
379
- "#{debug('NAM')}#{random_name}"
407
+ "#{dbg('NAM')}#{random_name}"
380
408
  end
381
409
 
382
410
  private
@@ -509,7 +537,7 @@ module RandomWords
509
537
  # @example
510
538
  # random_conjunction # Returns a random conjunction
511
539
  def random_conjunction
512
- "#{debug('COC')}#{coordinating_conjunctions.sample}"
540
+ "#{dbg('COC')}#{coordinating_conjunctions.sample}"
513
541
  end
514
542
 
515
543
  # Generate a random number with a plural noun
@@ -526,65 +554,65 @@ module RandomWords
526
554
  end
527
555
  if num == 1 || (RandomWords.testing && !RandomWords.tested.include?('random_noun'))
528
556
  RandomWords.tested << 'random_noun' if RandomWords.testing
529
- "#{debug('NUM')}#{number} #{random_noun}"
557
+ "#{dbg('NUM')}#{number} #{random_noun}"
530
558
  else
531
559
  RandomWords.tested << 'random_plural_noun' if RandomWords.testing
532
- "#{debug('NUM')}#{number} #{random_plural_noun}"
560
+ "#{dbg('NUM')}#{number} #{random_plural_noun}"
533
561
  end
534
562
  end
535
563
 
536
564
  # Generate a random phrase
537
565
  # @return [String] A randomly selected phrase
538
566
  def random_phrase
539
- "#{debug('PHR')}#{phrases.sample}"
567
+ "#{dbg('PHR')}#{phrases.sample}"
540
568
  end
541
569
 
542
570
  # Generate a random noun
543
571
  # @return [String] A randomly selected noun
544
572
  def random_noun
545
- "#{debug('NOU')}#{nouns.sample}"
573
+ "#{dbg('NOU')}#{nouns.sample}"
546
574
  end
547
575
 
548
576
  # Generate a random plural noun
549
577
  # @return [String] A randomly selected plural noun
550
578
  def random_plural_noun
551
- "#{debug('PLN')}#{plural_nouns.sample}"
579
+ "#{dbg('PLN')}#{plural_nouns.sample}"
552
580
  end
553
581
 
554
582
  # Generate a random verb
555
583
  # @return [String] A randomly selected verb
556
584
  def random_verb
557
- "#{debug('VER')}#{verbs.sample}"
585
+ "#{dbg('VER')}#{verbs.sample}"
558
586
  end
559
587
 
560
588
  # Generate a random plural verb
561
589
  # @return [String] A randomly selected plural verb
562
590
  def random_plural_verb
563
- "#{debug('PLV')}#{plural_verbs.sample}"
591
+ "#{dbg('PLV')}#{plural_verbs.sample}"
564
592
  end
565
593
 
566
594
  # Generate a random passive verb
567
595
  # @return [String] A randomly selected passive verb
568
596
  def random_passive_verb
569
- "#{debug('PAV')}#{passive_verbs.sample}"
597
+ "#{dbg('PAV')}#{passive_verbs.sample}"
570
598
  end
571
599
 
572
600
  # Generate a random adverb
573
601
  # @return [String] A randomly selected adverb
574
602
  def random_adverb
575
- "#{debug('ADV')}#{adverbs.sample}"
603
+ "#{dbg('ADV')}#{adverbs.sample}"
576
604
  end
577
605
 
578
606
  # Generate a random adjective
579
607
  # @return [String] A randomly selected adjective
580
608
  def random_adjective
581
- "#{debug('ADJ')}#{adjectives.sample}"
609
+ "#{dbg('ADJ')}#{adjectives.sample}"
582
610
  end
583
611
 
584
612
  # Generate a random article
585
613
  # @return [String] A randomly selected article
586
614
  def random_article
587
- "#{debug('ART')}#{articles.rotate[0]}"
615
+ "#{dbg('ART')}#{articles.rotate[0]}"
588
616
  end
589
617
 
590
618
  # Generate a random article for a noun
@@ -610,13 +638,13 @@ module RandomWords
610
638
  # Generate a random plural article
611
639
  # @return [String] A randomly selected plural article
612
640
  def random_plural_article
613
- "#{debug('PLA')}#{plural_articles.rotate[0]}"
641
+ "#{dbg('PLA')}#{plural_articles.rotate[0]}"
614
642
  end
615
643
 
616
644
  # Generate a random clause
617
645
  # @return [String] A randomly selected clause
618
646
  def random_clause
619
- "#{debug('CLA')}#{clauses.sample}"
647
+ "#{dbg('CLA')}#{clauses.sample}"
620
648
  end
621
649
 
622
650
  # Generate a random set of separators
@@ -627,25 +655,25 @@ module RandomWords
627
655
  # Generate a random separator
628
656
  # @return [String] A randomly selected separator
629
657
  def random_separator
630
- "#{debug('SEP')}#{random_separators.sample}"
658
+ "#{dbg('SEP')}#{random_separators.sample}"
631
659
  end
632
660
 
633
661
  # Generate a random subordinate conjunction
634
662
  # @return [String] A randomly selected subordinate conjunction
635
663
  def random_subordinate_conjunction
636
- "#{debug('SUC')}#{subordinate_conjunctions.rotate[0]}"
664
+ "#{dbg('SUC')}#{subordinate_conjunctions.rotate[0]}"
637
665
  end
638
666
 
639
667
  # Generate a random coordinating conjunction
640
668
  # @return [String] A randomly selected coordinating conjunction
641
669
  def random_coordinating_conjunction
642
- "#{debug('COC')}#{coordinating_conjunctions.rotate[0]}"
670
+ "#{dbg('COC')}#{coordinating_conjunctions.rotate[0]}"
643
671
  end
644
672
 
645
673
  # Generate a random preposition
646
674
  # @return [String] A randomly selected preposition
647
675
  def random_preposition
648
- "#{debug('PRE')}#{prepositions.rotate[0]}"
676
+ "#{dbg('PRE')}#{prepositions.rotate[0]}"
649
677
  end
650
678
 
651
679
  # Generate a random prepositional phrase
@@ -662,7 +690,7 @@ module RandomWords
662
690
  "#{random_article_for_word(noun)} #{noun}"
663
691
  end
664
692
 
665
- "#{debug('PRP')}#{preposition} #{phrase}"
693
+ "#{dbg('PRP')}#{preposition} #{phrase}"
666
694
  end
667
695
 
668
696
  # Generate a random terminator
@@ -12,12 +12,18 @@ module RandomWords
12
12
  # Initialize the HTML2Markdown converter
13
13
  # @param str [String] The HTML string to convert
14
14
  # @param baseurl [String] The base URL for resolving relative links
15
- def initialize(str, baseurl = nil)
15
+ def initialize(str, baseurl = nil, meta = {})
16
16
  @links = []
17
+ @footnotes = []
17
18
  @baseuri = (baseurl ? URI.parse(baseurl) : nil)
18
19
  @section_level = 0
19
- @encoding = str.encoding
20
- @markdown = output_for(Nokogiri::HTML(str, baseurl).root).gsub(/\n\n\n+/, "\n\n\n")
20
+ input = str.is_a?(String) ? str : str.output
21
+ @encoding = input.encoding
22
+ @markdown = output_for(Nokogiri::HTML(input, baseurl).root).gsub(/\n\n\n+/, "\n\n\n").strip
23
+ @title = meta[:title] if meta[:title]
24
+ @date = meta[:date] if meta[:date]
25
+ @style = meta[:style] if meta[:style]
26
+ @meta_type = meta[:type] if meta[:type]
21
27
  end
22
28
 
23
29
  # Convert the HTML to Markdown
@@ -26,12 +32,45 @@ module RandomWords
26
32
  # converter = HTML2Markdown.new('<p>Hello world</p>')
27
33
  # puts converter.to_s
28
34
  def to_s
35
+ meta = ''
36
+
37
+ if @meta_type
38
+ meta = <<~EOMETA
39
+ title: #{@title}
40
+ date: #{@date}
41
+ css: #{@style}
42
+ EOMETA
43
+ case @meta_type
44
+ when :multimarkdown
45
+ meta = <<~EOMMD
46
+ #{meta}
47
+
48
+ EOMMD
49
+ when :yaml
50
+ meta = <<~EOYAML
51
+ ---
52
+ #{meta}
53
+ ---
54
+ EOYAML
55
+ end
56
+ end
29
57
  i = 0
30
58
  @markdown = TableCleanup.new(@markdown).clean
31
- "#{@markdown}\n\n" + @links.map { |link|
32
- i += 1
33
- "[#{i}]: #{link[:href]}" + (link[:title] ? %( "#{link[:title]}") : '')
34
- }.join("\n")
59
+ out = "#{meta}#{@markdown}"
60
+
61
+ if @links.any?
62
+ out += "\n\n" + @links.map do |link|
63
+ i += 1
64
+ "[#{i}]: #{link[:href]}" + (link[:title] ? %( "#{link[:title]}") : '')
65
+ end.join("\n")
66
+ end
67
+
68
+ if @footnotes.any?
69
+ out += "\n\n" + @footnotes.map { |link|
70
+ "[^fn#{link[:counter]}]: #{link[:title]}"
71
+ }.join("\n\n")
72
+ end
73
+ out
35
74
  end
36
75
 
37
76
  # Recursively convert child nodes
@@ -42,6 +81,14 @@ module RandomWords
42
81
  end.join
43
82
  end
44
83
 
84
+ # Add a footnote to the list of links
85
+ # @param counter [String] The footnote counter
86
+ # @param link [Hash] The link to add
87
+ def add_footnote(counter, link)
88
+ @footnotes << { counter: counter, title: link[:title] }
89
+ @footnotes.length
90
+ end
91
+
45
92
  # Add a link to the list of links
46
93
  # @param link [Hash] The link to add
47
94
  def add_link(link)
@@ -69,7 +116,7 @@ module RandomWords
69
116
  # @param str [String] The string to wrap
70
117
  # @return [String] The wrapped string
71
118
  def wrap(str)
72
- return str if str =~ /\n/
119
+ return str if str.include?("\n")
73
120
 
74
121
  out = []
75
122
  line = []
@@ -80,7 +127,7 @@ module RandomWords
80
127
  line = []
81
128
  end
82
129
  end
83
- out << (line.join(' ') + (str[-1..-1] =~ /[ \t\n]/ ? str[-1..-1] : ''))
130
+ out << (line.join(' ') + (/[ \t\n]/.match?(str[-1..-1]) ? str[-1..-1] : ''))
84
131
  out.join("\n")
85
132
  end
86
133
 
@@ -89,7 +136,13 @@ module RandomWords
89
136
  # @return [String] The converted Markdown string
90
137
  def output_for(node)
91
138
  case node.name
92
- when 'head', 'style', 'script'
139
+ when 'title'
140
+ @title = node.content
141
+ ''
142
+ when 'head'
143
+ output_for_children(node)
144
+ ''
145
+ when 'style', 'script'
93
146
  ''
94
147
  when 'br'
95
148
  " \n"
@@ -106,7 +159,8 @@ module RandomWords
106
159
  "==#{output_for_children(node)}=="
107
160
  when 'blockquote'
108
161
  @section_level += 1
109
- o = "\n\n> #{output_for_children(node).lstrip.gsub("\n", "\n> ")}\n\n".gsub(/> \n(> \n)+/, "> \n")
162
+ o = "\n\n> #{output_for_children(node).lstrip.gsub("\n", "\n> ")}\n\n".gsub(/> \n(> \n)+/, "> \n").sub(/> \n$/,
163
+ "\n")
110
164
  @section_level -= 1
111
165
  o
112
166
  when 'cite'
@@ -130,7 +184,7 @@ module RandomWords
130
184
  @in_pre = false
131
185
  node.text.strip
132
186
  else
133
- "`#{output_for_children(node).gsub("\n", ' ')}`"
187
+ "`#{output_for_children(node).tr("\n", ' ')}`"
134
188
  end
135
189
  when 'pre'
136
190
  @in_pre = true
@@ -138,19 +192,25 @@ module RandomWords
138
192
  lang = '' if lang.nil? || lang.empty?
139
193
  "\n\n```#{lang}\n" + output_for_children(node).lstrip + "\n```\n\n"
140
194
  when 'hr'
141
- "\n\n----\n\n"
195
+ "\n\n* * * *\n\n"
142
196
  when 'a', 'link'
143
197
  link = { href: node['href'], title: node['title'] }
144
- "[#{output_for_children(node).gsub("\n", ' ')}][#{add_link(link)}]"
198
+ if link[:href] =~ /^#fn:(\d+)/
199
+ counter = Regexp.last_match[1]
200
+ add_footnote(counter, link)
201
+ "[^fn#{counter}]"
202
+ else
203
+ "[#{output_for_children(node).tr("\n", ' ')}][#{add_link(link)}]"
204
+ end
145
205
  when 'img'
146
206
  link = { href: node['src'], title: node['title'] }
147
207
  "![#{node['alt']}][#{add_link(link)}]"
148
208
  when 'video', 'audio', 'embed'
149
209
  link = { href: node['src'], title: node['title'] }
150
- "[#{output_for_children(node).gsub("\n", ' ')}][#{add_link(link)}]"
210
+ "[#{output_for_children(node).tr("\n", ' ')}][#{add_link(link)}]"
151
211
  when 'object'
152
212
  link = { href: node['data'], title: node['title'] }
153
- "[#{output_for_children(node).gsub("\n", ' ')}][#{add_link(link)}]"
213
+ "[#{output_for_children(node).tr("\n", ' ')}][#{add_link(link)}]"
154
214
  when 'i', 'em', 'u'
155
215
  "_#{output_for_children(node)}_"
156
216
  when 'b', 'strong'
@@ -176,10 +236,10 @@ module RandomWords
176
236
  header = "\n"
177
237
  if @table_header
178
238
  @table_header = false
179
- cells = node.children.select do |c|
239
+ cells = node.children.count do |c|
180
240
  %w[th td].include?(c.name)
181
- end.count
182
- header = "\n|#{cells.times.map { '-------' }.join('|')}|\n"
241
+ end
242
+ header = "\n|#{Array.new(cells) { '-------' }.join('|')}|\n"
183
243
  end
184
244
  node.children.select do |c|
185
245
  %w[th td].include?(c.name)
@@ -190,7 +250,7 @@ module RandomWords
190
250
  "| #{output_for_children(node)} |"
191
251
  when 'text'
192
252
  # Sometimes Nokogiri lies. Force the encoding back to what we know it is
193
- if (c = node.content.force_encoding(@encoding)) =~ /\S/
253
+ if /\S/.match?((c = node.content.force_encoding(@encoding)))
194
254
  c.gsub!(/\n\n+/, '<$PreserveDouble$>')
195
255
  c.gsub!(/\s+/, ' ')
196
256
  c.gsub('<$PreserveDouble$>', "\n\n")