# File lib/asciidoctor/substitutors.rb, line 988
  def sub_inline_xrefs(text, found = nil)
    if ((found ? found[:macroish] : (text.include? '[')) && (text.include? 'xref:')) ||
        ((text.include? '&') && (text.include? '<<'))
      text = text.gsub(InlineXrefMacroRx) {
        # alias match for Ruby 1.8.7 compat
        m = $~
        # honor the escape
        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

        # handles: #id
        if target
          refid = fragment
        # handles: path#, path.adoc#, path#id, or path.adoc#id
        elsif path
          if (ext_idx = path.rindex '.') && ASCIIDOC_EXTENSIONS[path.slice ext_idx, path.length]
            path = path.slice 0, ext_idx
          end
          # the referenced path is this document, or its contents has been included in this document
          if @document.attributes['docname'] == path || @document.catalog[:includes].include?(path)
            refid, path, target = fragment, nil, %(##{fragment})
          else
            refid = fragment ? %(#{path}##{fragment}) : path
            path = %(#{@document.attributes['relfileprefix']}#{path}#{@document.attributes.fetch 'outfilesuffix', '.html'})
            target = fragment ? %(#{path}##{fragment}) : path
          end
        # handles: id or Section Title
        else
          # resolve fragment as reftext if it's not a known ID and resembles reftext (includes space or has uppercase char)
          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