class Kramdown::Converter::Rfc2629

def convert_img(el, indent, opts) # misuse the tag!

misuse the tag!
def convert_img(el, indent, opts) # misuse the tag!
  if a = el.attr
    alt = a.delete('alt').strip
    alt = '' if alt == '!' # work around re-wrap uglyness
    if src = a.delete('src')
      a['target'] = src
    end
  end
  if alt == ":include:"   # Really bad misuse of tag...
    ann = el.attr.delete('ann')
    anchor = el.attr.delete('anchor') || (
      # not yet
      warn "*** missing anchor for '#{src}'"
      src
    )
    anchor = ::Kramdown::Parser::RFC2629Kramdown.idref_cleanup(anchor)
    anchor.gsub!('/', '_')              # should take out all illegals
    to_insert = ""
    src.scan(/(W3C|3GPP|[A-Z-]+)[.]?([A-Za-z_0-9.\(\)\/\+-]+)/) do |t, n|
      never_altproc = n.sub!(/^[.]/, "")
      fn = "reference.#{t}.#{n}.xml"
      sub, ttl, _can_anchor, altproc, always_altproc = XML_RESOURCE_ORG_MAP[t]
      ttl ||= KRAMDOWN_REFCACHETTL  # everything but RFCs might change a lot
      puts "*** Huh: #{fn}" unless sub
      if altproc && !never_altproc && (!KRAMDOWN_USE_TOOLS_SERVER || always_altproc)
        fn, url = altproc.call(fn, n)
      else
        url = "#{XML_RESOURCE_ORG_PREFIX}/#{sub}/#{fn}"
        fn = "alt-#{fn}" if never_altproc || KRAMDOWN_USE_TOOLS_SERVER
      end
      # if can_anchor # create anchor server-side for stand_alone: false
      #   url << "?anchor=#{anchor}"
      #   fn[/.xml$/] = "--anchor=#{anchor}.xml"
      # end
      to_insert = get_and_cache_resource(url, fn.gsub('/', '_'), ttl)
      to_insert.scrub! rescue nil # only do this for Ruby >= 2.1
      begin
        d = REXML::Document.new(to_insert)
        d.xml_decl.nowrite
        d.delete d.doctype
        d.context[:attribute_quote] = :quote  # Set double-quote as the attribute value delimiter
        d.root.attributes["anchor"] = anchor
        if t == "RFC" or t == "I-D"
          if KRAMDOWN_NO_TARGETS || !KRAMDOWN_KEEP_TARGETS
            d.root.attributes["target"] = nil
            REXML::XPath.each(d.root, "/reference/format") { |x|
              d.root.delete_element(x)
            }
          else
            REXML::XPath.each(d.root, "/reference/format") { |x|
              x.attributes["target"].sub!(%r{https?://www.ietf.org/internet-drafts/},
                                          %{https://www.ietf.org/archive/id/}) if t == "I-D"
            }
          end
        elsif t == "IANA"
          d.root.attributes["target"].sub!(%r{\Ahttp://www.iana.org/assignments/}, 'https://www.iana.org/assignments/')
        end
        if ann
          el = ::Kramdown::Converter::Rfc2629::process_markdown_to_rexml(ann).root
          el.name = "annotation"
          d.root.add_element(el)
        end
        to_insert = d.to_s
      rescue Exception => e
        warn "** Can't manipulate reference XML: #{e}"
        broken = true
        to_insert = nil
      end
      # this may be a bit controversial: Don't break the build if reference is broken
      if KRAMDOWN_OFFLINE || broken
        unless to_insert
          to_insert = "<reference anchor='#{anchor}'> <front> <title>*** BROKEN REFERENCE ***</title> <author> <organization/> </author> <date/> </front> </reference>"
          warn "*** KRAMDOWN_OFFLINE: Inserting broken reference for #{fn}"
        end
      else
        exit 66 unless to_insert # EX_NOINPUT
      end
    end
    to_insert
  else
    "<xref#{el_html_attributes(el)}>#{alt}</xref>"
  end
end