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
- #contents ⇒ Object
- #convert(output_format) ⇒ Object
- #inner_bbcode ⇒ Object
- #inner_html ⇒ Object
- #inner_markdown ⇒ Object
- #tag_name ⇒ Object
- #to_html ⇒ Object
- #to_markdown ⇒ Object
- #wrap_html(t) ⇒ Object
- #wrap_markdown(t) ⇒ Object
Methods included from RecursiveConversion
Instance Method Details
#contents ⇒ Object
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_bbcode ⇒ Object
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 ⇒ Object
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_markdown ⇒ Object
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_name ⇒ Object
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 ⇒ Object
258 259 260 |
# File 'lib/rbbcode/node_extensions.rb', line 258 def to_html convert :html end |
#to_markdown ⇒ Object
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 |