guile-tools is a scheme script that loads scheme modules
[bpt/guile.git] / module / scripts / snarf-guile-m4-docs.scm
1 ;;; snarf-guile-m4-docs --- Parse guile.m4 comments for texi documentation
2
3 ;; Copyright (C) 2002, 2006 Free Software Foundation, Inc.
4 ;;
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation; either version 2, or
8 ;; (at your option) any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this software; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;; Boston, MA 02110-1301 USA
19
20 ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
21
22 ;;; Commentary:
23
24 ;; Usage: snarf-guile-m4-docs FILE
25 ;;
26 ;; Grep FILE for comments preceding macro definitions, massage
27 ;; them into valid texi, and display to stdout. For each comment,
28 ;; lines preceding "^# Usage:" are discarded.
29 ;;
30 ;; TODO: Generalize.
31
32 ;;; Code:
33
34 (define-module (scripts snarf-guile-m4-docs)
35 :use-module (ice-9 rdelim)
36 :export (snarf-guile-m4-docs))
37
38 (define (display-texi lines)
39 (display "@deffn {Autoconf Macro}")
40 (for-each (lambda (line)
41 (display (cond ((and (>= (string-length line) 2)
42 (string=? "# " (substring line 0 2)))
43 (substring line 2))
44 ((string=? "#" (substring line 0 1))
45 (substring line 1))
46 (else line)))
47 (newline))
48 lines)
49 (display "@end deffn")
50 (newline) (newline))
51
52 (define (prefix? line sub)
53 (false-if-exception
54 (string=? sub (substring line 0 (string-length sub)))))
55
56 (define (massage-usage line)
57 (let loop ((line (string->list line)) (acc '()))
58 (if (null? line)
59 (list (list->string (reverse acc)))
60 (loop (cdr line)
61 (cons (case (car line)
62 ((#\( #\) #\,) #\space)
63 (else (car line)))
64 acc)))))
65
66 (define (snarf-guile-m4-docs . args)
67 (let* ((p (open-file (car args) "r"))
68 (next (lambda () (read-line p))))
69 (let loop ((line (next)) (acc #f))
70 (or (eof-object? line)
71 (cond ((prefix? line "# Usage:")
72 (loop (next) (massage-usage (substring line 8))))
73 ((prefix? line "AC_DEFUN")
74 (display-texi (reverse acc))
75 (loop (next) #f))
76 ((and acc (prefix? line "#"))
77 (loop (next) (cons line acc)))
78 (else
79 (loop (next) #f)))))))
80
81 (define main snarf-guile-m4-docs)
82
83 ;;; snarf-guile-m4-docs ends here