]> code.delx.au - gnu-emacs-elpa/blob - doc/yas-doc-helper.el
retrieve function documentation correctly
[gnu-emacs-elpa] / doc / yas-doc-helper.el
1 ;;; yas-doc-helper.el --- Help generate documentation for YASnippet
2
3 ;; Copyright (C) 2012 João Távora
4
5 ;; Author: João Távora <joaotavora@gmail.com>
6 ;; Keywords: convenience
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; Some functions to help generate YASnippet docs
24
25 ;;; Code:
26
27 (eval-when-compile
28 (require 'cl))
29 (require 'org)
30 (require 'org-publish)
31 (require 'yasnippet) ; docstrings must be loaded
32
33 (defun yas--document-symbol (symbol level)
34 (flet ((concat-lines (&rest lines)
35 (mapconcat #'identity lines "\n")))
36 (let* ((stars (make-string level ?*))
37 (heading (cond ((fboundp symbol)
38 (format "%s =%s= (%s)"
39 stars
40 symbol
41 (mapconcat #'symbol-name
42 (help-function-arglist symbol t) " ")))
43 (t
44 (format "%s =%s=\n" stars symbol))))
45 (after-heading
46 (concat-lines ":PROPERTIES:"
47 (format ":CUSTOM_ID: %s" symbol)
48 ":END:"))
49 (body (or (cond ((boundp symbol)
50 (documentation-property symbol 'variable-documentation t))
51 ((fboundp symbol)
52 (documentation symbol t))
53 (t
54 (format "*WARNING*: no symbol named =%s=" symbol)))
55 (format "*WARNING*: no doc for symbol =%s=" symbol)))
56 (case-fold-search nil))
57 ;; do some transformations on the body: FOO becomes /foo/ and
58 ;; `bar' becomes [[#bar][=bar=]]
59 (setq body (replace-regexp-in-string
60 "[A-Z][A-Z-]+" #'(lambda (match)
61 (format "/%s/" (downcase match)))
62 body)
63 body (replace-regexp-in-string "`\\([a-z-]+\\)'" #'(lambda (match)
64 (let* ((name (downcase (match-string 1 match)))
65 (sym (intern name)))
66 (if (and (or (boundp sym)
67 (fboundp sym))
68 (save-match-data
69 (string-match "^yas-" name)))
70 (format "[[#%s][=%s=]]"
71 name name)
72 (format "=%s=" name))))
73 body))
74 ;; output the paragraph
75 ;;
76 (concat-lines heading
77 after-heading
78 body))))
79
80 (defun yas--document-symbols (level &rest names-and-predicates)
81 (let ((sym-lists (make-vector (length names-and-predicates) nil)))
82 (loop for sym in yas--exported-syms
83 do (loop for test in (mapcar #'cdr names-and-predicates)
84 for i from 0
85 do (when (funcall test sym)
86 (push sym (aref sym-lists i))
87 (return))))
88 (loop for slist across sym-lists
89 for name in (mapcar #'car names-and-predicates)
90 concat (format "\n** %s\n" name)
91 concat (mapconcat (lambda (sym)
92 (yas--document-symbol sym (1+ level)))
93 slist "\n\n"))))
94
95 (defun yas--internal-link-snippet ()
96 (interactive)
97 (yas-expand-snippet "[[#$1][=${1:`yas/selected-text`}=]]"))
98
99 (define-key org-mode-map [M-f8] 'yas--internal-link-snippet)
100
101 ;; This lets all the org files be exported to HTML with
102 ;; `org-publish-current-project' (C-c C-e P).
103
104 (let* ((rev (or (with-temp-buffer
105 (when (eq (call-process "git" nil t nil
106 "rev-parse" "--verify" "HEAD") 0)
107 (buffer-string)))
108 yas--version))
109 (dir (if load-file-name (file-name-directory load-file-name)
110 default-directory))
111 (proj-plist
112 (list
113 :base-directory dir :publishing-directory dir
114 :html-postamble
115 (concat "<hr><p class='creator'>Generated by %c on %d from "
116 rev "</p>\n"
117 "<p class='xhtml-validation'>%v</p>\n")))
118 (project (assoc "yasnippet" org-publish-project-alist)))
119 (if project
120 (setcdr project proj-plist)
121 (push `("yasnippet" . ,proj-plist)
122 org-publish-project-alist)))
123
124 (defun yas--generate-html-batch ()
125 (let ((org-publish-use-timestamps-flag nil)
126 (org-export-copy-to-kill-ring nil)
127 (org-confirm-babel-evaluate nil)
128 (make-backup-files nil))
129 (org-publish "yasnippet" 'force)))
130
131
132
133 (provide 'yas-doc-helper)
134 ;;; yas-doc-helper.el ends here
135 ;; Local Variables:
136 ;; coding: utf-8
137 ;; End: