1 ! Copyright (C) 2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: arrays combinators combinators.short-circuit help
5 help.apropos help.markup help.stylesheet help.topics io
6 io.styles kernel math namespaces sequences sorting splitting
7 strings unicode words ;
13 : search-words ( str -- seq )
14 >lower "-" split [ [ blank? ] split-when ] map concat ;
16 : element-value ( element -- str )
19 { \ $link [ second article-name ] }
20 { \ $vocab-link [ second ] }
21 { \ $emphasis [ second ] }
22 { \ $subsection [ second article-name ] }
23 { \ $subsections [ rest [ article-name ] map join-words ] }
24 { \ $description [ rest [ element-value ] map join-words ] }
25 { \ $notes [ rest [ element-value ] map join-words ] }
26 { \ $snippet [ rest [ element-value ] map join-words ] }
29 ] [ dup string? [ drop f ] unless ] if ;
31 MEMO: article-words ( name -- words )
32 article-content [ element-value ] map join-words search-words
33 [ [ digit? ] all? ] reject
34 [ [ { [ letter? ] [ digit? ] } 1|| not ] trim ] map! harvest ;
36 : (search-articles) ( string -- seq' )
37 search-words [ { } ] [
40 '[ article-words _ swap subseq? ] filter
42 first '[ article-words [ _ head? ] any? ] filter
44 ] if-empty [ article-name ] sort-with ;
48 : search-articles ( string -- )
52 "Search results for “" "”" surround
53 title-style get [ format ] ($block)
56 (search-articles) [ word? ] partition swap
58 [ over empty? [ 2drop ] [ $heading $completions ] if ]
61 ] with-default-style nl ;