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