docbook.scm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ;;;; (texinfo docbook) -- translating sdocbook into stexinfo
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  4. ;;;; Copyright (C) 2007, 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. ;; @c
  23. ;; This module exports procedures for transforming a limited subset of
  24. ;; the SXML representation of docbook into stexi. It is not complete by
  25. ;; any means. The intention is to gather a number of routines and
  26. ;; stylesheets so that external modules can parse specific subsets of
  27. ;; docbook, for example that set generated by certain tools.
  28. ;;
  29. ;;; Code:
  30. (define-module (texinfo docbook)
  31. #:use-module (sxml fold)
  32. #:use-module ((srfi srfi-1) #:select (fold))
  33. #:export (*sdocbook->stexi-rules*
  34. *sdocbook-block-commands*
  35. sdocbook-flatten
  36. filter-empty-elements
  37. replace-titles))
  38. (define (identity . args)
  39. args)
  40. (define (identity-deattr tag . body)
  41. `(,tag ,@(if (and (pair? body) (pair? (car body))
  42. (eq? (caar body) '@))
  43. (cdr body)
  44. body)))
  45. (define (detag-one tag body)
  46. body)
  47. (define tag-replacements
  48. '((parameter var)
  49. (replaceable var)
  50. (type code)
  51. (function code)
  52. (literal samp)
  53. (emphasis emph)
  54. (simpara para)
  55. (programlisting example)
  56. (firstterm dfn)
  57. (filename file)
  58. (quote cite)
  59. (application cite)
  60. (symbol code)
  61. (note cartouche)
  62. (envar env)))
  63. (define ignore-list '())
  64. (define (stringify exp)
  65. (with-output-to-string (lambda () (write exp))))
  66. (define *sdocbook->stexi-rules*
  67. #;
  68. "A stylesheet for use with SSAX's @code{pre-post-order}, which defines
  69. a number of generic rules for transforming docbook into texinfo."
  70. `((@ *preorder* . ,identity)
  71. (% *preorder* . ,identity)
  72. (para . ,identity-deattr)
  73. (orderedlist ((listitem
  74. . ,(lambda (tag . body)
  75. `(item ,@body))))
  76. . ,(lambda (tag . body)
  77. `(enumerate ,@body)))
  78. (itemizedlist ((listitem
  79. . ,(lambda (tag . body)
  80. `(item ,@body))))
  81. . ,(lambda (tag . body)
  82. `(itemize ,@body)))
  83. (term . ,detag-one)
  84. (informalexample . ,detag-one)
  85. (section . ,identity)
  86. (subsection . ,identity)
  87. (subsubsection . ,identity)
  88. (ulink . ,(lambda (tag attrs . body)
  89. `(uref (% ,(assq 'url (cdr attrs))
  90. (title ,@body)))))
  91. (*text* . ,detag-one)
  92. (*default* . ,(lambda (tag . body)
  93. (let ((subst (assq tag tag-replacements)))
  94. (cond
  95. (subst
  96. (if (and (pair? body) (pair? (car body)) (eq? (caar body) '@))
  97. (begin
  98. (warn "Ignoring" tag "attributes" (car body))
  99. (append (cdr subst) (cdr body)))
  100. (append (cdr subst) body)))
  101. ((memq tag ignore-list) #f)
  102. (else
  103. (warn "Don't know how to convert" tag "to stexi")
  104. `(c (% (all ,(stringify (cons tag body))))))))))))
  105. ;; (variablelist
  106. ;; ((varlistentry
  107. ;; . ,(lambda (tag term . body)
  108. ;; `(entry (% (heading ,@(cdr term))) ,@body)))
  109. ;; (listitem
  110. ;; . ,(lambda (tag simpara)
  111. ;; simpara)))
  112. ;; . ,(lambda (tag attrs . body)
  113. ;; `(table (% (formatter (var))) ,@body)))
  114. (define *sdocbook-block-commands*
  115. #;
  116. "The set of sdocbook element tags that should not be nested inside
  117. each other. @xref{texinfo docbook sdocbook-flatten,,sdocbook-flatten},
  118. for more information."
  119. '(para programlisting informalexample indexterm variablelist
  120. orderedlist refsect1 refsect2 refsect3 refsect4 title example
  121. note itemizedlist))
  122. (define (inline-command? command)
  123. (not (memq command *sdocbook-block-commands*)))
  124. (define (sdocbook-flatten sdocbook)
  125. "\"Flatten\" a fragment of sdocbook so that block elements do not nest
  126. inside each other.
  127. Docbook is a nested format, where e.g. a @code{refsect2} normally
  128. appears inside a @code{refsect1}. Logical divisions in the document are
  129. represented via the tree topology; a @code{refsect2} element
  130. @emph{contains} all of the elements in its section.
  131. On the contrary, texinfo is a flat format, in which sections are marked
  132. off by standalone section headers like @code{@@chapter}, and block
  133. elements do not nest inside each other.
  134. This function takes a nested sdocbook fragment @var{sdocbook} and
  135. flattens all of the sections, such that e.g.
  136. @example
  137. (refsect1 (refsect2 (para \"Hello\")))
  138. @end example
  139. becomes
  140. @example
  141. ((refsect1) (refsect2) (para \"Hello\"))
  142. @end example
  143. Oftentimes (always?) sectioning elements have @code{<title>} as their
  144. first element child; users interested in processing the @code{refsect*}
  145. elements into proper sectioning elements like @code{chapter} might be
  146. interested in @code{replace-titles} and @code{filter-empty-elements}.
  147. @xref{texinfo docbook replace-titles,,replace-titles}, and @ref{texinfo
  148. docbook filter-empty-elements,,filter-empty-elements}.
  149. Returns a nodeset, as described in @ref{sxml xpath}. That is to say,
  150. this function returns an untagged list of stexi elements."
  151. (define (fhere str accum block cont)
  152. (values (cons str accum)
  153. block
  154. cont))
  155. (define (fdown node accum block cont)
  156. (let ((command (car node))
  157. (attrs (and (pair? (cdr node)) (pair? (cadr node))
  158. (eq? (caadr node) '%)
  159. (cadr node))))
  160. (values (if attrs (cddr node) (cdr node))
  161. '()
  162. '()
  163. (lambda (accum block)
  164. (values
  165. `(,command ,@(if attrs (list attrs) '())
  166. ,@(reverse accum))
  167. block)))))
  168. (define (fup node paccum pblock pcont kaccum kblock kcont)
  169. (call-with-values (lambda () (kcont kaccum kblock))
  170. (lambda (ret block)
  171. (if (inline-command? (car ret))
  172. (values (cons ret paccum) (append kblock pblock) pcont)
  173. (values paccum (append kblock (cons ret pblock)) pcont)))))
  174. (call-with-values
  175. (lambda () (foldts*-values fdown fup fhere sdocbook '() '() #f))
  176. (lambda (accum block cont)
  177. (reverse block))))
  178. (define (filter-empty-elements sdocbook)
  179. "Filters out empty elements in an sdocbook nodeset. Mostly useful
  180. after running @code{sdocbook-flatten}."
  181. (reverse
  182. (fold
  183. (lambda (x rest)
  184. (if (and (pair? x) (null? (cdr x)))
  185. rest
  186. (cons x rest)))
  187. '()
  188. sdocbook)))
  189. (define (replace-titles sdocbook-fragment)
  190. "Iterate over the sdocbook nodeset @var{sdocbook-fragment},
  191. transforming contiguous @code{refsect} and @code{title} elements into
  192. the appropriate texinfo sectioning command. Most useful after having run
  193. @code{sdocbook-flatten}.
  194. For example:
  195. @example
  196. (replace-titles '((refsect1) (title \"Foo\") (para \"Bar.\")))
  197. @result{} '((chapter \"Foo\") (para \"Bar.\"))
  198. @end example
  199. "
  200. (define sections '((refsect1 . chapter)
  201. (refsect2 . section)
  202. (refsect3 . subsection)
  203. (refsect4 . subsubsection)))
  204. (let lp ((in sdocbook-fragment) (out '()))
  205. (cond
  206. ((null? in)
  207. (reverse out))
  208. ((and (pair? (car in)) (assq (caar in) sections))
  209. ;; pull out the title
  210. => (lambda (pair)
  211. (lp (cddr in) (cons `(,(cdr pair) ,@(cdadr in)) out))))
  212. (else
  213. (lp (cdr in) (cons (car in) out))))))