Reuse indexes for D&D citation validation
This commit is contained in:
@@ -7,27 +7,37 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
// CitationResolver resolves cited ranges against one source document.
|
||||
type CitationResolver struct {
|
||||
doc *source.SourceDocument
|
||||
index source.DocumentIndex
|
||||
}
|
||||
|
||||
// NewCitationResolver prepares citation resolution for doc.
|
||||
func NewCitationResolver(doc *source.SourceDocument) (*CitationResolver, error) {
|
||||
if doc == nil {
|
||||
return nil, fmt.Errorf("source document must not be nil")
|
||||
}
|
||||
return &CitationResolver{doc: doc, index: source.NewDocumentIndex(doc)}, nil
|
||||
}
|
||||
|
||||
// CitedText resolves cited source ranges in document order, including each
|
||||
// source unit once, and joins the resulting text with newlines.
|
||||
func CitedText(doc *source.SourceDocument, refs []source.SourceRef) (string, error) {
|
||||
if doc == nil {
|
||||
return "", fmt.Errorf("source document must not be nil")
|
||||
}
|
||||
|
||||
included := make([]bool, len(doc.Units))
|
||||
func (r *CitationResolver) CitedText(refs []source.SourceRef) (string, error) {
|
||||
included := make([]bool, len(r.doc.Units))
|
||||
for _, ref := range refs {
|
||||
if err := source.ValidateRef(doc, ref); err != nil {
|
||||
if err := r.index.ValidateRef(ref); err != nil {
|
||||
return "", fmt.Errorf("resolve cited source range: %w", err)
|
||||
}
|
||||
start, _ := source.UnitIndex(doc, ref.StartUnitID)
|
||||
end, _ := source.UnitIndex(doc, ref.EndUnitID)
|
||||
start, _ := r.index.Position(ref.StartUnitID)
|
||||
end, _ := r.index.Position(ref.EndUnitID)
|
||||
for index := start; index <= end; index++ {
|
||||
included[index] = true
|
||||
}
|
||||
}
|
||||
|
||||
parts := make([]string, 0, len(doc.Units))
|
||||
for index, unit := range doc.Units {
|
||||
parts := make([]string, 0, len(r.doc.Units))
|
||||
for index, unit := range r.doc.Units {
|
||||
if included[index] {
|
||||
parts = append(parts, unit.Text)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user