1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
# frozen_string_literal: true
module Faker
class Markdown < Base
class << self
##
# Produces a random header format.
#
# @return [String]
#
# @example
# Faker::Markdown.headers #=> "##### Autem"
#
# @faker.version 1.8.0
def headers
"#{fetch('markdown.headers')} #{Lorem.word.capitalize}"
end
##
# Produces a random emphasis formatting on a random word in two sentences.
#
# @return [String]
#
# @example
# Faker::Markdown.emphasis #=> "_Incidunt atque quis repellat id impedit. Quas numquam quod incidunt dicta non. Blanditiis delectus laudantium atque reiciendis qui._"
#
# @faker.version 1.8.0
def emphasis
paragraph = Faker::Lorem.paragraph(sentence_count: 3)
words = paragraph.split
position = rand(0..(words.length - 1))
formatting = fetch('markdown.emphasis')
words[position] = "#{formatting}#{words[position]}#{formatting}"
words.join(' ')
end
##
# Produces a random ordered list of items between 1 and 10 randomly.
#
# @return [String]
#
# @example
# Faker::Markdown.ordered_list #=> "1. Qui reiciendis non consequatur atque.\n2. Quo doloremque veritatis tempora aut.\n3. Aspernatur.\n4. Ea ab.\n5. Qui.\n6. Sit pariatur nemo eveniet.\n7. Molestiae aut.\n8. Nihil molestias iure placeat.\n9. Dolore autem quisquam."
#
# @faker.version 1.8.0
def ordered_list
number = rand(1..10)
result = []
number.times do |i|
result << "#{i}. #{Faker::Lorem.sentence(word_count: 1)} \n"
end
result.join
end
##
# Produces a random unordered list of items between 1 and 10 randomly.
#
# @return [String]
#
# @example
# Faker::Markdown.unordered_list #=> "* Voluptatum aliquid tempora molestiae facilis non sed.\n* Nostrum omnis iste impedit voluptatum dolor.\n* Esse quidem et facere."
#
# @faker.version 1.8.0
def unordered_list
number = rand(1..10)
result = []
number.times do |_i|
result << "* #{Faker::Lorem.sentence(word_count: 1)} \n"
end
result.join
end
##
# Produces a random inline code snippet between two sentences.
#
# @return [String]
#
# @example
# Faker::Markdown.inline_code #=> "Aut eos quis suscipit. `Dignissimos voluptatem expedita qui.` Quo doloremque veritatis tempora aut."
#
# @faker.version 1.8.0
def inline_code
"`#{Faker::Lorem.sentence(word_count: 1)}`"
end
##
# Produces a random code block formatted in Ruby.
#
# @return [String]
#
# @example
# Faker::Markdown.block_code #=> "```ruby\nEos quasi qui.\n```"
#
# @faker.version 1.8.0
def block_code
"```ruby\n#{Lorem.sentence(word_count: 1)}\n```"
end
##
# Produces a random 3x4 table with a row of headings, a row of hyphens and two rows of data
#
# @return [String]
#
# @example
# Faker::Markdown.table #=> "ad | similique | voluptatem\n---- | ---- | ----\ncorrupti | est | rerum\nmolestiae | quidem | et"
#
# @faker.version 1.8.0
def table
table = []
3.times do
table << "#{Lorem.word} | #{Lorem.word} | #{Lorem.word}"
end
table.insert(1, '---- | ---- | ----')
table.join("\n")
end
##
# Produces a random method from the methods above, excluding the methods listed in the arguments.
#
# @overload random(methods)
# @param methods [Symbol] Specify which methods to exclude.
#
# @return [String, Array<String>]
#
# @example
# Faker::Markdown.random #=> returns output from a single method outlined above
# Faker::Markdown.random("table") #=> returns output from any single method outlined above except for "table"
# Faker::Markdown.random("ordered_list", "unordered_list") #=> returns output from any single method outlined above except for either ordered_list and unordered_list
#
# @faker.version 1.8.0
def random(*args)
method_list = available_methods
args&.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } }
send(method_list[Faker::Config.random.rand(0..(method_list.length - 1))])
end
##
# Produces a simulated blog-esque text-heavy block in markdown
#
# Keyword arguments: sentences, repeat
# @param sentences [Integer] Specifies how many sentences make a text block.
# @param repeat [Integer] Specifies how many times the text block repeats.
# @return [String]
#
# @example
# Faker::Markdown.sandwich #=> returns newline separated content of 1 header, 1 default lorem paragraph, and 1 random markdown element
# Faker::Markdown.sandwich(sentences: 5) #=> returns newline separated content of 1 header, 1 5-sentence lorem paragraph, and 1 random markdown element
# Faker::Markdown.sandwich(sentences: 6, repeat: 3) #=> returns newline separated content of 1 header, and then 3 sections consisting of, here, 1 6-sentence lorem paragraph and 1 random markdown element. The random markdown element is chosen at random in each iteration of the paragraph-markdown pairing.
#
# @faker.version 1.8.0
def sandwich(sentences: 3, repeat: 1)
text_block = []
text_block << headers
repeat.times do
text_block << Faker::Lorem.paragraph(sentence_count: sentences)
text_block << random
end
text_block.join("\n")
end
private
def available_methods
(Markdown.public_methods(false) - Base.methods).sort
end
end
end
end
|