-
Notifications
You must be signed in to change notification settings - Fork 0
/
gedcom_parser.rb
45 lines (40 loc) · 927 Bytes
/
gedcom_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
#!/usr/bin/env ruby
require 'nokogiri'
class Node
attr_accessor :type, :id, :value, :nodes
def initialize(type, id = nil, value = nil)
@type = type
@id = id
@value = value
@nodes = []
end
end
unless ARGV.empty?
nodes = Hash.new { |hash, key| hash[key] = [] }
# file in
File.open(ARGV[0]).each_line do |line|
parts = line.chomp.split
level = parts[0]
if parts[1].include?('@')
id = parts[1]
type = parts[2].downcase
else
type = parts[1].downcase
value = parts[2..-1].join(' ')
end
nodes[level] << Node.new(type, id, value)
end
# xml out
builder = Nokogiri::XML::Builder.new do |xml|
xml.gedcom {
nodes.each_key do |key|
nodes[key].each do |node|
options = {}
options[:id] = node.id if node.id
xml.send(node.type, node.value, options)
end
end
}
end
puts builder.to_xml
end