lib/markdown/heading.rb in tbpgr_utils-0.0.105 vs lib/markdown/heading.rb in tbpgr_utils-0.0.106
- old
+ new
@@ -1,36 +1,32 @@
-# encoding: utf-8
-
-class MarkdownString
- # Return markdown heading level1 from text
- #
- # === Example
- #
- # MarkdownString.heading1("title") # => "# title"
- # MarkdownString.heading1("") # => "# "
- # MarkdownString.heading1(nil) # => "# "
- # MarkdownString.heading1(12345) # => "# 12345"
- #
- def self.heading1(text)
- heading(text, 1)
- end
-
- # Return markdown heading level2 from text
- #
- # === Example
- #
- # MarkdownString.heading2("title") # => "## title"
- # MarkdownString.heading2("") # => "## "
- # MarkdownString.heading2(nil) # => "## "
- # MarkdownString.heading2(12345) # => "## 12345"
- #
- def self.heading2(text)
- heading(text, 2)
- end
-
- private
- def self.heading(text, level)
- return '#' * level + ' ' if text.nil?
- return '#' * level + ' ' if text.respond_to?(:empty) && text.empty?
- '#' * level + " #{text.to_s}"
- end
-end
+# encoding: utf-8
+
+class MarkdownString
+ class << self
+ # Return markdown heading level1-6 from text
+ #
+ # === Example
+ #
+ # MarkdownString.heading1("title") # => "# title"
+ # MarkdownString.heading2("title") # => "## title"
+ # MarkdownString.heading3("title") # => "### title"
+ # MarkdownString.heading4("title") # => "#### title"
+ # MarkdownString.heading5("title") # => "##### title"
+ # MarkdownString.heading6("title") # => "###### title"
+ # MarkdownString.heading1("") # => "# "
+ # MarkdownString.heading1(nil) # => "# "
+ # MarkdownString.heading1(12345) # => "# 12345"
+ #
+ [*1..6].each do |i|
+ define_method "heading#{i.to_s}".to_sym do |text|
+ heading(text, i)
+ end
+ end
+ end
+
+ private
+ def self.heading(text, level)
+ return '#' * level + ' ' if text.nil?
+ return '#' * level + ' ' if text.respond_to?(:empty) && text.empty?
+ '#' * level + " #{text.to_s}"
+ end
+end