@@ -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
73102String ? _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
90145final RegExp _whitespace = RegExp (r'\s+' );
146+ final RegExp _closingPunctuation = RegExp (r'''[)\]}"'׳״”’»›]+$''' );
91147
92148class OutlineView extends StatefulWidget {
93149 const OutlineView ({
0 commit comments