1 ;; org-e-groff.el --- Groff Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 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
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This library implements a Groff Memorandum Macro back-end for
25 ;; Org generic exporter.
29 ;; M-: (org-export-to-buffer 'e-groff "*Test e-Groff*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Groff
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
36 ;; "GROFF_CLASS_OPTIONS".
40 (eval-when-compile (require 'cl
))
43 (defvar orgtbl-exp-regexp
)
48 (org-export-define-backend e-groff
49 ((bold . org-e-groff-bold
)
50 (center-block . org-e-groff-center-block
)
51 (clock . org-e-groff-clock
)
52 (code . org-e-groff-code
)
53 (drawer . org-e-groff-drawer
)
54 (dynamic-block . org-e-groff-dynamic-block
)
55 (entity . org-e-groff-entity
)
56 (example-block . org-e-groff-example-block
)
57 (export-block . org-e-groff-export-block
)
58 (export-snippet . org-e-groff-export-snippet
)
59 (fixed-width . org-e-groff-fixed-width
)
60 (footnote-definition . org-e-groff-footnote-definition
)
61 (footnote-reference . org-e-groff-footnote-reference
)
62 (headline . org-e-groff-headline
)
63 (horizontal-rule . org-e-groff-horizontal-rule
)
64 (inline-src-block . org-e-groff-inline-src-block
)
65 (inlinetask . org-e-groff-inlinetask
)
66 (italic . org-e-groff-italic
)
67 (item . org-e-groff-item
)
68 (keyword . org-e-groff-keyword
)
69 ;; (latex-environment . org-e-groff-latex-environment)
70 ;; (latex-fragment . org-e-groff-latex-fragment)
71 (line-break . org-e-groff-line-break
)
72 (link . org-e-groff-link
)
73 (paragraph . org-e-groff-paragraph
)
74 (plain-list . org-e-groff-plain-list
)
75 (plain-text . org-e-groff-plain-text
)
76 (planning . org-e-groff-planning
)
77 (property-drawer . org-e-groff-property-drawer
)
78 (quote-block . org-e-groff-quote-block
)
79 (quote-section . org-e-groff-quote-section
)
80 (radio-target . org-e-groff-radio-target
)
81 (section . org-e-groff-section
)
82 (special-block . org-e-groff-special-block
)
83 (src-block . org-e-groff-src-block
)
84 (statistics-cookie . org-e-groff-statistics-cookie
)
85 (strike-through . org-e-groff-strike-through
)
86 (subscript . org-e-groff-subscript
)
87 (superscript . org-e-groff-superscript
)
88 (table . org-e-groff-table
)
89 (table-cell . org-e-groff-table-cell
)
90 (table-row . org-e-groff-table-row
)
91 (target . org-e-groff-target
)
92 (template . org-e-groff-template
)
93 (timestamp . org-e-groff-timestamp
)
94 (underline . org-e-groff-underline
)
95 (verbatim . org-e-groff-verbatim
)
96 (verse-block . org-e-groff-verse-block
))
100 ((?g
"As GROFF file" org-e-groff-export-to-groff
)
101 (?p
"As PDF file" org-e-groff-export-to-pdf
)
102 (?o
"As PDF file and open"
103 (lambda (s v b
) (org-open-file (org-e-groff-export-to-pdf s v b
))))))
105 ((:date
"DATE" nil org-e-groff-date-format t
)
106 (:groff-class
"GROFF_CLASS" nil org-e-groff-default-class t
)
107 (:groff-class-options
"GROFF_CLASS_OPTIONS" nil nil t
)
108 (:groff-header-extra
"GROFF_HEADER" nil nil newline
)))
112 ;;; User Configurable Variables
114 (defgroup org-export-e-groff nil
115 "Options for exporting Org mode files to Groff."
116 :tag
"Org Export Groff"
121 (defcustom org-e-groff-default-class
"internal"
122 "The default Groff class."
123 :group
'org-export-e-groff
124 :type
'(string :tag
"Groff class"))
126 (defcustom org-e-groff-classes
128 (:heading
'default
:type
"memo" :last-section
"toc"))
130 (:heading
'default
:type
"memo" :last-section
"toc"))
131 ("programmer" ".MT 2"
132 (:heading
'default
:type
"memo" :last-section
"toc"))
134 (:heading
'default
:type
"memo" :last-section
"toc"))
136 (:heading
'default
:type
"memo" :last-section
"toc"))
138 (:heading
'default
:type
"memo" :last-section
"sign"))
140 (:heading custom-function
:type
"custom" :last-section
"toc"))
142 (:heading
'default
:type
"memo"))
144 (:heading
'default
:type
"cover" :last-section
"toc"))
146 (:heading
'default
:type
"cover" :last-section
"toc"))
148 (:heading
'default
:type
"letter" :last-section
"sign"))
150 (:heading
'default
:type
"letter" :last-section
"sign"))
152 (:heading
'default
:type
"letter" :last-section
"sign"))
154 (:heading
'default
:type
"letter" :last-section
"sign"))
155 ("none" "" (:heading
'default
:type
"custom")))
157 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
158 ;; This is to facilitate the creation of custom pages.
160 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
161 ;; are made. This is to facilitate Abstract Insertion.
163 "This list describes the attributes for the documents being created.
164 It allows for the creation of new "
165 :group
'org-export-e-groff
167 (list (string :tag
"Document Type")
168 (string :tag
"Header")
169 (repeat :tag
"Options" :inline t
171 (list :tag
"Heading")
172 (function :tag
"Hook computing sectioning"))))))
175 (defcustom org-e-groff-date-format
176 (format-time-string "%Y-%m-%d")
177 "Format string for .ND "
178 :group
'org-export-e-groff
183 (defconst org-e-groff-special-tags
184 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
186 (defcustom org-e-groff-format-headline-function nil
187 "Function to format headline text.
189 This function will be called with 5 arguments:
190 TODO the todo keyword (string or nil).
191 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
192 PRIORITY the priority of the headline (integer or nil)
193 TEXT the main headline text (string).
194 TAGS the tags as a list of strings (list of strings or nil).
196 The function result will be used in the section format string.
198 As an example, one could set the variable to the following, in
199 order to reproduce the default set-up:
201 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
202 \"Default format function for an headline.\"
204 \(format \"\\fB%s\\fP \" todo))
206 \(format \"[\\#%c] \" priority))
210 \(mapconcat 'identity tags \":\"))))"
211 :group
'org-export-e-groff
216 (defcustom org-e-groff-active-timestamp-format
"\\fI%s\\fP"
217 "A printf format string to be applied to active timestamps."
218 :group
'org-export-e-groff
221 (defcustom org-e-groff-inactive-timestamp-format
"\\fI%s\\fP"
222 "A printf format string to be applied to inactive timestamps."
223 :group
'org-export-e-groff
226 (defcustom org-e-groff-diary-timestamp-format
"\\fI%s\\fP"
227 "A printf format string to be applied to diary timestamps."
228 :group
'org-export-e-groff
233 (defcustom org-e-groff-inline-image-rules
234 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
235 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
236 "Rules characterizing image files that can be inlined into Groff.
238 A rule consists in an association whose key is the type of link
239 to consider, and value is a regexp that will be matched against
242 Note that, by default, the image extensions actually allowed
243 depend on the way the Groff file is processed. When used with
244 pdfgroff, pdf, jpg and png images are OK. When processing
245 through dvi to Postscript, only ps and eps are allowed. The
246 default we use here encompasses both."
247 :group
'org-export-e-groff
248 :type
'(alist :key-type
(string :tag
"Type")
249 :value-type
(regexp :tag
"Path")))
251 (defcustom org-e-groff-link-with-unknown-path-format
"\\fI%s\\fP"
252 "Format string for links with unknown path type."
253 :group
'org-export-groff
258 (defcustom org-e-groff-tables-centered t
259 "When non-nil, tables are exported in a center environment."
260 :group
'org-export-e-groff
263 (defcustom org-e-groff-tables-verbatim nil
264 "When non-nil, tables are exported verbatim."
265 :group
'org-export-e-groff
268 (defcustom org-e-groff-table-scientific-notation
"%sE%s"
269 "Format string to display numbers in scientific notation.
270 The format should have \"%s\" twice, for mantissa and exponent
271 \(i.e. \"%s\\\\times10^{%s}\").
273 When nil, no transformation is made."
274 :group
'org-export-e-groff
276 (string :tag
"Format string")
277 (const :tag
"No formatting")))
281 (defcustom org-e-groff-text-markup-alist
282 '((bold .
"\\fB%s\\fP")
283 (code .
"\\fC%s\\fP")
284 (italic .
"\\fI%s\\fP")
285 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
286 (underline .
"\\fI%s\\fP") ; need to be revised.
287 (verbatim .
"protectedtexttt"))
288 "Alist of Groff expressions to convert text markup.
290 The key must be a symbol among `bold', `code', `italic',
291 `strike-through', `underline' and `verbatim'. The value is
292 a formatting string to wrap fontified text with it.
294 If no association can be found for a given markup, text will be
296 :group
'org-export-e-groff
298 :options
'(bold code italic strike-through underline verbatim
))
302 (defcustom org-e-groff-format-drawer-function nil
303 "Function called to format a drawer in Groff code.
305 The function must accept two parameters:
306 NAME the drawer name, like \"LOGBOOK\"
307 CONTENTS the contents of the drawer.
309 The function should return the string to be exported.
311 For example, the variable could be set to the following function
312 in order to mimic default behaviour:
314 \(defun org-e-groff-format-drawer-default \(name contents\)
315 \"Format a drawer element for Groff export.\"
317 :group
'org-export-e-groff
322 (defcustom org-e-groff-format-inlinetask-function nil
323 "Function called to format an inlinetask in Groff code.
325 The function must accept six parameters:
326 TODO the todo keyword, as a string
327 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
328 PRIORITY the inlinetask priority, as a string
329 NAME the inlinetask name, as a string.
330 TAGS the inlinetask tags, as a list of strings.
331 CONTENTS the contents of the inlinetask, as a string.
333 The function should return the string to be exported.
335 For example, the variable could be set to the following function
336 in order to mimic default behaviour:
338 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
339 \"Format an inline task element for Groff export.\"
343 \(format \"\\fB%s\\fP \" todo))
344 \(when priority (format \"[\\#%c] \" priority))
348 \(mapconcat 'identity tags \":\")))))
349 \(format (concat \".DS L\\n\"
353 full-title contents))"
354 :group
'org-export-e-groff
359 (defcustom org-e-groff-source-highlight nil
360 "Use GNU source highlight to embellish source blocks "
361 :group
'org-export-e-groff
364 (defcustom org-e-groff-source-highlight-langs
365 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
367 (c "c") (cc "cpp") (csharp "csharp") (d "d")
368 (fortran "fortran") (cobol "cobol") (pascal "pascal")
369 (ada "ada") (asm "asm")
370 (perl "perl") (cperl "perl")
371 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
372 (java "java") (javascript "javascript")
374 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
375 (ocaml "caml") (caml "caml")
376 (sql "sql") (sqlite "sql")
377 (html "html") (css "css") (xml "xml")
378 (bat "bat") (bison "bison") (clipper "clipper")
379 (ldap "ldap") (opa "opa")
380 (php "php") (postscript "postscript") (prolog "prolog")
381 (properties "properties") (makefile "makefile")
382 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
383 "Alist mapping languages to their listing language counterpart.
384 The key is a symbol, the major mode symbol without the \"-mode\".
385 The value is the string that should be inserted as the language
386 parameter for the listings package. If the mode name and the
387 listings name are the same, the language does not need an entry
388 in this list - but it does not hurt if it is present."
389 :group
'org-export-e-groff
392 (symbol :tag
"Major mode ")
393 (string :tag
"Listings language"))))
395 (defcustom org-e-groff-source-highlight-options nil
396 "Association list of options for the groff listings package.
398 These options are supplied as a comma-separated list to the
399 \\lstset command. Each element of the association list should be
400 a list containing two strings: the name of the option, and the
403 (setq org-e-groff-source-highlight-options
404 '((\"basicstyle\" \"\\small\")
405 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
407 will typeset the code in a small size font with underlined, bold
410 Note that the same options will be applied to blocks of all
412 :group
'org-export-e-groff
415 (string :tag
"Listings option name ")
416 (string :tag
"Listings option value"))))
418 (defvar org-e-groff-custom-lang-environments nil
419 "Alist mapping languages to language-specific Groff environments.
421 It is used during export of src blocks by the listings and
422 groff packages. For example,
424 \(setq org-e-groff-custom-lang-environments
425 '\(\(python \"pythoncode\"\)\)\)
427 would have the effect that if org encounters begin_src python
428 during groff export it will use pythoncode as the source-highlight
433 (defcustom org-e-groff-special-char
434 '(("(c)" .
"\\\\(co")
436 ("(rg)" .
"\\\\(rg"))
437 "CONS list in which the value of the car
438 is replace on the value of the CDR. "
439 :group
'org-export-e-groff
441 (cons :tag
"Character Subtitute"
442 (string :tag
"Original Character Group")
443 (string :tag
"Replacement Character"))))
447 (defcustom org-e-groff-pdf-process
448 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
449 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
450 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
452 "Commands to process a Groff file to a PDF file.
453 This is a list of strings, each of them will be given to the
454 shell as a command. %f in the command will be replaced by the
455 full file name, %b by the file base name \(i.e. without
456 extension) and %o by the base directory of the file."
457 :group
'org-export-pdf
459 (repeat :tag
"Shell command sequence"
460 (string :tag
"Shell command"))
461 (const :tag
"2 runs of pdfgroff"
462 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
463 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
464 (const :tag
"3 runs of pdfgroff"
465 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
466 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
467 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
470 (defcustom org-e-groff-logfiles-extensions
471 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
472 "The list of file extensions to consider as Groff logfiles."
473 :group
'org-export-e-groff
474 :type
'(repeat (string :tag
"Extension")))
476 (defcustom org-e-groff-remove-logfiles t
477 "Non-nil means remove the logfiles produced by PDF production.
478 These are the .aux, .log, .out, and .toc files."
479 :group
'org-export-e-groff
482 (defcustom org-e-groff-organization
"Org User"
483 "Name of the organization used to populate the .AF command."
484 :group
'org-export-e-groff
487 (defcustom org-e-groff-raster-to-ps nil
488 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
489 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
490 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
491 :group
'org-export-e-groff
493 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
494 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
495 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
496 (const :tag
"None" nil
)))
498 (defvar org-e-groff-registered-references nil
)
499 (defvar org-e-groff-special-content nil
)
503 ;;; Internal Functions
505 (defun org-e-groff--caption/label-string
(element info
)
506 "Return caption and label Groff string for ELEMENT.
508 INFO is a plist holding contextual information. If there's no
509 caption nor label, return the empty string.
511 For non-floats, see `org-e-groff--wrap-label'."
512 (let ((main (org-export-get-caption element
))
513 (short (org-export-get-caption element t
))
514 (label (org-element-property :name element
)))
515 (cond ((and (not main
) (not label
)) "")
516 ((not main
) (format "\\fI%s\\fP" label
))
517 ;; Option caption format with short name.
518 (short (format "%s\n.br\n - %s\n"
519 (org-export-data short info
)
520 (org-export-data main info
)))
521 ;; Standard caption format.
522 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
524 (defun org-e-groff--wrap-label (element output
)
525 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
526 This function shouldn't be used for floats. See
527 `org-e-groff--caption/label-string'."
528 (let ((label (org-element-property :name element
)))
529 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
531 (concat (format "%s\n.br\n" label
) output
))))
533 (defun org-e-groff--text-markup (text markup
)
534 "Format TEXT depending on MARKUP text markup.
535 See `org-e-groff-text-markup-alist' for details."
536 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist
))))
538 ;; No format string: Return raw text.
540 ((string= "protectedtexttt" fmt
)
542 (trans '(("\\" .
"\\")))
545 (while (string-match "[\\{}$%&_#~^]" text
)
546 (setq char
(match-string 0 text
))
547 (if (> (match-beginning 0) 0)
548 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
549 (setq text
(substring text
(1+ (match-beginning 0))))
550 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
551 rtn
(concat rtn char
)))
552 (setq text
(concat rtn text
))
553 (format "\\fC%s\\fP" text
)))
554 ;; Else use format string.
555 (t (format fmt text
)))))
558 (defun org-e-groff--get-tagged-content (tag info
)
559 (cdr (assoc tag org-e-groff-special-content
)))
561 (defun org-e-groff--mt-head (title contents attr info
)
564 ;; 1. Insert Organization
565 (let ((firm-option (plist-get attr
:firm
)))
567 ((stringp firm-option
)
568 (format ".AF \"%s\" \n" firm-option
))
569 (t (format ".AF \"%s\" \n" (or org-e-groff-organization
"")))))
572 (let ((subtitle1 (plist-get attr
:subtitle1
))
573 (subtitle2 (plist-get attr
:subtitle2
)))
577 (format ".TL \"%s\" \"%s\" \n%s\n"
579 (or subtitle2
"") " "))
581 ((not (or subtitle1 subtitle2
))
585 (format ".TL \"%s\" \"%s \" \n%s\n"
587 (or subtitle2
"") title
))))
590 ;; In Groff, .AU *MUST* be placed after .TL
591 ;; If From, populate with data from From else
593 (let ((author (and (plist-get info
:with-author
)
594 (let ((auth (plist-get info
:author
)))
595 (and auth
(org-export-data auth info
)))))
596 (email (and (plist-get info
:with-email
)
597 (org-export-data (plist-get info
:email
) info
)))
598 (from-data (org-e-groff--get-tagged-content "FROM" info
))
600 (to-data (org-e-groff--get-tagged-content "TO" info
)))
603 ((and author from-data
)
607 (format " \"%s\" " from-line
))
610 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
613 (format ".AU \"%s\" " author
) au-line
"\n")))
615 ((and author email
(not (string= "" email
)))
616 (format ".AU \"%s\" \"%s\"\n" author email
))
618 (author (format ".AU \"%s\"\n" author
))
623 ;; 4. Author Title, if present
624 (let ((at-item (plist-get attr
:author-title
)))
625 (if (and at-item
(stringp at-item
))
626 (format ".AT \"%s\" \n" at-item
)
630 (let ((date (org-export-data (plist-get info
:date
) info
)))
631 (and date
(format ".ND \"%s\"\n" date
)))
634 ;; If Abstract, then Populate Abstract
637 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info
))
638 (to-data (org-e-groff--get-tagged-content "TO" info
)))
641 (format ".AS\n%s\n.AE\n" abstract-data
))
643 (format ".AS\n%s\n.AE\n" to-data
))))))
645 (defun org-e-groff--letter-head (title contents attr info
)
646 (let ((author (and (plist-get info
:with-author
)
647 (let ((auth (plist-get info
:author
)))
648 (and auth
(org-export-data auth info
)))))
649 (email (and (plist-get info
:with-email
)
650 (org-export-data (plist-get info
:email
) info
)))
651 (from-data (org-e-groff--get-tagged-content "FROM" info
))
652 (at-item (plist-get attr
:author-title
))
653 (to-data (org-e-groff--get-tagged-content "TO" info
)))
656 ;; If FROM then get data from FROM
659 (replace-regexp-in-string "\\.P\n" "" from-data
))
664 (replace-regexp-in-string "\\.P\n" "" to-data
))
670 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
671 ((and author email
(not (string= "" email
)))
672 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
673 (author (format ".WA \"%s\"\n.WE\n" author
))
674 (t ".WA \"\" \n.WE\n"))
676 ;; If TO then get data from TO
679 (format ".IA \n%s\n.IE\n" to-data
)))))
684 (defun org-e-groff-template (contents info
)
685 "Return complete document string after Groff conversion.
686 CONTENTS is the transcoded contents string. INFO is a plist
687 holding export options."
688 (let* ((title (org-export-data (plist-get info
:title
) info
))
693 (list (plist-get info
:groff-class-options
))
695 (class (plist-get info
:groff-class
))
696 (class-options (plist-get info
:groff-class-options
))
697 (classes (assoc class org-e-groff-classes
))
698 (classes-options (car (last classes
)))
699 (heading-option (plist-get classes-options
:heading
))
700 (type-option (plist-get classes-options
:type
))
701 (last-option (plist-get classes-options
:last-section
))
702 (hyphenate (plist-get attr
:hyphenate
))
703 (justify-right (plist-get attr
:justify-right
))
705 (document-class-string
707 (org-element-normalize-string
708 (let* ((header (nth 1 (assoc class org-e-groff-classes
)))
709 (document-class-item (if (stringp header
) header
"")))
710 document-class-item
)))))
729 ((string= type-option
"custom") "")
731 ((and (stringp document-class-string
)
732 (string= type-option
"cover"))
735 (format ".COVER %s\n" document-class-string
)
736 (org-e-groff--mt-head title contents attr info
)
739 ((string= type-option
"memo")
741 (org-e-groff--mt-head title contents attr info
)
742 document-class-string
))
743 ((string= type-option
"letter")
745 (org-e-groff--letter-head title contents attr info
)
746 (let ((sa-item (plist-get attr
:salutation
))
747 (cn-item (plist-get attr
:confidential
))
748 (sj-item (plist-get attr
:subject
))
749 (rn-item (plist-get attr
:reference
))
750 (at-item (plist-get attr
:attention
)))
754 (if (stringp sa-item
)
755 (format ".LO SA \"%s\" \n" sa-item
)
759 (if (stringp cn-item
)
760 (format ".LO CN \"%s\"\n" cn-item
)
763 (when (and at-item
(stringp at-item
))
764 (format ".LO AT \"%s\" \n" at-item
))
765 (when (and title rn-item
)
766 (format ".LO RN \"%s\"\n" title
))
768 (when (and sj-item
(stringp sj-item
))
769 (format ".LO SJ \"%s\" \n" sj-item
))
772 ".LT " document-class-string
"\n"))))
779 ((string= last-option
"toc")
781 ((string= last-option
"sign")
782 (let ((fc-item (plist-get attr
:closing
)))
783 (concat (if (stringp fc-item
)
784 (format ".FC \"%s\" \n" fc-item
)
792 (when (string= (car item
) "NS")
793 (replace-regexp-in-string
794 "\\.P\n" "" (cdr item
))))
795 (reverse org-e-groff-special-content
) "\n")))))
799 ;;; Transcode Functions
803 ;; Babel Calls are ignored.
808 (defun org-e-groff-bold (bold contents info
)
809 "Transcode BOLD from Org to Groff.
810 CONTENTS is the text with bold markup. INFO is a plist holding
811 contextual information."
812 (org-e-groff--text-markup contents
'bold
))
816 (defun org-e-groff-center-block (center-block contents info
)
817 "Transcode a CENTER-BLOCK element from Org to Groff.
818 CONTENTS holds the contents of the center block. INFO is a plist
819 holding contextual information."
820 (org-e-groff--wrap-label
822 (format ".DS C \n%s\n.DE" contents
)))
826 (defun org-e-groff-clock (clock contents info
)
827 "Transcode a CLOCK element from Org to Groff.
828 CONTENTS is nil. INFO is a plist holding contextual
831 (format "\\fB%s\\fP " org-clock-string
)
832 (format org-e-groff-inactive-timestamp-format
833 (concat (org-translate-time (org-element-property :value clock
))
834 (let ((time (org-element-property :time clock
)))
835 (and time
(format " (%s)" time
)))))))
839 (defun org-e-groff-code (code contents info
)
840 "Transcode a CODE object from Org to Groff.
841 CONTENTS is nil. INFO is a plist used as a communication
843 (org-e-groff--text-markup (org-element-property :value code
) 'code
))
845 ;;; Comments and Comment Blocks are ignored.
849 (defun org-e-groff-drawer (drawer contents info
)
850 "Transcode a DRAWER element from Org to Groff.
851 CONTENTS holds the contents of the block. INFO is a plist
852 holding contextual information."
853 (let* ((name (org-element-property :drawer-name drawer
))
854 (output (if (functionp org-e-groff-format-drawer-function
)
855 (funcall org-e-groff-format-drawer-function
857 ;; If there's no user defined function: simply
858 ;; display contents of the drawer.
860 (org-e-groff--wrap-label drawer output
)))
864 (defun org-e-groff-dynamic-block (dynamic-block contents info
)
865 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
866 CONTENTS holds the contents of the block. INFO is a plist
867 holding contextual information. See `org-export-data'."
868 (org-e-groff--wrap-label dynamic-block contents
))
872 (defun org-e-groff-entity (entity contents info
)
873 "Transcode an ENTITY object from Org to Groff.
874 CONTENTS are the definition itself. INFO is a plist holding
875 contextual information."
876 (org-element-property :utf-8 entity
))
880 (defun org-e-groff-example-block (example-block contents info
)
881 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
882 CONTENTS is nil. INFO is a plist holding contextual
884 (org-e-groff--wrap-label
886 (format ".DS L\n%s\n.DE"
887 (org-export-format-code-default example-block info
))))
891 (defun org-e-groff-export-block (export-block contents info
)
892 "Transcode a EXPORT-BLOCK element from Org to Groff.
893 CONTENTS is nil. INFO is a plist holding contextual information."
894 (when (string= (org-element-property :type export-block
) "GROFF")
895 (org-remove-indentation (org-element-property :value export-block
))))
899 (defun org-e-groff-export-snippet (export-snippet contents info
)
900 "Transcode a EXPORT-SNIPPET object from Org to Groff.
901 CONTENTS is nil. INFO is a plist holding contextual information."
902 (when (eq (org-export-snippet-backend export-snippet
) 'e-groff
)
903 (org-element-property :value export-snippet
)))
907 (defun org-e-groff-fixed-width (fixed-width contents info
)
908 "Transcode a FIXED-WIDTH element from Org to Groff.
909 CONTENTS is nil. INFO is a plist holding contextual information."
910 (org-e-groff--wrap-label
912 (format "\\fC\n%s\\fP"
913 (org-remove-indentation
914 (org-element-property :value fixed-width
)))))
916 ;;; Footnote Definition
918 ;; Footnote Definitions are ignored.
920 ;; Footnotes are handled automatically in GROFF. Although manual
921 ;; references can be added, not really required.
923 (defun org-e-groff-footnote-reference (footnote-reference contents info
)
924 ;; Changing from info to footnote-reference
925 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
926 (n (org-export-get-footnote-number footnote-reference info
))
927 (data (org-trim (org-export-data raw info
)))
928 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
930 (if (string-match "fn:rl" ref-id
)
931 (if (member ref-id org-e-groff-registered-references
)
932 (format "\\*[%s]" ref-id
)
934 (push ref-id org-e-groff-registered-references
)
935 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
936 ;; else it is a footnote
937 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
941 (defun org-e-groff-headline (headline contents info
)
942 "Transcode an HEADLINE element from Org to Groff.
943 CONTENTS holds the contents of the headline. INFO is a plist
944 holding contextual information."
945 (let* ((class (plist-get info
:groff-class
))
946 (level (org-export-get-relative-level headline info
))
947 (numberedp (org-export-numbered-headline-p headline info
))
948 ;; Section formatting will set two placeholders: one for the
949 ;; title and the other for the contents.
950 (classes (assoc class org-e-groff-classes
))
951 (classes-options (car (last classes
)))
952 (heading-option (plist-get classes-options
:heading
))
956 ((and (symbolp heading-option
)
957 (fboundp heading-option
))
958 (funcall heading-option level numberedp
))
961 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
962 ".HU \"%s\"\n%s")))))
963 ;; End of section-fmt
964 (text (org-export-data (org-element-property :title headline
) info
))
966 (and (plist-get info
:with-todo-keywords
)
967 (let ((todo (org-element-property :todo-keyword headline
)))
968 (and todo
(org-export-data todo info
)))))
969 (todo-type (and todo
(org-element-property :todo-type headline
)))
970 (tags (and (plist-get info
:with-tags
)
971 (org-export-get-tags headline info
)))
972 (priority (and (plist-get info
:with-priority
)
973 (org-element-property :priority headline
)))
974 ;; Create the headline text along with a no-tag version. The
975 ;; latter is required to remove tags from table of contents.
976 (full-text (if (functionp org-e-groff-format-headline-function
)
977 ;; User-defined formatting function.
978 (funcall org-e-groff-format-headline-function
979 todo todo-type priority text tags
)
980 ;; Default formatting.
983 (format "\\fB%s\\fP " todo
))
984 (when priority
(format " [\\#%c] " priority
))
987 (format " \\fC:%s:\\fP "
988 (mapconcat 'identity tags
":"))))))
990 (if (functionp org-e-groff-format-headline-function
)
991 ;; User-defined formatting function.
992 (funcall org-e-groff-format-headline-function
993 todo todo-type priority text nil
)
994 ;; Default formatting.
996 (when todo
(format "\\fB%s\\fP " todo
))
997 (when priority
(format " [\\#%c] " priority
))
999 ;; Associate some \label to the headline for internal links.
1001 ;; (format "\\label{sec-%s}\n"
1002 ;; (mapconcat 'number-to-string
1003 ;; (org-export-get-headline-number headline info)
1007 (make-string (org-element-property :pre-blank headline
) 10)))
1010 ;; Case 1: Special Tag
1011 ((member (car tags
) org-e-groff-special-tags
)
1013 ((string= (car tags
) "BODY") contents
)
1015 ((string= (car tags
) "NS")
1017 (push (cons (car tags
)
1018 (format ".NS \"%s\" 1 \n%s"
1019 (car (org-element-property :title headline
))
1021 org-e-groff-special-content
) nil
))
1025 (push (cons (car tags
) contents
) org-e-groff-special-content
)
1028 ;; Case 2: This is a footnote section: ignore it.
1029 ((org-element-property :footnote-section-p headline
) nil
)
1031 ;; Case 3: This is a deep sub-tree: export it as a list item.
1032 ;; Also export as items headlines for which no section
1033 ;; format has been found.
1034 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1035 ;; Build the real contents of the sub-tree.
1036 (let ((low-level-body
1038 ;; If the headline is the first sibling, start a list.
1039 (when (org-export-first-sibling-p headline info
)
1040 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1042 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1043 ;; If headline is not the last sibling simply return
1044 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1046 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1047 (replace-regexp-in-string
1052 ;; Case 4. Standard headline. Export it as a section.
1054 (format section-fmt full-text
1055 (concat headline-label pre-blanks contents
))))))
1060 ;;; Inline Babel Call
1062 ;; Inline Babel Calls are ignored.
1064 ;;; Inline Src Block
1066 (defun org-e-groff-inline-src-block (inline-src-block contents info
)
1067 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1068 CONTENTS holds the contents of the item. INFO is a plist holding
1069 contextual information."
1070 (let* ((code (org-element-property :value inline-src-block
)))
1072 (org-e-groff-source-highlight
1073 (let* ((tmpdir (if (featurep 'xemacs
)
1075 temporary-file-directory
))
1076 (in-file (make-temp-name
1077 (expand-file-name "srchilite" tmpdir
)))
1078 (out-file (make-temp-name
1079 (expand-file-name "reshilite" tmpdir
)))
1080 (org-lang (org-element-property :language inline-src-block
))
1081 (lst-lang (cadr (assq (intern org-lang
)
1082 org-e-groff-source-highlight-langs
)))
1084 (cmd (concat (expand-file-name "source-highlight")
1086 " -f groff_mm_color "
1090 (let ((code-block ""))
1091 (with-temp-file in-file
(insert code
))
1093 (setq code-block
(org-file-contents out-file
))
1094 (delete-file in-file
)
1095 (delete-file out-file
)
1097 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1100 ;; Do not use a special package: transcode it verbatim.
1102 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1106 (defun org-e-groff-inlinetask (inlinetask contents info
)
1107 "Transcode an INLINETASK element from Org to Groff.
1108 CONTENTS holds the contents of the block. INFO is a plist
1109 holding contextual information."
1110 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1111 (todo (and (plist-get info
:with-todo-keywords
)
1112 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1113 (and todo
(org-export-data todo info
)))))
1114 (todo-type (org-element-property :todo-type inlinetask
))
1115 (tags (and (plist-get info
:with-tags
)
1116 (org-export-get-tags inlinetask info
)))
1117 (priority (and (plist-get info
:with-priority
)
1118 (org-element-property :priority inlinetask
))))
1119 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1120 ;; with appropriate arguments.
1121 (if (functionp org-e-groff-format-inlinetask-function
)
1122 (funcall org-e-groff-format-inlinetask-function
1123 todo todo-type priority title tags contents
)
1124 ;; Otherwise, use a default template.
1125 (org-e-groff--wrap-label
1129 (when todo
(format "\\fB%s\\fP " todo
))
1130 (when priority
(format " [\\#%c] " priority
))
1132 (when tags
(format " \\fC:%s:\\fP "
1133 (mapconcat 'identity tags
":"))))))
1134 (format (concat "\n.DS I\n"
1139 full-title contents
))))))
1143 (defun org-e-groff-italic (italic contents info
)
1144 "Transcode ITALIC from Org to Groff.
1145 CONTENTS is the text with italic markup. INFO is a plist holding
1146 contextual information."
1147 (org-e-groff--text-markup contents
'italic
))
1151 (defun org-e-groff-item (item contents info
)
1152 "Transcode an ITEM element from Org to Groff.
1153 CONTENTS holds the contents of the item. INFO is a plist holding
1154 contextual information."
1155 (let* ((bullet (org-element-property :bullet item
))
1156 (type (org-element-property
1157 :type
(org-element-property :parent item
)))
1158 (checkbox (case (org-element-property :checkbox item
)
1159 (on "\\o'\\(sq\\(mu'")
1161 (trans "\\o'\\(sq\\(mi'")))
1162 (tag (let ((tag (org-element-property :tag item
)))
1163 ;; Check-boxes must belong to the tag.
1164 (and tag
(format "%s"
1166 (org-export-data tag info
)))))))
1170 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1172 (org-trim (or contents
" "))))
1176 (org-trim (or contents
" "))))
1178 (let* ((bullet (org-trim bullet
))
1179 (marker (cond ((string= "-" bullet
) "\\(em")
1180 ((string= "*" bullet
) "\\(bu")
1182 (concat ".LI " marker
"\n"
1183 (org-trim (or contents
" "))))))))
1187 (defun org-e-groff-keyword (keyword contents info
)
1188 "Transcode a KEYWORD element from Org to Groff.
1189 CONTENTS is nil. INFO is a plist holding contextual information."
1190 (let ((key (org-element-property :key keyword
))
1191 (value (org-element-property :value keyword
)))
1193 ((string= key
"GROFF") value
)
1196 ;;; LaTeX Environment
1198 (defun org-e-groff-latex-environment (latex-environment contents info
)
1199 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1200 CONTENTS is nil. INFO is a plist holding contextual information."
1201 (let ((label (org-element-property :name latex-environment
))
1202 (value (org-remove-indentation
1203 (org-element-property :value latex-environment
))))
1204 (if (not (org-string-nw-p label
)) value
1205 ;; Environment is labelled: label must be within the environment
1206 ;; (otherwise, a reference pointing to that element will count
1207 ;; the section instead).
1210 (goto-char (point-min))
1212 (insert (format "%s\n" label
))
1217 (defun org-e-groff-latex-fragment (latex-fragment contents info
)
1218 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1219 CONTENTS is nil. INFO is a plist holding contextual information."
1220 (org-element-property :value latex-fragment
))
1224 (defun org-e-groff-line-break (line-break contents info
)
1225 "Transcode a LINE-BREAK object from Org to Groff.
1226 CONTENTS is nil. INFO is a plist holding contextual information."
1230 ;; Inline images just place a call to .PSPIC or .PS/.PE
1231 ;; and load the graph.
1233 (defun org-e-groff-link--inline-image (link info
)
1234 "Return Groff code for an inline image.
1235 LINK is the link pointing to the inline image. INFO is a plist
1236 used as a communication channel."
1237 (let* ((parent (org-export-get-parent-element link
))
1238 (path (let ((raw-path (org-element-property :path link
)))
1239 (if (not (file-name-absolute-p raw-path
)) raw-path
1240 (expand-file-name raw-path
))))
1241 (attr (org-export-read-attribute :attr_groff link
))
1243 (case (plist-get attr
:position
)
1248 (width (or (plist-get attr
:width
) ""))
1249 (height (or (plist-get attr
:height
) ""))
1250 (caption (and (not (plist-get attr
:disable-caption
))
1251 (org-e-groff--caption/label-string parent info
))))
1252 ;; Now clear ATTR from any special keyword and set a default value
1253 ;; if nothing is left. Return proper string.
1256 ((and org-e-groff-raster-to-ps
1257 (or (string-match ".\.png$" path
)
1258 (string-match ".\.jpg$" path
)))
1259 (let ((eps-path (concat path
".eps")))
1260 (shell-command (format org-e-groff-raster-to-ps path eps-path
))
1261 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1262 placement eps-path width height
)))
1263 ((string-match ".\.pic$" path
)
1264 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1265 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1266 placement path width height
)))
1267 (and caption
(format "\n.FG \"%s\"" caption
)))))
1269 (defun org-e-groff-link (link desc info
)
1270 "Transcode a LINK object from Org to Groff.
1272 DESC is the description part of the link, or the empty string.
1273 INFO is a plist holding contextual information. See
1276 (let* ((type (org-element-property :type link
))
1277 (raw-path (org-element-property :path link
))
1278 ;; Ensure DESC really exists, or set it to nil.
1279 (desc (and (not (string= desc
"")) desc
))
1280 (imagep (org-export-inline-image-p
1281 link org-e-groff-inline-image-rules
))
1283 ((member type
'("http" "https" "ftp" "mailto"))
1284 (concat type
":" raw-path
))
1285 ((string= type
"file")
1286 (when (string-match "\\(.+\\)::.+" raw-path
)
1287 (setq raw-path
(match-string 1 raw-path
)))
1288 (if (file-name-absolute-p raw-path
)
1289 (concat "file://" (expand-file-name raw-path
))
1290 (concat "file://" raw-path
)))
1295 (imagep (org-e-groff-link--inline-image link info
))
1296 ;; import groff files
1297 ((and (string= type
"file")
1298 (string-match ".\.groff$" raw-path
))
1299 (concat ".so " raw-path
"\n"))
1300 ;; Radio link: transcode target's contents and use them as link's
1302 ((string= type
"radio")
1303 (let ((destination (org-export-resolve-radio-link link info
)))
1305 (format "\\fI [%s] \\fP"
1306 (org-export-solidify-link-text path
)))))
1308 ;; Links pointing to an headline: find destination and build
1309 ;; appropriate referencing command.
1310 ((member type
'("custom-id" "fuzzy" "id"))
1311 (let ((destination (if (string= type
"fuzzy")
1312 (org-export-resolve-fuzzy-link link info
)
1313 (org-export-resolve-id-link link info
))))
1314 (case (org-element-type destination
)
1315 ;; Id link points to an external file.
1317 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1318 (format "\\fI file://%s \\fP" destination
)))
1319 ;; Fuzzy link points nowhere.
1321 (format org-e-groff-link-with-unknown-path-format
1324 (org-element-property :raw-link link
) info
))))
1325 ;; Fuzzy link points to an invisible target.
1327 ;; LINK points to an headline. If headlines are numbered
1328 ;; and the link has no description, display headline's
1329 ;; number. Otherwise, display description or headline's
1333 (if (and (plist-get info
:section-numbers
) (not desc
))
1334 (format "\\fI%s\\fP" label
)
1335 (format "\\fI%s\\fP"
1338 (org-element-property :title destination
) info
))))))
1339 ;; Fuzzy link points to a target. Do as above.
1341 (let ((path (org-export-solidify-link-text path
)))
1342 (if (not desc
) (format "\\fI%s\\fP" path
)
1343 (format "%s \\fBat\\fP \\fI%s\\fP" desc path
)))))))
1344 ;; External link with a description part.
1345 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1346 ;; External link without a description part.
1347 (path (format "\\fI%s\\fP" path
))
1348 ;; No path, only description. Try to do something useful.
1349 (t (format org-e-groff-link-with-unknown-path-format desc
)))))
1353 (defun org-e-groff-paragraph (paragraph contents info
)
1354 "Transcode a PARAGRAPH element from Org to Groff.
1355 CONTENTS is the contents of the paragraph, as a string. INFO is
1356 the plist used as a communication channel."
1357 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1359 (let* ((parent-type (car parent
))
1360 (fixed-paragraph "")
1361 (class (plist-get info
:groff-class
))
1362 (class-options (plist-get info
:groff-class-options
))
1363 (classes (assoc class org-e-groff-classes
))
1364 (classes-options (car (last classes
)))
1365 (paragraph-option (plist-get classes-options
:paragraph
)))
1367 ((and (symbolp paragraph-option
)
1368 (fboundp paragraph-option
))
1369 (funcall paragraph-option parent-type parent contents
))
1370 ((and (eq parent-type
'item
)
1371 (plist-get (nth 1 parent
) :bullet
))
1372 (setq fixed-paragraph
(concat "" contents
)))
1373 ((eq parent-type
'section
)
1374 (setq fixed-paragraph
(concat ".P\n" contents
)))
1375 ((eq parent-type
'footnote-definition
)
1376 (setq fixed-paragraph
(concat "" contents
)))
1377 (t (setq fixed-paragraph
(concat "" contents
))))
1382 (defun org-e-groff-plain-list (plain-list contents info
)
1383 "Transcode a PLAIN-LIST element from Org to Groff.
1384 CONTENTS is the contents of the list. INFO is a plist holding
1385 contextual information."
1386 (let* ((type (org-element-property :type plain-list
))
1387 (attr (mapconcat #'identity
1388 (org-element-property :attr_groff plain-list
)
1391 ((eq type
'ordered
) ".AL")
1392 ((eq type
'unordered
) ".BL")
1393 ((eq type
'descriptive
) ".VL 2.0i"))))
1394 (org-e-groff--wrap-label
1396 (format "%s\n%s\n.LE" groff-type contents
))))
1400 (defun org-e-groff-plain-text (text info
)
1401 "Transcode a TEXT string from Org to Groff.
1402 TEXT is the string to transcode. INFO is a plist holding
1403 contextual information."
1404 (let ((output text
))
1405 ;; Protect various characters.
1406 (setq output
(replace-regexp-in-string
1407 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1408 "$\\" output nil t
1))
1409 ;; Activate smart quotes. Be sure to provide original TEXT string
1410 ;; since OUTPUT may have been modified.
1411 (when (plist-get info
:with-smart-quotes
)
1412 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
1413 ;; Handle Special Characters
1414 (if org-e-groff-special-char
1415 (dolist (special-char-list org-e-groff-special-char
)
1417 (replace-regexp-in-string (car special-char-list
)
1418 (cdr special-char-list
) output
))))
1419 ;; Handle break preservation if required.
1420 (when (plist-get info
:preserve-breaks
)
1421 (setq output
(replace-regexp-in-string
1422 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output
)))
1428 (defun org-e-groff-planning (planning contents info
)
1429 "Transcode a PLANNING element from Org to Groff.
1430 CONTENTS is nil. INFO is a plist holding contextual
1437 (let ((closed (org-element-property :closed planning
)))
1440 (format "\\fR %s \\fP" org-closed-string
)
1441 (format org-e-groff-inactive-timestamp-format
1442 (org-translate-time closed
)))))
1443 (let ((deadline (org-element-property :deadline planning
)))
1446 (format "\\fB %s \\fP" org-deadline-string
)
1447 (format org-e-groff-active-timestamp-format
1448 (org-translate-time deadline
)))))
1449 (let ((scheduled (org-element-property :scheduled planning
)))
1452 (format "\\fR %s \\fP" org-scheduled-string
)
1453 (format org-e-groff-active-timestamp-format
1454 (org-translate-time scheduled
)))))))
1460 (defun org-e-groff-property-drawer (property-drawer contents info
)
1461 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1462 CONTENTS is nil. INFO is a plist holding contextual
1464 ;; The property drawer isn't exported but we want separating blank
1465 ;; lines nonetheless.
1470 (defun org-e-groff-quote-block (quote-block contents info
)
1471 "Transcode a QUOTE-BLOCK element from Org to Groff.
1472 CONTENTS holds the contents of the block. INFO is a plist
1473 holding contextual information."
1474 (org-e-groff--wrap-label
1476 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1480 (defun org-e-groff-quote-section (quote-section contents info
)
1481 "Transcode a QUOTE-SECTION element from Org to Groff.
1482 CONTENTS is nil. INFO is a plist holding contextual information."
1483 (let ((value (org-remove-indentation
1484 (org-element-property :value quote-section
))))
1485 (when value
(format ".DS L\n\\fI%s\\fP\n.DE\n" value
))))
1489 (defun org-e-groff-radio-target (radio-target text info
)
1490 "Transcode a RADIO-TARGET object from Org to Groff.
1491 TEXT is the text of the target. INFO is a plist holding
1492 contextual information."
1494 (org-export-solidify-link-text
1495 (org-element-property :value radio-target
))
1500 (defun org-e-groff-section (section contents info
)
1501 "Transcode a SECTION element from Org to Groff.
1502 CONTENTS holds the contents of the section. INFO is a plist
1503 holding contextual information."
1508 (defun org-e-groff-special-block (special-block contents info
)
1509 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1510 CONTENTS holds the contents of the block. INFO is a plist
1511 holding contextual information."
1512 (let ((type (downcase (org-element-property :type special-block
))))
1513 (org-e-groff--wrap-label
1515 (format "%s\n" contents
))))
1519 (defun org-e-groff-src-block (src-block contents info
)
1520 "Transcode a SRC-BLOCK element from Org to Groff.
1521 CONTENTS holds the contents of the item. INFO is a plist holding
1522 contextual information."
1523 (let* ((lang (org-element-property :language src-block
))
1524 (label (org-element-property :name src-block
))
1525 (code (org-element-property :value src-block
))
1526 (custom-env (and lang
1527 (cadr (assq (intern lang
)
1528 org-e-groff-custom-lang-environments
))))
1529 (num-start (case (org-element-property :number-lines src-block
)
1530 (continued (org-export-get-loc src-block info
))
1532 (retain-labels (org-element-property :retain-labels src-block
))
1533 (caption (and (not (org-export-read-attribute
1534 :attr_groff src-block
:disable-caption
))
1535 (org-e-groff--caption/label-string src-block info
))))
1538 ;; Case 1. No source fontification.
1539 ((not org-e-groff-source-highlight
)
1541 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1542 (org-export-format-code-default src-block info
))
1543 (and caption
(format ".EX \"%s\" " caption
))))
1545 ;; Case 2. Source fontification.
1546 (org-e-groff-source-highlight
1547 (let* ((tmpdir (if (featurep 'xemacs
)
1549 temporary-file-directory
))
1550 (in-file (make-temp-name
1551 (expand-file-name "srchilite" tmpdir
)))
1552 (out-file (make-temp-name
1553 (expand-file-name "reshilite" tmpdir
)))
1555 (org-lang (org-element-property :language src-block
))
1556 (lst-lang (cadr (assq (intern org-lang
)
1557 org-e-groff-source-highlight-langs
)))
1559 (cmd (concat "source-highlight"
1561 " -f groff_mm_color "
1567 (let ((code-block ""))
1568 (with-temp-file in-file
(insert code
))
1570 (setq code-block
(org-file-contents out-file
))
1571 (delete-file in-file
)
1572 (delete-file out-file
)
1573 (format "%s\n" code-block
))
1574 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1576 (and caption
(format ".EX \"%s\" " caption
))))))))
1579 ;;; Statistics Cookie
1581 (defun org-e-groff-statistics-cookie (statistics-cookie contents info
)
1582 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1583 CONTENTS is nil. INFO is a plist holding contextual information."
1584 (org-element-property :value statistics-cookie
))
1589 (defun org-e-groff-strike-through (strike-through contents info
)
1590 "Transcode STRIKE-THROUGH from Org to Groff.
1591 CONTENTS is the text with strike-through markup. INFO is a plist
1592 holding contextual information."
1593 (org-e-groff--text-markup contents
'strike-through
))
1597 (defun org-e-groff-subscript (subscript contents info
)
1598 "Transcode a SUBSCRIPT object from Org to Groff.
1599 CONTENTS is the contents of the object. INFO is a plist holding
1600 contextual information."
1601 (format "\\d\\s-2%s\\s+2\\u" contents
))
1603 ;;; Superscript "^_%s$
1605 (defun org-e-groff-superscript (superscript contents info
)
1606 "Transcode a SUPERSCRIPT object from Org to Groff.
1607 CONTENTS is the contents of the object. INFO is a plist holding
1608 contextual information."
1609 (format "\\u\\s-2%s\\s+2\\d" contents
))
1614 ;; `org-e-groff-table' is the entry point for table transcoding. It
1615 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1616 ;; delegates the job to `org-e-groff-table--org-table' function,
1617 ;; depending of the type of the table.
1619 ;; `org-e-groff-table--align-string' is a subroutine used to build
1620 ;; alignment string for Org tables.
1622 (defun org-e-groff-table (table contents info
)
1623 "Transcode a TABLE element from Org to Groff.
1624 CONTENTS is the contents of the table. INFO is a plist holding
1625 contextual information."
1627 ;; Case 1: verbatim table.
1628 ((or org-e-groff-tables-verbatim
1629 (let ((attr (read (format "(%s)"
1632 (org-element-property :attr_groff table
) " ")))))
1633 (and attr
(plist-get attr
:verbatim
))))
1635 (format ".DS L\n\\fC%s\\fP\n.DE"
1636 ;; Re-create table, without affiliated keywords.
1638 (org-element-interpret-data
1639 `(table nil
,@(org-element-contents table
))))))
1641 ;; Case 2: Standard table.
1642 (t (org-e-groff-table--org-table table contents info
))))
1644 (defun org-e-groff-table--align-string (divider table info
)
1645 "Return an appropriate Groff alignment string.
1646 TABLE is the considered table. INFO is a plist used as
1647 a communication channel."
1649 ;; Extract column groups and alignment from first (non-rule)
1655 (and (eq (org-element-property :type row
) 'standard
) row
))
1659 (let* ((borders (org-export-table-cell-borders cell info
))
1660 (raw-width (org-export-table-cell-width cell info
))
1661 (width-cm (when raw-width
(/ raw-width
5)))
1662 (width (if raw-width
(format "w(%dc)"
1663 (if (< width-cm
1) 1 width-cm
)) "")))
1664 ;; Check left border for the first cell only.
1665 ;; Alignment is nil on assignment
1667 (when (and (memq 'left borders
) (not alignment
))
1668 (push "|" alignment
))
1670 (case (org-export-table-cell-alignment cell info
)
1671 (left (concat "l" width divider
))
1672 (right (concat "r" width divider
))
1673 (center (concat "c" width divider
)))
1675 (when (memq 'right borders
) (push "|" alignment
))))
1677 (apply 'concat
(reverse alignment
))))
1679 (defun org-e-groff-table--org-table (table contents info
)
1680 "Return appropriate Groff code for an Org table.
1682 TABLE is the table type element to transcode. CONTENTS is its
1683 contents, as a string. INFO is a plist used as a communication
1686 This function assumes TABLE has `org' as its `:type' attribute."
1687 (let* ((attr (org-export-read-attribute :attr_groff table
))
1688 (label (org-element-property :name table
))
1689 (caption (and (not (plist-get attr
:disable-caption
))
1690 (org-e-groff--caption/label-string table info
)))
1691 (divider (if (plist-get attr
:divider
) "|" " "))
1693 ;; Determine alignment string.
1694 (alignment (org-e-groff-table--align-string divider table info
))
1696 ;; Extract others display options.
1698 (lines (org-split-string contents
"\n"))
1704 (if (plist-get attr
:expand
)
1707 (case (plist-get attr
:placement
)
1711 (if org-e-groff-tables-centered
1714 (case (plist-get attr
:boxtype
)
1716 ('doublebox
"doublebox")
1721 (if (not (null attr-item
))
1722 (add-to-list 'result-list attr-item
)))
1725 (title-line (plist-get attr
:title-line
))
1726 (long-cells (plist-get attr
:long-cells
))
1731 (or (car attr-list
) ""))
1734 (when (cdr attr-list
)
1735 (dolist (attr-item (cdr attr-list
))
1736 (setq output-list
(concat output-list
1737 (format ",%s" attr-item
)))))
1740 (when lines
(org-split-string (car lines
) "\t"))))
1741 ;; Prepare the final format string for the table.
1747 (concat ".TS\n " table-format
";\n"
1749 (let ((final-line ""))
1751 (dotimes (i (length first-line
))
1752 (setq final-line
(concat final-line
"cb" divider
))))
1754 (setq final-line
(concat final-line
"\n"))
1757 (setq final-line
(concat final-line alignment
))
1758 (dotimes (i (length first-line
))
1759 (setq final-line
(concat final-line
"c" divider
))))
1763 (let ((final-line "")
1765 (lines (org-split-string contents
"\n")))
1767 (dolist (line-item lines
)
1772 (if (string= line-item
"_")
1773 (setq long-line
(format "%s\n" line-item
))
1775 (let ((cell-item-list (org-split-string line-item
"\t")))
1776 (dolist (cell-item cell-item-list
)
1778 (cond ((eq cell-item
(car (last cell-item-list
)))
1779 (setq long-line
(concat long-line
1780 (format "T{\n%s\nT}\t\n" cell-item
))))
1782 (setq long-line
(concat long-line
1783 (format "T{\n%s\nT}\t" cell-item
))))))
1786 (setq final-line
(concat final-line long-line
)))
1788 (setq final-line
(concat final-line line-item
"\n"))))
1791 (if caption
(format ".TB \"%s\"" caption
) ""))))))
1795 (defun org-e-groff-table-cell (table-cell contents info
)
1796 "Transcode a TABLE-CELL element from Org to Groff
1797 CONTENTS is the cell contents. INFO is a plist used as
1798 a communication channel."
1800 (concat (if (and contents
1801 org-e-groff-table-scientific-notation
1802 (string-match orgtbl-exp-regexp contents
))
1803 ;; Use appropriate format string for scientific
1805 (format org-e-groff-table-scientific-notation
1806 (match-string 1 contents
)
1807 (match-string 2 contents
))
1809 (when (org-export-get-next-element table-cell info
) "\t"))))
1814 (defun org-e-groff-table-row (table-row contents info
)
1815 "Transcode a TABLE-ROW element from Org to Groff
1816 CONTENTS is the contents of the row. INFO is a plist used as
1817 a communication channel."
1818 ;; Rules are ignored since table separators are deduced from
1819 ;; borders of the current row.
1820 (when (eq (org-element-property :type table-row
) 'standard
)
1821 (let* ((attr (mapconcat 'identity
1822 (org-element-property
1823 :attr_groff
(org-export-get-parent table-row
))
1825 ;; TABLE-ROW's borders are extracted from its first cell.
1827 (org-export-table-cell-borders
1828 (car (org-element-contents table-row
)) info
)))
1830 ;; Mark horizontal lines
1831 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1834 ;; When BOOKTABS are activated enforce bottom rule even when
1835 ;; no hline was specifically marked.
1836 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1837 ((memq 'below borders
) "\n_"))))))
1841 (defun org-e-groff-target (target contents info
)
1842 "Transcode a TARGET object from Org to Groff.
1843 CONTENTS is nil. INFO is a plist holding contextual
1845 (format "\\fI%s\\fP"
1846 (org-export-solidify-link-text (org-element-property :value target
))))
1850 (defun org-e-groff-timestamp (timestamp contents info
)
1851 "Transcode a TIMESTAMP object from Org to Groff.
1852 CONTENTS is nil. INFO is a plist holding contextual
1854 (let ((value (org-translate-time
1855 (org-element-property :raw-value timestamp
))))
1856 (case (org-element-property :type timestamp
)
1857 ((active active-range
)
1858 (format org-e-groff-active-timestamp-format value
))
1859 ((inactive inactive-range
)
1860 (format org-e-groff-inactive-timestamp-format value
))
1861 (t (format org-e-groff-diary-timestamp-format value
)))))
1865 (defun org-e-groff-underline (underline contents info
)
1866 "Transcode UNDERLINE from Org to Groff.
1867 CONTENTS is the text with underline markup. INFO is a plist
1868 holding contextual information."
1869 (org-e-groff--text-markup contents
'underline
))
1873 (defun org-e-groff-verbatim (verbatim contents info
)
1874 "Transcode a VERBATIM object from Org to Groff.
1875 CONTENTS is nil. INFO is a plist used as a communication
1877 (org-e-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1881 (defun org-e-groff-verse-block (verse-block contents info
)
1882 "Transcode a VERSE-BLOCK element from Org to Groff.
1883 CONTENTS is verse block contents. INFO is a plist holding
1884 contextual information."
1885 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1888 ;;; Interactive functions
1890 (defun org-e-groff-export-to-groff
1891 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1892 "Export current buffer to a Groff file.
1894 If narrowing is active in the current buffer, only export its
1897 If a region is active, export that region.
1899 When optional argument SUBTREEP is non-nil, export the sub-tree
1900 at point, extracting information from the headline properties
1903 When optional argument VISIBLE-ONLY is non-nil, don't export
1904 contents of hidden elements.
1906 EXT-PLIST, when provided, is a property list with external
1907 parameters overriding Org default settings, but still inferior to
1908 file-local settings.
1910 When optional argument PUB-DIR is set, use it as the publishing
1913 Return output file's name."
1915 (setq org-e-groff-registered-references nil
)
1916 (setq org-e-groff-special-content nil
)
1917 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir
)))
1919 'e-groff outfile subtreep visible-only body-only ext-plist
)))
1921 (defun org-e-groff-export-to-pdf
1922 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1923 "Export current buffer to Groff then process through to PDF.
1925 If narrowing is active in the current buffer, only export its
1928 If a region is active, export that region.
1930 When optional argument SUBTREEP is non-nil, export the sub-tree
1931 at point, extracting information from the headline properties
1934 When optional argument VISIBLE-ONLY is non-nil, don't export
1935 contents of hidden elements.
1937 EXT-PLIST, when provided, is a property list with external
1938 parameters overriding Org default settings, but still inferior to
1939 file-local settings.
1941 When optional argument PUB-DIR is set, use it as the publishing
1944 Return PDF file's name."
1946 (org-e-groff-compile
1947 (org-e-groff-export-to-groff
1948 subtreep visible-only body-only ext-plist pub-dir
)))
1950 (defun org-e-groff-compile (file)
1951 "Compile a Groff file.
1953 FILE is the name of the file being compiled. Processing is done
1954 through the command specified in `org-e-groff-pdf-process'.
1956 Return PDF file name or an error if it couldn't be produced."
1957 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file
)))
1958 (full-name (file-truename file
))
1959 (out-dir (file-name-directory file
))
1960 ;; Make sure `default-directory' is set to FILE directory,
1961 ;; not to whatever value the current buffer may have.
1962 (default-directory (file-name-directory full-name
))
1964 (message (format "Processing Groff file %s ..." file
))
1965 (save-window-excursion
1967 ;; A function is provided: Apply it.
1968 ((functionp org-e-groff-pdf-process
)
1969 (funcall org-e-groff-pdf-process
(shell-quote-argument file
)))
1970 ;; A list is provided: Replace %b, %f and %o with appropriate
1971 ;; values in each command before applying it. Output is
1972 ;; redirected to "*Org PDF Groff Output*" buffer.
1973 ((consp org-e-groff-pdf-process
)
1974 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1978 (replace-regexp-in-string
1979 "%b" (shell-quote-argument base-name
)
1980 (replace-regexp-in-string
1981 "%f" (shell-quote-argument full-name
)
1982 (replace-regexp-in-string
1983 "%o" (shell-quote-argument out-dir
) command t t
)
1986 org-e-groff-pdf-process
)
1987 ;; Collect standard errors from output buffer.
1988 (setq errors
(org-e-groff-collect-errors outbuf
))))
1989 (t (error "No valid command to process to PDF")))
1990 (let ((pdffile (concat out-dir base-name
".pdf")))
1991 ;; Check for process failure. Provide collected errors if
1993 (if (not (file-exists-p pdffile
))
1994 (error (concat (format "PDF file %s wasn't produced" pdffile
)
1995 (when errors
(concat ": " errors
))))
1996 ;; Else remove log files, when specified, and signal end of
1997 ;; process to user, along with any error encountered.
1998 (when org-e-groff-remove-logfiles
1999 (dolist (ext org-e-groff-logfiles-extensions
)
2000 (let ((file (concat out-dir base-name
"." ext
)))
2001 (when (file-exists-p file
) (delete-file file
)))))
2002 (message (concat "Process completed"
2003 (if (not errors
) "."
2004 (concat " with errors: " errors
)))))
2005 ;; Return output file name.
2008 (defun org-e-groff-collect-errors (buffer)
2009 "Collect some kind of errors from \"groff\" output
2010 BUFFER is the buffer containing output.
2011 Return collected error types as a string, or nil if there was
2013 (with-current-buffer buffer
2015 (goto-char (point-max))
2020 (provide 'org-e-groff
)
2021 ;;; org-e-groff.el ends here