Browse files

refactoring

  • Loading branch information...
1 parent 612a84c commit 0fc2efba7f0e14542fefbf5dadafad783230c4e9 @archimag committed Aug 2, 2010
Showing with 132 additions and 47 deletions.
  1. +2 −1 restas-colorize.asd
  2. +11 −7 src/defmodule.lisp
  3. +90 −0 src/drawer.lisp
  4. +22 −3 src/{colorize.tmpl → drawer.tmpl}
  5. +7 −36 src/{colorize.lisp → routes.lisp}
View
3 restas-colorize.asd
@@ -10,4 +10,5 @@
:components ((:module "src"
:components ((:file "defmodule")
(:file "storage" :depends-on ("defmodule"))
- (:file "colorize" :depends-on ("storage"))))))
+ (:file "drawer" :depends-on ("defmodule"))
+ (:file "routes" :depends-on ("storage" "drawer"))))))
View
18 src/defmodule.lisp
@@ -27,21 +27,25 @@
(in-package #:restas.colorize)
-(defparameter *resource-dir*
- (asdf:component-pathname (asdf:find-system '#:restas-colorize)))
+;;;; load templates
+
+(defparameter *colorize-template-path*
+ (merge-pathnames "src/drawer.tmpl"
+ (asdf:component-pathname (asdf:find-system '#:restas-colorize))))
(closure-template:compile-template :common-lisp-backend
- (merge-pathnames "src/colorize.tmpl"
- (asdf:component-pathname (asdf:find-system '#:restas-colorize))))
+ *colorize-template-path*)
+
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; preferences
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *max-on-page* 10)
(defvar *storage* nil)
-(defvar *colorize-user-function*
+(defparameter *colorize-user-function*
#'(lambda () "anonymous"))
+(defun colorize-user ()
+ (if *colorize-user-function*
+ (funcall *colorize-user-function*)))
View
90 src/drawer.lisp
@@ -0,0 +1,90 @@
+;;;; drawer.lisp
+;;;;
+;;;; This file is part of the restas-colorize library, released under Lisp-LGPL.
+;;;; See file COPYING for details.
+;;;;
+;;;; Author: Moskvitin Andrey <archimag@gmail.com>
+
+(in-package #:restas.colorize)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;; generic interface
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defgeneric finalize-page (drawer data)
+ (:documentation "Finalize page"))
+
+(defgeneric render-route-data (drawer data route )
+ (:documentation "Render page for specific route"))
+
+(defgeneric colorize (drawer code lang)
+ (:documentation "Make highlight html from code")
+ (:method (drawer code lang)
+ (colorize::html-colorization lang code)))
+
+(defgeneric colorize-langs (drawer)
+ (:documentation "List of supported languages")
+ (:method (drawer)
+ (iter (for (id . title) in (colorize:coloring-types))
+ (collect (list :id (symbol-name id)
+ :title title)))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;; default implementation
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defclass base-drawer () ())
+
+(defmethod finalize-page ((drawer base-drawer) data)
+ (restas.colorize.view:finalize-page data))
+
+(defmethod colorize (drawer code (lang string))
+ (colorize drawer code (find-symbol lang :keyword)))
+
+(defmethod colorize (drawer (code string) (lang symbol))
+ (colorize::html-colorization lang code))
+
+(defmethod restas:render-object ((drawer base-drawer) (data list))
+ (let ((content (render-route-data drawer
+ data
+ (restas:route-symbol restas:*route*)))
+ (menu (restas.colorize.view:main-menu
+ (list :href-all (restas:genurl 'list-pastes)
+ :href-create (restas:genurl 'create-paste)))))
+ (finalize-page drawer
+ (list :content content
+ :menu menu
+ :title (getf data :title)))))
+
+
+(defmethod render-route-data ((drawer base-drawer) (data list) route)
+ (funcall (find-symbol (symbol-name route)
+ '#:restas.colorize.view)
+ data))
+
+(defmethod render-route-data ((drawer base-drawer) (data list) (route (eql 'view-paste)))
+ (call-next-method drawer
+ (list* :code (colorize drawer
+ (getf data :code)
+ (getf data :lang))
+ data)
+ route))
+
+(defmethod render-route-data ((drawer base-drawer) (data list) (route (eql 'create-paste)))
+ (call-next-method drawer
+ (list* :langs (colorize-langs drawer)
+ data)
+ route))
+
+(defmethod render-route-data ((drawer base-drawer) (data list) (route (eql 'preview-paste)))
+ (call-next-method drawer
+ (list* :langs (colorize-langs drawer)
+ :preview (colorize drawer
+ (getf data :code)
+ (getf data :lang))
+ data)
+ route))
+
+
+
+(setf *default-render-method* (make-instance 'base-drawer))
View
25 src/colorize.tmpl → src/drawer.tmpl
@@ -8,11 +8,32 @@
{namespace restas.colorize.view}
+{template finalizePage}
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+ {\n}
+ <html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset='utf-8'" />
+
+ {if $title}
+ <title>{$title}</title>
+ {/if}
+ </head>
+
+ <body>
+ {$menu |noAutoescape}
+
+ {$content |noAutoescape}
+ </body>
+ </html>
+{/template}
+
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Show main menu
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-{template withMainMenu}
+{template main-menu}
<ul class="colorize-top-menu">
<li>
<a href="{$hrefAll}">Все записи</a>
@@ -22,8 +43,6 @@
<a href="{$hrefCreate}">Создать</a>
</li>
</ul>
-
- {$body |noAutoescape}
{/template}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
View
43 src/colorize.lisp → src/routes.lisp
@@ -7,21 +7,6 @@
(in-package #:restas.colorize)
-(restas:define-default-render-method (obj)
- (closure-template.standard:xhtml-strict-frame
- (list :title (getf obj :title)
- :body (restas.colorize.view:with-main-menu
- (list :href-all (restas:genurl 'list-pastes)
- :href-create (restas:genurl 'create-paste)
- :body (restas:render-object (find-package '#:restas.colorize.view)
- obj)))
- :css (loop for item in '("style.css" "colorize.css")
- collect (restas:genurl 'css :file item)))))
-
-(defun colorize-user ()
- (if *colorize-user-function*
- (funcall *colorize-user-function*)))
-
(defun paste-plist/short (paste)
(list :href (restas:genurl 'view-paste :id (paste-id paste))
:date (local-time:format-timestring nil (paste-date paste))
@@ -40,10 +25,6 @@
(restas:define-route main ("")
(restas:redirect 'list-pastes))
-(restas:define-route css ("css/:file")
- (merge-pathnames (format nil "resources/css/~A" file)
- *resource-dir*))
-
(restas:define-route list-pastes ("all")
(let* ((total-count (storage-count-pastes *storage*))
(start (min (max (or (ignore-errors (parse-integer (hunchentoot:get-parameter "start")))
@@ -71,32 +52,22 @@
(restas:define-route view-paste (":id"
:parse-vars (list :id #'parse-integer))
- (let* ((paste (storage-get-paste *storage* id))
- (lang (paste-lang paste)))
+ (let* ((paste (storage-get-paste *storage* id)))
(list* :title (paste-title paste)
- :code (colorize::html-colorization (or (find-symbol lang :keyword)
- (error "Unknow coloring type: ~A" lang))
- (paste-code paste))
+ :code (paste-code paste)
:lang (paste-lang paste)
(paste-plist/short paste))))
(restas:define-route create-paste ("create")
- (list :langs (langs-plist)
- :title "Создать"))
+ (list :title "Создать"))
(restas:define-route preview-paste ("create"
:method :post
:requirement #'(lambda () (hunchentoot:post-parameter "preview")))
- (let ((code (hunchentoot:post-parameter "code"))
- (lang (hunchentoot:post-parameter "lang")))
- (list :langs (langs-plist)
- :preview (colorize::html-colorization (or (find-symbol lang :keyword)
- (error "Unknow coloring type: ~A" lang))
- code)
- :title (hunchentoot:post-parameter "title")
+ (list :title (hunchentoot:post-parameter "title")
:author (colorize-user)
- :code code
- :lang lang)))
+ :code (hunchentoot:post-parameter "code")
+ :lang (hunchentoot:post-parameter "lang")))
(restas:define-route save-paste ("create"
@@ -106,7 +77,7 @@
(if author
(restas:redirect 'view-paste
:id (paste-id (storage-add-paste *storage*
- (make-instance 'paste
+ (make-instance 'paste
:code (hunchentoot:post-parameter "code")
:author author
:lang (hunchentoot:post-parameter "lang")

0 comments on commit 0fc2efb

Please sign in to comment.