Re: From flat structure to a tree

Subject: Re: From flat structure to a tree
From: Brandon Ibach <bibach@xxxxxxxxxxxxxx>
Date: Fri, 4 Feb 2000 19:08:07 -0600
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
>   Scheme programming).
> * Call (make element gi: "div" (process-node-list sublist)) for
>   each sublist.
> * Apply the same thing recursively for H2, H3, ...
> You will have to replace the default rule with something along these
> lines - not the h1 rule.
   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)))))))

   Note that this is mostly off the top of my head, and completely
untested.  If it doesn't work, let me know, and I'll fix it. :)

-Brandon :)

 DSSSList info and archive:

Current Thread