plain-text.scm 9.8 KB

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