1
0
Fork 0
tekuti/tekuti/util.scm

284 lines
8.9 KiB
Scheme
Raw Normal View History

2008-02-11 22:58:10 +01:00
;; Tekuti
;; Copyright (C) 2008 Andy Wingo <wingo at pobox dot com>
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652
;; Boston, MA 02111-1307, USA gnu@gnu.org
;;; Commentary:
;;
2008-02-29 00:32:49 +01:00
;; Utility procedures and macros.
2008-02-11 22:58:10 +01:00
;;
;;; Code:
(define-module (tekuti util)
#:use-module (match-bind)
2008-02-29 00:32:49 +01:00
#:use-module (scheme kwargs)
2008-02-11 22:58:10 +01:00
#:use-module (srfi srfi-1)
2008-02-29 00:32:49 +01:00
#:use-module (srfi srfi-19)
2008-02-17 01:17:01 +01:00
#:export (expanduser match-lines dbg unwind-protect dbg dsu-sort
2008-02-29 00:32:49 +01:00
hash-push! list-has-length? list-head-match mapn filter-mapn
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
take-max read-hash write-hash shell:quote foldn
2008-02-29 00:32:49 +01:00
call-with-temp-file emailish? urlish?
date-increment date-comparator date-before? date-after? compose1
rfc822-date->timestamp timestamp->rfc822-date timestamp->atom-date
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
timestamp->date string-split/trimming
2008-02-28 14:28:38 +01:00
list-intersperse with-backtrace with-time-debugging define-memoized))
2008-02-11 22:58:10 +01:00
2008-02-29 00:32:49 +01:00
(define (emailish? x)
(match-bind "^([a-zA-Z0-9.+-]+)@([a-zA-Z0-9-]+\\.)+[a-zA-Z]+$"
x (_ . args)
x
#f))
(define (urlish? x)
(match-bind "^https?://([a-zA-Z0-9-]+\\.)+[a-zA-Z]+/[a-zA-Z0-9$_.+!*'(),;/?:@&=-]*$"
x (_ . args)
x
#f))
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
;; bad name relative to mapn...
(define (foldn kons n knil values)
(if (null? values)
knil
(foldn kons n
(apply kons knil (list-head values n))
(list-tail values n))))
2008-02-29 00:32:49 +01:00
(define (call-with-temp-file contents proc)
(let* ((template (string-copy "/tmp/tekutiXXXXXX"))
(tmp (mkstemp! template)))
(display contents tmp)
(close tmp)
(unwind-protect
(proc template)
(delete-file template))))
(define (shell:quote str)
(with-output-to-string
(lambda ()
(display #\')
(string-for-each (lambda (ch)
(if (eqv? ch #\')
(begin (display #\\) (display #\'))
(display ch)))
str)
(display #\'))))
2008-02-11 22:58:10 +01:00
(define (expanduser path)
(let ((parts (string-split path #\/)))
(if (eqv? (string-ref (car parts) 0) #\~)
(let ((user (if (= (string-length (car parts)) 1)
(cuserid)
(substring (car parts) 1))))
(string-join (cons (passwd:dir (getpwnam user)) (cdr parts)) "/"))
path)))
(define-macro (match-lines string pattern bindings expr)
(let ((line (gensym)) (seed (gensym)))
`(,fold
(lambda (,line ,seed)
(match-bind ,pattern ,line ,bindings
(cons ,expr ,seed)
,seed))
'() (string-split ,string #\newline))))
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
;; clause := ((pat args) body...)
(define-macro (match-case string . clauses)
(let ((str (gensym)))
`(let ((,str ,string))
,(let lp ((in clauses))
(let ((clause (car in)))
(if (eq? (car clause) 'else)
`(begin ,@(cdr clause))
`(match-bind ,(caar clause) ,str ,(cadar clause)
(begin ,@(cdr clause))
,(if (null? (cdr in))
#f
(lp (cdr in))))))))))
2008-02-11 22:58:10 +01:00
(define (dbg fmt . args)
(apply format (current-error-port) fmt args))
(define-macro (unwind-protect form . cleanups)
`(dynamic-wind (lambda () #t)
(lambda () ,form)
(lambda () ,@cleanups)))
(define (dsu-sort list key less)
(map cdr
(stable-sort (map (lambda (x) (cons (key x) x)) list)
(lambda (x y) (less (car x) (car y))))))
(define (hash-push! h key value)
(let ((handle (hash-create-handle! h key '())))
(set-cdr! handle (cons value (cdr handle)))))
2008-02-17 01:17:01 +01:00
2008-02-22 19:28:46 +01:00
(define (take-max list n)
(if (or (null? list) (zero? n))
'()
(cons (car list) (take-max (cdr list) (1- n)))))
2008-02-17 01:17:01 +01:00
(define (list-has-length? list len)
(cond
((zero? len) (null? list))
((null? list) #f)
(else (list-has-length? (cdr list) (1- len)))))
;; returns tail of l2
(define (list-head-match l1 l2 n)
(cond
((zero? n) l2)
((null? l2) #f)
((not (equal? (car l1) (car l2))) #f)
(else (list-head-match (cdr l1) (cdr l2) (1- n)))))
2008-02-20 09:24:18 +01:00
(define (mapn proc l nmax)
(let lp ((in l) (out '()) (n nmax))
(if (or (null? in) (zero? n))
(reverse out)
(lp (cdr in) (cons (proc (car in)) out) (1- n)))))
2008-02-29 00:32:49 +01:00
(define (filter-mapn proc l nmax)
(let lp ((in l) (out '()) (n nmax))
(if (or (null? in) (zero? n))
(reverse out)
(let ((val (proc (car in))))
(if val
(lp (cdr in) (cons val out) (1- n))
(lp (cdr in) out n))
))))
2008-02-20 09:24:18 +01:00
(define (list-intersperse src-l elem)
(if (null? src-l) src-l
(let loop ((l (cdr src-l)) (dest (cons (car src-l) '())))
(if (null? l) (reverse dest)
(loop (cdr l) (cons (car l) (cons elem dest)))))))
2008-02-21 23:48:44 +01:00
(define (handle-error key . args)
(let ((cep (current-error-port))
(highlights (if (or (eq? key 'wrong-type-arg)
(eq? key 'out-of-range))
(list-ref args 3)
'())))
(newline cep)
(display "Backtrace:\n")
(display-backtrace (fluid-ref the-last-stack) cep
#f #f highlights)
(newline cep)
(if (= (length args) 4)
(apply display-error (fluid-ref the-last-stack) cep args)
(format cep "~a" args))
(force-output cep)
(apply throw key args)))
2008-02-28 14:28:38 +01:00
(define (with-backtrace* proc)
2008-02-21 23:48:44 +01:00
(debug-enable 'backtrace)
(start-stack 'with-backtrace
(catch #t
proc
handle-error
(lambda args
(fluid-set! the-last-stack (make-stack #t 2 0))
(apply throw args)))))
2008-02-28 14:28:38 +01:00
(define-macro (with-backtrace . forms)
`(,with-backtrace* (lambda () ,@forms)))
(define (gettimeofday-diff prev)
(let ((now (gettimeofday)))
(+ (- (car now) (car prev))
(* 1e-6 (- (cdr now) (cdr prev))))))
(define (with-time-debugging* proc)
(pk 'start-clock)
(let ((start (gettimeofday)))
(unwind-protect
(proc)
(pk 'stop-clock (gettimeofday-diff start)))))
(define-macro (with-time-debugging . forms)
`(,with-time-debugging* (lambda () ,@forms)))
2008-02-21 23:48:44 +01:00
(define (memoize1 proc)
(let ((old-args #f) (cache #f) (proc proc))
(lambda args
(if (equal? args old-args)
cache
(let ((val (apply proc args)))
(set! old-args args)
(set! cache val)
val)))))
(define-macro (define-memoized form . body)
`(begin
(define ,form ,@body)
(set! ,(car form) (,memoize1 ,(car form)))))
2008-02-28 14:28:38 +01:00
(define (write-hash h)
(write (hash-fold acons '() h)))
(define (read-hash)
(let ((h (make-hash-table)))
(for-each (lambda (pair)
(hash-set! h (car pair) (cdr pair)))
(read))
h))
2008-02-29 00:32:49 +01:00
(define/kwargs (date-increment date (day 0) (month 0) (year 0))
(make-date (date-nanosecond date) (date-second date)
(date-minute date) (date-minute date)
(+ (date-day date) day) (+ (date-month date) month)
(+ (date-year date) year) (date-zone-offset date)))
(define (date-comparator date comp)
(let ((this (time-second (date->time-utc date))))
(lambda (that)
(comp that this))))
(define (date-before? date)
(date-comparator date <))
(define (date-after? date)
(date-comparator date >))
(define (compose1 proc . procs)
(if (null? procs)
proc
(let ((other (apply compose1 procs)))
(lambda (x)
(proc (other x))))))
comment deletion, post editing. * tekuti/comment.scm (make-new-comment): Refactor to decouple comment.scm from post.scm. * tekuti/git.scm (run): Generalize so it can run any executable. (git-ls-tree, git-ls-subdirs): Check for a #f treeish, return directly in that case, avoiding tracebacks in the forked kid. (git-mktree): Return #f if the tree is empty. (git-rev-list): New command, something like git log. (patch-blob): New function; unused, though. Perhaps I should just delete it. (git-commit-reverse-operations): New function, constructs munge-tree operations to revert a commit. (git-revert): New operator, reverts a commit. Can't use git's revert because it requires a working tree. (munge-tree): Rewrite to unify the various commands, and process the commands in order. Makes a bit more garbage, but is much more understandable. * tekuti/page-helpers.scm (post-editing-form): Add a preview. (admin-post-url): New helper. (redirect): New helper. (admin-post-redirect): New helper. * tekuti/page.scm (page-admin): Show recent changes in the sidebar. (page-admin-new-post): Redirect to the new post. (page-new-comment): Rework for make-new-comment changes, and redirect to the post. Would be nice to redirect to the comment though. (page-admin-changes): New page, lists recent changes. (page-admin-change): New page, shows a change, allowing it to be undone. (page-admin-revert-change): New page, actually reverts a change. Thus we have comment deletion. * tekuti/post.scm (munge-post): New helper, factored out of make-new-post. (parse-post-data): Another new helper. Needs to do more validation, though. (modify-post): Post editing, yay! * tekuti/request.scm (parse-www-form-urlencoded): Factored out of request-form-data. (*request-initializers*): Initialize 'query to have the parsed query string data. * tekuti/url.scm (url:path-part, url:query-part): New somewhat hacky functions. url.scm needs some love. (url:path-split): Split on only the "path part" of the URL. * tekuti/util.scm (foldn): New export. Probably a bad idea. (match-case): New macro, not used though. (string-split/trimming): New util. * tekuti/web.scm (choose-handler): Update the set of pages.
2008-03-07 13:09:47 +01:00
(define (string-split/trimming string delimiter)
(map string-trim-both (string-split string delimiter)))
2008-02-29 00:32:49 +01:00
(define (rfc822-date->timestamp str)
(+ (time-second (date->time-utc
(string->date str "~a, ~d ~b ~Y ~H:~M:~S GMT")))
(date-zone-offset (current-date))))
(define (timestamp->date timestamp)
(time-utc->date (make-time time-utc 0 timestamp) 0))
2008-02-29 00:32:49 +01:00
(define (timestamp->atom-date timestamp)
(date->string (timestamp->date timestamp)
2008-02-29 00:32:49 +01:00
"~Y-~m-~dT~H:~M:~SZ"))
(define (timestamp->rfc822-date timestamp)
(date->string (timestamp->date timestamp)
2008-02-29 00:32:49 +01:00
"~a, ~d ~b ~Y ~H:~M:~S GMT"))