-
Notifications
You must be signed in to change notification settings - Fork 20
/
util.go
205 lines (157 loc) 路 3.6 KB
/
util.go
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
package html
import (
"fmt"
"github.com/elliotchance/gedcom"
"github.com/elliotchance/gedcom/html/core"
"io"
"strings"
)
const symbolLetter = '#'
func write(w io.Writer, data []byte) (int64, error) {
n, err := w.Write(data)
return int64(n), err
}
func writeString(w io.Writer, data string) (int64, error) {
return write(w, []byte(data))
}
func appendString(w io.Writer, data string) int64 {
n, err := writeString(w, data)
if err != nil {
panic(err)
}
return n
}
func appendComponent(w io.Writer, component core.Component) int64 {
n, err := component.WriteHTMLTo(w)
if err != nil {
panic(err)
}
return n
}
func writeSprintf(w io.Writer, format string, args ...interface{}) (int64, error) {
return writeString(w, fmt.Sprintf(format, args...))
}
func appendSprintf(w io.Writer, format string, args ...interface{}) int64 {
n, err := writeSprintf(w, format, args...)
if err != nil {
panic(err)
}
return n
}
func writeNothing() (int64, error) {
return 0, nil
}
func PageIndividuals(firstLetter rune) string {
if firstLetter == symbolLetter {
return "individuals-symbol.html"
}
return fmt.Sprintf("individuals-%c.html", firstLetter)
}
func PageIndividual(document *gedcom.Document, individual *gedcom.IndividualNode, visibility LivingVisibility) string {
if individual.IsLiving() {
switch visibility {
case LivingVisibilityHide, LivingVisibilityPlaceholder:
return "#"
case LivingVisibilityShow:
// Proceed.
}
}
individuals := GetIndividuals(document)
for key, value := range individuals {
if value.Is(individual) {
return fmt.Sprintf("%s.html", key)
}
}
return "#"
}
func PagePlaces() string {
return "places.html"
}
func PagePlace(document *gedcom.Document, place string) string {
places := GetPlaces(document)
for key, value := range places {
if value.PrettyName == place {
return fmt.Sprintf("%s.html", key)
}
}
return "#"
}
func PageFamilies() string {
return "families.html"
}
func PageSources() string {
return "sources.html"
}
func PageSource(source *gedcom.SourceNode) string {
return fmt.Sprintf("%s.html", source.Pointer())
}
func PageStatistics() string {
return "statistics.html"
}
func PageSurnames() string {
return "surnames.html"
}
func colorForIndividual(individual *gedcom.IndividualNode) string {
if individual == nil {
return "black"
}
sex := individual.Sex()
switch {
case sex.IsMale():
return IndividualMaleColor
case sex.IsFemale():
return IndividualFemaleColor
}
return "black"
}
func colorClassForSex(sex *gedcom.SexNode) string {
switch {
case sex.IsMale():
return "primary"
case sex.IsFemale():
return "danger"
}
return "info"
}
func colorClassForIndividual(individual *gedcom.IndividualNode) string {
if individual == nil {
return "info"
}
return colorClassForSex(individual.Sex())
}
func getUniqueKey(s string) string {
i := -1
for {
i += 1
testString := s
if i > 0 {
testString = fmt.Sprintf("%s-%d", s, i)
}
if _, ok := individualMap[testString]; ok {
continue
}
if _, ok := placesMap[testString]; ok {
continue
}
return testString
}
// This should not be possible
panic(s)
}
func surnameStartsWith(individual *gedcom.IndividualNode, letter rune) bool {
name := individual.Name().Format(gedcom.NameFormatIndex)
if name == "" {
name = "#"
}
lowerName := strings.ToLower(name)
firstLetter := rune(lowerName[0])
return firstLetter == letter
}
func individualForNode(doc *gedcom.Document, node gedcom.Node) *gedcom.IndividualNode {
for _, individual := range doc.Individuals() {
if gedcom.HasNestedNode(individual, node) {
return individual
}
}
return nil
}