Re: From flat structure to a tree

Subject: Re: From flat structure to a tree
From: Jany Quintard <quintard.j@xxxxxx>
Date: Tue, 8 Feb 2000 15:06:49 +0100 (CET)
On Fri, 4 Feb 2000, Brandon Ibach wrote:

> Quoting Holger Klawitter <holger@xxxxxxxxxxxx>:
> > Not really thought through, but it should work if you do the following
> > instead of process-children:
> > .../...
> > * Get the node-list of the children of the current node.
> > * Make a list of lists out of them each starting with h1. (Involves
> > 
>    Not sure I completely followed that, but it got me thinking in the
> right direction, anyway. :)  If you view the problem as a matter of
> processing a sequence of elements, for any given header element (H?),
> you want to create <DIV><H?></H?>...</DIV> with the ... being all of
> the following siblings of the H? up until the next H? of equal or
> lesser ?.  Perhaps an example will make that clear...
>         <h1>Chapter 1</h1>
>           <h2>Section 1</h2>
>             <h3>Para 1</h3>
>               <p>Blah, blah</p>
>             <h3>Para 2</h3>
>               <p>Bleh, bleh</p>
>         <h1>Chapter 2</h1>
> with a stylesheet something like
>    (define six '(#\1 #\2 #\3 #\4 #\5 #\6))
>    (define spos (lambda (l) (- (length six) (length (member l six)))))
>    (define hlev (lambda (n) (let* ((g (gi n)) (gn (string-ref g 1)))
>      (and (char=? #\H (string-ref g 0)) (member gn six) gn))))
>    (define getkids (lambda (nd lev)
>      (let loop ((n (follow nd)) (r (empty-node-list)))
>        (if (or (node-list-empty? n) (let ((h (hlev (node-list-first n))))
>                                       (and h (<= (spos h) (spos lev)))))
>            r (loop (node-list-rest n) (node-list r (node-list-first n)))))))
>    (element p (make element))
>    ; other element rules here, as needed
>    (default (let ((h (hlev (current-node))))
>      (if h (make element gi: "DIV" (make element)
>              (process-node-list (getkids (current-node) h)))
>          (error (string-append "Invalid element " (gi (current-node)))))))

Very good. This is *much* simpler than the code I wrote when trying to
implement Holgers suggestion (my poor twisted mind).
I wonder if the default rule could be written this way :

(default (let ((h (hlev (current-node))))
  (if h (make element gi: "DIV"
          (process-node-list (getkids (current-node) h)))
        (make element))))

This should do all element processing, provided a wholesome
(copy-attributes) function. (I have one)

There is a last problem which prevents the code to work :
On heading elements, the (getkids) function calls (hlev) and I get
systematically an error when trying to get the gi of the node. In my test
file, this is a <p>. But the (gi) returns #f 
There is something (or many things) I do not understand.

Any idea.

 DSSSList info and archive:

Current Thread