class DoctorIpsum::Markdown

Public Class Methods

blockquote(input = nil, level = 5) click to toggle source

Simple Blockquote (no Markdown inside…yet)

# File lib/doctor_ipsum/markdown.rb, line 18
def blockquote(input = nil, level = 5)
  input ||= sentences
  [].tap { |x| resolve_md(input).split(' ').each_slice(level) { |s| x << s.join(' ').prepend('> ') } }.join("\n")
end
emphasis(input = nil, level = 1) click to toggle source

Uses underscore

# File lib/doctor_ipsum/markdown.rb, line 11
def emphasis(input = nil, level = 1)
  input ||= words
  underscore = '_'*[0,level,2].sort[1]
  resolve_md(input).prepend(underscore).concat(underscore)
end
entry(*args) click to toggle source

Random markdown blog entry

# File lib/doctor_ipsum/markdown.rb, line 51
def entry(*args)
  out = []
  allowed = ["header","emphasis","paragraphs","blockquote",
             "list","link","image"]
  elements = ( args ? allowed : filter(args.flatten,allowed) )
  out << header if elements.include? 'header'
  rand(3..10).times do
    cmd = elements.sample
    out << (cmd == 'image' ? image(500,500) : send(cmd) ) << paragraphs
  end
  out.join("\n\n")
end
header(input = nil, level = 1) click to toggle source

Uses Atx-style i.e. ## Header

# File lib/doctor_ipsum/markdown.rb, line 5
def header(input = nil, level = 1)
  input ||= words
  resolve_md(input, true).prepend('#'*[0,level,6].sort[1]+' ')
end
horizontal() click to toggle source

10 dashes

# File lib/doctor_ipsum/markdown.rb, line 34
def horizontal
  '-'*10
end
image(width = rand(300..500), height = rand(300..500)) click to toggle source

Images from placehold.it

# File lib/doctor_ipsum/markdown.rb, line 45
def image(width = rand(300..500), height = rand(300..500))
  url = 'placehold.it/'+width.to_s+'x'+height.to_s
  basic_format( nil, url, nil).prepend('!')
end
list(input = nil, level = 2, ordered = false) click to toggle source

Uses *

# File lib/doctor_ipsum/markdown.rb, line 24
def list(input = nil, level = 2, ordered = false)
  input ||= words(10)
  [].tap { |x|
    resolve_md(input).split(' ').each_slice(level).with_index { |s,i|
      x << s.join(' ').prepend((ordered ? (i+1).to_s+'.' : '*')+' ')
    }
  }.join("\n")
end

Private Class Methods

basic_format( text = nil, url = nil, tag = nil ) click to toggle source
# File lib/doctor_ipsum/markdown.rb, line 72
def self.basic_format( text = nil, url = nil, tag = nil )
  text ||= sentence.chomp('.')
  url  ||= 'google.com'
  tag  ||= word.join(' ')
  "[" + text + "](http://" + url + ' "' + tag + '")'
end
filter(arr, allowed) click to toggle source
# File lib/doctor_ipsum/markdown.rb, line 68
def self.filter(arr, allowed)
  arr.keep_if { |x| allowed.include? x }
end
resolve_md(input, title=false) click to toggle source
# File lib/doctor_ipsum/markdown.rb, line 79
def self.resolve_md(input, title=false)
  case input
    when Array then title ? input.map {|x| x.capitalize}.join(' ') : input.join(' ')
    else title ? input.split(' ').map {|x| x.capitalize}.join(' ') : input
  end
end