gedcom_ruby 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gedcom_ruby.rb +154 -0
  3. metadata +86 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01d0045ecaf38d4b17a327420824cbd443e34680
4
+ data.tar.gz: 12f7533caa6b10d388b646851346eac98d8bb87d
5
+ SHA512:
6
+ metadata.gz: d31e0cb2feccff47205a872d29384af6d908e200e5128e65af9eb457f28c7b178aab7b99b39833ca467fadd83c0a18ae7e1c0c54f2b546454fea3ae330d869af
7
+ data.tar.gz: 49839581e48e33da2e0a31d3426f6afc1bc709dfc0c2db79b02cdd97c0955b352c5db2f49ef3e4082f547cc7e1728216c56bfff867a249837e181dea8bd28392
@@ -0,0 +1,154 @@
1
+ # -------------------------------------------------------------------------
2
+ # gedcom.rb -- core module definition of GEDCOM-Ruby interface
3
+ # Copyright (C) 2003 Jamis Buck (jgb3@email.byu.edu)
4
+ # -------------------------------------------------------------------------
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ # -------------------------------------------------------------------------
19
+
20
+ #require '_gedcom'
21
+ require 'gedcom_ruby/date'
22
+ require 'stringio'
23
+
24
+ module GEDCOM
25
+ attr_accessor :auto_concat
26
+ ANY = [:any]
27
+
28
+ class Parser
29
+ def initialize(&block)
30
+ @callbacks = {
31
+ :before => Hash.new{|h,k| h[k] = []}, # Default to an empty array
32
+ :after => Hash.new{|h,k| h[k] = []} # Default to an empty array
33
+ }
34
+
35
+ @context_stack = []
36
+ @data_stack = []
37
+ @current_level = -1
38
+
39
+ @auto_concat = true
40
+
41
+ instance_eval(&block) if block_given?
42
+ end
43
+
44
+ def before(tags, callback=nil, &block)
45
+ tags = [tags].flatten
46
+ callback = check_proc_or_block(callback, &block)
47
+ @callbacks[:before][tags].push(callback)
48
+ end
49
+
50
+ def after(tags, callback=nil, &block)
51
+ tags = [tags].flatten
52
+ callback = check_proc_or_block(callback, &block)
53
+ @callbacks[:after][tags].push(callback)
54
+ end
55
+
56
+ def parse(file)
57
+ case file
58
+ when String
59
+ if file =~ /\n/mo
60
+ parse_string(file)
61
+ else
62
+ parse_file(file)
63
+ end
64
+ when IO
65
+ parse_io(file)
66
+ else
67
+ raise ArgumentError.new("requires a String or IO")
68
+ end
69
+ end
70
+
71
+ def context
72
+ @context_stack
73
+ end
74
+
75
+
76
+ protected
77
+
78
+ def check_proc_or_block(proc, &block)
79
+ unless proc or block_given?
80
+ raise ArgumentError.new("proc or block required")
81
+ end
82
+ proc = method(proc) if proc.kind_of? Symbol
83
+ proc ||= Proc.new(&block)
84
+ end
85
+
86
+ def parse_file(file)
87
+ File.open(file) do |io|
88
+ parse_io(io)
89
+ end
90
+ end
91
+
92
+ def parse_string(str)
93
+ parse_io(StringIO.new(str))
94
+ end
95
+
96
+ def parse_io(io)
97
+ io.each_line do |line|
98
+ level, tag, rest = line.chop.split( ' ', 3 )
99
+ next if level.nil? or tag.nil?
100
+ level = level.to_i
101
+
102
+ if (tag == 'CONT' || tag == 'CONC') and @auto_concat
103
+ concat_data tag, rest
104
+ next
105
+ end
106
+
107
+ unwind_to(level)
108
+
109
+ tag, rest = rest, tag if tag =~ /@.*@/
110
+
111
+ @context_stack.push(tag)
112
+ @data_stack.push(rest)
113
+ @current_level = level
114
+
115
+ do_callbacks(:before, @context_stack, rest)
116
+ end
117
+ unwind_to -1
118
+ end
119
+
120
+ def unwind_to(level)
121
+ while @current_level >= level
122
+ do_callbacks(:after, @context_stack, @data_stack.last)
123
+ @context_stack.pop
124
+ @data_stack.pop
125
+ @current_level -= 1
126
+ end
127
+ end
128
+
129
+ def concat_data(tag, rest)
130
+ if @data_stack[-1].nil?
131
+ @data_stack[-1] = rest
132
+ else
133
+ if @context_stack[-1] == 'BLOB'
134
+ @data_stack[-1] << rest
135
+ else
136
+ if tag == 'CONT'
137
+ @data_stack[-1] << "\n" + (rest || "")
138
+ elsif tag == 'CONC'
139
+ old = @data_stack[-1].chomp
140
+ @data_stack[-1] = old + (rest || "")
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ def do_callbacks(context_sym, tags, data)
147
+ relevant_callbacks = @callbacks[context_sym][tags] + @callbacks[context_sym][ANY]
148
+ relevant_callbacks.each do |callback|
149
+ callback.call(data)
150
+ end
151
+ end
152
+ end #/ Parser
153
+
154
+ end #/ GEDCOM
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gedcom_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Derek Kniffin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.3.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: byebug
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.3.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.3.0
53
+ description: This is a module for the Ruby language that defines a callback GEDCOM
54
+ parser. It does not do any validation of a GEDCOM file, but, using application-defined
55
+ callback hooks, can traverse any well-formed GEDCOM.
56
+ email: derek.kniffin@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/gedcom_ruby.rb
62
+ homepage: https://github.com/dkniffin/gedcom-ruby
63
+ licenses:
64
+ - GNU LESSER GENERAL PUBLIC LICENSE
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.5
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A Ruby library for easily doing custom, callback-based GEDCOM parsing
86
+ test_files: []