citedPassages function
Passages of passages cited in text as [1], [1, 2], [1][2],
[1-3], [1], 【1】, [1、2] or 「1」, in order of first citation
and without repeats. Brackets inside inline code or fenced code blocks
are not citations.
Implementation
List<RagPassage> citedPassages(String text, List<RagPassage> passages) {
final byId = {for (final p in passages) p.id: p};
final cited = <String, RagPassage>{};
void cite(String id) {
final passage = byId[id];
if (passage != null) cited.putIfAbsent(passage.id, () => passage);
}
final prose = text.replaceAllMapped(_code, (m) => ' ' * m[0]!.length);
for (final match in _citation.allMatches(prose)) {
final ids = match
.groups([for (var i = 1; i <= match.groupCount; i++) i])
.firstWhere((g) => g != null)!;
for (final part in ids.split(RegExp('[,,、]'))) {
final bounds = _asciiDigits(
part,
).split(RegExp('[-–~~〜-]')).map((b) => int.tryParse(b.trim())).toList();
if (bounds.any((b) => b == null)) continue;
if (bounds.length == 1) {
cite('${bounds.single}');
continue;
}
final (from, to) = (bounds.first!, bounds.last!);
// Only ids that were given count, so a wide range stays cheap.
for (final p in passages) {
final n = int.tryParse(p.id);
if (n != null && n >= from && n <= to) cite(p.id);
}
}
}
return cited.values.toList();
}