<commit_before>(in-package :common-lisp-user)

(defpackage :easyweb
  (:use #:cl #:hunchentoot)
  (:export #:map-urls
	   #:start-server))

(in-package :easyweb)

;;static file dispatcher?
;;regex dispatcher?
;;prefix dispatcher?
;;folder dispatcher?
;;
;;GET and POST parameter pass??????
#|(defmacro map-urls (prefix &body mappings)
  (format t "~%~%URL Mapping")
  (and (every #'listp (mapcar #'identity mappings))
       (not (setf *dispatch-table* nil))
       (mapc #'(lambda(m)
		 (destructuring-bind (pattern . view) m
		   (format t "~%~S..::..~S" pattern view)
		   (push (create-regex-dispatcher (format nil "~A~A" prefix pattern) 
						  view)
			 *dispatch-table*)))
	     mappings)))

(defun mapping (prefix &rest mappings)
  (labels ((destruct-mapping (lst)
	     ;(format t "~A" lst)))
	     (destructuring-bind ((pattern pattern-type) (view params)) lst
	       (case pattern-type
		 (:file
		  (format t "file")
		  )
		 (:folder
		  (format t "folder")
		  )
		 (:prefix
		  (format t "prefix")
		  (push (create-prefix-dispatcher (format nil "~A~A" prefix pattern) 
						 view)
			*dispatch-table*))
		 (:regex
		  (format t "~%~S..::..~S" pattern view)
		  (push (create-regex-dispatcher (format nil "~A~A" prefix pattern) 
						 view)
			 *dispatch-table*)))
	       )))
	   (setf *dispatch-table* nil)
	   (mapc #'destruct-mapping mappings)))|#

(defun get-lambda-list (fn)
  ;;may be platform dependent code
  ;;but i still need it
  ;;return the parameter list of given function
  
  ;;as an aexample the output for url-handler is:
  (list (make-symbol (string-uppercase "LINK"))))
  
(defmacro map-urls (prefix &body body)
    `(progn
       ,@(mapcar #'(lambda (mapping)
                     (let* ((uri-part (car mapping))
                            (type (car uri-part))
                            (uri nil))
                       (case type
                         ((:regex :prefix :file :folder)
                          (let* ((handler (cadr mapping))
                                 (args (get-lambda-list handler))
                                 (uri (format nil "~A~A" prefix (cadr uri-part))))
                            `(define-easy-handler (,(gensym) :uri ,uri)
                                                  ,args
                                                  (,handler ,@(mapcan #'(lambda (arg)
									  `(,(hunchentoot::convert-parameter (string-downcase arg) 'keyword) ,arg))
								      args)))))
                         (:prefix "prefix")
                         (:file "file-")
                         (:folder "folder")
                         (otherwise "other"))))
                 body)))

(defun start-server (&key (port 8000))
  (defparameter *httpd* (start (make-instance 'acceptor :port port))))<commit_msg>get-lambda-list%<commit_after>(in-package :common-lisp-user)

(defpackage :easyweb
  (:use #:cl #:hunchentoot)
  (:export #:map-urls
	   #:start-server))

(in-package :easyweb)

;;static file dispatcher?
;;regex dispatcher?
;;prefix dispatcher?
;;folder dispatcher?
;;
;;GET and POST parameter pass??????
#|(defmacro map-urls (prefix &body mappings)
  (format t "~%~%URL Mapping")
  (and (every #'listp (mapcar #'identity mappings))
       (not (setf *dispatch-table* nil))
       (mapc #'(lambda(m)
		 (destructuring-bind (pattern . view) m
		   (format t "~%~S..::..~S" pattern view)
		   (push (create-regex-dispatcher (format nil "~A~A" prefix pattern) 
						  view)
			 *dispatch-table*)))
	     mappings)))

(defun mapping (prefix &rest mappings)
  (labels ((destruct-mapping (lst)
	     ;(format t "~A" lst)))
	     (destructuring-bind ((pattern pattern-type) (view params)) lst
	       (case pattern-type
		 (:file
		  (format t "file")
		  )
		 (:folder
		  (format t "folder")
		  )
		 (:prefix
		  (format t "prefix")
		  (push (create-prefix-dispatcher (format nil "~A~A" prefix pattern) 
						 view)
			*dispatch-table*))
		 (:regex
		  (format t "~%~S..::..~S" pattern view)
		  (push (create-regex-dispatcher (format nil "~A~A" prefix pattern) 
						 view)
			 *dispatch-table*)))
	       )))
	   (setf *dispatch-table* nil)
	   (mapc #'destruct-mapping mappings)))|#

(defun get-lambda-list (fn)
  ;;may be platform dependent code
  ;;but i still need it
  ;;return the parameter list of given function
(let ((ns (easyweb.html::empty-string))
	       (lambda-list nil))
	   (with-output-to-string (stream ns)
	     (let ((*standard-output* stream))
	       (describe fn)))
	   (with-input-from-string (str ns)
	     (do ((line (read-line str nil 'eof) (read-line str nil 'eof)))
		 ((eq line 'eof) lambda-list)
	       (let ((start (search "Lambda-list: " line :test #'string=)))
		 (when start
		   (setf lambda-list (subseq line (+ start
						     (length "Lambda-list: "))))))))
	   lambda-list))


(defmacro map-urls (prefix &body body)
    `(progn
       ,@(mapcar #'(lambda (mapping)
                     (let* ((uri-part (car mapping))
                            (type (car uri-part))
                            (uri nil))
                       (case type
                         ((:regex :prefix :file :folder)
                          (let* ((handler (cadr mapping))
                                 (args (get-lambda-list handler))
                                 (uri (format nil "~A~A" prefix (cadr uri-part))))
                            `(define-easy-handler (,(gensym) :uri ,uri)
                                                  ,args
                                                  (,handler ,@(mapcan #'(lambda (arg)
									  `(,(hunchentoot::convert-parameter (string-downcase arg) 'keyword) ,arg))
								      args)))))
                         (:prefix "prefix")
                         (:file "file-")
                         (:folder "folder")
                         (otherwise "other"))))
                 body)))

(defun start-server (&key (port 8000))
  (defparameter *httpd* (start (make-instance 'acceptor :port port))))
{'fraction_non_alphanumeric': 0.15503097271053073, 'fraction_numerical': 0.0013393604553825547, 'mean_word_length': 1.8929782082324456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1487928', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1747, 'n_words': 502}