convert 2.21 KB
Newer Older
Ollivier Robert's avatar
Ollivier Robert committed
1
#!/usr/bin/env ruby
2
# Encoding: utf-8
Ollivier Robert's avatar
Ollivier Robert committed
3 4 5 6 7 8 9
# Pour lancer le script il faut ruby, rubygems et les gems nokogiri et htmldiff

require 'rubygems'
require 'nokogiri'
require 'htmldiff'
require 'erb'

10
IN = ARGV.shift
Ollivier Robert's avatar
Ollivier Robert committed
11

12
data = File.read(IN)
Ollivier Robert's avatar
Ollivier Robert committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
sections = data.scan(%r(<Amend>.+?</Amend>)m)

Amendment = Struct.new :num, :note, :color, :commission_rows, :amendement_rows, :members, :article

include HTMLDiff

class Amendment
  Template = ERB.new File.read('template.erb'), nil, '-'
  
  def format
    Template.result(binding)
  end
  
  def rows
    [commission_rows.size, amendement_rows.size].max
  end
end

amendments = sections.map do |xml|
  doc = Nokogiri::XML(xml)

  num, note = doc.css('NumAm').text.split(' ', 2)
  num = num.to_i
  note.gsub!(/\s/, '') if note
  
  color = case note.to_s.gsub(/[^-+]/, '')
    when "--"
      "red"
    when "-"
      "IndianRed"
    when "++", "+++"
      "green"
    when "+"
      "lightgreen"
    when ""
      "lightgrey"
    else
      raise "Invalid note: #{note.inspect}"
  end
  
53 54
  infos = xml.scan(%r(Draft motion for a resolution\nAmendment\n(.+)\n^(Or. <Original|C|T|))m).first
  raise "No info found #{xml}" unless infos
Ollivier Robert's avatar
Ollivier Robert committed
55 56 57 58 59 60 61 62 63 64 65 66 67
  
  
  infos = infos.first.split(/\n/)

  commission_rows = []
  amendement_rows = []
  infos.each_slice(2) do |commission, amendment|
    commission = commission.to_s
    amendment = amendment.to_s
    commission_rows << diff(commission, amendment).to_s.gsub(%r(<ins class=".+?">.+?</ins>), "").gsub(%r(<del class=".+?">(.+?)</del>), "'''\\1'''")
    amendement_rows << diff(commission, amendment).to_s.gsub(%r(<del class=".+?">.+?</del>), "").gsub(%r(<ins class=".+?">(.+?)</ins>), "'''\\1'''")
  end
  
68 69 70
  members = doc.css('Members').text.split(%r{, }).map{|m|
    "[[#{m.split.join('')}|#{m}]]"
  }.join(', ')
Ollivier Robert's avatar
Ollivier Robert committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  article = doc.css('Article').text

  Amendment.new num, note, color, commission_rows, amendement_rows, members, article
end


File.open "result.mediawiki", "w" do |f|
  grouped = amendments.group_by { |am| (am.num - 1) / 10 }
  grouped.keys.sort.each do |index|
    ams = grouped[index].sort_by { |am| am.num }
    first = ams.first.num
    last = ams.last.num
    f.puts "== Amendments #{first}#{last} =="
    ams.each do |am|
      f.puts am.format
    end
    f.puts
  end
end