]> gitweb.factorcode.org Git - factor.git/blob - basis/help/search/search.factor
core: subseq-index? -> subseq-of?
[factor.git] / basis / help / search / search.factor
1 ! Copyright (C) 2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
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 ;
8
9 IN: help.search
10
11 <PRIVATE
12
13 : search-words ( str -- seq )
14     >lower "-" split [ [ blank? ] split-when ] map concat ;
15
16 : element-value ( element -- str )
17     dup array? [
18         dup ?first {
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 ] }
27             [ 2drop f ]
28         } case
29     ] [ dup string? [ drop f ] unless ] if ;
30
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  ;
35
36 : (search-articles) ( string -- seq' )
37     search-words [ { } ] [
38         [ all-articles ] dip
39         dup length 1 > [
40             '[ article-words _ subseq-of? ] filter
41         ] [
42             first '[ article-words [ _ head? ] any? ] filter
43         ] if
44     ] if-empty [ article-name ] sort-with ;
45
46 PRIVATE>
47
48 : search-articles ( string -- )
49     [
50         last-element off
51         [
52             "Search results for “" "”" surround
53             title-style get [ format ] ($block)
54         ]
55         [
56             (search-articles) [ word? ] partition swap
57             "Articles" "Words"
58             [ over empty? [ 2drop ] [ $heading $completions ] if ]
59             bi-curry@ bi*
60         ] bi
61     ] with-default-style nl ;