Class: RubyMark

Inherits:
Object
  • Object
show all
Defined in:
lib/rubymark.rb

Instance Method Summary collapse

Constructor Details

#initialize(markdown) ⇒ RubyMark

Returns a new instance of RubyMark.



2
3
4
# File 'lib/rubymark.rb', line 2

def initialize(markdown)
  @markdown = markdown
end

Instance Method Details

#blockquotes(markdown) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubymark.rb', line 73

def blockquotes(markdown)
  markdown.gsub!(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/) do
	  bq = $1
	  if bq
      bq.gsub!(/^[ \t]*>[ \t]?/, "") # trim one level of quoting
    bq.gsub!(/^[ \t]+$/, "")       # trim whitespace-only lines
    bq.strip!
  
    "<blockquote><p>\n#{bq}\n</p></blockquote>\n\n"
  else
    $&
   end
  end
  markdown
end

#emphasis(markdown) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubymark.rb', line 20

def emphasis(markdown)
  markdown
    .gsub(/__(.*)__/, "<strong>\\1</strong>")
    .gsub(/_(.*)_/, "<em>\\1</em>")
    .gsub(/ <em> /, " _ ")
    .gsub(/ <\/em> /, " _ ")
    .gsub(/\\<em>/, "_")
    .gsub(/\\<\/em>/, "_")
    .gsub(/\*\*(.*)\*\*/, "<strong>\\1</strong>")
    .gsub(/\*(.*)\*/, "<em>\\1</em>")
    .gsub(/ <em> /, " * ")
    .gsub(/ <\/em> /, " * ")
    .gsub(/\\<em>/, "*")
    .gsub(/\\<\/em>/, "*")
end

#headings(markdown) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubymark.rb', line 36

def headings(markdown)
   markdown
      .gsub(/^\#\#\#\#\#\# (.*) \#\#\#\#\#\#/, '<h6>\1</h6>')
      .gsub(/^\#\#\#\#\#\# (.*)/, '<h6>\1</h6>')
      .gsub(/^\#\#\#\#\# (.*) \#\#\#\#\#/, '<h5>\1</h5>')
      .gsub(/^\#\#\#\#\# (.*)/, '<h5>\1</h5>')
      .gsub(/^\#\#\#\# (.*) \#\#\#\#/, '<h4>\1</h4>')
      .gsub(/^\#\#\#\# (.*)/, '<h4>\1</h4>')
      .gsub(/^\#\#\# (.*) \#\#\#/, '<h3>\1</h3>')
      .gsub(/^\#\#\# (.*)/, '<h3>\1</h3>')
      .gsub(/^\#\# (.*) \#\#/, '<h2>\1</h2>')
      .gsub(/^\#\# (.*)/, '<h2>\1</h2>')
      .gsub(/^\# (.*) \#/, '<h1>\1</h1>')
      .gsub(/^\# (.*)/, '<h1>\1</h1>')
end


67
68
69
70
71
# File 'lib/rubymark.rb', line 67

def links(markdown)
  markdown
    .gsub(/\[(.*)\]\((.*) "(.*)"\)/, "<a href=\"\\2\" title=\"\\3\">\\1</a>")
    .gsub(/\[(.*)\]\((.*)\)/, "<a href=\"\\2\">\\1</a>")
end

#ordered_lists(markdown) ⇒ Object



6
7
8
9
10
11
# File 'lib/rubymark.rb', line 6

def ordered_lists(markdown)
  markdown
    .gsub(/\n\n(\d*\. .*)/, "\n<ol>\n\\1")
    .gsub(/^(\d*\. .*)\n\n/, "\\1\n</ol>\n")
    .gsub(/^\d*\. (.*)/, "<li>\\1</li>")
end

#paragraphs(markdown) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubymark.rb', line 52

def paragraphs(markdown)
  markdown
    .gsub(/^\n(.+)\n$/, "\n<p>\\1</p>\n")
    .gsub(/\A(.+)/, "<p>\\1")
    .gsub(/(.+)\Z/, "\\1</p>")
    .gsub(/\n\n/, "</p>\n\n<p>")
    .gsub(/\n\n<p><\/p>\n\n/, "\n\n")
    .gsub(/\A<\/p>/, "")
    .gsub(/<p>\Z/, "")
    .gsub(/<p>(<.*>)/, "\\1")
    .gsub(/(<.*>)<\/p>/, "\\1")
    .gsub(/<p>(<.*>)/, "\\1")
    .gsub(/(<.*>)<\/p>/, "\\1")
end

#to_htmlObject



89
90
91
# File 'lib/rubymark.rb', line 89

def to_html
  links(paragraphs(headings(emphasis(unordered_lists(ordered_lists(blockquotes(@markdown)))))))
end

#unordered_lists(markdown) ⇒ Object



13
14
15
16
17
18
# File 'lib/rubymark.rb', line 13

def unordered_lists(markdown)
  markdown
    .gsub(/\n\n(\* .*)/, "<ul>\n\\1")
    .gsub(/^(\* .*)\n\n/, "\\1\n</ul>\n\n")
    .gsub(/^\* (.*)/, "<li>\\1</li>")
end