This is the mail archive of the
guile@sources.redhat.com
mailing list for the Guile project.
Re: Challenge
On Thu, Aug 03, 2000 at 12:24:52AM -0500, Chris Cramer wrote:
> On Tue, Aug 01, 2000 at 09:50:51PM -0700, Peter C. Norton wrote:
> > Who wants to convert a script from perl to guile? I've got a script I
> > whipped up to turn a template and some content into the guile web pages. I
> > think it's kinda silly that I've done it in perl, but I can't do it in
> > guile. Can you? Can you do it better?
>
> Better? I dunno, I can't even read the perl script. I worked entirely from the
> comments... beware, this is untested.
Ok, I happened to have some more time and I cleaned out all the obvious
bugs. It actually works now, although I'm not entirely sure it does
what you want. I would attach a diff but the diff was actually 5 lines
bigger than the new code.
--
C. Ray C. aka Christopher Cramer
crayc@pyro.net
http://www.pyro.net/~crayc/
; the perl translation challenge
(define stuff
(map
(lambda (x)
(list
(make-regexp (string-append "<" x ">(.*)</" x ">")) ;extraction
(make-regexp (string-append "<" x "></" x ">")))) ;insertion
'("section_name" "local_nav" "main_content")))
(define (dump-port port)
(let loop ((c (read-char port)) (l '()))
(if (eof-object? c)
(apply string (reverse l))
(loop (read-char port) (cons c l)))))
(define template (dump-port (open-input-file (cadr command-line))))
(define content (dump-port (open-input-file (caddr command-line))))
(define target (open-output-file (cadddr command-line)))
(define (extract rx s)
(let ((m (regexp-exec rx s)))
(if (not m)
#f
(match:substring m 1))))
(define (insert rx in out)
(let ((m (regexp-exec rx in)))
(if (not m)
in
(regexp-substitute #f m 'pre out 'post))))
(define output
(let loop ((stuff stuff) (out template))
(if (null? stuff)
out
(let ((x (extract (caar stuff) content)))
(if (not x)
(loop (cdr stuff) out)
(loop (cdr stuff) (insert (cadar stuff) out x)))))))
(display output target)
(close target) ;don't get any actual output until you close it
;damn buffering =^P