1 | (in-package :abcl/documentation) |
---|
2 | |
---|
3 | (defun grovel-docstrings-as-tex (&key |
---|
4 | (package (find-package :java)) |
---|
5 | (directory (asdf:component-pathname (asdf:find-component :abcl/documentation 'grovel)))) |
---|
6 | "Transform exported symbols documentation in PACKAGE to DIRECTORY." |
---|
7 | (let ((output-file (merge-pathnames (format nil "~A.tex" (string-downcase (package-name package))) directory)) |
---|
8 | (symbols (loop :for symbol :being :each :external-symbol :of package :collecting symbol))) |
---|
9 | (with-open-file (stream output-file :direction :output) |
---|
10 | (format t "Writing output to '~A'.~%" output-file) |
---|
11 | (loop :for symbol :in (sort symbols (lambda (a b) (string-lessp (symbol-name a) (symbol-name b)))) |
---|
12 | :doing (let ((documentation (symbol-as-tex symbol))) |
---|
13 | (when documentation |
---|
14 | (format stream "~&~A~%~%" documentation))))))) |
---|
15 | |
---|
16 | (defun texify-string (string &optional remove) |
---|
17 | (with-output-to-string (s) |
---|
18 | (loop for char across string |
---|
19 | do (if (find char '(#\& #\% #\# #\_)) |
---|
20 | (unless remove |
---|
21 | (write-char #\\ s) |
---|
22 | (write-char char s)) |
---|
23 | (write-char char s))))) |
---|
24 | |
---|
25 | (defun texify (thing) |
---|
26 | "Return STRING with LaTeX-sensitive characters escaped. |
---|
27 | Downcase symbol names but leave strings alone." |
---|
28 | (cond ((listp thing) |
---|
29 | (format nil "~A" (mapcar #'texify thing))) |
---|
30 | ((stringp thing) (texify-string thing)) |
---|
31 | ((symbolp thing) (texify-string (string-downcase (symbol-name thing)))))) |
---|
32 | |
---|
33 | (defun arglist-as-tex (symbol) |
---|
34 | (handler-case |
---|
35 | (loop :for arg :in (ext:arglist symbol) |
---|
36 | :collecting (texify arg)) |
---|
37 | (t (e) |
---|
38 | (progn (warn "Failed to form arglist for ~A: ~A" symbol e) |
---|
39 | (list ""))))) |
---|
40 | |
---|
41 | (defvar *type-alist* |
---|
42 | '((:function |
---|
43 | . "Function") |
---|
44 | (:macro |
---|
45 | . "Macro") |
---|
46 | (:variable |
---|
47 | . "Variable") |
---|
48 | (:class |
---|
49 | . "Class") |
---|
50 | (:special-operator |
---|
51 | . "Special Operator") |
---|
52 | (:generic-function |
---|
53 | . "Generic Function"))) |
---|
54 | |
---|
55 | (defun symbol-as-tex (symbol) |
---|
56 | "Return the TeX representation of a SYMBOL as Tex." |
---|
57 | (let (type documentation arglist doc symbol-name package-name) |
---|
58 | (when (setf doc (swank-backend:describe-symbol-for-emacs symbol)) |
---|
59 | (cond |
---|
60 | ((find :function doc) |
---|
61 | (setf type :function |
---|
62 | documentation (second doc) |
---|
63 | arglist (format nil "~{~A~^ ~}" (arglist-as-tex symbol)))) |
---|
64 | ((find :variable doc) |
---|
65 | (setf type :variable |
---|
66 | documentation (second doc))) |
---|
67 | ((find :macro doc) |
---|
68 | (setf type :macro |
---|
69 | documentation (second doc))) |
---|
70 | ((find :generic-function doc) |
---|
71 | (setf type :generic-function |
---|
72 | documentation (second doc))) |
---|
73 | ((find :class doc) |
---|
74 | (setf type :class |
---|
75 | documentation (second doc))) |
---|
76 | ((find :special-operator doc) |
---|
77 | (setf type :special-operator |
---|
78 | documentation (second doc))) |
---|
79 | (t |
---|
80 | (warn "Unknown type of documentation for symbol ~A: ~A" |
---|
81 | symbol doc))) |
---|
82 | (setf symbol-name (string-downcase |
---|
83 | symbol) |
---|
84 | package-name (string-downcase |
---|
85 | (package-name (find-package (symbol-package symbol))))) |
---|
86 | (format nil "~&\\paragraph{} |
---|
87 | \\label{~A:~A} |
---|
88 | \\index{~A} |
---|
89 | --- ~A: \\textbf{~A} [\\textbf{~A}] \\textit{~A} |
---|
90 | |
---|
91 | \\begin{adjustwidth}{5em}{5em} |
---|
92 | ~A |
---|
93 | \\end{adjustwidth}" |
---|
94 | (texify-string (package-name (find-package (symbol-package symbol))) t) |
---|
95 | (texify-string (symbol-name symbol) t) |
---|
96 | (texify-string (symbol-name symbol) t) |
---|
97 | (cdr (assoc type *type-alist*)) |
---|
98 | (texify symbol-name) |
---|
99 | (texify package-name) |
---|
100 | (if arglist arglist "") |
---|
101 | (if documentation (texify documentation) ""))))) |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|