def sub_inline_xrefs(text, found = nil)
if ((found ? found[:macroish] : (text.include? '[')) && (text.include? 'xref:')) ||
((text.include? '&') && (text.include? '<<'))
text = text.gsub(InlineXrefMacroRx) {
m = $~
if m[0].start_with? RS
next m[0][1..-1]
end
if (id = m[1])
id, reftext = id.split ',', 2
reftext = reftext.lstrip if reftext
else
id = m[2]
if (reftext = m[3]) && (reftext.include? R_SB)
reftext = reftext.gsub ESC_R_SB, R_SB
end
end
if (hash_idx = id.index '#')
if hash_idx > 0
if (fragment_len = id.length - hash_idx - 1) > 0
path, fragment = (id.slice 0, hash_idx), (id.slice hash_idx + 1, fragment_len)
else
path, fragment = (id.slice 0, hash_idx), nil
end
else
target, path, fragment = id, nil, (id.slice 1, id.length)
end
else
path, fragment = nil, id
end
if target
refid = fragment
elsif path
if (ext_idx = path.rindex '.') && ASCIIDOC_EXTENSIONS[path.slice ext_idx, path.length]
path = path.slice 0, ext_idx
end
if @document.attributes['docname'] == path || @document.catalog[:includes].include?(path)
refid, path, target = fragment, nil, %(##{fragment})
else
refid = fragment ? %(
path = %(#{@document.attributes['relfileprefix']}#{path}#{@document.attributes.fetch 'outfilesuffix', '.html'})
target = fragment ? %(
end
else
unless @document.catalog[:ids].key? fragment
if ((fragment.include? ' ') || fragment.downcase != fragment) &&
(resolved_id = @document.catalog[:ids].key fragment)
fragment = resolved_id
elsif $VERBOSE
warn %(asciidoctor: WARNING: invalid reference: #{fragment})
end
end
refid, target = fragment, %(##{fragment})
end
Inline.new(self, :anchor, reftext, :type => :xref, :target => target, :attributes => {'path' => path, 'fragment' => fragment, 'refid' => refid}).convert
}
end
text
end