1 ;;;; (texinfo plain-text) -- rendering stexinfo as plain text
3 ;;;; Copyright (C) 2009, 2010, 2011, 2013 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 #:use-module (ice-9 match)
35 #:export (stexi->plain-text))
37 ;; The return value is a string.
38 (define (arg-ref key %-args)
39 (and=> (and=> (assq key (cdr %-args)) cdr)
41 (define (arg-req key %-args)
42 (or (arg-ref key %-args)
43 (error "Missing argument:" key %-args)))
45 (define (make-ticker str)
47 (define (make-enumerator n)
51 (format #f "~A. " last))))
53 (define *indent* (make-fluid ""))
54 (define *itemizer* (make-fluid (make-ticker "* ")))
56 (define-macro (with-indent n . body)
57 `(with-fluids ((*indent* (string-append (fluid-ref *indent*)
58 (make-string ,n #\space))))
61 (define (make-indenter n proc)
62 (lambda args (with-indent n (apply proc args))))
64 (define (string-indent str)
65 (string-append (fluid-ref *indent*) str "\n"))
67 (define-macro (with-itemizer itemizer . body)
68 `(with-fluids ((*itemizer* ,itemizer))
71 (define (wrap* . strings)
72 (let ((indent (fluid-ref *indent*)))
73 (fill-string (string-concatenate strings)
74 #:line-width 72 #:initial-indent indent
75 #:subsequent-indent indent)))
76 (define (wrap . strings)
77 (string-append (apply wrap* strings) "\n\n"))
78 (define (wrap-heading . strings)
79 (string-append (apply wrap* strings) "\n"))
81 (define (ref tag args)
82 (let* ((node (arg-req 'node args))
83 (name (or (arg-ref 'name args) node))
84 (manual (arg-ref 'manual args)))
87 (or (and=> (assq tag '((xref "See ") (pxref "see "))) cadr) "")
89 (if manual `(" in manual " ,manual) '())))))
91 (define (uref tag args)
92 (let ((url (arg-req 'url args))
93 (title (arg-ref 'title args)))
95 (string-append title " (" url ")")
96 (string-append "`" url "'"))))
98 (define (def tag args . body)
102 (map (lambda (x) (arg-ref x args))
103 '(data-type class name arguments)))
106 (let* ((category (case tag
108 ((defspec) "Special Form")
109 ((defvar) "Variable")
110 (else (arg-req 'category args)))))
112 (wrap-heading (string-append " - " category ": " (first-line)))
113 (with-indent 5 (stexi->plain-text body)))))
115 (define (enumerate tag . elts)
116 (define (tonumber start)
117 (let ((c (string-ref start 0)))
118 (cond ((number? c) (string->number start))
119 (else (1+ (- (char->integer c)
120 (char->integer (if (char-upper-case? c) #\A #\a))))))))
121 (let* ((args? (and (pair? elts) (pair? (car elts))
122 (eq? (caar elts) '%)))
123 (start (and args? (arg-ref 'start (car elts)))))
124 (with-itemizer (make-enumerator (if start (tonumber start) 1))
126 (stexi->plain-text (if start (cdr elts) elts))))))
128 (define (itemize tag args . elts)
129 (with-itemizer (make-ticker "* ")
131 (stexi->plain-text elts))))
133 (define (item tag . elts)
134 (let* ((ret (stexi->plain-text elts))
135 (tick ((fluid-ref *itemizer*)))
136 (tick-pos (- (string-length (fluid-ref *indent*))
137 (string-length tick))))
138 (if (and (not (string-null? ret)) (not (negative? tick-pos)))
139 (string-copy! ret tick-pos tick))
142 (define (table tag args . body)
143 (stexi->plain-text body))
145 (define (entry tag args . body)
146 (let ((heading (wrap-heading
147 (stexi->plain-text (arg-req 'heading args)))))
148 (string-append heading
149 (with-indent 5 (stexi->plain-text body)))))
151 (define (make-underliner char)
153 (let ((str (stexi->plain-text body)))
157 (string-indent (make-string (string-length str) char))
160 (define chapter (make-underliner #\*))
161 (define section (make-underliner #\=))
162 (define subsection (make-underliner #\-))
163 (define subsubsection (make-underliner #\.))
165 (define (example tag . body)
166 (let ((ret (stexi->plain-text body)))
169 (with-indent 5 (map string-indent (string-split ret #\newline))))
172 (define (verbatim tag . body)
173 (let ((ret (stexi->plain-text body)))
176 (map string-indent (string-split ret #\newline)))
179 (define (fragment tag . body)
180 (string-concatenate (map-in-order stexi->plain-text body)))
182 (define (para tag . body)
183 (wrap (stexi->plain-text body)))
185 (define (make-surrounder str)
187 (string-append str (stexi->plain-text body) str)))
189 (define (code tag . body)
190 (string-append "`" (stexi->plain-text body) "'"))
192 (define (key tag . body)
193 (string-append "<" (stexi->plain-text body) ">"))
195 (define (var tag . body)
196 (string-upcase (stexi->plain-text body)))
198 (define (passthrough tag . body)
199 (stexi->plain-text body))
201 (define (texinfo tag args . body)
202 (let ((title (chapter 'foo (arg-req 'title args))))
203 (string-append title (stexi->plain-text body))))
206 '(page setfilename setchapternewpage iftex ifinfo ifplaintext ifxml sp vskip
207 menu ignore syncodeindex comment c % node anchor))
208 (define (ignored? tag)
209 (memq tag ignore-list))
215 (subsection ,subsection)
216 (subsubsection ,subsubsection)
218 (appendixsec ,section)
219 (appendixsubsec ,subsection)
220 (appendixsubsubsec ,subsubsection)
221 (unnumbered ,chapter)
222 (unnumberedsec ,section)
223 (unnumberedsubsec ,subsection)
224 (unnumberedsubsubsec ,subsubsection)
225 (majorheading ,chapter)
226 (chapheading ,chapter)
228 (subheading ,subsection)
229 (subsubheading ,subsubsection)
231 (strong ,(make-surrounder "*"))
244 (dfn ,(make-surrounder "\""))
245 (cite ,(make-surrounder "\""))
248 (emph ,(make-surrounder "_"))
250 (copyright ,(lambda args "(C)"))
251 (result ,(lambda args "==>"))
252 (dots ,(lambda args "..."))
259 (quotation ,(make-indenter 5 para))
261 (enumerate ,enumerate)
267 (smallexample ,example)
270 (*fragment* ,fragment)
292 (define (stexi->plain-text tree)
293 "Transform @var{tree} into plain text. Returns a string."
297 (((? symbol? tag) body ...)
298 (let ((handler (and (not (ignored? tag))
299 (or (and=> (assq tag tag-handlers) cadr)
305 (string-concatenate (map-in-order stexi->plain-text tree)))
308 ;;; arch-tag: f966c3f6-3b46-4790-bbf9-3ad27e4917c2