org-export: Use new API for macro expansion
[org-mode.git] / contrib / lisp / org-e-man.el
blob9c641ae50af33da5acdb80d0c8eb5e896e7142e3
1 ;; org-e-man.el --- Man Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;;
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/>.
23 ;;; Commentary:
25 ;; This library implements a Man back-end for Org generic exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'e-man "*Test e-Man*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Man
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;;; Code:
40 (require 'org-export)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
52 ;;;; Define Back-End
54 (defvar org-e-man-translate-alist
55 '((babel-call . org-e-man-babel-call)
56 (bold . org-e-man-bold)
57 (center-block . org-e-man-center-block)
58 (clock . org-e-man-clock)
59 (code . org-e-man-code)
60 (comment . org-e-man-comment)
61 (comment-block . org-e-man-comment-block)
62 (drawer . org-e-man-drawer)
63 (dynamic-block . org-e-man-dynamic-block)
64 (entity . org-e-man-entity)
65 (example-block . org-e-man-example-block)
66 (export-block . org-e-man-export-block)
67 (export-snippet . org-e-man-export-snippet)
68 (fixed-width . org-e-man-fixed-width)
69 (footnote-definition . org-e-man-footnote-definition)
70 (footnote-reference . org-e-man-footnote-reference)
71 (headline . org-e-man-headline)
72 (horizontal-rule . org-e-man-horizontal-rule)
73 (inline-babel-call . org-e-man-inline-babel-call)
74 (inline-src-block . org-e-man-inline-src-block)
75 (inlinetask . org-e-man-inlinetask)
76 (italic . org-e-man-italic)
77 (item . org-e-man-item)
78 (keyword . org-e-man-keyword)
79 (man-environment . org-e-man-man-environment)
80 (man-fragment . org-e-man-man-fragment)
81 (line-break . org-e-man-line-break)
82 (link . org-e-man-link)
83 (paragraph . org-e-man-paragraph)
84 (plain-list . org-e-man-plain-list)
85 (plain-text . org-e-man-plain-text)
86 (planning . org-e-man-planning)
87 (property-drawer . org-e-man-property-drawer)
88 (quote-block . org-e-man-quote-block)
89 (quote-section . org-e-man-quote-section)
90 (radio-target . org-e-man-radio-target)
91 (section . org-e-man-section)
92 (special-block . org-e-man-special-block)
93 (src-block . org-e-man-src-block)
94 (statistics-cookie . org-e-man-statistics-cookie)
95 (strike-through . org-e-man-strike-through)
96 (subscript . org-e-man-subscript)
97 (superscript . org-e-man-superscript)
98 (table . org-e-man-table)
99 (table-cell . org-e-man-table-cell)
100 (table-row . org-e-man-table-row)
101 (target . org-e-man-target)
102 (template . org-e-man-template)
103 (timestamp . org-e-man-timestamp)
104 (underline . org-e-man-underline)
105 (verbatim . org-e-man-verbatim)
106 (verse-block . org-e-man-verse-block))
107 "Alist between element or object types and translators.")
109 (defconst org-e-man-options-alist
110 '((:date "DATE" nil nil t)
111 (:man-class "MAN_CLASS" nil nil t)
112 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
113 (:man-header-extra "MAN_HEADER" nil nil newline))
114 "Alist between Man export properties and ways to set them.
115 See `org-export-options-alist' for more information on the
116 structure of the values.")
121 ;;; User Configurable Variables
124 (defgroup org-export-e-man nil
125 "Options for exporting Org mode files to Man."
126 :tag "Org Export Man"
127 :group 'org-export)
130 ;;;; Tables
133 (defcustom org-e-man-tables-centered t
134 "When non-nil, tables are exported in a center environment."
135 :group 'org-export-e-man
136 :type 'boolean)
138 (defcustom org-e-man-tables-verbatim nil
139 "When non-nil, tables are exported verbatim."
140 :group 'org-export-e-man
141 :type 'boolean)
143 (defcustom org-e-man-table-scientific-notation "%sE%s"
144 "Format string to display numbers in scientific notation.
145 The format should have \"%s\" twice, for mantissa and exponent
146 \(i.e. \"%s\\\\times10^{%s}\").
148 When nil, no transformation is made."
149 :group 'org-export-e-man
150 :type '(choice
151 (string :tag "Format string")
152 (const :tag "No formatting")))
155 ;;;; Inlinetasks
158 ;; Src blocks
160 (defcustom org-e-man-source-highlight nil
161 "Use GNU source highlight to embellish source blocks "
162 :group 'org-export-e-man
163 :type 'boolean)
165 (defcustom org-e-man-source-highlight-langs
166 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
167 (scheme "scheme")
168 (c "c") (cc "cpp") (csharp "csharp") (d "d")
169 (fortran "fortran") (cobol "cobol") (pascal "pascal")
170 (ada "ada") (asm "asm")
171 (perl "perl") (cperl "perl")
172 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
173 (java "java") (javascript "javascript")
174 (tex "latex")
175 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
176 (ocaml "caml") (caml "caml")
177 (sql "sql") (sqlite "sql")
178 (html "html") (css "css") (xml "xml")
179 (bat "bat") (bison "bison") (clipper "clipper")
180 (ldap "ldap") (opa "opa")
181 (php "php") (postscript "postscript") (prolog "prolog")
182 (properties "properties") (makefile "makefile")
183 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
184 "Alist mapping languages to their listing language counterpart.
185 The key is a symbol, the major mode symbol without the \"-mode\".
186 The value is the string that should be inserted as the language
187 parameter for the listings package. If the mode name and the
188 listings name are the same, the language does not need an entry
189 in this list - but it does not hurt if it is present."
190 :group 'org-export-e-man
191 :type '(repeat
192 (list
193 (symbol :tag "Major mode ")
194 (string :tag "Listings language"))))
197 (defvar org-e-man-custom-lang-environments nil
198 "Alist mapping languages to language-specific Man environments.
200 It is used during export of src blocks by the listings and
201 man packages. For example,
203 \(setq org-e-man-custom-lang-environments
204 '\(\(python \"pythoncode\"\)\)\)
206 would have the effect that if org encounters begin_src python
207 during man export."
211 ;;;; Plain text
213 (defcustom org-e-man-quotes
214 '(("fr"
215 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
216 ("\\(\\S-\\)\"" . "~»")
217 ("\\(\\s-\\|(\\|^\\)'" . "'"))
218 ("en"
219 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
220 ("\\(\\S-\\)\"" . "''")
221 ("\\(\\s-\\|(\\|^\\)'" . "`")))
223 "Alist for quotes to use when converting english double-quotes.
225 The CAR of each item in this alist is the language code.
226 The CDR of each item in this alist is a list of three CONS:
227 - the first CONS defines the opening quote;
228 - the second CONS defines the closing quote;
229 - the last CONS defines single quotes.
231 For each item in a CONS, the first string is a regexp
232 for allowed characters before/after the quote, the second
233 string defines the replacement string for this quote."
234 :group 'org-export-e-man
235 :type '(list
236 (cons :tag "Opening quote"
237 (string :tag "Regexp for char before")
238 (string :tag "Replacement quote "))
239 (cons :tag "Closing quote"
240 (string :tag "Regexp for char after ")
241 (string :tag "Replacement quote "))
242 (cons :tag "Single quote"
243 (string :tag "Regexp for char before")
244 (string :tag "Replacement quote "))))
247 ;;;; Compilation
249 (defcustom org-e-man-pdf-process
250 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
251 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
252 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
254 "Commands to process a Man file to a PDF file.
255 This is a list of strings, each of them will be given to the
256 shell as a command. %f in the command will be replaced by the
257 full file name, %b by the file base name \(i.e. without
258 extension) and %o by the base directory of the file.
261 By default, Org uses 3 runs of to do the processing.
263 Alternatively, this may be a Lisp function that does the
264 processing. This function should accept the file name as
265 its single argument."
266 :group 'org-export-pdf
267 :type '(choice
268 (repeat :tag "Shell command sequence"
269 (string :tag "Shell command"))
270 (const :tag "2 runs of pdfgroff"
271 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
272 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
273 (const :tag "3 runs of pdfgroff"
274 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
275 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
276 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
277 (function)))
279 (defcustom org-e-man-logfiles-extensions
280 '("log" "out" "toc")
281 "The list of file extensions to consider as Man logfiles."
282 :group 'org-export-e-man
283 :type '(repeat (string :tag "Extension")))
285 (defcustom org-e-man-remove-logfiles t
286 "Non-nil means remove the logfiles produced by PDF production.
287 These are the .aux, .log, .out, and .toc files."
288 :group 'org-export-e-man
289 :type 'boolean)
293 ;; Preamble
296 ;; Adding MAN as a block parser to make sure that its contents
297 ;; does not execute
299 (add-to-list 'org-element-block-name-alist
300 '("MAN" . org-element-export-block-parser))
306 ;;; Internal Functions
308 (defun org-e-man--caption/label-string (caption label info)
309 "Return caption and label Man string for floats.
311 CAPTION is a cons cell of secondary strings, the car being the
312 standard caption and the cdr its short form. LABEL is a string
313 representing the label. INFO is a plist holding contextual
314 information.
316 If there's no caption nor label, return the empty string.
318 For non-floats, see `org-e-man--wrap-label'."
319 (let ((label-str ""))
320 (cond
321 ((and (not caption) (not label)) "")
322 ((not caption) (format "\\fI%s\\fP" label))
323 ;; Option caption format with short name.
324 ((cdr caption)
325 (format "\\fR%s\\fP - \\fI%s\\P - %s\n"
326 (org-export-data (cdr caption) info)
327 label-str
328 (org-export-data (car caption) info)))
329 ;; Standard caption format.
330 (t (format "\\fR%s\\fP"
331 (org-export-data (car caption) info))))))
333 (defun org-e-man--quotation-marks (text info)
334 "Export quotation marks depending on language conventions.
335 TEXT is a string containing quotation marks to be replaced. INFO
336 is a plist used as a communication channel."
337 (mapc (lambda(l)
338 (let ((start 0))
339 (while (setq start (string-match (car l) text start))
340 (let ((new-quote (concat (match-string 1 text) (cdr l))))
341 (setq text (replace-match new-quote t t text))))))
342 (cdr (or (assoc (plist-get info :language) org-e-man-quotes)
343 ;; Falls back on English.
344 (assoc "en" org-e-man-quotes))))
345 text)
347 (defun org-e-man--wrap-label (element output)
348 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
349 This function shouldn't be used for floats. See
350 `org-e-man--caption/label-string'."
351 (let ((label (org-element-property :name element)))
352 (if (or (not output) (not label) (string= output "") (string= label ""))
353 output
354 (concat (format "%s\n.br\n" label) output))))
359 ;;; Template
361 (defun org-e-man-template (contents info)
362 "Return complete document string after Man conversion.
363 CONTENTS is the transcoded contents string. INFO is a plist
364 holding export options."
365 (let* ((title (org-export-data (plist-get info :title) info))
366 (attr
367 (read (format "(%s)"
368 (mapconcat
369 #'identity
370 (list (plist-get info :man-class-options))
371 " "))))
372 (section-item (plist-get attr :section-id)))
374 (concat
375 (cond
376 ((and title (stringp section-item))
377 (format ".TH \"%s\" \"%s\" \n" title section-item))
378 ((and (string= "" title) (stringp section-item))
379 (format ".TH \"%s\" \"%s\" \n" " " section-item))
380 (title
381 (format ".TH \"%s\" \"1\" \n" title))
383 ".TH \" \" \"1\" "))
384 contents)))
389 ;;; Transcode Functions
391 ;;;; Babel Call
393 ;; Babel Calls are ignored.
396 ;;;; Bold
398 (defun org-e-man-bold (bold contents info)
399 "Transcode BOLD from Org to Man.
400 CONTENTS is the text with bold markup. INFO is a plist holding
401 contextual information."
402 (format "\\fB%s\\fP" contents))
405 ;;;; Center Block
407 (defun org-e-man-center-block (center-block contents info)
408 "Transcode a CENTER-BLOCK element from Org to Man.
409 CONTENTS holds the contents of the center block. INFO is a plist
410 holding contextual information."
411 (org-e-man--wrap-label
412 center-block
413 (format ".ce %d\n.nf\n%s\n.fi"
414 (- (length (split-string contents "\n")) 1)
415 contents)))
418 ;;;; Clock
420 (defun org-e-man-clock (clock contents info)
421 "Transcode a CLOCK element from Org to Man.
422 CONTENTS is nil. INFO is a plist holding contextual
423 information."
427 ;;;; Code
429 (defun org-e-man-code (code contents info)
430 "Transcode a CODE object from Org to Man.
431 CONTENTS is nil. INFO is a plist used as a communication
432 channel."
433 (format "\\fC%s\\fP" code))
436 ;;;; Comment
437 ;; Comments are ignored.
440 ;;;; Comment Block
441 ;; Comment Blocks are ignored.
444 ;;;; Drawer
446 (defun org-e-man-drawer (drawer contents info)
447 "Transcode a DRAWER element from Org to Man.
448 DRAWER holds the drawer information
449 CONTENTS holds the contents of the block.
450 INFO is a plist holding contextual information. "
451 contents)
454 ;;;; Dynamic Block
456 (defun org-e-man-dynamic-block (dynamic-block contents info)
457 "Transcode a DYNAMIC-BLOCK element from Org to Man.
458 CONTENTS holds the contents of the block. INFO is a plist
459 holding contextual information. See `org-export-data'."
460 (org-e-man--wrap-label dynamic-block contents))
463 ;;;; Entity
465 (defun org-e-man-entity (entity contents info)
466 "Transcode an ENTITY object from Org to Man.
467 CONTENTS are the definition itself. INFO is a plist holding
468 contextual information."
469 (let ((ent (org-element-property :utf8 entity))) ent))
472 ;;;; Example Block
474 (defun org-e-man-example-block (example-block contents info)
475 "Transcode an EXAMPLE-BLOCK element from Org to Man.
476 CONTENTS is nil. INFO is a plist holding contextual
477 information."
478 (org-e-man--wrap-label
479 example-block
480 (format ".RS\n.nf\n%s\n.fi\n.RE"
481 (org-export-format-code-default example-block info))))
483 ;;;; Export Block
485 (defun org-e-man-export-block (export-block contents info)
486 "Transcode a EXPORT-BLOCK element from Org to Man.
487 CONTENTS is nil. INFO is a plist holding contextual information."
488 (when (string= (org-element-property :type export-block) "MAN")
489 (org-remove-indentation (org-element-property :value export-block))))
492 ;;;; Export Snippet
494 (defun org-e-man-export-snippet (export-snippet contents info)
495 "Transcode a EXPORT-SNIPPET object from Org to Man.
496 CONTENTS is nil. INFO is a plist holding contextual information."
497 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
498 (org-element-property :value export-snippet)))
501 ;;;; Fixed Width
503 (defun org-e-man-fixed-width (fixed-width contents info)
504 "Transcode a FIXED-WIDTH element from Org to Man.
505 CONTENTS is nil. INFO is a plist holding contextual information."
506 (org-e-man--wrap-label
507 fixed-width
508 (format "\\fC\n%s\\fP"
509 (org-remove-indentation
510 (org-element-property :value fixed-width)))))
513 ;;;; Footnote Definition
514 ;; Footnote Definitions are ignored.
516 ;;;; Footnote References
517 ;; Footnote References are Ignored
520 ;;;; Headline
522 (defun org-e-man-headline (headline contents info)
523 "Transcode an HEADLINE element from Org to Man.
524 CONTENTS holds the contents of the headline. INFO is a plist
525 holding contextual information."
526 (let* ((level (org-export-get-relative-level headline info))
527 (numberedp (org-export-numbered-headline-p headline info))
528 ;; Section formatting will set two placeholders: one for the
529 ;; title and the other for the contents.
530 (section-fmt
531 (case level
532 (1 ".SH \"%s\"\n%s")
533 (2 ".SS \"%s\"\n%s")
534 (3 ".SS \"%s\"\n%s")
535 (t nil)))
536 (text (org-export-data (org-element-property :title headline) info)))
538 (cond
539 ;; Case 1: This is a footnote section: ignore it.
540 ((org-element-property :footnote-section-p headline) nil)
542 ;; Case 2. This is a deep sub-tree: export it as a list item.
543 ;; Also export as items headlines for which no section
544 ;; format has been found.
545 ((or (not section-fmt) (org-export-low-level-p headline info))
546 ;; Build the real contents of the sub-tree.
547 (let ((low-level-body
548 (concat
549 ;; If the headline is the first sibling, start a list.
550 (when (org-export-first-sibling-p headline info)
551 (format "%s\n" ".RS"))
552 ;; Itemize headline
553 ".TP\n.ft I\n" text "\n.ft\n"
554 contents ".RE")))
555 ;; If headline is not the last sibling simply return
556 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
557 ;; blank line.
558 (if (not (org-export-last-sibling-p headline info)) low-level-body
559 (replace-regexp-in-string
560 "[ \t\n]*\\'" ""
561 low-level-body))))
563 ;; Case 3. Standard headline. Export it as a section.
564 (t (format section-fmt text contents)))))
567 ;;;; Horizontal Rule
568 ;; Not supported
571 ;;;; Inline Babel Call
572 ;; Inline Babel Calls are ignored.
575 ;;;; Inline Src Block
577 (defun org-e-man-inline-src-block (inline-src-block contents info)
578 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
579 CONTENTS holds the contents of the item. INFO is a plist holding
580 contextual information."
581 (let* ((code (org-element-property :value inline-src-block)))
582 (cond
583 (org-e-man-source-highlight
584 (let* ((tmpdir (if (featurep 'xemacs)
585 temp-directory
586 temporary-file-directory))
587 (in-file (make-temp-name
588 (expand-file-name "srchilite" tmpdir)))
589 (out-file (make-temp-name
590 (expand-file-name "reshilite" tmpdir)))
591 (org-lang (org-element-property :language inline-src-block))
592 (lst-lang (cadr (assq (intern org-lang)
593 org-e-man-source-highlight-langs)))
595 (cmd (concat (expand-file-name "source-highlight")
596 " -s " lst-lang
597 " -f groff_man"
598 " -i " in-file
599 " -o " out-file)))
601 (if lst-lang
602 (let ((code-block ""))
603 (with-temp-file in-file (insert code))
604 (shell-command cmd)
605 (setq code-block (org-file-contents out-file))
606 (delete-file in-file)
607 (delete-file out-file)
608 code-block)
609 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
610 code))))
612 ;; Do not use a special package: transcode it verbatim.
614 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
615 "\\fP\n.fi\n.RE\n")))))
618 ;;;; Inlinetask
619 ;;;; Italic
621 (defun org-e-man-italic (italic contents info)
622 "Transcode ITALIC from Org to Man.
623 CONTENTS is the text with italic markup. INFO is a plist holding
624 contextual information."
625 (format "\\fI%s\\fP" contents))
628 ;;;; Item
630 (defun org-e-man-item (item contents info)
632 "Transcode an ITEM element from Org to Man.
633 CONTENTS holds the contents of the item. INFO is a plist holding
634 contextual information."
636 (let* ((bullet (org-element-property :bullet item))
637 (type (org-element-property :type (org-element-property :parent item)))
638 (checkbox (case (org-element-property :checkbox item)
639 (on "\\o'\\(sq\\(mu'") ;;
640 (off "\\(sq ") ;;
641 (trans "\\o'\\(sq\\(mi'"))) ;;
643 (tag (let ((tag (org-element-property :tag item)))
644 ;; Check-boxes must belong to the tag.
645 (and tag (format "\\fB%s\\fP"
646 (concat checkbox
647 (org-export-data tag info)))))))
649 (if (and (null tag)
650 (null checkbox))
651 (let* ((bullet (org-trim bullet))
652 (marker (cond ((string= "-" bullet) "\\(em")
653 ((string= "*" bullet) "\\(bu")
654 ((eq type 'ordered)
655 (format "%s " (org-trim bullet)))
656 (t "\\(dg"))))
657 (concat ".IP " marker " 4\n"
658 (org-trim (or contents " "))))
659 ; else
660 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
661 (org-trim (or contents " "))))))
664 ;;;; Keyword
666 (defun org-e-man-keyword (keyword contents info)
667 "Transcode a KEYWORD element from Org to Man.
668 CONTENTS is nil. INFO is a plist holding contextual information."
669 (let ((key (org-element-property :key keyword))
670 (value (org-element-property :value keyword)))
671 (cond
672 ((string= key "MAN") value)
673 ((string= key "INDEX") nil)
674 ;; Invisible targets.
675 ((string= key "TARGET") nil)
676 ((string= key "TOC") nil))))
679 ;;;; Man Environment
681 (defun org-e-man-man-environment (man-environment contents info)
682 "Transcode a MAN-ENVIRONMENT element from Org to Man.
683 CONTENTS is nil. INFO is a plist holding contextual information."
684 (let ((label (org-element-property :name man-environment))
685 (value (org-remove-indentation
686 (org-element-property :value man-environment))))
687 (if (not (org-string-nw-p label)) value
688 ;; Environment is labelled: label must be within the environment
689 ;; (otherwise, a reference pointing to that element will count
690 ;; the section instead).
691 (with-temp-buffer
692 (insert value)
693 (goto-char (point-min))
694 (forward-line)
695 (insert (format "%s\n" label))
696 (buffer-string)))))
699 ;;;; Man Fragment
701 (defun org-e-man-man-fragment (man-fragment contents info)
702 "Transcode a MAN-FRAGMENT object from Org to Man.
703 CONTENTS is nil. INFO is a plist holding contextual information."
704 (org-element-property :value man-fragment))
707 ;;;; Line Break
709 (defun org-e-man-line-break (line-break contents info)
710 "Transcode a LINE-BREAK object from Org to Man.
711 CONTENTS is nil. INFO is a plist holding contextual information."
712 ".br\n")
715 ;;;; Link
717 (defun org-e-man-link (link desc info)
718 "Transcode a LINK object from Org to Man.
720 DESC is the description part of the link, or the empty string.
721 INFO is a plist holding contextual information. See
722 `org-export-data'."
724 (let* ((type (org-element-property :type link))
725 (raw-path (org-element-property :path link))
726 ;; Ensure DESC really exists, or set it to nil.
727 (desc (and (not (string= desc "")) desc))
729 (path (cond
730 ((member type '("http" "https" "ftp" "mailto"))
731 (concat type ":" raw-path))
732 ((string= type "file")
733 (if (file-name-absolute-p raw-path)
734 (concat "file://" (expand-file-name raw-path))
735 (concat "file://" raw-path)))
736 (t raw-path)))
737 protocol)
738 (cond
739 ;; External link with a description part.
740 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
741 ;; External link without a description part.
742 (path (format "\\fI%s\\fP" path))
743 ;; No path, only description. Try to do something useful.
744 (t (format "\\fI%s\\fP" desc)))))
747 ;;;; Paragraph
749 (defun org-e-man-paragraph (paragraph contents info)
750 "Transcode a PARAGRAPH element from Org to Man.
751 CONTENTS is the contents of the paragraph, as a string. INFO is
752 the plist used as a communication channel."
753 (let ((parent (plist-get (nth 1 paragraph) :parent)))
754 (when parent
755 (let ((parent-type (car parent))
756 (fixed-paragraph ""))
757 (cond ((and (eq parent-type 'item)
758 (plist-get (nth 1 parent) :bullet))
759 (setq fixed-paragraph (concat "" contents)))
760 ((eq parent-type 'section)
761 (setq fixed-paragraph (concat ".PP\n" contents)))
762 ((eq parent-type 'footnote-definition)
763 (setq fixed-paragraph contents))
764 (t (setq fixed-paragraph (concat "" contents))))
765 fixed-paragraph))))
768 ;;;; Plain List
770 (defun org-e-man-plain-list (plain-list contents info)
771 "Transcode a PLAIN-LIST element from Org to Man.
772 CONTENTS is the contents of the list. INFO is a plist holding
773 contextual information."
774 contents)
777 ;;;; Plain Text
779 (defun org-e-man-plain-text (text info)
780 "Transcode a TEXT string from Org to Man.
781 TEXT is the string to transcode. INFO is a plist holding
782 contextual information."
783 ;; Protect
784 (setq text (replace-regexp-in-string
785 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
786 "$\\" text nil t 1))
788 ;; Handle quotation marks
789 (setq text (org-e-man--quotation-marks text info))
791 ;; Handle break preservation if required.
793 (when (plist-get info :preserve-breaks)
794 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
795 text)))
796 ;; Return value.
797 text)
800 ;;;; Planning
802 ;;;; Property Drawer
805 ;;;; Quote Block
807 (defun org-e-man-quote-block (quote-block contents info)
808 "Transcode a QUOTE-BLOCK element from Org to Man.
809 CONTENTS holds the contents of the block. INFO is a plist
810 holding contextual information."
811 (org-e-man--wrap-label
812 quote-block
813 (format ".RS\n%s\n.RE" contents)))
816 ;;;; Quote Section
818 (defun org-e-man-quote-section (quote-section contents info)
819 "Transcode a QUOTE-SECTION element from Org to Man.
820 CONTENTS is nil. INFO is a plist holding contextual information."
821 (let ((value (org-remove-indentation
822 (org-element-property :value quote-section))))
823 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
826 ;;;; Radio Target
828 (defun org-e-man-radio-target (radio-target text info)
829 "Transcode a RADIO-TARGET object from Org to Man.
830 TEXT is the text of the target. INFO is a plist holding
831 contextual information."
832 text)
835 ;;;; Section
837 (defun org-e-man-section (section contents info)
838 "Transcode a SECTION element from Org to Man.
839 CONTENTS holds the contents of the section. INFO is a plist
840 holding contextual information."
841 contents)
844 ;;;; Special Block
846 (defun org-e-man-special-block (special-block contents info)
847 "Transcode a SPECIAL-BLOCK element from Org to Man.
848 CONTENTS holds the contents of the block. INFO is a plist
849 holding contextual information."
850 (let ((type (downcase (org-element-property :type special-block))))
851 (org-e-man--wrap-label
852 special-block
853 (format "%s\n" contents))))
856 ;;;; Src Block
858 (defun org-e-man-src-block (src-block contents info)
859 "Transcode a SRC-BLOCK element from Org to Man.
860 CONTENTS holds the contents of the item. INFO is a plist holding
861 contextual information."
863 (let* ((lang (org-element-property :language src-block))
864 (caption (org-element-property :caption src-block))
865 (label (org-element-property :name src-block))
866 (code (org-element-property :value src-block))
867 (custom-env (and lang
868 (cadr (assq (intern lang)
869 org-e-man-custom-lang-environments))))
870 (num-start (case (org-element-property :number-lines src-block)
871 (continued (org-export-get-loc src-block info))
872 (new 0)))
873 (retain-labels (org-element-property :retain-labels src-block)))
874 (cond
875 ;; Case 1. No source fontification.
876 ((not org-e-man-source-highlight)
877 (let ((caption-str (org-e-man--caption/label-string caption label info)))
878 (concat
879 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
880 (org-export-format-code-default src-block info)))))
881 ((and org-e-man-source-highlight)
882 (let* ((tmpdir (if (featurep 'xemacs)
883 temp-directory
884 temporary-file-directory))
886 (in-file (make-temp-name
887 (expand-file-name "srchilite" tmpdir)))
888 (out-file (make-temp-name
889 (expand-file-name "reshilite" tmpdir)))
891 (org-lang (org-element-property :language src-block))
892 (lst-lang (cadr (assq (intern org-lang)
893 org-e-man-source-highlight-langs)))
895 (cmd (concat "source-highlight"
896 " -s " lst-lang
897 " -f groff_man "
898 " -i " in-file
899 " -o " out-file)))
901 (if lst-lang
902 (let ((code-block ""))
903 (with-temp-file in-file (insert code))
904 (shell-command cmd)
905 (setq code-block (org-file-contents out-file))
906 (delete-file in-file)
907 (delete-file out-file)
908 code-block)
909 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE"
910 code)))))))
913 ;;;; Statistics Cookie
915 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
916 "Transcode a STATISTICS-COOKIE object from Org to Man.
917 CONTENTS is nil. INFO is a plist holding contextual information."
918 (org-element-property :value statistics-cookie))
921 ;;;; Strike-Through
923 (defun org-e-man-strike-through (strike-through contents info)
924 "Transcode STRIKE-THROUGH from Org to Man.
925 CONTENTS is the text with strike-through markup. INFO is a plist
926 holding contextual information."
927 (format "\\fI%s\\fP" contents))
930 ;;;; Subscript
932 (defun org-e-man-subscript (subscript contents info)
933 "Transcode a SUBSCRIPT object from Org to Man.
934 CONTENTS is the contents of the object. INFO is a plist holding
935 contextual information."
936 (format "\\d\\s-2%s\\s+2\\u" contents))
939 ;;;; Superscript "^_%s$
941 (defun org-e-man-superscript (superscript contents info)
942 "Transcode a SUPERSCRIPT object from Org to Man.
943 CONTENTS is the contents of the object. INFO is a plist holding
944 contextual information."
945 (format "\\u\\s-2%s\\s+2\\d" contents))
948 ;;;; Table
950 ;; `org-e-man-table' is the entry point for table transcoding. It
951 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
952 ;; delegates the job to either `org-e-man-table--table.el-table' or
953 ;; `org-e-man-table--org-table' functions, depending of the type of
954 ;; the table.
956 ;; `org-e-man-table--align-string' is a subroutine used to build
957 ;; alignment string for Org tables.
959 (defun org-e-man-table (table contents info)
960 "Transcode a TABLE element from Org to Man.
961 CONTENTS is the contents of the table. INFO is a plist holding
962 contextual information."
963 (cond
964 ;; Case 1: verbatim table.
965 ((or org-e-man-tables-verbatim
966 (let ((attr
967 (read
968 (format
969 "(%s)"
970 (mapconcat
971 #'identity
972 (org-element-property :attr_man table)
973 " ")))))
975 (and attr (plist-get attr :verbatim))))
977 (format ".nf\n\\fC%s\\fP\n.fi"
978 ;; Re-create table, without affiliated keywords.
979 (org-trim
980 (org-element-interpret-data
981 `(table nil ,@(org-element-contents table))))))
982 ;; Case 2: Standard table.
983 (t (org-e-man-table--org-table table contents info))))
985 (defun org-e-man-table--align-string (divider table info)
986 "Return an appropriate Man alignment string.
987 TABLE is the considered table. INFO is a plist used as
988 a communication channel."
989 (let (alignment)
990 ;; Extract column groups and alignment from first (non-rule)
991 ;; row.
992 (org-element-map
993 (org-element-map
994 table 'table-row
995 (lambda (row)
996 (and (eq (org-element-property :type row) 'standard) row))
997 info 'first-match)
998 'table-cell
999 (lambda (cell)
1000 (let* ((borders (org-export-table-cell-borders cell info))
1001 (raw-width (org-export-table-cell-width cell info))
1002 (width-cm (when raw-width (/ raw-width 5)))
1003 (width (if raw-width (format "w(%dc)"
1004 (if (< width-cm 1) 1 width-cm)) "")))
1005 ;; Check left border for the first cell only.
1006 (when (and (memq 'left borders) (not alignment))
1007 (push "|" alignment))
1008 (push
1009 (case (org-export-table-cell-alignment cell info)
1010 (left (concat "l" width divider))
1011 (right (concat "r" width divider))
1012 (center (concat "c" width divider)))
1013 alignment)
1014 (when (memq 'right borders) (push "|" alignment))))
1015 info)
1016 (apply 'concat (reverse alignment))))
1018 (defun org-e-man-table--org-table (table contents info)
1019 "Return appropriate Man code for an Org table.
1021 TABLE is the table type element to transcode. CONTENTS is its
1022 contents, as a string. INFO is a plist used as a communication
1023 channel.
1025 This function assumes TABLE has `org' as its `:type' attribute."
1026 (let* ((label (org-element-property :name table))
1027 (caption (org-e-man--caption/label-string
1028 (org-element-property :caption table) label info))
1029 (attr
1030 (read
1031 (format
1032 "(%s)"
1033 (mapconcat
1034 #'identity
1035 (org-element-property :attr_man table)
1036 " "))))
1038 (divider (if (plist-get attr :divider)
1040 " "))
1042 ;; Determine alignment string.
1043 (alignment (org-e-man-table--align-string divider table info))
1044 ;; Extract others display options.
1045 (lines (org-split-string contents "\n"))
1047 (attr-list
1048 (let ((result-list '()))
1049 (dolist (attr-item
1050 (list
1051 (if (plist-get attr :expand)
1052 "expand"
1053 nil)
1055 (case (plist-get attr :placement)
1056 ('center "center")
1057 ('left nil)
1059 (if org-e-man-tables-centered
1060 "center" "")))
1062 (case (plist-get attr :boxtype)
1063 ('box "box")
1064 ('doublebox "doublebox")
1065 ('allbox "allbox")
1066 ('none nil)
1067 (t "box"))))
1069 (if attr-item
1070 (add-to-list 'result-list attr-item)))
1071 result-list))
1074 (title-line (plist-get attr :title-line))
1076 (table-format
1077 (concat
1078 (format "%s"
1079 (or (car attr-list) ""))
1081 (let ((output-list '()))
1082 (when (cdr attr-list)
1083 (dolist (attr-item (cdr attr-list))
1084 (setq output-list (concat output-list (format ",%s" attr-item)))))
1085 output-list)
1086 "")))
1088 (first-line
1089 (when lines (org-split-string (car lines) "\t"))))
1090 ;; Prepare the final format string for the table.
1092 (cond
1093 ;; Others.
1094 (lines (concat ".TS\n " table-format ";\n"
1096 (format "%s.\n"
1097 (let ((final-line ""))
1099 (when title-line
1100 (dotimes (i (length first-line))
1101 (setq final-line (concat final-line "cb" divider))))
1103 (setq final-line (concat final-line "\n"))
1104 (if alignment
1105 (setq final-line (concat final-line alignment))
1106 (dotimes (i (length first-line))
1107 (setq final-line (concat final-line "c" divider))))
1108 final-line))
1110 (format "%s.TE"
1111 (let ((final-line ""))
1112 (dolist (line-item lines)
1113 (cond
1115 (setq lines (org-split-string contents "\n"))
1117 (setq final-line (concat final-line
1118 (car (org-split-string line-item "\\\\")) "\n")))))
1119 final-line)))))))
1122 ;;;; Table Cell
1124 (defun org-e-man-table-cell (table-cell contents info)
1125 "Transcode a TABLE-CELL element from Org to Man
1126 CONTENTS is the cell contents. INFO is a plist used as
1127 a communication channel."
1128 (concat (if (and contents
1129 org-e-man-table-scientific-notation
1130 (string-match orgtbl-exp-regexp contents))
1131 ;; Use appropriate format string for scientific
1132 ;; notation.
1133 (format org-e-man-table-scientific-notation
1134 (match-string 1 contents)
1135 (match-string 2 contents))
1136 contents)
1137 (when (org-export-get-next-element table-cell info) " \t ")))
1140 ;;;; Table Row
1142 (defun org-e-man-table-row (table-row contents info)
1143 "Transcode a TABLE-ROW element from Org to Man
1144 CONTENTS is the contents of the row. INFO is a plist used as
1145 a communication channel."
1146 ;; Rules are ignored since table separators are deduced from
1147 ;; borders of the current row.
1148 (when (eq (org-element-property :type table-row) 'standard)
1149 (let* ((attr (mapconcat 'identity
1150 (org-element-property
1151 :attr_man (org-export-get-parent table-row))
1152 " "))
1153 ;; TABLE-ROW's borders are extracted from its first cell.
1154 (borders
1155 (org-export-table-cell-borders
1156 (car (org-element-contents table-row)) info)))
1157 (concat
1158 ;; Mark "hline" for horizontal lines.
1159 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1160 contents "\\\\\n"
1161 (cond
1162 ;; When BOOKTABS are activated enforce bottom rule even when
1163 ;; no hline was specifically marked.
1164 ((and (memq 'bottom borders) (memq 'below borders)) "_\n")
1165 ((memq 'below borders) "_"))))))
1168 ;;;; Target
1170 (defun org-e-man-target (target contents info)
1171 "Transcode a TARGET object from Org to Man.
1172 CONTENTS is nil. INFO is a plist holding contextual
1173 information."
1174 (format "\\fI%s\\fP"
1175 (org-export-solidify-link-text (org-element-property :value target))))
1178 ;;;; Timestamp
1180 (defun org-e-man-timestamp (timestamp contents info)
1181 "Transcode a TIMESTAMP object from Org to Man.
1182 CONTENTS is nil. INFO is a plist holding contextual
1183 information."
1187 ;;;; Underline
1189 (defun org-e-man-underline (underline contents info)
1190 "Transcode UNDERLINE from Org to Man.
1191 CONTENTS is the text with underline markup. INFO is a plist
1192 holding contextual information."
1193 (format "\\fI%s\\fP" contents))
1196 ;;;; Verbatim
1198 (defun org-e-man-verbatim (verbatim contents info)
1199 "Transcode a VERBATIM object from Org to Man.
1200 CONTENTS is nil. INFO is a plist used as a communication
1201 channel."
1202 (format ".nf\n%s\n.fi" contents))
1205 ;;;; Verse Block
1207 (defun org-e-man-verse-block (verse-block contents info)
1208 "Transcode a VERSE-BLOCK element from Org to Man.
1209 CONTENTS is verse block contents. INFO is a plist holding
1210 contextual information."
1211 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1215 ;;; Interactive functions
1217 (defun org-e-man-export-to-man
1218 (&optional subtreep visible-only body-only ext-plist pub-dir)
1219 "Export current buffer to a Man file.
1221 If narrowing is active in the current buffer, only export its
1222 narrowed part.
1224 If a region is active, export that region.
1226 When optional argument SUBTREEP is non-nil, export the sub-tree
1227 at point, extracting information from the headline properties
1228 first.
1230 When optional argument VISIBLE-ONLY is non-nil, don't export
1231 contents of hidden elements.
1233 When optional argument BODY-ONLY is non-nil, only the body
1234 without any markers.
1236 EXT-PLIST, when provided, is a property list with external
1237 parameters overriding Org default settings, but still inferior to
1238 file-local settings.
1240 When optional argument PUB-DIR is set, use it as the publishing
1241 directory.
1243 Return output file's name."
1244 (interactive)
1245 (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
1246 (org-export-to-file
1247 'e-man outfile subtreep visible-only body-only ext-plist)))
1249 (defun org-e-man-export-to-pdf
1250 (&optional subtreep visible-only body-only ext-plist pub-dir)
1251 "Export current buffer to Groff then process through to PDF.
1253 If narrowing is active in the current buffer, only export its
1254 narrowed part.
1256 If a region is active, export that region.
1258 When optional argument SUBTREEP is non-nil, export the sub-tree
1259 at point, extracting information from the headline properties
1260 first.
1262 When optional argument VISIBLE-ONLY is non-nil, don't export
1263 contents of hidden elements.
1265 When optional argument BODY-ONLY is non-nil, only write between
1266 markers.
1268 EXT-PLIST, when provided, is a property list with external
1269 parameters overriding Org default settings, but still inferior to
1270 file-local settings.
1272 When optional argument PUB-DIR is set, use it as the publishing
1273 directory.
1275 Return PDF file's name."
1276 (interactive)
1277 (org-e-man-compile
1278 (org-e-man-export-to-man
1279 subtreep visible-only body-only ext-plist pub-dir)))
1281 (defun org-e-man-compile (grofffile)
1282 "Compile a Groff file.
1284 GROFFFILE is the name of the file being compiled. Processing is
1285 done through the command specified in `org-e-man-pdf-process'.
1287 Return PDF file name or an error if it couldn't be produced."
1288 (let* ((wconfig (current-window-configuration))
1289 (grofffile (file-truename grofffile))
1290 (base (file-name-sans-extension grofffile))
1291 errors)
1292 (message (format "Processing Groff file %s ..." grofffile))
1293 (unwind-protect
1294 (progn
1295 (cond
1296 ;; A function is provided: Apply it.
1297 ((functionp org-e-man-pdf-process)
1298 (funcall org-e-man-pdf-process (shell-quote-argument grofffile)))
1299 ;; A list is provided: Replace %b, %f and %o with appropriate
1300 ;; values in each command before applying it. Output is
1301 ;; redirected to "*Org PDF Groff Output*" buffer.
1302 ((consp org-e-man-pdf-process)
1303 (let* ((out-dir (or (file-name-directory grofffile) "./"))
1304 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
1305 (mapc
1306 (lambda (command)
1307 (shell-command
1308 (replace-regexp-in-string
1309 "%b" (shell-quote-argument base)
1310 (replace-regexp-in-string
1311 "%f" (shell-quote-argument grofffile)
1312 (replace-regexp-in-string
1313 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1314 outbuf))
1315 org-e-man-pdf-process)
1316 ;; Collect standard errors from output buffer.
1317 (setq errors (org-e-man-collect-errors outbuf))))
1318 (t (error "No valid command to process to PDF")))
1319 (let ((pdffile (concat base ".pdf")))
1320 ;; Check for process failure. Provide collected errors if
1321 ;; possible.
1322 (if (not (file-exists-p pdffile))
1323 (error (concat (format "PDF file %s wasn't produced" pdffile)
1324 (when errors (concat ": " errors))))
1325 ;; Else remove log files, when specified, and signal end of
1326 ;; process to user, along with any error encountered.
1327 (when org-e-man-remove-logfiles
1328 (dolist (ext org-e-man-logfiles-extensions)
1329 (let ((file (concat base "." ext)))
1330 (when (file-exists-p file) (delete-file file)))))
1331 (message (concat "Process completed"
1332 (if (not errors) "."
1333 (concat " with errors: " errors)))))
1334 ;; Return output file name.
1335 pdffile))
1336 (set-window-configuration wconfig))))
1338 (defun org-e-man-collect-errors (buffer)
1339 "Collect some kind of errors from \"groff\" output
1340 BUFFER is the buffer containing output.
1341 Return collected error types as a string, or nil if there was
1342 none."
1343 (with-current-buffer buffer
1344 (save-excursion
1345 (goto-char (point-max))
1346 ;; Find final run
1347 nil)))
1350 (provide 'org-e-man)
1351 ;;; org-e-man.el ends here