plain-text.scm 9.5 KB

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