ox: Optional export of title
[org-mode.git] / contrib / lisp / ox-groff.el
blob9f54f71677239d527018efe2e8fb9b81f9c9db7f
1 ;;; ox-groff.el --- Groff Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a Groff Memorandum Macro back-end for Org
26 ;; generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'groff "*Test Groff*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Groff
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
36 ;; "GROFF_CLASS_OPTIONS".
38 ;;; Code:
40 (eval-when-compile (require 'cl))
41 (require 'ox)
43 (defvar orgtbl-exp-regexp)
46 ;;; Define Back-End
48 (org-export-define-backend 'groff
49 '((bold . org-groff-bold)
50 (center-block . org-groff-center-block)
51 (clock . org-groff-clock)
52 (code . org-groff-code)
53 (comment . (lambda (&rest args) ""))
54 (comment-block . (lambda (&rest args) ""))
55 (drawer . org-groff-drawer)
56 (dynamic-block . org-groff-dynamic-block)
57 (entity . org-groff-entity)
58 (example-block . org-groff-example-block)
59 (export-block . org-groff-export-block)
60 (export-snippet . org-groff-export-snippet)
61 (fixed-width . org-groff-fixed-width)
62 (footnote-definition . org-groff-footnote-definition)
63 (footnote-reference . org-groff-footnote-reference)
64 (headline . org-groff-headline)
65 (horizontal-rule . org-groff-horizontal-rule)
66 (inline-src-block . org-groff-inline-src-block)
67 (inlinetask . org-groff-inlinetask)
68 (italic . org-groff-italic)
69 (item . org-groff-item)
70 (keyword . org-groff-keyword)
71 (line-break . org-groff-line-break)
72 (link . org-groff-link)
73 (node-property . org-groff-node-property)
74 (paragraph . org-groff-paragraph)
75 (plain-list . org-groff-plain-list)
76 (plain-text . org-groff-plain-text)
77 (planning . org-groff-planning)
78 (property-drawer . org-groff-property-drawer)
79 (quote-block . org-groff-quote-block)
80 (radio-target . org-groff-radio-target)
81 (section . org-groff-section)
82 (special-block . org-groff-special-block)
83 (src-block . org-groff-src-block)
84 (statistics-cookie . org-groff-statistics-cookie)
85 (strike-through . org-groff-strike-through)
86 (subscript . org-groff-subscript)
87 (superscript . org-groff-superscript)
88 (table . org-groff-table)
89 (table-cell . org-groff-table-cell)
90 (table-row . org-groff-table-row)
91 (target . org-groff-target)
92 (template . org-groff-template)
93 (timestamp . org-groff-timestamp)
94 (underline . org-groff-underline)
95 (verbatim . org-groff-verbatim)
96 (verse-block . org-groff-verse-block))
97 :export-block "GROFF"
98 :menu-entry
99 '(?g "Export to GROFF"
100 ((?g "As GROFF file" org-groff-export-to-groff)
101 (?p "As PDF file" org-groff-export-to-pdf)
102 (?o "As PDF file and open"
103 (lambda (a s v b)
104 (if a (org-groff-export-to-pdf t s v b)
105 (org-open-file (org-groff-export-to-pdf nil s v b)))))))
106 :options-alist
107 '((:groff-class "GROFF_CLASS" nil org-groff-default-class t)
108 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
109 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
113 ;;; User Configurable Variables
115 (defgroup org-export-groff nil
116 "Options for exporting Org mode files to Groff."
117 :tag "Org Export Groff"
118 :group 'org-export)
120 ;;; Preamble
122 (defcustom org-groff-default-class "internal"
123 "The default Groff class."
124 :group 'org-export-groff
125 :type '(string :tag "Groff class"))
127 (defcustom org-groff-classes
128 '(("file" ".MT 1"
129 (:heading 'default :type "memo" :last-section "toc"))
130 ("internal" ".MT 0"
131 (:heading 'default :type "memo" :last-section "toc"))
132 ("programmer" ".MT 2"
133 (:heading 'default :type "memo" :last-section "toc"))
134 ("engineer" ".MT 3"
135 (:heading 'default :type "memo" :last-section "toc"))
136 ("external" ".MT 4"
137 (:heading 'default :type "memo" :last-section "toc"))
138 ("letter" ".MT 5"
139 (:heading 'default :type "memo" :last-section "sign"))
140 ("custom" ".so file"
141 (:heading custom-function :type "custom" :last-section "toc"))
142 ("dummy" ""
143 (:heading 'default :type "memo"))
144 ("ms" "ms"
145 (:heading 'default :type "cover" :last-section "toc"))
146 ("se_ms" "se_ms"
147 (:heading 'default :type "cover" :last-section "toc"))
148 ("block" "BL"
149 (:heading 'default :type "letter" :last-section "sign"))
150 ("semiblock" "SB"
151 (:heading 'default :type "letter" :last-section "sign"))
152 ("fullblock" "FB"
153 (:heading 'default :type "letter" :last-section "sign"))
154 ("simplified" "SP"
155 (:heading 'default :type "letter" :last-section "sign"))
156 ("none" "" (:heading 'default :type "custom")))
158 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
159 ;; This is to facilitate the creation of custom pages.
161 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
162 ;; are made. This is to facilitate Abstract Insertion.
164 "This list describes the attributes for the documents being created.
165 It allows for the creation of new "
166 :group 'org-export-groff
167 :type '(repeat
168 (list (string :tag "Document Type")
169 (string :tag "Header")
170 (repeat :tag "Options" :inline t
171 (choice
172 (list :tag "Heading")
173 (function :tag "Hook computing sectioning"))))))
175 ;;; Headline
177 (defconst org-groff-special-tags
178 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
180 (defcustom org-groff-format-headline-function nil
181 "Function to format headline text.
183 This function will be called with 5 arguments:
184 TODO the todo keyword (string or nil).
185 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
186 PRIORITY the priority of the headline (integer or nil)
187 TEXT the main headline text (string).
188 TAGS the tags as a list of strings (list of strings or nil).
190 The function result will be used in the section format string.
192 As an example, one could set the variable to the following, in
193 order to reproduce the default set-up:
195 \(defun org-groff-format-headline (todo todo-type priority text tags)
196 \"Default format function for a headline.\"
197 \(concat (when todo
198 \(format \"\\fB%s\\fP \" todo))
199 \(when priority
200 \(format \"[\\#%c] \" priority))
201 text
202 \(when tags
203 \(format \" %s \"
204 \(mapconcat 'identity tags \":\"))))"
205 :group 'org-export-groff
206 :type 'function)
208 ;;; Timestamps
210 (defcustom org-groff-active-timestamp-format "\\fI%s\\fP"
211 "A printf format string to be applied to active timestamps."
212 :group 'org-export-groff
213 :type 'string)
215 (defcustom org-groff-inactive-timestamp-format "\\fI%s\\fP"
216 "A printf format string to be applied to inactive timestamps."
217 :group 'org-export-groff
218 :type 'string)
220 (defcustom org-groff-diary-timestamp-format "\\fI%s\\fP"
221 "A printf format string to be applied to diary timestamps."
222 :group 'org-export-groff
223 :type 'string)
225 ;;; Links
227 (defcustom org-groff-inline-image-rules
228 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
229 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
230 "Rules characterizing image files that can be inlined into Groff.
232 A rule consists in an association whose key is the type of link
233 to consider, and value is a regexp that will be matched against
234 link's path.
236 Note that, by default, the image extensions actually allowed
237 depend on the way the Groff file is processed. When used with
238 pdfgroff, pdf, jpg and png images are OK. When processing
239 through dvi to Postscript, only ps and eps are allowed. The
240 default we use here encompasses both."
241 :group 'org-export-groff
242 :type '(alist :key-type (string :tag "Type")
243 :value-type (regexp :tag "Path")))
245 (defcustom org-groff-link-with-unknown-path-format "\\fI%s\\fP"
246 "Format string for links with unknown path type."
247 :group 'org-export-groff
248 :type 'string)
250 ;;; Tables
252 (defcustom org-groff-tables-centered t
253 "When non-nil, tables are exported in a center environment."
254 :group 'org-export-groff
255 :type 'boolean)
257 (defcustom org-groff-tables-verbatim nil
258 "When non-nil, tables are exported verbatim."
259 :group 'org-export-groff
260 :type 'boolean)
262 (defcustom org-groff-table-scientific-notation "%sE%s"
263 "Format string to display numbers in scientific notation.
264 The format should have \"%s\" twice, for mantissa and exponent
265 \(i.e. \"%s\\\\times10^{%s}\").
267 When nil, no transformation is made."
268 :group 'org-export-groff
269 :type '(choice
270 (string :tag "Format string")
271 (const :tag "No formatting")))
273 ;;; Text markup
275 (defcustom org-groff-text-markup-alist
276 '((bold . "\\fB%s\\fP")
277 (code . "\\fC%s\\fP")
278 (italic . "\\fI%s\\fP")
279 (strike-through . "\\fC%s\\fP") ; Strike through and underline
280 (underline . "\\fI%s\\fP") ; need to be revised.
281 (verbatim . "protectedtexttt"))
282 "Alist of Groff expressions to convert text markup.
284 The key must be a symbol among `bold', `code', `italic',
285 `strike-through', `underline' and `verbatim'. The value is
286 a formatting string to wrap fontified text with it.
288 If no association can be found for a given markup, text will be
289 returned as-is."
290 :group 'org-export-groff
291 :type 'alist
292 :options '(bold code italic strike-through underline verbatim))
294 ;;; Drawers
296 (defcustom org-groff-format-drawer-function nil
297 "Function called to format a drawer in Groff code.
299 The function must accept two parameters:
300 NAME the drawer name, like \"LOGBOOK\"
301 CONTENTS the contents of the drawer.
303 The function should return the string to be exported.
305 For example, the variable could be set to the following function
306 in order to mimic default behaviour:
308 \(defun org-groff-format-drawer-default \(name contents\)
309 \"Format a drawer element for Groff export.\"
310 contents\)"
311 :group 'org-export-groff
312 :type 'function)
314 ;;; Inlinetasks
316 (defcustom org-groff-format-inlinetask-function nil
317 "Function called to format an inlinetask in Groff code.
319 The function must accept six parameters:
320 TODO the todo keyword, as a string
321 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
322 PRIORITY the inlinetask priority, as a string
323 NAME the inlinetask name, as a string.
324 TAGS the inlinetask tags, as a list of strings.
325 CONTENTS the contents of the inlinetask, as a string.
327 The function should return the string to be exported.
329 For example, the variable could be set to the following function
330 in order to mimic default behaviour:
332 \(defun org-groff-format-inlinetask \(todo type priority name tags contents\)
333 \"Format an inline task element for Groff export.\"
334 \(let ((full-title
335 \(concat
336 \(when todo
337 \(format \"\\fB%s\\fP \" todo))
338 \(when priority (format \"[\\#%c] \" priority))
339 title
340 \(when tags
341 \(format \":%s:\"
342 \(mapconcat 'identity tags \":\")))))
343 \(format (concat \".DS L\\n\"
344 \"%s\\n\\n\"
345 \"%s\"
346 \".DE\")
347 full-title contents))"
348 :group 'org-export-groff
349 :type 'function)
351 ;; Src blocks
353 (defcustom org-groff-source-highlight nil
354 "Use GNU source highlight to embellish source blocks "
355 :group 'org-export-groff
356 :type 'boolean)
358 (defcustom org-groff-source-highlight-langs
359 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
360 (scheme "scheme")
361 (c "c") (cc "cpp") (csharp "csharp") (d "d")
362 (fortran "fortran") (cobol "cobol") (pascal "pascal")
363 (ada "ada") (asm "asm")
364 (perl "perl") (cperl "perl")
365 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
366 (java "java") (javascript "javascript")
367 (tex "latex")
368 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
369 (ocaml "caml") (caml "caml")
370 (sql "sql") (sqlite "sql")
371 (html "html") (css "css") (xml "xml")
372 (bat "bat") (bison "bison") (clipper "clipper")
373 (ldap "ldap") (opa "opa")
374 (php "php") (postscript "postscript") (prolog "prolog")
375 (properties "properties") (makefile "makefile")
376 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
377 "Alist mapping languages to their listing language counterpart.
378 The key is a symbol, the major mode symbol without the \"-mode\".
379 The value is the string that should be inserted as the language
380 parameter for the listings package. If the mode name and the
381 listings name are the same, the language does not need an entry
382 in this list - but it does not hurt if it is present."
383 :group 'org-export-groff
384 :type '(repeat
385 (list
386 (symbol :tag "Major mode ")
387 (string :tag "Listings language"))))
389 (defcustom org-groff-source-highlight-options nil
390 "Association list of options for the groff listings package.
392 These options are supplied as a comma-separated list to the
393 \\lstset command. Each element of the association list should be
394 a list containing two strings: the name of the option, and the
395 value. For example,
397 (setq org-groff-source-highlight-options
398 '((\"basicstyle\" \"\\small\")
399 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
401 will typeset the code in a small size font with underlined, bold
402 black keywords.
404 Note that the same options will be applied to blocks of all
405 languages."
406 :group 'org-export-groff
407 :type '(repeat
408 (list
409 (string :tag "Listings option name ")
410 (string :tag "Listings option value"))))
412 (defvar org-groff-custom-lang-environments nil
413 "Alist mapping languages to language-specific Groff environments.
415 It is used during export of src blocks by the listings and
416 groff packages. For example,
418 \(setq org-groff-custom-lang-environments
419 '\(\(python \"pythoncode\"\)\)\)
421 would have the effect that if org encounters begin_src python
422 during groff export it will use pythoncode as the source-highlight
423 language.")
425 ;;; Plain text
427 (defcustom org-groff-special-char
428 '(("(c)" . "\\\\(co")
429 ("(tm)" . "\\\\(tm")
430 ("(rg)" . "\\\\(rg"))
431 "CONS list in which the value of the car
432 is replace on the value of the CDR. "
433 :group 'org-export-groff
434 :type '(list
435 (cons :tag "Character Subtitute"
436 (string :tag "Original Character Group")
437 (string :tag "Replacement Character"))))
439 ;;; Compilation
441 (defcustom org-groff-pdf-process
442 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
443 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
444 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
446 "Commands to process a Groff file to a PDF file.
447 This is a list of strings, each of them will be given to the
448 shell as a command. %f in the command will be replaced by the
449 full file name, %b by the file base name \(i.e. without
450 extension) and %o by the base directory of the file."
451 :group 'org-export-pdf
452 :type '(choice
453 (repeat :tag "Shell command sequence"
454 (string :tag "Shell command"))
455 (const :tag "2 runs of pdfgroff"
456 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
457 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
458 (const :tag "3 runs of pdfgroff"
459 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
460 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
461 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
462 (function)))
464 (defcustom org-groff-logfiles-extensions
465 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
466 "The list of file extensions to consider as Groff logfiles."
467 :group 'org-export-groff
468 :type '(repeat (string :tag "Extension")))
470 (defcustom org-groff-remove-logfiles t
471 "Non-nil means remove the logfiles produced by PDF production.
472 These are the .aux, .log, .out, and .toc files."
473 :group 'org-export-groff
474 :type 'boolean)
476 (defcustom org-groff-organization "Org User"
477 "Name of the organization used to populate the .AF command."
478 :group 'org-export-groff
479 :type 'string)
481 (defcustom org-groff-raster-to-ps nil
482 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
483 `org-groff-inline-image-rules' is adjusted accordingly if not conversion is being
484 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
485 :group 'org-export-groff
486 :type '(choice
487 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
488 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
489 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
490 (const :tag "None" nil)))
492 (defvar org-groff-registered-references nil)
493 (defvar org-groff-special-content nil)
497 ;;; Internal Functions
499 (defun org-groff--caption/label-string (element info)
500 "Return caption and label Groff string for ELEMENT.
502 INFO is a plist holding contextual information. If there's no
503 caption nor label, return the empty string.
505 For non-floats, see `org-groff--wrap-label'."
506 (let ((main (org-export-get-caption element))
507 (short (org-export-get-caption element t))
508 (label (org-element-property :name element)))
509 (cond ((and (not main) (not label)) "")
510 ((not main) (format "\\fI%s\\fP" label))
511 ;; Option caption format with short name.
512 (short (format "%s\n.br\n - %s\n"
513 (org-export-data short info)
514 (org-export-data main info)))
515 ;; Standard caption format.
516 (t (format "\\fR%s\\fP" (org-export-data main info))))))
518 (defun org-groff--wrap-label (element output)
519 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
520 This function shouldn't be used for floats. See
521 `org-groff--caption/label-string'."
522 (let ((label (org-element-property :name element)))
523 (if (or (not output) (not label) (string= output "") (string= label ""))
524 output
525 (concat (format "%s\n.br\n" label) output))))
527 (defun org-groff--text-markup (text markup)
528 "Format TEXT depending on MARKUP text markup.
529 See `org-groff-text-markup-alist' for details."
530 (let ((fmt (cdr (assq markup org-groff-text-markup-alist))))
531 (cond
532 ;; No format string: Return raw text.
533 ((not fmt) text)
534 ((string= "protectedtexttt" fmt)
535 (let ((start 0)
536 (trans '(("\\" . "\\")))
537 (rtn "")
538 char)
539 (while (string-match "[\\{}$%&_#~^]" text)
540 (setq char (match-string 0 text))
541 (if (> (match-beginning 0) 0)
542 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
543 (setq text (substring text (1+ (match-beginning 0))))
544 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
545 rtn (concat rtn char)))
546 (setq text (concat rtn text))
547 (format "\\fC%s\\fP" text)))
548 ;; Else use format string.
549 (t (format fmt text)))))
552 (defun org-groff--get-tagged-content (tag info)
553 (cdr (assoc tag org-groff-special-content)))
555 (defun org-groff--mt-head (title contents attr info)
556 (concat
558 ;; 1. Insert Organization
559 (let ((firm-option (plist-get attr :firm)))
560 (cond
561 ((stringp firm-option)
562 (format ".AF \"%s\" \n" firm-option))
563 (t (format ".AF \"%s\" \n" (or org-groff-organization "")))))
565 ;; 2. Title
566 (let ((title (if (plist-get info :with-title) title ""))
567 (subtitle1 (plist-get attr :subtitle1))
568 (subtitle2 (plist-get attr :subtitle2)))
570 (cond
571 ((string= "" title)
572 (format ".TL \"%s\" \"%s\" \n%s\n"
573 (or subtitle1 "")
574 (or subtitle2 "") " "))
576 ((not (or subtitle1 subtitle2))
577 (format ".TL\n%s\n"
578 (or title "")))
580 (format ".TL \"%s\" \"%s \" \n%s\n"
581 (or subtitle1 "")
582 (or subtitle2 "") title))))
584 ;; 3. Author.
585 ;; In Groff, .AU *MUST* be placed after .TL
586 ;; If From, populate with data from From else
588 (let ((author (and (plist-get info :with-author)
589 (let ((auth (plist-get info :author)))
590 (and auth (org-export-data auth info)))))
591 (email (and (plist-get info :with-email)
592 (org-export-data (plist-get info :email) info)))
593 (from-data (org-groff--get-tagged-content "FROM" info))
595 (to-data (org-groff--get-tagged-content "TO" info)))
597 (cond
598 ((and author from-data)
599 (let ((au-line
600 (mapconcat
601 (lambda (from-line)
602 (format " \"%s\" " from-line))
603 (split-string
604 (setq from-data
605 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
607 (concat
608 (format ".AU \"%s\" " author) au-line "\n")))
610 ((and author email (not (string= "" email)))
611 (format ".AU \"%s\" \"%s\"\n" author email))
613 (author (format ".AU \"%s\"\n" author))
615 (t ".AU \"\" \n")))
618 ;; 4. Author Title, if present
619 (let ((at-item (plist-get attr :author-title)))
620 (if (and at-item (stringp at-item))
621 (format ".AT \"%s\" \n" at-item)
622 ""))
624 ;; 5. Date.
625 (when (plist-get info :with-date)
626 (let ((date (org-export-data (org-export-get-date info) info)))
627 (and (org-string-nw-p date) (format ".ND \"%s\"\n" date))))
630 ;; If Abstract, then Populate Abstract
633 (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info))
634 (to-data (org-groff--get-tagged-content "TO" info)))
635 (cond
636 (abstract-data
637 (format ".AS\n%s\n.AE\n" abstract-data))
638 (to-data
639 (format ".AS\n%s\n.AE\n" to-data))))))
641 (defun org-groff--letter-head (title contents attr info)
642 (let ((author (and (plist-get info :with-author)
643 (let ((auth (plist-get info :author)))
644 (and auth (org-export-data auth info)))))
645 (email (and (plist-get info :with-email)
646 (org-export-data (plist-get info :email) info)))
647 (from-data (org-groff--get-tagged-content "FROM" info))
648 (at-item (plist-get attr :author-title))
649 (to-data (org-groff--get-tagged-content "TO" info)))
652 ;; If FROM then get data from FROM
653 (if from-data
654 (setq from-data
655 (replace-regexp-in-string "\\.P\n" "" from-data))
656 (setq from-data ""))
658 (if to-data
659 (setq to-data
660 (replace-regexp-in-string "\\.P\n" "" to-data))
661 (setq from-data ""))
663 (concat
664 (cond
665 (from-data
666 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
667 ((and author email (not (string= "" email)))
668 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
669 (author (format ".WA \"%s\"\n.WE\n" author))
670 (t ".WA \"\" \n.WE\n"))
672 ;; If TO then get data from TO
674 (when to-data
675 (format ".IA \n%s\n.IE\n" to-data)))))
678 ;;; Template
680 (defun org-groff-template (contents info)
681 "Return complete document string after Groff conversion.
682 CONTENTS is the transcoded contents string. INFO is a plist
683 holding export options."
684 (let* ((title (org-export-data (plist-get info :title) info))
685 (attr (read
686 (format "(%s)"
687 (mapconcat
688 #'identity
689 (list (plist-get info :groff-class-options))
690 " "))))
691 (class (plist-get info :groff-class))
692 (class-options (plist-get info :groff-class-options))
693 (classes (assoc class org-groff-classes))
694 (classes-options (car (last classes)))
695 (heading-option (plist-get classes-options :heading))
696 (type-option (plist-get classes-options :type))
697 (last-option (plist-get classes-options :last-section))
698 (hyphenate (plist-get attr :hyphenate))
699 (justify-right (plist-get attr :justify-right))
701 (document-class-string
702 (progn
703 (org-element-normalize-string
704 (let* ((header (nth 1 (assoc class org-groff-classes)))
705 (document-class-item (if (stringp header) header "")))
706 document-class-item)))))
709 (concat
710 (if justify-right
711 (case justify-right
712 ('yes ".SA 1 \n")
713 ('no ".SA 0 \n")
714 (t ""))
717 (if hyphenate
718 (case hyphenate
719 ('yes ".nr Hy 1 \n")
720 ('no ".nr Hy 0 \n")
721 (t ""))
724 (cond
725 ((string= type-option "custom") "")
727 ((and (stringp document-class-string)
728 (string= type-option "cover"))
730 (concat
731 (format ".COVER %s\n" document-class-string)
732 (org-groff--mt-head title contents attr info)
733 ".COVEND\n"))
735 ((string= type-option "memo")
736 (concat
737 (org-groff--mt-head title contents attr info)
738 document-class-string))
739 ((string= type-option "letter")
740 (concat
741 (org-groff--letter-head title contents attr info)
742 (let ((sa-item (plist-get attr :salutation))
743 (cn-item (plist-get attr :confidential))
744 (sj-item (plist-get attr :subject))
745 (rn-item (plist-get attr :reference))
746 (at-item (plist-get attr :attention)))
748 (concat
750 (if (stringp sa-item)
751 (format ".LO SA \"%s\" \n" sa-item)
752 ".LO SA\n")
754 (when cn-item
755 (if (stringp cn-item)
756 (format ".LO CN \"%s\"\n" cn-item)
757 ".LO CN\n"))
759 (when (and at-item (stringp at-item))
760 (format ".LO AT \"%s\" \n" at-item))
761 (when (and title rn-item)
762 (format ".LO RN \"%s\"\n" title))
764 (when (and sj-item (stringp sj-item))
765 (format ".LO SJ \"%s\" \n" sj-item))
768 ".LT " document-class-string "\n"))))
770 (t ""))
772 contents
774 (cond
775 ((string= last-option "toc")
776 ".TC")
777 ((string= last-option "sign")
778 (let ((fc-item (plist-get attr :closing)))
779 (concat (if (stringp fc-item)
780 (format ".FC \"%s\" \n" fc-item)
781 ".FC\n")
782 ".SG\n")))
783 (t ""))
785 (progn
786 (mapconcat
787 (lambda (item)
788 (when (string= (car item) "NS")
789 (replace-regexp-in-string
790 "\\.P\n" "" (cdr item))))
791 (reverse org-groff-special-content) "\n")))))
795 ;;; Transcode Functions
797 ;;; Babel Call
799 ;; Babel Calls are ignored.
802 ;;; Bold
804 (defun org-groff-bold (bold contents info)
805 "Transcode BOLD from Org to Groff.
806 CONTENTS is the text with bold markup. INFO is a plist holding
807 contextual information."
808 (org-groff--text-markup contents 'bold))
810 ;;; Center Block
812 (defun org-groff-center-block (center-block contents info)
813 "Transcode a CENTER-BLOCK element from Org to Groff.
814 CONTENTS holds the contents of the center block. INFO is a plist
815 holding contextual information."
816 (org-groff--wrap-label
817 center-block
818 (format ".DS C \n%s\n.DE" contents)))
820 ;;; Clock
822 (defun org-groff-clock (clock contents info)
823 "Transcode a CLOCK element from Org to Groff.
824 CONTENTS is nil. INFO is a plist holding contextual
825 information."
826 (concat
827 (format "\\fB%s\\fP " org-clock-string)
828 (format org-groff-inactive-timestamp-format
829 (concat (org-translate-time
830 (org-element-property :raw-value
831 (org-element-property :value clock)))
832 (let ((time (org-element-property :duration clock)))
833 (and time (format " (%s)" time)))))))
835 ;;; Code
837 (defun org-groff-code (code contents info)
838 "Transcode a CODE object from Org to Groff.
839 CONTENTS is nil. INFO is a plist used as a communication
840 channel."
841 (org-groff--text-markup (org-element-property :value code) 'code))
843 ;;; Comments and Comment Blocks are ignored.
845 ;;; Drawer
847 (defun org-groff-drawer (drawer contents info)
848 "Transcode a DRAWER element from Org to Groff.
849 CONTENTS holds the contents of the block. INFO is a plist
850 holding contextual information."
851 (let* ((name (org-element-property :drawer-name drawer))
852 (output (if (functionp org-groff-format-drawer-function)
853 (funcall org-groff-format-drawer-function
854 name contents)
855 ;; If there's no user defined function: simply
856 ;; display contents of the drawer.
857 contents)))
858 (org-groff--wrap-label drawer output)))
860 ;;; Dynamic Block
862 (defun org-groff-dynamic-block (dynamic-block contents info)
863 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
864 CONTENTS holds the contents of the block. INFO is a plist
865 holding contextual information. See `org-export-data'."
866 (org-groff--wrap-label dynamic-block contents))
868 ;;; Entity
870 (defun org-groff-entity (entity contents info)
871 "Transcode an ENTITY object from Org to Groff.
872 CONTENTS are the definition itself. INFO is a plist holding
873 contextual information."
874 (org-element-property :utf-8 entity))
876 ;;; Example Block
878 (defun org-groff-example-block (example-block contents info)
879 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
880 CONTENTS is nil. INFO is a plist holding contextual
881 information."
882 (org-groff--wrap-label
883 example-block
884 (format ".DS L\n%s\n.DE"
885 (org-export-format-code-default example-block info))))
887 ;;; Export Block
889 (defun org-groff-export-block (export-block contents info)
890 "Transcode a EXPORT-BLOCK element from Org to Groff.
891 CONTENTS is nil. INFO is a plist holding contextual information."
892 (when (string= (org-element-property :type export-block) "GROFF")
893 (org-remove-indentation (org-element-property :value export-block))))
895 ;;; Export Snippet
897 (defun org-groff-export-snippet (export-snippet contents info)
898 "Transcode a EXPORT-SNIPPET object from Org to Groff.
899 CONTENTS is nil. INFO is a plist holding contextual information."
900 (when (eq (org-export-snippet-backend export-snippet) 'groff)
901 (org-element-property :value export-snippet)))
903 ;;; Fixed Width
905 (defun org-groff-fixed-width (fixed-width contents info)
906 "Transcode a FIXED-WIDTH element from Org to Groff.
907 CONTENTS is nil. INFO is a plist holding contextual information."
908 (org-groff--wrap-label
909 fixed-width
910 (format "\\fC\n%s\\fP"
911 (org-remove-indentation
912 (org-element-property :value fixed-width)))))
914 ;;; Footnote Definition
916 ;; Footnote Definitions are ignored.
918 ;; Footnotes are handled automatically in GROFF. Although manual
919 ;; references can be added, not really required.
921 (defun org-groff-footnote-reference (footnote-reference contents info)
922 ;; Changing from info to footnote-reference
923 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
924 (n (org-export-get-footnote-number footnote-reference info))
925 (data (org-trim (org-export-data raw info)))
926 (ref-id (plist-get (nth 1 footnote-reference) :label)))
927 ;; It is a reference
928 (if (string-match "fn:rl" ref-id)
929 (if (member ref-id org-groff-registered-references)
930 (format "\\*[%s]" ref-id)
931 (progn
932 (push ref-id org-groff-registered-references)
933 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
934 ;; else it is a footnote
935 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
937 ;;; Headline
939 (defun org-groff-headline (headline contents info)
940 "Transcode a HEADLINE element from Org to Groff.
941 CONTENTS holds the contents of the headline. INFO is a plist
942 holding contextual information."
943 (let* ((class (plist-get info :groff-class))
944 (level (org-export-get-relative-level headline info))
945 (numberedp (org-export-numbered-headline-p headline info))
946 ;; Section formatting will set two placeholders: one for the
947 ;; title and the other for the contents.
948 (classes (assoc class org-groff-classes))
949 (classes-options (car (last classes)))
950 (heading-option (plist-get classes-options :heading))
951 (section-fmt
952 (progn
953 (cond
954 ((and (symbolp heading-option)
955 (fboundp heading-option))
956 (funcall heading-option level numberedp))
957 ((> level 7) nil)
958 (t (if numberedp
959 (concat ".H " (number-to-string level) " \"%s\"\n%s")
960 ".HU \"%s\"\n%s")))))
961 ;; End of section-fmt
962 (text (org-export-data (org-element-property :title headline) info))
963 (todo
964 (and (plist-get info :with-todo-keywords)
965 (let ((todo (org-element-property :todo-keyword headline)))
966 (and todo (org-export-data todo info)))))
967 (todo-type (and todo (org-element-property :todo-type headline)))
968 (tags (and (plist-get info :with-tags)
969 (org-export-get-tags headline info)))
970 (priority (and (plist-get info :with-priority)
971 (org-element-property :priority headline)))
972 ;; Create the headline text along with a no-tag version. The
973 ;; latter is required to remove tags from table of contents.
974 (full-text (if (functionp org-groff-format-headline-function)
975 ;; User-defined formatting function.
976 (funcall org-groff-format-headline-function
977 todo todo-type priority text tags)
978 ;; Default formatting.
979 (concat
980 (when todo
981 (format "\\fB%s\\fP " todo))
982 (when priority (format " [\\#%c] " priority))
983 text
984 (when tags
985 (format " \\fC:%s:\\fP "
986 (mapconcat 'identity tags ":"))))))
987 (full-text-no-tag
988 (if (functionp org-groff-format-headline-function)
989 ;; User-defined formatting function.
990 (funcall org-groff-format-headline-function
991 todo todo-type priority text nil)
992 ;; Default formatting.
993 (concat
994 (when todo (format "\\fB%s\\fP " todo))
995 (when priority (format " [\\#%c] " priority))
996 text)))
997 ;; Associate some \label to the headline for internal links.
998 ;; (headline-label
999 ;; (format "\\label{sec-%s}\n"
1000 ;; (mapconcat 'number-to-string
1001 ;; (org-export-get-headline-number headline info)
1002 ;; "-")))
1003 (headline-label "")
1004 (pre-blanks
1005 (make-string (org-element-property :pre-blank headline) 10)))
1007 (cond
1008 ;; Case 1: Special Tag
1009 ((member (car tags) org-groff-special-tags)
1010 (cond
1011 ((string= (car tags) "BODY") contents)
1013 ((string= (car tags) "NS")
1014 (progn
1015 (push (cons (car tags)
1016 (format ".NS \"%s\" 1 \n%s"
1017 (car (org-element-property :title headline))
1018 (or contents " ")))
1019 org-groff-special-content) nil))
1022 (progn
1023 (push (cons (car tags) contents) org-groff-special-content)
1024 nil))))
1026 ;; Case 2: This is a footnote section: ignore it.
1027 ((org-element-property :footnote-section-p headline) nil)
1029 ;; Case 3: This is a deep sub-tree: export it as a list item.
1030 ;; Also export as items headlines for which no section
1031 ;; format has been found.
1032 ((or (not section-fmt) (org-export-low-level-p headline info))
1033 ;; Build the real contents of the sub-tree.
1034 (let ((low-level-body
1035 (concat
1036 ;; If the headline is the first sibling, start a list.
1037 (when (org-export-first-sibling-p headline info)
1038 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1039 ;; Itemize headline
1040 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1041 ;; If headline is not the last sibling simply return
1042 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1043 ;; blank line.
1044 (if (not (org-export-last-sibling-p headline info)) low-level-body
1045 (replace-regexp-in-string
1046 "[ \t\n]*\\'"
1047 (concat "\n.LE")
1048 low-level-body))))
1050 ;; Case 4. Standard headline. Export it as a section.
1052 (format section-fmt full-text
1053 (concat headline-label pre-blanks contents))))))
1055 ;;; Horizontal Rule
1056 ;; Not supported
1058 ;;; Inline Babel Call
1060 ;; Inline Babel Calls are ignored.
1062 ;;; Inline Src Block
1064 (defun org-groff-inline-src-block (inline-src-block contents info)
1065 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1066 CONTENTS holds the contents of the item. INFO is a plist holding
1067 contextual information."
1068 (let* ((code (org-element-property :value inline-src-block)))
1069 (cond
1070 (org-groff-source-highlight
1071 (let* ((tmpdir (if (featurep 'xemacs)
1072 temp-directory
1073 temporary-file-directory))
1074 (in-file (make-temp-name
1075 (expand-file-name "srchilite" tmpdir)))
1076 (out-file (make-temp-name
1077 (expand-file-name "reshilite" tmpdir)))
1078 (org-lang (org-element-property :language inline-src-block))
1079 (lst-lang (cadr (assq (intern org-lang)
1080 org-groff-source-highlight-langs)))
1082 (cmd (concat (expand-file-name "source-highlight")
1083 " -s " lst-lang
1084 " -f groff_mm_color "
1085 " -i " in-file
1086 " -o " out-file)))
1087 (if lst-lang
1088 (let ((code-block ""))
1089 (with-temp-file in-file (insert code))
1090 (shell-command cmd)
1091 (setq code-block (org-file-contents out-file))
1092 (delete-file in-file)
1093 (delete-file out-file)
1094 code-block)
1095 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1096 code))))
1098 ;; Do not use a special package: transcode it verbatim.
1100 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1102 ;;; Inlinetask
1104 (defun org-groff-inlinetask (inlinetask contents info)
1105 "Transcode an INLINETASK element from Org to Groff.
1106 CONTENTS holds the contents of the block. INFO is a plist
1107 holding contextual information."
1108 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1109 (todo (and (plist-get info :with-todo-keywords)
1110 (let ((todo (org-element-property :todo-keyword inlinetask)))
1111 (and todo (org-export-data todo info)))))
1112 (todo-type (org-element-property :todo-type inlinetask))
1113 (tags (and (plist-get info :with-tags)
1114 (org-export-get-tags inlinetask info)))
1115 (priority (and (plist-get info :with-priority)
1116 (org-element-property :priority inlinetask))))
1117 ;; If `org-groff-format-inlinetask-function' is provided, call it
1118 ;; with appropriate arguments.
1119 (if (functionp org-groff-format-inlinetask-function)
1120 (funcall org-groff-format-inlinetask-function
1121 todo todo-type priority title tags contents)
1122 ;; Otherwise, use a default template.
1123 (org-groff--wrap-label
1124 inlinetask
1125 (let ((full-title
1126 (concat
1127 (when todo (format "\\fB%s\\fP " todo))
1128 (when priority (format " [\\#%c] " priority))
1129 title
1130 (when tags (format " \\fC:%s:\\fP "
1131 (mapconcat 'identity tags ":"))))))
1132 (format (concat "\n.DS I\n"
1133 "%s\n"
1134 ".sp"
1135 "%s\n"
1136 ".DE")
1137 full-title contents))))))
1139 ;;; Italic
1141 (defun org-groff-italic (italic contents info)
1142 "Transcode ITALIC from Org to Groff.
1143 CONTENTS is the text with italic markup. INFO is a plist holding
1144 contextual information."
1145 (org-groff--text-markup contents 'italic))
1147 ;;; Item
1149 (defun org-groff-item (item contents info)
1150 "Transcode an ITEM element from Org to Groff.
1151 CONTENTS holds the contents of the item. INFO is a plist holding
1152 contextual information."
1153 (let* ((bullet (org-element-property :bullet item))
1154 (type (org-element-property
1155 :type (org-element-property :parent item)))
1156 (checkbox (case (org-element-property :checkbox item)
1157 (on "\\o'\\(sq\\(mu'")
1158 (off "\\(sq")
1159 (trans "\\o'\\(sq\\(mi'")))
1160 (tag (let ((tag (org-element-property :tag item)))
1161 ;; Check-boxes must belong to the tag.
1162 (and tag (format "%s"
1163 (concat checkbox
1164 (org-export-data tag info)))))))
1166 (cond
1167 ((or checkbox tag)
1168 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1169 "\n"
1170 (org-trim (or contents " "))))
1171 ((eq type 'ordered)
1172 (concat ".LI"
1173 "\n"
1174 (org-trim (or contents " "))))
1176 (let* ((bullet (org-trim bullet))
1177 (marker (cond ((string= "-" bullet) "\\(em")
1178 ((string= "*" bullet) "\\(bu")
1179 (t "\\(dg"))))
1180 (concat ".LI " marker "\n"
1181 (org-trim (or contents " "))))))))
1183 ;;; Keyword
1185 (defun org-groff-keyword (keyword contents info)
1186 "Transcode a KEYWORD element from Org to Groff.
1187 CONTENTS is nil. INFO is a plist holding contextual information."
1188 (let ((key (org-element-property :key keyword))
1189 (value (org-element-property :value keyword)))
1190 (cond
1191 ((string= key "GROFF") value)
1192 (t nil))))
1194 ;;; Line Break
1196 (defun org-groff-line-break (line-break contents info)
1197 "Transcode a LINE-BREAK object from Org to Groff.
1198 CONTENTS is nil. INFO is a plist holding contextual information."
1199 ".br\n")
1201 ;;; Link
1202 ;; Inline images just place a call to .PSPIC or .PS/.PE
1203 ;; and load the graph.
1205 (defun org-groff-link--inline-image (link info)
1206 "Return Groff code for an inline image.
1207 LINK is the link pointing to the inline image. INFO is a plist
1208 used as a communication channel."
1209 (let* ((parent (org-export-get-parent-element link))
1210 (path (let ((raw-path (org-element-property :path link)))
1211 (if (not (file-name-absolute-p raw-path)) raw-path
1212 (expand-file-name raw-path))))
1213 (attr (org-export-read-attribute :attr_groff link))
1214 (placement
1215 (let ((pos (plist-get attr :position)))
1216 (cond ((string= pos 'center) "")
1217 ((string= pos 'left) "-L")
1218 ((string= pos 'right) "-R")
1219 (t ""))))
1220 (width (or (plist-get attr :width) ""))
1221 (height (or (plist-get attr :height) ""))
1222 (caption (and (not (plist-get attr :disable-caption))
1223 (org-groff--caption/label-string parent info))))
1224 ;; Now clear ATTR from any special keyword and set a default value
1225 ;; if nothing is left. Return proper string.
1226 (concat
1227 (cond
1228 ((and org-groff-raster-to-ps
1229 (or (string-match ".\.png$" path)
1230 (string-match ".\.jpg$" path)))
1231 (let ((eps-path (concat path ".eps")))
1232 (shell-command (format org-groff-raster-to-ps path eps-path))
1233 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1234 placement eps-path width height)))
1235 ((string-match ".\.pic$" path)
1236 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1237 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1238 placement path width height)))
1239 (and caption (format "\n.FG \"%s\"" caption)))))
1241 (defun org-groff-link (link desc info)
1242 "Transcode a LINK object from Org to Groff.
1244 DESC is the description part of the link, or the empty string.
1245 INFO is a plist holding contextual information. See
1246 `org-export-data'."
1248 (let* ((type (org-element-property :type link))
1249 (raw-path (org-element-property :path link))
1250 ;; Ensure DESC really exists, or set it to nil.
1251 (desc (and (not (string= desc "")) desc))
1252 (imagep (org-export-inline-image-p
1253 link org-groff-inline-image-rules))
1254 (path (cond
1255 ((member type '("http" "https" "ftp" "mailto"))
1256 (concat type ":" raw-path))
1257 ((and (string= type "file") (file-name-absolute-p raw-path))
1258 (concat "file://" raw-path))
1259 (t raw-path))))
1260 (cond
1261 ((org-export-custom-protocol-maybe link desc info))
1262 ;; Image file.
1263 (imagep (org-groff-link--inline-image link info))
1264 ;; import groff files
1265 ((and (string= type "file")
1266 (string-match ".\.groff$" raw-path))
1267 (concat ".so " raw-path "\n"))
1268 ;; Radio link: transcode target's contents and use them as link's
1269 ;; description.
1270 ((string= type "radio")
1271 (let ((destination (org-export-resolve-radio-link link info)))
1272 (if (not destination) desc
1273 (format "\\fI [%s] \\fP"
1274 (org-export-solidify-link-text
1275 (org-element-property :value destination))))))
1277 ;; Links pointing to a headline: find destination and build
1278 ;; appropriate referencing command.
1279 ((member type '("custom-id" "fuzzy" "id"))
1280 (let ((destination (if (string= type "fuzzy")
1281 (org-export-resolve-fuzzy-link link info)
1282 (org-export-resolve-id-link link info))))
1283 (case (org-element-type destination)
1284 ;; Id link points to an external file.
1285 (plain-text
1286 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1287 (format "\\fI file://%s \\fP" destination)))
1288 ;; Fuzzy link points nowhere.
1289 ('nil
1290 (format org-groff-link-with-unknown-path-format
1291 (or desc
1292 (org-export-data
1293 (org-element-property :raw-link link) info))))
1294 ;; LINK points to a headline. If headlines are numbered and
1295 ;; the link has no description, display headline's number.
1296 ;; Otherwise, display description or headline's title.
1297 (headline
1298 (let ((label ""))
1299 (if (and (plist-get info :section-numbers) (not desc))
1300 (format "\\fI%s\\fP" label)
1301 (format "\\fI%s\\fP"
1302 (or desc
1303 (org-export-data
1304 (org-element-property :title destination) info))))))
1305 ;; Fuzzy link points to a target. Do as above.
1306 (otherwise
1307 (let ((path (org-export-solidify-link-text path)))
1308 (if (not desc) (format "\\fI%s\\fP" path)
1309 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1310 ;; External link with a description part.
1311 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1312 ;; External link without a description part.
1313 (path (format "\\fI%s\\fP" path))
1314 ;; No path, only description. Try to do something useful.
1315 (t (format org-groff-link-with-unknown-path-format desc)))))
1317 ;;; Node Property
1319 (defun org-groff-node-property (node-property contents info)
1320 "Transcode a NODE-PROPERTY element from Org to Groff.
1321 CONTENTS is nil. INFO is a plist holding contextual
1322 information."
1323 (format "%s:%s"
1324 (org-element-property :key node-property)
1325 (let ((value (org-element-property :value node-property)))
1326 (if value (concat " " value) ""))))
1328 ;;; Paragraph
1330 (defun org-groff-paragraph (paragraph contents info)
1331 "Transcode a PARAGRAPH element from Org to Groff.
1332 CONTENTS is the contents of the paragraph, as a string. INFO is
1333 the plist used as a communication channel."
1334 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1335 (when parent
1336 (let* ((parent-type (car parent))
1337 (fixed-paragraph "")
1338 (class (plist-get info :groff-class))
1339 (class-options (plist-get info :groff-class-options))
1340 (classes (assoc class org-groff-classes))
1341 (classes-options (car (last classes)))
1342 (paragraph-option (plist-get classes-options :paragraph)))
1343 (cond
1344 ((and (symbolp paragraph-option)
1345 (fboundp paragraph-option))
1346 (funcall paragraph-option parent-type parent contents))
1347 ((and (eq parent-type 'item)
1348 (plist-get (nth 1 parent) :bullet))
1349 (setq fixed-paragraph (concat "" contents)))
1350 ((eq parent-type 'section)
1351 (setq fixed-paragraph (concat ".P\n" contents)))
1352 ((eq parent-type 'footnote-definition)
1353 (setq fixed-paragraph (concat "" contents)))
1354 (t (setq fixed-paragraph (concat "" contents))))
1355 fixed-paragraph))))
1357 ;;; Plain List
1359 (defun org-groff-plain-list (plain-list contents info)
1360 "Transcode a PLAIN-LIST element from Org to Groff.
1361 CONTENTS is the contents of the list. INFO is a plist holding
1362 contextual information."
1363 (let* ((type (org-element-property :type plain-list))
1364 (attr (mapconcat #'identity
1365 (org-element-property :attr_groff plain-list)
1366 " "))
1367 (groff-type (cond
1368 ((eq type 'ordered) ".AL")
1369 ((eq type 'unordered) ".BL")
1370 ((eq type 'descriptive) ".VL 2.0i"))))
1371 (org-groff--wrap-label
1372 plain-list
1373 (format "%s\n%s\n.LE" groff-type contents))))
1375 ;;; Plain Text
1377 (defun org-groff-plain-text (text info)
1378 "Transcode a TEXT string from Org to Groff.
1379 TEXT is the string to transcode. INFO is a plist holding
1380 contextual information."
1381 (let ((output text))
1382 ;; Protect various characters.
1383 (setq output (replace-regexp-in-string
1384 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1385 "$\\" output nil t 1))
1386 ;; Activate smart quotes. Be sure to provide original TEXT string
1387 ;; since OUTPUT may have been modified.
1388 (when (plist-get info :with-smart-quotes)
1389 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
1390 ;; Handle Special Characters
1391 (if org-groff-special-char
1392 (dolist (special-char-list org-groff-special-char)
1393 (setq output
1394 (replace-regexp-in-string (car special-char-list)
1395 (cdr special-char-list) output))))
1396 ;; Handle break preservation if required.
1397 (when (plist-get info :preserve-breaks)
1398 (setq output (replace-regexp-in-string
1399 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
1400 ;; Return value.
1401 output))
1403 ;;; Planning
1405 (defun org-groff-planning (planning contents info)
1406 "Transcode a PLANNING element from Org to Groff.
1407 CONTENTS is nil. INFO is a plist holding contextual
1408 information."
1409 (concat
1410 (mapconcat
1411 'identity
1412 (delq nil
1413 (list
1414 (let ((closed (org-element-property :closed planning)))
1415 (when closed
1416 (concat
1417 (format "\\fR %s \\fP" org-closed-string)
1418 (format org-groff-inactive-timestamp-format
1419 (org-translate-time
1420 (org-element-property :raw-value closed))))))
1421 (let ((deadline (org-element-property :deadline planning)))
1422 (when deadline
1423 (concat
1424 (format "\\fB %s \\fP" org-deadline-string)
1425 (format org-groff-active-timestamp-format
1426 (org-translate-time
1427 (org-element-property :raw-value deadline))))))
1428 (let ((scheduled (org-element-property :scheduled planning)))
1429 (when scheduled
1430 (concat
1431 (format "\\fR %s \\fP" org-scheduled-string)
1432 (format org-groff-active-timestamp-format
1433 (org-translate-time
1434 (org-element-property :raw-value scheduled))))))))
1436 ""))
1438 ;;;; Property Drawer
1440 (defun org-groff-property-drawer (property-drawer contents info)
1441 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1442 CONTENTS holds the contents of the drawer. INFO is a plist
1443 holding contextual information."
1444 (and (org-string-nw-p contents)
1445 (format "\\fC\n%s\\fP" contents)))
1447 ;;; Quote Block
1449 (defun org-groff-quote-block (quote-block contents info)
1450 "Transcode a QUOTE-BLOCK element from Org to Groff.
1451 CONTENTS holds the contents of the block. INFO is a plist
1452 holding contextual information."
1453 (org-groff--wrap-label
1454 quote-block
1455 (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
1457 ;;; Radio Target
1459 (defun org-groff-radio-target (radio-target text info)
1460 "Transcode a RADIO-TARGET object from Org to Groff.
1461 TEXT is the text of the target. INFO is a plist holding
1462 contextual information."
1463 (format "%s - %s"
1464 (org-export-solidify-link-text
1465 (org-element-property :value radio-target))
1466 text))
1468 ;;; Section
1470 (defun org-groff-section (section contents info)
1471 "Transcode a SECTION element from Org to Groff.
1472 CONTENTS holds the contents of the section. INFO is a plist
1473 holding contextual information."
1474 contents)
1476 ;;; Special Block
1478 (defun org-groff-special-block (special-block contents info)
1479 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1480 CONTENTS holds the contents of the block. INFO is a plist
1481 holding contextual information."
1482 (let ((type (org-element-property :type special-block)))
1483 (org-groff--wrap-label
1484 special-block
1485 (format "%s\n" contents))))
1487 ;;; Src Block
1489 (defun org-groff-src-block (src-block contents info)
1490 "Transcode a SRC-BLOCK element from Org to Groff.
1491 CONTENTS holds the contents of the item. INFO is a plist holding
1492 contextual information."
1493 (let* ((lang (org-element-property :language src-block))
1494 (label (org-element-property :name src-block))
1495 (code (org-element-property :value src-block))
1496 (custom-env (and lang
1497 (cadr (assq (intern lang)
1498 org-groff-custom-lang-environments))))
1499 (num-start (case (org-element-property :number-lines src-block)
1500 (continued (org-export-get-loc src-block info))
1501 (new 0)))
1502 (retain-labels (org-element-property :retain-labels src-block))
1503 (caption (and (not (org-export-read-attribute
1504 :attr_groff src-block :disable-caption))
1505 (org-groff--caption/label-string src-block info))))
1507 (cond
1508 ;; Case 1. No source fontification.
1509 ((not org-groff-source-highlight)
1510 (concat
1511 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1512 (org-export-format-code-default src-block info))
1513 (and caption (format ".EX \"%s\" " caption))))
1515 ;; Case 2. Source fontification.
1516 (org-groff-source-highlight
1517 (let* ((tmpdir (if (featurep 'xemacs)
1518 temp-directory
1519 temporary-file-directory))
1520 (in-file (make-temp-name
1521 (expand-file-name "srchilite" tmpdir)))
1522 (out-file (make-temp-name
1523 (expand-file-name "reshilite" tmpdir)))
1525 (org-lang (org-element-property :language src-block))
1526 (lst-lang (cadr (assq (intern org-lang)
1527 org-groff-source-highlight-langs)))
1529 (cmd (concat "source-highlight"
1530 " -s " lst-lang
1531 " -f groff_mm_color "
1532 " -i " in-file
1533 " -o " out-file)))
1535 (concat
1536 (if lst-lang
1537 (let ((code-block ""))
1538 (with-temp-file in-file (insert code))
1539 (shell-command cmd)
1540 (setq code-block (org-file-contents out-file))
1541 (delete-file in-file)
1542 (delete-file out-file)
1543 (format "%s\n" code-block))
1544 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1545 code))
1546 (and caption (format ".EX \"%s\" " caption))))))))
1549 ;;; Statistics Cookie
1551 (defun org-groff-statistics-cookie (statistics-cookie contents info)
1552 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1553 CONTENTS is nil. INFO is a plist holding contextual information."
1554 (org-element-property :value statistics-cookie))
1557 ;;; Strike-Through
1559 (defun org-groff-strike-through (strike-through contents info)
1560 "Transcode STRIKE-THROUGH from Org to Groff.
1561 CONTENTS is the text with strike-through markup. INFO is a plist
1562 holding contextual information."
1563 (org-groff--text-markup contents 'strike-through))
1565 ;;; Subscript
1567 (defun org-groff-subscript (subscript contents info)
1568 "Transcode a SUBSCRIPT object from Org to Groff.
1569 CONTENTS is the contents of the object. INFO is a plist holding
1570 contextual information."
1571 (format "\\d\\s-2%s\\s+2\\u" contents))
1573 ;;; Superscript "^_%s$
1575 (defun org-groff-superscript (superscript contents info)
1576 "Transcode a SUPERSCRIPT object from Org to Groff.
1577 CONTENTS is the contents of the object. INFO is a plist holding
1578 contextual information."
1579 (format "\\u\\s-2%s\\s+2\\d" contents))
1582 ;;; Table
1584 ;; `org-groff-table' is the entry point for table transcoding. It
1585 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1586 ;; delegates the job to `org-groff-table--org-table' function,
1587 ;; depending of the type of the table.
1589 ;; `org-groff-table--align-string' is a subroutine used to build
1590 ;; alignment string for Org tables.
1592 (defun org-groff-table (table contents info)
1593 "Transcode a TABLE element from Org to Groff.
1594 CONTENTS is the contents of the table. INFO is a plist holding
1595 contextual information."
1596 (cond
1597 ;; Case 1: verbatim table.
1598 ((or org-groff-tables-verbatim
1599 (let ((attr (read (format "(%s)"
1600 (mapconcat
1601 #'identity
1602 (org-element-property :attr_groff table) " ")))))
1603 (and attr (plist-get attr :verbatim))))
1605 (format ".DS L\n\\fC%s\\fP\n.DE"
1606 ;; Re-create table, without affiliated keywords.
1607 (org-trim
1608 (org-element-interpret-data
1609 `(table nil ,@(org-element-contents table))))))
1611 ;; Case 2: Standard table.
1612 (t (org-groff-table--org-table table contents info))))
1614 (defun org-groff-table--align-string (divider table info)
1615 "Return an appropriate Groff alignment string.
1616 TABLE is the considered table. INFO is a plist used as
1617 a communication channel."
1618 (let (alignment)
1619 ;; Extract column groups and alignment from first (non-rule) row.
1620 (org-element-map
1621 (org-element-map table 'table-row
1622 (lambda (row)
1623 (and (eq (org-element-property :type row) 'standard) row))
1624 info 'first-match)
1625 'table-cell
1626 (lambda (cell)
1627 (let* ((borders (org-export-table-cell-borders cell info))
1628 (raw-width (org-export-table-cell-width cell info))
1629 (width-cm (when raw-width (/ raw-width 5)))
1630 (width (if raw-width (format "w(%dc)"
1631 (if (< width-cm 1) 1 width-cm)) "")))
1632 ;; Check left border for the first cell only.
1633 ;; Alignment is nil on assignment
1635 (when (and (memq 'left borders) (not alignment))
1636 (push "|" alignment))
1637 (push
1638 (case (org-export-table-cell-alignment cell info)
1639 (left (concat "l" width divider))
1640 (right (concat "r" width divider))
1641 (center (concat "c" width divider)))
1642 alignment)
1643 (when (memq 'right borders) (push "|" alignment))))
1644 info)
1645 (apply 'concat (reverse alignment))))
1647 (defun org-groff-table--org-table (table contents info)
1648 "Return appropriate Groff code for an Org table.
1650 TABLE is the table type element to transcode. CONTENTS is its
1651 contents, as a string. INFO is a plist used as a communication
1652 channel.
1654 This function assumes TABLE has `org' as its `:type' attribute."
1655 (let* ((attr (org-export-read-attribute :attr_groff table))
1656 (label (org-element-property :name table))
1657 (caption (and (not (plist-get attr :disable-caption))
1658 (org-groff--caption/label-string table info)))
1659 (divider (if (plist-get attr :divider) "|" " "))
1661 ;; Determine alignment string.
1662 (alignment (org-groff-table--align-string divider table info))
1664 ;; Extract others display options.
1666 (lines (org-split-string contents "\n"))
1668 (attr-list
1669 (delq nil
1670 (list (and (plist-get attr :expand) "expand")
1671 (let ((placement (plist-get attr :placement)))
1672 (cond ((string= placement 'center) "center")
1673 ((string= placement 'left) nil)
1674 (t (if org-groff-tables-centered "center" ""))))
1675 (or (plist-get attr :boxtype) "box"))))
1677 (title-line (plist-get attr :title-line))
1678 (long-cells (plist-get attr :long-cells))
1680 (table-format
1681 (concat
1682 (or (car attr-list) "")
1684 (let (output-list)
1685 (when (cdr attr-list)
1686 (dolist (attr-item (cdr attr-list))
1687 (setq output-list (concat output-list
1688 (format ",%s" attr-item)))))
1689 output-list) "")))
1690 (first-line
1691 (when lines (org-split-string (car lines) "\t"))))
1692 ;; Prepare the final format string for the table.
1695 (cond
1696 ;; Others.
1697 (lines
1698 (concat ".TS\n " table-format ";\n"
1699 (format "%s.\n"
1700 (let ((final-line ""))
1701 (when title-line
1702 (dotimes (i (length first-line))
1703 (setq final-line (concat final-line "cb" divider))))
1705 (setq final-line (concat final-line "\n"))
1707 (if alignment
1708 (setq final-line (concat final-line alignment))
1709 (dotimes (i (length first-line))
1710 (setq final-line (concat final-line "c" divider))))
1711 final-line))
1713 (format "%s.TE\n"
1714 (let ((final-line "")
1715 (long-line "")
1716 (lines (org-split-string contents "\n")))
1718 (dolist (line-item lines)
1719 (setq long-line "")
1721 (if long-cells
1722 (progn
1723 (if (string= line-item "_")
1724 (setq long-line (format "%s\n" line-item))
1725 ;; else string =
1726 (let ((cell-item-list (org-split-string line-item "\t")))
1727 (dolist (cell-item cell-item-list)
1729 (cond ((eq cell-item (car (last cell-item-list)))
1730 (setq long-line (concat long-line
1731 (format "T{\n%s\nT}\t\n" cell-item))))
1733 (setq long-line (concat long-line
1734 (format "T{\n%s\nT}\t" cell-item))))))
1735 long-line))
1736 ;; else long cells
1737 (setq final-line (concat final-line long-line)))
1739 (setq final-line (concat final-line line-item "\n"))))
1740 final-line))
1742 (if caption (format ".TB \"%s\"" caption) ""))))))
1744 ;;; Table Cell
1746 (defun org-groff-table-cell (table-cell contents info)
1747 "Transcode a TABLE-CELL element from Org to Groff
1748 CONTENTS is the cell contents. INFO is a plist used as
1749 a communication channel."
1750 (progn
1751 (concat (if (and contents
1752 org-groff-table-scientific-notation
1753 (string-match orgtbl-exp-regexp contents))
1754 ;; Use appropriate format string for scientific
1755 ;; notation.
1756 (format org-groff-table-scientific-notation
1757 (match-string 1 contents)
1758 (match-string 2 contents))
1759 contents)
1760 (when (org-export-get-next-element table-cell info) "\t"))))
1763 ;;; Table Row
1765 (defun org-groff-table-row (table-row contents info)
1766 "Transcode a TABLE-ROW element from Org to Groff
1767 CONTENTS is the contents of the row. INFO is a plist used as
1768 a communication channel."
1769 ;; Rules are ignored since table separators are deduced from
1770 ;; borders of the current row.
1771 (when (eq (org-element-property :type table-row) 'standard)
1772 (let* ((attr (mapconcat 'identity
1773 (org-element-property
1774 :attr_groff (org-export-get-parent table-row))
1775 " "))
1776 ;; TABLE-ROW's borders are extracted from its first cell.
1777 (borders
1778 (org-export-table-cell-borders
1779 (car (org-element-contents table-row)) info)))
1780 (concat
1781 ;; Mark horizontal lines
1782 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1783 contents
1784 (cond
1785 ;; When BOOKTABS are activated enforce bottom rule even when
1786 ;; no hline was specifically marked.
1787 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1788 ((memq 'below borders) "\n_"))))))
1790 ;;; Target
1792 (defun org-groff-target (target contents info)
1793 "Transcode a TARGET object from Org to Groff.
1794 CONTENTS is nil. INFO is a plist holding contextual
1795 information."
1796 (format "\\fI%s\\fP"
1797 (org-export-solidify-link-text (org-element-property :value target))))
1799 ;;; Timestamp
1801 (defun org-groff-timestamp (timestamp contents info)
1802 "Transcode a TIMESTAMP object from Org to Groff.
1803 CONTENTS is nil. INFO is a plist holding contextual
1804 information."
1805 (let ((value (org-groff-plain-text
1806 (org-timestamp-translate timestamp) info)))
1807 (case (org-element-property :type timestamp)
1808 ((active active-range)
1809 (format org-groff-active-timestamp-format value))
1810 ((inactive inactive-range)
1811 (format org-groff-inactive-timestamp-format value))
1812 (t (format org-groff-diary-timestamp-format value)))))
1814 ;;; Underline
1816 (defun org-groff-underline (underline contents info)
1817 "Transcode UNDERLINE from Org to Groff.
1818 CONTENTS is the text with underline markup. INFO is a plist
1819 holding contextual information."
1820 (org-groff--text-markup contents 'underline))
1822 ;;; Verbatim
1824 (defun org-groff-verbatim (verbatim contents info)
1825 "Transcode a VERBATIM object from Org to Groff.
1826 CONTENTS is nil. INFO is a plist used as a communication
1827 channel."
1828 (org-groff--text-markup (org-element-property :value verbatim) 'verbatim))
1830 ;;; Verse Block
1832 (defun org-groff-verse-block (verse-block contents info)
1833 "Transcode a VERSE-BLOCK element from Org to Groff.
1834 CONTENTS is verse block contents. INFO is a plist holding
1835 contextual information."
1836 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
1839 ;;; Interactive functions
1841 (defun org-groff-export-to-groff
1842 (&optional async subtreep visible-only body-only ext-plist)
1843 "Export current buffer to a Groff file.
1845 If narrowing is active in the current buffer, only export its
1846 narrowed part.
1848 If a region is active, export that region.
1850 A non-nil optional argument ASYNC means the process should happen
1851 asynchronously. The resulting file should be accessible through
1852 the `org-export-stack' interface.
1854 When optional argument SUBTREEP is non-nil, export the sub-tree
1855 at point, extracting information from the headline properties
1856 first.
1858 When optional argument VISIBLE-ONLY is non-nil, don't export
1859 contents of hidden elements.
1861 EXT-PLIST, when provided, is a property list with external
1862 parameters overriding Org default settings, but still inferior to
1863 file-local settings.
1865 Return output file's name."
1866 (interactive)
1867 (let ((outfile (org-export-output-file-name ".groff" subtreep))
1868 (org-groff-registered-references nil)
1869 (org-groff-special-content nil))
1870 (org-export-to-file 'groff outfile
1871 async subtreep visible-only body-only ext-plist)))
1873 (defun org-groff-export-to-pdf
1874 (&optional async subtreep visible-only body-only ext-plist)
1875 "Export current buffer to Groff then process through to PDF.
1877 If narrowing is active in the current buffer, only export its
1878 narrowed part.
1880 If a region is active, export that region.
1882 A non-nil optional argument ASYNC means the process should happen
1883 asynchronously. The resulting file should be accessible through
1884 the `org-export-stack' interface.
1886 When optional argument SUBTREEP is non-nil, export the sub-tree
1887 at point, extracting information from the headline properties
1888 first.
1890 When optional argument VISIBLE-ONLY is non-nil, don't export
1891 contents of hidden elements.
1893 EXT-PLIST, when provided, is a property list with external
1894 parameters overriding Org default settings, but still inferior to
1895 file-local settings.
1897 Return PDF file's name."
1898 (interactive)
1899 (let ((outfile (org-export-output-file-name ".groff" subtreep)))
1900 (org-export-to-file 'groff outfile
1901 async subtreep visible-only body-only ext-plist
1902 (lambda (file) (org-groff-compile file)))))
1904 (defun org-groff-compile (file)
1905 "Compile a Groff file.
1907 FILE is the name of the file being compiled. Processing is done
1908 through the command specified in `org-groff-pdf-process'.
1910 Return PDF file name or an error if it couldn't be produced."
1911 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1912 (full-name (file-truename file))
1913 (out-dir (file-name-directory file))
1914 ;; Properly set working directory for compilation.
1915 (default-directory (if (file-name-absolute-p file)
1916 (file-name-directory full-name)
1917 default-directory))
1918 errors)
1919 (message (format "Processing Groff file %s ..." file))
1920 (save-window-excursion
1921 (cond
1922 ;; A function is provided: Apply it.
1923 ((functionp org-groff-pdf-process)
1924 (funcall org-groff-pdf-process (shell-quote-argument file)))
1925 ;; A list is provided: Replace %b, %f and %o with appropriate
1926 ;; values in each command before applying it. Output is
1927 ;; redirected to "*Org PDF Groff Output*" buffer.
1928 ((consp org-groff-pdf-process)
1929 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1930 (mapc
1931 (lambda (command)
1932 (shell-command
1933 (replace-regexp-in-string
1934 "%b" (shell-quote-argument base-name)
1935 (replace-regexp-in-string
1936 "%f" (shell-quote-argument full-name)
1937 (replace-regexp-in-string
1938 "%o" (shell-quote-argument out-dir) command t t)
1939 t t) t t)
1940 outbuf))
1941 org-groff-pdf-process)
1942 ;; Collect standard errors from output buffer.
1943 (setq errors (org-groff-collect-errors outbuf))))
1944 (t (error "No valid command to process to PDF")))
1945 (let ((pdffile (concat out-dir base-name ".pdf")))
1946 ;; Check for process failure. Provide collected errors if
1947 ;; possible.
1948 (if (not (file-exists-p pdffile))
1949 (error (concat (format "PDF file %s wasn't produced" pdffile)
1950 (when errors (concat ": " errors))))
1951 ;; Else remove log files, when specified, and signal end of
1952 ;; process to user, along with any error encountered.
1953 (when org-groff-remove-logfiles
1954 (dolist (ext org-groff-logfiles-extensions)
1955 (let ((file (concat out-dir base-name "." ext)))
1956 (when (file-exists-p file) (delete-file file)))))
1957 (message (concat "Process completed"
1958 (if (not errors) "."
1959 (concat " with errors: " errors)))))
1960 ;; Return output file name.
1961 pdffile))))
1963 (defun org-groff-collect-errors (buffer)
1964 "Collect some kind of errors from \"groff\" output
1965 BUFFER is the buffer containing output.
1966 Return collected error types as a string, or nil if there was
1967 none."
1968 (with-current-buffer buffer
1969 (save-excursion
1970 (goto-char (point-max))
1971 ;; Find final run
1972 nil)))
1975 (provide 'ox-groff)
1976 ;;; ox-groff.el ends here