| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode/utf16" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // Snippet is "In the text" (level-up §4.6): a block's words near a |
| 11 | // match, and the spans to mark. Marks are [start, end) offsets in |
| 12 | // UTF-16 code units, which is how a script indexes a string, so the |
| 13 | // script marks the text by slicing it and never parses markup. |
| 14 | type Snippet struct { |
| 15 | Text string `json:"text"` |
| 16 | Marks [][2]int `json:"marks"` |
| 17 | } |
| 18 | |
| 19 | // SnippetPart is a run of a snippet's text, marked or not: what the |
| 20 | // scriptless page renders. |
| 21 | type SnippetPart struct { |
| 22 | Text string |
| 23 | Marked bool |
| 24 | } |
| 25 | |
| 26 | // snippetWidth is how many characters a snippet holds, and lead how many |
| 27 | // of them come before the match. |
| 28 | const ( |
| 29 | snippetWidth = 160 |
| 30 | snippetLead = 50 |
| 31 | ) |
| 32 | |
| 33 | // snippet is the first block that holds a token, cut to fit, with every |
| 34 | // word a token starts marked; nil if no block holds one. |
| 35 | func snippet(page *entry, tokens []string) *Snippet { |
| 36 | for _, block := range page.blocks { |
| 37 | text := strings.Join(strings.Fields(markup.Words(block)), " ") |
| 38 | found := wordsOf(text) |
| 39 | for _, item := range found { |
| 40 | if longestToken(item.fold, tokens) != "" { |
| 41 | return marked(text, found, item.start, tokens) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | // marked is up to snippetWidth characters of text near byte offset at. |
| 49 | func marked(text string, found []word, at int, tokens []string) *Snippet { |
| 50 | start, end := window(text, at) |
| 51 | out := &Snippet{Marks: [][2]int{}} |
| 52 | if start > 0 { |
| 53 | out.Text = "…" |
| 54 | } |
| 55 | lead := units(out.Text) |
| 56 | out.Text += text[start:end] |
| 57 | if end < len(text) { |
| 58 | out.Text += "…" |
| 59 | } |
| 60 | for _, item := range found { |
| 61 | if item.start >= end { |
| 62 | break |
| 63 | } |
| 64 | token := longestToken(item.fold, tokens) |
| 65 | if token == "" || item.start < start { |
| 66 | continue |
| 67 | } |
| 68 | stop := item.start + prefixBytes(text[item.start:item.end], token) |
| 69 | if stop > end { |
| 70 | break |
| 71 | } |
| 72 | out.Marks = append(out.Marks, [2]int{ |
| 73 | lead + units(text[start:item.start]), |
| 74 | lead + units(text[start:stop])}) |
| 75 | } |
| 76 | return out |
| 77 | } |
| 78 | |
| 79 | // window is the byte span of up to snippetWidth runes of text, starting |
| 80 | // snippetLead runes before byte offset at. |
| 81 | func window(text string, at int) (int, int) { |
| 82 | var starts []int |
| 83 | for offset := range text { |
| 84 | starts = append(starts, offset) |
| 85 | } |
| 86 | center := len([]rune(text[:at])) |
| 87 | first := max(0, center-snippetLead) |
| 88 | last := first + snippetWidth |
| 89 | if last >= len(starts) { |
| 90 | return starts[first], len(text) |
| 91 | } |
| 92 | return starts[first], starts[last] |
| 93 | } |
| 94 | |
| 95 | // longestToken is the longest token folded starts, or "". |
| 96 | func longestToken(folded string, tokens []string) string { |
| 97 | best := "" |
| 98 | for _, token := range tokens { |
| 99 | if len(token) > len(best) && strings.HasPrefix(folded, token) { |
| 100 | best = token |
| 101 | } |
| 102 | } |
| 103 | return best |
| 104 | } |
| 105 | |
| 106 | // prefixBytes is how many bytes of word, as written, fold to token: the |
| 107 | // part of the word the query typed. |
| 108 | func prefixBytes(word, token string) int { |
| 109 | for offset := range word { |
| 110 | if offset > 0 && len(Fold(word[:offset])) >= len(token) { |
| 111 | return offset |
| 112 | } |
| 113 | } |
| 114 | return len(word) |
| 115 | } |
| 116 | |
| 117 | // units is text's length in UTF-16 code units. |
| 118 | func units(text string) int { |
| 119 | count := 0 |
| 120 | for _, char := range text { |
| 121 | count += utf16.RuneLen(char) |
| 122 | } |
| 123 | return count |
| 124 | } |
| 125 | |
| 126 | // Parts is the snippet as runs, marked and not, for a template. |
| 127 | func (snip *Snippet) Parts() []SnippetPart { |
| 128 | var parts []SnippetPart |
| 129 | runes := []rune(snip.Text) |
| 130 | at, place := 0, 0 |
| 131 | take := func(stop int, marked bool) { |
| 132 | begin := place |
| 133 | for place < len(runes) && at < stop { |
| 134 | at += utf16.RuneLen(runes[place]) |
| 135 | place++ |
| 136 | } |
| 137 | if place > begin { |
| 138 | parts = append(parts, SnippetPart{Text: string(runes[begin:place]), |
| 139 | Marked: marked}) |
| 140 | } |
| 141 | } |
| 142 | for _, span := range snip.Marks { |
| 143 | take(span[0], false) |
| 144 | take(span[1], true) |
| 145 | } |
| 146 | take(units(snip.Text), false) |
| 147 | return parts |
| 148 | } |
| 149 | |