37 lines
980 B
Go
37 lines
980 B
Go
package shared
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
)
|
|
|
|
// 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))
|
|
for _, ref := range refs {
|
|
if err := source.ValidateRef(doc, ref); err != nil {
|
|
return "", fmt.Errorf("resolve cited source range: %w", err)
|
|
}
|
|
start, _ := source.UnitIndex(doc, ref.StartUnitID)
|
|
end, _ := source.UnitIndex(doc, ref.EndUnitID)
|
|
for index := start; index <= end; index++ {
|
|
included[index] = true
|
|
}
|
|
}
|
|
|
|
parts := make([]string, 0, len(doc.Units))
|
|
for index, unit := range doc.Units {
|
|
if included[index] {
|
|
parts = append(parts, unit.Text)
|
|
}
|
|
}
|
|
return strings.Join(parts, "\n"), nil
|
|
}
|