;; content-on-sx — multi-document index.
;;
;; Projects a list of documents into summary cards (the blog index page), with
;; tag filtering (category pages) and a tag cloud. Composes content/summary +
;; doc metadata.
;;
;; Requires (loaded by harness): summary.sx (content/summary), meta.sx (doc-tags).

(define
  idx-in?
  (fn
    (x xs)
    (cond
      ((= (len xs) 0) false)
      ((= (first xs) x) true)
      (else (idx-in? x (rest xs))))))

(define
  idx-dedup
  (fn
    (xs seen)
    (if
      (= (len xs) 0)
      (reverse seen)
      (if
        (idx-in? (first xs) seen)
        (idx-dedup (rest xs) seen)
        (idx-dedup (rest xs) (cons (first xs) seen))))))

(define content/index (fn (docs) (map content/summary docs)))

(define content/has-tag? (fn (doc tag) (idx-in? tag (doc-tags doc))))

(define
  content/index-by-tag
  (fn
    (docs tag)
    (map content/summary (filter (fn (d) (content/has-tag? d tag)) docs))))

(define
  content/all-tags
  (fn (docs) (idx-dedup (ct-flatmap-tags docs) (list))))

(define
  ct-flatmap-tags
  (fn
    (docs)
    (if
      (= (len docs) 0)
      (list)
      (append (doc-tags (first docs)) (ct-flatmap-tags (rest docs))))))
