;; content-on-sx — document flatten.
;;
;; Un-nests a sectioned document into a flat block sequence: each section is
;; replaced inline by its (recursively flattened) children, dropping the section
;; wrapper. The inverse of content/wrap-section, for flat export targets.
;; Immutable; inline tree handling (no section.sx dep).
;;
;; Requires (loaded by harness): block.sx, doc.sx.

(define
  flat-section?
  (fn (b) (and (st-instance? b) (= (get b :class) "CtSection"))))

(define
  flat-blocks
  (fn
    (blocks)
    (if
      (= (len blocks) 0)
      (list)
      (let
        ((b (first blocks)))
        (append
          (if
            (flat-section? b)
            (let
              ((ch (st-iv-get b "children")))
              (if (list? ch) (flat-blocks ch) (list)))
            (list b))
          (flat-blocks (rest blocks)))))))

(define
  content/flatten
  (fn (doc) (doc-with-blocks doc (flat-blocks (doc-blocks doc)))))
