Class: MarkdownString

Inherits:
Object show all
Defined in:
lib/markdown/hr.rb,
lib/markdown/ol.rb,
lib/markdown/ul.rb,
lib/markdown/bold.rb,
lib/markdown/code.rb,
lib/markdown/link.rb,
lib/markdown/codes.rb,
lib/markdown/italic.rb,
lib/markdown/heading.rb,
lib/markdown/backquotes.rb

Class Method Summary collapse

Class Method Details

.backquotes(text) ⇒ Object

Return markdown backquotes from text

Example

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

result

>hoge
>hige
>hage


16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

Return markdown bold from text

Example

case list

MarkdownString.bold("hoge") # '**hoge**'
MarkdownString.bold("") # '****'
MarkdownString.bold(nil) # '****'


14
15
16
17
18
19
# 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) ⇒ Object

Return markdown code from text

Example

MarkdownString.code('print "hoge"') # => '`print "hoge"`'


10
11
12
13
14
15
# 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) ⇒ Object

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
~~~


20
21
22
23
24
25
# 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

.hrObject

Return markdown hr

Example

case list

MarkdownString.hr # => '---'


12
13
14
# File 'lib/markdown/hr.rb', line 12

def self.hr
  '---'
end

.italic(text) ⇒ Object

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([]) # => ""


35
36
37
38
39
40
# 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

Return markdown link from label and url

Example

case list

MarkdownString.link 'label', 'http://hogehogehoge.com'

result

'[label](http://hogehogehoge.com)'


16
17
18
19
20
21
22
# File 'lib/markdown/link.rb', line 16

def self.link(label, url)
  label = '' if label.nil?
  url = '' if url.nil?
  return label unless label.is_a?(String)
  return url unless url.is_a?(String)
  "[#{label}](#{url})"
end

.ol(list) ⇒ Object

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([]) # => ""


35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

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([]) # => ""


35
36
37
38
39
40
41
42
43
# 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