-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathposts_bibtex.go
151 lines (137 loc) · 3.17 KB
/
posts_bibtex.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
package website
import (
"embed"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"path/filepath"
"strings"
"github.com/jschaf/bibtex"
"github.com/jschaf/bibtex/ast"
)
type references []reference
func (r references) cite(key string) (reference, bool) {
for _, ref := range r {
if ref.Key == key {
return ref, true
}
}
return reference{}, false
}
type reference struct {
// Key is the bibtex (or reference) key.
Key string
// Index is the index of the reference in the list of references.
Index int
Type string
Title string
Authors []author
Year string
URL *url.URL
}
type author struct {
First string
Last string
}
func referencesFromBibtex(postsFS embed.FS, path string) (references, error) {
bibPath := strings.TrimSuffix(path, filepath.Ext(path)) + ".bib"
bibFile, err := postsFS.Open(bibPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("opening bib file %s: %w", bibPath, err)
}
return readBibtex(bibFile)
}
func readBibtex(in io.Reader) (references, error) {
bib := bibtex.New(
bibtex.WithResolvers(
// NewAuthorResolver creates a resolver for the "author" field that
// parses author names into an ast.Authors node.
bibtex.NewAuthorResolver("author"),
// RenderParsedTextResolver replaces ast.ParsedText with a
// simplified rendering of ast.Text.
bibtex.NewRenderParsedTextResolver(),
),
)
bibFile, err := bib.Parse(in)
if err != nil {
return nil, err
}
entries, err := bib.Resolve(bibFile)
if err != nil {
return nil, err
}
references := make(references, len(entries))
for i, entry := range entries {
ref := reference{
// References start at 1, not 0. Hence the +1.
Index: i + 1,
Key: entry.Key,
// TODO: validate the type
Type: entry.Type,
}
title, err := entryTagAsString(entry, bibtex.FieldTitle)
if err != nil {
return nil, err
}
ref.Title = title
authorsTag, ok := entry.Tags[bibtex.FieldAuthor]
if !ok {
return nil, fmt.Errorf(
"missing field %q in entry %q",
bibtex.FieldAuthor,
entry.Key,
)
}
authors, ok := authorsTag.(ast.Authors)
if !ok {
return nil, fmt.Errorf(
"field %q is not text in entry %q",
bibtex.FieldAuthor,
entry.Key,
)
}
ref.Authors = make([]author, len(authors))
for i, member := range authors {
ref.Authors[i] = author{
First: member.First.(*ast.Text).Value,
Last: member.Last.(*ast.Text).Value,
}
}
rawURL, err := entryTagAsString(entry, bibtex.Field("url"))
if err != nil {
return nil, err
}
year, err := entryTagAsString(entry, bibtex.FieldYear)
if err != nil {
return nil, err
}
ref.Year = year
url, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("parsing URL %q: %w", rawURL, err)
}
ref.URL = url
references[i] = ref
}
return references, nil
}
func entryTagAsString(entry bibtex.Entry, field bibtex.Field) (string, error) {
tag, ok := entry.Tags[field]
if !ok {
return "", fmt.Errorf("missing field %q in entry %q", field, entry.Key)
}
text, ok := tag.(*ast.Text)
if !ok {
return "", fmt.Errorf(
"field %q is not text in entry %q",
field,
entry.Key,
)
}
return text.Value, nil
}