summaryrefslogtreecommitdiffstats
path: root/database.go
blob: 945f9eb7a1b4da43196eaf550398c5db73d49a59 (plain)
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
package bibtex

import (
	"bytes"
	"strconv"
	"strings"
)

type Value interface {
	Marshal() string
	String() string
}

type BraceLiteral string
type StringLiteral string
type NumberLiteral int
type VarLiteral struct {
	Name  string
	Value *Value
}

type LiteralList []Value

func (l BraceLiteral) String() string {
	return string(l)
}

func (l BraceLiteral) Marshal() string {
	return "{" + string(l) + "}"
}

func (l StringLiteral) String() string {
	return string(l)
}

func (l StringLiteral) Marshal() string {
	return "\"" + string(l) + "\""
}

func (l NumberLiteral) Marshal() string {
	return strconv.Itoa(int(l))
}

func (l NumberLiteral) String() string {
	return strconv.Itoa(int(l))
}

func (l VarLiteral) String() string {
	return (*l.Value).String()
}

func (l VarLiteral) Marshal() string {
	return l.Name
}

func (l LiteralList) String() string {
	var buf bytes.Buffer
	for _, lit := range l {
		buf.WriteString(lit.String())
	}
	return buf.String()
}

func (l LiteralList) Marshal() string {
	res := make([]string, len(l))
	for i, lit := range l {
		res[i] = lit.Marshal()
	}
	return strings.Join(res, " # ")
}

func flatten(v Value) Value {
	switch t := v.(type) {
	case LiteralList:
		if len(t) == 1 {
			return flatten(t[0])
		} else {
			return BraceLiteral(t.String())
		}
	case VarLiteral:
		return flatten(*t.Value)
	default:
		return v
	}
}

type Entry struct {
	Type   string           `json:"type"`
	Key    string           `json:"key"`
	Fields map[string]Value `json:"fields"`
	FNames []string         `json:"-"`
}

func (e Entry) Marshal() string {
	var buf bytes.Buffer
	temp := make([]string, len(e.FNames))
	open, close := "{", "}"
	if strings.Contains(e.Key, "}") {
		open, close = "(", ")"
	}
	buf.WriteString("@" + e.Type + open + e.Key + ",")
	if len(e.FNames) == 0 {
		buf.WriteString(close)
		return buf.String()
	} else {
		buf.WriteString("\n\t")
	}

	for i, field := range e.FNames {
		temp[i] = field + " = " + e.Fields[field].Marshal()
	}
	buf.WriteString(strings.Join(temp, ",\n\t"))
	buf.WriteString("\n" + close)
	return buf.String()
}

type Database struct {
	SNames    []string         `json:"-"`
	Strings   map[string]Value `json:"strings,omitempty"`
	Entries   map[string]Entry `json:"entries"`
	Preamble  Value            `json:"preamble,omitempty"`
	EKeys     []string         `json:"-"`
	CrossRefs map[string]int   `json:"crossrefs,omitempty"`
}

func (d *Database) UnTex() {
	for _, entry := range d.Entries {
		for field, value := range entry.Fields {
			switch v := value.(type) {
			case BraceLiteral:
				entry.Fields[field] = BraceLiteral(UnTex(v.String()))
			case StringLiteral:
				entry.Fields[field] = StringLiteral(UnTex(v.String()))
			case Names:
				v.untex()
			}
		}
	}
}

func (d *Database) Flatten() {
	for _, entry := range d.Entries {
		for field, value := range entry.Fields {
			entry.Fields[field] = flatten(value)
		}
	}
}

func (d *Database) SplitNames() {
	for _, entry := range d.Entries {
		for field, value := range entry.Fields {
			if field == "author" || field == "editor" {
				entry.Fields[field] = SplitNames(value.String())
			}
		}
	}
}

func (d *Database) Resolve() {
	var key string
	var ref Entry
	for _, entry := range d.Entries {
		for field, value := range entry.Fields {
			if field != "crossref" {
				continue
			}
			key = strings.ToLower(value.String())
			ref = d.Entries[key]
			for f, v := range ref.Fields {
				if _, in := entry.Fields[f]; !in {
					entry.Fields[f] = v
					entry.FNames = append(entry.FNames, f)
				}
			}
		}
	}
}