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) ⇒ 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)
  # 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}")
    # 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)
  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_htmlObject



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

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

#inner_markdownObject



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

def inner_markdown
  contents.elements.collect do |node|
    recursively_convert(node, :to_markdown)
  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_htmlObject



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

def to_html
  convert :html
end

#to_markdownObject



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

def to_markdown
  convert :markdown
end

#wrap_html(t) ⇒ Object



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

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

#wrap_markdown(t) ⇒ Object



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

def wrap_markdown(t)
  t + inner_markdown + t
end