+ (cond ((null list) nil)
+ ((symbolp list) (list list))
+ ((consp list) (append (all-symbols (car list))
+ (all-symbols (cdr list))))
+ (t nil)))
+
+
+(defun frob-doc-line (line var-symbols)
+ "Format symbols in LINE texinfo-style: either as code or as
+ variables if the symbol in question is contained in
+ var-symbols."
+ (with-output-to-string (result)
+ (let ((last 0))
+ (dolist (symbol-index (locate-symbols line))
+ (write-string (subseq line last (first symbol-index)) result)
+ (let ((symbol-name (apply #'subseq line symbol-index)))
+ (format result (if (member symbol-name var-symbols
+ :test #'string=)
+ "@var{~A}"
+ "@code{~A}")
+ (string-downcase symbol-name)))
+ (setf last (second symbol-index)))
+ (write-string (subseq line last) result))))
+
+(defparameter *itemize-start-characters* '(#\* #\-)
+ "Characters that might start an itemization in docstrings when
+ at the start of a line.")
+
+(defun indentation (line)
+ "Position of first non-SPACE character in LINE."
+ (position-if-not (lambda (c) (char= c #\Space)) line))
+
+(defun maybe-itemize-offset (line)
+ "Return NIL or the indentation offset if LINE looks like it
+ starts an item in an itemization."
+ (let ((offset (indentation line)))
+ (when (and offset
+ (member (char line offset) *itemize-start-characters*
+ :test #'char=))
+ offset)))
+
+(defun collect-maybe-itemized-section (lines starting-line arglist-symbols)
+ ;; Return index of next line to be processed outside
+ (let ((this-offset (maybe-itemize-offset (svref lines starting-line)))
+ (result nil)
+ (lines-consumed 0))
+ (loop for line-number from starting-line below (length lines)
+ for line = (svref lines line-number)
+ for indentation = (indentation line)
+ for offset = (maybe-itemize-offset line)
+ do (cond
+ ((not indentation)
+ ;; empty line -- inserts paragraph.
+ (push "" result)
+ (incf lines-consumed))
+ ((and offset (> indentation this-offset))
+ ;; nested itemization -- handle recursively
+ (multiple-value-bind (sub-lines-consumed sub-itemization)
+ (collect-maybe-itemized-section lines line-number
+ arglist-symbols)
+ (when sub-lines-consumed
+ (incf line-number (1- sub-lines-consumed)) ; +1 on next loop
+ (incf lines-consumed sub-lines-consumed)
+ (setf result (nconc (nreverse sub-itemization) result)))))
+ ((and offset (= indentation this-offset))
+ ;; start of new item
+ (push (format nil "@item ~A"
+ (frob-doc-line (subseq line (1+ offset))
+ arglist-symbols))
+ result)
+ (incf lines-consumed))
+ ((and (not offset) (> indentation this-offset))
+ ;; continued item from previous line
+ (push (frob-doc-line line arglist-symbols) result)
+ (incf lines-consumed))
+ (t
+ ;; end of itemization
+ (loop-finish))))
+ (if
+ ;; a single-line itemization isn't.
+ (> (count-if (lambda (line) (> (length line) 0)) result) 1)
+ (values lines-consumed
+ `("@itemize" ,@(reverse result) "@end itemize"))
+ nil)))
+
+
+(defun maybe-table-offset (line)
+ "Return NIL or the indentation offset if LINE looks like it
+ starts an item in a tabulation, i.e., there's only a symbol on the line."
+ (let ((offset (indentation line)))
+ (when (and offset
+ (every (lambda (c)
+ (or (char= c #\Space)
+ (find c *symbol-characters* :test #'char=)))
+ line))
+ offset)))
+
+
+(defun collect-maybe-table-section (lines starting-line arglist-symbols)
+ ;; Return index of next line to be processed outside
+ (let ((this-offset (maybe-table-offset (svref lines starting-line)))
+ (result nil)
+ (lines-consumed 0))
+ (loop for line-number from starting-line below (length lines)
+ for line = (svref lines line-number)
+ for indentation = (indentation line)
+ for offset = (maybe-table-offset line)
+ do (cond
+ ((not indentation)
+ ;; empty line -- inserts paragraph.
+ (push "" result)
+ (incf lines-consumed))
+ ((and offset (= indentation this-offset))
+ ;; start of new item, or continuation of previous item
+ (if (and result (search "@item" (car result) :test #'char=))
+ (push (format nil "@itemx ~A"
+ (frob-doc-line line arglist-symbols))
+ result)
+ (progn
+ (push "" result)
+ (push (format nil "@item ~A"
+ (frob-doc-line line arglist-symbols))
+ result)))
+ (incf lines-consumed))
+ ((> indentation this-offset)
+ ;; continued item from previous line
+ (push (frob-doc-line line arglist-symbols) result)
+ (incf lines-consumed))
+ (t
+ ;; end of itemization
+ (loop-finish))))
+ (if
+ ;; a single-line table isn't.
+ (> (count-if (lambda (line) (> (length line) 0)) result) 1)
+ (values lines-consumed
+ `("" "@table @code" ,@(reverse result) "@end table" ""))
+ nil)))
+
+
+
+
+(defun string-as-lines (string)
+ (coerce (with-input-from-string (s string)
+ (loop for line = (read-line s nil nil)
+ while line collect line))
+ 'vector))