(def dox-show-src (src)

; show 'src as source code.
; expect to override this later when pretty-printing is available
(inspect src))

(def dox-show-info (name what texts args src)

; show the given info.src dox info
; 'info arg is a dox object from the dox system
"~(if (eq? what 'mac) "Macro"
      (eq? what 'def) "Function"
      what) : ~name

args : ~(inspect args)

~(joinstr “n” texts)

source

~(dox-show-src src) “)

(def dox-show-one-example (name example)

"~|name| ~(car example)

example :

~(dox-show-src:cadr example)

returns : ~(dox-show-src:caddr example)


“)

(def dox-show-examples (name examples)

(if examples
"

Examples for ~name

~(joinstr “nn” (map (curry dox-show-one-example name) examples))

“))

(def dox-all-items ()

; return all documentation items in a single list
(apply joinlists (map dox-lookup (dox-names))))

(def dox-with-documentation (dox-item)

; a documentation filter that returns non-nil for items with text documentation
; use with 'dox-all-items to gather dox items that are explicitly documented
; for example (cleverly-show (collect dox-with-documentation (dox-all-items)))
(nth 2 dox-item))

(mac dox (name)

; show dox for the given name, or all available dox if name is not given
(if name
    `(let infos (dox-lookup ',name)
       (if (no infos)
           (p "No documentation for" ',name)
           (each info infos
                 (p (dox-show-info info.name info.what info.texts info.args info.src))))
       (let examples (dox-examples ',name)
         (if (no examples)
             (p "No examples for" ',name)
             (p (joinstr "\n" (map (curry dox-show-examples ',name) examples)))))
       nil)
    `(let infos (collect dox-with-documentation (dox-all-items))
       (p "documentation available for the following:")
       (each info infos
             (p:inspect:firstn 2 info)))))

(def-colon-syntax dox names

(let target (cadr names)
  `(dox ,(if (eq? target '||)
             nil
             target))))

;; like mac-expand but only goes n steps (def explain-mac (n expr)

(if (eq? n 0)
    expr
    (let macfn (hash-get macs (car expr))
      (if macfn
          (explain-mac (- n 1)
                      (apply macfn
                             (cdr expr)))
          expr))))

(def chapter-remove-item (chapter-name item-name)

; remove the named item from the named chapter
(chapter nydp/documentation)
(let ch (chapter-find chapter-name)
  (= ch.contents (collect (fn (item) (!eq? item.name item-name)) ch.contents))))

; return the first dox item of the given type with the given name (def dox-item-by-type (type name)

(let n (sym name)
  (detect (fn (i) (eq? n i.name)) (dox-items-by-type type))))