Skip to content

Commit 1d512b1

Browse files
committed
תקן שיוך סימן עמוד בכותרות PDF
1 parent 118aaa6 commit 1d512b1

2 files changed

Lines changed: 97 additions & 24 deletions

File tree

‎lib/pdf_book/view/pdf_outlines_screen.dart‎

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,56 @@ List<PdfOutlineSearchEntry> filterPdfOutline(
4747
// הנרמול מוחק "." ו-":", ולכן "ב." היה תואם כל כותרת עם האות ב ו"קיא:"
4848
// היה שווה ל"קיא.". שאילתה שמסתיימת בסימן עמוד מחפשת את הדף כמילה שלמה.
4949
final amud = _amudMark(rawQuery);
50-
final daf = amud == null
51-
? null
52-
: normalizedQuery.substring(normalizedQuery.lastIndexOf(' ') + 1);
53-
final matches = <({int rank, int index})>[];
54-
for (var i = 0; i < entries.length; i++) {
55-
final title = entries[i].normalizedTitle;
56-
if (!title.contains(normalizedQuery)) continue;
57-
if (daf != null && !_titleHasDaf(entries[i].node.title, daf, amud!)) {
58-
continue;
50+
final dafStart = normalizedQuery.lastIndexOf(' ') + 1;
51+
final daf = amud == null ? null : normalizedQuery.substring(dafStart);
52+
final exact = <PdfOutlineSearchEntry>[];
53+
final exactUnmarked = <PdfOutlineSearchEntry>[];
54+
final prefix = <PdfOutlineSearchEntry>[];
55+
final prefixUnmarked = <PdfOutlineSearchEntry>[];
56+
final contains = <PdfOutlineSearchEntry>[];
57+
final containsUnmarked = <PdfOutlineSearchEntry>[];
58+
for (final entry in entries) {
59+
final title = entry.normalizedTitle;
60+
final firstMatch = title.indexOf(normalizedQuery);
61+
final rank = title == normalizedQuery
62+
? 0
63+
: firstMatch == 0
64+
? 1
65+
: firstMatch > 0
66+
? 2
67+
: -1;
68+
if (rank < 0) continue;
69+
final dafMatch = daf == null
70+
? (matches: true, marked: false)
71+
: _titleDafMatch(
72+
entry.node.title,
73+
title,
74+
normalizedQuery,
75+
firstMatch,
76+
daf,
77+
dafStart,
78+
amud!,
79+
);
80+
if (!dafMatch.matches) continue;
81+
final prefersMarked = amud != null && dafMatch.marked;
82+
switch (rank) {
83+
case 0:
84+
(prefersMarked ? exact : exactUnmarked).add(entry);
85+
case 1:
86+
(prefersMarked ? prefix : prefixUnmarked).add(entry);
87+
default:
88+
(prefersMarked ? contains : containsUnmarked).add(entry);
5989
}
60-
final rank = findNormalizedTextMatchRank(
61-
normalizedQuery: normalizedQuery,
62-
normalizedPrimaryText: title,
63-
);
64-
matches.add((rank: rank, index: i));
6590
}
66-
// מדויק, תחילית ואז הכלה; בתוך כל דרגה — סדר תוכן העניינים.
67-
matches.sort(
68-
(a, b) => a.rank != b.rank ? a.rank - b.rank : a.index - b.index,
69-
);
70-
return [for (final m in matches) entries[m.index]];
91+
// מדויק, תחילית ואז הכלה. סימן עמוד תואם קודם באותה דרגה.
92+
return [
93+
...exact,
94+
...exactUnmarked,
95+
...prefix,
96+
...prefixUnmarked,
97+
...contains,
98+
...containsUnmarked,
99+
];
71100
}
72101

73102
String? _amudMark(String text) {
@@ -78,16 +107,43 @@ String? _amudMark(String text) {
78107
}
79108

80109
/// האם יש בכותרת מילה שהיא הדף [daf], בלי סימן עמוד או עם הסימן [amud].
81-
bool _titleHasDaf(String rawTitle, String daf, String amud) {
110+
({bool matches, bool marked}) _titleDafMatch(
111+
String rawTitle,
112+
String normalizedTitle,
113+
String normalizedQuery,
114+
int firstMatch,
115+
String daf,
116+
int dafStart,
117+
String amud,
118+
) {
119+
final matches = <int>{};
120+
for (var start = firstMatch; start >= 0;) {
121+
matches.add(start + dafStart);
122+
start = normalizedTitle.indexOf(normalizedQuery, start + 1);
123+
}
124+
var normalizedOffset = 0;
125+
var hasWord = false;
126+
var unmarked = false;
82127
for (final word in rawTitle.split(_whitespace)) {
83-
if (normalizeFindText(word) != daf) continue;
84-
final mark = _amudMark(word);
85-
if (mark == null || mark == amud) return true;
128+
final normalizedWord = normalizeFindText(word);
129+
if (normalizedWord.isEmpty) continue;
130+
if (hasWord) normalizedOffset++;
131+
hasWord = true;
132+
final dafOffset = normalizedWord.length - daf.length;
133+
if (normalizedWord.endsWith(daf) &&
134+
(dafOffset == 0 || normalizedWord[dafOffset - 1] == ' ') &&
135+
matches.contains(normalizedOffset + dafOffset)) {
136+
final mark = _amudMark(word.replaceFirst(_closingPunctuation, ''));
137+
if (mark == amud) return (matches: true, marked: true);
138+
if (mark == null) unmarked = true;
139+
}
140+
normalizedOffset += normalizedWord.length;
86141
}
87-
return false;
142+
return (matches: unmarked, marked: false);
88143
}
89144

90145
final RegExp _whitespace = RegExp(r'\s+');
146+
final RegExp _closingPunctuation = RegExp(r'''[)\]}"'׳״”’»›]+$''');
91147

92148
class OutlineView extends StatefulWidget {
93149
const OutlineView({

‎test/pdf_book/pdf_outline_daf_amud_filter_test.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ void main() {
2828
expect(_filter(chullin, 'קיא.'), ['קיא.']);
2929
});
3030

31+
test('סימן עמוד בתוך סוגריים נשמר, וסימן תואם קודם באותה דרגה', () {
32+
expect(_filter(['דף (קיא.)', 'דף (קיא:)', 'דף קיא'], 'קיא:'), [
33+
'דף (קיא:)',
34+
'דף קיא',
35+
]);
36+
expect(_filter(['דף ב', 'דף ב.'], 'ב.'), ['דף ב.', 'דף ב']);
37+
// בלי סימן מפורש נשמר סדר תוכן העניינים.
38+
expect(_filter(['דף ב', 'דף ב.'], 'ב'), ['דף ב', 'דף ב.']);
39+
});
40+
41+
test('סימן העמוד שייך לאותו מופע שבו מסתיימת התאמת השאילתה', () {
42+
expect(_filter(['חלק ב: דף ב.'], 'חלק ב.'), isEmpty);
43+
expect(_filter(['חלק ב: דף ב.'], 'ב.'), ['חלק ב: דף ב.']);
44+
expect(_filter(['דף־ב:', 'דף־ב.'], 'דף ב:'), ['דף־ב:']);
45+
expect(_filter(['יב־ב:'], 'יב:'), isEmpty);
46+
});
47+
3148
test('דף עם עמוד בתוך כותרת ארוכה נמצא, ובלי עמוד — כל הדף', () {
3249
const titles = ['חולין דף ב.', 'חולין דף ב:', 'חולין דף נב.', 'מסכת ב"ב'];
3350
expect(_filter(titles, 'ב:'), ['חולין דף ב:']);

0 commit comments

Comments
 (0)