docgen.el 7.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
;;;;; -*-coding: iso-8859-1;-*-
;;;;;
;;;;; $Id$
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
;;;;;
;;;;; This file is part of the LysKOM Emacs LISP client.
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
;;;;; the Free Software Foundation; either version 2, or (at your option) 
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: docgen.el
;;;;
;;;; Generate documentation data for inclusion in the user manual
;;;;

(require 'cl)


(defvar lyskom-docgen-external-refs
  '((lyskom-read-text-no-prefix-arg . lyskom-read-text-no-prefix-arg)))

(defvar lyskom-docgen-errors nil)

(defvar lyskom-docgen-found-variables nil)
(defvar lyskom-docgen-found-functions nil)

(defvar lyskom-docgen-xrefd-variables nil)
(defvar lyskom-docgen-xrefd-functions nil)


(defun lyskom-docgen-error (fmt &rest args)
  (apply 'message fmt args)
  (setq lyskom-docgen-errors (cons (apply 'format fmt args)
                                     lyskom-docgen-errors)))

(defun lyskom-docgen-report-errors ()
  (when nil
    (lyskom-traverse msg (nreverse lyskom-docgen-errors)
      (message msg))))

(defun lyskom-docgen-princ (str)
  (insert str))

(defun lyskom-docgen-fixup-sgml (str)
  (when str
    (let ((s str))
      (setq s (replace-in-string s "<" "&lt;"))
      (setq s (replace-in-string s ">" "&lt;"))
      (setq s (replace-in-string s "&" "&amp;"))
      s)))

(defun lyskom-docgen-get-name (fn lang)
  (let ((lyskom-language lang))
    (lyskom-command-name fn)))

(defun lyskom-docgen-get-doc (fn)
  (let ((doc (documentation fn)))
    (when (and doc (string-match "Lisp documentation:" doc))
      (setq doc (substring doc 0 (match-beginning 0))))
    (lyskom-docgen-transform-refs fn (lyskom-docgen-fixup-sgml doc))))

(defun lyskom-docgen-transform-refs (fn doc)
  (if doc
      (let ((result "")
            (start 0))
        (while (string-match "`\\(.*?\\)'" doc start)
          (setq result (concat result (substring doc start (match-beginning 0))))
          (let ((sym (intern (match-string 1 doc))))
            (cond 
             ((assq sym lyskom-docgen-external-refs)
              (setq result
                    (concat result
                            (format "<xref linkend=\"%s\" />" 
                                    (cdr (assq sym lyskom-docgen-external-refs))))))
             ((fboundp sym)
              (unless (memq sym lyskom-commands)
                (lyskom-docgen-error "%s: reference to non-command %s" fn sym))
              (setq result
                    (concat result
                            (format "<link linkend=\"fn:%s\"><command>%s</command></link> (<link linkend=\"fn:%s\"><function>%s</function></link>)"
                                    sym
                                    (lyskom-docgen-get-name fn 'en)
                                    sym
                                    sym)))
              (setq lyskom-docgen-xrefd-functions 
                    (add-to-list 'lyskom-docgen-xrefd-functions sym)))
             ((boundp sym)
              (setq result
                    (concat result
                            (format "<link linkend=\"var:%s\">%s</link>"
                                    sym sym)))
              (setq lyskom-docgen-xrefd-variables 
                    (add-to-list 'lyskom-docgen-xrefd-variables sym)))
             (t (lyskom-docgen-error "%s: undefined reference to %s" fn sym)
                (setq result (concat result 
                                     "`"
                                     (match-string 1 doc)
                                     "'"))))
            (setq start (match-end 0))))
        (setq result (concat result (substring doc start)))
        (concat "      <para>\n"
                (replace-in-string result "\n\n+" "\n      </para>\n      <para>\n")
                "\n      </para>\n")
        )
    (lyskom-docgen-error "%s: No documentation!" fn)
    "<para>Undocumented.</para>"))

(defun lyskom-docgen-format-key-sequence (binding)
  (mapconcat
   (lambda (x) 
     (format "<keycap>%s</keycap>" 
             (lyskom-docgen-fixup-sgml (key-description x))))
   binding " "))


(defun lyskom-docgen-2 ()
  (lyskom-docgen-princ "<variablelist>\n")
  (lyskom-traverse command lyskom-commands
    (let* ((name-sv (lyskom-docgen-get-name command 'sv))
           (name-en (lyskom-docgen-get-name command 'en))
           (bind-sv (where-is-internal command lyskom-sv-mode-map))
           (bind-en (where-is-internal command lyskom-en-mode-map))
           (doc-en (lyskom-docgen-get-doc command)))
      (lyskom-docgen-princ
       (format "\
  <varlistentry>
    <term><anchor id=\"fn:%s\" />%s</term>
    <listitem>
      <informaltable frame=\"top\">
        <tgroup cols=\"2\">
          <tbody valign=\"top\">
            <row>
              <entry>English name</entry>
              <entry>%s</entry>
            </row>
            <row>
              <entry>English binding</entry>
              <entry>%s</entry>
            </row>
            <row>
              <entry>Swedish name</entry>
              <entry>%s</entry>
            </row>
            <row>
              <entry>Swedish binding</entry>
              <entry>%s</entry>
            </row>
          </tbody>
        </tgroup>
      </informaltable>
%s
    </listitem>
  </varlistentry>\n\n" 
               command 
               command 
               name-en
               (lyskom-docgen-format-key-sequence bind-en)
               name-sv
               (lyskom-docgen-format-key-sequence bind-sv)
               doc-en))
      (setq lyskom-docgen-found-functions
            (cons command lyskom-docgen-found-functions))))
  (lyskom-docgen-princ "</variablelist>\n\n"))

(defun lyskom-docgen-generate-missing-anchors ()
  (lyskom-docgen-princ "<para>")
  (lyskom-traverse fn (set-difference lyskom-docgen-xrefd-functions
                                      lyskom-docgen-found-functions)
    (lyskom-docgen-princ
     (format "<anchor id=\"fn:%s\" />Missing target: %s;" fn fn)))

  (lyskom-traverse var (set-difference lyskom-docgen-xrefd-variables
                                       lyskom-docgen-found-variables)
        (lyskom-docgen-princ
         (format "<anchor id=\"var:%s\" />Missing target: %s;" var var)))
  (lyskom-docgen-princ "</para>"))



(defun lyskom-docgen ()
  (let ((buf (get-buffer-create "*LysKOM DOCGEN*")))
    (set-buffer buf)
    (set-language-environment 'latin-1)
    (set-buffer-multibyte nil)
    (lyskom-docgen-princ "
<?xml version='1.0' encoding='iso-8859-1' ?>
<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\"
               \"docbook/dtd/xml/4.2/docbookx.dtd\">
<book lang=\"en\">
  <chapter>
    <title>Command Reference</title>

    <para>
      This chapter describes all commands in LysKOM. Commands are
      listed in alphabetical order by english name.
    </para>
")
    (let ((lyskom-docgen-errors nil))
      (unwind-protect
          (lyskom-docgen-2)
        (lyskom-docgen-report-errors)))
    (lyskom-docgen-generate-missing-anchors)
    (lyskom-docgen-princ "\
<section id=\"lyskom-read-text-no-prefix-arg\">
  <title>Dummy</title>
  <para>Dummy</para>
</section>\n")
    (lyskom-docgen-princ "</chapter></book>")
    (write-file "DOC.xml")))



(load-file "./lyskom.elc")