guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 1.4 build problems (sorry!)


From: Jorgen Schaefer
Subject: Re: 1.4 build problems (sorry!)
Date: 23 Feb 2002 02:47:53 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1

Thien-Thi Nguyen <address@hidden> writes:

>    From: "Peter C. Norton" <address@hidden>
>    Date: Fri, 22 Feb 2002 04:22:03 -0800
> 
>    I recall asking for help when I did that bit of perl, but nothing
>    really came of it.  Anyone want to help my practical scheme skills?
> 
> i've appended the perl script below so we can collaboratively rework the
> beast.

I don't want to destroy the collaboration, but i've implemented
similar scripts in bash+sed and Python lately, so i thought i'd
just continue with Scheme now :)

Works well here. Sorry for not commenting the thing, but I think
it's pretty self-explanatory.

Greetings,
        -- Jorgen

-- 
((email . "address@hidden")          (www . "http://forcix.cx/";)
 (irc   . "address@hidden (IRCnet)") (gpg .    "1024D/028AF63C"))

#!/usr/bin/guile \
-e main -s
A <section_name>...</section_name> pair that will appear in the title and
the h1.

A single <local_nav> </local_nav> pair that gets substitued into the page,
and a single <main_content> </main_content> pair.

The tempate file has to have local_nav appear before main_content.  Both
must be present.  On the template side, there must be no content between the
tags that the victim will populate.

The first argument is the template file, the second is the file it's to be
applied to the third is the output filename
!#

(use-modules (ice-9 regex)
             (ice-9 rdelim))

(define (main args)
  (if (not (= 4 (length args)))
      (error "usage: mkpage <template> <datafile> <outputfile>"))
  (let* ((template (open-input-file (list-ref args 1)))
         (datafile (open-input-file (list-ref args 2)))
         (outputfile-name (list-ref args 3))
         (outputfile-new (string-append outputfile-name ".new")))
    (write-to-file (apply apply-data
                          (read-complete-file template)
                          (read-datafile datafile))
                   outputfile-new)
    (rename-file outputfile-new outputfile-name)))

(define (write-to-file string filename)
  (display string (open-output-file filename)))

(define (read-complete-file port)
  (read-delimited "" port))

;; Returns a list (section-name local-nav main-content)
;; Could use multiple return values instead...
(define (read-datafile port)
  (let* ((data (read-complete-file port))
         (section-match (string-match "<section_name>(.*)</section_name>" 
                                      data))
         (local-match (string-match "<local_nav>(.*)</local_nav>"
                                    data))
         (main-match (string-match "<main_content>(.*)</main_content>"
                                   data)))
    (list (if section-match
              (match:substring section-match 1)
              "")
          (if local-match
              (match:substring local-match 1)
              "")
          (if main-match
              (match:substring main-match 1)
              ""))))

(define (apply-data template section-name local-nav main-content)
  (substitute-match
   (make-regexp "<section_name>[^<]*</section_name>")
   section-name
   (substitute-match
    (make-regexp "<local_nav>[^<]*</local_nav>")
    local-nav
    (substitute-match
     (make-regexp "<main_content>[^<]*</main_content>")
     main-content
     template))))

(define (substitute-match rx replacement data)
  (let ((match (regexp-exec rx data)))
    (if (not match)
        data
        (string-append ; inefficient, but we can live with that
         (match:prefix match)
         replacement
         (substitute-match rx replacement (match:suffix match))))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]