unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Artanis web REPL
@ 2016-06-24 11:43 Jan Wedekind
  2016-06-24 17:20 ` Nala Ginrut
  0 siblings, 1 reply; 6+ messages in thread
From: Jan Wedekind @ 2016-06-24 11:43 UTC (permalink / raw)
  To: guile-user

Just managed to get a simple web REPL working. It still needs some form of 
safe environments for each browser sessions. Maybe one can do something 
like the IPython notebooks in the future.

Any comments/suggestions welcome.

     ; prototype Guile repl for the browser
     (use-modules (srfi srfi-26) (artanis artanis) (artanis cookie) (artanis utils) (ice-9 regex))

     (init-server)

     (define (dot lst) (apply cons lst))

     (define (quoted expr) (call-with-output-string (cut write expr <>)))

     (define (line-breaks s) (regexp-substitute/global #f "\n" s 'pre "<br/>" 'post))

     (define lines '())

     (define (reset session)
       (set! lines (assoc-set! lines session '()))
       (output session (format #f "; session: ~a~&" session)))

     (define (output session line)
       (assoc-set! lines session (cons line (assoc-ref lines session))))

     (define (repl session line)
       (catch #t
         (lambda ()
           (output session line)
           (let [(result (eval-string line (current-module)))]
             (if (not (unspecified? result))
               (output session (format #f "; ~a~&" (quoted result))))))
         (lambda (key function fmt vals . args)
           (let* [(msg  (apply format #f fmt vals))
                  (info (format #f "; ~a" msg))]
             (output session info)))))

     (define (editor session)
       (tpl->response
         `(html
            (body
              ,(map (lambda (line) `(p ,(line-breaks (eliminate-evil-HTML-entities line)))) (reverse (assoc-ref lines session)))
              (form (@ (action "") (method "post"))
                (input (@ (type "text") (name "line") (autofocus "autofocus"))))))))

     (get "/" #:session 'spawn
       (lambda (rc)
         (let [(session (:session rc 'spawn))]
           (reset session)
           (editor session))))

     (post "/"
       (lambda (rc)
         (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
                (post-data (map dot (generate-kv-from-post-qstr (rc-body rc))))
                (line      (uri-decode (assoc-ref post-data "line")))]
           (repl session line)
           (editor session))))
     (run)



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-25 20:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-24 11:43 Artanis web REPL Jan Wedekind
2016-06-24 17:20 ` Nala Ginrut
2016-06-24 21:40   ` Jan Wedekind
2016-06-24 21:51   ` Jan Wedekind
2016-06-25  1:14     ` Nala Ginrut
2016-06-25 20:09       ` Jan Wedekind

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).