This is the mail archive of the guile@sources.redhat.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Challenge


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.

-- 
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)
            (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-input-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 s)
    (let ((m (regexp-exec rx template)))
        (if (not m)
            s
            (regexp-substitute #f m 'pre s 'post))))

(define output
    (let loop ((stuff stuff) (output content))
        (if (null? stuff)
            output
            (let ((x (extract (caar stuff) content)))
                (if (not x)
                    (loop (cdr stuff) output)
                    (loop (cdr stuff) (insert (cadar stuff)))))))

(display output target)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]