-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_ged.rb
90 lines (85 loc) · 2.24 KB
/
parse_ged.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# (c) 2017 Pavel Suchmann <pavel@suchmann.cz>
# Licensed under the terms of the GNU GPL version 3 (or later)
require_relative 'person'
def parse_ged filename
mode, given, surname, sex, birth, death, ref, bd, marriage = nil, nil, nil, nil, nil, nil, nil, nil, nil
husband, wife, children = nil, nil, []
people = {
'__?M?__' => Person.new('???', '', 'M', nil, nil),
'__?F?__' => Person.new('???', '', 'F', nil, nil)
}
File.readlines(filename).each do |row|
line = row.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
level, token, *rest = line.chomp.split /\s+/
rest = rest.join ' '
level = level.to_i
if level == 0
case mode
when :person
people[ref] = Person.new(given, surname, sex, birth, death)
when :family
children.each do |child_ref|
child = people[child_ref]
child.add_parent husband
child.add_parent wife
end
unless husband.nil?
wife = '__?F?__' if wife.nil?
people[husband].add_family(wife, children, marriage)
end
unless wife.nil?
husband = '__?M?__' if husband.nil?
people[wife].add_family(husband, children, marriage)
end
end
case rest
when 'INDI'
mode = :person
given, surname, sex, birth, death, bd = nil, nil, nil, nil, nil, nil
when 'FAM'
mode = :family
husband, wife, children, marriage = nil, nil, [], nil
else
mode = nil
end
ref = token
else
case token
when 'GIVN'
given = rest
when 'SURN'
surname = rest
when 'SEX'
sex = rest
when 'BIRT'
bd = :birth
birth = nil
when 'DEAT'
bd = :death
death = nil
when 'MARR'
bd = :marriage
marriage = nil
when 'DATE'
case bd
when :birth
birth = rest
when :death
death = rest
when :marriage
marriage = rest
end if level == 2
bd = nil
when 'HUSB'
husband = rest
when 'WIFE'
wife = rest
when 'CHIL'
children << rest
else
bd = nil
end
end
end
people
end