PageRenderTime 75ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/site/muse/lisp/muse-groff.el

https://github.com/AriT93/emacs
Emacs Lisp | 267 lines | 194 code | 33 blank | 40 comment | 10 complexity | 049a69fcac8010d1ef4732ca7aefe0ad MD5 | raw file
  1. ;;; muse-groff.el --- publish groff -mom -mwww files
  2. ;; Copyright (C) 2005 Free Software Foundation, Inc.
  3. ;; Author: Andrew J. Korty (ajk AT iu DOT edu)
  4. ;; Date: Tue 5-Jul-2005
  5. ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
  6. ;; Emacs Muse is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published
  8. ;; by the Free Software Foundation; either version 2, or (at your
  9. ;; option) any later version.
  10. ;; Emacs Muse is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs Muse; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;; Boston, MA 02110-1301, USA.
  18. ;;; Commentary:
  19. ;;; Contributors:
  20. ;;; Code:
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;; Muse Publishing Using groff -mom -mwww
  24. ;;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. (require 'muse-publish)
  27. (defgroup muse-groff nil
  28. "Rules for marking up a Muse file with groff -mom -mwww macros."
  29. :group 'muse-publish)
  30. (defcustom muse-groff-extension ".groff"
  31. "Default file extension for publishing groff -mom -mwww files."
  32. :type 'string
  33. :group 'muse-groff)
  34. (defcustom muse-groff-pdf-extension ".pdf"
  35. "Default file extension for publishing groff -mom -mwww files to PDF."
  36. :type 'string
  37. :group 'muse-groff)
  38. (defcustom muse-groff-header
  39. ".TITLE \"<lisp>(muse-publishing-directive \"title\")</lisp>\"
  40. .SUBTITLE \"<lisp>(muse-publishing-directive \"date\")</lisp>\"
  41. .AUTHOR \"<lisp>(muse-publishing-directive \"author\")</lisp>\"
  42. .PRINTSTYLE TYPESET
  43. .de list
  44. . LIST \\$1
  45. . SHIFT_LIST \\$2
  46. ..
  47. .PARA_INDENT 0
  48. .START
  49. <lisp>(and muse-publish-generate-contents \".TOC\n\")</lisp>\n"
  50. "Header used for publishing groff -mom -mwww files."
  51. :type '(choice string file)
  52. :group 'muse-groff)
  53. (defcustom muse-groff-footer " "
  54. "Footer used for publishing groff -mom -mwww files."
  55. :type '(choice string file)
  56. :group 'muse-groff)
  57. (defcustom muse-groff-markup-regexps
  58. `((10400 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?\n"
  59. "\\(["
  60. muse-regexp-blank
  61. "]*\n\\)+\\(<\\(blockquote\\|center\\)>\n\\)?")
  62. 0 muse-groff-markup-paragraph))
  63. "List of markup regexps for identifying regions in a Muse page.
  64. For more on the structure of this list, see `muse-publish-markup-regexps'."
  65. :type '(repeat (choice
  66. (list :tag "Markup rule"
  67. integer
  68. (choice regexp symbol)
  69. integer
  70. (choice string function symbol))
  71. function))
  72. :group 'muse-groff)
  73. (defcustom muse-groff-markup-functions
  74. '((table . muse-groff-markup-table))
  75. "An alist of style types to custom functions for that kind of text.
  76. For more on the structure of this list, see
  77. `muse-publish-markup-functions'."
  78. :type '(alist :key-type symbol :value-type function)
  79. :group 'muse-groff)
  80. (defcustom muse-groff-markup-tags
  81. '()
  82. "A list of tag specifications, for specially marking up GROFF."
  83. :type '(repeat (list (string :tag "Markup tag")
  84. (boolean :tag "Expect closing tag" :value t)
  85. (boolean :tag "Parse attributes" :value nil)
  86. (boolean :tag "Nestable" :value nil)
  87. function))
  88. :group 'muse-groff)
  89. (defcustom muse-groff-markup-strings
  90. `((image-with-desc . "\n.MPIMG -R %s.%s\n")
  91. (image . "\n.MPIMG -R %s.%s\n")
  92. (image-link . "\n.\\\" %s\n.MPIMG -R %s.%s")
  93. (url . "\n.URL %s %s\n\\z")
  94. (link . "\n.URL %s %s\n\\z")
  95. (email-addr . "\f[C]%s\f[]")
  96. (emdash . "\\(em")
  97. (rule . "\n.RULE\n")
  98. (no-break-space . "\\h")
  99. (enddots . "....")
  100. (dots . "...")
  101. ;; (part . "\\part{")
  102. ;; (part-end . "}")
  103. ;; (chapter . "\\chapter{")
  104. ;; (chapter-end . "}")
  105. (section . ".HEAD \"")
  106. (section-end . "\"")
  107. (subsection . ".SUBHEAD \"")
  108. (subsection-end . "\"")
  109. (subsubsection . ".PARAHEAD \"")
  110. (subsubsection-end . "\"")
  111. ;; (footnote . "\\c\n.FOOTNOTE\n")
  112. ;; (footnote-end . "\n.FOOTNOTE OFF\n")
  113. ;; (footnotemark . "\\footnotemark[%d]")
  114. ;; (footnotetext . "\\footnotetext[%d]{")
  115. ;; (footnotetext-end . "}")
  116. (begin-underline . "\n.UNDERSCORE \"")
  117. (end-underline . "\"\n")
  118. (begin-literal . "\\fC")
  119. (end-literal . "\\fP")
  120. (begin-emph . "\\fI")
  121. (end-emph . "\\fP")
  122. (begin-more-emph . "\\fB")
  123. (end-more-emph . "\\fP")
  124. (begin-most-emph . "\\f(BI")
  125. (end-most-emph . "\\fP")
  126. (begin-verse . ".QUOTE")
  127. (end-verse . ".QUOTE OFF")
  128. (begin-center . "\n.CENTER\n")
  129. (end-center . "\n.QUAD L\n")
  130. (begin-example . ,(concat
  131. ".QUOTE_FONT CR\n.QUOTE_INDENT 1\n"".QUOTE_SIZE -2\n"
  132. ".UNDERLINE_QUOTES OFF\n.QUOTE"))
  133. (end-example . ".QUOTE OFF")
  134. (begin-quote . ".BLOCKQUOTE")
  135. (end-quote . ".BLOCKQUOTE OFF")
  136. (begin-uli . ".list BULLET\n.SHIFT_LIST 2m\n.ITEM\n")
  137. (end-uli . "\n.LIST OFF")
  138. (begin-oli . ".list DIGIT\n.SHIFT_LIST 2m\n.ITEM\n")
  139. (end-oli . "\n.LIST OFF")
  140. (begin-ddt . "\\fB")
  141. (begin-dde . "\\fP\n.IR 4P\n")
  142. (end-ddt . ".IRX CLEAR"))
  143. "Strings used for marking up text.
  144. These cover the most basic kinds of markup, the handling of which
  145. differs little between the various styles."
  146. :type '(alist :key-type symbol :value-type string)
  147. :group 'muse-groff)
  148. (defcustom muse-groff-markup-specials
  149. '((?\\ . "\\e"))
  150. "A table of characters which must be represented specially."
  151. :type '(alist :key-type character :value-type string)
  152. :group 'muse-groff)
  153. (defun muse-groff-markup-paragraph ()
  154. (let ((end (copy-marker (match-end 0) t)))
  155. (goto-char (1+ (match-beginning 0)))
  156. (delete-region (point) end)
  157. (unless (looking-at "\.\\(\\(\\(SUB\\|PARA\\)?HEAD \\)\\|RULE$\\)")
  158. (muse-insert-markup ".ALD .5v\n.PP\n.ne 2\n"))))
  159. (defun muse-groff-protect-leading-chars ()
  160. "Protect leading periods and apostrophes from being interpreted as
  161. command characters."
  162. (while (re-search-forward "^[.']" nil t)
  163. (replace-match "\\\\&\\&" t)))
  164. (defun muse-groff-concat-lists ()
  165. "Join like lists."
  166. (let ((type "")
  167. arg begin)
  168. (while (re-search-forward "^\.LIST[ \t]+\\(.*\\)\n" nil t)
  169. (setq arg (match-string 1))
  170. (if (string= arg "OFF")
  171. (setq begin (match-beginning 0))
  172. (if (and begin (string= type arg))
  173. (delete-region begin (match-end 0))
  174. (setq type arg
  175. begin 0))))))
  176. (defun muse-groff-fixup-dquotes ()
  177. "Fixup double quotes."
  178. (let ((open t))
  179. (while (search-forward "\"" nil t)
  180. (unless (get-text-property (match-beginning 0) 'read-only)
  181. (if (and (bolp) (eq (char-before) ?\n))
  182. (setq open t))
  183. (if open
  184. (progn
  185. (replace-match "``")
  186. (setq open nil))
  187. (replace-match "''")
  188. (setq open t))))))
  189. (defun muse-groff-prepare-buffer ()
  190. (goto-char (point-min))
  191. (muse-groff-protect-leading-chars))
  192. (defun muse-groff-finalize-buffer ()
  193. (goto-char (point-min))
  194. (muse-groff-concat-lists))
  195. (defun muse-groff-pdf-browse-file (file)
  196. (shell-command (concat "open " file)))
  197. (defun muse-groff-pdf-generate (file output-path final-target)
  198. (muse-publish-transform-output
  199. file output-path final-target "PDF"
  200. (function
  201. (lambda (file output-path)
  202. (let ((command
  203. (format
  204. (concat "file=%s; ext=%s; cd %s && cp $file$ext $file.ref && "
  205. "groff -mom -mwww -t $file$ext > $file.ps && "
  206. "pstopdf $file.ps")
  207. (file-name-sans-extension file)
  208. muse-groff-extension
  209. (file-name-directory output-path))))
  210. (shell-command command))))
  211. ".ps"))
  212. (unless (assoc "groff" muse-publishing-styles)
  213. (muse-define-style "groff"
  214. :suffix 'muse-groff-extension
  215. :regexps 'muse-groff-markup-regexps
  216. ;;; :functions 'muse-groff-markup-functions
  217. :strings 'muse-groff-markup-strings
  218. :tags 'muse-groff-markup-tags
  219. :specials 'muse-groff-markup-specials
  220. :before 'muse-groff-prepare-buffer
  221. :after 'muse-groff-finalize-buffer
  222. :header 'muse-groff-header
  223. :footer 'muse-groff-footer
  224. :browser 'find-file)
  225. (muse-derive-style "groff-pdf" "groff"
  226. :final 'muse-groff-pdf-generate
  227. :browser 'muse-groff-pdf-browse-file
  228. :osuffix 'muse-groff-pdf-extension))
  229. (provide 'muse-groff)
  230. ;;; muse-groff.el ends here
  231. ;;
  232. ;; Local Variables:
  233. ;; indent-tabs-mode: nil
  234. ;; End: