Created
January 6, 2012 12:27
-
-
Save osa1/1570375 to your computer and use it in GitHub Desktop.
web interface generator for zebot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(in-package :cl-user) | |
(ql:quickload '("hunchentoot" "cl-who")) | |
(defpackage zebot-web | |
(:use :cl :hunchentoot :cl-who)) | |
(in-package zebot-web) | |
;; TODO: find a way to use symbols from other packages | |
;; (it should be related with ASDF) | |
(defmacro concat (&rest forms) | |
`(concatenate 'string ,@forms)) | |
(defvar *log-folder* "/home/sinan/Desktop/cl/logs/") | |
(defmacro standard-page ((&key title) &body body) | |
`(with-html-output-to-string (*standard-output* nil :prologue t :indent t) | |
(:html | |
(:meta :charset "utf-8") | |
(:head | |
(:title ,title) | |
(:link :type "text/css" | |
:rel "stylesheet" | |
:href "/static/reset.css") | |
(:link :type "text/css" | |
:rel "stylesheet" | |
:href "/static/main.css")) | |
(:body | |
(:div :class "main" | |
,@body))))) | |
(defmacro define-url-fn ((name) &body body) | |
`(progn | |
(defun ,name () | |
,@body) | |
(push (create-prefix-dispatcher ,(format nil "/~(~a~).html" name) ',name) *dispatch-table*))) | |
(defun list-file-names (&optional (folder *log-folder*)) | |
(mapcar (lambda (pathname) | |
(let ((filename (file-namestring pathname))) | |
(pathname-name filename))) | |
(directory (make-pathname :directory folder :name :wild :type "html")))) | |
;; create main menu | |
(define-url-fn (log-list) | |
(standard-page (:title "log list") | |
(:div :class "header" "Channel List:") | |
(loop for log in (list-file-names) | |
collect (htm (:div :class "menulink" | |
(:a :href (concat log ".html") (str log))))))) | |
;; create channel logs | |
(dolist (page-name (list-file-names)) | |
(let* ((in (open (merge-pathnames *log-folder* | |
(make-pathname :name page-name | |
:type "html")))) | |
(text (car (loop for line = (read-line in nil) | |
while line collect line)))) | |
;; TODO: define-url-fn macro is just making generating pages with | |
;; contents created by other functions harder | |
(push | |
(create-prefix-dispatcher | |
(concat "/" page-name ".html") | |
(lambda () | |
(standard-page (:title "Channel logs") | |
(:div :class "header" (str (concat "Chat logs for #" page-name))) | |
(str text)))) | |
*dispatch-table*))) | |
(defvar server (make-instance 'easy-acceptor :port 4242 | |
:document-root "/home/sinan/Desktop/cl/static")) | |
(start server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment