Re: Best way to label hierarchical sections with their depths
[email protected] Tue, 28 Oct 2003 11:55:44 -0800 (PST)
| Newsgroups | gmane.lisp.scheme.ssax-sxml |
|---|---|
| Message-ID | <[email protected]> |
Hello!
> I have a document that looks something like this:
> (define doc2
> '(article
> (section (title "T1") (para "text")
> (section (title "T1.1") (para "text")
> (section (title "T1.1.1") (para "text"))
> (section (title "T1.1.2") (para "text")))
> (section (title "T1.2") (para "text")))
> (section (title "T2") (para "text"))))
>
> How to convert into something like this:
>
> (*section 1 (title "T1")
> ...
> (*section 2 (title "T1.1")
> ...
> (*section 3 (title "T1.1.1")
> ...
> )))
When information flows only up-and-down the branches (but not from a
node to its sibling), simple solutions are possible. For example, the
following. Note that we effectively "modify" the stylesheet to
incorporate the level number into the handler of the 'section'
element. My favorite trick: viral infection, which pushes the virus
deeper and deeper into the tree as the computation progresses.
(define my-ss
(letrec
((make-section-handler
(lambda (level)
(lambda (tag . elems)
(pre-post-order
`(*section ,level ,@elems)
(cons `(section *preorder* .
,(make-section-handler (+ 1 level)))
my-ss))))))
`((article *macro*
. ,(lambda (tag . elems)
`(*article ,@elems)))
(section *preorder* . ,(make-section-handler 1))
(*default* . ,(lambda x x))
(*text* . ,(lambda (tag str) str)))))
(pp (pre-post-order doc2 my-ss))
The code seems to print the desired result.
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/