]> code.delx.au - gnu-emacs-elpa/blob - doc/yas-doc-helper.el
fix docstring extraction for "fancy" docs
[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 (let ((doc-synth (car-safe (get symbol 'function-documentation))))
53 (if (functionp doc-synth)
54 (funcall doc-synth nil)
55 (documentation symbol t))))
56 (t
57 (format "*WARNING*: no symbol named =%s=" symbol)))
58 (format "*WARNING*: no doc for symbol =%s=" symbol)))
59 (case-fold-search nil))
60 ;; do some transformations on the body: FOO becomes /foo/ and
61 ;; `bar' becomes [[#bar][=bar=]]
62 (setq body (replace-regexp-in-string
63 "[A-Z][A-Z-]+" #'(lambda (match)
64 (format "/%s/" (downcase match)))
65 body)
66 body (replace-regexp-in-string "`\\([a-z-]+\\)'" #'(lambda (match)
67 (let* ((name (downcase (match-string 1 match)))
68 (sym (intern name)))
69 (if (and (or (boundp sym)
70 (fboundp sym))
71 (save-match-data
72 (string-match "^yas-" name)))
73 (format "[[#%s][=%s=]]"
74 name name)
75 (format "=%s=" name))))
76 body))
77 ;; output the paragraph
78 ;;
79 (concat-lines heading
80 after-heading
81 body))))
82
83 (defun yas--document-symbols (level &rest names-and-predicates)
84 (let ((sym-lists (make-vector (length names-and-predicates) nil)))
85 (loop for sym in yas--exported-syms
86 do (loop for test in (mapcar #'cdr names-and-predicates)
87 for i from 0
88 do (when (funcall test sym)
89 (push sym (aref sym-lists i))
90 (return))))
91 (loop for slist across sym-lists
92 for name in (mapcar #'car names-and-predicates)
93 concat (format "\n** %s\n" name)
94 concat (mapconcat (lambda (sym)
95 (yas--document-symbol sym (1+ level)))
96 slist "\n\n"))))
97
98 (defun yas--internal-link-snippet ()
99 (interactive)
100 (yas-expand-snippet "[[#$1][=${1:`yas/selected-text`}=]]"))
101
102 (define-key org-mode-map [M-f8] 'yas--internal-link-snippet)
103
104 ;; This lets all the org files be exported to HTML with
105 ;; `org-publish-current-project' (C-c C-e P).
106
107 (let* ((rev (or (with-temp-buffer
108 (when (eq (call-process "git" nil t nil
109 "rev-parse" "--verify" "HEAD") 0)
110 (buffer-string)))
111 yas--version))
112 (dir (if load-file-name (file-name-directory load-file-name)
113 default-directory))
114 (proj-plist
115 (list
116 :base-directory dir :publishing-directory dir
117 :html-postamble
118 (concat "<hr><p class='creator'>Generated by %c on %d from "
119 rev "</p>\n"
120 "<p class='xhtml-validation'>%v</p>\n")))
121 (project (assoc "yasnippet" org-publish-project-alist)))
122 (if project
123 (setcdr project proj-plist)
124 (push `("yasnippet" . ,proj-plist)
125 org-publish-project-alist)))
126
127 (defun yas--generate-html-batch ()
128 (let ((org-publish-use-timestamps-flag nil)
129 (org-export-copy-to-kill-ring nil)
130 (org-confirm-babel-evaluate nil)
131 (make-backup-files nil))
132 (org-publish "yasnippet" 'force)))
133
134
135
136 (provide 'yas-doc-helper)
137 ;;; yas-doc-helper.el ends here
138 ;; Local Variables:
139 ;; coding: utf-8
140 ;; End: