1 ;;;; (texinfo plain-text) -- rendering stexinfo as plain text
3 ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;;;; Copyright (C) 2003,2004,2009 Andy Wingo <wingo at pobox dot com>
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 ;;Transformation from stexi to plain-text. Strives to re-create the
24 ;;output from @code{info}; comes pretty damn close.
28 (define-module (texinfo plain-text)
29 #:use-module (texinfo)
30 #:use-module (texinfo string-utils)
31 #:use-module (sxml transform)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-13)
34 #:export (stexi->plain-text))
36 ;; The return value is a string.
37 (define (arg-ref key %-args)
38 (and=> (and=> (assq key (cdr %-args)) cdr)
40 (define (arg-req key %-args)
41 (or (arg-ref key %-args)
42 (error "Missing argument:" key %-args)))
44 (define *indent* (make-fluid))
45 (define *itemizer* (make-fluid))
47 (define (make-ticker str)
49 (define (make-enumerator n)
53 (format #f "~A. " last))))
55 (fluid-set! *indent* "")
56 ;; Shouldn't be necessary to do this, but just in case.
57 (fluid-set! *itemizer* (make-ticker "* "))
59 (define-macro (with-indent n . body)
60 `(with-fluids ((*indent* (string-append (fluid-ref *indent*)
61 (make-string ,n #\space))))
64 (define (make-indenter n proc)
65 (lambda args (with-indent n (apply proc args))))
67 (define (string-indent str)
68 (string-append (fluid-ref *indent*) str "\n"))
70 (define-macro (with-itemizer itemizer . body)
71 `(with-fluids ((*itemizer* ,itemizer))
74 (define (wrap* . strings)
75 (let ((indent (fluid-ref *indent*)))
76 (fill-string (string-concatenate strings)
77 #:line-width 72 #:initial-indent indent
78 #:subsequent-indent indent)))
79 (define (wrap . strings)
80 (string-append (apply wrap* strings) "\n\n"))
81 (define (wrap-heading . strings)
82 (string-append (apply wrap* strings) "\n"))
84 (define (ref tag args)
85 (let* ((node (arg-req 'node args))
86 (name (or (arg-ref 'name args) node))
87 (manual (arg-ref 'manual args)))
90 (or (and=> (assq tag '((xref "See ") (pxref "see "))) cadr) "")
92 (if manual `(" in manual " ,manual) '())))))
94 (define (uref tag args)
95 (let ((url (arg-req 'url args))
96 (title (arg-ref 'title args)))
98 (string-append title " (" url ")")
99 (string-append "`" url "'"))))
101 (define (def tag args . body)
102 (define (list/spaces . elts)
103 (let lp ((in elts) (out '()))
104 (cond ((null? in) (reverse! out))
105 ((null? (car in)) (lp (cdr in) out))
108 (if (null? out) out (cons " " out))))))))
112 (map (lambda (x) (arg-ref x args))
113 '(data-type class name arguments)))
116 (let* ((category (case tag
118 ((defspec) "Special Form")
119 ((defvar) "Variable")
120 (else (arg-req 'category args)))))
122 (wrap-heading (string-append " - " category ": " (first-line)))
123 (with-indent 5 (stexi->plain-text body)))))
125 (define (enumerate tag . elts)
126 (define (tonumber start)
127 (let ((c (string-ref start 0)))
128 (cond ((number? c) (string->number start))
129 (else (1+ (- (char->integer c)
130 (char->integer (if (char-upper-case? c) #\A #\a))))))))
131 (let* ((args? (and (pair? elts) (pair? (car elts))
132 (eq? (caar elts) '%)))
133 (start (and args? (arg-ref 'start (car elts)))))
134 (with-itemizer (make-enumerator (if start (tonumber start) 1))
136 (stexi->plain-text (if start (cdr elts) elts))))))
138 (define (itemize tag args . elts)
139 (with-itemizer (make-ticker "* ")
141 (stexi->plain-text elts))))
143 (define (item tag . elts)
144 (let* ((ret (stexi->plain-text elts))
145 (tick ((fluid-ref *itemizer*)))
146 (tick-pos (- (string-length (fluid-ref *indent*))
147 (string-length tick))))
148 (if (and (not (string-null? ret)) (not (negative? tick-pos)))
149 (string-copy! ret tick-pos tick))
152 (define (table tag args . body)
153 (stexi->plain-text body))
155 (define (entry tag args . body)
156 (let ((heading (wrap-heading
157 (stexi->plain-text (arg-req 'heading args)))))
158 (string-append heading
159 (with-indent 5 (stexi->plain-text body)))))
161 (define (make-underliner char)
163 (let ((str (stexi->plain-text body)))
167 (string-indent (make-string (string-length str) char))
170 (define chapter (make-underliner #\*))
171 (define section (make-underliner #\=))
172 (define subsection (make-underliner #\-))
173 (define subsubsection (make-underliner #\.))
175 (define (example tag . body)
176 (let ((ret (stexi->plain-text body)))
179 (with-indent 5 (map string-indent (string-split ret #\newline))))
182 (define (verbatim tag . body)
183 (let ((ret (stexi->plain-text body)))
186 (map string-indent (string-split ret #\newline)))
189 (define (fragment tag . body)
190 (string-concatenate (map-in-order stexi->plain-text body)))
192 (define (para tag . body)
193 (wrap (stexi->plain-text body)))
195 (define (make-surrounder str)
197 (string-append str (stexi->plain-text body) str)))
199 (define (code tag . body)
200 (string-append "`" (stexi->plain-text body) "'"))
202 (define (key tag . body)
203 (string-append "<" (stexi->plain-text body) ">"))
205 (define (var tag . body)
206 (string-upcase (stexi->plain-text body)))
208 (define (passthrough tag . body)
209 (stexi->plain-text body))
211 (define (texinfo tag args . body)
212 (let ((title (chapter 'foo (arg-req 'title args))))
213 (string-append title (stexi->plain-text body))))
216 '(page setfilename setchapternewpage iftex ifinfo ifplaintext ifxml sp vskip
217 menu ignore syncodeindex comment c % node anchor))
218 (define (ignored? tag)
219 (memq tag ignore-list))
225 (subsection ,subsection)
226 (subsubsection ,subsubsection)
228 (appendixsec ,section)
229 (appendixsubsec ,subsection)
230 (appendixsubsubsec ,subsubsection)
231 (unnumbered ,chapter)
232 (unnumberedsec ,section)
233 (unnumberedsubsec ,subsection)
234 (unnumberedsubsubsec ,subsubsection)
235 (majorheading ,chapter)
236 (chapheading ,chapter)
238 (subheading ,subsection)
239 (subsubheading ,subsubsection)
241 (strong ,(make-surrounder "*"))
253 (dfn ,(make-surrounder "\""))
254 (cite ,(make-surrounder "\""))
257 (emph ,(make-surrounder "_"))
259 (copyright ,(lambda args "(C)"))
260 (result ,(lambda args "==>"))
267 (quotation ,(make-indenter 5 para))
269 (enumerate ,enumerate)
275 (smallexample ,example)
278 (*fragment* ,fragment)
300 (define (stexi->plain-text tree)
301 "Transform @var{tree} into plain text. Returns a string."
304 ((string? tree) tree)
307 ((symbol? (car tree))
308 (let ((handler (and (not (ignored? (car tree)))
309 (or (and=> (assq (car tree) tag-handlers) cadr)
311 (if handler (apply handler tree) "")))
313 (string-concatenate (map-in-order stexi->plain-text tree)))))
316 ;;; arch-tag: f966c3f6-3b46-4790-bbf9-3ad27e4917c2