;; search highlight / snippet — Haskell source fragment. Depends on tokenize. ;; Operates on document text (not the index): marks query-matching tokens with ;; [..] and extracts a context window around the first match. Tokens are ;; normalized (lowercase, punctuation-stripped) by `tokens`, matching index side. ;; highlight :: [Term] -> String -> String ;; snippet :: Int -> [Term] -> String -> String (ctx tokens each side of 1st match) (define search/highlight-src "hlMark terms t = if elem t terms then \"[\" ++ t ++ \"]\" else t\nhighlight terms text = unwords (map (hlMark terms) (tokens text))\nhlIdxFrom terms [] i = 0 - 1\nhlIdxFrom terms (t:ts) i = if elem t terms then i else hlIdxFrom terms ts (i + 1)\nhlIdx terms toks = hlIdxFrom terms toks 0\nhlMax0 x = if x < 0 then 0 else x\nsnipStart ctx i = if i < 0 then 0 else hlMax0 (i - ctx)\nsnipToks ctx terms toks = unwords (map (hlMark terms) (take (2 * ctx + 1) (drop (snipStart ctx (hlIdx terms toks)) toks)))\nsnippet ctx terms text = snipToks ctx terms (tokens text)\n")