Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Markdown module (#630) #835

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ PATH
GEM
remote: https://rubygems.org/
specs:
i18n (0.7.0)
minitest (5.8.3)
rake (10.1.0)
test-unit (2.5.5)
i18n (0.8.1)
minitest (5.10.1)
power_assert (1.0.1)
rake (12.0.0)
test-unit (3.2.3)
power_assert
timecop (0.8.1)

PLATFORMS
Expand All @@ -24,4 +26,4 @@ DEPENDENCIES
timecop

BUNDLED WITH
1.13.6
1.13.1
29 changes: 29 additions & 0 deletions doc/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Faker::Markdown

Generates markdown formatting with Lorem Ipsum text

```ruby
#Headers - generates a random header format
Faker::Markdown.headers #=> "##### Autem"

# Emphasis - generates random emphasis formatting on a random word in two sentences
Faker::Markdown.emphasis #=> "Incidunt atque quis repellat id impedit. Quas numquam quod incidunt dicta non. Blanditiis delectus laudantium atque reiciendis qui."

# Ordered List - generates an ordered list of items between 1 and 10 randomly
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."

# Unordered List - generates an unordered list of items between 1 and 10 randomly
Faker::Markdown.unordered_list #=> "* Voluptatum aliquid tempora molestiae facilis non sed.\n* Nostrum omnis iste impedit voluptatum dolor.\n* Esse quidem et facere."

# Inline code - generates an inline code snippet between two sentences
Faker::Markdown.inline_code #=> "Aut eos quis suscipit. `Dignissimos voluptatem expedita qui.` Quo doloremque veritatis tempora aut."

# Code Block - generates a code block formatted in ruby
Faker::Markdown.block_code #=> "```ruby\nEos quasi qui.\n```"

# Table - generates a 3x3 table
Faker::Markdown.table #=> "ad | similique | voluptatem\ncorrupti | est | rerum\nmolestiae | quidem | et"

# Random - randomly chooses an above method
Faker::Markdown.random # returns output from a single method outlined above
```
1 change: 1 addition & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def unique(max_retries = 10_000)
require 'faker/friends'
require 'faker/rick_and_morty'
require 'faker/matz'
require 'faker/markdown'

require 'extensions/array'
require 'extensions/symbol'
Expand Down
66 changes: 66 additions & 0 deletions lib/faker/markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Faker
class Markdown < Base
class << self

def headers
"#{fetch('markdown.headers')} #{Lorem.word.capitalize}"
end

def emphasis
paragraph = Faker::Lorem.paragraph(3)
words = paragraph.split(' ')
position = rand(0..words.length - 1)
formatting = fetch('markdown.emphasis')
words[position] = "#{formatting}#{words[position]}#{formatting}"
words.join(' ')
end

def ordered_list
number = rand(1..10)

result = []
number.times do |i|
result << "#{i.to_s}. #{Faker::Lorem.sentence(1)} \n"
end
result.join('')
end

def unordered_list
number = rand(1..10)

result = []
number.times do |i|
result << "* #{Faker::Lorem.sentence(1)} \n"
end
result.join('')
end

def inline_code
"`#{Faker::Lorem.sentence(1)}`"
end

def block_code
"```ruby\n#{Lorem.sentence(1)}\n```"
end

def table
table = []
3.times do
table << "#{Lorem.word} | #{Lorem.word} | #{Lorem.word}"
end
table.join("\n")
end

def random
send(available_methods[rand(0..available_methods.length - 1)])
end

private

def available_methods
Markdown.public_methods(false) - Base.methods
end

end
end
end
4 changes: 4 additions & 0 deletions lib/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,7 @@ en:
"Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, 'I was surprised by this feature of the language, so therefore Ruby violates the principle of least surprise.' Wait. Wait. The principle of least surprise is not for you only.",
"Sometimes people jot down pseudo-code on paper. If that pseudo-code runs directly on their computers, its best, isn't it? Ruby tries to be like that, like pseudo-code that runs. Python people say that too."
]

markdown:
headers: ["#", "##", "###", "####", "#####", "######"]
emphasis: ["_", "~", "*", "**"]
71 changes: 71 additions & 0 deletions test/test_faker_markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')

class TestFakerMarkdown < Test::Unit::TestCase
def setup
@tester = Faker::Markdown
end

def test_headers
test_trigger = @tester.headers.split(' ')

assert(test_trigger.length == 2)
assert(test_trigger.first.include?('#'))
end

def test_emphasis
test_trigger = @tester.emphasis.split('')

assert(test_trigger.to_set.intersect?(["_", "~", "*", "**"].to_set))
end

def test_ordered_list
test_trigger = @tester.ordered_list.split("\n")

test_trigger.each do |line|
assert_instance_of(Fixnum, line[0].to_i)
end
end

def test_unordered_list
test_trigger = @tester.unordered_list.split("\n")

test_trigger.each do |line|
assert_equal("*", line[0])
end
end

def test_inline_code
test_trigger = @tester.inline_code.split('')

assert_equal(test_trigger.first, "`")
assert_equal(test_trigger.last, "`")
end

def test_block_code
test_trigger = @tester.block_code.split('')

assert_equal(test_trigger[0], "`")
assert_equal(test_trigger[1], "`")
assert_equal(test_trigger[2], "`")
assert_equal(test_trigger[-1], "`")
assert_equal(test_trigger[-2], "`")
assert_equal(test_trigger[-3], "`")
end

def test_table
test_trigger = @tester.table.split("\n")

test_trigger.each do |table_data|
assert_instance_of(String, table_data)
end

assert_equal(test_trigger.length, 3)
end

def test_random
test_trigger = @tester.random

assert_instance_of( String, test_trigger)
end

end