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.1.2'
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/md_to_bbcode.rb', line 11 def self.md_to_bbcode(markdown) bbcode_lines = [] in_a_list = false in_a_code_block = false in_bold_text = false in_italic_text = false markdown.split("\n").each do |line| unindented_line = in_a_list && line =~ /^ (.*)$/ ? $1 : line if unindented_line =~ /^```(.*)$/ if in_a_code_block # We exit a code block bbcode_lines << "#{in_a_list ? ' ' : ''}[/code]\n" in_a_code_block = false else # We enter a code block bbcode_lines << "#{in_a_list ? ' ' : ''}[code]" in_a_code_block = true end elsif in_a_code_block # Don't change anything at the formatting bbcode_lines << "#{line}\n" else # Handle in-line code markers first line = line.split(/(`.+?`)/).map do |field| if field[0] == '`' # Don't change anything at the formatting "#{in_bold_text ? '' : '[b]'}[font=Courier New]#{field[1..-2]}[/font]#{in_bold_text ? '' : '[/b]'}" else # Apply the formatting except the formatting used for whole lines (headers, bullets, lists...) # Images (do this gsub before links and any single tags, like bold, headings...) field.gsub!(/!\[(.*?)\]\((.*?)\)/, '[img]\2[/img]') # Links field.gsub!(/\[(.*?)\]\((.*?)\)/, '[url=\2]\1[/url]') # Bold (do this before italic) if in_bold_text field.gsub!(/\*\*(.*?)\*\*/, '[/b]\1[b]') else field.gsub!(/\*\*(.*?)\*\*/, '[b]\1[/b]') end if field =~ /.*\*\*.*/ if in_bold_text field.gsub!(/(.*)\*\*(.*)/, '\1[/b]\2') in_bold_text = false else field.gsub!(/(.*)\*\*(.*)/, '\1[b]\2') in_bold_text = true end end # Italic if in_italic_text field.gsub!(/\*(\S.*?)\*/, '[/i]\1[i]') else field.gsub!(/\*(\S.*?)\*/, '[i]\1[/i]') end if in_italic_text && field =~ /.*\S\*.*/ field.gsub!(/(.*\S)\*(.*)/, '\1[/i]\2') in_italic_text = false elsif !in_italic_text && field =~ /.*\*\S.*/ field.gsub!(/(.*)\*(\S.*)/, '\1[i]\2') in_italic_text = true end field end end.join # Heading 1 line.gsub!(/^# (.*?)$/, '[size=6][b]\1[/b][/size]') # Heading 2 line.gsub!(/^## (.*?)$/, '[size=6]\1[/size]') # Heading 3 line.gsub!(/^### (.*?)$/, '[size=5][b]\1[/b][/size]') # Heading 4 line.gsub!(/^#### (.*?)$/, '[size=5]\1[/size]') # Bullets if line =~ /^\* (.+)$/ # Single bullet line bbcode_lines << "[list]\n" unless in_a_list bbcode_lines << "[*]#{$1}\n" in_a_list = true elsif line =~ /^\d\. (.+)$/ # Single numbered line bbcode_lines << "[list=1]\n" unless in_a_list bbcode_lines << "[*]#{$1}\n" in_a_list = true else if in_a_list && !(line =~ /^ (.*)$/) bbcode_lines << "[/list]\n" in_a_list = false end bbcode_lines << "#{line}\n" end end end bbcode_lines << "[/list]\n" if in_a_list bbcode = bbcode_lines.join markdown.end_with?("\n") ? bbcode : bbcode[0..-2] end |