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
))
47 (org-export-define-backend e-groff
48 ((bold . org-e-groff-bold
)
49 (center-block . org-e-groff-center-block
)
50 (clock . org-e-groff-clock
)
51 (code . org-e-groff-code
)
52 (drawer . org-e-groff-drawer
)
53 (dynamic-block . org-e-groff-dynamic-block
)
54 (entity . org-e-groff-entity
)
55 (example-block . org-e-groff-example-block
)
56 (export-block . org-e-groff-export-block
)
57 (export-snippet . org-e-groff-export-snippet
)
58 (fixed-width . org-e-groff-fixed-width
)
59 (footnote-definition . org-e-groff-footnote-definition
)
60 (footnote-reference . org-e-groff-footnote-reference
)
61 (headline . org-e-groff-headline
)
62 (horizontal-rule . org-e-groff-horizontal-rule
)
63 (inline-src-block . org-e-groff-inline-src-block
)
64 (inlinetask . org-e-groff-inlinetask
)
65 (italic . org-e-groff-italic
)
66 (item . org-e-groff-item
)
67 (keyword . org-e-groff-keyword
)
68 ;; (latex-environment . org-e-groff-latex-environment)
69 ;; (latex-fragment . org-e-groff-latex-fragment)
70 (line-break . org-e-groff-line-break
)
71 (link . org-e-groff-link
)
72 (paragraph . org-e-groff-paragraph
)
73 (plain-list . org-e-groff-plain-list
)
74 (plain-text . org-e-groff-plain-text
)
75 (planning . org-e-groff-planning
)
76 (property-drawer . org-e-groff-property-drawer
)
77 (quote-block . org-e-groff-quote-block
)
78 (quote-section . org-e-groff-quote-section
)
79 (radio-target . org-e-groff-radio-target
)
80 (section . org-e-groff-section
)
81 (special-block . org-e-groff-special-block
)
82 (src-block . org-e-groff-src-block
)
83 (statistics-cookie . org-e-groff-statistics-cookie
)
84 (strike-through . org-e-groff-strike-through
)
85 (subscript . org-e-groff-subscript
)
86 (superscript . org-e-groff-superscript
)
87 (table . org-e-groff-table
)
88 (table-cell . org-e-groff-table-cell
)
89 (table-row . org-e-groff-table-row
)
90 (target . org-e-groff-target
)
91 (template . org-e-groff-template
)
92 (timestamp . org-e-groff-timestamp
)
93 (underline . org-e-groff-underline
)
94 (verbatim . org-e-groff-verbatim
)
95 (verse-block . org-e-groff-verse-block
))
99 ((?g
"As GROFF file" org-e-groff-export-to-groff
)
100 (?p
"As PDF file" org-e-groff-export-to-pdf
)
101 (?o
"As PDF file and open"
102 (lambda (s v b
) (org-open-file (org-e-groff-export-to-pdf s v b
))))))
104 ((:date
"DATE" nil org-e-groff-date-format t
)
105 (:groff-class
"GROFF_CLASS" nil org-e-groff-default-class t
)
106 (:groff-class-options
"GROFF_CLASS_OPTIONS" nil nil t
)
107 (:groff-header-extra
"GROFF_HEADER" nil nil newline
)))
111 ;;; User Configurable Variables
113 (defgroup org-export-e-groff nil
114 "Options for exporting Org mode files to Groff."
115 :tag
"Org Export Groff"
120 (defcustom org-e-groff-default-class
"internal"
121 "The default Groff class."
122 :group
'org-export-e-groff
123 :type
'(string :tag
"Groff class"))
125 (defcustom org-e-groff-classes
127 (:heading
'default
:type
"memo" :last-section
"toc"))
129 (:heading
'default
:type
"memo" :last-section
"toc"))
130 ("programmer" ".MT 2"
131 (:heading
'default
:type
"memo" :last-section
"toc"))
133 (:heading
'default
:type
"memo" :last-section
"toc"))
135 (:heading
'default
:type
"memo" :last-section
"toc"))
137 (:heading
'default
:type
"memo" :last-section
"sign"))
139 (:heading custom-function
:type
"custom" :last-section
"toc"))
141 (:heading
'default
:type
"memo"))
143 (:heading
'default
:type
"cover" :last-section
"toc"))
145 (:heading
'default
:type
"cover" :last-section
"toc"))
147 (:heading
'default
:type
"letter" :last-section
"sign"))
149 (:heading
'default
:type
"letter" :last-section
"sign"))
151 (:heading
'default
:type
"letter" :last-section
"sign"))
153 (:heading
'default
:type
"letter" :last-section
"sign"))
154 ("none" "" (:heading
'default
:type
"custom")))
156 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
157 ;; This is to facilitate the creation of custom pages.
159 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
160 ;; are made. This is to facilitate Abstract Insertion.
162 "This list describes the attributes for the documents being created.
163 It allows for the creation of new "
164 :group
'org-export-e-groff
166 (list (string :tag
"Document Type")
167 (string :tag
"Header")
168 (repeat :tag
"Options" :inline t
170 (list :tag
"Heading")
171 (function :tag
"Hook computing sectioning"))))))
174 (defcustom org-e-groff-date-format
175 (format-time-string "%Y-%m-%d")
176 "Format string for .ND "
177 :group
'org-export-e-groff
182 (defconst org-e-groff-special-tags
183 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
185 (defcustom org-e-groff-format-headline-function nil
186 "Function to format headline text.
188 This function will be called with 5 arguments:
189 TODO the todo keyword (string or nil).
190 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
191 PRIORITY the priority of the headline (integer or nil)
192 TEXT the main headline text (string).
193 TAGS the tags as a list of strings (list of strings or nil).
195 The function result will be used in the section format string.
197 As an example, one could set the variable to the following, in
198 order to reproduce the default set-up:
200 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
201 \"Default format function for an headline.\"
203 \(format \"\\fB%s\\fP \" todo))
205 \(format \"[\\#%c] \" priority))
209 \(mapconcat 'identity tags \":\"))))"
210 :group
'org-export-e-groff
215 (defcustom org-e-groff-active-timestamp-format
"\\fI%s\\fP"
216 "A printf format string to be applied to active timestamps."
217 :group
'org-export-e-groff
220 (defcustom org-e-groff-inactive-timestamp-format
"\\fI%s\\fP"
221 "A printf format string to be applied to inactive timestamps."
222 :group
'org-export-e-groff
225 (defcustom org-e-groff-diary-timestamp-format
"\\fI%s\\fP"
226 "A printf format string to be applied to diary timestamps."
227 :group
'org-export-e-groff
232 (defcustom org-e-groff-inline-image-rules
233 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
234 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
235 "Rules characterizing image files that can be inlined into Groff.
237 A rule consists in an association whose key is the type of link
238 to consider, and value is a regexp that will be matched against
241 Note that, by default, the image extensions actually allowed
242 depend on the way the Groff file is processed. When used with
243 pdfgroff, pdf, jpg and png images are OK. When processing
244 through dvi to Postscript, only ps and eps are allowed. The
245 default we use here encompasses both."
246 :group
'org-export-e-groff
247 :type
'(alist :key-type
(string :tag
"Type")
248 :value-type
(regexp :tag
"Path")))
250 (defcustom org-e-groff-link-with-unknown-path-format
"\\fI%s\\fP"
251 "Format string for links with unknown path type."
252 :group
'org-export-groff
257 (defcustom org-e-groff-tables-centered t
258 "When non-nil, tables are exported in a center environment."
259 :group
'org-export-e-groff
262 (defcustom org-e-groff-tables-verbatim nil
263 "When non-nil, tables are exported verbatim."
264 :group
'org-export-e-groff
267 (defcustom org-e-groff-table-scientific-notation
"%sE%s"
268 "Format string to display numbers in scientific notation.
269 The format should have \"%s\" twice, for mantissa and exponent
270 \(i.e. \"%s\\\\times10^{%s}\").
272 When nil, no transformation is made."
273 :group
'org-export-e-groff
275 (string :tag
"Format string")
276 (const :tag
"No formatting")))
280 (defcustom org-e-groff-text-markup-alist
281 '((bold .
"\\fB%s\\fP")
282 (code .
"\\fC%s\\fP")
283 (italic .
"\\fI%s\\fP")
284 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
285 (underline .
"\\fI%s\\fP") ; need to be revised.
286 (verbatim .
"protectedtexttt"))
287 "Alist of Groff expressions to convert text markup.
289 The key must be a symbol among `bold', `code', `italic',
290 `strike-through', `underline' and `verbatim'. The value is
291 a formatting string to wrap fontified text with it.
293 If no association can be found for a given markup, text will be
295 :group
'org-export-e-groff
297 :options
'(bold code italic strike-through underline verbatim
))
301 (defcustom org-e-groff-format-drawer-function nil
302 "Function called to format a drawer in Groff code.
304 The function must accept two parameters:
305 NAME the drawer name, like \"LOGBOOK\"
306 CONTENTS the contents of the drawer.
308 The function should return the string to be exported.
310 For example, the variable could be set to the following function
311 in order to mimic default behaviour:
313 \(defun org-e-groff-format-drawer-default \(name contents\)
314 \"Format a drawer element for Groff export.\"
316 :group
'org-export-e-groff
321 (defcustom org-e-groff-format-inlinetask-function nil
322 "Function called to format an inlinetask in Groff code.
324 The function must accept six parameters:
325 TODO the todo keyword, as a string
326 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
327 PRIORITY the inlinetask priority, as a string
328 NAME the inlinetask name, as a string.
329 TAGS the inlinetask tags, as a list of strings.
330 CONTENTS the contents of the inlinetask, as a string.
332 The function should return the string to be exported.
334 For example, the variable could be set to the following function
335 in order to mimic default behaviour:
337 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
338 \"Format an inline task element for Groff export.\"
342 \(format \"\\fB%s\\fP \" todo))
343 \(when priority (format \"[\\#%c] \" priority))
347 \(mapconcat 'identity tags \":\")))))
348 \(format (concat \".DS L\\n\"
352 full-title contents))"
353 :group
'org-export-e-groff
358 (defcustom org-e-groff-source-highlight nil
359 "Use GNU source highlight to embellish source blocks "
360 :group
'org-export-e-groff
363 (defcustom org-e-groff-source-highlight-langs
364 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
366 (c "c") (cc "cpp") (csharp "csharp") (d "d")
367 (fortran "fortran") (cobol "cobol") (pascal "pascal")
368 (ada "ada") (asm "asm")
369 (perl "perl") (cperl "perl")
370 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
371 (java "java") (javascript "javascript")
373 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
374 (ocaml "caml") (caml "caml")
375 (sql "sql") (sqlite "sql")
376 (html "html") (css "css") (xml "xml")
377 (bat "bat") (bison "bison") (clipper "clipper")
378 (ldap "ldap") (opa "opa")
379 (php "php") (postscript "postscript") (prolog "prolog")
380 (properties "properties") (makefile "makefile")
381 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
382 "Alist mapping languages to their listing language counterpart.
383 The key is a symbol, the major mode symbol without the \"-mode\".
384 The value is the string that should be inserted as the language
385 parameter for the listings package. If the mode name and the
386 listings name are the same, the language does not need an entry
387 in this list - but it does not hurt if it is present."
388 :group
'org-export-e-groff
391 (symbol :tag
"Major mode ")
392 (string :tag
"Listings language"))))
394 (defcustom org-e-groff-source-highlight-options nil
395 "Association list of options for the groff listings package.
397 These options are supplied as a comma-separated list to the
398 \\lstset command. Each element of the association list should be
399 a list containing two strings: the name of the option, and the
402 (setq org-e-groff-source-highlight-options
403 '((\"basicstyle\" \"\\small\")
404 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
406 will typeset the code in a small size font with underlined, bold
409 Note that the same options will be applied to blocks of all
411 :group
'org-export-e-groff
414 (string :tag
"Listings option name ")
415 (string :tag
"Listings option value"))))
417 (defvar org-e-groff-custom-lang-environments nil
418 "Alist mapping languages to language-specific Groff environments.
420 It is used during export of src blocks by the listings and
421 groff packages. For example,
423 \(setq org-e-groff-custom-lang-environments
424 '\(\(python \"pythoncode\"\)\)\)
426 would have the effect that if org encounters begin_src python
427 during groff export it will use pythoncode as the source-highlight
432 (defcustom org-e-groff-quotes
434 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
435 ("\\(\\S-\\)\"" .
"~»")
436 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
438 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
439 ("\\(\\S-\\)\"" .
"''")
440 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
441 "Alist for quotes to use when converting english double-quotes.
443 The CAR of each item in this alist is the language code.
444 The CDR of each item in this alist is a list of three CONS:
445 - the first CONS defines the opening quote;
446 - the second CONS defines the closing quote;
447 - the last CONS defines single quotes.
449 For each item in a CONS, the first string is a regexp
450 for allowed characters before/after the quote, the second
451 string defines the replacement string for this quote."
452 :group
'org-export-e-groff
454 (cons :tag
"Opening quote"
455 (string :tag
"Regexp for char before")
456 (string :tag
"Replacement quote "))
457 (cons :tag
"Closing quote"
458 (string :tag
"Regexp for char after ")
459 (string :tag
"Replacement quote "))
460 (cons :tag
"Single quote"
461 (string :tag
"Regexp for char before")
462 (string :tag
"Replacement quote "))))
464 (defcustom org-e-groff-special-char
465 '(("(c)" .
"\\\\(co")
467 ("(rg)" .
"\\\\(rg"))
468 "CONS list in which the value of the car
469 is replace on the value of the CDR. "
470 :group
'org-export-e-groff
472 (cons :tag
"Character Subtitute"
473 (string :tag
"Original Character Group")
474 (string :tag
"Replacement Character"))))
478 (defcustom org-e-groff-pdf-process
479 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
480 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
481 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
483 "Commands to process a Groff file to a PDF file.
484 This is a list of strings, each of them will be given to the
485 shell as a command. %f in the command will be replaced by the
486 full file name, %b by the file base name \(i.e. without
487 extension) and %o by the base directory of the file."
488 :group
'org-export-pdf
490 (repeat :tag
"Shell command sequence"
491 (string :tag
"Shell command"))
492 (const :tag
"2 runs of pdfgroff"
493 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
494 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
495 (const :tag
"3 runs of pdfgroff"
496 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
497 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
498 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
501 (defcustom org-e-groff-logfiles-extensions
502 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
503 "The list of file extensions to consider as Groff logfiles."
504 :group
'org-export-e-groff
505 :type
'(repeat (string :tag
"Extension")))
507 (defcustom org-e-groff-remove-logfiles t
508 "Non-nil means remove the logfiles produced by PDF production.
509 These are the .aux, .log, .out, and .toc files."
510 :group
'org-export-e-groff
513 (defcustom org-e-groff-organization
"Org User"
514 "Name of the organization used to populate the .AF command."
515 :group
'org-export-e-groff
518 (defcustom org-e-groff-raster-to-ps nil
519 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
520 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
521 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
522 :group
'org-export-e-groff
524 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
525 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
526 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
527 (const :tag
"None" nil
)))
529 (defvar org-e-groff-registered-references nil
)
530 (defvar org-e-groff-special-content nil
)
534 ;;; Internal Functions
536 (defun org-e-groff--caption/label-string
(element info
)
537 "Return caption and label Groff string for ELEMENT.
539 INFO is a plist holding contextual information. If there's no
540 caption nor label, return the empty string.
542 For non-floats, see `org-e-groff--wrap-label'."
543 (let ((main (org-export-get-caption element
))
544 (short (org-export-get-caption element t
))
545 (label (org-element-property :name element
)))
546 (cond ((and (not main
) (not label
)) "")
547 ((not main
) (format "\\fI%s\\fP" label
))
548 ;; Option caption format with short name.
549 (short (format "%s\n.br\n - %s\n"
550 (org-export-data short info
)
551 (org-export-data main info
)))
552 ;; Standard caption format.
553 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
555 (defun org-e-groff--quotation-marks (text info
)
556 "Export quotation marks depending on language conventions.
557 TEXT is a string containing quotation marks to be replaced. INFO
558 is a plist used as a communication channel."
561 (while (setq start
(string-match (car l
) text start
))
562 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
563 (setq text
(replace-match new-quote t t text
))))))
564 (cdr (or (assoc (plist-get info
:language
) org-e-groff-quotes
)
565 ;; Falls back on English.
566 (assoc "en" org-e-groff-quotes
))))
569 (defun org-e-groff--wrap-label (element output
)
570 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
571 This function shouldn't be used for floats. See
572 `org-e-groff--caption/label-string'."
573 (let ((label (org-element-property :name element
)))
574 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
576 (concat (format "%s\n.br\n" label
) output
))))
578 (defun org-e-groff--text-markup (text markup
)
579 "Format TEXT depending on MARKUP text markup.
580 See `org-e-groff-text-markup-alist' for details."
581 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist
))))
583 ;; No format string: Return raw text.
585 ((string= "protectedtexttt" fmt
)
587 (trans '(("\\" .
"\\")))
590 (while (string-match "[\\{}$%&_#~^]" text
)
591 (setq char
(match-string 0 text
))
592 (if (> (match-beginning 0) 0)
593 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
594 (setq text
(substring text
(1+ (match-beginning 0))))
595 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
596 rtn
(concat rtn char
)))
597 (setq text
(concat rtn text
))
598 (format "\\fC%s\\fP" text
)))
599 ;; Else use format string.
600 (t (format fmt text
)))))
603 (defun org-e-groff--get-tagged-content (tag info
)
604 (cdr (assoc tag org-e-groff-special-content
)))
606 (defun org-e-groff--mt-head (title contents attr info
)
609 ;; 1. Insert Organization
610 (let ((firm-option (plist-get attr
:firm
)))
612 ((stringp firm-option
)
613 (format ".AF \"%s\" \n" firm-option
))
614 (t (format ".AF \"%s\" \n" (or org-e-groff-organization
"")))))
617 (let ((subtitle1 (plist-get attr
:subtitle1
))
618 (subtitle2 (plist-get attr
:subtitle2
)))
622 (format ".TL \"%s\" \"%s\" \n%s\n"
624 (or subtitle2
"") " "))
626 ((not (or subtitle1 subtitle2
))
630 (format ".TL \"%s\" \"%s \" \n%s\n"
632 (or subtitle2
"") title
))))
635 ;; In Groff, .AU *MUST* be placed after .TL
636 ;; If From, populate with data from From else
638 (let ((author (and (plist-get info
:with-author
)
639 (let ((auth (plist-get info
:author
)))
640 (and auth
(org-export-data auth info
)))))
641 (email (and (plist-get info
:with-email
)
642 (org-export-data (plist-get info
:email
) info
)))
643 (from-data (org-e-groff--get-tagged-content "FROM" info
))
645 (to-data (org-e-groff--get-tagged-content "TO" info
)))
648 ((and author from-data
)
652 (format " \"%s\" " from-line
))
655 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
658 (format ".AU \"%s\" " author
) au-line
"\n")))
660 ((and author email
(not (string= "" email
)))
661 (format ".AU \"%s\" \"%s\"\n" author email
))
663 (author (format ".AU \"%s\"\n" author
))
668 ;; 4. Author Title, if present
669 (let ((at-item (plist-get attr
:author-title
)))
670 (if (and at-item
(stringp at-item
))
671 (format ".AT \"%s\" \n" at-item
)
675 (let ((date (org-export-data (plist-get info
:date
) info
)))
676 (and date
(format ".ND \"%s\"\n" date
)))
679 ;; If Abstract, then Populate Abstract
682 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info
))
683 (to-data (org-e-groff--get-tagged-content "TO" info
)))
686 (format ".AS\n%s\n.AE\n" abstract-data
))
688 (format ".AS\n%s\n.AE\n" to-data
))))))
690 (defun org-e-groff--letter-head (title contents attr info
)
691 (let ((author (and (plist-get info
:with-author
)
692 (let ((auth (plist-get info
:author
)))
693 (and auth
(org-export-data auth info
)))))
694 (email (and (plist-get info
:with-email
)
695 (org-export-data (plist-get info
:email
) info
)))
696 (from-data (org-e-groff--get-tagged-content "FROM" info
))
697 (at-item (plist-get attr
:author-title
))
698 (to-data (org-e-groff--get-tagged-content "TO" info
)))
701 ;; If FROM then get data from FROM
704 (replace-regexp-in-string "\\.P\n" "" from-data
))
709 (replace-regexp-in-string "\\.P\n" "" to-data
))
715 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
716 ((and author email
(not (string= "" email
)))
717 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
718 (author (format ".WA \"%s\"\n.WE\n" author
))
719 (t ".WA \"\" \n.WE\n"))
721 ;; If TO then get data from TO
724 (format ".IA \n%s\n.IE\n" to-data
)))))
729 (defun org-e-groff-template (contents info
)
730 "Return complete document string after Groff conversion.
731 CONTENTS is the transcoded contents string. INFO is a plist
732 holding export options."
733 (let* ((title (org-export-data (plist-get info
:title
) info
))
738 (list (plist-get info
:groff-class-options
))
740 (class (plist-get info
:groff-class
))
741 (class-options (plist-get info
:groff-class-options
))
742 (classes (assoc class org-e-groff-classes
))
743 (classes-options (car (last classes
)))
744 (heading-option (plist-get classes-options
:heading
))
745 (type-option (plist-get classes-options
:type
))
746 (last-option (plist-get classes-options
:last-section
))
747 (hyphenate (plist-get attr
:hyphenate
))
748 (justify-right (plist-get attr
:justify-right
))
750 (document-class-string
752 (org-element-normalize-string
753 (let* ((header (nth 1 (assoc class org-e-groff-classes
)))
754 (document-class-item (if (stringp header
) header
"")))
755 document-class-item
)))))
774 ((string= type-option
"custom") "")
776 ((and (stringp document-class-string
)
777 (string= type-option
"cover"))
780 (format ".COVER %s\n" document-class-string
)
781 (org-e-groff--mt-head title contents attr info
)
784 ((string= type-option
"memo")
786 (org-e-groff--mt-head title contents attr info
)
787 document-class-string
))
788 ((string= type-option
"letter")
790 (org-e-groff--letter-head title contents attr info
)
791 (let ((sa-item (plist-get attr
:salutation
))
792 (cn-item (plist-get attr
:confidential
))
793 (sj-item (plist-get attr
:subject
))
794 (rn-item (plist-get attr
:reference
))
795 (at-item (plist-get attr
:attention
)))
799 (if (stringp sa-item
)
800 (format ".LO SA \"%s\" \n" sa-item
)
804 (if (stringp cn-item
)
805 (format ".LO CN \"%s\"\n" cn-item
)
808 (when (and at-item
(stringp at-item
))
809 (format ".LO AT \"%s\" \n" at-item
))
810 (when (and title rn-item
)
811 (format ".LO RN \"%s\"\n" title
))
813 (when (and sj-item
(stringp sj-item
))
814 (format ".LO SJ \"%s\" \n" sj-item
))
817 ".LT " document-class-string
"\n"))))
824 ((string= last-option
"toc")
826 ((string= last-option
"sign")
827 (let ((fc-item (plist-get attr
:closing
)))
828 (concat (if (stringp fc-item
)
829 (format ".FC \"%s\" \n" fc-item
)
837 (when (string= (car item
) "NS")
838 (replace-regexp-in-string
839 "\\.P\n" "" (cdr item
))))
840 (reverse org-e-groff-special-content
) "\n")))))
844 ;;; Transcode Functions
848 ;; Babel Calls are ignored.
853 (defun org-e-groff-bold (bold contents info
)
854 "Transcode BOLD from Org to Groff.
855 CONTENTS is the text with bold markup. INFO is a plist holding
856 contextual information."
857 (org-e-groff--text-markup contents
'bold
))
861 (defun org-e-groff-center-block (center-block contents info
)
862 "Transcode a CENTER-BLOCK element from Org to Groff.
863 CONTENTS holds the contents of the center block. INFO is a plist
864 holding contextual information."
865 (org-e-groff--wrap-label
867 (format ".DS C \n%s\n.DE" contents
)))
871 (defun org-e-groff-clock (clock contents info
)
872 "Transcode a CLOCK element from Org to Groff.
873 CONTENTS is nil. INFO is a plist holding contextual
876 (format "\\fB%s\\fP " org-clock-string
)
877 (format org-e-groff-inactive-timestamp-format
878 (concat (org-translate-time (org-element-property :value clock
))
879 (let ((time (org-element-property :time clock
)))
880 (and time
(format " (%s)" time
)))))))
884 (defun org-e-groff-code (code contents info
)
885 "Transcode a CODE object from Org to Groff.
886 CONTENTS is nil. INFO is a plist used as a communication
888 (org-e-groff--text-markup (org-element-property :value code
) 'code
))
890 ;;; Comments and Comment Blocks are ignored.
894 (defun org-e-groff-drawer (drawer contents info
)
895 "Transcode a DRAWER element from Org to Groff.
896 CONTENTS holds the contents of the block. INFO is a plist
897 holding contextual information."
898 (let* ((name (org-element-property :drawer-name drawer
))
899 (output (if (functionp org-e-groff-format-drawer-function
)
900 (funcall org-e-groff-format-drawer-function
902 ;; If there's no user defined function: simply
903 ;; display contents of the drawer.
905 (org-e-groff--wrap-label drawer output
)))
909 (defun org-e-groff-dynamic-block (dynamic-block contents info
)
910 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
911 CONTENTS holds the contents of the block. INFO is a plist
912 holding contextual information. See `org-export-data'."
913 (org-e-groff--wrap-label dynamic-block contents
))
917 (defun org-e-groff-entity (entity contents info
)
918 "Transcode an ENTITY object from Org to Groff.
919 CONTENTS are the definition itself. INFO is a plist holding
920 contextual information."
921 (let ((ent (org-element-property :utf8 entity
))) ent
))
925 (defun org-e-groff-example-block (example-block contents info
)
926 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
927 CONTENTS is nil. INFO is a plist holding contextual
929 (org-e-groff--wrap-label
931 (format ".DS L\n%s\n.DE"
932 (org-export-format-code-default example-block info
))))
936 (defun org-e-groff-export-block (export-block contents info
)
937 "Transcode a EXPORT-BLOCK element from Org to Groff.
938 CONTENTS is nil. INFO is a plist holding contextual information."
939 (when (string= (org-element-property :type export-block
) "GROFF")
940 (org-remove-indentation (org-element-property :value export-block
))))
944 (defun org-e-groff-export-snippet (export-snippet contents info
)
945 "Transcode a EXPORT-SNIPPET object from Org to Groff.
946 CONTENTS is nil. INFO is a plist holding contextual information."
947 (when (eq (org-export-snippet-backend export-snippet
) 'e-groff
)
948 (org-element-property :value export-snippet
)))
952 (defun org-e-groff-fixed-width (fixed-width contents info
)
953 "Transcode a FIXED-WIDTH element from Org to Groff.
954 CONTENTS is nil. INFO is a plist holding contextual information."
955 (org-e-groff--wrap-label
957 (format "\\fC\n%s\\fP"
958 (org-remove-indentation
959 (org-element-property :value fixed-width
)))))
961 ;;; Footnote Definition
963 ;; Footnote Definitions are ignored.
965 ;; Footnotes are handled automatically in GROFF. Although manual
966 ;; references can be added, not really required.
968 (defun org-e-groff-footnote-reference (footnote-reference contents info
)
969 ;; Changing from info to footnote-reference
970 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
971 (n (org-export-get-footnote-number footnote-reference info
))
972 (data (org-trim (org-export-data raw info
)))
973 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
975 (if (string-match "fn:rl" ref-id
)
976 (if (member ref-id org-e-groff-registered-references
)
977 (format "\\*[%s]" ref-id
)
979 (push ref-id org-e-groff-registered-references
)
980 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
981 ;; else it is a footnote
982 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
986 (defun org-e-groff-headline (headline contents info
)
987 "Transcode an HEADLINE element from Org to Groff.
988 CONTENTS holds the contents of the headline. INFO is a plist
989 holding contextual information."
990 (let* ((class (plist-get info
:groff-class
))
991 (level (org-export-get-relative-level headline info
))
992 (numberedp (org-export-numbered-headline-p headline info
))
993 ;; Section formatting will set two placeholders: one for the
994 ;; title and the other for the contents.
995 (classes (assoc class org-e-groff-classes
))
996 (classes-options (car (last classes
)))
997 (heading-option (plist-get classes-options
:heading
))
1001 ((and (symbolp heading-option
)
1002 (fboundp heading-option
))
1003 (funcall heading-option level numberedp
))
1006 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
1007 ".HU \"%s\"\n%s")))))
1008 ;; End of section-fmt
1009 (text (org-export-data (org-element-property :title headline
) info
))
1011 (and (plist-get info
:with-todo-keywords
)
1012 (let ((todo (org-element-property :todo-keyword headline
)))
1013 (and todo
(org-export-data todo info
)))))
1014 (todo-type (and todo
(org-element-property :todo-type headline
)))
1015 (tags (and (plist-get info
:with-tags
)
1016 (org-export-get-tags headline info
)))
1017 (priority (and (plist-get info
:with-priority
)
1018 (org-element-property :priority headline
)))
1019 ;; Create the headline text along with a no-tag version. The
1020 ;; latter is required to remove tags from table of contents.
1021 (full-text (if (functionp org-e-groff-format-headline-function
)
1022 ;; User-defined formatting function.
1023 (funcall org-e-groff-format-headline-function
1024 todo todo-type priority text tags
)
1025 ;; Default formatting.
1028 (format "\\fB%s\\fP " todo
))
1029 (when priority
(format " [\\#%c] " priority
))
1032 (format " \\fC:%s:\\fP "
1033 (mapconcat 'identity tags
":"))))))
1035 (if (functionp org-e-groff-format-headline-function
)
1036 ;; User-defined formatting function.
1037 (funcall org-e-groff-format-headline-function
1038 todo todo-type priority text nil
)
1039 ;; Default formatting.
1041 (when todo
(format "\\fB%s\\fP " todo
))
1042 (when priority
(format " [\\#%c] " priority
))
1044 ;; Associate some \label to the headline for internal links.
1046 ;; (format "\\label{sec-%s}\n"
1047 ;; (mapconcat 'number-to-string
1048 ;; (org-export-get-headline-number headline info)
1052 (make-string (org-element-property :pre-blank headline
) 10)))
1055 ;; Case 1: Special Tag
1056 ((member (car tags
) org-e-groff-special-tags
)
1058 ((string= (car tags
) "BODY") contents
)
1060 ((string= (car tags
) "NS")
1062 (push (cons (car tags
)
1063 (format ".NS \"%s\" 1 \n%s"
1064 (car (org-element-property :title headline
))
1066 org-e-groff-special-content
) nil
))
1070 (push (cons (car tags
) contents
) org-e-groff-special-content
)
1073 ;; Case 2: This is a footnote section: ignore it.
1074 ((org-element-property :footnote-section-p headline
) nil
)
1076 ;; Case 3: This is a deep sub-tree: export it as a list item.
1077 ;; Also export as items headlines for which no section
1078 ;; format has been found.
1079 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1080 ;; Build the real contents of the sub-tree.
1081 (let ((low-level-body
1083 ;; If the headline is the first sibling, start a list.
1084 (when (org-export-first-sibling-p headline info
)
1085 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1087 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1088 ;; If headline is not the last sibling simply return
1089 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1091 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1092 (replace-regexp-in-string
1097 ;; Case 4. Standard headline. Export it as a section.
1099 (format section-fmt full-text
1100 (concat headline-label pre-blanks contents
))))))
1105 ;;; Inline Babel Call
1107 ;; Inline Babel Calls are ignored.
1109 ;;; Inline Src Block
1111 (defun org-e-groff-inline-src-block (inline-src-block contents info
)
1112 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1113 CONTENTS holds the contents of the item. INFO is a plist holding
1114 contextual information."
1115 (let* ((code (org-element-property :value inline-src-block
)))
1117 (org-e-groff-source-highlight
1118 (let* ((tmpdir (if (featurep 'xemacs
)
1120 temporary-file-directory
))
1121 (in-file (make-temp-name
1122 (expand-file-name "srchilite" tmpdir
)))
1123 (out-file (make-temp-name
1124 (expand-file-name "reshilite" tmpdir
)))
1125 (org-lang (org-element-property :language inline-src-block
))
1126 (lst-lang (cadr (assq (intern org-lang
)
1127 org-e-groff-source-highlight-langs
)))
1129 (cmd (concat (expand-file-name "source-highlight")
1131 " -f groff_mm_color "
1135 (let ((code-block ""))
1136 (with-temp-file in-file
(insert code
))
1138 (setq code-block
(org-file-contents out-file
))
1139 (delete-file in-file
)
1140 (delete-file out-file
)
1142 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1145 ;; Do not use a special package: transcode it verbatim.
1147 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1151 (defun org-e-groff-inlinetask (inlinetask contents info
)
1152 "Transcode an INLINETASK element from Org to Groff.
1153 CONTENTS holds the contents of the block. INFO is a plist
1154 holding contextual information."
1155 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1156 (todo (and (plist-get info
:with-todo-keywords
)
1157 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1158 (and todo
(org-export-data todo info
)))))
1159 (todo-type (org-element-property :todo-type inlinetask
))
1160 (tags (and (plist-get info
:with-tags
)
1161 (org-export-get-tags inlinetask info
)))
1162 (priority (and (plist-get info
:with-priority
)
1163 (org-element-property :priority inlinetask
))))
1164 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1165 ;; with appropriate arguments.
1166 (if (functionp org-e-groff-format-inlinetask-function
)
1167 (funcall org-e-groff-format-inlinetask-function
1168 todo todo-type priority title tags contents
)
1169 ;; Otherwise, use a default template.
1170 (org-e-groff--wrap-label
1174 (when todo
(format "\\fB%s\\fP " todo
))
1175 (when priority
(format " [\\#%c] " priority
))
1177 (when tags
(format " \\fC:%s:\\fP "
1178 (mapconcat 'identity tags
":"))))))
1179 (format (concat "\n.DS I\n"
1184 full-title contents
))))))
1188 (defun org-e-groff-italic (italic contents info
)
1189 "Transcode ITALIC from Org to Groff.
1190 CONTENTS is the text with italic markup. INFO is a plist holding
1191 contextual information."
1192 (org-e-groff--text-markup contents
'italic
))
1196 (defun org-e-groff-item (item contents info
)
1197 "Transcode an ITEM element from Org to Groff.
1198 CONTENTS holds the contents of the item. INFO is a plist holding
1199 contextual information."
1200 (let* ((bullet (org-element-property :bullet item
))
1201 (type (org-element-property
1202 :type
(org-element-property :parent item
)))
1203 (checkbox (case (org-element-property :checkbox item
)
1204 (on "\\o'\\(sq\\(mu'")
1206 (trans "\\o'\\(sq\\(mi'")))
1207 (tag (let ((tag (org-element-property :tag item
)))
1208 ;; Check-boxes must belong to the tag.
1209 (and tag
(format "%s"
1211 (org-export-data tag info
)))))))
1215 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1217 (org-trim (or contents
" "))))
1221 (org-trim (or contents
" "))))
1223 (let* ((bullet (org-trim bullet
))
1224 (marker (cond ((string= "-" bullet
) "\\(em")
1225 ((string= "*" bullet
) "\\(bu")
1227 (concat ".LI " marker
"\n"
1228 (org-trim (or contents
" "))))))))
1232 (defun org-e-groff-keyword (keyword contents info
)
1233 "Transcode a KEYWORD element from Org to Groff.
1234 CONTENTS is nil. INFO is a plist holding contextual information."
1235 (let ((key (org-element-property :key keyword
))
1236 (value (org-element-property :value keyword
)))
1238 ((string= key
"GROFF") value
)
1241 ;;; LaTeX Environment
1243 (defun org-e-groff-latex-environment (latex-environment contents info
)
1244 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1245 CONTENTS is nil. INFO is a plist holding contextual information."
1246 (let ((label (org-element-property :name latex-environment
))
1247 (value (org-remove-indentation
1248 (org-element-property :value latex-environment
))))
1249 (if (not (org-string-nw-p label
)) value
1250 ;; Environment is labelled: label must be within the environment
1251 ;; (otherwise, a reference pointing to that element will count
1252 ;; the section instead).
1255 (goto-char (point-min))
1257 (insert (format "%s\n" label
))
1262 (defun org-e-groff-latex-fragment (latex-fragment contents info
)
1263 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1264 CONTENTS is nil. INFO is a plist holding contextual information."
1265 (org-element-property :value latex-fragment
))
1269 (defun org-e-groff-line-break (line-break contents info
)
1270 "Transcode a LINE-BREAK object from Org to Groff.
1271 CONTENTS is nil. INFO is a plist holding contextual information."
1275 ;; Inline images just place a call to .PSPIC or .PS/.PE
1276 ;; and load the graph.
1278 (defun org-e-groff-link--inline-image (link info
)
1279 "Return Groff code for an inline image.
1280 LINK is the link pointing to the inline image. INFO is a plist
1281 used as a communication channel."
1282 (let* ((parent (org-export-get-parent-element link
))
1283 (path (let ((raw-path (org-element-property :path link
)))
1284 (if (not (file-name-absolute-p raw-path
)) raw-path
1285 (expand-file-name raw-path
))))
1286 (attr (org-export-read-attribute :attr_groff link
))
1288 (case (plist-get attr
:position
)
1293 (width (or (plist-get attr
:width
) ""))
1294 (height (or (plist-get attr
:height
) ""))
1295 (caption (and (not (plist-get attr
:disable-caption
))
1296 (org-e-groff--caption/label-string parent info
))))
1297 ;; Now clear ATTR from any special keyword and set a default value
1298 ;; if nothing is left. Return proper string.
1301 ((and org-e-groff-raster-to-ps
1302 (or (string-match ".\.png$" path
)
1303 (string-match ".\.jpg$" path
)))
1304 (let ((eps-path (concat path
".eps")))
1305 (shell-command (format org-e-groff-raster-to-ps path eps-path
))
1306 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1307 placement eps-path width height
)))
1308 ((string-match ".\.pic$" path
)
1309 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1310 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1311 placement path width height
)))
1312 (and caption
(format "\n.FG \"%s\"" caption
)))))
1314 (defun org-e-groff-link (link desc info
)
1315 "Transcode a LINK object from Org to Groff.
1317 DESC is the description part of the link, or the empty string.
1318 INFO is a plist holding contextual information. See
1321 (let* ((type (org-element-property :type link
))
1322 (raw-path (org-element-property :path link
))
1323 ;; Ensure DESC really exists, or set it to nil.
1324 (desc (and (not (string= desc
"")) desc
))
1325 (imagep (org-export-inline-image-p
1326 link org-e-groff-inline-image-rules
))
1328 ((member type
'("http" "https" "ftp" "mailto"))
1329 (concat type
":" raw-path
))
1330 ((string= type
"file")
1331 (when (string-match "\\(.+\\)::.+" raw-path
)
1332 (setq raw-path
(match-string 1 raw-path
)))
1333 (if (file-name-absolute-p raw-path
)
1334 (concat "file://" (expand-file-name raw-path
))
1335 (concat "file://" raw-path
)))
1340 (imagep (org-e-groff-link--inline-image link info
))
1341 ;; import groff files
1342 ((and (string= type
"file")
1343 (string-match ".\.groff$" raw-path
))
1344 (concat ".so " raw-path
"\n"))
1345 ;; Radio link: transcode target's contents and use them as link's
1347 ((string= type
"radio")
1348 (let ((destination (org-export-resolve-radio-link link info
)))
1350 (format "\\fI [%s] \\fP"
1351 (org-export-solidify-link-text path
)))))
1353 ;; Links pointing to an headline: find destination and build
1354 ;; appropriate referencing command.
1355 ((member type
'("custom-id" "fuzzy" "id"))
1356 (let ((destination (if (string= type
"fuzzy")
1357 (org-export-resolve-fuzzy-link link info
)
1358 (org-export-resolve-id-link link info
))))
1359 (case (org-element-type destination
)
1360 ;; Id link points to an external file.
1362 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1363 (format "\\fI file://%s \\fP" destination
)))
1364 ;; Fuzzy link points nowhere.
1366 (format org-e-groff-link-with-unknown-path-format
1369 (org-element-property :raw-link link
) info
))))
1370 ;; Fuzzy link points to an invisible target.
1372 ;; LINK points to an headline. If headlines are numbered
1373 ;; and the link has no description, display headline's
1374 ;; number. Otherwise, display description or headline's
1378 (if (and (plist-get info
:section-numbers
) (not desc
))
1379 (format "\\fI%s\\fP" label
)
1380 (format "\\fI%s\\fP"
1383 (org-element-property :title destination
) info
))))))
1384 ;; Fuzzy link points to a target. Do as above.
1386 (let ((path (org-export-solidify-link-text path
)))
1387 (if (not desc
) (format "\\fI%s\\fP" path
)
1388 (format "%s \\fBat\\fP \\fI%s\\fP" desc path
)))))))
1389 ;; External link with a description part.
1390 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1391 ;; External link without a description part.
1392 (path (format "\\fI%s\\fP" path
))
1393 ;; No path, only description. Try to do something useful.
1394 (t (format org-e-groff-link-with-unknown-path-format desc
)))))
1398 (defun org-e-groff-paragraph (paragraph contents info
)
1399 "Transcode a PARAGRAPH element from Org to Groff.
1400 CONTENTS is the contents of the paragraph, as a string. INFO is
1401 the plist used as a communication channel."
1402 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1404 (let* ((parent-type (car parent
))
1405 (fixed-paragraph "")
1406 (class (plist-get info
:groff-class
))
1407 (class-options (plist-get info
:groff-class-options
))
1408 (classes (assoc class org-e-groff-classes
))
1409 (classes-options (car (last classes
)))
1410 (paragraph-option (plist-get classes-options
:paragraph
)))
1412 ((and (symbolp paragraph-option
)
1413 (fboundp paragraph-option
))
1414 (funcall paragraph-option parent-type parent contents
))
1415 ((and (eq parent-type
'item
)
1416 (plist-get (nth 1 parent
) :bullet
))
1417 (setq fixed-paragraph
(concat "" contents
)))
1418 ((eq parent-type
'section
)
1419 (setq fixed-paragraph
(concat ".P\n" contents
)))
1420 ((eq parent-type
'footnote-definition
)
1421 (setq fixed-paragraph
(concat "" contents
)))
1422 (t (setq fixed-paragraph
(concat "" contents
))))
1427 (defun org-e-groff-plain-list (plain-list contents info
)
1428 "Transcode a PLAIN-LIST element from Org to Groff.
1429 CONTENTS is the contents of the list. INFO is a plist holding
1430 contextual information."
1431 (let* ((type (org-element-property :type plain-list
))
1432 (attr (mapconcat #'identity
1433 (org-element-property :attr_groff plain-list
)
1436 ((eq type
'ordered
) ".AL")
1437 ((eq type
'unordered
) ".BL")
1438 ((eq type
'descriptive
) ".VL 2.0i"))))
1439 (org-e-groff--wrap-label
1441 (format "%s\n%s\n.LE" groff-type contents
))))
1445 (defun org-e-groff-plain-text (text info
)
1446 "Transcode a TEXT string from Org to Groff.
1447 TEXT is the string to transcode. INFO is a plist holding
1448 contextual information."
1450 (setq text
(replace-regexp-in-string
1451 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1452 "$\\" text nil t
1))
1453 ;; Handle quotation marks
1454 (setq text
(org-e-groff--quotation-marks text info
))
1455 ;; Handle Special Characters
1456 (if org-e-groff-special-char
1457 (dolist (special-char-list org-e-groff-special-char
)
1459 (replace-regexp-in-string (car special-char-list
)
1460 (cdr special-char-list
) text
))))
1461 ;; Handle break preservation if required.
1462 (when (plist-get info
:preserve-breaks
)
1463 (setq text
(replace-regexp-in-string
1464 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text
)))
1470 (defun org-e-groff-planning (planning contents info
)
1471 "Transcode a PLANNING element from Org to Groff.
1472 CONTENTS is nil. INFO is a plist holding contextual
1479 (let ((closed (org-element-property :closed planning
)))
1482 (format "\\fR %s \\fP" org-closed-string
)
1483 (format org-e-groff-inactive-timestamp-format
1484 (org-translate-time closed
)))))
1485 (let ((deadline (org-element-property :deadline planning
)))
1488 (format "\\fB %s \\fP" org-deadline-string
)
1489 (format org-e-groff-active-timestamp-format
1490 (org-translate-time deadline
)))))
1491 (let ((scheduled (org-element-property :scheduled planning
)))
1494 (format "\\fR %s \\fP" org-scheduled-string
)
1495 (format org-e-groff-active-timestamp-format
1496 (org-translate-time scheduled
)))))))
1502 (defun org-e-groff-property-drawer (property-drawer contents info
)
1503 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1504 CONTENTS is nil. INFO is a plist holding contextual
1506 ;; The property drawer isn't exported but we want separating blank
1507 ;; lines nonetheless.
1512 (defun org-e-groff-quote-block (quote-block contents info
)
1513 "Transcode a QUOTE-BLOCK element from Org to Groff.
1514 CONTENTS holds the contents of the block. INFO is a plist
1515 holding contextual information."
1516 (org-e-groff--wrap-label
1518 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1522 (defun org-e-groff-quote-section (quote-section contents info
)
1523 "Transcode a QUOTE-SECTION element from Org to Groff.
1524 CONTENTS is nil. INFO is a plist holding contextual information."
1525 (let ((value (org-remove-indentation
1526 (org-element-property :value quote-section
))))
1527 (when value
(format ".DS L\n\\fI%s\\fP\n.DE\n" value
))))
1531 (defun org-e-groff-radio-target (radio-target text info
)
1532 "Transcode a RADIO-TARGET object from Org to Groff.
1533 TEXT is the text of the target. INFO is a plist holding
1534 contextual information."
1536 (org-export-solidify-link-text
1537 (org-element-property :value radio-target
))
1542 (defun org-e-groff-section (section contents info
)
1543 "Transcode a SECTION element from Org to Groff.
1544 CONTENTS holds the contents of the section. INFO is a plist
1545 holding contextual information."
1550 (defun org-e-groff-special-block (special-block contents info
)
1551 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1552 CONTENTS holds the contents of the block. INFO is a plist
1553 holding contextual information."
1554 (let ((type (downcase (org-element-property :type special-block
))))
1555 (org-e-groff--wrap-label
1557 (format "%s\n" contents
))))
1561 (defun org-e-groff-src-block (src-block contents info
)
1562 "Transcode a SRC-BLOCK element from Org to Groff.
1563 CONTENTS holds the contents of the item. INFO is a plist holding
1564 contextual information."
1565 (let* ((lang (org-element-property :language src-block
))
1566 (label (org-element-property :name src-block
))
1567 (code (org-element-property :value src-block
))
1568 (custom-env (and lang
1569 (cadr (assq (intern lang
)
1570 org-e-groff-custom-lang-environments
))))
1571 (num-start (case (org-element-property :number-lines src-block
)
1572 (continued (org-export-get-loc src-block info
))
1574 (retain-labels (org-element-property :retain-labels src-block
))
1575 (caption (and (not (org-export-read-attribute
1576 :attr_groff src-block
:disable-caption
))
1577 (org-e-groff--caption/label-string src-block info
))))
1580 ;; Case 1. No source fontification.
1581 ((not org-e-groff-source-highlight
)
1583 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1584 (org-export-format-code-default src-block info
))
1585 (and caption
(format ".EX \"%s\" " caption
))))
1587 ;; Case 2. Source fontification.
1588 (org-e-groff-source-highlight
1589 (let* ((tmpdir (if (featurep 'xemacs
)
1591 temporary-file-directory
))
1592 (in-file (make-temp-name
1593 (expand-file-name "srchilite" tmpdir
)))
1594 (out-file (make-temp-name
1595 (expand-file-name "reshilite" tmpdir
)))
1597 (org-lang (org-element-property :language src-block
))
1598 (lst-lang (cadr (assq (intern org-lang
)
1599 org-e-groff-source-highlight-langs
)))
1601 (cmd (concat "source-highlight"
1603 " -f groff_mm_color "
1609 (let ((code-block ""))
1610 (with-temp-file in-file
(insert code
))
1612 (setq code-block
(org-file-contents out-file
))
1613 (delete-file in-file
)
1614 (delete-file out-file
)
1615 (format "%s\n" code-block
))
1616 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1618 (and caption
(format ".EX \"%s\" " caption
))))))))
1621 ;;; Statistics Cookie
1623 (defun org-e-groff-statistics-cookie (statistics-cookie contents info
)
1624 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1625 CONTENTS is nil. INFO is a plist holding contextual information."
1626 (org-element-property :value statistics-cookie
))
1631 (defun org-e-groff-strike-through (strike-through contents info
)
1632 "Transcode STRIKE-THROUGH from Org to Groff.
1633 CONTENTS is the text with strike-through markup. INFO is a plist
1634 holding contextual information."
1635 (org-e-groff--text-markup contents
'strike-through
))
1639 (defun org-e-groff-subscript (subscript contents info
)
1640 "Transcode a SUBSCRIPT object from Org to Groff.
1641 CONTENTS is the contents of the object. INFO is a plist holding
1642 contextual information."
1643 (format "\\d\\s-2%s\\s+2\\u" contents
))
1645 ;;; Superscript "^_%s$
1647 (defun org-e-groff-superscript (superscript contents info
)
1648 "Transcode a SUPERSCRIPT object from Org to Groff.
1649 CONTENTS is the contents of the object. INFO is a plist holding
1650 contextual information."
1651 (format "\\u\\s-2%s\\s+2\\d" contents
))
1656 ;; `org-e-groff-table' is the entry point for table transcoding. It
1657 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1658 ;; delegates the job to `org-e-groff-table--org-table' function,
1659 ;; depending of the type of the table.
1661 ;; `org-e-groff-table--align-string' is a subroutine used to build
1662 ;; alignment string for Org tables.
1664 (defun org-e-groff-table (table contents info
)
1665 "Transcode a TABLE element from Org to Groff.
1666 CONTENTS is the contents of the table. INFO is a plist holding
1667 contextual information."
1669 ;; Case 1: verbatim table.
1670 ((or org-e-groff-tables-verbatim
1671 (let ((attr (read (format "(%s)"
1674 (org-element-property :attr_groff table
) " ")))))
1675 (and attr
(plist-get attr
:verbatim
))))
1677 (format ".DS L\n\\fC%s\\fP\n.DE"
1678 ;; Re-create table, without affiliated keywords.
1680 (org-element-interpret-data
1681 `(table nil
,@(org-element-contents table
))))))
1683 ;; Case 2: Standard table.
1684 (t (org-e-groff-table--org-table table contents info
))))
1686 (defun org-e-groff-table--align-string (divider table info
)
1687 "Return an appropriate Groff alignment string.
1688 TABLE is the considered table. INFO is a plist used as
1689 a communication channel."
1691 ;; Extract column groups and alignment from first (non-rule)
1697 (and (eq (org-element-property :type row
) 'standard
) row
))
1701 (let* ((borders (org-export-table-cell-borders cell info
))
1702 (raw-width (org-export-table-cell-width cell info
))
1703 (width-cm (when raw-width
(/ raw-width
5)))
1704 (width (if raw-width
(format "w(%dc)"
1705 (if (< width-cm
1) 1 width-cm
)) "")))
1706 ;; Check left border for the first cell only.
1707 ;; Alignment is nil on assignment
1709 (when (and (memq 'left borders
) (not alignment
))
1710 (push "|" alignment
))
1712 (case (org-export-table-cell-alignment cell info
)
1713 (left (concat "l" width divider
))
1714 (right (concat "r" width divider
))
1715 (center (concat "c" width divider
)))
1717 (when (memq 'right borders
) (push "|" alignment
))))
1719 (apply 'concat
(reverse alignment
))))
1721 (defun org-e-groff-table--org-table (table contents info
)
1722 "Return appropriate Groff code for an Org table.
1724 TABLE is the table type element to transcode. CONTENTS is its
1725 contents, as a string. INFO is a plist used as a communication
1728 This function assumes TABLE has `org' as its `:type' attribute."
1729 (let* ((attr (org-export-read-attribute :attr_groff table
))
1730 (label (org-element-property :name table
))
1731 (caption (and (not (plist-get attr
:disable-caption
))
1732 (org-e-groff--caption/label-string table info
)))
1733 (divider (if (plist-get attr
:divider
) "|" " "))
1735 ;; Determine alignment string.
1736 (alignment (org-e-groff-table--align-string divider table info
))
1738 ;; Extract others display options.
1740 (lines (org-split-string contents
"\n"))
1746 (if (plist-get attr
:expand
)
1749 (case (plist-get attr
:placement
)
1753 (if org-e-groff-tables-centered
1756 (case (plist-get attr
:boxtype
)
1758 ('doublebox
"doublebox")
1763 (if (not (null attr-item
))
1764 (add-to-list 'result-list attr-item
)))
1767 (title-line (plist-get attr
:title-line
))
1768 (long-cells (plist-get attr
:long-cells
))
1773 (or (car attr-list
) ""))
1776 (when (cdr attr-list
)
1777 (dolist (attr-item (cdr attr-list
))
1778 (setq output-list
(concat output-list
1779 (format ",%s" attr-item
)))))
1782 (when lines
(org-split-string (car lines
) "\t"))))
1783 ;; Prepare the final format string for the table.
1789 (concat ".TS\n " table-format
";\n"
1791 (let ((final-line ""))
1793 (dotimes (i (length first-line
))
1794 (setq final-line
(concat final-line
"cb" divider
))))
1796 (setq final-line
(concat final-line
"\n"))
1799 (setq final-line
(concat final-line alignment
))
1800 (dotimes (i (length first-line
))
1801 (setq final-line
(concat final-line
"c" divider
))))
1805 (let ((final-line "")
1807 (lines (org-split-string contents
"\n")))
1809 (dolist (line-item lines
)
1814 (if (string= line-item
"_")
1815 (setq long-line
(format "%s\n" line-item
))
1817 (let ((cell-item-list (org-split-string line-item
"\t")))
1818 (dolist (cell-item cell-item-list
)
1820 (cond ((eq cell-item
(car (last cell-item-list
)))
1821 (setq long-line
(concat long-line
1822 (format "T{\n%s\nT}\t\n" cell-item
))))
1824 (setq long-line
(concat long-line
1825 (format "T{\n%s\nT}\t" cell-item
))))))
1828 (setq final-line
(concat final-line long-line
)))
1830 (setq final-line
(concat final-line line-item
"\n"))))
1833 (if caption
(format ".TB \"%s\"" caption
) ""))))))
1837 (defun org-e-groff-table-cell (table-cell contents info
)
1838 "Transcode a TABLE-CELL element from Org to Groff
1839 CONTENTS is the cell contents. INFO is a plist used as
1840 a communication channel."
1842 (concat (if (and contents
1843 org-e-groff-table-scientific-notation
1844 (string-match orgtbl-exp-regexp contents
))
1845 ;; Use appropriate format string for scientific
1847 (format org-e-groff-table-scientific-notation
1848 (match-string 1 contents
)
1849 (match-string 2 contents
))
1851 (when (org-export-get-next-element table-cell info
) "\t"))))
1856 (defun org-e-groff-table-row (table-row contents info
)
1857 "Transcode a TABLE-ROW element from Org to Groff
1858 CONTENTS is the contents of the row. INFO is a plist used as
1859 a communication channel."
1860 ;; Rules are ignored since table separators are deduced from
1861 ;; borders of the current row.
1862 (when (eq (org-element-property :type table-row
) 'standard
)
1863 (let* ((attr (mapconcat 'identity
1864 (org-element-property
1865 :attr_groff
(org-export-get-parent table-row
))
1867 ;; TABLE-ROW's borders are extracted from its first cell.
1869 (org-export-table-cell-borders
1870 (car (org-element-contents table-row
)) info
)))
1872 ;; Mark horizontal lines
1873 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1876 ;; When BOOKTABS are activated enforce bottom rule even when
1877 ;; no hline was specifically marked.
1878 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1879 ((memq 'below borders
) "\n_"))))))
1883 (defun org-e-groff-target (target contents info
)
1884 "Transcode a TARGET object from Org to Groff.
1885 CONTENTS is nil. INFO is a plist holding contextual
1887 (format "\\fI%s\\fP"
1888 (org-export-solidify-link-text (org-element-property :value target
))))
1892 (defun org-e-groff-timestamp (timestamp contents info
)
1893 "Transcode a TIMESTAMP object from Org to Groff.
1894 CONTENTS is nil. INFO is a plist holding contextual
1896 (let ((value (org-translate-time (org-element-property :value timestamp
)))
1897 (type (org-element-property :type timestamp
)))
1898 (cond ((memq type
'(active active-range
))
1899 (format org-e-groff-active-timestamp-format value
))
1900 ((memq type
'(inactive inactive-range
))
1901 (format org-e-groff-inactive-timestamp-format value
))
1902 (t (format org-e-groff-diary-timestamp-format value
)))))
1906 (defun org-e-groff-underline (underline contents info
)
1907 "Transcode UNDERLINE from Org to Groff.
1908 CONTENTS is the text with underline markup. INFO is a plist
1909 holding contextual information."
1910 (org-e-groff--text-markup contents
'underline
))
1914 (defun org-e-groff-verbatim (verbatim contents info
)
1915 "Transcode a VERBATIM object from Org to Groff.
1916 CONTENTS is nil. INFO is a plist used as a communication
1918 (org-e-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1922 (defun org-e-groff-verse-block (verse-block contents info
)
1923 "Transcode a VERSE-BLOCK element from Org to Groff.
1924 CONTENTS is verse block contents. INFO is a plist holding
1925 contextual information."
1926 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1929 ;;; Interactive functions
1931 (defun org-e-groff-export-to-groff
1932 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1933 "Export current buffer to a Groff file.
1935 If narrowing is active in the current buffer, only export its
1938 If a region is active, export that region.
1940 When optional argument SUBTREEP is non-nil, export the sub-tree
1941 at point, extracting information from the headline properties
1944 When optional argument VISIBLE-ONLY is non-nil, don't export
1945 contents of hidden elements.
1947 EXT-PLIST, when provided, is a property list with external
1948 parameters overriding Org default settings, but still inferior to
1949 file-local settings.
1951 When optional argument PUB-DIR is set, use it as the publishing
1954 Return output file's name."
1956 (setq org-e-groff-registered-references nil
)
1957 (setq org-e-groff-special-content nil
)
1958 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir
)))
1960 'e-groff outfile subtreep visible-only body-only ext-plist
)))
1962 (defun org-e-groff-export-to-pdf
1963 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1964 "Export current buffer to Groff then process through to PDF.
1966 If narrowing is active in the current buffer, only export its
1969 If a region is active, export that region.
1971 When optional argument SUBTREEP is non-nil, export the sub-tree
1972 at point, extracting information from the headline properties
1975 When optional argument VISIBLE-ONLY is non-nil, don't export
1976 contents of hidden elements.
1978 EXT-PLIST, when provided, is a property list with external
1979 parameters overriding Org default settings, but still inferior to
1980 file-local settings.
1982 When optional argument PUB-DIR is set, use it as the publishing
1985 Return PDF file's name."
1987 (org-e-groff-compile
1988 (org-e-groff-export-to-groff
1989 subtreep visible-only body-only ext-plist pub-dir
)))
1991 (defun org-e-groff-compile (grofffile)
1992 "Compile a Groff file.
1994 GROFFFILE is the name of the file being compiled. Processing is
1995 done through the command specified in `org-e-groff-pdf-process'.
1997 Return PDF file name or an error if it couldn't be produced."
1998 (let* ((wconfig (current-window-configuration))
1999 (grofffile (file-truename grofffile
))
2000 (base (file-name-sans-extension grofffile
))
2002 (message (format "Processing Groff file %s ..." grofffile
))
2006 ;; A function is provided: Apply it.
2007 ((functionp org-e-groff-pdf-process
)
2008 (funcall org-e-groff-pdf-process
(shell-quote-argument grofffile
)))
2009 ;; A list is provided: Replace %b, %f and %o with appropriate
2010 ;; values in each command before applying it. Output is
2011 ;; redirected to "*Org PDF Groff Output*" buffer.
2012 ((consp org-e-groff-pdf-process
)
2013 (let* ((out-dir (or (file-name-directory grofffile
) "./"))
2014 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
2018 (replace-regexp-in-string
2019 "%b" (shell-quote-argument base
)
2020 (replace-regexp-in-string
2021 "%f" (shell-quote-argument grofffile
)
2022 (replace-regexp-in-string
2023 "%o" (shell-quote-argument out-dir
) command t t
)
2026 org-e-groff-pdf-process
)
2027 ;; Collect standard errors from output buffer.
2028 (setq errors
(org-e-groff-collect-errors outbuf
))))
2029 (t (error "No valid command to process to PDF")))
2030 (let ((pdffile (concat base
".pdf")))
2031 ;; Check for process failure. Provide collected errors if
2033 (if (not (file-exists-p pdffile
))
2034 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2035 (when errors
(concat ": " errors
))))
2036 ;; Else remove log files, when specified, and signal end of
2037 ;; process to user, along with any error encountered.
2038 (when org-e-groff-remove-logfiles
2039 (dolist (ext org-e-groff-logfiles-extensions
)
2040 (let ((file (concat base
"." ext
)))
2041 (when (file-exists-p file
) (delete-file file
)))))
2042 (message (concat "Process completed"
2043 (if (not errors
) "."
2044 (concat " with errors: " errors
)))))
2045 ;; Return output file name.
2047 (set-window-configuration wconfig
))))
2049 (defun org-e-groff-collect-errors (buffer)
2050 "Collect some kind of errors from \"groff\" output
2051 BUFFER is the buffer containing output.
2052 Return collected error types as a string, or nil if there was
2054 (with-current-buffer buffer
2056 (goto-char (point-max))
2061 (provide 'org-e-groff
)
2062 ;;; org-e-groff.el ends here