-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDh_GCard.pm
executable file
·2909 lines (2545 loc) · 67.7 KB
/
Dh_GCard.pm
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# Dh_GCard.pm - assorted functions for browsing a "GCard" database
#
# (c) 2010 David Haworth
#
# This file is part of DhG.
#
# DhG 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 3 of the License, or
# (at your option) any later version.
#
# DhG 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 DhG. If not, see <http://www.gnu.org/licenses/>.
package Dh_GCard;
use POSIX;
use Exporter();
@ISA = qw(Exporter);
@EXPORT =
(
# Data
DhG_FileList, # List of all files in database
DhG_Fileno, # Array mapping person ID to index in DhG_FileList
DhG_Name, # Array mapping person ID to names. undef means doesn't exist.
DhG_Gender, # Array mapping person ID to gender
DhG_Father_Name, # Array mapping person ID to their father's name
DhG_Father_Id, # Array mapping person ID to their father's ID
DhG_Mother_Name, # Array mapping person ID to their mother's name
DhG_Mother_Id, # Array mapping person ID to their mother's ID
DhG_Marriage_Dates, # Array mapping person ID to their marriage/partnership dates (tokenised string)
DhG_Marriage_Names, # Array mapping person ID to their partners (tokenised string)
DhG_Marriage_Ids, # Array mapping person ID to their partners' IDs (tokenised string)
DhG_Birth_Date, # Array mapping person ID to their birth date
DhG_Baptism_Date, # Array mapping person ID to their baptism date
DhG_Death_Date, # Array mapping person ID to their death date
DhG_Burial_Date, # Array mapping person ID to their burial date
DhG_Private, # Array of privacy flags (read from card file)
DhG_Private_Calc, # Array of calculated privacy flags
# Functions
DhG_LoadDatabase,
DhG_ClearDatabase,
DhG_LoadCard,
DhG_LoadCard_All,
DhG_LoadCard_FindFirstEvent,
DhG_LoadCard_GetNextEvent,
DhG_AnalyseRelations,
DhG_ParseName,
DhG_GetNoOfNames,
DhG_GetName,
DhG_GetFather,
DhG_GetMother,
DhG_GetSiblings,
DhG_GetOffspring,
DhG_GetSpouses,
DhG_GetPartners,
DhG_IsAlive,
DhG_IsPrivate,
DhG_GetFilename,
DhG_GetFilebase,
DhG_GetCardTemplateVars,
DhG_GetDescendantTreeTemplateVars,
DhG_GetAhnentafelTemplateVars,
DhG_GetNewPersonTemplateVars,
DhG_GetSurnameIndexTemplateVars,
DhG_GetPersonInfoLine,
DhG_Normalise,
DhG_Trim,
DhG_MungeUrl,
DhG_XMLify,
DhG_GetUniq,
DhG_EditCard,
DhG_EditFile,
DhG_SetVariable,
DhG_GetDebugLevel,
DhG_GetTemplateDir,
DhG_GetYearRange,
# These should go to another module
DhG_Dump,
DhG_PrintAncestorTree,
DhG_Search,
DhG_Test
);
use Dh_FileList;
sub DhG_LoadDatabase;
sub DhG_LoadCard;
sub DhG_LoadCard_Event;
sub DhG_NameToFilename;
sub DhG_Trim;
sub DhG_MungeUrl;
sub DhG_FormatDate;
sub DhG_GetTemplateDir;
sub DhG_ReadTranscriptFile;
sub DhG_ReadFileIntoVariable;
# Initialisation, "global-my" variables go here.
# Variables that can be changed with the set command.
my $DhG_DebugLevel = 0;
my $DhG_DateFormat = "raw";
my $DhG_CardBase = undef;
my $DhG_TextBase = "/data1/family-history/transcript";
my $DhG_TemplateDir = "/data1/toolbox/DhG/templates";
# Internal variables
my $outputfile_name = undef;
my $last_id = 0;
my %DhG_NameGenderMap;
my %DhG_printed;
return 1;
# DhG_Normalise() - "normalises" a string by replacing each multiple space-tab combination with a single space.
sub DhG_Normalise
{
my ($txt) = @_;
$txt = join(' ', split(/\s+/, DhG_Trim($txt)));
return $txt;
}
# DhG_XMLify() - converts some special characters to XML entities
# Returns an array!
sub DhG_XMLify
{
my @txt = @_;
for (@txt)
{
if ( defined $_ )
{
s/&/&/g;
s/</</g;
s/>/>/g;
}
}
return @txt;
}
# DhG_Trim() - trims leading and trailing spaces from string, returns result.
sub DhG_Trim
{
my ($txt) = @_;
$txt =~ s/^\s*(\S.*)$/$1/;
$txt =~ s/^(.*\S)\s*$/$1/;
return $txt;
}
# DhG_MungeUrl() - trims leading and trailing spaces from URL, translate, ..., returns result.
sub DhG_MungeUrl
{
my ($url) = @_;
# First, get rid of the junk
$url = DhG_Trim($url);
# The we can translate URLs for sites that are known to have changed
# This could also cope with "virtual URLs", variables etc. TBD
# Nothing defined yet.
# Finally, return the munged URL
return $url;
}
# DhG_SetVariable() sets a variable to the given value
sub DhG_SetVariable
{
my ($params) = @_;
my ($varname, $varvalue) = $params =~ m{^(.*)=(.*)$};
if ( defined $varname && defined $varvalue )
{
$varname = DhG_Trim($varname);
$varvalue = DhG_Trim($varvalue);
if ( $varname eq "DBG" )
{
$DhG_DebugLevel = $varvalue;
}
elsif ( $varname eq "DATE" )
{
# Values: year, full, ...?
$DhG_DateFormat = $varvalue;
}
elsif ( $varname eq "CARDBASE" )
{
# Base directory for new card files
$DhG_CardBase = $varvalue;
}
elsif ( $varname eq "TEMPLATEDIR" )
{
# Directory containing templates
$DhG_TemplateDir = $varvalue;
}
elsif ( $varname eq "TEXTBASE" )
{
# Base directory for transcript (.text) files
$DhG_TextBase = $varvalue;
}
else
{
print STDERR "Unknown or unrecognised variable $varname\n";
}
}
else
{
print STDERR "Eh?\n";
}
}
# DhG_LoadDatabase
#
# Loads the database consisting of all the .card files in the directory trees specified by the parameters
sub DhG_LoadDatabase
{
my @args = @_;
$DhG_Name[0] = "(Unknown)";
$DhG_Gender[0] = "?";
$DhG_Private[0] = 0;
$DhG_Private_Calc[0] = 0;
@DhG_FileList = DhFL_FileList(@args);
my $index = 0;
my $id;
my $fileno = 0;
my $filename;
while ( defined $DhG_FileList[$fileno] )
{
my $filename = $DhG_FileList[$fileno];
if ( $filename =~ m{\.card$} )
{
print "Loading $fileno: $filename\n" if ($DhG_DebugLevel > 9 );
$id = DhG_LoadCard($fileno);
if ( defined $id )
{
if ( $id > $last_id )
{
$last_id = $id;
}
}
}
$fileno++;
}
}
# DhG_LoadCard
#
# Loads the index card whose filename is given in the first parameter. The second parameter is
# intended to enable some plausibilty checks but isn't implemented at the moment.
sub DhG_LoadCard
{
my ($fileno, $check) = @_;
my $filename = $DhG_FileList[$fileno];
my $name = undef;
my $uniq = undef;
my $gender = undef;
my $privacy = undef;
my $father_name = undef;
my $father_uniq = undef;
my $mother_name = undef;
my $mother_uniq = undef;
my $id = undef;
my $err = 0;
my $stored = 0;
if ( !defined $check )
{
$check = 0;
}
open(DhG_INFILE, "<$filename") or die "Unable to open $filename for reading\n";
print STDOUT "Reading person from $filename\n" if ($DhG_DebugLevel > 99 );
while ( <DhG_INFILE> )
{
chomp;
my $line = DhG_Normalise($_);
if ( $line eq "" )
{
# Ignore blank lines
print STDOUT "Blank line\n" if ($DhG_DebugLevel > 99);
}
elsif ( $line eq "EOF" )
{
# Skip out of read loop if an EOF marker is found.
last;
}
else
{
$firstchar = substr($line, 0, 1);
$firstdigit = index("0123456789?", $firstchar);
if ( $firstchar eq "#" )
{
# Comment line: ignore
print STDOUT "Comment line \"$line\"\n" if ($DhG_DebugLevel > 99);
}
elsif ( $line eq "Male" || $line eq "Female" )
{
print STDOUT "Gender line \"$line\"\n" if ($DhG_DebugLevel > 99);
if ( defined $gender )
{
printf STDERR "$filename: multiple Gender entries\n";
}
else
{
$gender = $firstchar;
if ( defined $id )
{
$DhG_Gender[$id] = $gender;
}
}
}
elsif ( $line eq "Private" || $line eq "Public" )
{
print STDOUT "Privacy line \"$line\"\n" if ($DhG_DebugLevel > 99);
if ( defined $privacy )
{
printf STDERR "$filename: multiple Privacy entries\n";
}
else
{
if ( $line eq "Private" )
{
$privacy = 1;
}
else
{
$privacy = 0;
}
if ( defined $id )
{
$DhG_Private[$id] = $privacy;
}
}
}
elsif ( $firstchar eq "+" || $firstchar eq "-" )
{
# Supplementary information line
print STDOUT "Supplementary info line \"$line\"\n" if ($DhG_DebugLevel > 99);
}
elsif ( $firstchar eq "|" )
{
# Continuation line
print STDOUT "Continuation line \"$line\"\n" if ($DhG_DebugLevel > 99);
}
elsif ( $firstdigit >= 0 )
{
# Event entry
print STDOUT "Event line \"$line\"\n" if ($DhG_DebugLevel > 99);
if ( defined $id )
{
DhG_LoadCard_Event($filename, $id, $line) if ( $err == 0 );
}
else
{
printf STDERR "$filename: ERROR: Event record before identity has been defined\n";
}
}
else
{
my($keyword,$rest) = $line =~ m{^(\S*):\s*(\S.*)$};
if ( defined $keyword )
{
print STDOUT "Keyword line \"$keyword\" : Rest = \"$rest\"\n" if ($DhG_DebugLevel > 99);
if ( $keyword eq "Name" )
{
if ( defined $name )
{
printf STDERR "$filename: ERROR: multiple Name entries\n";
}
else
{
$name = $rest;
}
}
elsif ( $keyword eq "Uniq" )
{
if ( defined $uniq )
{
printf STDERR "$filename: ERROR: multiple Uniq entries\n";
}
else
{
$uniq = $rest;
if ( defined $DhG_Name[$uniq] )
{
printf STDERR "$filename: ERROR: duplicate ID ($uniq); ignoring file\n";
$err++;
}
}
}
elsif ( $keyword eq "Father" )
{
if ( defined $father )
{
printf STDERR "$filename: ERROR: multiple Father entries\n";
}
else
{
($father_name, $father_uniq) = DhG_ParseName($rest);
if ( defined $id )
{
$DhG_Father_Name[$id] = $father_name;
$DhG_Father_Id[$id] = $father_uniq;
}
if ( !defined $father_uniq )
{
printf STDERR "$filename: WARNING: Father has no unique ID\n"
if ( $DhG_DebugLevel > 9 );
}
}
}
elsif ( $keyword eq "Mother" )
{
if ( defined $mother )
{
printf STDERR "$filename: ERROR: multiple Mother entries\n";
}
else
{
($mother_name, $mother_uniq) = DhG_ParseName($rest);
if ( defined $id )
{
$DhG_Mother_Name[$id] = $mother_name;
$DhG_Mother_Id[$id] = $mother_uniq;
}
if ( !defined $mother_uniq )
{
printf STDERR "$filename: WARNING: Mother has no unique ID\n"
if ( $DhG_DebugLevel > 9 );
}
}
}
}
else
{
print STDERR "$filename: WARNING: Mystery line \"$line\"\n" if ($DhG_DebugLevel > 49);
}
# As soon as we have a name and uniq ID, store them!
if ( !$stored && defined $name && defined $uniq )
{
print STDOUT "Storing $name with ID $uniq\n" if ($DhG_DebugLevel > 49);
$id = $uniq;
$DhG_Name[$id] = $name;
$DhG_Gender[$id] = $gender;
$DhG_Father_Name[$id] = $father_name;
$DhG_Father_Id[$id] = $father_uniq;
$DhG_Mother_Name[$id] = $mother_name;
$DhG_Mother_Id[$id] = $mother_uniq;
$DhG_Fileno[$id] = $fileno;
$DhG_Marriage_Dates[$id] = "";
$DhG_Marriage_Names[$id] = "";
$DhG_Marriage_Ids[$id] = "";
$DhG_Private[$id] = $privacy;
$stored = 1;
}
}
}
}
close(DhG_INFILE);
if ( $DhG_DebugLevel > 79 && $err == 0 )
{
print STDOUT "\n";
print STDOUT "$DhG_Fileno[$id] ($DhG_FileList[$DhG_Fileno[$id]])\n";
print STDOUT "Name:";
print STDOUT " $DhG_Name[$id]" if ( defined $DhG_Name[$id] );
print STDOUT "\n";
print STDOUT "Gender: $DhG_Gender[$id]\n" if ( defined $DhG_Gender[$id] );
print STDOUT "Father: $DhG_Father_Name[$id]\n" if ( defined $DhG_Father_Name[$id] );
print STDOUT "F.Uniq: $DhG_Father_Id[$id]\n" if ( defined $DhG_Father_Id[$id] );
print STDOUT "Mother: $DhG_Mother_Name[$id]\n" if ( defined $DhG_Mother_Name[$id] );
print STDOUT "M.Uniq: $DhG_Mother_Id[$id]\n" if ( defined $DhG_Mother_Id[$id] );
}
return $id;
}
# DhG_LoadCard_Event() loads an event from a card file.
# INTERNAL
# Event line is in the form "DATE EVENT MORE-INFO"
# Since version 2, MORE-INFO is only for marriages and partnerships.
sub DhG_LoadCard_Event
{
my ($filename, $id, $evline) = @_;
my ($date, $event, $rest) = $evline =~ m{^(\S*)\s*(\S*)\s*(.*)$};
printf STDOUT "Event line: $date : $event : \"$rest\"\n" if ( $DhG_DebugLevel > 99 );
if ( $event eq "Birth" )
{
# Record date and place of birth
$DhG_Birth_Date[$id] = $date;
}
elsif ( $event eq "Baptism" )
{
# Record date and place of baptism
$DhG_Baptism_Date[$id] = $date;
}
elsif ( $event eq "Marriage" || $event eq "Partnership" )
{
# Record date of marriage/partnership and spouse/partner. Trickier - there might be more than one!
if ( $DhG_Marriage_Dates[$id] ne "" )
{
$DhG_Marriage_Dates[$id] .= "|";
$DhG_Marriage_Names[$id] .= "|";
$DhG_Marriage_Ids[$id] .= "|";
}
$DhG_Marriage_Dates[$id] .= $date;
my ($spouse_name, $spouse_id) = DhG_ParseName($rest);
$DhG_Marriage_Names[$id] .= $spouse_name;
if ( defined $spouse_id )
{
$DhG_Marriage_Ids[$id] .= $spouse_id;
}
else
{
$DhG_Marriage_Ids[$id] .= "?";
printf STDERR "$filename: WARNING: Spouse $spouse_name has no unique ID\n";
}
}
elsif ( $event eq "Death" )
{
# Record date and place of death
$DhG_Death_Date[$id] = $date;
}
elsif ( $event eq "Burial" )
{
# Record date and place of burial
$DhG_Burial_Date[$id] = $date;
}
}
# DhG_LoadCard_All() - reads the entire file from a named card file into an array
sub DhG_LoadCard_All
{
my ($cardname) = @_;
my $tmpstr;
my $err = 0;
my $skip_leading = 1;
@file_lines = ();
# print STDERR "Processing $cardname\n";
if ( open(DhG_INFILE, "<$cardname") )
{
while ( <DhG_INFILE> )
{
chomp;
$line = DhG_Trim($_);
if ( $line eq "EOF" )
{
# Skip out of reading loop if an EOF marker is found.
last;
}
push(@file_lines, $line);
}
close(DhG_INFILE);
}
else
{
print STDERR "Unable to open $cardname for reading\n";
}
return @file_lines;
}
# DhG_LoadCard_FindFirstEvent() - find the first event in a preloaded card file.
sub DhG_LoadCard_FindFirstEvent
{
my ($lref) = @_;
my ($lno, $line);
$lno = 0;
while ( defined ${$lref}[$lno] )
{
# Look at each line
$line = ${$lref}[$lno];
if ( $line =~ m{^[1-9?]} )
{
# Found an event. Return its line number.
print STDOUT "DBG: Found first event \"$line\" at $lno.\n" if ( $DhG_DebugLevel >= 100 );
return $lno;
}
$lno++;
}
# No events found; return -1 to indicate it.
return (-1);
}
# DhG_LoadCard_GetNextEvent() - extract the next event
sub DhG_LoadCard_GetNextEvent
{
my ($mark, $lref, $mode, $t_index, $tref, $im_index, $imref) = @_;
my ($date, $event, $info, $source) = ("", "", "", "");
my ($spouse, $spouse_file) = ("", "");
my ($line);
my ($e_d, $e_t, $e_r);
my $in_source = 0;
my $in_transcript = 0;
my $transcript_text = undef;
my $transcript_type = undef;
my $newline = "\n";
my ($source_name, $source_nLinks) = ("", 0);
my @source_link = ();
print STDOUT "DBG: DhG_LoadCard_GetNextEvent: mode = \"$mode\"\n" if ( $DhG_DebugLevel >= 100 );
if ( $mode eq 'html' )
{
$newline = "<br/>"
}
print STDOUT "DBG: DhG_LoadCard_GetNextEvent: newline = \"$newline\"\n" if ( $DhG_DebugLevel >= 100 );
$line = ${$lref}[$mark];
($e_d, $e_t, $e_r) = $line =~ m{^([^\s]+)\s+([^\s]+)(.*)$};
$e_r = DhG_Trim($e_r);
$e_t = ucfirst(lc(DhG_Trim($e_t)));
$date = DhG_FormatDate($e_d, "?", "full");
# DECISION: where the spouse goes (event or info) is decided by the template
# DECISION: apart from some exceptions, the entire rest of line is the event.
# IMPLICATIONS: First word of event will be normalised
# Marriage/partnership is a special case
# Misc is a special case for backwards compatibility.
$event = $e_t;
if ( $event eq "Marriage" || $event eq "Partnership" )
{
my ($spouse_name, $spouse_id) = DhG_ParseName($e_r);
if ( defined $spouse_id )
{
$spouse = DhG_GetName($spouse_id);
}
if ( (defined $spouse) && ($spouse ne "") )
{
my $spouse_years = DhG_GetYearRange($spouse_id);
$spouse .= " ($spouse_years)" if ( defined $spouse_years && $spouse_years ne "" );
$spouse_file = DhG_GetFilebase($spouse_id);
}
else
{
$spouse = $spouse_name;
}
}
elsif ( $event eq "Misc" )
{
# Event is Misc: replace it with what it really is (which might be a phrase).
# This is just for backwards compatibility
$event = $e_r;
}
else
{
# Join the first word (case-adjusted) onto the rest.
$event .= " ".$e_r;
}
print STDOUT "DBG: Found event \"$date $event\" at $mark.\n" if ( $DhG_DebugLevel >= 100 );
$mark++;
# Convert basic data to XML-friendly format. info and source have been selectively converted.
($date, $event, $spouse, $spouse_file) = DhG_XMLify($date, $event, $spouse, $spouse_file);
# Process the event
while ( defined ${$lref}[$mark] )
{
# Look at each line
$line = ${$lref}[$mark];
if ( $line =~ m{^[1-9?]} )
{
# Found next event. Output the remaining transcript if there is one.
if ( $in_transcript )
{
if ( defined $transcript_text )
{
my $tline = "";
$tline .= "<$transcript_type>" if ( $mode eq 'html' );
$tline .= $transcript_text;
$tline .= "</$transcript_type>" if ( $mode eq 'html' );
$tline .= "\n";
${$tref}[$t_index] = $tline;
$t_index++;
$source_link[$source_nLinks++] = "<a href=\"#transcript_$t_index\">[transcript $t_index]</a>";
}
$in_transcript = 0;
}
# Output the remaining source if there is one.
if ( $in_source )
{
$source .= $newline if ( $source ne "" );
$source .= "Source: ".$source_name;
if ( $mode eq 'html' )
{
my $i;
for ( $i = 0; $i < $source_nLinks; $i++ )
{
my $tag = "[".($i+1)."]";
$source .= " $source_link[$i]"
}
}
}
# Return the line number of the next event and the data for this event.
return ($mark, $date, $event, $spouse, $spouse_file, $info, $source, $t_index, $im_index);
}
elsif ( $line =~ m{^\+} )
{
# New primary info line
# Output the current transcript if there is one
if ( $in_transcript )
{
if ( defined $transcript_text )
{
my $tline = "";
$tline .= "<$transcript_type>" if ( $mode eq "html" );
$tline .= $transcript_text;
$tline .= "</$transcript_type>" if ( $mode eq "html" );
$tline .= "\n";
${$tref}[$t_index] = $tline;
$t_index++;
$source_link[$source_nLinks++] = "<a href=\"#transcript_$t_index\">[transcript $t_index]</a>";
}
$in_transcript = 0;
}
# Output the current source if there is one
if ( $in_source )
{
$source .= $newline if ( $source ne "" );
$source .= "Source: ".$source_name;
if ( $mode eq 'html' )
{
my $i;
for ( $i = 0; $i < $source_nLinks; $i++ )
{
$source .= " $source_link[$i]";
}
}
# Clear out the previous source.
$source_link = ();
$source_nLinks = 0;
$in_source = 0;
}
if ( $line =~ m{^\+Source} )
{
# Switch to SOURCE mode
($source_name) = $line =~ m{^\+Source (.*)$};
$source_name = DhG_Trim($source_name);
if ( $source_name eq "" )
{
print STDERR "Event source line $line hase no source name - ignoring.\n";
}
else
{
($source_name) = DhG_XMLify($source_name);
$in_source = 1;
}
}
else
{
my ($info_type, $info_val) = $line =~ m{^\+(\S*)\s(.*)$};
if ( defined $info_type && defined $info_val )
{
$info_type = ucfirst(lc(DhG_Trim($info_type)));
$info_val = ucfirst(DhG_Trim($info_val));
print STDOUT "DBG: Event info line \"$info_type - $info_val\" at $mark.\n"
if ( $DhG_DebugLevel >= 100 );
($info_type, $info_val) = DhG_XMLify($info_type, $info_val);
$info .= $newline if ( $info ne "" );
$info .= $info_type . ": " . $info_val;
}
else
{
print STDOUT "DBG: Ignoring event info line \"$line\" at $mark.\n"
if ( $DhG_DebugLevel >= 100 );
}
}
}
elsif ( $line =~ m{^\-} )
{
# New secondary info line
# Output the transcript if there is one.
if ( $in_transcript )
{
if ( defined $transcript_text )
{
my $tline = "";
$tline .= "<$transcript_type>" if ( $mode eq "html" );
$tline .= $transcript_text;
$tline .= "</$transcript_type>" if ( $mode eq "html" );
$tline .= "\n";
${$tref}[$t_index] = $tline;
$t_index++;
$source_link[$source_nLinks++] = "<a href=\"#transcript_$t_index\">[transcript $t_index]</a>";
}
$in_transcript = 0;
}
if ( $in_source )
{
# Secondary lines that are part of source records: we recognise URL, File and Transcript
if ( $line =~ m{^-URL} )
{
# A link to an arbitrary web site
my ($url) = $line =~ m{^\-URL (.*)$};
$url = DhG_MungeUrl($url);
$source_link[$source_nLinks++] = "<a href=\"$url\">[link]</a>";
}
elsif ( $line =~ m{^-Transcript} )
{
# A transcript
($transcript_type) = $line =~ m{^-Transcript (.*)$};
$transcript_type = DhG_Trim($transcript_type) if ( defined $transcript_type );
$transcript_type = "pre" if ( !defined $transcript_type || $transcript_type eq "" );
$in_transcript = 1;
$transcript_text = undef
}
elsif ( $line =~ m{^-File } )
{
# A file source. Does it specify a Transcript file with the ".text" suffix?
my ($source_type, $source_name) = $line =~ m{^\-File +([^ ]+) +(.*)$};
$source_type = DhG_Trim($source_type);
$source_name = DhG_Trim($source_name);
#print "$source_type $source_name\n";
if ( lc($source_type) eq "transcript" )
{
#print "source type - transcript\n";
if ( $source_name =~ m{^.*\.text$} )
{
#print "source name - *.text\n";
# Transcript file has the correct extension
$transcript_text = DhG_ReadTranscriptFile($source_name);
if ( defined $transcript_text )
{
#print "transcript text found $source_name\n";
#print "$transcript_text\n";
my $tline = "";
$tline .= "<pre>" if ( $mode eq "html" );
$tline .= $transcript_text;
$tline .= "</pre>" if ( $mode eq "html" );
$tline .= "\n";
${$tref}[$t_index] = $tline;
$t_index++;
$source_link[$source_nLinks++] =
"<a href=\"#transcript_$t_index\">[transcript $t_index]</a>";
$transcript_text = undef
}
else
{
print "Source file $source_name not found\n";
}
}
}
${$imref}[$im_index] = "$source_type: $source_name";
$im_index++;
$source_link[$source_nLinks++] = "<a href=\"#image_$im_index\">[image $im_index]</a>";
}
}
else
{
# Secondary lines that are parts of other info records
# Make sure there has been an info record first - ognore otherwise
if ( $info ne "" )
{
if ( $line =~ m{^-URL} )
{
# A link to an arbitrary web site
my ($url) = $line =~ m{^\-URL (.*)$};
$url = DhG_MungeUrl($url);
$info .= " <a href=\"$url\">[link]</a>";
}
}
}
}
elsif ( $line =~ m{^\|} )
{
# Continuation line
if ( $in_transcript )
{
my ($transcript_line) = $line =~ m{^\|(.*)$};
if ( defined $transcript_line )
{
($transcript_line) = DhG_XMLify($transcript_line);
if ( defined $transcript_text )
{
$transcript_text .= "\n".$transcript_line;
}
else
{
$transcript_text = $transcript_line;
}
}
}
}
else
{
# Ignore anything else
}
$mark++;
}
# No further event found. Output the remaining transcript and source if there is one.
if ( $in_transcript )
{
if ( defined $transcript_text )
{
my $tline = "";
$tline .= "<$transcript_type>" if ( $mode eq "html" );
$tline .= $transcript_text;
$tline .= "</$transcript_type>" if ( $mode eq "html" );
$tline .= "\n";
${$tref}[$t_index] = $tline;
$t_index++;
$source_link[$source_nLinks++] = "<a href=\"#transcript_$t_index\">[transcript $t_index]</a>";
}
$in_transcript = 0;
}
if ( $source_name ne "" )
{
$source .= $newline if ( $source ne "" );
$source .= "Source: ".$source_name;
if ( $mode eq 'html' )
{
my $i;
for ( $i = 0; $i < $source_nLinks; $i++ )
{
my $tag = "[".($i+1)."]";
$source .= " $source_link[$i]"
}
}
}
# Return (-1) to indicate end, and the data for this event.
return ((-1), $date, $event, $spouse, $spouse_file, $info, $source, $t_index, $im_index);
}
# DhG_ClearDatabase() - removes all records from database
sub DhG_ClearDatabase
{
@DhG_Name = undef;
@DhG_Gender = undef;
@DhG_FileList = undef;
@DhG_Name = undef;
@DhG_Gender = undef;
@DhG_Father_Name = undef;
@DhG_Father_Uniq = undef;
@DhG_Mother_Name = undef;
@DhG_Mother_Uniq = undef;
@DhG_Fileno = undef;
@DhG_Marriage_Dates = undef;