module MCMarkdown::Formatter::Image

Constants

IMG_ATTRIBUTES_PATTERN

rubular.com/r/lss0C9HqoP $1 => alt $2 => class/attrs

Public Instance Methods

image(link, title, alt_text) click to toggle source

add classes to images and render title as figure/figcaption ![alt {class}](/path/to/img.jpg “caption”)

# File lib/mc_markdown/formatters/image.rb, line 12
def image link, title, alt_text
  return "![#{alt_text}](#{link}#{(title && !title.empty?) ? " \"#{title}\"" : ''})" if extensions[:no_images]

  match_data = IMG_ATTRIBUTES_PATTERN.match (alt_text || "")
  alt_text = match_data[1] || ""
  attrs    = match_data[2] || ""

  # check for attrs in class field
  if attrs.include? ':'
    attrs = attrs.split(', ').each_with_object([]) do |frag, out|
      frag = frag.split ':'
      out.push "#{frag[0].strip}='#{frag[1].strip}'"
      out
    end.join(" ") + " "
  elsif !attrs.empty?
    classes = attrs
    attrs   = "class='#{attrs}' "
  end

  if title
    return "<figure class='img #{classes}'>" +
           "<img src='#{link}' alt='#{alt_text.strip}' />" +
           "<figcaption>" + ::Redcarpet::Markdown.new( self ).render(title).strip + "</figcaption>" +
           "</figure>"
  else
    return "<img src='#{link}' alt='#{alt_text.strip}' #{attrs}/>"
  end
end
postprocess(doc) click to toggle source

need to strip paragraph tags from around figures, has potential to cause invalid markup

Calls superclass method
# File lib/mc_markdown/formatters/image.rb, line 43
def postprocess doc
  doc = doc.gsub( /<p><figure/, '<figure' ).gsub( /<\/figure><\/p>/, '</figure>' )

  if defined?(super)
    return super(doc)
  else
    return doc
  end
end