;; content-on-sx — block query + table of contents. ;; ;; Collect blocks across the whole tree (descending into sections) by predicate ;; or type, and derive a table of contents from headings. Tree detection is ;; inline (class + st-iv-get) so this needs no section.sx. ;; ;; Requires (loaded by harness): block.sx, doc.sx. (define qry-section? (fn (b) (and (st-instance? b) (= (get b :class) "CtSection")))) (define qry-tree (fn (blocks) (if (= (len blocks) 0) (list) (let ((b (first blocks))) (append (cons b (if (qry-section? b) (let ((ch (st-iv-get b "children"))) (if (list? ch) (qry-tree ch) (list))) (list))) (qry-tree (rest blocks))))))) (define content/select (fn (doc pred) (filter pred (qry-tree (doc-blocks doc))))) (define content/select-type (fn (doc type) (content/select doc (fn (b) (= (blk-type b) type))))) (define content/count-type (fn (doc type) (len (content/select-type doc type)))) (define content/select-ids (fn (doc pred) (map (fn (b) (blk-id b)) (content/select doc pred)))) ;; table of contents: {:id :level :text} for every heading, in document order. (define content/headings (fn (doc) (map (fn (b) {:id (blk-id b) :text (blk-get b "text") :level (blk-get b "level")}) (content/select-type doc "heading"))))