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