#!/usr/bin/ruby =begin 0 @GEDTAG@ SOUR 1 AUTH 1 TITL 1 NOTE gedtag (http://hans.fugal.net/src/gedtag) ... 0 {FAM,INDI} ... 1 SOUR @GEDTAG@ 2 DATA 3 DATE =end GEDCOM_Regex = /^(?:\s|\xEF\xBB\xBF|\xFF\xFE|\xFE\xFF)*(\d)+\s+((@[^@]+@)\s+)?([A-Za-z_0-9]+)( (@[^@]+@|[^\r\n]+))?$/ # read the raw GEDCOM raw = ARGF.read key = "GEDTAG" # setup filename = ARGV[0] source = citation = author = title = date = nil gedcom = {} famindi = head = subm = false raw.each_line do |line| line.chomp! unless line.strip =~ GEDCOM_Regex $stderr.puts "Parse error: '#{line}', ignoring" next end level = $1.to_i xref = $3 ? $3[1..-2] : nil tag = $4.upcase value = $6 case level when 0 # wrap up HEAD/SUBM if subm if filename title = "#{filename}" else title = "#GEDCOM transmission" end # emit source record puts <<-EOF 0 @#{key}@ SOUR 1 AUTH #{author} 1 TITL #{title} 1 NOTE This source created and linked by gedtag (http://hans.fugal.net/src/gedtag) EOF citation = <<-EOF 1 SOUR @#{key}@ EOF if date citation += <<-EOF 2 DATA 3 DATE #{date} EOF end end # emit source citation if famindi puts citation puts " 2 PAGE #{famindi}" end head = subm = famindi = false case tag when /fam|indi/i famindi = xref when /head/i head = true when /subm/i subm = true end when 1 if head filename = value if tag =~ /file/i date = value if tag =~ /date/i elsif subm # get the author from the first SUBM record, likely the primary SUBM (and # there's usually only one) author ||= value if tag =~ /name/i end end # pretty-print print " " * level # emit the line puts line end # TODO # - override options (?) =begin Copyright (C) 2008 Hans Fugal This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. =end