Repository navigation
Expand file tree
/
Copy pathfile.go
More file actions
92 lines (80 loc) · 3.05 KB
/
Copy pathfile.go
File metadata and controls
92 lines (80 loc) · 3.05 KB
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
package bibtex
import "strings"
// File is the result of parsing a BibTeX database or extracting entries from
// a document. It owns its strings; the input buffer may be reused after
// parsing.
type File struct {
Entries []Entry `json:"entries"`
// Strings holds @string definitions in source order, with values expanded.
Strings []Field `json:"strings,omitempty"`
Preambles []string `json:"preambles,omitempty"`
// Encoding is "utf-8", or "windows-1252" when the input was not valid
// UTF-8 and was decoded as Windows-1252.
Encoding string `json:"encoding"`
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}
// Entry is one BibTeX entry such as @article{key, ...}.
type Entry struct {
// Type is lowercased, e.g. "article".
Type string `json:"type"`
// Key is the citation key with its original case.
Key string `json:"key"`
Fields []Field `json:"fields"`
Position
// Start and End are byte offsets of the entry in the input, from the @ to
// just after the closing delimiter.
Start int `json:"start"`
End int `json:"end"`
}
// Field is a field or @string definition. Name is lowercased. Value has
// macros expanded, # concatenation applied, outer delimiters removed and
// whitespace runs collapsed; TeX markup and inner braces are retained.
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Position
}
// Lookup returns the first value for a field name, matched case-insensitively.
func (e *Entry) Lookup(name string) (string, bool) {
for i := range e.Fields {
if strings.EqualFold(e.Fields[i].Name, name) {
return e.Fields[i].Value, true
}
}
return "", false
}
// Get returns the first value for a field name, or "" when it is absent.
func (e *Entry) Get(name string) string {
v, _ := e.Lookup(name)
return v
}
// verbatimFields are biblatex's verbatim fields, where ~ and -- are literal.
var verbatimFields = map[string]bool{"url": true, "doi": true, "eprint": true, "file": true, "pdf": true}
// Text returns a field converted to plain Unicode text with PlainText. The
// url, doi, eprint, file and pdf fields are returned with braces, \url{} and
// escapes such as \_ removed and other characters unchanged.
func (e *Entry) Text(name string) string {
v := e.Get(name)
if verbatimFields[strings.ToLower(name)] {
return verbatim(v)
}
return PlainText(v)
}
var verbatimEscapes = strings.NewReplacer(`\_`, "_", `\%`, "%", `\&`, "&", `\#`, "#", `\$`, "$", `\~`, "~", "{", "", "}", "")
func verbatim(v string) string {
v = strings.TrimSpace(v)
for _, cmd := range []string{`\url`, `\path`, `\href`} {
if rest, ok := strings.CutPrefix(v, cmd); ok {
// \href{target}{text}: keep the target.
if arg, _, found := strings.Cut(strings.TrimPrefix(strings.TrimSpace(rest), "{"), "}"); found {
v = arg
}
break
}
}
return strings.TrimSpace(verbatimEscapes.Replace(v))
}
// Authors returns the parsed author field.
func (e *Entry) Authors() []Name { return ParseNames(e.Get("author")) }
// Editors returns the parsed editor field.
func (e *Entry) Editors() []Name { return ParseNames(e.Get("editor")) }