plain-text.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;;; (texinfo plain-text) -- rendering stexinfo as plain text
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  4. ;;;; Copyright (C) 2003,2004,2009 Andy Wingo <wingo at pobox dot com>
  5. ;;;;
  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.
  10. ;;;;
  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.
  15. ;;;;
  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
  19. ;;;;
  20. ;;; Commentary:
  21. ;;
  22. ;;Transformation from stexi to plain-text. Strives to re-create the
  23. ;;output from @code{info}; comes pretty damn close.
  24. ;;
  25. ;;; Code:
  26. (define-module (texinfo plain-text)
  27. #:use-module (texinfo)
  28. #:use-module (texinfo string-utils)
  29. #:use-module (sxml transform)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-13)
  32. #:export (stexi->plain-text))
  33. ;; The return value is a string.
  34. (define (arg-ref key %-args)
  35. (and=> (and=> (assq key (cdr %-args)) cdr)
  36. stexi->plain-text))
  37. (define (arg-req key %-args)
  38. (or (arg-ref key %-args)
  39. (error "Missing argument:" key %-args)))
  40. (define *indent* (make-fluid))
  41. (define *itemizer* (make-fluid))
  42. (define (make-ticker str)
  43. (lambda () str))
  44. (define (make-enumerator n)
  45. (lambda ()
  46. (let ((last n))
  47. (set! n (1+ n))
  48. (format #f "~A. " last))))
  49. (fluid-set! *indent* "")
  50. ;; Shouldn't be necessary to do this, but just in case.
  51. (fluid-set! *itemizer* (make-ticker "* "))
  52. (define-macro (with-indent n . body)
  53. `(with-fluids ((*indent* (string-append (fluid-ref *indent*)
  54. (make-string ,n #\space))))
  55. ,@body))
  56. (define (make-indenter n proc)
  57. (lambda args (with-indent n (apply proc args))))
  58. (define (string-indent str)
  59. (string-append (fluid-ref *indent*) str "\n"))
  60. (define-macro (with-itemizer itemizer . body)
  61. `(with-fluids ((*itemizer* ,itemizer))
  62. ,@body))
  63. (define (wrap* . strings)
  64. (let ((indent (fluid-ref *indent*)))
  65. (fill-string (string-concatenate strings)
  66. #:line-width 72 #:initial-indent indent
  67. #:subsequent-indent indent)))
  68. (define (wrap . strings)
  69. (string-append (apply wrap* strings) "\n\n"))
  70. (define (wrap-heading . strings)
  71. (string-append (apply wrap* strings) "\n"))
  72. (define (ref tag args)
  73. (let* ((node (arg-req 'node args))
  74. (name (or (arg-ref 'name args) node))
  75. (manual (arg-ref 'manual args)))
  76. (string-concatenate
  77. (cons*
  78. (or (and=> (assq tag '((xref "See ") (pxref "see "))) cadr) "")
  79. name
  80. (if manual `(" in manual " ,manual) '())))))
  81. (define (uref tag args)
  82. (let ((url (arg-req 'url args))
  83. (title (arg-ref 'title args)))
  84. (if title
  85. (string-append title " (" url ")")
  86. (string-append "`" url "'"))))
  87. (define (def tag args . body)
  88. (define (list/spaces . elts)
  89. (let lp ((in elts) (out '()))
  90. (cond ((null? in) (reverse! out))
  91. ((null? (car in)) (lp (cdr in) out))
  92. (else (lp (cdr in)
  93. (cons (car in)
  94. (if (null? out) out (cons " " out))))))))
  95. (define (first-line)
  96. (string-join
  97. (filter identity
  98. (map (lambda (x) (arg-ref x args))
  99. '(data-type class name arguments)))
  100. " "))
  101. (let* ((category (case tag
  102. ((defun) "Function")
  103. ((defspec) "Special Form")
  104. ((defvar) "Variable")
  105. (else (arg-req 'category args)))))
  106. (string-append
  107. (wrap-heading (string-append " - " category ": " (first-line)))
  108. (with-indent 5 (stexi->plain-text body)))))
  109. (define (enumerate tag . elts)
  110. (define (tonumber start)
  111. (let ((c (string-ref start 0)))
  112. (cond ((number? c) (string->number start))
  113. (else (1+ (- (char->integer c)
  114. (char->integer (if (char-upper-case? c) #\A #\a))))))))
  115. (let* ((args? (and (pair? elts) (pair? (car elts))
  116. (eq? (caar elts) '%)))
  117. (start (and args? (arg-ref 'start (car elts)))))
  118. (with-itemizer (make-enumerator (if start (tonumber start) 1))
  119. (with-indent 5
  120. (stexi->plain-text (if start (cdr elts) elts))))))
  121. (define (itemize tag args . elts)
  122. (with-itemizer (make-ticker "* ")
  123. (with-indent 5
  124. (stexi->plain-text elts))))
  125. (define (item tag . elts)
  126. (let* ((ret (stexi->plain-text elts))
  127. (tick ((fluid-ref *itemizer*)))
  128. (tick-pos (- (string-length (fluid-ref *indent*))
  129. (string-length tick))))
  130. (if (and (not (string-null? ret)) (not (negative? tick-pos)))
  131. (string-copy! ret tick-pos tick))
  132. ret))
  133. (define (table tag args . body)
  134. (stexi->plain-text body))
  135. (define (entry tag args . body)
  136. (let ((heading (wrap-heading
  137. (stexi->plain-text (arg-req 'heading args)))))
  138. (string-append heading
  139. (with-indent 5 (stexi->plain-text body)))))
  140. (define (make-underliner char)
  141. (lambda (tag . body)
  142. (let ((str (stexi->plain-text body)))
  143. (string-append
  144. "\n"
  145. (string-indent str)
  146. (string-indent (make-string (string-length str) char))
  147. "\n"))))
  148. (define chapter (make-underliner #\*))
  149. (define section (make-underliner #\=))
  150. (define subsection (make-underliner #\-))
  151. (define subsubsection (make-underliner #\.))
  152. (define (example tag . body)
  153. (let ((ret (stexi->plain-text body)))
  154. (string-append
  155. (string-concatenate
  156. (with-indent 5 (map string-indent (string-split ret #\newline))))
  157. "\n")))
  158. (define (verbatim tag . body)
  159. (let ((ret (stexi->plain-text body)))
  160. (string-append
  161. (string-concatenate
  162. (map string-indent (string-split ret #\newline)))
  163. "\n")))
  164. (define (fragment tag . body)
  165. (string-concatenate (map-in-order stexi->plain-text body)))
  166. (define (para tag . body)
  167. (wrap (stexi->plain-text body)))
  168. (define (make-surrounder str)
  169. (lambda (tag . body)
  170. (string-append str (stexi->plain-text body) str)))
  171. (define (code tag . body)
  172. (string-append "`" (stexi->plain-text body) "'"))
  173. (define (key tag . body)
  174. (string-append "<" (stexi->plain-text body) ">"))
  175. (define (var tag . body)
  176. (string-upcase (stexi->plain-text body)))
  177. (define (passthrough tag . body)
  178. (stexi->plain-text body))
  179. (define (texinfo tag args . body)
  180. (let ((title (chapter 'foo (arg-req 'title args))))
  181. (string-append title (stexi->plain-text body))))
  182. (define ignore-list
  183. '(page setfilename setchapternewpage iftex ifinfo ifplaintext ifxml sp vskip
  184. menu ignore syncodeindex comment c % node anchor))
  185. (define (ignored? tag)
  186. (memq tag ignore-list))
  187. (define tag-handlers
  188. `((title ,chapter)
  189. (chapter ,chapter)
  190. (section ,section)
  191. (subsection ,subsection)
  192. (subsubsection ,subsubsection)
  193. (appendix ,chapter)
  194. (appendixsec ,section)
  195. (appendixsubsec ,subsection)
  196. (appendixsubsubsec ,subsubsection)
  197. (unnumbered ,chapter)
  198. (unnumberedsec ,section)
  199. (unnumberedsubsec ,subsection)
  200. (unnumberedsubsubsec ,subsubsection)
  201. (majorheading ,chapter)
  202. (chapheading ,chapter)
  203. (heading ,section)
  204. (subheading ,subsection)
  205. (subsubheading ,subsubsection)
  206. (strong ,(make-surrounder "*"))
  207. (sample ,code)
  208. (samp ,code)
  209. (code ,code)
  210. (kbd ,code)
  211. (key ,key)
  212. (var ,var)
  213. (env ,code)
  214. (file ,code)
  215. (command ,code)
  216. (option ,code)
  217. (url ,code)
  218. (dfn ,(make-surrounder "\""))
  219. (cite ,(make-surrounder "\""))
  220. (acro ,passthrough)
  221. (email ,key)
  222. (emph ,(make-surrounder "_"))
  223. (sc ,var)
  224. (copyright ,(lambda args "(C)"))
  225. (result ,(lambda args "==>"))
  226. (xref ,ref)
  227. (ref ,ref)
  228. (pxref ,ref)
  229. (uref ,uref)
  230. (texinfo ,texinfo)
  231. (quotation ,(make-indenter 5 para))
  232. (itemize ,itemize)
  233. (enumerate ,enumerate)
  234. (item ,item)
  235. (table ,table)
  236. (entry ,entry)
  237. (example ,example)
  238. (lisp ,example)
  239. (smallexample ,example)
  240. (smalllisp ,example)
  241. (verbatim ,verbatim)
  242. (*fragment* ,fragment)
  243. (deftp ,def)
  244. (defcv ,def)
  245. (defivar ,def)
  246. (deftypeivar ,def)
  247. (defop ,def)
  248. (deftypeop ,def)
  249. (defmethod ,def)
  250. (deftypemethod ,def)
  251. (defopt ,def)
  252. (defvr ,def)
  253. (defvar ,def)
  254. (deftypevr ,def)
  255. (deftypevar ,def)
  256. (deffn ,def)
  257. (deftypefn ,def)
  258. (defmac ,def)
  259. (defspec ,def)
  260. (defun ,def)
  261. (deftypefun ,def)))
  262. (define (stexi->plain-text tree)
  263. "Transform @var{tree} into plain text. Returns a string."
  264. (cond
  265. ((null? tree) "")
  266. ((string? tree) tree)
  267. ((pair? tree)
  268. (cond
  269. ((symbol? (car tree))
  270. (let ((handler (and (not (ignored? (car tree)))
  271. (or (and=> (assq (car tree) tag-handlers) cadr)
  272. para))))
  273. (if handler (apply handler tree) "")))
  274. (else
  275. (string-concatenate (map-in-order stexi->plain-text tree)))))
  276. (else "")))
  277. ;;; arch-tag: f966c3f6-3b46-4790-bbf9-3ad27e4917c2