Class: Kramdown::Converter::Bbcode

Inherits:
Base
  • Object
show all
Includes:
Utils::Html
Defined in:
lib/kramdown/converter/bbcode.rb

Overview

Outputs some of the most common Markdown elements as BBCode. Everything in this class is internal.

Direct Known Subclasses

Htlal

Constant Summary collapse

DISPATCHER =

:nodoc:

Hash.new {|h,k| h[k] = "convert_#{k}"}

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ Bbcode

Returns a new instance of Bbcode.



10
11
12
13
# File 'lib/kramdown/converter/bbcode.rb', line 10

def initialize(root, options)
  super
  @stack = []
end

Instance Method Details

#convert(el, opts = {}) ⇒ Object



15
16
17
# File 'lib/kramdown/converter/bbcode.rb', line 15

def convert(el, opts = {})
  send(DISPATCHER[el.type], el, opts)
end

#convert_a(el, opts) ⇒ Object



53
54
55
# File 'lib/kramdown/converter/bbcode.rb', line 53

def convert_a(el, opts)
  tag("url", el.attr['href'], inner(el, opts))
end

#convert_blank(el, opts) ⇒ Object



36
37
38
# File 'lib/kramdown/converter/bbcode.rb', line 36

def convert_blank(el, opts)
  "\n"
end

#convert_blockquote(el, opts) ⇒ Object



81
82
83
84
85
# File 'lib/kramdown/converter/bbcode.rb', line 81

def convert_blockquote(el, opts)
  # No newline because BBCode will add one itself.  We also run
  # results_to_text so we can clean up trailing newlines.
  tag("quote", nil, results_to_text(inner(el, opts)))
end

#convert_codeblock(el, opts) ⇒ Object



87
88
89
90
# File 'lib/kramdown/converter/bbcode.rb', line 87

def convert_codeblock(el, opts)
  # No newline because BBCode will add one itself.
  tag("code", nil, el.value.sub(/\n\z/, ''))
end

#convert_em(el, opts) ⇒ Object



45
46
47
# File 'lib/kramdown/converter/bbcode.rb', line 45

def convert_em(el, opts)
  tag("i", nil, inner(el, opts))
end

#convert_header(el, opts) ⇒ Object



28
29
30
# File 'lib/kramdown/converter/bbcode.rb', line 28

def convert_header(el, opts)
  tag("b", nil, inner(el, opts)) + ["\n"]
end

#convert_img(el, opts) ⇒ Object



92
93
94
# File 'lib/kramdown/converter/bbcode.rb', line 92

def convert_img(el, opts)
  tag("img", nil, el.attr['src'])
end

#convert_p(el, opts) ⇒ Object



32
33
34
# File 'lib/kramdown/converter/bbcode.rb', line 32

def convert_p(el, opts)
  inner(el, opts) + ["\n"]
end

#convert_root(el, opts) ⇒ Object



96
97
98
# File 'lib/kramdown/converter/bbcode.rb', line 96

def convert_root(el, opts)
  results_to_text(inner(el, opts))
end

#convert_smart_quote(el, opts) ⇒ Object



57
58
59
# File 'lib/kramdown/converter/bbcode.rb', line 57

def convert_smart_quote(el, opts)
  entity_to_str(smart_quote_entity(el))
end

#convert_strong(el, opts) ⇒ Object



49
50
51
# File 'lib/kramdown/converter/bbcode.rb', line 49

def convert_strong(el, opts)
  tag("b", nil, inner(el, opts))
end

#convert_table(el, opts) ⇒ Object



61
62
63
# File 'lib/kramdown/converter/bbcode.rb', line 61

def convert_table(el, opts)
  tag("table", nil, ["\n"] + inner(el, opts))
end

#convert_tbody(el, opts) ⇒ Object



65
66
67
# File 'lib/kramdown/converter/bbcode.rb', line 65

def convert_tbody(el, opts)
  inner(el, opts)
end

#convert_td(el, opts) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/kramdown/converter/bbcode.rb', line 73

def convert_td(el, opts)
  if @stack.last.first.attr['class'] == 'hgls-h'
    tag("td", nil, tag("size", "24", inner(el, opts)))
  else
    tag("td", nil, inner(el, opts))
  end
end

#convert_text(el, opts) ⇒ Object



40
41
42
43
# File 'lib/kramdown/converter/bbcode.rb', line 40

def convert_text(el, opts)
  # Wouldn't it be nice if we could escape BBCode?
  el.value.gsub(/\s+/, ' ') # Ignore newlines in raw text.
end

#convert_tr(el, opts) ⇒ Object



69
70
71
# File 'lib/kramdown/converter/bbcode.rb', line 69

def convert_tr(el, opts)
  tag("tr", nil, inner(el, opts)) + ["\n"]
end

#inner(el, opts) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/kramdown/converter/bbcode.rb', line 19

def inner(el, opts)
  @stack.push([el, opts])
  result = el.children.map do |inner_el|
    convert(inner_el, options)
  end
  @stack.pop
  result
end

#results_to_text(results) ⇒ Object



108
109
110
# File 'lib/kramdown/converter/bbcode.rb', line 108

def results_to_text(results)
  results.flatten.compact.join.sub(/\n+\z/, '')
end

#tag(name, arg, content) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/kramdown/converter/bbcode.rb', line 100

def tag(name, arg, content)
  if arg
    ["[#{name}=#{arg}]", content, "[/#{name}]"]
  else
    ["[#{name}]", content, "[/#{name}]"]
  end
end