#!/usr/bin/env ruby

require 'commit'

com = Commit.new
{ 'jw' => 'math.xml', 'ew' => 'math_en.xml' }.each do |xml|
# { 'jw' => 'math.xml' }.each do |xml|
  f = File.open(xml[1])

  title = ''
  time = nil
  id = 0

  while f.gets
    if /<title>([^<]+)<\/title>/ =~ $_
      title = $1
#    elsif /<timestamp>([^<]+)<\/timestamp>/ =~ $_
#      time = Time.parse($1).to_i
#    elsif /^      <id>(\d+)<\/id>/ =~ $_
#      id = $1
    else
      while (i = $_.index('&lt;math&gt;'))
        if ((j = $_.index('&lt;nowiki&gt;')) && j < i)
          if (e = $_.index('&lt;/nowiki&gt;'))
            $_ = $_[e+15..-1]
          else
            $_ = ''
          end
          next
        end
        $_ = $_[i+12..-1]
        e = $_.index('&lt;/math&gt;')
        break if !e
        fm = $_[0...e]

        fm.gsub!(/&lt;/, '<')
        fm.gsub!(/&gt;/, '>')
        fm.gsub!(/&amp;/, '&')
        fm.split('\\\\').each do |m|
          com.add(m, xml[0], title)
        end
      end
    end
  end
end

com.close

