Hierarchical Numbering

Hierarchical numbering using specific countable elements

Richard Light

Sometimes sections within a document have to be numbered according to the familiar ‘X.X.X’ scheme, but only specified element types ‘count’ as contributors to this numbering. Also, there can be a glorious mixture of said element types at each level. Thus (child-number) is no use, and even counting preceding siblings isn't good enough.

This code uses (node-list-filter), which in turn uses (node-list-reduce) - these are both printed in the DSSSL standard, but are not implemented in Jade v1.0 so you need to type them in. This is the core routine:

; preced-count: gives the number of 'significant' (for numbering)
; sibling elements prior to node. This routine relies on a specific
; list of element types, which must include all elements that
; are numbered:
(define (preced-count node gilist)
      (node-list-length
            (node-list-filter
                (lambda (n1)
                        (member (gi n1) gilist)
                )
                (preced node)
            )
      )
)

You need to declare your ‘countable elements’ as a list of strings:

(define countable-elements '("AAA" "BBB" "CCC" ...))

Then you add one to the number returned by (preced-count) to get the correct number for the current element in a (number-clause) routine:

(define (number-clause node top-level)
        (case (gi node)
              (("ROOT-NODE-GI")
               (list)
               )
              (else
               (if top-level
                   (list (+ (preced-count node countable-elements) 1))
                   (cons (+ (preced-count node countable-elements) 1)
                         (number-clause (parent node) top-level)
                   )
               )
              )
        )
)

... which in turn is called by a (number-heading) routine which returns a formatted string ...

(define (number-heading node top-level)
  (literal
   (format-number-list (reverse (number-clause node top-level))
                       "1"
                       ".")))

... that you can stick in front of the relevant heading:

                ...
                (make sequence
                      (number-heading (parent (current-node)) #t)
                      (literal " ")
                      (process-children)
                )
                ...

(This assumes that the heading is a child of the structural element which is being counted – hence (number-heading) is applied to the parent of the heading element.)