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