1 ;; ox-groff.el --- Groff Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 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 Org
29 ;; M-: (org-export-to-buffer 'groff "*Test Groff*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Groff
32 ;; export. See ox.el for more details on how this exporter works.
34 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
35 ;; "GROFF_CLASS_OPTIONS".
39 (eval-when-compile (require 'cl
))
42 (defvar orgtbl-exp-regexp
)
47 (org-export-define-backend groff
48 ((bold . org-groff-bold
)
49 (center-block . org-groff-center-block
)
50 (clock . org-groff-clock
)
51 (code . org-groff-code
)
52 (drawer . org-groff-drawer
)
53 (dynamic-block . org-groff-dynamic-block
)
54 (entity . org-groff-entity
)
55 (example-block . org-groff-example-block
)
56 (export-block . org-groff-export-block
)
57 (export-snippet . org-groff-export-snippet
)
58 (fixed-width . org-groff-fixed-width
)
59 (footnote-definition . org-groff-footnote-definition
)
60 (footnote-reference . org-groff-footnote-reference
)
61 (headline . org-groff-headline
)
62 (horizontal-rule . org-groff-horizontal-rule
)
63 (inline-src-block . org-groff-inline-src-block
)
64 (inlinetask . org-groff-inlinetask
)
65 (italic . org-groff-italic
)
66 (item . org-groff-item
)
67 (keyword . org-groff-keyword
)
68 (line-break . org-groff-line-break
)
69 (link . org-groff-link
)
70 (paragraph . org-groff-paragraph
)
71 (plain-list . org-groff-plain-list
)
72 (plain-text . org-groff-plain-text
)
73 (planning . org-groff-planning
)
74 (property-drawer . org-groff-property-drawer
)
75 (quote-block . org-groff-quote-block
)
76 (quote-section . org-groff-quote-section
)
77 (radio-target . org-groff-radio-target
)
78 (section . org-groff-section
)
79 (special-block . org-groff-special-block
)
80 (src-block . org-groff-src-block
)
81 (statistics-cookie . org-groff-statistics-cookie
)
82 (strike-through . org-groff-strike-through
)
83 (subscript . org-groff-subscript
)
84 (superscript . org-groff-superscript
)
85 (table . org-groff-table
)
86 (table-cell . org-groff-table-cell
)
87 (table-row . org-groff-table-row
)
88 (target . org-groff-target
)
89 (template . org-groff-template
)
90 (timestamp . org-groff-timestamp
)
91 (underline . org-groff-underline
)
92 (verbatim . org-groff-verbatim
)
93 (verse-block . org-groff-verse-block
))
97 ((?g
"As GROFF file" org-groff-export-to-groff
)
98 (?p
"As PDF file" org-groff-export-to-pdf
)
99 (?o
"As PDF file and open"
101 (if a
(org-groff-export-to-pdf t s v b
)
102 (org-open-file (org-groff-export-to-pdf nil s v b
)))))))
104 ((:groff-class
"GROFF_CLASS" nil org-groff-default-class t
)
105 (:groff-class-options
"GROFF_CLASS_OPTIONS" nil nil t
)
106 (:groff-header-extra
"GROFF_HEADER" nil nil newline
)))
110 ;;; User Configurable Variables
112 (defgroup org-export-groff nil
113 "Options for exporting Org mode files to Groff."
114 :tag
"Org Export Groff"
119 (defcustom org-groff-default-class
"internal"
120 "The default Groff class."
121 :group
'org-export-groff
122 :type
'(string :tag
"Groff class"))
124 (defcustom org-groff-classes
126 (:heading
'default
:type
"memo" :last-section
"toc"))
128 (:heading
'default
:type
"memo" :last-section
"toc"))
129 ("programmer" ".MT 2"
130 (:heading
'default
:type
"memo" :last-section
"toc"))
132 (:heading
'default
:type
"memo" :last-section
"toc"))
134 (:heading
'default
:type
"memo" :last-section
"toc"))
136 (:heading
'default
:type
"memo" :last-section
"sign"))
138 (:heading custom-function
:type
"custom" :last-section
"toc"))
140 (:heading
'default
:type
"memo"))
142 (:heading
'default
:type
"cover" :last-section
"toc"))
144 (:heading
'default
:type
"cover" :last-section
"toc"))
146 (:heading
'default
:type
"letter" :last-section
"sign"))
148 (:heading
'default
:type
"letter" :last-section
"sign"))
150 (:heading
'default
:type
"letter" :last-section
"sign"))
152 (:heading
'default
:type
"letter" :last-section
"sign"))
153 ("none" "" (:heading
'default
:type
"custom")))
155 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
156 ;; This is to facilitate the creation of custom pages.
158 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
159 ;; are made. This is to facilitate Abstract Insertion.
161 "This list describes the attributes for the documents being created.
162 It allows for the creation of new "
163 :group
'org-export-groff
165 (list (string :tag
"Document Type")
166 (string :tag
"Header")
167 (repeat :tag
"Options" :inline t
169 (list :tag
"Heading")
170 (function :tag
"Hook computing sectioning"))))))
174 (defconst org-groff-special-tags
175 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
177 (defcustom org-groff-format-headline-function nil
178 "Function to format headline text.
180 This function will be called with 5 arguments:
181 TODO the todo keyword (string or nil).
182 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
183 PRIORITY the priority of the headline (integer or nil)
184 TEXT the main headline text (string).
185 TAGS the tags as a list of strings (list of strings or nil).
187 The function result will be used in the section format string.
189 As an example, one could set the variable to the following, in
190 order to reproduce the default set-up:
192 \(defun org-groff-format-headline (todo todo-type priority text tags)
193 \"Default format function for an headline.\"
195 \(format \"\\fB%s\\fP \" todo))
197 \(format \"[\\#%c] \" priority))
201 \(mapconcat 'identity tags \":\"))))"
202 :group
'org-export-groff
207 (defcustom org-groff-active-timestamp-format
"\\fI%s\\fP"
208 "A printf format string to be applied to active timestamps."
209 :group
'org-export-groff
212 (defcustom org-groff-inactive-timestamp-format
"\\fI%s\\fP"
213 "A printf format string to be applied to inactive timestamps."
214 :group
'org-export-groff
217 (defcustom org-groff-diary-timestamp-format
"\\fI%s\\fP"
218 "A printf format string to be applied to diary timestamps."
219 :group
'org-export-groff
224 (defcustom org-groff-inline-image-rules
225 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
226 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
227 "Rules characterizing image files that can be inlined into Groff.
229 A rule consists in an association whose key is the type of link
230 to consider, and value is a regexp that will be matched against
233 Note that, by default, the image extensions actually allowed
234 depend on the way the Groff file is processed. When used with
235 pdfgroff, pdf, jpg and png images are OK. When processing
236 through dvi to Postscript, only ps and eps are allowed. The
237 default we use here encompasses both."
238 :group
'org-export-groff
239 :type
'(alist :key-type
(string :tag
"Type")
240 :value-type
(regexp :tag
"Path")))
242 (defcustom org-groff-link-with-unknown-path-format
"\\fI%s\\fP"
243 "Format string for links with unknown path type."
244 :group
'org-export-groff
249 (defcustom org-groff-tables-centered t
250 "When non-nil, tables are exported in a center environment."
251 :group
'org-export-groff
254 (defcustom org-groff-tables-verbatim nil
255 "When non-nil, tables are exported verbatim."
256 :group
'org-export-groff
259 (defcustom org-groff-table-scientific-notation
"%sE%s"
260 "Format string to display numbers in scientific notation.
261 The format should have \"%s\" twice, for mantissa and exponent
262 \(i.e. \"%s\\\\times10^{%s}\").
264 When nil, no transformation is made."
265 :group
'org-export-groff
267 (string :tag
"Format string")
268 (const :tag
"No formatting")))
272 (defcustom org-groff-text-markup-alist
273 '((bold .
"\\fB%s\\fP")
274 (code .
"\\fC%s\\fP")
275 (italic .
"\\fI%s\\fP")
276 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
277 (underline .
"\\fI%s\\fP") ; need to be revised.
278 (verbatim .
"protectedtexttt"))
279 "Alist of Groff expressions to convert text markup.
281 The key must be a symbol among `bold', `code', `italic',
282 `strike-through', `underline' and `verbatim'. The value is
283 a formatting string to wrap fontified text with it.
285 If no association can be found for a given markup, text will be
287 :group
'org-export-groff
289 :options
'(bold code italic strike-through underline verbatim
))
293 (defcustom org-groff-format-drawer-function nil
294 "Function called to format a drawer in Groff code.
296 The function must accept two parameters:
297 NAME the drawer name, like \"LOGBOOK\"
298 CONTENTS the contents of the drawer.
300 The function should return the string to be exported.
302 For example, the variable could be set to the following function
303 in order to mimic default behaviour:
305 \(defun org-groff-format-drawer-default \(name contents\)
306 \"Format a drawer element for Groff export.\"
308 :group
'org-export-groff
313 (defcustom org-groff-format-inlinetask-function nil
314 "Function called to format an inlinetask in Groff code.
316 The function must accept six parameters:
317 TODO the todo keyword, as a string
318 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
319 PRIORITY the inlinetask priority, as a string
320 NAME the inlinetask name, as a string.
321 TAGS the inlinetask tags, as a list of strings.
322 CONTENTS the contents of the inlinetask, as a string.
324 The function should return the string to be exported.
326 For example, the variable could be set to the following function
327 in order to mimic default behaviour:
329 \(defun org-groff-format-inlinetask \(todo type priority name tags contents\)
330 \"Format an inline task element for Groff export.\"
334 \(format \"\\fB%s\\fP \" todo))
335 \(when priority (format \"[\\#%c] \" priority))
339 \(mapconcat 'identity tags \":\")))))
340 \(format (concat \".DS L\\n\"
344 full-title contents))"
345 :group
'org-export-groff
350 (defcustom org-groff-source-highlight nil
351 "Use GNU source highlight to embellish source blocks "
352 :group
'org-export-groff
355 (defcustom org-groff-source-highlight-langs
356 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
358 (c "c") (cc "cpp") (csharp "csharp") (d "d")
359 (fortran "fortran") (cobol "cobol") (pascal "pascal")
360 (ada "ada") (asm "asm")
361 (perl "perl") (cperl "perl")
362 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
363 (java "java") (javascript "javascript")
365 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
366 (ocaml "caml") (caml "caml")
367 (sql "sql") (sqlite "sql")
368 (html "html") (css "css") (xml "xml")
369 (bat "bat") (bison "bison") (clipper "clipper")
370 (ldap "ldap") (opa "opa")
371 (php "php") (postscript "postscript") (prolog "prolog")
372 (properties "properties") (makefile "makefile")
373 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
374 "Alist mapping languages to their listing language counterpart.
375 The key is a symbol, the major mode symbol without the \"-mode\".
376 The value is the string that should be inserted as the language
377 parameter for the listings package. If the mode name and the
378 listings name are the same, the language does not need an entry
379 in this list - but it does not hurt if it is present."
380 :group
'org-export-groff
383 (symbol :tag
"Major mode ")
384 (string :tag
"Listings language"))))
386 (defcustom org-groff-source-highlight-options nil
387 "Association list of options for the groff listings package.
389 These options are supplied as a comma-separated list to the
390 \\lstset command. Each element of the association list should be
391 a list containing two strings: the name of the option, and the
394 (setq org-groff-source-highlight-options
395 '((\"basicstyle\" \"\\small\")
396 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
398 will typeset the code in a small size font with underlined, bold
401 Note that the same options will be applied to blocks of all
403 :group
'org-export-groff
406 (string :tag
"Listings option name ")
407 (string :tag
"Listings option value"))))
409 (defvar org-groff-custom-lang-environments nil
410 "Alist mapping languages to language-specific Groff environments.
412 It is used during export of src blocks by the listings and
413 groff packages. For example,
415 \(setq org-groff-custom-lang-environments
416 '\(\(python \"pythoncode\"\)\)\)
418 would have the effect that if org encounters begin_src python
419 during groff export it will use pythoncode as the source-highlight
424 (defcustom org-groff-special-char
425 '(("(c)" .
"\\\\(co")
427 ("(rg)" .
"\\\\(rg"))
428 "CONS list in which the value of the car
429 is replace on the value of the CDR. "
430 :group
'org-export-groff
432 (cons :tag
"Character Subtitute"
433 (string :tag
"Original Character Group")
434 (string :tag
"Replacement Character"))))
438 (defcustom org-groff-pdf-process
439 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
440 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
441 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
443 "Commands to process a Groff file to a PDF file.
444 This is a list of strings, each of them will be given to the
445 shell as a command. %f in the command will be replaced by the
446 full file name, %b by the file base name \(i.e. without
447 extension) and %o by the base directory of the file."
448 :group
'org-export-pdf
450 (repeat :tag
"Shell command sequence"
451 (string :tag
"Shell command"))
452 (const :tag
"2 runs of pdfgroff"
453 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
454 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
455 (const :tag
"3 runs of pdfgroff"
456 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
457 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
458 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
461 (defcustom org-groff-logfiles-extensions
462 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
463 "The list of file extensions to consider as Groff logfiles."
464 :group
'org-export-groff
465 :type
'(repeat (string :tag
"Extension")))
467 (defcustom org-groff-remove-logfiles t
468 "Non-nil means remove the logfiles produced by PDF production.
469 These are the .aux, .log, .out, and .toc files."
470 :group
'org-export-groff
473 (defcustom org-groff-organization
"Org User"
474 "Name of the organization used to populate the .AF command."
475 :group
'org-export-groff
478 (defcustom org-groff-raster-to-ps nil
479 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
480 `org-groff-inline-image-rules' is adjusted accordingly if not conversion is being
481 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
482 :group
'org-export-groff
484 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
485 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
486 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
487 (const :tag
"None" nil
)))
489 (defvar org-groff-registered-references nil
)
490 (defvar org-groff-special-content nil
)
494 ;;; Internal Functions
496 (defun org-groff--caption/label-string
(element info
)
497 "Return caption and label Groff string for ELEMENT.
499 INFO is a plist holding contextual information. If there's no
500 caption nor label, return the empty string.
502 For non-floats, see `org-groff--wrap-label'."
503 (let ((main (org-export-get-caption element
))
504 (short (org-export-get-caption element t
))
505 (label (org-element-property :name element
)))
506 (cond ((and (not main
) (not label
)) "")
507 ((not main
) (format "\\fI%s\\fP" label
))
508 ;; Option caption format with short name.
509 (short (format "%s\n.br\n - %s\n"
510 (org-export-data short info
)
511 (org-export-data main info
)))
512 ;; Standard caption format.
513 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
515 (defun org-groff--wrap-label (element output
)
516 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
517 This function shouldn't be used for floats. See
518 `org-groff--caption/label-string'."
519 (let ((label (org-element-property :name element
)))
520 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
522 (concat (format "%s\n.br\n" label
) output
))))
524 (defun org-groff--text-markup (text markup
)
525 "Format TEXT depending on MARKUP text markup.
526 See `org-groff-text-markup-alist' for details."
527 (let ((fmt (cdr (assq markup org-groff-text-markup-alist
))))
529 ;; No format string: Return raw text.
531 ((string= "protectedtexttt" fmt
)
533 (trans '(("\\" .
"\\")))
536 (while (string-match "[\\{}$%&_#~^]" text
)
537 (setq char
(match-string 0 text
))
538 (if (> (match-beginning 0) 0)
539 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
540 (setq text
(substring text
(1+ (match-beginning 0))))
541 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
542 rtn
(concat rtn char
)))
543 (setq text
(concat rtn text
))
544 (format "\\fC%s\\fP" text
)))
545 ;; Else use format string.
546 (t (format fmt text
)))))
549 (defun org-groff--get-tagged-content (tag info
)
550 (cdr (assoc tag org-groff-special-content
)))
552 (defun org-groff--mt-head (title contents attr info
)
555 ;; 1. Insert Organization
556 (let ((firm-option (plist-get attr
:firm
)))
558 ((stringp firm-option
)
559 (format ".AF \"%s\" \n" firm-option
))
560 (t (format ".AF \"%s\" \n" (or org-groff-organization
"")))))
563 (let ((subtitle1 (plist-get attr
:subtitle1
))
564 (subtitle2 (plist-get attr
:subtitle2
)))
568 (format ".TL \"%s\" \"%s\" \n%s\n"
570 (or subtitle2
"") " "))
572 ((not (or subtitle1 subtitle2
))
576 (format ".TL \"%s\" \"%s \" \n%s\n"
578 (or subtitle2
"") title
))))
581 ;; In Groff, .AU *MUST* be placed after .TL
582 ;; If From, populate with data from From else
584 (let ((author (and (plist-get info
:with-author
)
585 (let ((auth (plist-get info
:author
)))
586 (and auth
(org-export-data auth info
)))))
587 (email (and (plist-get info
:with-email
)
588 (org-export-data (plist-get info
:email
) info
)))
589 (from-data (org-groff--get-tagged-content "FROM" info
))
591 (to-data (org-groff--get-tagged-content "TO" info
)))
594 ((and author from-data
)
598 (format " \"%s\" " from-line
))
601 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
604 (format ".AU \"%s\" " author
) au-line
"\n")))
606 ((and author email
(not (string= "" email
)))
607 (format ".AU \"%s\" \"%s\"\n" author email
))
609 (author (format ".AU \"%s\"\n" author
))
614 ;; 4. Author Title, if present
615 (let ((at-item (plist-get attr
:author-title
)))
616 (if (and at-item
(stringp at-item
))
617 (format ".AT \"%s\" \n" at-item
)
621 (when (plist-get info
:with-date
)
622 (let ((date (org-export-data (plist-get info
:date
) info
)))
623 (and date
(format ".ND \"%s\"\n" date
))))
626 ;; If Abstract, then Populate Abstract
629 (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info
))
630 (to-data (org-groff--get-tagged-content "TO" info
)))
633 (format ".AS\n%s\n.AE\n" abstract-data
))
635 (format ".AS\n%s\n.AE\n" to-data
))))))
637 (defun org-groff--letter-head (title contents attr info
)
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-groff--get-tagged-content "FROM" info
))
644 (at-item (plist-get attr
:author-title
))
645 (to-data (org-groff--get-tagged-content "TO" info
)))
648 ;; If FROM then get data from FROM
651 (replace-regexp-in-string "\\.P\n" "" from-data
))
656 (replace-regexp-in-string "\\.P\n" "" to-data
))
662 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
663 ((and author email
(not (string= "" email
)))
664 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
665 (author (format ".WA \"%s\"\n.WE\n" author
))
666 (t ".WA \"\" \n.WE\n"))
668 ;; If TO then get data from TO
671 (format ".IA \n%s\n.IE\n" to-data
)))))
676 (defun org-groff-template (contents info
)
677 "Return complete document string after Groff conversion.
678 CONTENTS is the transcoded contents string. INFO is a plist
679 holding export options."
680 (let* ((title (org-export-data (plist-get info
:title
) info
))
685 (list (plist-get info
:groff-class-options
))
687 (class (plist-get info
:groff-class
))
688 (class-options (plist-get info
:groff-class-options
))
689 (classes (assoc class org-groff-classes
))
690 (classes-options (car (last classes
)))
691 (heading-option (plist-get classes-options
:heading
))
692 (type-option (plist-get classes-options
:type
))
693 (last-option (plist-get classes-options
:last-section
))
694 (hyphenate (plist-get attr
:hyphenate
))
695 (justify-right (plist-get attr
:justify-right
))
697 (document-class-string
699 (org-element-normalize-string
700 (let* ((header (nth 1 (assoc class org-groff-classes
)))
701 (document-class-item (if (stringp header
) header
"")))
702 document-class-item
)))))
721 ((string= type-option
"custom") "")
723 ((and (stringp document-class-string
)
724 (string= type-option
"cover"))
727 (format ".COVER %s\n" document-class-string
)
728 (org-groff--mt-head title contents attr info
)
731 ((string= type-option
"memo")
733 (org-groff--mt-head title contents attr info
)
734 document-class-string
))
735 ((string= type-option
"letter")
737 (org-groff--letter-head title contents attr info
)
738 (let ((sa-item (plist-get attr
:salutation
))
739 (cn-item (plist-get attr
:confidential
))
740 (sj-item (plist-get attr
:subject
))
741 (rn-item (plist-get attr
:reference
))
742 (at-item (plist-get attr
:attention
)))
746 (if (stringp sa-item
)
747 (format ".LO SA \"%s\" \n" sa-item
)
751 (if (stringp cn-item
)
752 (format ".LO CN \"%s\"\n" cn-item
)
755 (when (and at-item
(stringp at-item
))
756 (format ".LO AT \"%s\" \n" at-item
))
757 (when (and title rn-item
)
758 (format ".LO RN \"%s\"\n" title
))
760 (when (and sj-item
(stringp sj-item
))
761 (format ".LO SJ \"%s\" \n" sj-item
))
764 ".LT " document-class-string
"\n"))))
771 ((string= last-option
"toc")
773 ((string= last-option
"sign")
774 (let ((fc-item (plist-get attr
:closing
)))
775 (concat (if (stringp fc-item
)
776 (format ".FC \"%s\" \n" fc-item
)
784 (when (string= (car item
) "NS")
785 (replace-regexp-in-string
786 "\\.P\n" "" (cdr item
))))
787 (reverse org-groff-special-content
) "\n")))))
791 ;;; Transcode Functions
795 ;; Babel Calls are ignored.
800 (defun org-groff-bold (bold contents info
)
801 "Transcode BOLD from Org to Groff.
802 CONTENTS is the text with bold markup. INFO is a plist holding
803 contextual information."
804 (org-groff--text-markup contents
'bold
))
808 (defun org-groff-center-block (center-block contents info
)
809 "Transcode a CENTER-BLOCK element from Org to Groff.
810 CONTENTS holds the contents of the center block. INFO is a plist
811 holding contextual information."
812 (org-groff--wrap-label
814 (format ".DS C \n%s\n.DE" contents
)))
818 (defun org-groff-clock (clock contents info
)
819 "Transcode a CLOCK element from Org to Groff.
820 CONTENTS is nil. INFO is a plist holding contextual
823 (format "\\fB%s\\fP " org-clock-string
)
824 (format org-groff-inactive-timestamp-format
825 (concat (org-translate-time
826 (org-element-property :raw-value
827 (org-element-property :value clock
)))
828 (let ((time (org-element-property :duration clock
)))
829 (and time
(format " (%s)" time
)))))))
833 (defun org-groff-code (code contents info
)
834 "Transcode a CODE object from Org to Groff.
835 CONTENTS is nil. INFO is a plist used as a communication
837 (org-groff--text-markup (org-element-property :value code
) 'code
))
839 ;;; Comments and Comment Blocks are ignored.
843 (defun org-groff-drawer (drawer contents info
)
844 "Transcode a DRAWER element from Org to Groff.
845 CONTENTS holds the contents of the block. INFO is a plist
846 holding contextual information."
847 (let* ((name (org-element-property :drawer-name drawer
))
848 (output (if (functionp org-groff-format-drawer-function
)
849 (funcall org-groff-format-drawer-function
851 ;; If there's no user defined function: simply
852 ;; display contents of the drawer.
854 (org-groff--wrap-label drawer output
)))
858 (defun org-groff-dynamic-block (dynamic-block contents info
)
859 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
860 CONTENTS holds the contents of the block. INFO is a plist
861 holding contextual information. See `org-export-data'."
862 (org-groff--wrap-label dynamic-block contents
))
866 (defun org-groff-entity (entity contents info
)
867 "Transcode an ENTITY object from Org to Groff.
868 CONTENTS are the definition itself. INFO is a plist holding
869 contextual information."
870 (org-element-property :utf-8 entity
))
874 (defun org-groff-example-block (example-block contents info
)
875 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
876 CONTENTS is nil. INFO is a plist holding contextual
878 (org-groff--wrap-label
880 (format ".DS L\n%s\n.DE"
881 (org-export-format-code-default example-block info
))))
885 (defun org-groff-export-block (export-block contents info
)
886 "Transcode a EXPORT-BLOCK element from Org to Groff.
887 CONTENTS is nil. INFO is a plist holding contextual information."
888 (when (string= (org-element-property :type export-block
) "GROFF")
889 (org-remove-indentation (org-element-property :value export-block
))))
893 (defun org-groff-export-snippet (export-snippet contents info
)
894 "Transcode a EXPORT-SNIPPET object from Org to Groff.
895 CONTENTS is nil. INFO is a plist holding contextual information."
896 (when (eq (org-export-snippet-backend export-snippet
) 'groff
)
897 (org-element-property :value export-snippet
)))
901 (defun org-groff-fixed-width (fixed-width contents info
)
902 "Transcode a FIXED-WIDTH element from Org to Groff.
903 CONTENTS is nil. INFO is a plist holding contextual information."
904 (org-groff--wrap-label
906 (format "\\fC\n%s\\fP"
907 (org-remove-indentation
908 (org-element-property :value fixed-width
)))))
910 ;;; Footnote Definition
912 ;; Footnote Definitions are ignored.
914 ;; Footnotes are handled automatically in GROFF. Although manual
915 ;; references can be added, not really required.
917 (defun org-groff-footnote-reference (footnote-reference contents info
)
918 ;; Changing from info to footnote-reference
919 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
920 (n (org-export-get-footnote-number footnote-reference info
))
921 (data (org-trim (org-export-data raw info
)))
922 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
924 (if (string-match "fn:rl" ref-id
)
925 (if (member ref-id org-groff-registered-references
)
926 (format "\\*[%s]" ref-id
)
928 (push ref-id org-groff-registered-references
)
929 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
930 ;; else it is a footnote
931 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
935 (defun org-groff-headline (headline contents info
)
936 "Transcode an HEADLINE element from Org to Groff.
937 CONTENTS holds the contents of the headline. INFO is a plist
938 holding contextual information."
939 (let* ((class (plist-get info
:groff-class
))
940 (level (org-export-get-relative-level headline info
))
941 (numberedp (org-export-numbered-headline-p headline info
))
942 ;; Section formatting will set two placeholders: one for the
943 ;; title and the other for the contents.
944 (classes (assoc class org-groff-classes
))
945 (classes-options (car (last classes
)))
946 (heading-option (plist-get classes-options
:heading
))
950 ((and (symbolp heading-option
)
951 (fboundp heading-option
))
952 (funcall heading-option level numberedp
))
955 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
956 ".HU \"%s\"\n%s")))))
957 ;; End of section-fmt
958 (text (org-export-data (org-element-property :title headline
) info
))
960 (and (plist-get info
:with-todo-keywords
)
961 (let ((todo (org-element-property :todo-keyword headline
)))
962 (and todo
(org-export-data todo info
)))))
963 (todo-type (and todo
(org-element-property :todo-type headline
)))
964 (tags (and (plist-get info
:with-tags
)
965 (org-export-get-tags headline info
)))
966 (priority (and (plist-get info
:with-priority
)
967 (org-element-property :priority headline
)))
968 ;; Create the headline text along with a no-tag version. The
969 ;; latter is required to remove tags from table of contents.
970 (full-text (if (functionp org-groff-format-headline-function
)
971 ;; User-defined formatting function.
972 (funcall org-groff-format-headline-function
973 todo todo-type priority text tags
)
974 ;; Default formatting.
977 (format "\\fB%s\\fP " todo
))
978 (when priority
(format " [\\#%c] " priority
))
981 (format " \\fC:%s:\\fP "
982 (mapconcat 'identity tags
":"))))))
984 (if (functionp org-groff-format-headline-function
)
985 ;; User-defined formatting function.
986 (funcall org-groff-format-headline-function
987 todo todo-type priority text nil
)
988 ;; Default formatting.
990 (when todo
(format "\\fB%s\\fP " todo
))
991 (when priority
(format " [\\#%c] " priority
))
993 ;; Associate some \label to the headline for internal links.
995 ;; (format "\\label{sec-%s}\n"
996 ;; (mapconcat 'number-to-string
997 ;; (org-export-get-headline-number headline info)
1001 (make-string (org-element-property :pre-blank headline
) 10)))
1004 ;; Case 1: Special Tag
1005 ((member (car tags
) org-groff-special-tags
)
1007 ((string= (car tags
) "BODY") contents
)
1009 ((string= (car tags
) "NS")
1011 (push (cons (car tags
)
1012 (format ".NS \"%s\" 1 \n%s"
1013 (car (org-element-property :title headline
))
1015 org-groff-special-content
) nil
))
1019 (push (cons (car tags
) contents
) org-groff-special-content
)
1022 ;; Case 2: This is a footnote section: ignore it.
1023 ((org-element-property :footnote-section-p headline
) nil
)
1025 ;; Case 3: This is a deep sub-tree: export it as a list item.
1026 ;; Also export as items headlines for which no section
1027 ;; format has been found.
1028 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1029 ;; Build the real contents of the sub-tree.
1030 (let ((low-level-body
1032 ;; If the headline is the first sibling, start a list.
1033 (when (org-export-first-sibling-p headline info
)
1034 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1036 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1037 ;; If headline is not the last sibling simply return
1038 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1040 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1041 (replace-regexp-in-string
1046 ;; Case 4. Standard headline. Export it as a section.
1048 (format section-fmt full-text
1049 (concat headline-label pre-blanks contents
))))))
1054 ;;; Inline Babel Call
1056 ;; Inline Babel Calls are ignored.
1058 ;;; Inline Src Block
1060 (defun org-groff-inline-src-block (inline-src-block contents info
)
1061 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1062 CONTENTS holds the contents of the item. INFO is a plist holding
1063 contextual information."
1064 (let* ((code (org-element-property :value inline-src-block
)))
1066 (org-groff-source-highlight
1067 (let* ((tmpdir (if (featurep 'xemacs
)
1069 temporary-file-directory
))
1070 (in-file (make-temp-name
1071 (expand-file-name "srchilite" tmpdir
)))
1072 (out-file (make-temp-name
1073 (expand-file-name "reshilite" tmpdir
)))
1074 (org-lang (org-element-property :language inline-src-block
))
1075 (lst-lang (cadr (assq (intern org-lang
)
1076 org-groff-source-highlight-langs
)))
1078 (cmd (concat (expand-file-name "source-highlight")
1080 " -f groff_mm_color "
1084 (let ((code-block ""))
1085 (with-temp-file in-file
(insert code
))
1087 (setq code-block
(org-file-contents out-file
))
1088 (delete-file in-file
)
1089 (delete-file out-file
)
1091 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1094 ;; Do not use a special package: transcode it verbatim.
1096 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1100 (defun org-groff-inlinetask (inlinetask contents info
)
1101 "Transcode an INLINETASK element from Org to Groff.
1102 CONTENTS holds the contents of the block. INFO is a plist
1103 holding contextual information."
1104 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1105 (todo (and (plist-get info
:with-todo-keywords
)
1106 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1107 (and todo
(org-export-data todo info
)))))
1108 (todo-type (org-element-property :todo-type inlinetask
))
1109 (tags (and (plist-get info
:with-tags
)
1110 (org-export-get-tags inlinetask info
)))
1111 (priority (and (plist-get info
:with-priority
)
1112 (org-element-property :priority inlinetask
))))
1113 ;; If `org-groff-format-inlinetask-function' is provided, call it
1114 ;; with appropriate arguments.
1115 (if (functionp org-groff-format-inlinetask-function
)
1116 (funcall org-groff-format-inlinetask-function
1117 todo todo-type priority title tags contents
)
1118 ;; Otherwise, use a default template.
1119 (org-groff--wrap-label
1123 (when todo
(format "\\fB%s\\fP " todo
))
1124 (when priority
(format " [\\#%c] " priority
))
1126 (when tags
(format " \\fC:%s:\\fP "
1127 (mapconcat 'identity tags
":"))))))
1128 (format (concat "\n.DS I\n"
1133 full-title contents
))))))
1137 (defun org-groff-italic (italic contents info
)
1138 "Transcode ITALIC from Org to Groff.
1139 CONTENTS is the text with italic markup. INFO is a plist holding
1140 contextual information."
1141 (org-groff--text-markup contents
'italic
))
1145 (defun org-groff-item (item contents info
)
1146 "Transcode an ITEM element from Org to Groff.
1147 CONTENTS holds the contents of the item. INFO is a plist holding
1148 contextual information."
1149 (let* ((bullet (org-element-property :bullet item
))
1150 (type (org-element-property
1151 :type
(org-element-property :parent item
)))
1152 (checkbox (case (org-element-property :checkbox item
)
1153 (on "\\o'\\(sq\\(mu'")
1155 (trans "\\o'\\(sq\\(mi'")))
1156 (tag (let ((tag (org-element-property :tag item
)))
1157 ;; Check-boxes must belong to the tag.
1158 (and tag
(format "%s"
1160 (org-export-data tag info
)))))))
1164 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1166 (org-trim (or contents
" "))))
1170 (org-trim (or contents
" "))))
1172 (let* ((bullet (org-trim bullet
))
1173 (marker (cond ((string= "-" bullet
) "\\(em")
1174 ((string= "*" bullet
) "\\(bu")
1176 (concat ".LI " marker
"\n"
1177 (org-trim (or contents
" "))))))))
1181 (defun org-groff-keyword (keyword contents info
)
1182 "Transcode a KEYWORD element from Org to Groff.
1183 CONTENTS is nil. INFO is a plist holding contextual information."
1184 (let ((key (org-element-property :key keyword
))
1185 (value (org-element-property :value keyword
)))
1187 ((string= key
"GROFF") value
)
1192 (defun org-groff-line-break (line-break contents info
)
1193 "Transcode a LINE-BREAK object from Org to Groff.
1194 CONTENTS is nil. INFO is a plist holding contextual information."
1198 ;; Inline images just place a call to .PSPIC or .PS/.PE
1199 ;; and load the graph.
1201 (defun org-groff-link--inline-image (link info
)
1202 "Return Groff code for an inline image.
1203 LINK is the link pointing to the inline image. INFO is a plist
1204 used as a communication channel."
1205 (let* ((parent (org-export-get-parent-element link
))
1206 (path (let ((raw-path (org-element-property :path link
)))
1207 (if (not (file-name-absolute-p raw-path
)) raw-path
1208 (expand-file-name raw-path
))))
1209 (attr (org-export-read-attribute :attr_groff link
))
1211 (case (plist-get attr
:position
)
1216 (width (or (plist-get attr
:width
) ""))
1217 (height (or (plist-get attr
:height
) ""))
1218 (caption (and (not (plist-get attr
:disable-caption
))
1219 (org-groff--caption/label-string parent info
))))
1220 ;; Now clear ATTR from any special keyword and set a default value
1221 ;; if nothing is left. Return proper string.
1224 ((and org-groff-raster-to-ps
1225 (or (string-match ".\.png$" path
)
1226 (string-match ".\.jpg$" path
)))
1227 (let ((eps-path (concat path
".eps")))
1228 (shell-command (format org-groff-raster-to-ps path eps-path
))
1229 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1230 placement eps-path width height
)))
1231 ((string-match ".\.pic$" path
)
1232 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1233 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1234 placement path width height
)))
1235 (and caption
(format "\n.FG \"%s\"" caption
)))))
1237 (defun org-groff-link (link desc info
)
1238 "Transcode a LINK object from Org to Groff.
1240 DESC is the description part of the link, or the empty string.
1241 INFO is a plist holding contextual information. See
1244 (let* ((type (org-element-property :type link
))
1245 (raw-path (org-element-property :path link
))
1246 ;; Ensure DESC really exists, or set it to nil.
1247 (desc (and (not (string= desc
"")) desc
))
1248 (imagep (org-export-inline-image-p
1249 link org-groff-inline-image-rules
))
1251 ((member type
'("http" "https" "ftp" "mailto"))
1252 (concat type
":" raw-path
))
1253 ((string= type
"file")
1254 (when (string-match "\\(.+\\)::.+" raw-path
)
1255 (setq raw-path
(match-string 1 raw-path
)))
1256 (if (file-name-absolute-p raw-path
)
1257 (concat "file://" (expand-file-name raw-path
))
1258 (concat "file://" raw-path
)))
1263 (imagep (org-groff-link--inline-image link info
))
1264 ;; import groff files
1265 ((and (string= type
"file")
1266 (string-match ".\.groff$" raw-path
))
1267 (concat ".so " raw-path
"\n"))
1268 ;; Radio link: transcode target's contents and use them as link's
1270 ((string= type
"radio")
1271 (let ((destination (org-export-resolve-radio-link link info
)))
1273 (format "\\fI [%s] \\fP"
1274 (org-export-solidify-link-text path
)))))
1276 ;; Links pointing to an headline: find destination and build
1277 ;; appropriate referencing command.
1278 ((member type
'("custom-id" "fuzzy" "id"))
1279 (let ((destination (if (string= type
"fuzzy")
1280 (org-export-resolve-fuzzy-link link info
)
1281 (org-export-resolve-id-link link info
))))
1282 (case (org-element-type destination
)
1283 ;; Id link points to an external file.
1285 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1286 (format "\\fI file://%s \\fP" destination
)))
1287 ;; Fuzzy link points nowhere.
1289 (format org-groff-link-with-unknown-path-format
1292 (org-element-property :raw-link link
) info
))))
1293 ;; Fuzzy link points to an invisible target.
1295 ;; LINK points to an headline. If headlines are numbered
1296 ;; and the link has no description, display headline's
1297 ;; number. Otherwise, display description or headline's
1301 (if (and (plist-get info
:section-numbers
) (not desc
))
1302 (format "\\fI%s\\fP" label
)
1303 (format "\\fI%s\\fP"
1306 (org-element-property :title destination
) info
))))))
1307 ;; Fuzzy link points to a target. Do as above.
1309 (let ((path (org-export-solidify-link-text path
)))
1310 (if (not desc
) (format "\\fI%s\\fP" path
)
1311 (format "%s \\fBat\\fP \\fI%s\\fP" desc path
)))))))
1312 ;; External link with a description part.
1313 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1314 ;; External link without a description part.
1315 (path (format "\\fI%s\\fP" path
))
1316 ;; No path, only description. Try to do something useful.
1317 (t (format org-groff-link-with-unknown-path-format desc
)))))
1321 (defun org-groff-paragraph (paragraph contents info
)
1322 "Transcode a PARAGRAPH element from Org to Groff.
1323 CONTENTS is the contents of the paragraph, as a string. INFO is
1324 the plist used as a communication channel."
1325 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1327 (let* ((parent-type (car parent
))
1328 (fixed-paragraph "")
1329 (class (plist-get info
:groff-class
))
1330 (class-options (plist-get info
:groff-class-options
))
1331 (classes (assoc class org-groff-classes
))
1332 (classes-options (car (last classes
)))
1333 (paragraph-option (plist-get classes-options
:paragraph
)))
1335 ((and (symbolp paragraph-option
)
1336 (fboundp paragraph-option
))
1337 (funcall paragraph-option parent-type parent contents
))
1338 ((and (eq parent-type
'item
)
1339 (plist-get (nth 1 parent
) :bullet
))
1340 (setq fixed-paragraph
(concat "" contents
)))
1341 ((eq parent-type
'section
)
1342 (setq fixed-paragraph
(concat ".P\n" contents
)))
1343 ((eq parent-type
'footnote-definition
)
1344 (setq fixed-paragraph
(concat "" contents
)))
1345 (t (setq fixed-paragraph
(concat "" contents
))))
1350 (defun org-groff-plain-list (plain-list contents info
)
1351 "Transcode a PLAIN-LIST element from Org to Groff.
1352 CONTENTS is the contents of the list. INFO is a plist holding
1353 contextual information."
1354 (let* ((type (org-element-property :type plain-list
))
1355 (attr (mapconcat #'identity
1356 (org-element-property :attr_groff plain-list
)
1359 ((eq type
'ordered
) ".AL")
1360 ((eq type
'unordered
) ".BL")
1361 ((eq type
'descriptive
) ".VL 2.0i"))))
1362 (org-groff--wrap-label
1364 (format "%s\n%s\n.LE" groff-type contents
))))
1368 (defun org-groff-plain-text (text info
)
1369 "Transcode a TEXT string from Org to Groff.
1370 TEXT is the string to transcode. INFO is a plist holding
1371 contextual information."
1372 (let ((output text
))
1373 ;; Protect various characters.
1374 (setq output
(replace-regexp-in-string
1375 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1376 "$\\" output nil t
1))
1377 ;; Activate smart quotes. Be sure to provide original TEXT string
1378 ;; since OUTPUT may have been modified.
1379 (when (plist-get info
:with-smart-quotes
)
1380 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
1381 ;; Handle Special Characters
1382 (if org-groff-special-char
1383 (dolist (special-char-list org-groff-special-char
)
1385 (replace-regexp-in-string (car special-char-list
)
1386 (cdr special-char-list
) output
))))
1387 ;; Handle break preservation if required.
1388 (when (plist-get info
:preserve-breaks
)
1389 (setq output
(replace-regexp-in-string
1390 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output
)))
1396 (defun org-groff-planning (planning contents info
)
1397 "Transcode a PLANNING element from Org to Groff.
1398 CONTENTS is nil. INFO is a plist holding contextual
1405 (let ((closed (org-element-property :closed planning
)))
1408 (format "\\fR %s \\fP" org-closed-string
)
1409 (format org-groff-inactive-timestamp-format
1411 (org-element-property :raw-value closed
))))))
1412 (let ((deadline (org-element-property :deadline planning
)))
1415 (format "\\fB %s \\fP" org-deadline-string
)
1416 (format org-groff-active-timestamp-format
1418 (org-element-property :raw-value deadline
))))))
1419 (let ((scheduled (org-element-property :scheduled planning
)))
1422 (format "\\fR %s \\fP" org-scheduled-string
)
1423 (format org-groff-active-timestamp-format
1425 (org-element-property :raw-value scheduled
))))))))
1431 (defun org-groff-property-drawer (property-drawer contents info
)
1432 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1433 CONTENTS is nil. INFO is a plist holding contextual
1435 ;; The property drawer isn't exported but we want separating blank
1436 ;; lines nonetheless.
1441 (defun org-groff-quote-block (quote-block contents info
)
1442 "Transcode a QUOTE-BLOCK element from Org to Groff.
1443 CONTENTS holds the contents of the block. INFO is a plist
1444 holding contextual information."
1445 (org-groff--wrap-label
1447 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1451 (defun org-groff-quote-section (quote-section contents info
)
1452 "Transcode a QUOTE-SECTION element from Org to Groff.
1453 CONTENTS is nil. INFO is a plist holding contextual information."
1454 (let ((value (org-remove-indentation
1455 (org-element-property :value quote-section
))))
1456 (when value
(format ".DS L\n\\fI%s\\fP\n.DE\n" value
))))
1460 (defun org-groff-radio-target (radio-target text info
)
1461 "Transcode a RADIO-TARGET object from Org to Groff.
1462 TEXT is the text of the target. INFO is a plist holding
1463 contextual information."
1465 (org-export-solidify-link-text
1466 (org-element-property :value radio-target
))
1471 (defun org-groff-section (section contents info
)
1472 "Transcode a SECTION element from Org to Groff.
1473 CONTENTS holds the contents of the section. INFO is a plist
1474 holding contextual information."
1479 (defun org-groff-special-block (special-block contents info
)
1480 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1481 CONTENTS holds the contents of the block. INFO is a plist
1482 holding contextual information."
1483 (let ((type (downcase (org-element-property :type special-block
))))
1484 (org-groff--wrap-label
1486 (format "%s\n" contents
))))
1490 (defun org-groff-src-block (src-block contents info
)
1491 "Transcode a SRC-BLOCK element from Org to Groff.
1492 CONTENTS holds the contents of the item. INFO is a plist holding
1493 contextual information."
1494 (let* ((lang (org-element-property :language src-block
))
1495 (label (org-element-property :name src-block
))
1496 (code (org-element-property :value src-block
))
1497 (custom-env (and lang
1498 (cadr (assq (intern lang
)
1499 org-groff-custom-lang-environments
))))
1500 (num-start (case (org-element-property :number-lines src-block
)
1501 (continued (org-export-get-loc src-block info
))
1503 (retain-labels (org-element-property :retain-labels src-block
))
1504 (caption (and (not (org-export-read-attribute
1505 :attr_groff src-block
:disable-caption
))
1506 (org-groff--caption/label-string src-block info
))))
1509 ;; Case 1. No source fontification.
1510 ((not org-groff-source-highlight
)
1512 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1513 (org-export-format-code-default src-block info
))
1514 (and caption
(format ".EX \"%s\" " caption
))))
1516 ;; Case 2. Source fontification.
1517 (org-groff-source-highlight
1518 (let* ((tmpdir (if (featurep 'xemacs
)
1520 temporary-file-directory
))
1521 (in-file (make-temp-name
1522 (expand-file-name "srchilite" tmpdir
)))
1523 (out-file (make-temp-name
1524 (expand-file-name "reshilite" tmpdir
)))
1526 (org-lang (org-element-property :language src-block
))
1527 (lst-lang (cadr (assq (intern org-lang
)
1528 org-groff-source-highlight-langs
)))
1530 (cmd (concat "source-highlight"
1532 " -f groff_mm_color "
1538 (let ((code-block ""))
1539 (with-temp-file in-file
(insert code
))
1541 (setq code-block
(org-file-contents out-file
))
1542 (delete-file in-file
)
1543 (delete-file out-file
)
1544 (format "%s\n" code-block
))
1545 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1547 (and caption
(format ".EX \"%s\" " caption
))))))))
1550 ;;; Statistics Cookie
1552 (defun org-groff-statistics-cookie (statistics-cookie contents info
)
1553 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1554 CONTENTS is nil. INFO is a plist holding contextual information."
1555 (org-element-property :value statistics-cookie
))
1560 (defun org-groff-strike-through (strike-through contents info
)
1561 "Transcode STRIKE-THROUGH from Org to Groff.
1562 CONTENTS is the text with strike-through markup. INFO is a plist
1563 holding contextual information."
1564 (org-groff--text-markup contents
'strike-through
))
1568 (defun org-groff-subscript (subscript contents info
)
1569 "Transcode a SUBSCRIPT object from Org to Groff.
1570 CONTENTS is the contents of the object. INFO is a plist holding
1571 contextual information."
1572 (format "\\d\\s-2%s\\s+2\\u" contents
))
1574 ;;; Superscript "^_%s$
1576 (defun org-groff-superscript (superscript contents info
)
1577 "Transcode a SUPERSCRIPT object from Org to Groff.
1578 CONTENTS is the contents of the object. INFO is a plist holding
1579 contextual information."
1580 (format "\\u\\s-2%s\\s+2\\d" contents
))
1585 ;; `org-groff-table' is the entry point for table transcoding. It
1586 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1587 ;; delegates the job to `org-groff-table--org-table' function,
1588 ;; depending of the type of the table.
1590 ;; `org-groff-table--align-string' is a subroutine used to build
1591 ;; alignment string for Org tables.
1593 (defun org-groff-table (table contents info
)
1594 "Transcode a TABLE element from Org to Groff.
1595 CONTENTS is the contents of the table. INFO is a plist holding
1596 contextual information."
1598 ;; Case 1: verbatim table.
1599 ((or org-groff-tables-verbatim
1600 (let ((attr (read (format "(%s)"
1603 (org-element-property :attr_groff table
) " ")))))
1604 (and attr
(plist-get attr
:verbatim
))))
1606 (format ".DS L\n\\fC%s\\fP\n.DE"
1607 ;; Re-create table, without affiliated keywords.
1609 (org-element-interpret-data
1610 `(table nil
,@(org-element-contents table
))))))
1612 ;; Case 2: Standard table.
1613 (t (org-groff-table--org-table table contents info
))))
1615 (defun org-groff-table--align-string (divider table info
)
1616 "Return an appropriate Groff alignment string.
1617 TABLE is the considered table. INFO is a plist used as
1618 a communication channel."
1620 ;; Extract column groups and alignment from first (non-rule) row.
1622 (org-element-map table
'table-row
1624 (and (eq (org-element-property :type row
) 'standard
) row
))
1628 (let* ((borders (org-export-table-cell-borders cell info
))
1629 (raw-width (org-export-table-cell-width cell info
))
1630 (width-cm (when raw-width
(/ raw-width
5)))
1631 (width (if raw-width
(format "w(%dc)"
1632 (if (< width-cm
1) 1 width-cm
)) "")))
1633 ;; Check left border for the first cell only.
1634 ;; Alignment is nil on assignment
1636 (when (and (memq 'left borders
) (not alignment
))
1637 (push "|" alignment
))
1639 (case (org-export-table-cell-alignment cell info
)
1640 (left (concat "l" width divider
))
1641 (right (concat "r" width divider
))
1642 (center (concat "c" width divider
)))
1644 (when (memq 'right borders
) (push "|" alignment
))))
1646 (apply 'concat
(reverse alignment
))))
1648 (defun org-groff-table--org-table (table contents info
)
1649 "Return appropriate Groff code for an Org table.
1651 TABLE is the table type element to transcode. CONTENTS is its
1652 contents, as a string. INFO is a plist used as a communication
1655 This function assumes TABLE has `org' as its `:type' attribute."
1656 (let* ((attr (org-export-read-attribute :attr_groff table
))
1657 (label (org-element-property :name table
))
1658 (caption (and (not (plist-get attr
:disable-caption
))
1659 (org-groff--caption/label-string table info
)))
1660 (divider (if (plist-get attr
:divider
) "|" " "))
1662 ;; Determine alignment string.
1663 (alignment (org-groff-table--align-string divider table info
))
1665 ;; Extract others display options.
1667 (lines (org-split-string contents
"\n"))
1673 (if (plist-get attr
:expand
)
1676 (case (plist-get attr
:placement
)
1680 (if org-groff-tables-centered
1683 (case (plist-get attr
:boxtype
)
1685 ('doublebox
"doublebox")
1690 (if (not (null attr-item
))
1691 (add-to-list 'result-list attr-item
)))
1694 (title-line (plist-get attr
:title-line
))
1695 (long-cells (plist-get attr
:long-cells
))
1700 (or (car attr-list
) ""))
1703 (when (cdr attr-list
)
1704 (dolist (attr-item (cdr attr-list
))
1705 (setq output-list
(concat output-list
1706 (format ",%s" attr-item
)))))
1709 (when lines
(org-split-string (car lines
) "\t"))))
1710 ;; Prepare the final format string for the table.
1716 (concat ".TS\n " table-format
";\n"
1718 (let ((final-line ""))
1720 (dotimes (i (length first-line
))
1721 (setq final-line
(concat final-line
"cb" divider
))))
1723 (setq final-line
(concat final-line
"\n"))
1726 (setq final-line
(concat final-line alignment
))
1727 (dotimes (i (length first-line
))
1728 (setq final-line
(concat final-line
"c" divider
))))
1732 (let ((final-line "")
1734 (lines (org-split-string contents
"\n")))
1736 (dolist (line-item lines
)
1741 (if (string= line-item
"_")
1742 (setq long-line
(format "%s\n" line-item
))
1744 (let ((cell-item-list (org-split-string line-item
"\t")))
1745 (dolist (cell-item cell-item-list
)
1747 (cond ((eq cell-item
(car (last cell-item-list
)))
1748 (setq long-line
(concat long-line
1749 (format "T{\n%s\nT}\t\n" cell-item
))))
1751 (setq long-line
(concat long-line
1752 (format "T{\n%s\nT}\t" cell-item
))))))
1755 (setq final-line
(concat final-line long-line
)))
1757 (setq final-line
(concat final-line line-item
"\n"))))
1760 (if caption
(format ".TB \"%s\"" caption
) ""))))))
1764 (defun org-groff-table-cell (table-cell contents info
)
1765 "Transcode a TABLE-CELL element from Org to Groff
1766 CONTENTS is the cell contents. INFO is a plist used as
1767 a communication channel."
1769 (concat (if (and contents
1770 org-groff-table-scientific-notation
1771 (string-match orgtbl-exp-regexp contents
))
1772 ;; Use appropriate format string for scientific
1774 (format org-groff-table-scientific-notation
1775 (match-string 1 contents
)
1776 (match-string 2 contents
))
1778 (when (org-export-get-next-element table-cell info
) "\t"))))
1783 (defun org-groff-table-row (table-row contents info
)
1784 "Transcode a TABLE-ROW element from Org to Groff
1785 CONTENTS is the contents of the row. INFO is a plist used as
1786 a communication channel."
1787 ;; Rules are ignored since table separators are deduced from
1788 ;; borders of the current row.
1789 (when (eq (org-element-property :type table-row
) 'standard
)
1790 (let* ((attr (mapconcat 'identity
1791 (org-element-property
1792 :attr_groff
(org-export-get-parent table-row
))
1794 ;; TABLE-ROW's borders are extracted from its first cell.
1796 (org-export-table-cell-borders
1797 (car (org-element-contents table-row
)) info
)))
1799 ;; Mark horizontal lines
1800 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1803 ;; When BOOKTABS are activated enforce bottom rule even when
1804 ;; no hline was specifically marked.
1805 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1806 ((memq 'below borders
) "\n_"))))))
1810 (defun org-groff-target (target contents info
)
1811 "Transcode a TARGET object from Org to Groff.
1812 CONTENTS is nil. INFO is a plist holding contextual
1814 (format "\\fI%s\\fP"
1815 (org-export-solidify-link-text (org-element-property :value target
))))
1819 (defun org-groff-timestamp (timestamp contents info
)
1820 "Transcode a TIMESTAMP object from Org to Groff.
1821 CONTENTS is nil. INFO is a plist holding contextual
1823 (let ((value (org-groff-plain-text
1824 (org-timestamp-translate timestamp
) info
)))
1825 (case (org-element-property :type timestamp
)
1826 ((active active-range
)
1827 (format org-groff-active-timestamp-format value
))
1828 ((inactive inactive-range
)
1829 (format org-groff-inactive-timestamp-format value
))
1830 (t (format org-groff-diary-timestamp-format value
)))))
1834 (defun org-groff-underline (underline contents info
)
1835 "Transcode UNDERLINE from Org to Groff.
1836 CONTENTS is the text with underline markup. INFO is a plist
1837 holding contextual information."
1838 (org-groff--text-markup contents
'underline
))
1842 (defun org-groff-verbatim (verbatim contents info
)
1843 "Transcode a VERBATIM object from Org to Groff.
1844 CONTENTS is nil. INFO is a plist used as a communication
1846 (org-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1850 (defun org-groff-verse-block (verse-block contents info
)
1851 "Transcode a VERSE-BLOCK element from Org to Groff.
1852 CONTENTS is verse block contents. INFO is a plist holding
1853 contextual information."
1854 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1857 ;;; Interactive functions
1859 (defun org-groff-export-to-groff
1860 (&optional async subtreep visible-only body-only ext-plist
)
1861 "Export current buffer to a Groff file.
1863 If narrowing is active in the current buffer, only export its
1866 If a region is active, export that region.
1868 A non-nil optional argument ASYNC means the process should happen
1869 asynchronously. The resulting file should be accessible through
1870 the `org-export-stack' interface.
1872 When optional argument SUBTREEP is non-nil, export the sub-tree
1873 at point, extracting information from the headline properties
1876 When optional argument VISIBLE-ONLY is non-nil, don't export
1877 contents of hidden elements.
1879 EXT-PLIST, when provided, is a property list with external
1880 parameters overriding Org default settings, but still inferior to
1881 file-local settings.
1883 Return output file's name."
1885 (let ((outfile (org-export-output-file-name ".groff" subtreep
)))
1887 (org-export-async-start
1888 (lambda (f) (org-export-add-to-stack f
'groff
))
1889 (let ((org-groff-registered-references nil
)
1890 (org-groff-special-content nil
))
1893 'groff
,outfile
,subtreep
,visible-only
,body-only
1895 (let ((org-groff-registered-references nil
)
1896 (org-groff-special-content nil
))
1898 'groff outfile subtreep visible-only body-only ext-plist
)))))
1900 (defun org-groff-export-to-pdf
1901 (&optional async subtreep visible-only body-only ext-plist
)
1902 "Export current buffer to Groff then process through to PDF.
1904 If narrowing is active in the current buffer, only export its
1907 If a region is active, export that region.
1909 A non-nil optional argument ASYNC means the process should happen
1910 asynchronously. The resulting file should be accessible through
1911 the `org-export-stack' interface.
1913 When optional argument SUBTREEP is non-nil, export the sub-tree
1914 at point, extracting information from the headline properties
1917 When optional argument VISIBLE-ONLY is non-nil, don't export
1918 contents of hidden elements.
1920 EXT-PLIST, when provided, is a property list with external
1921 parameters overriding Org default settings, but still inferior to
1922 file-local settings.
1924 Return PDF file's name."
1927 (let ((outfile (org-export-output-file-name ".groff" subtreep
)))
1928 (org-export-async-start
1929 (lambda (f) (org-export-add-to-stack f
'groff
))
1933 'groff
,outfile
,subtreep
,visible-only
,body-only
1936 (org-groff-export-to-groff
1937 nil subtreep visible-only body-only ext-plist
))))
1939 (defun org-groff-compile (file)
1940 "Compile a Groff file.
1942 FILE is the name of the file being compiled. Processing is done
1943 through the command specified in `org-groff-pdf-process'.
1945 Return PDF file name or an error if it couldn't be produced."
1946 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file
)))
1947 (full-name (file-truename file
))
1948 (out-dir (file-name-directory file
))
1949 ;; Make sure `default-directory' is set to FILE directory,
1950 ;; not to whatever value the current buffer may have.
1951 (default-directory (file-name-directory full-name
))
1953 (message (format "Processing Groff file %s ..." file
))
1954 (save-window-excursion
1956 ;; A function is provided: Apply it.
1957 ((functionp org-groff-pdf-process
)
1958 (funcall org-groff-pdf-process
(shell-quote-argument file
)))
1959 ;; A list is provided: Replace %b, %f and %o with appropriate
1960 ;; values in each command before applying it. Output is
1961 ;; redirected to "*Org PDF Groff Output*" buffer.
1962 ((consp org-groff-pdf-process
)
1963 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1967 (replace-regexp-in-string
1968 "%b" (shell-quote-argument base-name
)
1969 (replace-regexp-in-string
1970 "%f" (shell-quote-argument full-name
)
1971 (replace-regexp-in-string
1972 "%o" (shell-quote-argument out-dir
) command t t
)
1975 org-groff-pdf-process
)
1976 ;; Collect standard errors from output buffer.
1977 (setq errors
(org-groff-collect-errors outbuf
))))
1978 (t (error "No valid command to process to PDF")))
1979 (let ((pdffile (concat out-dir base-name
".pdf")))
1980 ;; Check for process failure. Provide collected errors if
1982 (if (not (file-exists-p pdffile
))
1983 (error (concat (format "PDF file %s wasn't produced" pdffile
)
1984 (when errors
(concat ": " errors
))))
1985 ;; Else remove log files, when specified, and signal end of
1986 ;; process to user, along with any error encountered.
1987 (when org-groff-remove-logfiles
1988 (dolist (ext org-groff-logfiles-extensions
)
1989 (let ((file (concat out-dir base-name
"." ext
)))
1990 (when (file-exists-p file
) (delete-file file
)))))
1991 (message (concat "Process completed"
1992 (if (not errors
) "."
1993 (concat " with errors: " errors
)))))
1994 ;; Return output file name.
1997 (defun org-groff-collect-errors (buffer)
1998 "Collect some kind of errors from \"groff\" output
1999 BUFFER is the buffer containing output.
2000 Return collected error types as a string, or nil if there was
2002 (with-current-buffer buffer
2004 (goto-char (point-max))
2010 ;;; ox-groff.el ends here