Module: MCMarkdown::Formatter::Links

Included in:
Base
Defined in:
lib/mc_markdown/formatters/links.rb

Constant Summary collapse

/^ ([^_{}]+) ({(.+?)})? (\s\_[a-zA-Z]+)? \s?$/x

Instance Method Summary collapse

Instance Method Details

add ability to add classes to links [Link Test class _target ](link)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mc_markdown/formatters/links.rb', line 15

def link link, title, content
  return "[#{content}](#{link}#{(title && !title.empty?) ? " \"#{title}\"" : ''})" if extensions[:no_links]

  match_data = LINK_ATTRIBUTES_PATTERN.match content
  content    = match_data[1]
  attrs      = match_data[3] || ""
  target     = match_data[4]

  # 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?
    attrs = "class='#{attrs}'"
  end

  if extensions[:xml]
    link.encode!(xml: :text)
    # content gets loosely encoded before it
    # enters the formatter
  end

  # there is always a link
  return_string = "<a href='#{link}'"
  return_string << " " << attrs                if !attrs.empty?
  return_string << " target='#{target.strip}'" if target
  return_string << ">#{content.strip}</a>"

  return return_string
end