guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Artanis web REPL


From: Jan Wedekind
Subject: Artanis web REPL
Date: Fri, 24 Jun 2016 12:43:29 +0100 (BST)
User-agent: Alpine 2.11 (DEB 23 2013-08-11)

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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]