Module: RbbCode::TagNode

Includes:
RecursiveConversion
Defined in:
lib/rbbcode/node_extensions.rb

Constant Summary collapse

TAG_MAPPINGS =

For each tag name, we can either: (a) map to a simple HTML tag or Markdown character, or (b) invoke a separate Ruby module for more advanced logic.

{
  html: {'b' => 'strong', 'i' => 'em', 'u' => 'u', 'url' => URLTagNode, 'img' => ImgTagNode},
  markdown: {'b' => '**', 'i' => '*', 'u' => UTagNode, 'url' => URLTagNode, 'img' => ImgTagNode}
}

Instance Method Summary collapse

Methods included from RecursiveConversion

#recursively_convert

Instance Method Details

#contentsObject



205
206
207
208
209
# File 'lib/rbbcode/node_extensions.rb', line 205

def contents
  # The first element is the opening tag, the second is everything inside,

  # and the third is the closing tag.

  elements[1] 
end

#convert(output_format, options) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rbbcode/node_extensions.rb', line 239

def convert(output_format, options)
  # Consult TAG_MAPPINGS to decide how to process this type of tag.

  t = TAG_MAPPINGS[output_format][tag_name]
  if t.nil?
    raise "No tag mapping found for #{tag_name}"
  elsif t.is_a?(Module)
    # This type of tag requires more than just a simple mapping from one tag name

    # to another. So we invoke a separate Ruby module.

    extend(t)
    send("#{tag_name}_to_#{output_format}", options)
    # Thus, if our tag_name is"url, and TAG_MAPPINGS points us to URLTagNode,

    # that module must define url_to_html.

  else
    # For this type of tag, a simple mapping from the tag name to a string (such as

    # <i>) suffices.

    send("wrap_#{output_format}", t, options)
  end
end

#inner_bbcodeObject



215
216
217
# File 'lib/rbbcode/node_extensions.rb', line 215

def inner_bbcode
  contents.elements.collect { |e| e.text_value }.join
end

#inner_html(options) ⇒ Object



219
220
221
222
223
# File 'lib/rbbcode/node_extensions.rb', line 219

def inner_html(options)
  contents.elements.collect do |node|
    recursively_convert(node, :to_html, options)
  end.join
end

#inner_markdown(options) ⇒ Object



225
226
227
228
229
# File 'lib/rbbcode/node_extensions.rb', line 225

def inner_markdown(options)
  contents.elements.collect do |node|
    recursively_convert(node, :to_markdown, options)
  end.join
end

#tag_nameObject



211
212
213
# File 'lib/rbbcode/node_extensions.rb', line 211

def tag_name
  elements.first.text_value.slice(1..-2).downcase
end

#to_html(options) ⇒ Object



258
259
260
# File 'lib/rbbcode/node_extensions.rb', line 258

def to_html(options)
  convert :html, options
end

#to_markdown(options) ⇒ Object



262
263
264
# File 'lib/rbbcode/node_extensions.rb', line 262

def to_markdown(options)
  convert :markdown, options
end

#wrap_html(t, options) ⇒ Object



231
232
233
# File 'lib/rbbcode/node_extensions.rb', line 231

def wrap_html(t, options)
  "<#{t}>" + inner_html(options) + "</#{t}>"
end

#wrap_markdown(t, options) ⇒ Object



235
236
237
# File 'lib/rbbcode/node_extensions.rb', line 235

def wrap_markdown(t, options)
  t + inner_markdown(options) + t
end