blob: eb7e00bb53a3f6825628b4eb652b1107767cc4c8 [file] [log] [blame]
Fred Drakebfc18bd2002-05-03 04:50:51 +00001;;; py2texi.el -- Conversion of Python LaTeX documentation to Texinfo
2
3;; Copyright (C) 1998, 1999, 2001, 2002 Milan Zamazal
4
5;; Author: Milan Zamazal <pdm@zamazal.org>
6;; Version: $Id$
7;; Keywords: python
8
9;; COPYRIGHT NOTICE
10;;
11;; This program is free software; you can redistribute it and/or modify it
12;; under the terms of the GNU General Public License as published by the Free
13;; Software Foundation; either version 2, or (at your option) any later
14;; version.
15;;
16;; This program is distributed in the hope that it will be useful, but
17;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19;; for more details.
20;;
21;; You can find the GNU General Public License at
22;; http://www.gnu.org/copyleft/gpl.html
23;; or you can write to the Free Software Foundation, Inc., 59 Temple Place,
24;; Suite 330, Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27
28;; This is a Q&D hack for conversion of Python manuals to on-line help format.
29;; I desperately needed usable online documenta for Python, so I wrote this.
30;; The result code is ugly and need not contain complete information from
31;; Python manuals. I apologize for my ignorance, especially ignorance to
32;; python.sty. Improvements of this convertor are welcomed.
33
34;; How to use it:
35;; Load this file and apply `M-x py2texi'. You will be asked for name of a
36;; file to be converted.
37
38;; Where to find it:
39;; New versions of this code might be found at
40;; http://www.zamazal.org/software/python/py2texi/ .
41
42;;; Code:
43
44
45(require 'texinfo)
46(eval-when-compile
47 (require 'cl))
48
49
50(defvar py2texi-python-version "2.2"
51 "What to substitute for the \\version macro.")
52
53(defvar py2texi-python-short-version
54 (progn
55 (string-match "[0-9]+\\.[0-9]+" py2texi-python-version)
56 (match-string 0 py2texi-python-version))
57 "Short version number, usually set by the LaTeX commands.")
58
Fred Drake66abcee2002-11-13 19:31:04 +000059(defvar py2texi-texi-file-name nil
60 "If non-nil, that string is used as the name of the Texinfo file.
61Otherwise a generated Texinfo file name is used.")
62
63(defvar py2texi-info-file-name nil
64 "If non-nil, that string is used as the name of the Info file.
65Otherwise a generated Info file name is used.")
66
Fred Drakebfc18bd2002-05-03 04:50:51 +000067(defvar py2texi-stop-on-problems nil
68 "*If non-nil, stop when you encouter soft problem.")
69
70(defconst py2texi-environments
71 '(("abstract" 0 "@quotation" "@end quotation\n")
72 ("center" 0 "" "")
73 ("cfuncdesc" 3
74 (progn (setq findex t)
75 "\n@table @code\n@item \\1 \\2(\\3)\n@findex \\2\n")
Fred Drake66abcee2002-11-13 19:31:04 +000076 "@end table\n")
77 ("cmemberdesc" 3
78 "\n@table @code\n@item \\2 \\3\n"
79 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +000080 ("classdesc" 2
81 (progn (setq obindex t)
82 "\n@table @code\n@item \\1(\\2)\n@obindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +000083 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +000084 ("classdesc*" 1
85 (progn (setq obindex t)
86 "\n@table @code\n@item \\1\n@obindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +000087 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +000088 ("csimplemacrodesc" 1
89 (progn (setq cindex t)
90 "\n@table @code\n@item \\1\n@cindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +000091 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +000092 ("ctypedesc" 1
93 (progn (setq cindex t)
94 "\n@table @code\n@item \\1\n@cindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +000095 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +000096 ("cvardesc" 2
97 (progn (setq findex t)
98 "\n@table @code\n@item \\1 \\2\n@findex \\2\n")
Fred Drake66abcee2002-11-13 19:31:04 +000099 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000100 ("datadesc" 1
101 (progn (setq findex t)
102 "\n@table @code\n@item \\1\n@findex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000103 "@end table\n")
104 ("datadescni" 1 "\n@table @code\n@item \\1\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000105 ("definitions" 0 "@table @dfn" "@end table\n")
106 ("description" 0 "@table @samp" "@end table\n")
107 ("displaymath" 0 "" "")
108 ("document" 0
109 (concat "@defcodeindex mo\n"
110 "@defcodeindex ob\n"
111 "@titlepage\n"
112 (format "@title " title "\n")
113 (format "@author " author "\n")
114 "@page\n"
115 author-address
116 "@end titlepage\n"
117 "@node Top, , , (dir)\n")
118 (concat "@indices\n"
119 "@contents\n"
120 "@bye\n"))
121 ("enumerate" 0 "@enumerate" "@end enumerate")
122 ("excdesc" 1
123 (progn (setq obindex t)
124 "\n@table @code\n@item \\1\n@obindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000125 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000126 ("excclassdesc" 2
127 (progn (setq obindex t)
128 "\n@table @code\n@item \\1(\\2)\n@obindex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000129 "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000130 ("flushleft" 0 "" "")
Fred Drake66abcee2002-11-13 19:31:04 +0000131 ("fulllineitems" 0 "\n@table @code\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000132 ("funcdesc" 2
133 (progn (setq findex t)
134 "\n@table @code\n@item \\1(\\2)\n@findex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000135 "@end table\n")
136 ("funcdescni" 2 "\n@table @code\n@item \\1(\\2)\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000137 ("itemize" 0 "@itemize @bullet" "@end itemize\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000138 ("list" 2 "\n@table @code\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000139 ("longtableii" 4 (concat "@multitable @columnfractions .5 .5\n"
140 "@item \\3 @tab \\4\n"
141 "@item ------- @tab ------ \n")
142 "@end multitable\n")
143 ("longtableiii" 5 (concat "@multitable @columnfractions .33 .33 .33\n"
144 "@item \\3 @tab \\4 @tab \\5\n"
145 "@item ------- @tab ------ @tab ------\n")
146 "@end multitable\n")
147 ("memberdesc" 1
148 (progn (setq findex t)
149 "\n@table @code\n@item \\1\n@findex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000150 "@end table\n")
151 ("memberdescni" 1 "\n@table @code\n@item \\1\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000152 ("methoddesc" 2
153 (progn (setq findex t)
154 "\n@table @code\n@item \\1(\\2)\n@findex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000155 "@end table\n")
156 ("methoddescni" 2 "\n@table @code\n@item \\1(\\2)\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000157 ("notice" 0 "@emph{Notice:} " "")
158 ("opcodedesc" 2
159 (progn (setq findex t)
160 "\n@table @code\n@item \\1 \\2\n@findex \\1\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000161 "@end table\n")
162 ("productionlist" 0 "\n@table @code\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000163 ("quotation" 0 "@quotation" "@end quotation")
Fred Drake66abcee2002-11-13 19:31:04 +0000164 ("seealso" 0 "See also:\n@table @emph\n" "@end table\n")
165 ("seealso*" 0 "@table @emph\n" "@end table\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000166 ("sloppypar" 0 "" "")
167 ("small" 0 "" "")
168 ("tableii" 4 (concat "@multitable @columnfractions .5 .5\n"
169 "@item \\3 @tab \\4\n"
170 "@item ------- @tab ------ \n")
171 "@end multitable\n")
172 ("tableiii" 5 (concat "@multitable @columnfractions .33 .33 .33\n"
173 "@item \\3 @tab \\4 @tab \\5\n"
174 "@item ------- @tab ------ @tab ------\n")
175 "@end multitable\n")
176 ("tableiv" 6 (concat
177 "@multitable @columnfractions .25 .25 .25 .25\n"
178 "@item \\3 @tab \\4 @tab \\5 @tab \\6\n"
179 "@item ------- @tab ------- @tab ------- @tab -------\n")
180 "@end multitable\n")
181 ("tablev" 7 (concat
182 "@multitable @columnfractions .20 .20 .20 .20 .20\n"
183 "@item \\3 @tab \\4 @tab \\5 @tab \\6 @tab \\7\n"
184 "@item ------- @tab ------- @tab ------- @tab ------- @tab -------\n")
185 "@end multitable\n"))
186 "Associative list defining substitutions for environments.
187Each list item is of the form (ENVIRONMENT ARGNUM BEGIN END) where:
188- ENVIRONMENT is LaTeX environment name
189- ARGNUM is number of (required) macro arguments
190- BEGIN is substitution for \begin{ENVIRONMENT}
191- END is substitution for \end{ENVIRONMENT}
192Both BEGIN and END are evaled. Moreover, you can reference arguments through
193\N regular expression notation in strings of BEGIN.")
194
195(defconst py2texi-commands
196 '(("ABC" 0 "ABC")
197 ("appendix" 0 (progn (setq appendix t) ""))
198 ("ASCII" 0 "ASCII")
199 ("author" 1 (progn (setq author (match-string 1 string)) ""))
200 ("authoraddress" 1
201 (progn (setq author-address (match-string 1 string)) ""))
202 ("b" 1 "@w{\\1}")
203 ("bf" 0 "@destroy")
204 ("bifuncindex" 1 (progn (setq findex t) "@findex{\\1}"))
205 ("C" 0 "C")
206 ("c" 0 "@,")
207 ("catcode" 0 "")
208 ("cdata" 1 "@code{\\1}")
209 ("centerline" 1 "@center \\1")
Fred Drake66abcee2002-11-13 19:31:04 +0000210 ("cfuncline" 3 "@itemx \\1 \\2(\\3)\n@findex \\2")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000211 ("cfunction" 1 "@code{\\1}")
212 ("chapter" 1 (format "@node \\1\n@%s \\1\n"
213 (if appendix "appendix" "chapter")))
214 ("chapter*" 1 "@node \\1\n@unnumbered \\1\n")
215 ("character" 1 "@samp{\\1}")
216 ("citetitle" 1 "@ref{Top,,,\\1}")
217 ("class" 1 "@code{\\1}")
Fred Drake66abcee2002-11-13 19:31:04 +0000218 ("cmemberline" 3 "@itemx \\2 \\3\n")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000219 ("code" 1 "@code{\\1}")
220 ("command" 1 "@command{\\1}")
221 ("constant" 1 "@code{\\1}")
222 ("copyright" 1 "@copyright{}")
223 ("Cpp" 0 "C++")
Fred Draked805fef2002-06-27 18:38:06 +0000224 ("csimplemacro" 1 "@code{\\1}")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000225 ("ctype" 1 "@code{\\1}")
226 ("dataline" 1 (progn (setq findex t) "@item \\1\n@findex \\1\n"))
227 ("date" 1 "\\1")
228 ("declaremodule" 2 (progn (setq cindex t) "@label{\\2}@cindex{\\2}"))
229 ("deprecated" 2 "@emph{This is deprecated in Python \\1. \\2}")
230 ("dfn" 1 "@dfn{\\1}")
231 ("documentclass" 1 py2texi-magic)
232 ("e" 0 "@backslash{}")
233 ("else" 0 (concat "@end ifinfo\n@" (setq last-if "iftex")))
234 ("EOF" 0 "@code{EOF}")
235 ("email" 1 "@email{\\1}")
236 ("emph" 1 "@emph{\\1}")
237 ("envvar" 1 "@samp{\\1}")
238 ("exception" 1 "@code{\\1}")
239 ("exindex" 1 (progn (setq obindex t) "@obindex{\\1}"))
240 ("fi" 0 (concat "@end " last-if))
241 ("file" 1 "@file{\\1}")
242 ("filevar" 1 "@file{@var{\\1}}")
243 ("footnote" 1 "@footnote{\\1}")
244 ("frac" 0 "")
245 ("funcline" 2 (progn (setq findex t) "@item \\1 \\2\n@findex \\1"))
246 ("funclineni" 2 "@item \\1 \\2")
247 ("function" 1 "@code{\\1}")
248 ("grammartoken" 1 "@code{\\1}")
249 ("hline" 0 "")
250 ("ifhtml" 0 (concat "@" (setq last-if "ifinfo")))
251 ("iftexi" 0 (concat "@" (setq last-if "ifinfo")))
252 ("index" 1 (progn (setq cindex t) "@cindex{\\1}"))
253 ("indexii" 2 (progn (setq cindex t) "@cindex{\\1 \\2}"))
254 ("indexiii" 3 (progn (setq cindex t) "@cindex{\\1 \\2 \\3}"))
255 ("indexiv" 3 (progn (setq cindex t) "@cindex{\\1 \\2 \\3 \\4}"))
256 ("infinity" 0 "@emph{infinity}")
257 ("it" 0 "@destroy")
258 ("kbd" 1 "@key{\\1}")
259 ("keyword" 1 "@code{\\1}")
260 ("kwindex" 1 (progn (setq cindex t) "@cindex{\\1}"))
261 ("label" 1 "@label{\\1}")
262 ("Large" 0 "")
263 ("LaTeX" 0 "La@TeX{}")
264 ("large" 0 "")
265 ("ldots" 0 "@dots{}")
266 ("leftline" 1 "\\1")
267 ("lineii" 2 "@item \\1 @tab \\2")
268 ("lineiii" 3 "@item \\1 @tab \\2 @tab \\3")
269 ("lineiv" 4 "@item \\1 @tab \\2 @tab \\3 @tab \\4")
270 ("linev" 5 "@item \\1 @tab \\2 @tab \\3 @tab \\4 @tab \\5")
271 ("localmoduletable" 0 "")
272 ("longprogramopt" 1 "@option{--\\1}")
273 ("mailheader" 1 "@code{\\1}")
274 ("makeindex" 0 "")
275 ("makemodindex" 0 "")
276 ("maketitle" 0 (concat "@top " title "\n"))
277 ("makevar" 1 "@code{\\1}")
278 ("manpage" 2 "@samp{\\1(\\2)}")
279 ("mbox" 1 "@w{\\1}")
280 ("member" 1 "@code{\\1}")
281 ("memberline" 1 "@item \\1\n@findex \\1\n")
282 ("menuselection" 1 "@samp{\\1}")
283 ("method" 1 "@code{\\1}")
284 ("methodline" 2 (progn (setq moindex t) "@item \\1(\\2)\n@moindex \\1\n"))
285 ("methodlineni" 2 "@item \\1(\\2)\n")
286 ("mimetype" 1 "@samp{\\1}")
287 ("module" 1 "@samp{\\1}")
Fred Drake66abcee2002-11-13 19:31:04 +0000288 ("moduleauthor" 2 "")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000289 ("modulesynopsis" 1 "\\1")
290 ("moreargs" 0 "@dots{}")
291 ("n" 0 "@backslash{}n")
292 ("newcommand" 2 "")
293 ("newsgroup" 1 "@samp{\\1}")
294 ("nodename" 1
295 (save-excursion
296 (save-match-data
297 (re-search-backward "^@node "))
298 (delete-region (point) (save-excursion (end-of-line) (point)))
299 (insert "@node " (match-string 1 string))
300 ""))
301 ("noindent" 0 "@noindent ")
302 ("note" 1 "@emph{Note:} \\1")
303 ("NULL" 0 "@code{NULL}")
304 ("obindex" 1 (progn (setq obindex t) "@obindex{\\1}"))
305 ("opindex" 1 (progn (setq cindex t) "@cindex{\\1}"))
306 ("option" 1 "@option{\\1}")
307 ("optional" 1 "[\\1]")
308 ("pep" 1 (progn (setq cindex t) "PEP@ \\1@cindex PEP \\1\n"))
309 ("pi" 0 "pi")
310 ("platform" 1 "")
311 ("plusminus" 0 "+-")
312 ("POSIX" 0 "POSIX")
313 ("production" 2 "@item \\1 \\2")
Fred Drake66abcee2002-11-13 19:31:04 +0000314 ("productioncont" 1 "@item @w{} \\1")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000315 ("program" 1 "@command{\\1}")
316 ("programopt" 1 "@option{\\1}")
317 ("protect" 0 "")
318 ("pytype" 1 "@code{\\1}")
319 ("ref" 1 "@ref{\\1}")
320 ("refbimodindex" 1 (progn (setq moindex t) "@moindex{\\1}"))
321 ("refmodindex" 1 (progn (setq moindex t) "@moindex{\\1}"))
322 ("refmodule" 1 "@samp{\\1}")
323 ("refstmodindex" 1 (progn (setq moindex t) "@moindex{\\1}"))
324 ("regexp" 1 "\"\\1\"")
325 ("release" 1
326 (progn (setq py2texi-python-version (match-string 1 string)) ""))
327 ("renewcommand" 2 "")
328 ("rfc" 1 (progn (setq cindex t) "RFC@ \\1@cindex RFC \\1\n"))
329 ("rm" 0 "@destroy")
330 ("samp" 1 "@samp{\\1}")
331 ("section" 1 (let ((str (match-string 1 string)))
332 (save-match-data
333 (if (string-match "\\(.*\\)[ \t\n]*---[ \t\n]*\\(.*\\)"
334 str)
335 (format
336 "@node %s\n@section %s\n"
337 (py2texi-backslash-quote (match-string 1 str))
338 (py2texi-backslash-quote (match-string 2 str)))
339 "@node \\1\n@section \\1\n"))))
Fred Drake66abcee2002-11-13 19:31:04 +0000340 ("sectionauthor" 2 "")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000341 ("seemodule" 2 "@ref{\\1} \\2")
342 ("seepep" 3 "\n@table @strong\n@item PEP\\1 \\2\n\\3\n@end table\n")
343 ("seerfc" 3 "\n@table @strong\n@item RFC\\1 \\2\n\\3\n@end table\n")
344 ("seetext" 1 "\\1")
345 ("seetitle" 1 "@cite{\\1}")
346 ("seeurl" 2 "\n@table @url\n@item \\1\n\\2\n@end table\n")
347 ("setindexsubitem" 1 (progn (setq cindex t) "@cindex \\1"))
348 ("setreleaseinfo" 1 (progn (setq py2texi-releaseinfo "")))
349 ("setshortversion" 1
350 (progn (setq py2texi-python-short-version (match-string 1 string)) ""))
351 ("shortversion" 0 py2texi-python-short-version)
352 ("sqrt" 0 "")
353 ("stindex" 1 (progn (setq cindex t) "@cindex{\\1}"))
354 ("stmodindex" 1 (progn (setq moindex t) "@moindex{\\1}"))
355 ("strong" 1 "@strong{\\1}")
356 ("sub" 0 "/")
357 ("subsection" 1 "@node \\1\n@subsection \\1\n")
358 ("subsubsection" 1 "@node \\1\n@subsubsection \\1\n")
359 ("sum" 0 "")
360 ("tableofcontents" 0 "")
361 ("term" 1 "@item \\1")
362 ("textasciitilde" 0 "~")
363 ("textasciicircum" 0 "^")
364 ("textbackslash" 0 "@backslash{}")
Fred Draked805fef2002-06-27 18:38:06 +0000365 ("textgreater" 0 ">")
366 ("textless" 0 "<")
Fred Drakebfc18bd2002-05-03 04:50:51 +0000367 ("textrm" 1 "\\1")
368 ("texttt" 1 "@code{\\1}")
369 ("textunderscore" 0 "_")
370 ("title" 1 (progn (setq title (match-string 1 string)) "@settitle \\1"))
371 ("today" 0 "@today{}")
372 ("token" 1 "@code{\\1}")
373 ("tt" 0 "@destroy")
374 ("ttindex" 1 (progn (setq cindex t) "@cindex{\\1}"))
375 ("u" 0 "@backslash{}u")
376 ("ulink" 2 "\\1")
377 ("UNIX" 0 "UNIX")
378 ("unspecified" 0 "@dots{}")
379 ("url" 1 "@url{\\1}")
380 ("usepackage" 1 "")
381 ("var" 1 "@var{\\1}")
382 ("verbatiminput" 1 "@code{\\1}")
383 ("version" 0 py2texi-python-version)
384 ("versionadded" 1 "@emph{Added in Python version \\1}")
385 ("versionchanged" 1 "@emph{Changed in Python version \\1}")
386 ("vskip" 1 "")
387 ("vspace" 1 "")
388 ("warning" 1 "@emph{\\1}")
389 ("withsubitem" 2 "\\2")
390 ("XXX" 1 "@strong{\\1}"))
391 "Associative list of command substitutions.
392Each list item is of the form (COMMAND ARGNUM SUBSTITUTION) where:
393- COMMAND is LaTeX command name
394- ARGNUM is number of (required) command arguments
395- SUBSTITUTION substitution for the command. It is evaled and you can
396 reference command arguments through the \\N regexp notation in strings.")
397
398(defvar py2texi-magic "@documentclass\n"
399 "\"Magic\" string for auxiliary insertion at the beginning of document.")
400
401(defvar py2texi-dirs '("./" "../texinputs/")
402 "Where to search LaTeX input files.")
403
404(defvar py2texi-buffer "*py2texi*"
405 "The name of a buffer where Texinfo is generated.")
406
407(defconst py2texi-xemacs (string-match "^XEmacs" (emacs-version))
408 "Running under XEmacs?")
409
410
411(defmacro py2texi-search (regexp &rest body)
412 `(progn
413 (goto-char (point-min))
414 (while (re-search-forward ,regexp nil t)
415 ,@body)))
416
417(defmacro py2texi-search-safe (regexp &rest body)
418 `(py2texi-search ,regexp
419 (unless (py2texi-protected)
420 ,@body)))
421
422
423(defun py2texi-message (message)
424 "Report message and stop if `py2texi-stop-on-problems' is non-nil."
425 (if py2texi-stop-on-problems
426 (error message)
427 (message message)))
428
429
430(defun py2texi-backslash-quote (string)
431 "Double backslahes in STRING."
432 (let ((i 0))
433 (save-match-data
434 (while (setq i (string-match "\\\\" string i))
435 (setq string (replace-match "\\\\\\\\" t nil string))
436 (setq i (+ i 2))))
437 string))
438
439
440(defun py2texi (file)
441 "Convert Python LaTeX documentation FILE to Texinfo."
442 (interactive "fFile to convert: ")
443 (switch-to-buffer (get-buffer-create py2texi-buffer))
444 (erase-buffer)
445 (insert-file file)
446 (let ((case-fold-search nil)
447 (title "")
448 (author "")
449 (author-address "")
450 (appendix nil)
451 (findex nil)
452 (obindex nil)
453 (cindex nil)
454 (moindex nil)
455 last-if)
456 (py2texi-process-verbatims)
457 (py2texi-process-comments)
458 (py2texi-process-includes)
459 (py2texi-process-funnyas)
460 (py2texi-process-environments)
461 (py2texi-process-commands)
462 (py2texi-fix-indentation)
463 (py2texi-fix-nodes)
464 (py2texi-fix-references)
465 (py2texi-fix-indices)
466 (py2texi-process-simple-commands)
467 (py2texi-fix-fonts)
468 (py2texi-fix-braces)
469 (py2texi-fix-backslashes)
470 (py2texi-destroy-empties)
471 (py2texi-fix-newlines)
472 (py2texi-adjust-level))
Fred Drake66abcee2002-11-13 19:31:04 +0000473 (let* ((texi-file-name (or py2texi-texi-file-name
474 (py2texi-texi-file-name file)))
475 (info-file-name (or py2texi-info-file-name
476 (py2texi-info-file-name texi-file-name))))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000477 (goto-char (point-min))
478 (when (looking-at py2texi-magic)
479 (delete-region (point) (progn (beginning-of-line 2) (point)))
480 (insert "\\input texinfo @c -*-texinfo-*-\n")
Fred Drake66abcee2002-11-13 19:31:04 +0000481 (insert "@setfilename " info-file-name))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000482 (when (re-search-forward "@chapter" nil t)
483 (texinfo-all-menus-update t))
484 (goto-char (point-min))
Fred Drake66abcee2002-11-13 19:31:04 +0000485 (write-file texi-file-name)
486 (message (format "You can apply `makeinfo %s' now." texi-file-name))))
487
488
489(defun py2texi-texi-file-name (filename)
490 "Generate name of Texinfo file from original file name FILENAME."
491 (concat filename
492 (if (string-match "\\.tex$" filename) "i" ".texi")))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000493
494
495(defun py2texi-info-file-name (filename)
496 "Generate name of info file from original file name FILENAME."
497 (setq filename (expand-file-name filename))
498 (let ((directory (file-name-directory filename))
499 (basename (file-name-nondirectory filename)))
500 (concat directory "python-"
501 (substring basename 0 (- (length basename) 4)) "info")))
502
503
504(defun py2texi-process-verbatims ()
505 "Process and protect verbatim environments."
506 (let (delimiter
507 beg
508 end)
509 (py2texi-search-safe "\\\\begin{\\(verbatim\\|displaymath\\)}"
510 (replace-match "@example")
511 (setq beg (copy-marker (point) nil))
512 (re-search-forward "\\\\end{\\(verbatim\\|displaymath\\)}")
513 (setq end (copy-marker (match-beginning 0) nil))
514 (replace-match "@end example")
515 (py2texi-texinfo-escape beg end)
516 (put-text-property (- beg (length "@example"))
517 (+ end (length "@end example"))
518 'py2texi-protected t))
519 (py2texi-search-safe "\\\\verb\\([^a-z]\\)"
520 (setq delimiter (match-string 1))
521 (replace-match "@code{")
522 (setq beg (copy-marker (point) nil))
523 (re-search-forward (regexp-quote delimiter))
524 (setq end (copy-marker (match-beginning 0) nil))
525 (replace-match "}")
526 (put-text-property (- beg (length "@code{")) (+ end (length "}"))
527 'py2texi-protected t)
528 (py2texi-texinfo-escape beg end))))
529
530
531(defun py2texi-process-comments ()
532 "Remove comments."
533 (let (point)
534 (py2texi-search-safe "%"
535 (setq point (point))
536 (when (save-excursion
537 (re-search-backward "\\(^\\|[^\\]\\(\\\\\\\\\\)*\\)%\\=" nil t))
538 (delete-region (1- point)
539 (save-excursion (beginning-of-line 2) (point)))))))
540
541
542(defun py2texi-process-includes ()
543 "Include LaTeX input files.
544Do not include .ind files."
545 (let ((path (file-name-directory file))
546 filename
547 dirs
548 includefile)
549 (py2texi-search-safe "\\\\input{\\([^}]+\\)}"
550 (setq filename (match-string 1))
551 (unless (save-match-data (string-match "\\.tex$" filename))
552 (setq filename (concat filename ".tex")))
553 (setq includefile (save-match-data
554 (string-match "\\.ind\\.tex$" filename)))
555 (setq dirs py2texi-dirs)
556 (while (and (not includefile) dirs)
557 (setq includefile (concat path (car dirs) filename))
558 (unless (file-exists-p includefile)
559 (setq includefile nil)
560 (setq dirs (cdr dirs))))
561 (if includefile
562 (save-restriction
563 (narrow-to-region (match-beginning 0) (match-end 0))
564 (delete-region (point-min) (point-max))
565 (when (stringp includefile)
566 (insert-file-contents includefile)
567 (goto-char (point-min))
568 (insert "\n")
569 (py2texi-process-verbatims)
570 (py2texi-process-comments)
571 (py2texi-process-includes)))
572 (replace-match (format "\\\\emph{Included file %s}" filename))
573 (py2texi-message (format "Input file %s not found" filename))))))
574
575
576(defun py2texi-process-funnyas ()
577 "Convert @s."
578 (py2texi-search-safe "@"
579 (replace-match "@@")))
580
581
582(defun py2texi-process-environments ()
583 "Process LaTeX environments."
584 (let ((stack ())
585 kind
586 environment
587 parameter
588 arguments
589 n
590 string
591 description)
592 (py2texi-search-safe (concat "\\\\\\(begin\\|end\\|item\\)"
593 "\\({\\([^}]*\\)}\\|[[]\\([^]]*\\)[]]\\|\\)")
594 (setq kind (match-string 1)
595 environment (match-string 3)
596 parameter (match-string 4))
597 (replace-match "")
598 (cond
599 ((string= kind "begin")
600 (setq description (assoc environment py2texi-environments))
601 (if description
602 (progn
603 (setq n (cadr description))
604 (setq description (cddr description))
605 (setq string (py2texi-tex-arguments n))
606 (string-match (py2texi-regexp n) string)
607 ; incorrect but sufficient
608 (insert (replace-match (eval (car description))
609 t nil string))
610 (setq stack (cons (cadr description) stack)))
611 (py2texi-message (format "Unknown environment: %s" environment))
612 (setq stack (cons "" stack))))
613 ((string= kind "end")
614 (insert (eval (car stack)))
615 (setq stack (cdr stack)))
616 ((string= kind "item")
617 (insert "\n@item " (or parameter "") "\n"))))
618 (when stack
619 (py2texi-message (format "Unclosed environment: %s" (car stack))))))
620
621
622(defun py2texi-process-commands ()
623 "Process LaTeX commands."
624 (let (done
625 command
626 command-info
627 string
628 n)
629 (while (not done)
630 (setq done t)
631 (py2texi-search-safe "\\\\\\([a-zA-Z*]+\\)\\(\\[[^]]*\\]\\)?"
632 (setq command (match-string 1))
633 (setq command-info (assoc command py2texi-commands))
634 (if command-info
635 (progn
636 (setq done nil)
637 (replace-match "")
638 (setq command-info (cdr command-info))
639 (setq n (car command-info))
640 (setq string (py2texi-tex-arguments n))
641 (string-match (py2texi-regexp n) string)
642 ; incorrect but sufficient
643 (insert (replace-match (eval (cadr command-info))
644 t nil string)))
645 (py2texi-message (format "Unknown command: %s (not processed)"
646 command)))))))
647
648
649(defun py2texi-argument-pattern (count)
650 (let ((filler "\\(?:[^{}]\\|\\\\{\\|\\\\}\\)*"))
651 (if (<= count 0)
652 filler
653 (concat filler "\\(?:{"
654 (py2texi-argument-pattern (1- count))
655 "}" filler "\\)*" filler))))
656(defconst py2texi-tex-argument
657 (concat
658 "{\\("
659 (py2texi-argument-pattern 10) ;really at least 10!
660 "\\)}[ \t%@c\n]*")
661 "Regexp describing LaTeX command argument including argument separators.")
662
663
664(defun py2texi-regexp (n)
665 "Make regexp matching N LaTeX command arguments."
666 (if (= n 0)
667 ""
668 (let ((regexp "^[^{]*"))
669 (while (> n 0)
670 (setq regexp (concat regexp py2texi-tex-argument))
671 (setq n (1- n)))
672 regexp)))
673
674
675(defun py2texi-tex-arguments (n)
676 "Remove N LaTeX command arguments and return them as a string."
677 (let ((point (point))
678 (i 0)
679 result
680 match)
681 (if (= n 0)
682 (progn
683 (when (re-search-forward "\\=\\({}\\| *\\)" nil t)
684 (replace-match ""))
685 "")
686 (while (> n 0)
687 (unless (re-search-forward
688 "\\(\\=\\|[^\\\\]\\)\\(\\\\\\\\\\)*\\([{}]\\)" nil t)
689 (debug))
690 (if (string= (match-string 3) "{")
691 (setq i (1+ i))
692 (setq i (1- i))
693 (when (<= i 0)
694 (setq n (1- n)))))
695 (setq result (buffer-substring-no-properties point (point)))
696 (while (string-match "\n[ \t]*" result)
697 (setq result (replace-match " " t nil result)))
698 (delete-region point (point))
699 result)))
700
701
702(defun py2texi-process-simple-commands ()
703 "Replace single character LaTeX commands."
704 (let (char)
705 (py2texi-search-safe "\\\\\\([^a-z]\\)"
706 (setq char (match-string 1))
707 (replace-match (format "%s%s"
708 (if (or (string= char "{")
709 (string= char "}")
710 (string= char " "))
711 "@"
712 "")
713 (if (string= char "\\")
714 "\\\\"
715 char))))))
716
717
718(defun py2texi-fix-indentation ()
719 "Remove white space at the beginning of lines."
720 (py2texi-search-safe "^[ \t]+"
721 (replace-match "")))
722
723
724(defun py2texi-fix-nodes ()
725 "Remove unwanted characters from nodes and make nodes unique."
726 (let ((nodes (make-hash-table :test 'equal))
727 id
728 counter
729 string
Fred Drake66abcee2002-11-13 19:31:04 +0000730 label
731 index)
Fred Drakebfc18bd2002-05-03 04:50:51 +0000732 (py2texi-search "^@node +\\(.*\\)$"
733 (setq string (match-string 1))
734 (if py2texi-xemacs
735 (replace-match "@node " t)
736 (replace-match "" t nil nil 1))
Fred Drake66abcee2002-11-13 19:31:04 +0000737 (while (string-match "@label{[^}]*}" string)
Fred Drakebfc18bd2002-05-03 04:50:51 +0000738 (setq label (match-string 0 string))
739 (setq string (replace-match "" t nil string)))
Fred Drake66abcee2002-11-13 19:31:04 +0000740 (while (string-match "@..?index{[^}]*}" string)
741 (setq index (match-string 0 string))
742 (setq string (replace-match "" t nil string)))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000743 (while (string-match "@[a-zA-Z]+\\|[{}():]\\|``\\|''" string)
744 (setq string (replace-match "" t nil string)))
745 (while (string-match " -- " string)
746 (setq string (replace-match " - " t nil string)))
Fred Drake66abcee2002-11-13 19:31:04 +0000747 (while (string-match "\\." string)
748 (setq string (replace-match "" t nil string)))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000749 (when (string-match " +$" string)
750 (setq string (replace-match "" t nil string)))
751 (when (string-match "^\\(Built-in\\|Standard\\) Module \\|The " string)
752 (setq string (replace-match "" t nil string)))
753 (string-match "^[^,]+" string)
754 (setq id (match-string 0 string))
755 (setq counter (gethash id nodes))
756 (if counter
757 (progn
758 (setq counter (1+ counter))
759 (setq string (replace-match (format "\\& %d" counter)
760 t nil string)))
761 (setq counter 1))
762 (setf (gethash id nodes) counter)
763 (insert string)
Fred Drake66abcee2002-11-13 19:31:04 +0000764 (beginning-of-line 3)
Fred Drakebfc18bd2002-05-03 04:50:51 +0000765 (when label
Fred Drake66abcee2002-11-13 19:31:04 +0000766 (insert label "\n"))
767 (when index
768 (insert index "\n")))))
Fred Drakebfc18bd2002-05-03 04:50:51 +0000769
770
771(defun py2texi-fix-references ()
772 "Process labels and make references to point to appropriate nodes."
773 (let ((labels ())
774 node)
775 (py2texi-search-safe "@label{\\([^}]*\\)}"
776 (setq node (save-excursion
777 (save-match-data
778 (and (re-search-backward "@node +\\([^,\n]+\\)" nil t)
779 (match-string 1)))))
780 (when node
781 (setq labels (cons (cons (match-string 1) node) labels)))
782 (replace-match ""))
783 (py2texi-search-safe "@ref{\\([^}]*\\)}"
784 (setq node (assoc (match-string 1) labels))
785 (replace-match "")
786 (when node
787 (insert (format "@ref{%s}" (cdr node)))))))
788
789
790(defun py2texi-fix-indices ()
791 "Remove unwanted characters from @*index commands and create final indices."
792 (py2texi-search-safe "@..?index\\>[^\n]*\\(\\)\n"
793 (replace-match "" t nil nil 1))
794 (py2texi-search-safe "@..?index\\>[^\n]*\\(\\)"
795 (replace-match "\n" t nil nil 1))
796 (py2texi-search-safe "@..?index\\({\\)\\([^}]+\\)\\(}+\\)"
797 (replace-match " " t nil nil 1)
798 (replace-match "" t nil nil 3)
799 (let ((string (match-string 2)))
800 (save-match-data
801 (while (string-match "@[a-z]+{" string)
802 (setq string (replace-match "" nil nil string)))
803 (while (string-match "{" string)
804 (setq string (replace-match "" nil nil string))))
805 (replace-match string t t nil 2)))
806 (py2texi-search-safe "@..?index\\>.*\\([{}]\\|@[a-z]*\\)"
807 (replace-match "" t nil nil 1)
808 (goto-char (match-beginning 0)))
809 (py2texi-search-safe "[^\n]\\(\\)@..?index\\>"
810 (replace-match "\n" t nil nil 1))
811 (goto-char (point-max))
812 (re-search-backward "@indices")
813 (replace-match "")
814 (insert (if moindex
815 (concat "@node Module Index\n"
816 "@unnumbered Module Index\n"
817 "@printindex mo\n")
818 "")
819 (if obindex
820 (concat "@node Class-Exception-Object Index\n"
821 "@unnumbered Class, Exception, and Object Index\n"
822 "@printindex ob\n")
823 "")
824 (if findex
825 (concat "@node Function-Method-Variable Index\n"
826 "@unnumbered Function, Method, and Variable Index\n"
827 "@printindex fn\n")
828 "")
829 (if cindex
830 (concat "@node Miscellaneous Index\n"
831 "@unnumbered Miscellaneous Index\n"
832 "@printindex cp\n")
833 "")))
834
835
836(defun py2texi-fix-backslashes ()
837 "Make backslashes from auxiliary commands."
838 (py2texi-search-safe "@backslash{}"
839 (replace-match "\\\\")))
840
841
842(defun py2texi-fix-fonts ()
843 "Remove garbage after unstructured font commands."
844 (let (string)
845 (py2texi-search-safe "@destroy"
846 (replace-match "")
847 (when (eq (preceding-char) ?{)
848 (forward-char -1)
849 (setq string (py2texi-tex-arguments 1))
850 (insert (substring string 1 (1- (length string))))))))
851
852
853(defun py2texi-fix-braces ()
854 "Escape braces for Texinfo."
855 (let (string)
856 (py2texi-search "{"
857 (unless (or (py2texi-protected)
858 (save-excursion
859 (re-search-backward
860 "@\\([a-zA-Z]*\\|multitable.*\\){\\=" nil t)))
861 (forward-char -1)
862 (setq string (py2texi-tex-arguments 1))
863 (insert "@" (substring string 0 (1- (length string))) "@}")))))
864
865
866(defun py2texi-fix-newlines ()
867 "Remove extra newlines."
868 (py2texi-search "\n\n\n+"
869 (replace-match "\n\n"))
870 (py2texi-search-safe "@item.*\n\n"
871 (delete-backward-char 1))
872 (py2texi-search "@end example"
873 (unless (looking-at "\n\n")
874 (insert "\n"))))
875
876
877(defun py2texi-destroy-empties ()
878 "Remove all comments.
879This avoids some makeinfo errors."
880 (py2texi-search "@c\\>"
881 (unless (eq (py2texi-protected) t)
882 (delete-region (- (point) 2) (save-excursion (end-of-line) (point)))
883 (cond
884 ((looking-at "\n\n")
885 (delete-char 1))
886 ((save-excursion (re-search-backward "^[ \t]*\\=" nil t))
887 (delete-region (save-excursion (beginning-of-line) (point))
888 (1+ (point))))))))
889
890
891(defun py2texi-adjust-level ()
892 "Increase heading level to @chapter, if needed.
893This is only needed for distutils, so it has a very simple form only."
894 (goto-char (point-min))
895 (unless (re-search-forward "@chapter\\>" nil t)
896 (py2texi-search-safe "@section\\>"
897 (replace-match "@chapter" t))
898 (py2texi-search-safe "@\\(sub\\)\\(sub\\)?section\\>"
899 (replace-match "" nil nil nil 1))))
900
901
902(defun py2texi-texinfo-escape (beg end)
903 "Escape Texinfo special characters in region."
904 (save-excursion
905 (goto-char beg)
906 (while (re-search-forward "[@{}]" end t)
907 (replace-match "@\\&"))))
908
909
910(defun py2texi-protected ()
911 "Return protection status of the point before current point."
912 (get-text-property (1- (point)) 'py2texi-protected))
913
914
915;;; Announce
916
917(provide 'py2texi)
918
919
920;;; py2texi.el ends here