Index: stml2.scm
==================================================================
--- stml2.scm
+++ stml2.scm
@@ -22,50 +22,78 @@
(use regex)
;; The (usually global) sdat contains everything about the session
;;
(defstruct sdat
+ ;; database
(dbtype 'pg)
(dbinit #f)
(conn #f)
+ ;; page info
(page "home")
(page-type 'html)
(toppage "index")
+ (curr-page "home")
(content-type "Content-type: text/html; charset=iso-8859-1\n\n")
+ ;; forms and variables
(formdat #f)
(params '())
(path-params '())
(session-key #f)
(pagedat '())
- (curr-page "home")
(alt-page-dat #f)
- (sroot "./")
(session-cookie #f)
+ (pagevars (make-hash-table))
+ (pagevars-before (make-hash-table))
+ (sessionvars (make-hash-table))
+ (sessionvars-before (make-hash-table))
+ (globalvars (make-hash-table))
+ (globalvars-before (make-hash-table))
+ ;; ports and log file
(curr-err #f)
(log-port (current-error-port))
(logfile "/tmp/stml.log")
(seen-pages '())
(page-dir-style #t)
(debug-mode #f)
(session-id #f)
- (pagevars (make-hash-table))
- (pagevars-before (make-hash-table))
- (sessionvars (make-hash-table))
- (sessionvars-before (make-hash-table))
- (globalvars (make-hash-table))
- (globalvars-before (make-hash-table))
(request-method #f)
(domain "localhost")
(twikidir #f)
(script #f)
(force-ssl #f)
- (shared-hash (make-hash-table)))
+ (shared-hash (make-hash-table))
+ ;; paths
+ (sroot "./")
+ (models #f)
+ (views #f)
+)
-(define (apply-config-file session #!optional (configf #f))
+(define (sdat-set-if session configdat var settor)
+ (let ((val (s:find-param var configdat)))
+ (if val (settor session val))))
+
+(define (session:initialize session #!optional (configf #f))
+ ;; (let* ((rawconfigdat (session:read-config session configf))
+ ;; (configdat (if rawconfigdat (eval rawconfigdat) '())))
+ ;; (sdat-set-if session configdat 'sroot sdat-root-set!)
+ ;; (sdat-set-if session configdat 'logfile sdat-logfile-set!)
+ ;; (sdat-set-if session configdat 'dbtype sdat-dbtype-set!)
+ ;; (sdat-set-if session configdat 'dbinit sdat-dbinit-set!)
+ ;; (sdat-set-if session configdat 'domain sdat-domain-set!)
+ ;; (sdat-set-if session configdat 'twikidir sdat-twikidir-set!)
+ ;; (sdat-set-if session configdat 'page-dir-style sdat-page-set!)
+ ;; (sdat-set-if session configdat 'sroot sdat-root-set!)
+ ;; (sdat-set-if session configdat 'sroot sdat-root-set!)
+ ;; (sdat-set-if session configdat 'sroot sdat-root-set!)
+ ;; following are set always from config
+ ;; (sdat-page-dir-style-set! session (s:find-param 'page-dir-style configdat))
(let* ((rawconfigdat (session:read-config session configf))
(configdat (if rawconfigdat (eval rawconfigdat) '()))
(sroot (s:find-param 'sroot configdat))
+ (models (s:find-param 'models configdat))
+ (views (s:find-param 'views configdat))
(logfile (s:find-param 'logfile configdat))
(dbtype (s:find-param 'dbtype configdat))
(dbinit (s:find-param 'dbinit configdat))
(domain (s:find-param 'domain configdat))
(twikidir (s:find-param 'twikidir configdat))
@@ -72,10 +100,12 @@
(page-dir (s:find-param 'page-dir-style configdat))
(debugmode (s:find-param 'debugmode configdat))
(script (s:find-param 'script configdat))
(force-ssl (s:find-param 'force-ssl configdat)))
(if sroot (sdat-sroot-set! session sroot))
+ (if models (sdat-models-set! session models))
+ (if views (sdat-views-set! session views))
(if logfile (sdat-logfile-set! session logfile))
(if dbtype (sdat-dbtype-set! session dbtype))
(if dbinit (sdat-dbinit-set! session dbinit))
(if domain (sdat-domain-set! session domain))
(if twikidir (sdat-twikidir-set! session twikidir))
@@ -461,15 +491,15 @@
;;
(define (s:get-inp key . params)
(or (apply s:get-input key params)
(apply s:get-param key params)))
-#;(define (s:load-model model)
+(define (s:load-model model)
(session:load-model s:session model))
-#;(define (s:model-path model)
- (session:model-path s:session model))
+(define (s:model-path)
+ (session:model-path s:session))
;; share data between pages calls. NOTE: This is not persistent
;; between cgi calls. Use sessionvars for that.
;;
(define (s:shared-hash)
@@ -1658,11 +1688,12 @@
;;
;; (let ((dbtype (sdat-dbtype self)))
;; (print "dbtype: " dbtype)
;; (sdat-dbtype-set! self (eval dbtype))))
-(define (session:setup self)
+(define (session:setup self #!optional (configf #f))
+ (session:initialize self configf)
(let ((dbtype (sdat-dbtype self))
(debugmode (sdat-debug-mode self))
(dbinit (eval (sdat-dbinit self)))
(dbexists #f))
(let ((dbfname (alist-ref 'dbname dbinit)))
@@ -2149,21 +2180,23 @@
(list "ERROR: page-dir-style must be stored, dir or flat, got " dir-style)))))
(define (session:call self page parts)
(session:call-parts self page 'both))
-;; (define (session:load-model self model)
-;; (let ((model.scm (string-append (sdat-sroot self) "/models/" model ".scm"))
-;; (model.so (string-append (sdat-sroot self) "/models/" model ".so")))
-;; (if (file-exists? model.so)
-;; (load model.so)
-;; (if (file-exists? model.scm)
-;; (load model.scm)
-;; (s:log "ERROR: model " model.scm " not found")))))
-
-;; (define (session:model-path self model)
-;; (string-append (sdat-sroot self) "/models/" model ".scm"))
+(define (session:load-model self model)
+ (let* ((mpath (session:model-path self))
+ (model.scm (string-append mpath "/" model ".scm"))
+ (model.so (string-append mpath "/" model ".so")))
+ (if (file-exists? model.so)
+ (load model.so)
+ (if (file-exists? model.scm)
+ (load model.scm)
+ (s:log "ERROR: model " model.scm " not found")))))
+
+(define (session:model-path self)
+ (or (sdat-models self)
+ (string-append (sdat-sroot self) "/models/")))
(define (session:pp-formdat self)
(let ((dat (formdat:all->strings (sdat-formdat self))))
(string-intersperse dat "
")))
@@ -2531,7 +2564,111 @@
(define s:session-var-delete! s:session-var-del!)
;; utility to get all vars as hash table
(define (s:session-get-sessionvars)
(sdat-sessionvars s:session))
+
+;;======================================================================
+;; Sugar
+;;======================================================================
+;;
+;; (require 'syntax-case)
+;;
+;; (define-syntax s:if-param
+;; (syntax-rules ()
+;; [(_ s x) (if (s:get s) x (s:comment "s:if not"))]
+;; [(_ s x y) (if (s:get s) x y)]))
+;; ;;
+;; (define-syntax s:if-test
+;; (syntax-rules ()
+;; [(_ s x) (if (string=? "yep" s) x (list "s:if not"))]
+;; [(_ s x y) (if (string=? "yep" s) x y)]))
+
+;; Some of these routines use:
+;;
+;; http://www.cs.toronto.edu/~gfb/scheme/simple-macros.html
+;;
+;; Syntax for defining macros in a simple style similar to function definiton,
+;; when there is a single pattern for the argument list and there are no keywords.
+;;
+;; (define-simple-syntax (name arg ...) body ...)
+;;
+
+(define-syntax define-simple-syntax
+ (syntax-rules ()
+ ((_ (name arg ...) body ...)
+ (define-syntax name (syntax-rules () ((name arg ...) (begin body ...)))))))
+
+;;======================================================================
+;; syntatic sugar items
+;;======================================================================
+
+;; We often seem to want to include stuff if a conditional is met
+;; otherwise not include it. This routine makes that slightly cleaner
+;; since using a pure if results in # objects. (admittedly they
+;; should be ignored but this is slightly cleaner I think).
+;;
+;; NOTE: This has to be a macro or the true clause will be evaluated
+;; whether "a" is true or false
+
+;; If a is true return b, else return '()
+(define-simple-syntax (s:if a b)
+ (if a b '()))
+
+
+;; Using the Simple-Syntax System
+;;
+;; The syntax for defining macros in this system is similar to that for defining functions. In fact if the macro has a fixed number of arguments the syntax is identical. For example:
+;;
+;; ; Define a simple macro to add a value to a variable.
+;; ;
+;; (define-simple-syntax (+= variable value)
+;; (set! variable (+ variable value)))
+;;
+;; ; Use it.
+;; ;
+;; (define v 2)
+;; (+= v 7)
+;; v ; => 9
+;;
+;; For a fixed number of arguments followed by an unknown number of arguments we use ... after a single argument to represent the unknown number (possibly zero) of arguments. For example, let's revise our definition of += to allow zero or more values to be added:
+;;
+;; ; Define a simple macro to add a zero or more values to a variable
+;; ;
+;; (define-simple-syntax (+= variable value ...)
+;; (set! variable (+ variable value ...)))
+;;
+;; ; Use it
+;; ;
+;; (define v 2)
+;; (+= v 7)
+;; v ; => 9
+;; (+= v 3 4)
+;; v ; => 16
+;; (+= v)
+;; v ; => 16
+;;
+
+(define-simple-syntax (s:if-param varname first ...)
+ (if (s:get varname)
+ first
+ ...))
+
+(define-simple-syntax (s:if-sessionvar varname first ...)
+ (if (s:session-var-get varname)
+ first
+ ...))
+
+;; (define-macro (s:if-param varname ...)
+;; (match dat
+;; (() '())
+;; ((a) `(if (s:get ,varname) ,a '()))
+;; ((a b) `(if (s:get ,varname) ,a ,b))))
+;;
+;; (define-macro (s:if-sessionvar varname . dat)
+;; (match dat
+;; (() '())
+;; ((a) `(if (s:session-var-get ,varname) ,a '()))
+;; ((a b) `(if (s:session-var-get ,varname) ,a ,b))))
+;;
)