Skip to content

Instantly share code, notes, and snippets.

@nicferrier
Last active December 14, 2015 17:59
Show Gist options
  • Save nicferrier/5125633 to your computer and use it in GitHub Desktop.
Save nicferrier/5125633 to your computer and use it in GitHub Desktop.
map a url to variable names, suggested by #emacs jaimef. like what sinatra does
(defun elnode/bind-path (pattern httpcon)
(let* ((path (elnode-http-pathinfo httpcon))
(lst (split-string pattern "/" t))
(patlst
(let ((i 0))
(loop for part in lst
if (string-match-p "^:.*" part)
collect (cons i (intern (substring part 1)))
do (setq i (+ 1 i)))))
(splt (split-string path "/")))
(let ((i 0))
(loop for part in splt
if (aget patlst i)
collect (cons (aget patlst i) (list part))
do (setq i (+ i 1))))))
(defmacro elnode-bind-path (pattern httpcon &rest body)
(declare (indent 2))
`(let (,@(elnode/bind-path pattern httpcon))
,@body))
;; Demo of the underlying func
(flet ((elnode-http-pathinfo (httpcon) "/one/a/b"))
(elnode/bind-path "/one/:one/:two" :httpcon))
;; Now the macro
(flet ((elnode-http-pathinfo (httpcon) "/one/a/b"))
(pp-macroexpand-expression
'(elnode-bind-path "/one/:one/:two" :httpcon
(+ 1 2))))
@spacebat
Copy link

spacebat commented Mar 9, 2013

Works if the omit-nulls parameter is dropped from the first split-string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment