Class: MdToBbcode::BbcodeRenderer
- Inherits:
-
Redcarpet::Render::StripDown
- Object
- Redcarpet::Render::StripDown
- MdToBbcode::BbcodeRenderer
- Defined in:
- lib/md_to_bbcode/bbcode_renderer.rb
Constant Summary collapse
- MARKER_BOLD =
'--MdToBbcode--Bold--'- MARKER_ITALIC =
'--MdToBbcode--Italic--'
Instance Method Summary collapse
- #block_code(code, language) ⇒ Object
- #codespan(code) ⇒ Object
- #double_emphasis(text) ⇒ Object
- #emphasis(text) ⇒ Object
- #header(text, header_level) ⇒ Object
- #image(link, title, alt_text) ⇒ Object
- #link(link, title, content) ⇒ Object
- #list(contents, list_type) ⇒ Object
- #list_item(text, list_type) ⇒ Object
- #postprocess(doc) ⇒ Object
- #preprocess(doc) ⇒ Object
Instance Method Details
#block_code(code, language) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 118 def block_code(code, language) "[code]#{code. gsub(MARKER_BOLD, '**'). gsub(MARKER_ITALIC, '*') }[/code]\n" end |
#codespan(code) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 93 def codespan(code) "[b][font=Courier New]#{code. gsub(MARKER_BOLD, '**'). gsub(MARKER_ITALIC, '*') }[/font][/b]" end |
#double_emphasis(text) ⇒ Object
69 70 71 72 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 69 def double_emphasis(text) # In case the text already contains bold tags (which can be the case as codespan uses them), remove them. "[b]#{text.gsub('[b]', '').gsub('[/b]', '')}[/b]" end |
#emphasis(text) ⇒ Object
65 66 67 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 65 def emphasis(text) "[i]#{text}[/i]" end |
#header(text, header_level) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 78 def header(text, header_level) case header_level when 1 "\n[size=6][b]#{text}[/b][/size]\n\n" when 2 "\n[size=6]#{text}[/size]\n\n" when 3 "\n[size=5][b]#{text}[/b][/size]\n\n" when 4 "\n[size=5]#{text}[/size]\n\n" else "\n[size=4][b]#{text}[/b][/size]\n\n" end end |
#image(link, title, alt_text) ⇒ Object
100 101 102 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 100 def image(link, title, alt_text) "[img]#{link}[/img]" end |
#link(link, title, content) ⇒ Object
74 75 76 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 74 def link(link, title, content) "[url=#{link}]#{content}[/url]" end |
#list(contents, list_type) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 104 def list(contents, list_type) case list_type when :ordered "[list=1]\n#{contents}[/list]\n" else "[list]\n#{contents}[/list]\n" end end |
#list_item(text, list_type) ⇒ Object
113 114 115 116 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 113 def list_item(text, list_type) # If the item spans multiple lines, prefix each new line with 2 spaces to keep the list running. "[*]#{text.split(/(?<=\n)/).join(" ")}" end |
#postprocess(doc) ⇒ Object
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 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 20 def postprocess(doc) # Convert bold and italic correctly due to bug https://github.com/vmg/redcarpet/issues/396 bbcode_lines = [] in_bold_text = false in_italic_text = false # Due to Redcarpet bug https://github.com/vmg/redcarpet/issues/600 we have to change some in-line code to blocks # TODO: Remove when Redcarpet will be fixed doc.split("\n").map do |line| case line when /^(\s*)\[b\]\[font=Courier New\]([^\[]*)$/ "#{$1}[code]" when /^(\s*)\[\/font\]\[\/b\]$/ "#{$1}[/code]" else # Replace the bold markers with [b] or [/b], and # make sure we remove occurences of bold text ([b] and [/b] already part of the text) when bold is already applied: this can happen as we use bold text for inline code # TODO: Remove this when https://github.com/vmg/redcarpet/issues/396 will be corrected fields = line.split(MARKER_BOLD) + (line.end_with?(MARKER_BOLD) ? [''] : []) line = fields.map.with_index do |field, idx| content = in_bold_text ? field.gsub('[b]', '').gsub('[/b]', '') : field if idx == fields.size - 1 content else in_bold_text = !in_bold_text "#{content}[#{in_bold_text ? '' : '/'}b]" end end.join # Italic if in_italic_text line.gsub!(/#{Regexp.escape(MARKER_ITALIC)}(\S.*?)#{Regexp.escape(MARKER_ITALIC)}/, '[/i]\1[i]') else line.gsub!(/#{Regexp.escape(MARKER_ITALIC)}(\S.*?)#{Regexp.escape(MARKER_ITALIC)}/, '[i]\1[/i]') end if in_italic_text && line =~ /.*\S#{Regexp.escape(MARKER_ITALIC)}.*/ line.gsub!(/(.*\S)#{Regexp.escape(MARKER_ITALIC)}(.*)/, '\1[/i]\2') in_italic_text = false elsif !in_italic_text && line =~ /.*#{Regexp.escape(MARKER_ITALIC)}\S.*/ line.gsub!(/(.*)#{Regexp.escape(MARKER_ITALIC)}(\S.*)/, '\1[i]\2') in_italic_text = true end line end end.join("\n").gsub('<br>', "\n") end |
#preprocess(doc) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/md_to_bbcode/bbcode_renderer.rb', line 10 def preprocess(doc) # Due to Redcarpet bug https://github.com/vmg/redcarpet/issues/396 we have to handle bold and italic conversions before hand # Be careful to mark them uniquely so that we don't convert them if they were in a code block # TODO: Remove this when Redcarpet bug will be fixed doc. gsub('**', MARKER_BOLD). gsub(/\*(\S|$)/, "#{MARKER_ITALIC}\\1"). gsub(/(\S)\*/, "\\1#{MARKER_ITALIC}") end |