class MarkdownString

Public Class Methods

backquotes(text) click to toggle source

Return markdown backquotes from text

Example

MarkdownString.backquotes "hoge\nhige\nhage\n"

result

>hoge
>hige
>hage
# File lib/markdown/backquotes.rb, line 16
def self.backquotes(text)
  return '>' if text.nil?
  return text unless text.is_a?(String)
  return '>' if text.empty?
  text.split("\n").reduce([]) do |ret, elm|
    ret << ">#{elm}  "
    ret
  end.join("\n") + "\n"
end
bold(text) click to toggle source

Return markdown bold from text

Example

case list

MarkdownString.bold("hoge") # '**hoge**'
MarkdownString.bold("") # '****'
MarkdownString.bold(nil) # '****'
# File lib/markdown/bold.rb, line 14
def self.bold(text)
  return '****' if text.nil?
  return text unless text.is_a?(String)
  return '****' if text.empty?
  "**#{text}**"
end
code(text) click to toggle source

Return markdown code from text

Example

MarkdownString.code('print "hoge"') # => '`print "hoge"`'
# File lib/markdown/code.rb, line 10
def self.code(text)
  return '``' if text.nil?
  return text unless text.is_a?(String)
  return '``' if text.empty?
  "`#{text}`"
end
codes(text, lang = nil) click to toggle source

Return markdown codes from text

Example

MarkdownString.codes("class Hoge\n  def hoge\n    'hoge'\n  end\nend\n")

result

~~~ruby
class Hoge
  def hoge
    'hoge'
  end
end
~~~
# File lib/markdown/codes.rb, line 20
def self.codes(text, lang = nil)
  lang = '' if lang.nil?
  text = '' if text.nil?
  return text unless text.is_a?(String)
  "~~~#{lang}\n#{text}\n~~~\n"
end
hr() click to toggle source

Return markdown hr

Example

case list

MarkdownString.hr # => '---'
# File lib/markdown/hr.rb, line 12
def self.hr
  '---'
end
italic(text) click to toggle source

Return markdown italic from text

Example

case list

MarkdownString.italic(%w{a b c})

resitalict

* a
* b
* c

case not list

MarkdownString.italic("test") # => "test"

case nil list

MarkdownString.italic([nil, nil])

resitalict

*
*

case empty list

MarkdownString.italic([]) # => ""
# File lib/markdown/italic.rb, line 35
def self.italic(text)
  return '**' if text.nil?
  return text unless text.is_a?(String)
  return '**' if text.empty?
  "*#{text}*"
end
ol(list) click to toggle source

Return markdown ol from text

Example

case list

MarkdownString.ol(%w{a b c})

resolt

1. a
1. b
1. c

case not list

MarkdownString.ol("test") # => "test"

case nil list

MarkdownString.ol([nil, nil])

resolt

1.
1.

case empty list

MarkdownString.ol([]) # => ""
# File lib/markdown/ol.rb, line 35
def self.ol(list)
  return list unless list.is_a?(Array)
  return '' if list.empty?
  list.reduce([]) do |ret, elm|
    elm = '' if elm.nil?
    ret << "1. #{elm}"
    ret
  end.join("\n") + "\n"
end
ul(list) click to toggle source

Return markdown ul from text

Example

case list

MarkdownString.ul(%w{a b c})

result

* a
* b
* c

case not list

MarkdownString.ul("test") # => "test"

case nil list

MarkdownString.ul([nil, nil])

result

*
*

case empty list

MarkdownString.ul([]) # => ""
# File lib/markdown/ul.rb, line 35
def self.ul(list)
  return list unless list.is_a?(Array)
  return '' if list.empty?
  list.reduce([]) do |ret, elm|
    elm = '' if elm.nil?
    ret << "* #{elm}"
    ret
  end.join("\n") + "\n"
end

Private Class Methods

heading(text, level) click to toggle source
# File lib/markdown/heading.rb, line 27
def self.heading(text, level)
  return '#' * level + ' ' if text.nil?
  return '#' * level + ' ' if text.respond_to?(:empty) && text.empty?
  '#' * level + " #{text}"
end