Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Silly fortune database example |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
0f7cbfc3e626da2b19f0e7ec78126e47 |
User & Date: | murphy 2018-09-01 13:06:22.405 |
Context
2018-09-01
| ||
15:08 | Improved content modification tools check-in: 56a431953b user: murphy tags: trunk | |
13:06 | Silly fortune database example check-in: 0f7cbfc3e6 user: murphy tags: trunk | |
04:23 | Improved dialog interface, injection code check-in: f19371823b user: murphy tags: trunk | |
Changes
Added examples/fortune.scm.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | ;; ;; MIT License ;; ;; Copyright (c) 2018 Thomas Chust ;; ;; Permission is hereby granted, free of charge, to any person obtaining a copy ;; of this software and associated documentation files (the "Software"), to deal ;; in the Software without restriction, including without limitation the rights ;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ;; copies of the Software, and to permit persons to whom the Software is ;; furnished to do so, subject to the following conditions: ;; ;; The above copyright notice and this permission notice shall be included in ;; all copies or substantial portions of the Software. ;; ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ;; SOFTWARE. ;; (import (chicken fixnum) (chicken io) (only (chicken blob) string->blob) (only (chicken time) current-seconds) (only srfi-1 break!) (only srfi-13 string-null? string-prefix? string-join) srfi-4 tweetnacl lmdb webview) (define rng (open-random-stream (string->blob "Not a very random stream key ;-)") (do ([v (make-u8vector random-stream-noncebytes)] [n (current-seconds) (quotient n 256)] [i 0 (fx+ i 1)]) ((fx>= i (u8vector-length v)) v) (u8vector-set! v i (modulo n 256))))) (open-database-environment "fortune.mdb" #:no-subdirectory) (define db (with-transaction (lambda () (open-database)))) (define-values (add-fortune! add-fortunes!) (letrec ([%add-fortune! (lambda (text) (database-set! db (hash text) text))] [add-fortune! (lambda (text) (with-transaction (lambda () (%add-fortune! text))))] [add-fortunes! (lambda (text) (with-transaction (lambda () (let loop ([text text] [count 0]) (if (pair? text) (begin (%add-fortune! (car text)) (loop (cdr text) (+ count 1))) count)))))]) (values add-fortune! add-fortunes!))) (define (add-fortunes-file! path) (add-fortunes! (let loop ([i (call-with-input-file path read-lines)] [o '()]) (let-values ([(h i) (break! (cut string=? <> "%") i)]) (let ([o (if (pair? h) (cons (string-join h "\n") o) o)]) (if (pair? i) (loop (cdr i) o) o)))))) (define (get-fortune) (with-transaction (lambda () (let retry ([key (read-string hash-bytes rng)]) (unless (and (string? key) (not (string-null? key))) (set! key #f)) (or (call-with-current-continuation (lambda (return) (database-fold (lambda (key val seed) (return val)) #f db #:from key))) (and key (retry (substring key 0 (fx- (string-length key) 1))))))) #:read-only)) (define (render-fortune) (cond [(get-fortune) => (cut list 'pre <>)] [else '(pre ([style "text-color: grey;"]) "(nothing found)")])) (define (inject-fortune view) (webview-eval view "document.getElementById('save').style.setProperty('display','none')") (webview-inject view "fortune" (render-fortune))) (define (inject-editor view) (webview-eval view "document.getElementById('save').style.setProperty('display','inline')") (webview-inject view "fortune" '(textarea ([cols "72"] [rows "16"] [wrap "off"]) ""))) (define (render-app) `(begin (div ([class "toolbar"]) (button ([type "click"] [onclick "external.invoke('random')"]) "Random") (button ([type "click"] [onclick "external.invoke('add-file')"]) "Add File…") (button ([type "click"] [onclick "external.invoke('edit')"]) "New…") (button ([type "click"] [id "save"] [onclick "external.invoke('save:' + document.getElementById('fortune').getElementsByTagName('textarea')[0].value)"] [style "display: none;"]) "Save")) (div ([class "content"]) (code ([id "fortune"]) ,(render-fortune))))) (define (inject-app view) (webview-inject view "app" (render-app))) (webview "Fortune" (lambda (view msg) (cond [(string=? "load" msg) (inject-app view)] [(string=? "random" msg) (inject-fortune view)] [(string=? "add-file" msg) (and-let* ([p (webview-dialog view "Add File" #:open)] [n (add-fortunes-file! p)]) (webview-dialog view "Add File" #:info (string-append (number->string n) " fortunes added")))] [(string=? "edit" msg) (inject-editor view)] [(string-prefix? "save:" msg) (add-fortune! (substring msg 5)) (inject-fortune view)] [else (webview-log (string-append "Unknown Message: " msg))])) #:width 640 #:height 320) (close-database-environment) ;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;; |