def transform_anchors( str, rs )
@log.debug " Transforming anchors"
@scanner.string = str.dup
text = ''
until @scanner.empty?
if @scanner.scan( /\[/ )
link = ''; linkid = ''
depth = 1
startpos = @scanner.pos
@log.debug " Found a bracket-open at %d" % startpos
while depth.nonzero?
linktext = @scanner.scan_until( /\]|\[/ )
if linktext
@log.debug " Found a bracket at depth %d: %p" % [ depth, linktext ]
link += linktext
depth += ( linktext[-1, 1] == ']' ? -1 : 1 )
@log.debug " Depth is now #{depth}"
else
@log.debug " Missing closing brace, assuming non-link."
link += @scanner.rest
@scanner.terminate
return text + '[' + link
end
end
link.slice!( -1 )
@log.debug " Found leading link %p" % link
if @scanner.scan( RefLinkIdRegex )
linkid = @scanner[1]
linkid = link.dup if linkid.empty?
linkid.downcase!
@log.debug " Found a linkid: %p" % linkid
if rs.urls.key?( linkid )
@log.debug " Found link key in the link table: %p" % rs.urls[linkid]
url = escape_md( rs.urls[linkid] )
text += %{<a href="#{url}"}
if rs.titles.key?(linkid)
text += %{ title="%s"} % escape_md( rs.titles[linkid] )
end
text += %{>#{link}</a>}
else
@log.debug " Linkid %p not found in link table" % linkid
@log.debug " Appending original string instead: "
@log.debug "%p" % @scanner.string[ startpos-1 .. @scanner.pos-1 ]
text += @scanner.string[ startpos-1 .. @scanner.pos-1 ]
end
elsif @scanner.scan( InlineLinkRegex )
url = @scanner[1]
title = @scanner[3]
@log.debug " Found an inline link to %p" % url
text += %{<a href="%s"} % escape_md( url )
if title
title.gsub!( /"/, """ )
text += %{ title="%s"} % escape_md( title )
end
text += %{>#{link}</a>}
else
@log.debug "No linkid, so no anchor. Appending literal text."
text += @scanner.string[ startpos-1 .. @scanner.pos-1 ]
end
else
@log.debug " Scanning to the next link from %p" % @scanner.rest
text += @scanner.scan( /[^\[]+/ )
end
end
return text
end