webapps.wiki: adding search bar
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 10 Aug 2022 22:54:22 +0000 (15:54 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 10 Aug 2022 22:54:22 +0000 (15:54 -0700)
extra/webapps/wiki/search.xml [new file with mode: 0644]
extra/webapps/wiki/wiki-common.xml
extra/webapps/wiki/wiki.css
extra/webapps/wiki/wiki.factor

diff --git a/extra/webapps/wiki/search.xml b/extra/webapps/wiki/search.xml
new file mode 100644 (file)
index 0000000..ffb4310
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version='1.0' ?>
+
+<t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
+
+    <t:title>Search results for '<t:label t:name="search"/>'</t:title>
+
+       <div class="description">
+       <ul>
+               <t:bind-each t:name="results">
+                       <li>
+                               <t:a t:href="$wiki/view" t:rest="title"><t:label t:name="title"/></t:a>
+                       </li>
+               </t:bind-each>
+    </ul>
+    </div>
+
+    <t:if t:value="empty">
+    <p>No results found!</p>
+    </t:if>
+
+</t:chloe>
index 2c572e852b6780e16f56eaf0e5f6256f2dcd2bec..1a04c9bc53f648451057690bbec99961f6d3f9ae 100644 (file)
@@ -2,11 +2,11 @@
 
 <t:chloe xmlns:t="http://factorcode.org/chloe/1.0">
 
-       <t:atom t:href="$wiki/changes.atom">
-               Recent Changes
-       </t:atom>
+    <t:atom t:href="$wiki/changes.atom">
+        Recent Changes
+    </t:atom>
 
-       <t:style t:include="resource:extra/webapps/wiki/wiki.css" />
+    <t:style t:include="resource:extra/webapps/wiki/wiki.css" />
 
     <nav>
         <t:if t:value="contents">
             <t:button t:action="$login-realm/logout" t:aside="begin" class="link-button link">Logout</t:button>
 
         </t:if>
+
+        <t:form t:method="get" t:action="$wiki/search" style="display: inline;">
+            <input placeholder="Search" name="search" type="text"/>
+            <input type="submit" value="Go"/>
+        </t:form>
     </nav>
 
     <div class="page">
index ff23e2907a1e356cb682f17dac17996133568020..4b88a427c2d5c63c304d0c763d6b0660a72d8629 100644 (file)
@@ -130,6 +130,11 @@ nav img {
     display: none;
 }
 
+nav input {
+    border-radius: 3px;
+    padding: 3px;
+}
+
 .page nav {
     width: calc(100vw - 200px);
     max-width: 800px;
@@ -184,6 +189,10 @@ nav img {
         filter: invert(80%);
     }
 
+    nav input {
+        font-size: smaller;
+    }
+
     #left {
         border: none;
         border-left: 1px dashed #666;
index ef7ce8dafdad730846e4aa48905b6ade618378a8..3074915bb04599bfda52c3ebb122bb606c3d687d 100644 (file)
@@ -3,9 +3,10 @@
 USING: accessors calendar db.tuples db.types farkup
 furnace.actions furnace.auth furnace.boilerplate
 furnace.recaptcha furnace.redirection furnace.syndication
-furnace.utilities html.forms http.server.dispatchers
+furnace.utilities html.forms http.server http.server.dispatchers
 http.server.static kernel lcs make namespaces present random
-sequences sorting splitting urls validators ;
+regexp sequences simple-tokenizer sorting splitting unicode urls
+validators ;
 IN: webapps.wiki
 
 : wiki-url ( rest path -- url )
@@ -305,6 +306,33 @@ M: revision feed-entry-url id>> revision-url ;
 
         { wiki "articles" } >>template ;
 
+: <search-articles-action> ( -- action )
+    <page-action>
+
+        [
+            "search" param [ unicode:blank? ] trim
+            dup "search" set-value
+
+            [ f ] [
+                tokenize [
+                    " " "\s+" replace "\\b" dup surround
+                    "i" <optioned-regexp>
+                ] map
+            ] if-empty
+
+            [ f ] [
+                f <article> select-tuples
+                [ title>> ] sort-with
+                [ revision>> <revision> select-tuple ] map
+                swap '[ content>> _ [ first-match ] with all? ] filter
+            ] if-empty
+
+            [ "results" set-value ]
+            [ not "empty" set-value ] bi
+        ] >>init
+
+        { wiki "search" } >>template ;
+
 : list-user-edits ( -- seq )
     f <revision> "author" value >>author select-tuples
     reverse-chronological-order ;
@@ -352,6 +380,7 @@ M: revision feed-entry-url id>> revision-url ;
         <rollback-action> "rollback" add-responder
         <user-edits-action> "user-edits" add-responder
         <list-articles-action> "articles" add-responder
+        <search-articles-action> "search" add-responder
         <list-changes-action> "changes" add-responder
         <user-edits-feed-action> "user-edits.atom" add-responder
         <list-changes-feed-action> "changes.atom" add-responder