1 ;;; ox-groff.el --- Groff Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements a Groff Memorandum Macro back-end for Org
30 ;; M-: (org-export-to-buffer 'groff "*Test Groff*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Groff
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
36 ;; "GROFF_CLASS_OPTIONS".
40 (eval-when-compile (require 'cl
))
43 (defvar orgtbl-exp-regexp
)
48 (org-export-define-backend 'groff
49 '((bold . org-groff-bold
)
50 (center-block . org-groff-center-block
)
51 (clock . org-groff-clock
)
52 (code . org-groff-code
)
53 (drawer . org-groff-drawer
)
54 (dynamic-block . org-groff-dynamic-block
)
55 (entity . org-groff-entity
)
56 (example-block . org-groff-example-block
)
57 (export-block . org-groff-export-block
)
58 (export-snippet . org-groff-export-snippet
)
59 (fixed-width . org-groff-fixed-width
)
60 (footnote-definition . org-groff-footnote-definition
)
61 (footnote-reference . org-groff-footnote-reference
)
62 (headline . org-groff-headline
)
63 (horizontal-rule . org-groff-horizontal-rule
)
64 (inline-src-block . org-groff-inline-src-block
)
65 (inlinetask . org-groff-inlinetask
)
66 (italic . org-groff-italic
)
67 (item . org-groff-item
)
68 (keyword . org-groff-keyword
)
69 (line-break . org-groff-line-break
)
70 (link . org-groff-link
)
71 (node-property . org-groff-node-property
)
72 (paragraph . org-groff-paragraph
)
73 (plain-list . org-groff-plain-list
)
74 (plain-text . org-groff-plain-text
)
75 (planning . org-groff-planning
)
76 (property-drawer . org-groff-property-drawer
)
77 (quote-block . org-groff-quote-block
)
78 (radio-target . org-groff-radio-target
)
79 (section . org-groff-section
)
80 (special-block . org-groff-special-block
)
81 (src-block . org-groff-src-block
)
82 (statistics-cookie . org-groff-statistics-cookie
)
83 (strike-through . org-groff-strike-through
)
84 (subscript . org-groff-subscript
)
85 (superscript . org-groff-superscript
)
86 (table . org-groff-table
)
87 (table-cell . org-groff-table-cell
)
88 (table-row . org-groff-table-row
)
89 (target . org-groff-target
)
90 (template . org-groff-template
)
91 (timestamp . org-groff-timestamp
)
92 (underline . org-groff-underline
)
93 (verbatim . org-groff-verbatim
)
94 (verse-block . org-groff-verse-block
))
97 '(?g
"Export to GROFF"
98 ((?g
"As GROFF file" org-groff-export-to-groff
)
99 (?p
"As PDF file" org-groff-export-to-pdf
)
100 (?o
"As PDF file and open"
102 (if a
(org-groff-export-to-pdf t s v b
)
103 (org-open-file (org-groff-export-to-pdf nil s v b
)))))))
105 '((:groff-class
"GROFF_CLASS" nil org-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-groff nil
114 "Options for exporting Org mode files to Groff."
115 :tag
"Org Export Groff"
120 (defcustom org-groff-default-class
"internal"
121 "The default Groff class."
122 :group
'org-export-groff
123 :type
'(string :tag
"Groff class"))
125 (defcustom org-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-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"))))))
175 (defconst org-groff-special-tags
176 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
178 (defcustom org-groff-format-headline-function nil
179 "Function to format headline text.
181 This function will be called with 5 arguments:
182 TODO the todo keyword (string or nil).
183 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
184 PRIORITY the priority of the headline (integer or nil)
185 TEXT the main headline text (string).
186 TAGS the tags as a list of strings (list of strings or nil).
188 The function result will be used in the section format string.
190 As an example, one could set the variable to the following, in
191 order to reproduce the default set-up:
193 \(defun org-groff-format-headline (todo todo-type priority text tags)
194 \"Default format function for a headline.\"
196 \(format \"\\fB%s\\fP \" todo))
198 \(format \"[\\#%c] \" priority))
202 \(mapconcat 'identity tags \":\"))))"
203 :group
'org-export-groff
208 (defcustom org-groff-active-timestamp-format
"\\fI%s\\fP"
209 "A printf format string to be applied to active timestamps."
210 :group
'org-export-groff
213 (defcustom org-groff-inactive-timestamp-format
"\\fI%s\\fP"
214 "A printf format string to be applied to inactive timestamps."
215 :group
'org-export-groff
218 (defcustom org-groff-diary-timestamp-format
"\\fI%s\\fP"
219 "A printf format string to be applied to diary timestamps."
220 :group
'org-export-groff
225 (defcustom org-groff-inline-image-rules
226 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
227 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
228 "Rules characterizing image files that can be inlined into Groff.
230 A rule consists in an association whose key is the type of link
231 to consider, and value is a regexp that will be matched against
234 Note that, by default, the image extensions actually allowed
235 depend on the way the Groff file is processed. When used with
236 pdfgroff, pdf, jpg and png images are OK. When processing
237 through dvi to Postscript, only ps and eps are allowed. The
238 default we use here encompasses both."
239 :group
'org-export-groff
240 :type
'(alist :key-type
(string :tag
"Type")
241 :value-type
(regexp :tag
"Path")))
243 (defcustom org-groff-link-with-unknown-path-format
"\\fI%s\\fP"
244 "Format string for links with unknown path type."
245 :group
'org-export-groff
250 (defcustom org-groff-tables-centered t
251 "When non-nil, tables are exported in a center environment."
252 :group
'org-export-groff
255 (defcustom org-groff-tables-verbatim nil
256 "When non-nil, tables are exported verbatim."
257 :group
'org-export-groff
260 (defcustom org-groff-table-scientific-notation
"%sE%s"
261 "Format string to display numbers in scientific notation.
262 The format should have \"%s\" twice, for mantissa and exponent
263 \(i.e. \"%s\\\\times10^{%s}\").
265 When nil, no transformation is made."
266 :group
'org-export-groff
268 (string :tag
"Format string")
269 (const :tag
"No formatting")))
273 (defcustom org-groff-text-markup-alist
274 '((bold .
"\\fB%s\\fP")
275 (code .
"\\fC%s\\fP")
276 (italic .
"\\fI%s\\fP")
277 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
278 (underline .
"\\fI%s\\fP") ; need to be revised.
279 (verbatim .
"protectedtexttt"))
280 "Alist of Groff expressions to convert text markup.
282 The key must be a symbol among `bold', `code', `italic',
283 `strike-through', `underline' and `verbatim'. The value is
284 a formatting string to wrap fontified text with it.
286 If no association can be found for a given markup, text will be
288 :group
'org-export-groff
290 :options
'(bold code italic strike-through underline verbatim
))
294 (defcustom org-groff-format-drawer-function nil
295 "Function called to format a drawer in Groff code.
297 The function must accept two parameters:
298 NAME the drawer name, like \"LOGBOOK\"
299 CONTENTS the contents of the drawer.
301 The function should return the string to be exported.
303 For example, the variable could be set to the following function
304 in order to mimic default behaviour:
306 \(defun org-groff-format-drawer-default \(name contents\)
307 \"Format a drawer element for Groff export.\"
309 :group
'org-export-groff
314 (defcustom org-groff-format-inlinetask-function nil
315 "Function called to format an inlinetask in Groff code.
317 The function must accept six parameters:
318 TODO the todo keyword, as a string
319 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
320 PRIORITY the inlinetask priority, as a string
321 NAME the inlinetask name, as a string.
322 TAGS the inlinetask tags, as a list of strings.
323 CONTENTS the contents of the inlinetask, as a string.
325 The function should return the string to be exported.
327 For example, the variable could be set to the following function
328 in order to mimic default behaviour:
330 \(defun org-groff-format-inlinetask \(todo type priority name tags contents\)
331 \"Format an inline task element for Groff export.\"
335 \(format \"\\fB%s\\fP \" todo))
336 \(when priority (format \"[\\#%c] \" priority))
340 \(mapconcat 'identity tags \":\")))))
341 \(format (concat \".DS L\\n\"
345 full-title contents))"
346 :group
'org-export-groff
351 (defcustom org-groff-source-highlight nil
352 "Use GNU source highlight to embellish source blocks "
353 :group
'org-export-groff
356 (defcustom org-groff-source-highlight-langs
357 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
359 (c "c") (cc "cpp") (csharp "csharp") (d "d")
360 (fortran "fortran") (cobol "cobol") (pascal "pascal")
361 (ada "ada") (asm "asm")
362 (perl "perl") (cperl "perl")
363 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
364 (java "java") (javascript "javascript")
366 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
367 (ocaml "caml") (caml "caml")
368 (sql "sql") (sqlite "sql")
369 (html "html") (css "css") (xml "xml")
370 (bat "bat") (bison "bison") (clipper "clipper")
371 (ldap "ldap") (opa "opa")
372 (php "php") (postscript "postscript") (prolog "prolog")
373 (properties "properties") (makefile "makefile")
374 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
375 "Alist mapping languages to their listing language counterpart.
376 The key is a symbol, the major mode symbol without the \"-mode\".
377 The value is the string that should be inserted as the language
378 parameter for the listings package. If the mode name and the
379 listings name are the same, the language does not need an entry
380 in this list - but it does not hurt if it is present."
381 :group
'org-export-groff
384 (symbol :tag
"Major mode ")
385 (string :tag
"Listings language"))))
387 (defcustom org-groff-source-highlight-options nil
388 "Association list of options for the groff listings package.
390 These options are supplied as a comma-separated list to the
391 \\lstset command. Each element of the association list should be
392 a list containing two strings: the name of the option, and the
395 (setq org-groff-source-highlight-options
396 '((\"basicstyle\" \"\\small\")
397 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
399 will typeset the code in a small size font with underlined, bold
402 Note that the same options will be applied to blocks of all
404 :group
'org-export-groff
407 (string :tag
"Listings option name ")
408 (string :tag
"Listings option value"))))
410 (defvar org-groff-custom-lang-environments nil
411 "Alist mapping languages to language-specific Groff environments.
413 It is used during export of src blocks by the listings and
414 groff packages. For example,
416 \(setq org-groff-custom-lang-environments
417 '\(\(python \"pythoncode\"\)\)\)
419 would have the effect that if org encounters begin_src python
420 during groff export it will use pythoncode as the source-highlight
425 (defcustom org-groff-special-char
426 '(("(c)" .
"\\\\(co")
428 ("(rg)" .
"\\\\(rg"))
429 "CONS list in which the value of the car
430 is replace on the value of the CDR. "
431 :group
'org-export-groff
433 (cons :tag
"Character Subtitute"
434 (string :tag
"Original Character Group")
435 (string :tag
"Replacement Character"))))
439 (defcustom org-groff-pdf-process
440 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
441 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
442 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
444 "Commands to process a Groff file to a PDF file.
445 This is a list of strings, each of them will be given to the
446 shell as a command. %f in the command will be replaced by the
447 full file name, %b by the file base name \(i.e. without
448 extension) and %o by the base directory of the file."
449 :group
'org-export-pdf
451 (repeat :tag
"Shell command sequence"
452 (string :tag
"Shell command"))
453 (const :tag
"2 runs of pdfgroff"
454 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
455 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
456 (const :tag
"3 runs of pdfgroff"
457 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
458 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
459 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
462 (defcustom org-groff-logfiles-extensions
463 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
464 "The list of file extensions to consider as Groff logfiles."
465 :group
'org-export-groff
466 :type
'(repeat (string :tag
"Extension")))
468 (defcustom org-groff-remove-logfiles t
469 "Non-nil means remove the logfiles produced by PDF production.
470 These are the .aux, .log, .out, and .toc files."
471 :group
'org-export-groff
474 (defcustom org-groff-organization
"Org User"
475 "Name of the organization used to populate the .AF command."
476 :group
'org-export-groff
479 (defcustom org-groff-raster-to-ps nil
480 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
481 `org-groff-inline-image-rules' is adjusted accordingly if not conversion is being
482 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
483 :group
'org-export-groff
485 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
486 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
487 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
488 (const :tag
"None" nil
)))
490 (defvar org-groff-registered-references nil
)
491 (defvar org-groff-special-content nil
)
495 ;;; Internal Functions
497 (defun org-groff--caption/label-string
(element info
)
498 "Return caption and label Groff string for ELEMENT.
500 INFO is a plist holding contextual information. If there's no
501 caption nor label, return the empty string.
503 For non-floats, see `org-groff--wrap-label'."
504 (let ((main (org-export-get-caption element
))
505 (short (org-export-get-caption element t
))
506 (label (org-element-property :name element
)))
507 (cond ((and (not main
) (not label
)) "")
508 ((not main
) (format "\\fI%s\\fP" label
))
509 ;; Option caption format with short name.
510 (short (format "%s\n.br\n - %s\n"
511 (org-export-data short info
)
512 (org-export-data main info
)))
513 ;; Standard caption format.
514 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
516 (defun org-groff--wrap-label (element output
)
517 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
518 This function shouldn't be used for floats. See
519 `org-groff--caption/label-string'."
520 (let ((label (org-element-property :name element
)))
521 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
523 (concat (format "%s\n.br\n" label
) output
))))
525 (defun org-groff--text-markup (text markup
)
526 "Format TEXT depending on MARKUP text markup.
527 See `org-groff-text-markup-alist' for details."
528 (let ((fmt (cdr (assq markup org-groff-text-markup-alist
))))
530 ;; No format string: Return raw text.
532 ((string= "protectedtexttt" fmt
)
534 (trans '(("\\" .
"\\")))
537 (while (string-match "[\\{}$%&_#~^]" text
)
538 (setq char
(match-string 0 text
))
539 (if (> (match-beginning 0) 0)
540 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
541 (setq text
(substring text
(1+ (match-beginning 0))))
542 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
543 rtn
(concat rtn char
)))
544 (setq text
(concat rtn text
))
545 (format "\\fC%s\\fP" text
)))
546 ;; Else use format string.
547 (t (format fmt text
)))))
550 (defun org-groff--get-tagged-content (tag info
)
551 (cdr (assoc tag org-groff-special-content
)))
553 (defun org-groff--mt-head (title contents attr info
)
556 ;; 1. Insert Organization
557 (let ((firm-option (plist-get attr
:firm
)))
559 ((stringp firm-option
)
560 (format ".AF \"%s\" \n" firm-option
))
561 (t (format ".AF \"%s\" \n" (or org-groff-organization
"")))))
564 (let ((title (if (plist-get info
:with-title
) title
""))
565 (subtitle1 (plist-get attr
:subtitle1
))
566 (subtitle2 (plist-get attr
:subtitle2
)))
570 (format ".TL \"%s\" \"%s\" \n%s\n"
572 (or subtitle2
"") " "))
574 ((not (or subtitle1 subtitle2
))
578 (format ".TL \"%s\" \"%s \" \n%s\n"
580 (or subtitle2
"") title
))))
583 ;; In Groff, .AU *MUST* be placed after .TL
584 ;; If From, populate with data from From else
586 (let ((author (and (plist-get info
:with-author
)
587 (let ((auth (plist-get info
:author
)))
588 (and auth
(org-export-data auth info
)))))
589 (email (and (plist-get info
:with-email
)
590 (org-export-data (plist-get info
:email
) info
)))
591 (from-data (org-groff--get-tagged-content "FROM" info
))
593 (to-data (org-groff--get-tagged-content "TO" info
)))
596 ((and author from-data
)
600 (format " \"%s\" " from-line
))
603 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
606 (format ".AU \"%s\" " author
) au-line
"\n")))
608 ((and author email
(not (string= "" email
)))
609 (format ".AU \"%s\" \"%s\"\n" author email
))
611 (author (format ".AU \"%s\"\n" author
))
616 ;; 4. Author Title, if present
617 (let ((at-item (plist-get attr
:author-title
)))
618 (if (and at-item
(stringp at-item
))
619 (format ".AT \"%s\" \n" at-item
)
623 (when (plist-get info
:with-date
)
624 (let ((date (org-export-data (org-export-get-date info
) info
)))
625 (and (org-string-nw-p date
) (format ".ND \"%s\"\n" date
))))
628 ;; If Abstract, then Populate Abstract
631 (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info
))
632 (to-data (org-groff--get-tagged-content "TO" info
)))
635 (format ".AS\n%s\n.AE\n" abstract-data
))
637 (format ".AS\n%s\n.AE\n" to-data
))))))
639 (defun org-groff--letter-head (title contents attr info
)
640 (let ((author (and (plist-get info
:with-author
)
641 (let ((auth (plist-get info
:author
)))
642 (and auth
(org-export-data auth info
)))))
643 (email (and (plist-get info
:with-email
)
644 (org-export-data (plist-get info
:email
) info
)))
645 (from-data (org-groff--get-tagged-content "FROM" info
))
646 (at-item (plist-get attr
:author-title
))
647 (to-data (org-groff--get-tagged-content "TO" info
)))
650 ;; If FROM then get data from FROM
653 (replace-regexp-in-string "\\.P\n" "" from-data
))
658 (replace-regexp-in-string "\\.P\n" "" to-data
))
664 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
665 ((and author email
(not (string= "" email
)))
666 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
667 (author (format ".WA \"%s\"\n.WE\n" author
))
668 (t ".WA \"\" \n.WE\n"))
670 ;; If TO then get data from TO
673 (format ".IA \n%s\n.IE\n" to-data
)))))
678 (defun org-groff-template (contents info
)
679 "Return complete document string after Groff conversion.
680 CONTENTS is the transcoded contents string. INFO is a plist
681 holding export options."
682 (let* ((title (org-export-data (plist-get info
:title
) info
))
687 (list (plist-get info
:groff-class-options
))
689 (class (plist-get info
:groff-class
))
690 (class-options (plist-get info
:groff-class-options
))
691 (classes (assoc class org-groff-classes
))
692 (classes-options (car (last classes
)))
693 (heading-option (plist-get classes-options
:heading
))
694 (type-option (plist-get classes-options
:type
))
695 (last-option (plist-get classes-options
:last-section
))
696 (hyphenate (plist-get attr
:hyphenate
))
697 (justify-right (plist-get attr
:justify-right
))
699 (document-class-string
701 (org-element-normalize-string
702 (let* ((header (nth 1 (assoc class org-groff-classes
)))
703 (document-class-item (if (stringp header
) header
"")))
704 document-class-item
)))))
723 ((string= type-option
"custom") "")
725 ((and (stringp document-class-string
)
726 (string= type-option
"cover"))
729 (format ".COVER %s\n" document-class-string
)
730 (org-groff--mt-head title contents attr info
)
733 ((string= type-option
"memo")
735 (org-groff--mt-head title contents attr info
)
736 document-class-string
))
737 ((string= type-option
"letter")
739 (org-groff--letter-head title contents attr info
)
740 (let ((sa-item (plist-get attr
:salutation
))
741 (cn-item (plist-get attr
:confidential
))
742 (sj-item (plist-get attr
:subject
))
743 (rn-item (plist-get attr
:reference
))
744 (at-item (plist-get attr
:attention
)))
748 (if (stringp sa-item
)
749 (format ".LO SA \"%s\" \n" sa-item
)
753 (if (stringp cn-item
)
754 (format ".LO CN \"%s\"\n" cn-item
)
757 (when (and at-item
(stringp at-item
))
758 (format ".LO AT \"%s\" \n" at-item
))
759 (when (and title rn-item
)
760 (format ".LO RN \"%s\"\n" title
))
762 (when (and sj-item
(stringp sj-item
))
763 (format ".LO SJ \"%s\" \n" sj-item
))
766 ".LT " document-class-string
"\n"))))
773 ((string= last-option
"toc")
775 ((string= last-option
"sign")
776 (let ((fc-item (plist-get attr
:closing
)))
777 (concat (if (stringp fc-item
)
778 (format ".FC \"%s\" \n" fc-item
)
786 (when (string= (car item
) "NS")
787 (replace-regexp-in-string
788 "\\.P\n" "" (cdr item
))))
789 (reverse org-groff-special-content
) "\n")))))
793 ;;; Transcode Functions
797 ;; Babel Calls are ignored.
802 (defun org-groff-bold (bold contents info
)
803 "Transcode BOLD from Org to Groff.
804 CONTENTS is the text with bold markup. INFO is a plist holding
805 contextual information."
806 (org-groff--text-markup contents
'bold
))
810 (defun org-groff-center-block (center-block contents info
)
811 "Transcode a CENTER-BLOCK element from Org to Groff.
812 CONTENTS holds the contents of the center block. INFO is a plist
813 holding contextual information."
814 (org-groff--wrap-label
816 (format ".DS C \n%s\n.DE" contents
)))
820 (defun org-groff-clock (clock contents info
)
821 "Transcode a CLOCK element from Org to Groff.
822 CONTENTS is nil. INFO is a plist holding contextual
825 (format "\\fB%s\\fP " org-clock-string
)
826 (format org-groff-inactive-timestamp-format
827 (concat (org-translate-time
828 (org-element-property :raw-value
829 (org-element-property :value clock
)))
830 (let ((time (org-element-property :duration clock
)))
831 (and time
(format " (%s)" time
)))))))
835 (defun org-groff-code (code contents info
)
836 "Transcode a CODE object from Org to Groff.
837 CONTENTS is nil. INFO is a plist used as a communication
839 (org-groff--text-markup (org-element-property :value code
) 'code
))
841 ;;; Comments and Comment Blocks are ignored.
845 (defun org-groff-drawer (drawer contents info
)
846 "Transcode a DRAWER element from Org to Groff.
847 CONTENTS holds the contents of the block. INFO is a plist
848 holding contextual information."
849 (let* ((name (org-element-property :drawer-name drawer
))
850 (output (if (functionp org-groff-format-drawer-function
)
851 (funcall org-groff-format-drawer-function
853 ;; If there's no user defined function: simply
854 ;; display contents of the drawer.
856 (org-groff--wrap-label drawer output
)))
860 (defun org-groff-dynamic-block (dynamic-block contents info
)
861 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
862 CONTENTS holds the contents of the block. INFO is a plist
863 holding contextual information. See `org-export-data'."
864 (org-groff--wrap-label dynamic-block contents
))
868 (defun org-groff-entity (entity contents info
)
869 "Transcode an ENTITY object from Org to Groff.
870 CONTENTS are the definition itself. INFO is a plist holding
871 contextual information."
872 (org-element-property :utf-8 entity
))
876 (defun org-groff-example-block (example-block contents info
)
877 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
878 CONTENTS is nil. INFO is a plist holding contextual
880 (org-groff--wrap-label
882 (format ".DS L\n%s\n.DE"
883 (org-export-format-code-default example-block info
))))
887 (defun org-groff-export-block (export-block contents info
)
888 "Transcode a EXPORT-BLOCK element from Org to Groff.
889 CONTENTS is nil. INFO is a plist holding contextual information."
890 (when (string= (org-element-property :type export-block
) "GROFF")
891 (org-remove-indentation (org-element-property :value export-block
))))
895 (defun org-groff-export-snippet (export-snippet contents info
)
896 "Transcode a EXPORT-SNIPPET object from Org to Groff.
897 CONTENTS is nil. INFO is a plist holding contextual information."
898 (when (eq (org-export-snippet-backend export-snippet
) 'groff
)
899 (org-element-property :value export-snippet
)))
903 (defun org-groff-fixed-width (fixed-width contents info
)
904 "Transcode a FIXED-WIDTH element from Org to Groff.
905 CONTENTS is nil. INFO is a plist holding contextual information."
906 (org-groff--wrap-label
908 (format "\\fC\n%s\\fP"
909 (org-remove-indentation
910 (org-element-property :value fixed-width
)))))
912 ;;; Footnote Definition
914 ;; Footnote Definitions are ignored.
916 ;; Footnotes are handled automatically in GROFF. Although manual
917 ;; references can be added, not really required.
919 (defun org-groff-footnote-reference (footnote-reference contents info
)
920 ;; Changing from info to footnote-reference
921 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
922 (n (org-export-get-footnote-number footnote-reference info
))
923 (data (org-trim (org-export-data raw info
)))
924 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
926 (if (string-match "fn:rl" ref-id
)
927 (if (member ref-id org-groff-registered-references
)
928 (format "\\*[%s]" ref-id
)
930 (push ref-id org-groff-registered-references
)
931 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
932 ;; else it is a footnote
933 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
937 (defun org-groff-headline (headline contents info
)
938 "Transcode a HEADLINE element from Org to Groff.
939 CONTENTS holds the contents of the headline. INFO is a plist
940 holding contextual information."
941 (let* ((class (plist-get info
:groff-class
))
942 (level (org-export-get-relative-level headline info
))
943 (numberedp (org-export-numbered-headline-p headline info
))
944 ;; Section formatting will set two placeholders: one for the
945 ;; title and the other for the contents.
946 (classes (assoc class org-groff-classes
))
947 (classes-options (car (last classes
)))
948 (heading-option (plist-get classes-options
:heading
))
952 ((and (symbolp heading-option
)
953 (fboundp heading-option
))
954 (funcall heading-option level numberedp
))
957 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
958 ".HU \"%s\"\n%s")))))
959 ;; End of section-fmt
960 (text (org-export-data (org-element-property :title headline
) info
))
962 (and (plist-get info
:with-todo-keywords
)
963 (let ((todo (org-element-property :todo-keyword headline
)))
964 (and todo
(org-export-data todo info
)))))
965 (todo-type (and todo
(org-element-property :todo-type headline
)))
966 (tags (and (plist-get info
:with-tags
)
967 (org-export-get-tags headline info
)))
968 (priority (and (plist-get info
:with-priority
)
969 (org-element-property :priority headline
)))
970 ;; Create the headline text along with a no-tag version. The
971 ;; latter is required to remove tags from table of contents.
972 (full-text (if (functionp org-groff-format-headline-function
)
973 ;; User-defined formatting function.
974 (funcall org-groff-format-headline-function
975 todo todo-type priority text tags
)
976 ;; Default formatting.
979 (format "\\fB%s\\fP " todo
))
980 (when priority
(format " [\\#%c] " priority
))
983 (format " \\fC:%s:\\fP "
984 (mapconcat 'identity tags
":"))))))
986 (if (functionp org-groff-format-headline-function
)
987 ;; User-defined formatting function.
988 (funcall org-groff-format-headline-function
989 todo todo-type priority text nil
)
990 ;; Default formatting.
992 (when todo
(format "\\fB%s\\fP " todo
))
993 (when priority
(format " [\\#%c] " priority
))
995 ;; Associate some \label to the headline for internal links.
997 ;; (format "\\label{sec-%s}\n"
998 ;; (mapconcat 'number-to-string
999 ;; (org-export-get-headline-number headline info)
1003 (make-string (org-element-property :pre-blank headline
) 10)))
1006 ;; Case 1: Special Tag
1007 ((member (car tags
) org-groff-special-tags
)
1009 ((string= (car tags
) "BODY") contents
)
1011 ((string= (car tags
) "NS")
1013 (push (cons (car tags
)
1014 (format ".NS \"%s\" 1 \n%s"
1015 (car (org-element-property :title headline
))
1017 org-groff-special-content
) nil
))
1021 (push (cons (car tags
) contents
) org-groff-special-content
)
1024 ;; Case 2: This is a footnote section: ignore it.
1025 ((org-element-property :footnote-section-p headline
) nil
)
1027 ;; Case 3: This is a deep sub-tree: export it as a list item.
1028 ;; Also export as items headlines for which no section
1029 ;; format has been found.
1030 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1031 ;; Build the real contents of the sub-tree.
1032 (let ((low-level-body
1034 ;; If the headline is the first sibling, start a list.
1035 (when (org-export-first-sibling-p headline info
)
1036 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1038 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1039 ;; If headline is not the last sibling simply return
1040 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1042 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1043 (replace-regexp-in-string
1048 ;; Case 4. Standard headline. Export it as a section.
1050 (format section-fmt full-text
1051 (concat headline-label pre-blanks contents
))))))
1056 ;;; Inline Babel Call
1058 ;; Inline Babel Calls are ignored.
1060 ;;; Inline Src Block
1062 (defun org-groff-inline-src-block (inline-src-block contents info
)
1063 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1064 CONTENTS holds the contents of the item. INFO is a plist holding
1065 contextual information."
1066 (let* ((code (org-element-property :value inline-src-block
)))
1068 (org-groff-source-highlight
1069 (let* ((tmpdir (if (featurep 'xemacs
)
1071 temporary-file-directory
))
1072 (in-file (make-temp-name
1073 (expand-file-name "srchilite" tmpdir
)))
1074 (out-file (make-temp-name
1075 (expand-file-name "reshilite" tmpdir
)))
1076 (org-lang (org-element-property :language inline-src-block
))
1077 (lst-lang (cadr (assq (intern org-lang
)
1078 org-groff-source-highlight-langs
)))
1080 (cmd (concat (expand-file-name "source-highlight")
1082 " -f groff_mm_color "
1086 (let ((code-block ""))
1087 (with-temp-file in-file
(insert code
))
1089 (setq code-block
(org-file-contents out-file
))
1090 (delete-file in-file
)
1091 (delete-file out-file
)
1093 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1096 ;; Do not use a special package: transcode it verbatim.
1098 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1102 (defun org-groff-inlinetask (inlinetask contents info
)
1103 "Transcode an INLINETASK element from Org to Groff.
1104 CONTENTS holds the contents of the block. INFO is a plist
1105 holding contextual information."
1106 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1107 (todo (and (plist-get info
:with-todo-keywords
)
1108 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1109 (and todo
(org-export-data todo info
)))))
1110 (todo-type (org-element-property :todo-type inlinetask
))
1111 (tags (and (plist-get info
:with-tags
)
1112 (org-export-get-tags inlinetask info
)))
1113 (priority (and (plist-get info
:with-priority
)
1114 (org-element-property :priority inlinetask
))))
1115 ;; If `org-groff-format-inlinetask-function' is provided, call it
1116 ;; with appropriate arguments.
1117 (if (functionp org-groff-format-inlinetask-function
)
1118 (funcall org-groff-format-inlinetask-function
1119 todo todo-type priority title tags contents
)
1120 ;; Otherwise, use a default template.
1121 (org-groff--wrap-label
1125 (when todo
(format "\\fB%s\\fP " todo
))
1126 (when priority
(format " [\\#%c] " priority
))
1128 (when tags
(format " \\fC:%s:\\fP "
1129 (mapconcat 'identity tags
":"))))))
1130 (format (concat "\n.DS I\n"
1135 full-title contents
))))))
1139 (defun org-groff-italic (italic contents info
)
1140 "Transcode ITALIC from Org to Groff.
1141 CONTENTS is the text with italic markup. INFO is a plist holding
1142 contextual information."
1143 (org-groff--text-markup contents
'italic
))
1147 (defun org-groff-item (item contents info
)
1148 "Transcode an ITEM element from Org to Groff.
1149 CONTENTS holds the contents of the item. INFO is a plist holding
1150 contextual information."
1151 (let* ((bullet (org-element-property :bullet item
))
1152 (type (org-element-property
1153 :type
(org-element-property :parent item
)))
1154 (checkbox (case (org-element-property :checkbox item
)
1155 (on "\\o'\\(sq\\(mu'")
1157 (trans "\\o'\\(sq\\(mi'")))
1158 (tag (let ((tag (org-element-property :tag item
)))
1159 ;; Check-boxes must belong to the tag.
1160 (and tag
(format "%s"
1162 (org-export-data tag info
)))))))
1166 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1168 (org-trim (or contents
" "))))
1172 (org-trim (or contents
" "))))
1174 (let* ((bullet (org-trim bullet
))
1175 (marker (cond ((string= "-" bullet
) "\\(em")
1176 ((string= "*" bullet
) "\\(bu")
1178 (concat ".LI " marker
"\n"
1179 (org-trim (or contents
" "))))))))
1183 (defun org-groff-keyword (keyword contents info
)
1184 "Transcode a KEYWORD element from Org to Groff.
1185 CONTENTS is nil. INFO is a plist holding contextual information."
1186 (let ((key (org-element-property :key keyword
))
1187 (value (org-element-property :value keyword
)))
1189 ((string= key
"GROFF") value
)
1194 (defun org-groff-line-break (line-break contents info
)
1195 "Transcode a LINE-BREAK object from Org to Groff.
1196 CONTENTS is nil. INFO is a plist holding contextual information."
1200 ;; Inline images just place a call to .PSPIC or .PS/.PE
1201 ;; and load the graph.
1203 (defun org-groff-link--inline-image (link info
)
1204 "Return Groff code for an inline image.
1205 LINK is the link pointing to the inline image. INFO is a plist
1206 used as a communication channel."
1207 (let* ((parent (org-export-get-parent-element link
))
1208 (path (let ((raw-path (org-element-property :path link
)))
1209 (if (not (file-name-absolute-p raw-path
)) raw-path
1210 (expand-file-name raw-path
))))
1211 (attr (org-export-read-attribute :attr_groff link
))
1213 (let ((pos (plist-get attr
:position
)))
1214 (cond ((string= pos
'center
) "")
1215 ((string= pos
'left
) "-L")
1216 ((string= pos
'right
) "-R")
1218 (width (or (plist-get attr
:width
) ""))
1219 (height (or (plist-get attr
:height
) ""))
1220 (caption (and (not (plist-get attr
:disable-caption
))
1221 (org-groff--caption/label-string parent info
))))
1222 ;; Now clear ATTR from any special keyword and set a default value
1223 ;; if nothing is left. Return proper string.
1226 ((and org-groff-raster-to-ps
1227 (or (string-match ".\.png$" path
)
1228 (string-match ".\.jpg$" path
)))
1229 (let ((eps-path (concat path
".eps")))
1230 (shell-command (format org-groff-raster-to-ps path eps-path
))
1231 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1232 placement eps-path width height
)))
1233 ((string-match ".\.pic$" path
)
1234 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1235 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1236 placement path width height
)))
1237 (and caption
(format "\n.FG \"%s\"" caption
)))))
1239 (defun org-groff-link (link desc info
)
1240 "Transcode a LINK object from Org to Groff.
1242 DESC is the description part of the link, or the empty string.
1243 INFO is a plist holding contextual information. See
1246 (let* ((type (org-element-property :type link
))
1247 (raw-path (org-element-property :path link
))
1248 ;; Ensure DESC really exists, or set it to nil.
1249 (desc (and (not (string= desc
"")) desc
))
1250 (imagep (org-export-inline-image-p
1251 link org-groff-inline-image-rules
))
1253 ((member type
'("http" "https" "ftp" "mailto"))
1254 (concat type
":" raw-path
))
1255 ((string= type
"file") (org-export-file-uri raw-path
))
1258 ((org-export-custom-protocol-maybe link desc
'groff
))
1260 (imagep (org-groff-link--inline-image link info
))
1261 ;; import groff files
1262 ((and (string= type
"file")
1263 (string-match ".\.groff$" raw-path
))
1264 (concat ".so " raw-path
"\n"))
1265 ;; Radio link: transcode target's contents and use them as link's
1267 ((string= type
"radio")
1268 (let ((destination (org-export-resolve-radio-link link info
)))
1269 (if (not destination
) desc
1270 (format "\\fI [%s] \\fP"
1271 (org-export-get-reference destination info
)))))
1273 ;; Links pointing to a headline: find destination and build
1274 ;; appropriate referencing command.
1275 ((member type
'("custom-id" "fuzzy" "id"))
1276 (let ((destination (if (string= type
"fuzzy")
1277 (org-export-resolve-fuzzy-link link info
)
1278 (org-export-resolve-id-link link info
))))
1279 (case (org-element-type destination
)
1280 ;; Id link points to an external file.
1282 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1283 (format "\\fI file://%s \\fP" destination
)))
1284 ;; Fuzzy link points nowhere.
1286 (format org-groff-link-with-unknown-path-format
1289 (org-element-property :raw-link link
) info
))))
1290 ;; LINK points to a headline. If headlines are numbered and
1291 ;; the link has no description, display headline's number.
1292 ;; Otherwise, display description or headline's title.
1295 (if (and (plist-get info
:section-numbers
) (not desc
))
1296 (format "\\fI%s\\fP" label
)
1297 (format "\\fI%s\\fP"
1300 (org-element-property :title destination
) info
))))))
1301 ;; Fuzzy link points to a target. Do as above.
1303 (let ((ref (org-export-get-reference destination info
)))
1304 (if (not desc
) (format "\\fI%s\\fP" ref
)
1305 (format "%s \\fBat\\fP \\fI%s\\fP" desc ref
)))))))
1306 ;; External link with a description part.
1307 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1308 ;; External link without a description part.
1309 (path (format "\\fI%s\\fP" path
))
1310 ;; No path, only description. Try to do something useful.
1311 (t (format org-groff-link-with-unknown-path-format desc
)))))
1315 (defun org-groff-node-property (node-property contents info
)
1316 "Transcode a NODE-PROPERTY element from Org to Groff.
1317 CONTENTS is nil. INFO is a plist holding contextual
1320 (org-element-property :key node-property
)
1321 (let ((value (org-element-property :value node-property
)))
1322 (if value
(concat " " value
) ""))))
1326 (defun org-groff-paragraph (paragraph contents info
)
1327 "Transcode a PARAGRAPH element from Org to Groff.
1328 CONTENTS is the contents of the paragraph, as a string. INFO is
1329 the plist used as a communication channel."
1330 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1332 (let* ((parent-type (car parent
))
1333 (fixed-paragraph "")
1334 (class (plist-get info
:groff-class
))
1335 (class-options (plist-get info
:groff-class-options
))
1336 (classes (assoc class org-groff-classes
))
1337 (classes-options (car (last classes
)))
1338 (paragraph-option (plist-get classes-options
:paragraph
)))
1340 ((and (symbolp paragraph-option
)
1341 (fboundp paragraph-option
))
1342 (funcall paragraph-option parent-type parent contents
))
1343 ((and (eq parent-type
'item
)
1344 (plist-get (nth 1 parent
) :bullet
))
1345 (setq fixed-paragraph
(concat "" contents
)))
1346 ((eq parent-type
'section
)
1347 (setq fixed-paragraph
(concat ".P\n" contents
)))
1348 ((eq parent-type
'footnote-definition
)
1349 (setq fixed-paragraph
(concat "" contents
)))
1350 (t (setq fixed-paragraph
(concat "" contents
))))
1355 (defun org-groff-plain-list (plain-list contents info
)
1356 "Transcode a PLAIN-LIST element from Org to Groff.
1357 CONTENTS is the contents of the list. INFO is a plist holding
1358 contextual information."
1359 (let* ((type (org-element-property :type plain-list
))
1360 (attr (mapconcat #'identity
1361 (org-element-property :attr_groff plain-list
)
1364 ((eq type
'ordered
) ".AL")
1365 ((eq type
'unordered
) ".BL")
1366 ((eq type
'descriptive
) ".VL 2.0i"))))
1367 (org-groff--wrap-label
1369 (format "%s\n%s\n.LE" groff-type contents
))))
1373 (defun org-groff-plain-text (text info
)
1374 "Transcode a TEXT string from Org to Groff.
1375 TEXT is the string to transcode. INFO is a plist holding
1376 contextual information."
1377 (let ((output text
))
1378 ;; Protect various characters.
1379 (setq output
(replace-regexp-in-string
1380 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1381 "$\\" output nil t
1))
1382 ;; Activate smart quotes. Be sure to provide original TEXT string
1383 ;; since OUTPUT may have been modified.
1384 (when (plist-get info
:with-smart-quotes
)
1385 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
1386 ;; Handle Special Characters
1387 (if org-groff-special-char
1388 (dolist (special-char-list org-groff-special-char
)
1390 (replace-regexp-in-string (car special-char-list
)
1391 (cdr special-char-list
) output
))))
1392 ;; Handle break preservation if required.
1393 (when (plist-get info
:preserve-breaks
)
1394 (setq output
(replace-regexp-in-string
1395 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output
)))
1401 (defun org-groff-planning (planning contents info
)
1402 "Transcode a PLANNING element from Org to Groff.
1403 CONTENTS is nil. INFO is a plist holding contextual
1410 (let ((closed (org-element-property :closed planning
)))
1413 (format "\\fR %s \\fP" org-closed-string
)
1414 (format org-groff-inactive-timestamp-format
1416 (org-element-property :raw-value closed
))))))
1417 (let ((deadline (org-element-property :deadline planning
)))
1420 (format "\\fB %s \\fP" org-deadline-string
)
1421 (format org-groff-active-timestamp-format
1423 (org-element-property :raw-value deadline
))))))
1424 (let ((scheduled (org-element-property :scheduled planning
)))
1427 (format "\\fR %s \\fP" org-scheduled-string
)
1428 (format org-groff-active-timestamp-format
1430 (org-element-property :raw-value scheduled
))))))))
1434 ;;;; Property Drawer
1436 (defun org-groff-property-drawer (property-drawer contents info
)
1437 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1438 CONTENTS holds the contents of the drawer. INFO is a plist
1439 holding contextual information."
1440 (and (org-string-nw-p contents
)
1441 (format "\\fC\n%s\\fP" contents
)))
1445 (defun org-groff-quote-block (quote-block contents info
)
1446 "Transcode a QUOTE-BLOCK element from Org to Groff.
1447 CONTENTS holds the contents of the block. INFO is a plist
1448 holding contextual information."
1449 (org-groff--wrap-label
1451 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1455 (defun org-groff-radio-target (radio-target text info
)
1456 "Transcode a RADIO-TARGET object from Org to Groff.
1457 TEXT is the text of the target. INFO is a plist holding
1458 contextual information."
1459 (format "%s - %s" (org-export-get-reference radio-target info
) text
))
1463 (defun org-groff-section (section contents info
)
1464 "Transcode a SECTION element from Org to Groff.
1465 CONTENTS holds the contents of the section. INFO is a plist
1466 holding contextual information."
1471 (defun org-groff-special-block (special-block contents info
)
1472 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1473 CONTENTS holds the contents of the block. INFO is a plist
1474 holding contextual information."
1475 (let ((type (org-element-property :type special-block
)))
1476 (org-groff--wrap-label
1478 (format "%s\n" contents
))))
1482 (defun org-groff-src-block (src-block contents info
)
1483 "Transcode a SRC-BLOCK element from Org to Groff.
1484 CONTENTS holds the contents of the item. INFO is a plist holding
1485 contextual information."
1486 (let* ((lang (org-element-property :language src-block
))
1487 (label (org-element-property :name src-block
))
1488 (code (org-element-property :value src-block
))
1489 (custom-env (and lang
1490 (cadr (assq (intern lang
)
1491 org-groff-custom-lang-environments
))))
1492 (num-start (case (org-element-property :number-lines src-block
)
1493 (continued (org-export-get-loc src-block info
))
1495 (retain-labels (org-element-property :retain-labels src-block
))
1496 (caption (and (not (org-export-read-attribute
1497 :attr_groff src-block
:disable-caption
))
1498 (org-groff--caption/label-string src-block info
))))
1501 ;; Case 1. No source fontification.
1502 ((not org-groff-source-highlight
)
1504 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1505 (org-export-format-code-default src-block info
))
1506 (and caption
(format ".EX \"%s\" " caption
))))
1508 ;; Case 2. Source fontification.
1509 (org-groff-source-highlight
1510 (let* ((tmpdir (if (featurep 'xemacs
)
1512 temporary-file-directory
))
1513 (in-file (make-temp-name
1514 (expand-file-name "srchilite" tmpdir
)))
1515 (out-file (make-temp-name
1516 (expand-file-name "reshilite" tmpdir
)))
1518 (org-lang (org-element-property :language src-block
))
1519 (lst-lang (cadr (assq (intern org-lang
)
1520 org-groff-source-highlight-langs
)))
1522 (cmd (concat "source-highlight"
1524 " -f groff_mm_color "
1530 (let ((code-block ""))
1531 (with-temp-file in-file
(insert code
))
1533 (setq code-block
(org-file-contents out-file
))
1534 (delete-file in-file
)
1535 (delete-file out-file
)
1536 (format "%s\n" code-block
))
1537 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1539 (and caption
(format ".EX \"%s\" " caption
))))))))
1542 ;;; Statistics Cookie
1544 (defun org-groff-statistics-cookie (statistics-cookie contents info
)
1545 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1546 CONTENTS is nil. INFO is a plist holding contextual information."
1547 (org-element-property :value statistics-cookie
))
1552 (defun org-groff-strike-through (strike-through contents info
)
1553 "Transcode STRIKE-THROUGH from Org to Groff.
1554 CONTENTS is the text with strike-through markup. INFO is a plist
1555 holding contextual information."
1556 (org-groff--text-markup contents
'strike-through
))
1560 (defun org-groff-subscript (subscript contents info
)
1561 "Transcode a SUBSCRIPT object from Org to Groff.
1562 CONTENTS is the contents of the object. INFO is a plist holding
1563 contextual information."
1564 (format "\\d\\s-2%s\\s+2\\u" contents
))
1566 ;;; Superscript "^_%s$
1568 (defun org-groff-superscript (superscript contents info
)
1569 "Transcode a SUPERSCRIPT object from Org to Groff.
1570 CONTENTS is the contents of the object. INFO is a plist holding
1571 contextual information."
1572 (format "\\u\\s-2%s\\s+2\\d" contents
))
1577 ;; `org-groff-table' is the entry point for table transcoding. It
1578 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1579 ;; delegates the job to `org-groff-table--org-table' function,
1580 ;; depending of the type of the table.
1582 ;; `org-groff-table--align-string' is a subroutine used to build
1583 ;; alignment string for Org tables.
1585 (defun org-groff-table (table contents info
)
1586 "Transcode a TABLE element from Org to Groff.
1587 CONTENTS is the contents of the table. INFO is a plist holding
1588 contextual information."
1590 ;; Case 1: verbatim table.
1591 ((or org-groff-tables-verbatim
1592 (let ((attr (read (format "(%s)"
1595 (org-element-property :attr_groff table
) " ")))))
1596 (and attr
(plist-get attr
:verbatim
))))
1598 (format ".DS L\n\\fC%s\\fP\n.DE"
1599 ;; Re-create table, without affiliated keywords.
1601 (org-element-interpret-data
1602 `(table nil
,@(org-element-contents table
))))))
1604 ;; Case 2: Standard table.
1605 (t (org-groff-table--org-table table contents info
))))
1607 (defun org-groff-table--align-string (divider table info
)
1608 "Return an appropriate Groff alignment string.
1609 TABLE is the considered table. INFO is a plist used as
1610 a communication channel."
1612 ;; Extract column groups and alignment from first (non-rule) row.
1614 (org-element-map table
'table-row
1616 (and (eq (org-element-property :type row
) 'standard
) row
))
1620 (let* ((borders (org-export-table-cell-borders cell info
))
1621 (raw-width (org-export-table-cell-width cell info
))
1622 (width-cm (when raw-width
(/ raw-width
5)))
1623 (width (if raw-width
(format "w(%dc)"
1624 (if (< width-cm
1) 1 width-cm
)) "")))
1625 ;; Check left border for the first cell only.
1626 ;; Alignment is nil on assignment
1628 (when (and (memq 'left borders
) (not alignment
))
1629 (push "|" alignment
))
1631 (case (org-export-table-cell-alignment cell info
)
1632 (left (concat "l" width divider
))
1633 (right (concat "r" width divider
))
1634 (center (concat "c" width divider
)))
1636 (when (memq 'right borders
) (push "|" alignment
))))
1638 (apply 'concat
(reverse alignment
))))
1640 (defun org-groff-table--org-table (table contents info
)
1641 "Return appropriate Groff code for an Org table.
1643 TABLE is the table type element to transcode. CONTENTS is its
1644 contents, as a string. INFO is a plist used as a communication
1647 This function assumes TABLE has `org' as its `:type' attribute."
1648 (let* ((attr (org-export-read-attribute :attr_groff table
))
1649 (label (org-element-property :name table
))
1650 (caption (and (not (plist-get attr
:disable-caption
))
1651 (org-groff--caption/label-string table info
)))
1652 (divider (if (plist-get attr
:divider
) "|" " "))
1654 ;; Determine alignment string.
1655 (alignment (org-groff-table--align-string divider table info
))
1657 ;; Extract others display options.
1659 (lines (org-split-string contents
"\n"))
1663 (list (and (plist-get attr
:expand
) "expand")
1664 (let ((placement (plist-get attr
:placement
)))
1665 (cond ((string= placement
'center
) "center")
1666 ((string= placement
'left
) nil
)
1667 (t (if org-groff-tables-centered
"center" ""))))
1668 (or (plist-get attr
:boxtype
) "box"))))
1670 (title-line (plist-get attr
:title-line
))
1671 (long-cells (plist-get attr
:long-cells
))
1675 (or (car attr-list
) "")
1678 (when (cdr attr-list
)
1679 (dolist (attr-item (cdr attr-list
))
1680 (setq output-list
(concat output-list
1681 (format ",%s" attr-item
)))))
1684 (when lines
(org-split-string (car lines
) "\t"))))
1685 ;; Prepare the final format string for the table.
1691 (concat ".TS\n " table-format
";\n"
1693 (let ((final-line ""))
1695 (dotimes (i (length first-line
))
1696 (setq final-line
(concat final-line
"cb" divider
))))
1698 (setq final-line
(concat final-line
"\n"))
1701 (setq final-line
(concat final-line alignment
))
1702 (dotimes (i (length first-line
))
1703 (setq final-line
(concat final-line
"c" divider
))))
1707 (let ((final-line "")
1709 (lines (org-split-string contents
"\n")))
1711 (dolist (line-item lines
)
1716 (if (string= line-item
"_")
1717 (setq long-line
(format "%s\n" line-item
))
1719 (let ((cell-item-list (org-split-string line-item
"\t")))
1720 (dolist (cell-item cell-item-list
)
1722 (cond ((eq cell-item
(car (last cell-item-list
)))
1723 (setq long-line
(concat long-line
1724 (format "T{\n%s\nT}\t\n" cell-item
))))
1726 (setq long-line
(concat long-line
1727 (format "T{\n%s\nT}\t" cell-item
))))))
1730 (setq final-line
(concat final-line long-line
)))
1732 (setq final-line
(concat final-line line-item
"\n"))))
1735 (if caption
(format ".TB \"%s\"" caption
) ""))))))
1739 (defun org-groff-table-cell (table-cell contents info
)
1740 "Transcode a TABLE-CELL element from Org to Groff
1741 CONTENTS is the cell contents. INFO is a plist used as
1742 a communication channel."
1744 (concat (if (and contents
1745 org-groff-table-scientific-notation
1746 (string-match orgtbl-exp-regexp contents
))
1747 ;; Use appropriate format string for scientific
1749 (format org-groff-table-scientific-notation
1750 (match-string 1 contents
)
1751 (match-string 2 contents
))
1753 (when (org-export-get-next-element table-cell info
) "\t"))))
1758 (defun org-groff-table-row (table-row contents info
)
1759 "Transcode a TABLE-ROW element from Org to Groff
1760 CONTENTS is the contents of the row. INFO is a plist used as
1761 a communication channel."
1762 ;; Rules are ignored since table separators are deduced from
1763 ;; borders of the current row.
1764 (when (eq (org-element-property :type table-row
) 'standard
)
1765 (let* ((attr (mapconcat 'identity
1766 (org-element-property
1767 :attr_groff
(org-export-get-parent table-row
))
1769 ;; TABLE-ROW's borders are extracted from its first cell.
1771 (org-export-table-cell-borders
1772 (car (org-element-contents table-row
)) info
)))
1774 ;; Mark horizontal lines
1775 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1778 ;; When BOOKTABS are activated enforce bottom rule even when
1779 ;; no hline was specifically marked.
1780 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1781 ((memq 'below borders
) "\n_"))))))
1785 (defun org-groff-target (target contents info
)
1786 "Transcode a TARGET object from Org to Groff.
1787 CONTENTS is nil. INFO is a plist holding contextual
1789 (format "\\fI%s\\fP" (org-export-get-reference target info
)))
1793 (defun org-groff-timestamp (timestamp contents info
)
1794 "Transcode a TIMESTAMP object from Org to Groff.
1795 CONTENTS is nil. INFO is a plist holding contextual
1797 (let ((value (org-groff-plain-text
1798 (org-timestamp-translate timestamp
) info
)))
1799 (case (org-element-property :type timestamp
)
1800 ((active active-range
)
1801 (format org-groff-active-timestamp-format value
))
1802 ((inactive inactive-range
)
1803 (format org-groff-inactive-timestamp-format value
))
1804 (t (format org-groff-diary-timestamp-format value
)))))
1808 (defun org-groff-underline (underline contents info
)
1809 "Transcode UNDERLINE from Org to Groff.
1810 CONTENTS is the text with underline markup. INFO is a plist
1811 holding contextual information."
1812 (org-groff--text-markup contents
'underline
))
1816 (defun org-groff-verbatim (verbatim contents info
)
1817 "Transcode a VERBATIM object from Org to Groff.
1818 CONTENTS is nil. INFO is a plist used as a communication
1820 (org-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1824 (defun org-groff-verse-block (verse-block contents info
)
1825 "Transcode a VERSE-BLOCK element from Org to Groff.
1826 CONTENTS is verse block contents. INFO is a plist holding
1827 contextual information."
1828 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1831 ;;; Interactive functions
1833 (defun org-groff-export-to-groff
1834 (&optional async subtreep visible-only body-only ext-plist
)
1835 "Export current buffer to a Groff file.
1837 If narrowing is active in the current buffer, only export its
1840 If a region is active, export that region.
1842 A non-nil optional argument ASYNC means the process should happen
1843 asynchronously. The resulting file should be accessible through
1844 the `org-export-stack' interface.
1846 When optional argument SUBTREEP is non-nil, export the sub-tree
1847 at point, extracting information from the headline properties
1850 When optional argument VISIBLE-ONLY is non-nil, don't export
1851 contents of hidden elements.
1853 EXT-PLIST, when provided, is a property list with external
1854 parameters overriding Org default settings, but still inferior to
1855 file-local settings.
1857 Return output file's name."
1859 (let ((outfile (org-export-output-file-name ".groff" subtreep
))
1860 (org-groff-registered-references nil
)
1861 (org-groff-special-content nil
))
1862 (org-export-to-file 'groff outfile
1863 async subtreep visible-only body-only ext-plist
)))
1865 (defun org-groff-export-to-pdf
1866 (&optional async subtreep visible-only body-only ext-plist
)
1867 "Export current buffer to Groff then process through to PDF.
1869 If narrowing is active in the current buffer, only export its
1872 If a region is active, export that region.
1874 A non-nil optional argument ASYNC means the process should happen
1875 asynchronously. The resulting file should be accessible through
1876 the `org-export-stack' interface.
1878 When optional argument SUBTREEP is non-nil, export the sub-tree
1879 at point, extracting information from the headline properties
1882 When optional argument VISIBLE-ONLY is non-nil, don't export
1883 contents of hidden elements.
1885 EXT-PLIST, when provided, is a property list with external
1886 parameters overriding Org default settings, but still inferior to
1887 file-local settings.
1889 Return PDF file's name."
1891 (let ((outfile (org-export-output-file-name ".groff" subtreep
)))
1892 (org-export-to-file 'groff outfile
1893 async subtreep visible-only body-only ext-plist
1894 (lambda (file) (org-groff-compile file
)))))
1896 (defun org-groff-compile (file)
1897 "Compile a Groff file.
1899 FILE is the name of the file being compiled. Processing is done
1900 through the command specified in `org-groff-pdf-process'.
1902 Return PDF file name or an error if it couldn't be produced."
1903 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file
)))
1904 (full-name (file-truename file
))
1905 (out-dir (file-name-directory file
))
1906 ;; Properly set working directory for compilation.
1907 (default-directory (if (file-name-absolute-p file
)
1908 (file-name-directory full-name
)
1911 (message (format "Processing Groff file %s ..." file
))
1912 (save-window-excursion
1914 ;; A function is provided: Apply it.
1915 ((functionp org-groff-pdf-process
)
1916 (funcall org-groff-pdf-process
(shell-quote-argument file
)))
1917 ;; A list is provided: Replace %b, %f and %o with appropriate
1918 ;; values in each command before applying it. Output is
1919 ;; redirected to "*Org PDF Groff Output*" buffer.
1920 ((consp org-groff-pdf-process
)
1921 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1925 (replace-regexp-in-string
1926 "%b" (shell-quote-argument base-name
)
1927 (replace-regexp-in-string
1928 "%f" (shell-quote-argument full-name
)
1929 (replace-regexp-in-string
1930 "%o" (shell-quote-argument out-dir
) command t t
)
1933 org-groff-pdf-process
)
1934 ;; Collect standard errors from output buffer.
1935 (setq errors
(org-groff-collect-errors outbuf
))))
1936 (t (error "No valid command to process to PDF")))
1937 (let ((pdffile (concat out-dir base-name
".pdf")))
1938 ;; Check for process failure. Provide collected errors if
1940 (if (not (file-exists-p pdffile
))
1941 (error (concat (format "PDF file %s wasn't produced" pdffile
)
1942 (when errors
(concat ": " errors
))))
1943 ;; Else remove log files, when specified, and signal end of
1944 ;; process to user, along with any error encountered.
1945 (when org-groff-remove-logfiles
1946 (dolist (ext org-groff-logfiles-extensions
)
1947 (let ((file (concat out-dir base-name
"." ext
)))
1948 (when (file-exists-p file
) (delete-file file
)))))
1949 (message (concat "Process completed"
1950 (if (not errors
) "."
1951 (concat " with errors: " errors
)))))
1952 ;; Return output file name.
1955 (defun org-groff-collect-errors (buffer)
1956 "Collect some kind of errors from \"groff\" output
1957 BUFFER is the buffer containing output.
1958 Return collected error types as a string, or nil if there was
1960 (with-current-buffer buffer
1962 (goto-char (point-max))
1968 ;;; ox-groff.el ends here