Module: MdToBbcode
- Defined in:
- lib/md_to_bbcode.rb,
lib/md_to_bbcode/version.rb,
lib/md_to_bbcode/core_extensions/string/md_to_bbcode.rb
Defined Under Namespace
Modules: CoreExtensions
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.md_to_bbcode(markdown) ⇒ Object
Convert a Markdown string to Bbcode.
Class Method Details
.md_to_bbcode(markdown) ⇒ Object
Convert a Markdown string to Bbcode
- Parameters
-
markdown (String): The Markdown string
- Result
-
String: BBCode converted string
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/md_to_bbcode.rb', line 11 def self.md_to_bbcode(markdown) bbcode_lines = [] already_in_list = false markdown. # Images (do this gsub before links and any single tags, like bold, headings...) gsub(/!\[(.*?)\]\((.*?)\)/, '[img]\2[/img]'). # Links gsub(/\[(.*?)\]\((.*?)\)/, '[url=\2]\1[/url]'). # Bold gsub(/\*\*(.*?)\*\*/m, '[b]\1[/b]'). # Heading 1 gsub(/^# (.*?)$/, '[size=6][b]\1[/b][/size]'). # Heading 2 gsub(/^## (.*?)$/, '[size=6]\1[/size]'). # Heading 3 gsub(/^### (.*?)$/, '[size=5][b]\1[/b][/size]'). # Heading 4 gsub(/^#### (.*?)$/, '[size=5]\1[/size]'). # Code blocks (do this before in-line code) gsub(/```(.*?)\n(.*?)```/m, "[code]\\2[/code]"). # In-line code gsub(/`(.*?)`/, '[b][font=Courier New]\1[/font][/b]'). # Perform lists transformations split("\n").each do |line| if line =~ /^\* (.+)$/ # Single bullet line bbcode_lines << '[list]' unless already_in_list bbcode_lines << "[*]#{$1}" already_in_list = true elsif line =~ /^\d\. (.+)$/ # Single numbered line bbcode_lines << '[list=1]' unless already_in_list bbcode_lines << "[*]#{$1}" already_in_list = true else if already_in_list && !(line =~ /^ (.*)$/) bbcode_lines << '[/list]' already_in_list = false end bbcode_lines << line end end bbcode_lines << '[/list]' if already_in_list bbcode_lines << '' if markdown.end_with?("\n") bbcode_lines.join("\n") end |