forked from gemaraproj/go-gemara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexicon_load.go
More file actions
114 lines (104 loc) · 3.58 KB
/
lexicon_load.go
File metadata and controls
114 lines (104 loc) · 3.58 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package markdown
import (
"context"
"fmt"
"strings"
"github.com/gemaraproj/go-gemara"
"github.com/gemaraproj/go-gemara/fetcher"
"github.com/gemaraproj/go-gemara/internal/codec"
)
// resolveLexiconURL returns the source string (URL or local path) for the lexicon artifact.
// Precedence: metadata.mapping-references entry whose id matches metadata.lexicon.reference-id;
// else metadata.lexicon.remarks if it is a fetchable URL (must use http://, https://, or file://).
func resolveLexiconURL(meta gemara.Metadata) (string, error) {
if meta.Lexicon == nil {
return "", fmt.Errorf("lexicon mapping is nil")
}
refID := strings.TrimSpace(meta.Lexicon.ReferenceId)
for _, mappingRef := range meta.MappingReferences {
if mappingRef.Id == refID && refID != "" {
mappedURL := strings.TrimSpace(mappingRef.Url)
if mappedURL == "" {
return "", fmt.Errorf("mapping-references entry %q has empty url", refID)
}
return mappedURL, nil
}
}
remarks := strings.TrimSpace(meta.Lexicon.Remarks)
if isLexiconFetchURL(remarks) {
return remarks, nil
}
if refID == "" {
return "", fmt.Errorf("metadata.lexicon has empty reference-id and remarks is not a fetchable URL")
}
return "", fmt.Errorf("no mapping-references entry with id %q for metadata.lexicon", refID)
}
// loadLexiconFromURI fetches a Lexicon from an http(s):// URL, a file:// URI,
// or a local file path, and returns normalized entries.
func loadLexiconFromURI(ctx context.Context, uri string) ([]lexiconEntry, error) {
doc, err := gemara.Load[gemara.Lexicon](ctx, &fetcher.URI{}, uri)
if err != nil {
return nil, fmt.Errorf("load lexicon: %w", err)
}
return parseLexiconDocument(doc)
}
func parseLexiconDocument(doc *gemara.Lexicon) ([]lexiconEntry, error) {
if err := validateLexicon(doc); err != nil {
return nil, err
}
return normalizeLexicon(doc)
}
// parseLexiconYAML decodes bytes as a single Gemara Lexicon document and returns normalized entries.
func parseLexiconYAML(data []byte) ([]lexiconEntry, error) {
var doc gemara.Lexicon
if err := codec.UnmarshalYAML(data, &doc); err != nil {
return nil, fmt.Errorf("decode lexicon YAML: %w", err)
}
return parseLexiconDocument(&doc)
}
func validateLexicon(lexDoc *gemara.Lexicon) error {
if lexDoc == nil {
return fmt.Errorf("lexicon is nil")
}
if len(lexDoc.Terms) == 0 {
return fmt.Errorf("lexicon has no terms")
}
for termIdx, term := range lexDoc.Terms {
if strings.TrimSpace(term.Title) == "" && strings.TrimSpace(term.Id) == "" {
return fmt.Errorf("lexicon terms[%d]: title and id are both empty", termIdx)
}
if strings.TrimSpace(term.Definition) == "" {
return fmt.Errorf("lexicon terms[%d]: definition is empty", termIdx)
}
for refIdx, refLine := range term.References {
if strings.TrimSpace(refLine.Citation) == "" {
return fmt.Errorf("lexicon terms[%d].references[%d]: citation is empty", termIdx, refIdx)
}
}
}
return nil
}
func normalizeLexicon(lexDoc *gemara.Lexicon) ([]lexiconEntry, error) {
seen := make(map[string]struct{})
out := make([]lexiconEntry, 0, len(lexDoc.Terms))
for termIdx, term := range lexDoc.Terms {
canonical := strings.TrimSpace(term.Title)
if canonical == "" {
canonical = strings.TrimSpace(term.Id)
}
if err := markGemaraCanonicalSeen(seen, canonical); err != nil {
return nil, err
}
syns, err := trimSynonyms(term.Synonyms, termIdx, "lexicon terms")
if err != nil {
return nil, err
}
out = append(out, lexiconEntry{
Canonical: canonical,
Definition: strings.TrimSpace(term.Definition),
Synonyms: syns,
Refs: refLinesFromGemara(term.References),
})
}
return out, nil
}