Debian Codegraph @

reports/register-rtf.ll

raw
1 /*
2 * @progname register-rtf.ll
3 * @version 1.6
4 * @author Doug McCallum
5 * @category
6 * @output RTF
7 * @description
8 *
9 * RTF based Register Report Generator.
10 * This program has many options but basically takes a person
11 * and generates an RTF document that can be read by a number
12 * of word processors. The document can optionally be cross-indexed
13 * and footnoted. The format is close to the NEHGS Register Form.
14 *
15 * Options are set by setting variables at the beginning of main
16 * The options are:
17 * option values
18 * ------------- ----------------------------------------------------
19 * doindex 0 == no index, 1 == create index
20 * prefix a string. This is prefixed to the standard
21 * numbers for people
22 * donotes 0 == don't output notes, 1 == output notes in-line
23 * 2 == output notes at end with a reference in text
24 * author a string used in the author info field
25 * strictness 0 == all descendents, 1 == modified form, 2 = strict
26 * childby 0 == list children together, 1 == indicate parent by
27 * titlepage 0 == no title page, 1 == generate title page
28 * dosources 0 == don't output sources, 1 == output sources
29 * occupation 0 == don't output occupation, 1 == output occupation
30 *
31 * Not implemented yet:
32 * showrefn 0 == don't show user refn tags, 1 == show tags
33 *
34 *
35 * Notes:
36 * when an index is created, it must be turned on in the word processor
37 * since only the indexing is actually done. Each time a name is seen
38 * it is indexed. If the name is a reference to the person as child of,
39 * then it is indexed in plain form numbers. If the person is a spouse
40 * the page number is italic and if the person is the first entry in
41 * the family info, then the page number is bold.
42 *
43 * The "prefix" is intended for use when doing multifamily reports.
44 * Further work needs to be done, but it can get you quite a ways
45 * toward that end. A future version of this program may handle
46 * the multi-family case directly.
47 *
48 * If notes are done in-line, each NOTE is a new paragraph and blank
49 * lines mark paragraphs. If done as endnotes, the NOTES are indicated
50 * with ids in the form [id1, id2] and then a Notes page created to
51 * print all the notes.
52 *
53 * All main lines and the Generation lines are treated as headings and
54 * can be put into a table of contents. This is always done but the
55 * table is not inserted into the document.
56 *
57 * If a marriage would occur multiple times, it is only referred
58 * to and not duplicated after the first time. This is complicated
59 * by an indi having multiple marriages and the duplicated spouse
60 * also having multiple marriages with only one marriage in the duplicate
61 * tree. These are called out as well.
62 *
63 * I don't plan to implement the generation tags (superscript generation
64 * numbers) on individuals at this point. They are easy enough to do
65 * since the current generation is always known but I find them not
66 * particularly useful in the text.
67 *
68 * The program can take a long time since it manipulates a lot of data.
69 * Future versions may improve performance as better algorithms are
70 * found.
71 *
72 * An individual's occupation(s) should be output into the report
73 * but isn't currently done. This probably should be an option along
74 * with the specific format to use.
75 *
76 * A future version should replace all occurrences of English
77 * text with variable references to make translation easier. Mostly
78 * this is the label stuff (b., d., bur., children, etc.). There should
79 * also be an option to expand the abbreviations to the full word.
80 *
81 * At some point, information such as the various baptism, christening,
82 * and other event information will be included as well. This will
83 * most likely be done as hidden text of some form or perhaps as
84 * annotations. This will make the text available but it would have
85 * to be turned on in the document.
86 *
87 * Sources should eventually be turned into endnotes so Word can do
88 * any special processing.
89 */
90 include("rtflib")
91 global(childby)
92 global(curr_index)
93 global(doindex)
94 global(donotes)
95 global(dosources)
96 global(endtags)
97 global(generation)
98 global(inum_set)
99 global(nextgen)
100 global(prev_fam_list)
101 global(strictness)
102 global(taglist)
103 global(titlepage)
104 global(all_sources)
105 global(occupation)
106
107 proc main()
108 {
109 /* program options */
110 set(doindex, 1) /* set to zero if you don't want an index */
111 set(prefix, "") /* string to prefix indi number with */
112 set(donotes, 1) /* 0 = no notes, 1 = inline, 2 = end */
113 set(dosources, 1) /* 0 = no sources, 1 = sources */
114 set(childby, 1) /* 0 = all children, 1 = children by spouse */
115 set(strictness, 0) /* all descendents, including female lines */
116 set(occupation, 1)
117 set(author, "Author/Compiler Name") /* default author name */
118 set(titlepage, 1) /* want a title page */
119
120 set(now, gettoday())
121 dayformat(1)
122 monthformat(1)
123 dateformat(8)
124 set(created, stddate(now))
125
126 /* program proper */
127
128 /* initialize the variables used */
129
130 indiset(inum_set) /* this set keeps the family numbers */
131 list(prev_fam_list) /* keeps track of marriages to avoid dups */
132 list(generation) /* current generation being processed */
133 list(nextgen) /* next generation to process */
134 table(endtags) /* if notes at end, keep track of tags */
135 list(taglist) /* the tags in order created */
136 set(curr_index, 0) /* current index for indi/family */
137 set(curr_gen, 0) /* current generation counter */
138 set(indi,0)
139 list(all_sources) /* contains list of all sources referenced */
140
141 getindi(indi)
142 if (not(indi)) {
143 /* if no one selected, exit */
144 return (0)
145 }
146 /*
147 * initialize the RTF file for standard paper (default)
148 */
149 call rtf_open(0)
150 set(title, concat("Descendents of ", fullname(indi, 0, 1, 128)))
151 call rtf_set_info(title, name(indi), author, author, created)
152 /*
153 * define default table size of 6in with 4 columns of
154 * .125in, .375in, .375in, 5.125in
155 */
156 call rtf_set_row_width(4, 8640) /* 6in * 1440 */
157 call rtf_set_col_width(540) /* .45in * 1440 */
158 call rtf_set_col_width(720) /* .500in * 1440 */
159 call rtf_set_col_width(540) /* .45in * 1440 */
160 /* want a footer with page number centered */
161 call rtf_footer(0, 2)
162 if (dosources) {
163 /* if doing sources, what footnote type */
164 call rtf_ftn_type(1, 0) /* everything at end */
165 }
166
167 /* add the first person to the list of people to process */
168 /* this primes the pump, so to speak */
169 call next_indi(indi)
170 enqueue(nextgen, indi)
171 if (titlepage) {
172 call dotitle(author, title)
173 }
174
175 /*
176 * all is setup to go down the descendency list.
177 * continue until all are individuals are processed.
178 * note that nextgen is the next generation to process
179 * and generation is the current one.
180 * both are queues so we keep order. Basically, all
181 * children of the person being processed are appended
182 * to the end of the nextgen list.
183 */
184
185 while (or(length(generation), length(nextgen))) {
186 /*
187 * if no current generation but a nextgen exists,
188 * start a new paragraph with header for the Generation
189 * and make the nextgen the current generation
190 */
191 if (empty(generation)) {
192 call rtf_para_indent(0, 0)
193 call rtf_pstart(1)
194 call rtf_para_keepnext()
195 call rtf_hstart()
196 set(curr_gen, add(curr_gen, 1))
197 capitalize(ord(curr_gen))
198 " Generation\n"
199 call rtf_hend()
200 call rtf_pend()
201 set(generation, nextgen)
202 list(nextgen)
203 }
204 /*
205 * the real work is done in out_register
206 * get the next person to process and then
207 * let out_register do the work.
208 */
209 set(indi, dequeue(generation))
210 call out_register(indi)
211 }
212 if (dosources) {
213 call dump_sources()
214 }
215 if (eq(donotes, 2)) {
216 /* end notes need to be dumped if they exist */
217 call endnotes()
218 }
219 call rtf_close()
220 }
221
222 /*
223 * out_register( indi )
224 * outputs the standard register format for the
225 * individual. Any children get added to nextgen
226 * if they have families. Global variables are used
227 * to modify the exact output.
228 */
229 proc out_register(indi)
230 {
231 /*
232 * We always start a new paragraph with hanging indent for the number.
233 * It is then tagged to be kept intact to avoid splitting across pages.
234 * The indivual's number if found and printed and then the name
235 * is output followed by marriage(s), birth, death, etc.
236 */
237 call rtf_pstart(3)
238 call rtf_para_indent(neg(540), 540)
239 call rtf_para_keepintact()
240 d(inum(indi)) "."
241 call rtf_tab(0)
242 call rtf_bold(1)
243 fullname(indi, 0, 1, 128)
244 /* the individual's name is entered as a level 2 TOC header */
245 call rtf_toc_entry(2, fullname(indi, 0, 1, 128))
246 call rtf_bold(0)
247 if (doindex) {
248 /*
249 * optional indexing, main entry is bold
250 * It would be nice to have index same text
251 * as the output name but the format is different.
252 * Need to find a better way to tie together so edit
253 * will do the right thing.
254 */
255 rtf_index(surname(indi), givens(indi), 1)
256 }
257 /*
258 * it is important to not print duplicate marriages since in some
259 * families this can lead to excessive information. In my own,
260 * there were 5 children of one ancestor married 5 children of
261 * another ancestor. Over seven generations there have been
262 * additional crossings of the lines and a non-pruned tree is HUGE
263 *
264 * There are two types of pruning of this type that need to be
265 * considered. The first is the simple case of a single marriage
266 * that is duplicated. It can be handled with a simple reference
267 * to the first occurrence. The second type is more complex where
268 * there are multiple marriages. Some of the marriages may be
269 * duplicates and need to be pruned, but some may be ones that
270 * haven't been seen yet. This occurs when person A marries person B.
271 * The implication that B is married to A. If B is also married to C
272 * the multiple case occurs. In this case if B is seen as a child
273 * and the family info is about to be output, the marriage to A is
274 * a duplicate but the one to C is not.
275 */
276 if (check_marriages(indi)) {
277 /*
278 * duplicate marriages (common ancestors)
279 * are not duplicated but referred to the only
280 * instance. If there were multiple marriages,
281 * then the duplicates can be referred to but the
282 * non-duplicate ones need expansion.
283 */
284 call rtf_italic(1)
285 set(prev, prevmarr(indi))
286 "(See marriage to "
287 set(f, getel(prev_fam_list, prev))
288 if (male(indi)) { set(s, wife(f)) }
289 else { set(s, husband(f)) }
290 fullname(s, 0, 1, 128)
291 if (and(doindex, surname(s))) {
292 rtf_index(surname(s), givens(s), 2)
293 }
294 ", number " prefix d(inum(s)) ")"
295 call rtf_italic(0)
296 call rtf_para_space(0, 120)
297 call rtf_pend()
298 } else {
299 /* not a complete duplicate so generate lots of text */
300 call rtf_para_keepnext()
301 if(e, birth(indi)) {
302 " b. " long(e)
303 if (dosources) {
304 call do_sources(e)
305 }
306 }
307 /*
308 * this is an attempt to track duplicate marriages.
309 * It needs to be looked at more carefully.
310 */
311 set(nmarr, nfamilies(indi))
312 /*
313 * run through all of this person's families
314 */
315 families (indi, famvar, spvar, cnt) {
316 if (spvar) {
317 set(prev, check_prev(famvar))
318 if (not(prev)) {
319 /* save for future reference */
320 enqueue(prev_fam_list, famvar)
321 }
322 /*
323 * basic format of marriage is
324 * m. [date][[,] place] [(mnum)] spouse
325 * [b. [date][, place]]
326 * ([daughter|son] of spouse's parents)
327 * [d. [date][, place]] [bur. [date][, place]].
328 */
329 /* If first spouse, use a ';' but ',' for rest */
330 if (eq(1, cnt)) {
331 "; m."
332 } elsif (ne(0, cnt)) {
333 ", m."
334 }
335 if (not(prev)) {
336 if (e, marriage(famvar)) {
337 " " long(e)
338 if (dosources) {
339 call do_sources(e)
340 }
341 ","
342 }
343 }
344 if (gt(nmarr, 1)) {
345 " (" d(cnt) ")"
346 }
347 if (not(prev)) {
348 " " fullname(spvar, 0, 1, 128)
349 if (and(doindex, surname(spvar))) {
350 rtf_index(surname(spvar),
351 givens(spvar), 2)
352 }
353 set(items, 0)
354 if (e, birth(spvar)) {
355 " b. " long(e)
356 if (dosources) {
357 call do_sources(e)
358 }
359 set(items, 1)
360 }
361 /*
362 * we know parents so give a referral.
363 * in a future version, this should be updated
364 * to determine if spouses had common ancestor
365 * and give the family number cross-reference.
366 * This would apply to my own genealogy.
367 */
368 if (f, parents(spvar)) {
369 call rtf_italic(1)
370 if (male(spvar)) {
371 " (son of "
372 } else {
373 " (daughter of "
374 }
375 set(j, "")
376 if (f, father(spvar)) {
377 set(j, " and ")
378 fullname(f, 0, 1, 128)
379 }
380 if (f, mother(spvar)) {
381 j fullname(f, 0, 1, 128)
382 }
383 ")"
384 call rtf_italic(0)
385
386 /* spouse's death info */
387 if (e, death(spvar)) {
388 if (eq(items, 1)) {
389 ", d. "
390 } else {
391 " d. "
392 }
393 long(e)
394 if (dosources) {
395 call do_sources(e)
396 }
397 set(items, 1)
398 }
399
400 /* spouse's burial info */
401 if (e, burial(spvar)) {
402 if (eq(1, items)) {
403 ", bur. "
404 } else {
405 " bur. "
406 }
407 long(e)
408 if (dosources) {
409 call do_sources(e)
410 }
411 set(items, 1)
412 }
413 }
414 } else {
415 fullname(spvar, 0, 1, 128)
416 call rtf_italic(1)
417 " (see marriage to number " d(inum(spvar))
418 ")"
419 call rtf_italic(0)
420 }
421 }
422 }
423 if (gt(cnt,0)) {
424 ".\n"
425 }
426 /* indi's remaining information */
427 if (e, death(indi)) {
428 " " givens(indi) " died " long(e)
429 if (dosources) {
430 call do_sources(e)
431 }
432 if (e, burial(indi)) {
433 " and was buried " long(e)
434 if (dosources) {
435 call do_sources(e)
436 }
437 }
438 ".\n"
439 } elsif (e, burial(indi)) {
440 " " givens(indi) " was buried " long(e)
441 if (dosources) {
442 call do_sources(e)
443 }
444 ".\n"
445 }
446 /*
447 * all occupations are given if any are found.
448 */
449 if (occupation) {
450 call do_occupation(indi)
451 }
452 /* if doing notes, make sure we get them now */
453 if (donotes) {
454 call do_notes(indi, donotes, 0)
455 families(indi, famvar, spvar, cnt) {
456 if (spvar) {
457 call do_notes(spvar, donotes, 0)
458 }
459 }
460 }
461 /*
462 * now the children
463 * starting a table is a new paragraph. Keep it all together
464 * and put in a label cell in first row. Then dump
465 * each child into a row.
466 */
467 call rtf_tstart(4)
468 call rtf_para_keepnext()
469 if (or(eq(nmarr, 1), not(childby))) {
470 call rtf_cstart()
471 call rtf_cstart()
472 call rtf_cstart()
473 call rtf_cstart()
474 " Children:"
475 call rtf_cend()
476 }
477 set(numchildren, 1)
478 set(tsize, totalchildren(indi))
479 set(fcnt, 0)
480 families(indi, famvar, spvar, cnt) {
481 /*
482 * if childby is set, then put spouse info
483 * out to identify which family children
484 * came from. Skip families with no children
485 */
486 if (and(childby, gt(nmarr, 1))) {
487 if (not(nchildren(famvar))) {
488 continue()
489 }
490 incr(fcnt)
491 if (gt(fcnt, 1)) {
492 call rtf_endrow()
493 call rtf_endrow()
494 }
495 call rtf_cstart()
496 call rtf_cstart()
497 call rtf_cstart()
498 call rtf_cstart()
499 " Children with "
500 fullname(spvar, 0, 1, 128)
501 ":"
502 call rtf_cend()
503 }
504 children (famvar, ch, num) {
505 /* want to know if this is someone to expand */
506 set(ival, determine(ch, indi))
507 call rtf_endrow()
508 if (lt(numchildren, tsize)) {
509 call rtf_para_keepnext()
510 }
511 /* note that nothing goes in cell 1 */
512 call rtf_cstart()
513 /* start the cell where we do a number */
514 call rtf_cstart()
515 call rtf_para_rightjust()
516
517 /* if the indi is non-zero, then tag it */
518 if (ne(ival, 0)) {
519 person_prefix d(ival) "."
520 }
521
522 /* the roman numeral/child order cell */
523 call rtf_cstart()
524 call rtf_para_rightjust()
525 roman(numchildren) "."
526
527 /* the name and info cell */
528 call rtf_cstart()
529 call rtf_para_leftjust()
530 fullname(ch, 0, 1, 128)
531 if (doindex) {
532 rtf_index(surname(ch), givens(ch), 0)
533 }
534
535 /* we always give birth info */
536 if (b, birth(ch)) {
537 if (gt(ival, 0)) {
538 if (strlen(date(b))) {
539 " b. "
540 date(b)
541 }
542 } else {
543 " b. "
544 long(b)
545 if (dosources) {
546 call do_sources(e)
547 }
548 }
549 }
550
551 /*
552 * if a non-expanded indi, give more info
553 * such as death, marriages, etc. If
554 * expanded, don't since the full record
555 * will contain it.
556 */
557 if (eq(ival, 0)) {
558 if (e, death(ch)) {
559 if (b) { "," }
560 " d. " long(e)
561 if (dosources) {
562 call do_sources(e)
563 }
564 }
565 set(nsp, nfamilies(ch))
566 /* all known spouses */
567 spouses(ch, sp, fm, cnt) {
568 "; m. "
569 if (gt(nsp, 1)) {
570 "(" d(cnt) ") "
571 }
572 if (e, marriage(fm)) {
573 long(e)
574 if (dosources) {
575 call do_sources(e)
576 }
577 ", "
578 }
579 fullname(sp, 0, 1, 128)
580 if (doindex) {
581 rtf_index(surname(sp),
582 givens(sp), 2)
583 }
584 }
585 ". "
586 if (donotes) {
587 call do_notes(ch, donotes, 1)
588 }
589 } else {
590 "."
591 }
592 call rtf_cend()
593 incr(numchildren)
594 }
595 }
596 call rtf_tend()
597 call rtf_pend()
598 }
599 call rtf_para_space(0, 0)
600 }
601
602 /*
603 * next_indi(indi)
604 * find the next unique number for this individual
605 * the global curr_index keeps the current value
606 * the inum_set keeps track of the indi/number pairs
607 */
608 proc next_indi(indi)
609 {
610 set(curr_index, add(curr_index, 1))
611 addtoset(inum_set, indi, curr_index)
612 }
613
614 /*
615 * inum(indi)
616 * find the unique number for this indi
617 * if there is one it is in inum_set
618 * zero is returned if there isn't a mapping
619 */
620 func inum(indi)
621 {
622 forindiset(inum_set, indvar, inumval, cnt) {
623 if (eq(indvar, indi)) {
624 return (inumval)
625 }
626 }
627 return (0)
628 }
629
630 /*
631 * find_fam(indi, spouse)
632 * find the family (fam) indi and spouse create
633 */
634 func find_fam(indi, sps)
635 {
636 spouses (indi, s, f, c) {
637 if (eq(sps, s)) {
638 return (f)
639 }
640 }
641 }
642
643 /*
644 * check_marriages(indi)
645 * check to see if an individual has any marriages and return
646 * the inum of the first spouse that has one
647 */
648 func check_marriages(indi)
649 {
650 set(res, 0)
651 set(notyet, 0)
652 families (indi, f, s, c) {
653 if (x, check_prev(f)) {
654 incr(res)
655 } else {
656 incr(notyet)
657 }
658 }
659 if (and(res, not(notyet))) {
660 return (1)
661 } else {
662 return (0)
663 }
664 }
665
666 /*
667 * check_prev(fam)
668 * check to see if a previous marriage and return non-zero
669 * if there was one and zero if none.
670 */
671 func check_prev(fam)
672 {
673 forlist(prev_fam_list, f, cnt) {
674 if (eq(fam, f)) {
675 return (cnt)
676 }
677 }
678 return (0)
679 }
680
681 /*
682 * determine(indi, par)
683 * determine if the indi is one to expand.
684 * The par is the parent descended from so that
685 * female lines can be skipped if strictness is
686 * set.
687 */
688 func determine(indi, par)
689 {
690 if (and(eq(strictness, 2), female(indi))) {
691 /* strictest form doesn't follow female lines */
692 return (0)
693 }
694 if (and(eq(strictness, 1), female(par))) {
695 /* modified form gives one generation from a female line */
696 return (0)
697 }
698 set(nchil, 0)
699 families (indi, fm, sp, cnt) {
700 set(nchil, add(nchil, nchildren(fm)))
701 }
702 if (gt(nchil, 0)) {
703 enqueue(nextgen, indi)
704 call next_indi(indi)
705 return (inum(indi))
706 }
707 return (0)
708 }
709
710 /*
711 * do_notes(indi, where, type)
712 * where is inline vs. end
713 * type is in or out of table
714 */
715 proc do_notes(indi, where, type)
716 {
717 /* where == 1 is inline */
718 if (eq(where, 1)) {
719 set(didpara, 0)
720 set(innote, 0)
721 set(root, inode(indi))
722 traverse(root, node, level) {
723 if (and(innote, le(level, innote))) {
724 set(innote, 0)
725 }
726 if (eqstr(tag(node), "NOTE")) {
727 if (not(type)) {
728 call rtf_pstart(3)
729 } else {
730 call rtf_cpar()
731 }
732 set(innote, level)
733 call fixstring(value(node))
734 set(didpara, 1)
735 } elsif (eqstr(tag(node), "CONT")) {
736 if (innote) {
737 if (eq(0, strlen(value(node)))) {
738 if (not(type)) {
739 call rtf_pstart(3)
740 } else {
741 call rtf_cpar()
742 }
743 } else {
744 " \n"
745 call fixstring(value(node))
746 }
747 }
748 }
749 }
750 } elsif (eq(where, 2)) { /* where == 2 is at end */
751 set(found, 0)
752 set(tagprefix, 0)
753 set(root, inode(indi))
754 traverse(root, node, level) {
755 if (eqstr(tag(node), "NOTE")) {
756 if (not(found)) {
757 " ["
758 } else {
759 ", "
760 }
761 incr(found)
762 if (not(tagprefix)) {
763 set(tagprefix, tagname(indi))
764 }
765 tagprefix d(found)
766 }
767 }
768 if (found) {
769 "]"
770 }
771 }
772 }
773
774 /*
775 * fixstring(str)
776 * fix the string to not break RTF output
777 * Any {, }, or \ characters must be escaped.
778 * Then output the string
779 */
780 proc fixstring(str)
781 {
782 if (i, index(str, "{", 1)) {
783 call fixstring(substring(str, 1, i))
784 "\\{"
785 incr(i)
786 call fixstring(substring(str, i, sub(strlen(str), i)))
787 } elsif (i, index(str, "}", 1)) {
788 call fixstring(substring(str, 1, i))
789 "\\}"
790 incr(i)
791 call fixstring(substring(str, i, sub(strlen(str), i)))
792 } elsif (i, index(str, "\\", 1)) {
793 call fixstring(substring(str, 1, i))
794 "\\\\"
795 incr(i)
796 call fixstring(substring(str, i, sub(strlen(str), i)))
797 } else {
798 str
799 }
800 }
801
802 /*
803 * prevmarr(indi)
804 * determine if an indi had a previously output marriage.
805 */
806 func prevmarr(indi)
807 {
808 spouses (indi, s, f, c) {
809 forlist (prev_fam_list, fm, cnt) {
810 if (eq(f, fm)) {
811 return (cnt)
812 }
813 }
814 }
815 return (0)
816 }
817
818 /*
819 * dotitle(author, title)
820 */
821 proc dotitle(author, title)
822 {
823 "\\titlepg"
824 "\\pvmrg\\posy2880\\qc\\fs48 "
825 title
826 "\\line\\line "
827 "\\fs32 by\\line "
828 author
829 "\\par\\sect\\pgnrestart\n"
830 }
831
832 /*
833 * tagname(indi)
834 * from an indi, create a unique tag to use for notes references
835 * for endnote form.
836 */
837 func tagname(indi)
838 {
839 list(parts)
840 /*
841 * the algorithm is:
842 * first 3 letters of surname
843 * first letter of first and any middle name
844 * if conflict, try adding "a", "b", etc. until unique.
845 */
846 extractnames(inode(indi), parts, nparts, surpart)
847 set(surnm, substring(getel(parts, surpart), 1, 3))
848 set(firstp, substring(getel(parts, 1), 1, 1))
849 if (gt(nparts, 2)) {
850 set(midp, substring(getel(parts, 2), 1, 1))
851 if (not(strcmp(midp, "\""))) {
852 set(midp, substring(midp, 2, 2))
853 }
854 } else {
855 set(midp, "")
856 }
857 set(tagvar, concat(surnm, firstp, midp))
858 set(suffix, "")
859 set(v, 0)
860 while (lookup(endtags, concat(tagvar, suffix))) {
861 incr(v)
862 set(suffix, substring("abcdefghijklmnopqrstuvwxyz",
863 v, v))
864 }
865 insert(endtags, tagvar, indi)
866 call sorttag(tagvar)
867 set(tagvar, concat(tagvar, suffix))
868 return (tagvar)
869 }
870
871 /*
872 * sorttag(str)
873 * do an insertion sort of str into the taglist list of notes
874 */
875 proc sorttag(str)
876 {
877 list(tmp)
878 set(done, 0)
879 set(any, 0)
880 while (l, dequeue(taglist)) {
881 set(any, 1)
882 if (not(done)) {
883 set(r, strcmp(str, l))
884 if (le(r, 0)) {
885 set(done, 1)
886 enqueue(tmp, str)
887 }
888 if (ne(r, 0)) {
889 enqueue(tmp, l)
890 }
891 } else {
892 enqueue(tmp, l)
893 }
894 }
895
896 if (or(not(any), not(done))) {
897 enqueue(tmp, str)
898 set(any, 1)
899 }
900
901 /* set to null so we can copy the new list */
902 list(taglist)
903 if (any) {
904 while (l, dequeue(tmp)) {
905 enqueue(taglist, l)
906 }
907 }
908 }
909
910 /*
911 * endnotes()
912 * at end, dump the endnotes in a reasonable format
913 */
914 proc endnotes()
915 {
916 call rtf_newpage()
917 call rtf_para_indent(0, 0)
918 call rtf_pstart(1)
919 call rtf_hstart()
920 "Notes"
921 call rtf_hend()
922 call rtf_pend()
923 while (l, dequeue(taglist)) {
924 set(indi, lookup(endtags, l))
925 if (indi) {
926 call dumpnote(indi, l)
927 }
928 }
929 }
930
931 /*
932 * dumpnote(indi, tagstr)
933 * dump the notes for this indi, using tagstr as the prefix
934 */
935 proc dumpnote(indi, tagstr)
936 {
937 set(didpara, 0)
938 set(innote, 0)
939 set(root, inode(indi))
940 set(which, 0)
941 traverse(root, node, level) {
942 if (and(innote, le(level, innote))) {
943 set(innote, 0)
944 }
945 if (nestr(tag(node), "NOTE")) {
946 call rtf_pstart(3)
947 call rtf_para_indent(neg(1440), 1440)
948 incr(which)
949 set(innote, level)
950 tagstr d(which)
951 rtf_tab(0)
952 value(node)
953 set(didpara, 1)
954 } elsif (nestr(tag(node), "CONT")) {
955 if (innote) {
956 if (eq(0, strlen(value(node)))) {
957 call rtf_pstart(3)
958 } else {
959 " \n" value(node)
960 }
961 }
962 }
963 }
964 }
965
966 /*
967 * totalchildren(indi)
968 * count all the children this indi had
969 */
970 func totalchildren(indi)
971 {
972 set(total, 0)
973 families (indi, fam, sp, cnt) {
974 set(total, add(total, nchildren(fam)))
975 }
976 return (total)
977 }
978
979 /*
980 * do_sources(e)
981 * find all the sources associated with the event
982 * and create the footnote reference. If dosources is
983 * greater than 1, just gather the footnotes to stick at
984 * the end of family rather than in-line for each event
985 * {mode 2 not implemented yet}
986 */
987 proc do_sources(e)
988 {
989 set(evlist, sources(e))
990 list(taglist)
991 while (s, dequeue(evlist)) {
992 set(srcvar, fmt_source(s))
993 set(taglist, source_process(srcvar))
994 }
995 if (not(empty(taglist))) {
996 call rtf_super(1)
997 set(pre, "")
998 forlist(taglist, var, cnt) {
999 pre d(var)
1000 set(pre, ", ")
1001 }
1002 call rtf_super(0)
1003 }
1004 }
1005
1006 /*
1007 * fmt_source(s)
1008 * for a source node, traverse it and put into a normalized
1009 * reference/footnote format. New forms should be added as
1010 * necessary since there are lots of possibilities.
1011 */
1012 func fmt_source(s)
1013 {
1014 set(prefix, "")
1015 set(cont, "")
1016 set(result, "")
1017 set(title,0)
1018 set(sour, 0)
1019 set(dt, 0)
1020 set(text, 0)
1021 set(publ, 0)
1022 set(page, 0)
1023 traverse (s, node, l) {
1024 if (gt(l, 2)) {
1025 continue()
1026 }
1027 if (reference(value(node))) {
1028 set(indresult, fmt_source(dereference(value(node))))
1029 } else {
1030 if (eq(l, 0)) {
1031 continue()
1032 } elsif (eqstr(tag(node), "SOUR")) {
1033 set(sour, text_node(node))
1034 } elsif (eqstr(tag(node), "TEXT")) {
1035 set(text, text_node(node))
1036 } elsif (eqstr(tag(node), "DATE")) {
1037 set(dt, date(node))
1038 } elsif (eqstr(tag(node), "TITL")) {
1039 set(title, text_node(node))
1040 } elsif (eqstr(tag(node), "PAGE")) {
1041 set(page, concat("page ", value(node)))
1042 }
1043 }
1044 }
1045 set(result, "")
1046 if (indresult) {
1047 set(result, indresult)
1048 set(prefix, ", ")
1049 }
1050 if (title) {
1051 set(result, concat(result, prefix, title))
1052 set(prefix, ", ")
1053 }
1054 if (sour) {
1055 set(result, concat(result, prefix, sour))
1056 set(prefix, ", ")
1057 }
1058 if (dt) {
1059 set(result, concat(result, prefix, dt))
1060 set(prefix, ", ")
1061 }
1062 if (text) {
1063 set(result, concat(result, prefix, text))
1064 set(prefix, ", ")
1065 }
1066 if (publ) {
1067 set(result, concat(result, prefix, publ))
1068 set(prefix, ", ")
1069 }
1070 if (page) {
1071 set(result, concat(result, prefix, page))
1072 set(prefix, ", ")
1073 }
1074 return (result)
1075 }
1076
1077 /*
1078 * sources(e)
1079 * for an event, look for all source nodes and make a list
1080 * to return.
1081 */
1082 func sources(ev)
1083 {
1084 list(evs)
1085 if (not(ev)) {
1086 return (evs)
1087 }
1088 set(cnt, 0)
1089 traverse(ev, node, lev) {
1090 if (eqstr(tag(node), "SOUR")) {
1091 enqueue(evs, node)
1092 incr(cnt)
1093 }
1094 }
1095 return (evs)
1096 }
1097
1098 /*
1099 * source_process(src)
1100 * look for the string src in the list of known sources
1101 * if it exists, use that index. If it doesn't add to list
1102 * and use the new index. Then remove duplicate entries
1103 * and ultimately return the list of uniqe references.
1104 */
1105 func source_process(src)
1106 {
1107 list(taglist)
1108 set(found,0)
1109 forlist(all_sources, str, cnt) {
1110 if (eqstr(str, src)) {
1111 set(found, cnt)
1112 break()
1113 }
1114 }
1115 if (not(found)) {
1116 enqueue(all_sources, src)
1117 incr(cnt)
1118 set(taglist, addtolist(taglist, cnt))
1119 } else {
1120 set(taglist, addtolist(taglist, found))
1121 }
1122 return (taglist)
1123 }
1124
1125 /*
1126 * addtolist(lst, num)
1127 * add the value "num" to the list "lst" if
1128 * it isn't already there.
1129 */
1130 func addtolist(lst, num)
1131 {
1132 set(found, 0)
1133 list(newlist)
1134 forlist(lst, val, cnt) {
1135 if (not(found)) {
1136 if (eq(val, num)) {
1137 return (lst) /* no change - a dup */
1138 } elsif (gt(val, num)) {
1139 set(found, 1)
1140 enqueue(newlist, num)
1141 enqueue(newlist, val)
1142 }
1143 } else {
1144 enqueue(newlist, val)
1145 }
1146 }
1147 if (not(found)) {
1148 enqueue(newlist, num)
1149 }
1150 return (newlist)
1151 }
1152 /*
1153 * dump_sources()
1154 * dump the entire list of reference sources with proper tags.
1155 */
1156 proc dump_sources()
1157 {
1158 if (not(empty(all_sources))) {
1159 call rtf_pend()
1160 call rtf_newpage()
1161 call rtf_para_indent(0, 0)
1162 call rtf_pstart(1)
1163 call rtf_hstart()
1164 "References"
1165 call rtf_hend()
1166 call rtf_pend()
1167 forlist(all_sources, src, num) {
1168 call rtf_pstart(3)
1169 call rtf_para_indent(neg(540), 540)
1170 d(num)
1171 call rtf_tab(0)
1172 src
1173 call rtf_pend()
1174 }
1175 }
1176 }
1177
1178 /*
1179 * text_node(node)
1180 * convert a text type node (TEXT or SOUR) into a long
1181 * string with CONT entries separated by space.
1182 */
1183 func text_node(node)
1184 {
1185 set(result, "")
1186 set(prefix, "")
1187 traverse(node, n, l) {
1188 set(result, concat(result, prefix, value(n)))
1189 set(prefix, " ")
1190 }
1191 return (result)
1192 }
1193
1194 /*
1195 * do_occupation(ind)
1196 * print out occupation(s) of the individual in
1197 * a meaningful form.
1198 */
1199 proc do_occupation(indi)
1200 {
1201 list(occu)
1202 set(count, 0)
1203 traverse (inode(indi), node, lev) {
1204 if (eqstr(tag(node), "OCCU")) {
1205 /* have an occupation */
1206 enqueue(occu, value(node))
1207 incr(count)
1208 }
1209 }
1210 if (not(empty(occu))) {
1211 " "
1212 pn(indi, 0)
1213 " was a "
1214 set(sep, "")
1215 forlist(occu, item, cnt) {
1216 item sep
1217 if (eq(count, add(cnt, 1))) {
1218 set(sep, ", and ")
1219 } else {
1220 set(sep, ", ")
1221 }
1222 }
1223 ". "
1224 }
1225 }