class Kramdown::Parser::Kramdown
def parse_link
Parse the link at the current scanner position. This method is used to parse normal links as
def parse_link result = @src.scan(LINK_START) reset_pos = @src.pos link_type = (result =~ /^!/ ? :img : :a) # no nested links allowed if link_type == :a && (@tree.type == :img || @tree.type == :a || @stack.any? {|t,s| t && (t.type == :img || t.type == :a)}) add_text(result) return end el = Element.new(link_type) stop_re = /\]|!?\[/ count = 1 found = parse_spans(el, stop_re) do case @src.matched when "[", "![" count += 1 when "]" count -= 1 end count - el.children.select {|c| c.type == :img}.size == 0 end if !found || el.children.empty? @src.pos = reset_pos add_text(result) return end alt_text = extract_string(reset_pos...@src.pos) conv_link_id = alt_text.gsub(/(\s|\n)+/m, ' ').gsub(LINK_ID_NON_CHARS, '').downcase @src.scan(stop_re) # reference style link or no link url if @src.scan(LINK_INLINE_ID_RE) || !@src.check(/\(/) link_id = (@src[1] || conv_link_id).downcase if @doc.parse_infos[:link_defs].has_key?(link_id) add_link(el, @doc.parse_infos[:link_defs][link_id].first, @doc.parse_infos[:link_defs][link_id].last, alt_text) else warning("No link definition for link ID '#{link_id}' found") @src.pos = reset_pos add_text(result) end return end # link url in parentheses if @src.scan(/\(<(.*?)>/) link_url = @src[1] if @src.scan(/\)/) add_link(el, link_url, nil, alt_text) return end else link_url = '' re = /\(|\)|\s/ nr_of_brackets = 0 while temp = @src.scan_until(re) link_url += temp case @src.matched when /\s/ break when '(' nr_of_brackets += 1 when ')' nr_of_brackets -= 1 break if nr_of_brackets == 0 end end link_url = link_url[1..-2] if nr_of_brackets == 0 add_link(el, link_url, nil, alt_text) return end end if @src.scan(LINK_INLINE_TITLE_RE) add_link(el, link_url, @src[2], alt_text) else @src.pos = reset_pos add_text(result) end end