#!/usr/bin/env ruby -KU
require 'rexml/document'
require 'yaml'
require 'optparse'

PROGNAME = $0 # "kramdown-rfc-extract-figures-tables"

target = :section
targets = [:section, :note, :rfc]
require 'optparse'
begin
  op = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROGNAME} [options] [-|document.xml]"
    opts.on("-tFMT", "--to=FMT", targets, "Target format #{targets.map(&:to_s)}") do |v|
      target = v
    end
  end
  op.parse!
rescue Exception => e
  warn e
  exit 1
end

class String
  def spacify
    gsub(/\s+/, " ")
  end
end

lists = Hash.new { |h, k| h[k] = [] }
d = REXML::Document.new(ARGF)
unless d.root
  warn "** #{PROGNAME}: Cannot parse input"
  exit 1
end
REXML::XPath.each(d.root, %{//figure|//*[name()="table" or name()="texttable"]}) do |x|
  gi = x.name
  ref = x[:anchor]
  # p [gi, ref]
  out = []
  REXML::XPath.each(x, "name") do |nm|
    out << nm.children.map{|ch| ch.to_s}.join
    # p [gi, out.last]
  end
  REXML::XPath.each(x, "@title") do |ttl|
    out << ttl.to_s.spacify
    # p [gi, out.last]
  end
  gi1 = if gi == "texttable"; "table" else gi end
  if out == []         # nameless
    # nameless, anchorless fig doesn't get a number; ignore
    next if gi == "figure" && !ref
    # Synthesize name (if not redundant)
    out = ["#{gi1.capitalize} #{lists[gi1].size + 1}"] if !ref
  end
  # p [gi, out]
  lists[gi1] << [ref, out.join(" ")]
end

lists.each do |k, v|
  item = k.capitalize
  title = "List of #{item}s"
  case target
  when :note
    puts
    puts "--- note_#{title.gsub(" ", "_")}"
    puts
  when :section, :rfc
    puts
    puts "# #{title}"
    puts "{:unnumbered}"
    puts
    puts "{:compact#{" hangindent=\"11\"" if target == :rfc}}"
  else
    fail
  end
  v.each_with_index do |(ref, ti), n|
    ti.sub!(/,?[\p{Zl}\p{Zp}\p{Cc}].*/, "") # first line of caption only
    if target == :rfc
      if ref
        puts "{{#{ref}}}:"
        puts ": {{<<#{ref}}}"
      else
        puts "#{item} #{n+1}:"
        puts ": #{ti}"
        warn "** No anchor on #{item} #{n+1} \"#{ti}\""
      end
      puts
    else
      ti = "[#{ti}](##{ref})" if ref
      puts "#{n+1}. #{ti}"
    end
  end
end
