org.el: Display images in link descriptions.
[org-mode.git] / lisp / ox-man.el
blob760e264ce73203e91fc1838cad0bf6c60bd36039
1 ;; ox-man.el --- Man 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 ;; Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements a Man back-end for Org generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Man
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'ox)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
46 (defvar orgtbl-exp-regexp)
50 ;;; Define Back-End
52 (org-export-define-backend 'man
53 '((babel-call . org-man-babel-call)
54 (bold . org-man-bold)
55 (center-block . org-man-center-block)
56 (clock . org-man-clock)
57 (code . org-man-code)
58 (comment . (lambda (&rest args) ""))
59 (comment-block . (lambda (&rest args) ""))
60 (drawer . org-man-drawer)
61 (dynamic-block . org-man-dynamic-block)
62 (entity . org-man-entity)
63 (example-block . org-man-example-block)
64 (export-block . org-man-export-block)
65 (export-snippet . org-man-export-snippet)
66 (fixed-width . org-man-fixed-width)
67 (footnote-definition . org-man-footnote-definition)
68 (footnote-reference . org-man-footnote-reference)
69 (headline . org-man-headline)
70 (horizontal-rule . org-man-horizontal-rule)
71 (inline-babel-call . org-man-inline-babel-call)
72 (inline-src-block . org-man-inline-src-block)
73 (inlinetask . org-man-inlinetask)
74 (italic . org-man-italic)
75 (item . org-man-item)
76 (keyword . org-man-keyword)
77 (line-break . org-man-line-break)
78 (link . org-man-link)
79 (node-property . org-man-node-property)
80 (paragraph . org-man-paragraph)
81 (plain-list . org-man-plain-list)
82 (plain-text . org-man-plain-text)
83 (planning . org-man-planning)
84 (property-drawer . org-man-property-drawer)
85 (quote-block . org-man-quote-block)
86 (quote-section . org-man-quote-section)
87 (radio-target . org-man-radio-target)
88 (section . org-man-section)
89 (special-block . org-man-special-block)
90 (src-block . org-man-src-block)
91 (statistics-cookie . org-man-statistics-cookie)
92 (strike-through . org-man-strike-through)
93 (subscript . org-man-subscript)
94 (superscript . org-man-superscript)
95 (table . org-man-table)
96 (table-cell . org-man-table-cell)
97 (table-row . org-man-table-row)
98 (target . org-man-target)
99 (template . org-man-template)
100 (timestamp . org-man-timestamp)
101 (underline . org-man-underline)
102 (verbatim . org-man-verbatim)
103 (verse-block . org-man-verse-block))
104 :export-block "MAN"
105 :menu-entry
106 '(?m "Export to MAN"
107 ((?m "As MAN file" org-man-export-to-man)
108 (?p "As PDF file" org-man-export-to-pdf)
109 (?o "As PDF file and open"
110 (lambda (a s v b)
111 (if a (org-man-export-to-pdf t s v b)
112 (org-open-file (org-man-export-to-pdf nil s v b)))))))
113 :options-alist
114 '((:man-class "MAN_CLASS" nil nil t)
115 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
116 (:man-header-extra "MAN_HEADER" nil nil newline)))
120 ;;; User Configurable Variables
122 (defgroup org-export-man nil
123 "Options for exporting Org mode files to Man."
124 :tag "Org Export Man"
125 :group 'org-export)
127 ;;; Tables
129 (defcustom org-man-tables-centered t
130 "When non-nil, tables are exported in a center environment."
131 :group 'org-export-man
132 :version "24.4"
133 :package-version '(Org . "8.0")
134 :type 'boolean)
136 (defcustom org-man-tables-verbatim nil
137 "When non-nil, tables are exported verbatim."
138 :group 'org-export-man
139 :version "24.4"
140 :package-version '(Org . "8.0")
141 :type 'boolean)
144 (defcustom org-man-table-scientific-notation "%sE%s"
145 "Format string to display numbers in scientific notation.
146 The format should have \"%s\" twice, for mantissa and exponent
147 \(i.e. \"%s\\\\times10^{%s}\").
149 When nil, no transformation is made."
150 :group 'org-export-man
151 :version "24.4"
152 :package-version '(Org . "8.0")
153 :type '(choice
154 (string :tag "Format string")
155 (const :tag "No formatting")))
158 ;;; Inlinetasks
159 ;; Src blocks
161 (defcustom org-man-source-highlight nil
162 "Use GNU source highlight to embellish source blocks "
163 :group 'org-export-man
164 :version "24.4"
165 :package-version '(Org . "8.0")
166 :type 'boolean)
169 (defcustom org-man-source-highlight-langs
170 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
171 (scheme "scheme")
172 (c "c") (cc "cpp") (csharp "csharp") (d "d")
173 (fortran "fortran") (cobol "cobol") (pascal "pascal")
174 (ada "ada") (asm "asm")
175 (perl "perl") (cperl "perl")
176 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
177 (java "java") (javascript "javascript")
178 (tex "latex")
179 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
180 (ocaml "caml") (caml "caml")
181 (sql "sql") (sqlite "sql")
182 (html "html") (css "css") (xml "xml")
183 (bat "bat") (bison "bison") (clipper "clipper")
184 (ldap "ldap") (opa "opa")
185 (php "php") (postscript "postscript") (prolog "prolog")
186 (properties "properties") (makefile "makefile")
187 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
188 "Alist mapping languages to their listing language counterpart.
189 The key is a symbol, the major mode symbol without the \"-mode\".
190 The value is the string that should be inserted as the language
191 parameter for the listings package. If the mode name and the
192 listings name are the same, the language does not need an entry
193 in this list - but it does not hurt if it is present."
194 :group 'org-export-man
195 :version "24.4"
196 :package-version '(Org . "8.0")
197 :type '(repeat
198 (list
199 (symbol :tag "Major mode ")
200 (string :tag "Listings language"))))
204 (defvar org-man-custom-lang-environments nil
205 "Alist mapping languages to language-specific Man environments.
207 It is used during export of src blocks by the listings and
208 man packages. For example,
210 \(setq org-man-custom-lang-environments
211 '\(\(python \"pythoncode\"\)\)\)
213 would have the effect that if org encounters begin_src python
214 during man export."
218 ;;; Compilation
220 (defcustom org-man-pdf-process
221 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
222 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
223 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
225 "Commands to process a Man file to a PDF file.
226 This is a list of strings, each of them will be given to the
227 shell as a command. %f in the command will be replaced by the
228 full file name, %b by the file base name (i.e. without directory
229 and extension parts) and %o by the base directory of the file.
232 By default, Org uses 3 runs of to do the processing.
234 Alternatively, this may be a Lisp function that does the
235 processing. This function should accept the file name as
236 its single argument."
237 :group 'org-export-pdf
238 :group 'org-export-man
239 :version "24.4"
240 :package-version '(Org . "8.0")
241 :type '(choice
242 (repeat :tag "Shell command sequence"
243 (string :tag "Shell command"))
244 (const :tag "2 runs of pdfgroff"
245 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
246 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
247 (const :tag "3 runs of pdfgroff"
248 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
249 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
250 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
251 (function)))
253 (defcustom org-man-logfiles-extensions
254 '("log" "out" "toc")
255 "The list of file extensions to consider as Man logfiles."
256 :group 'org-export-man
257 :version "24.4"
258 :package-version '(Org . "8.0")
259 :type '(repeat (string :tag "Extension")))
261 (defcustom org-man-remove-logfiles t
262 "Non-nil means remove the logfiles produced by PDF production.
263 These are the .aux, .log, .out, and .toc files."
264 :group 'org-export-man
265 :version "24.4"
266 :package-version '(Org . "8.0")
267 :type 'boolean)
271 ;;; Internal Functions
273 (defun org-man--caption/label-string (element info)
274 "Return caption and label Man string for ELEMENT.
276 INFO is a plist holding contextual information. If there's no
277 caption nor label, return the empty string.
279 For non-floats, see `org-man--wrap-label'."
280 (let ((label (org-element-property :label element))
281 (main (org-export-get-caption element))
282 (short (org-export-get-caption element t)))
283 (cond ((and (not main) (not label)) "")
284 ((not main) (format "\\fI%s\\fP" label))
285 ;; Option caption format with short name.
286 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
287 (org-export-data short info)
288 (org-export-data main info)))
289 ;; Standard caption format.
290 (t (format "\\fR%s\\fP" (org-export-data main info))))))
292 (defun org-man--wrap-label (element output)
293 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
294 This function shouldn't be used for floats. See
295 `org-man--caption/label-string'."
296 (let ((label (org-element-property :name element)))
297 (if (or (not output) (not label) (string= output "") (string= label ""))
298 output
299 (concat (format "%s\n.br\n" label) output))))
303 ;;; Template
305 (defun org-man-template (contents info)
306 "Return complete document string after Man conversion.
307 CONTENTS is the transcoded contents string. INFO is a plist
308 holding export options."
309 (let* ((title (org-export-data (plist-get info :title) info))
310 (attr (read (format "(%s)"
311 (mapconcat
312 #'identity
313 (list (plist-get info :man-class-options))
314 " "))))
315 (section-item (plist-get attr :section-id)))
317 (concat
319 (cond
320 ((and title (stringp section-item))
321 (format ".TH \"%s\" \"%s\" \n" title section-item))
322 ((and (string= "" title) (stringp section-item))
323 (format ".TH \"%s\" \"%s\" \n" " " section-item))
324 (title
325 (format ".TH \"%s\" \"1\" \n" title))
327 ".TH \" \" \"1\" "))
328 contents)))
333 ;;; Transcode Functions
335 ;;; Babel Call
337 ;; Babel Calls are ignored.
340 ;;; Bold
342 (defun org-man-bold (bold contents info)
343 "Transcode BOLD from Org to Man.
344 CONTENTS is the text with bold markup. INFO is a plist holding
345 contextual information."
346 (format "\\fB%s\\fP" contents))
349 ;;; Center Block
351 (defun org-man-center-block (center-block contents info)
352 "Transcode a CENTER-BLOCK element from Org to Man.
353 CONTENTS holds the contents of the center block. INFO is a plist
354 holding contextual information."
355 (org-man--wrap-label
356 center-block
357 (format ".ce %d\n.nf\n%s\n.fi"
358 (- (length (split-string contents "\n")) 1 )
359 contents)))
362 ;;; Clock
364 (defun org-man-clock (clock contents info)
365 "Transcode a CLOCK element from Org to Man.
366 CONTENTS is nil. INFO is a plist holding contextual
367 information."
368 "" )
371 ;;; Code
373 (defun org-man-code (code contents info)
374 "Transcode a CODE object from Org to Man.
375 CONTENTS is nil. INFO is a plist used as a communication
376 channel."
377 (format "\\fC%s\\fP" code))
380 ;;; Comment
382 ;; Comments are ignored.
385 ;;; Comment Block
387 ;; Comment Blocks are ignored.
390 ;;; Drawer
392 (defun org-man-drawer (drawer contents info)
393 "Transcode a DRAWER element from Org to Man.
394 DRAWER holds the drawer information
395 CONTENTS holds the contents of the block.
396 INFO is a plist holding contextual information. "
397 contents)
400 ;;; Dynamic Block
402 (defun org-man-dynamic-block (dynamic-block contents info)
403 "Transcode a DYNAMIC-BLOCK element from Org to Man.
404 CONTENTS holds the contents of the block. INFO is a plist
405 holding contextual information. See `org-export-data'."
406 (org-man--wrap-label dynamic-block contents))
409 ;;; Entity
411 (defun org-man-entity (entity contents info)
412 "Transcode an ENTITY object from Org to Man.
413 CONTENTS are the definition itself. INFO is a plist holding
414 contextual information."
415 (org-element-property :utf-8 entity))
418 ;;; Example Block
420 (defun org-man-example-block (example-block contents info)
421 "Transcode an EXAMPLE-BLOCK element from Org to Man.
422 CONTENTS is nil. INFO is a plist holding contextual
423 information."
424 (org-man--wrap-label
425 example-block
426 (format ".RS\n.nf\n%s\n.fi\n.RE"
427 (org-export-format-code-default example-block info))))
430 ;;; Export Block
432 (defun org-man-export-block (export-block contents info)
433 "Transcode a EXPORT-BLOCK element from Org to Man.
434 CONTENTS is nil. INFO is a plist holding contextual information."
435 (when (string= (org-element-property :type export-block) "MAN")
436 (org-remove-indentation (org-element-property :value export-block))))
439 ;;; Export Snippet
441 (defun org-man-export-snippet (export-snippet contents info)
442 "Transcode a EXPORT-SNIPPET object from Org to Man.
443 CONTENTS is nil. INFO is a plist holding contextual information."
444 (when (eq (org-export-snippet-backend export-snippet) 'man)
445 (org-element-property :value export-snippet)))
448 ;;; Fixed Width
450 (defun org-man-fixed-width (fixed-width contents info)
451 "Transcode a FIXED-WIDTH element from Org to Man.
452 CONTENTS is nil. INFO is a plist holding contextual information."
453 (org-man--wrap-label
454 fixed-width
455 (format "\\fC\n%s\\fP"
456 (org-remove-indentation
457 (org-element-property :value fixed-width)))))
460 ;;; Footnote Definition
462 ;; Footnote Definitions are ignored.
464 ;;; Footnote References
466 ;; Footnote References are Ignored
469 ;;; Headline
471 (defun org-man-headline (headline contents info)
472 "Transcode a HEADLINE element from Org to Man.
473 CONTENTS holds the contents of the headline. INFO is a plist
474 holding contextual information."
475 (let* ((level (org-export-get-relative-level headline info))
476 (numberedp (org-export-numbered-headline-p headline info))
477 ;; Section formatting will set two placeholders: one for the
478 ;; title and the other for the contents.
479 (section-fmt
480 (case level
481 (1 ".SH \"%s\"\n%s")
482 (2 ".SS \"%s\"\n%s")
483 (3 ".SS \"%s\"\n%s")
484 (t nil)))
485 (text (org-export-data (org-element-property :title headline) info)))
487 (cond
488 ;; Case 1: This is a footnote section: ignore it.
489 ((org-element-property :footnote-section-p headline) nil)
491 ;; Case 2. This is a deep sub-tree: export it as a list item.
492 ;; Also export as items headlines for which no section
493 ;; format has been found.
494 ((or (not section-fmt) (org-export-low-level-p headline info))
495 ;; Build the real contents of the sub-tree.
496 (let ((low-level-body
497 (concat
498 ;; If the headline is the first sibling, start a list.
499 (when (org-export-first-sibling-p headline info)
500 (format "%s\n" ".RS"))
501 ;; Itemize headline
502 ".TP\n.ft I\n" text "\n.ft\n"
503 contents ".RE")))
504 ;; If headline is not the last sibling simply return
505 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
506 ;; blank line.
507 (if (not (org-export-last-sibling-p headline info)) low-level-body
508 (replace-regexp-in-string
509 "[ \t\n]*\\'" ""
510 low-level-body))))
512 ;; Case 3. Standard headline. Export it as a section.
513 (t (format section-fmt text contents )))))
515 ;;; Horizontal Rule
516 ;; Not supported
518 ;;; Inline Babel Call
520 ;; Inline Babel Calls are ignored.
522 ;;; Inline Src Block
524 (defun org-man-inline-src-block (inline-src-block contents info)
525 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
526 CONTENTS holds the contents of the item. INFO is a plist holding
527 contextual information."
528 (let* ((code (org-element-property :value inline-src-block)))
529 (cond
530 (org-man-source-highlight
531 (let* ((tmpdir (if (featurep 'xemacs)
532 temp-directory
533 temporary-file-directory ))
534 (in-file (make-temp-name
535 (expand-file-name "srchilite" tmpdir)))
536 (out-file (make-temp-name
537 (expand-file-name "reshilite" tmpdir)))
538 (org-lang (org-element-property :language inline-src-block))
539 (lst-lang (cadr (assq (intern org-lang)
540 org-man-source-highlight-langs)))
542 (cmd (concat (expand-file-name "source-highlight")
543 " -s " lst-lang
544 " -f groff_man"
545 " -i " in-file
546 " -o " out-file )))
548 (if lst-lang
549 (let ((code-block "" ))
550 (with-temp-file in-file (insert code))
551 (shell-command cmd)
552 (setq code-block (org-file-contents out-file))
553 (delete-file in-file)
554 (delete-file out-file)
555 code-block)
556 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
557 code))))
559 ;; Do not use a special package: transcode it verbatim.
561 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
562 "\\fP\n.fi\n.RE\n")))))
565 ;;; Inlinetask
566 ;;; Italic
568 (defun org-man-italic (italic contents info)
569 "Transcode ITALIC from Org to Man.
570 CONTENTS is the text with italic markup. INFO is a plist holding
571 contextual information."
572 (format "\\fI%s\\fP" contents))
575 ;;; Item
578 (defun org-man-item (item contents info)
580 "Transcode an ITEM element from Org to Man.
581 CONTENTS holds the contents of the item. INFO is a plist holding
582 contextual information."
584 (let* ((bullet (org-element-property :bullet item))
585 (type (org-element-property :type (org-element-property :parent item)))
586 (checkbox (case (org-element-property :checkbox item)
587 (on "\\o'\\(sq\\(mu'") ;;
588 (off "\\(sq ") ;;
589 (trans "\\o'\\(sq\\(mi'" ))) ;;
591 (tag (let ((tag (org-element-property :tag item)))
592 ;; Check-boxes must belong to the tag.
593 (and tag (format "\\fB%s\\fP"
594 (concat checkbox
595 (org-export-data tag info)))))))
597 (if (and (null tag )
598 (null checkbox))
599 (let* ((bullet (org-trim bullet))
600 (marker (cond ((string= "-" bullet) "\\(em")
601 ((string= "*" bullet) "\\(bu")
602 ((eq type 'ordered)
603 (format "%s " (org-trim bullet)))
604 (t "\\(dg"))))
605 (concat ".IP " marker " 4\n"
606 (org-trim (or contents " " ))))
607 ; else
608 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
609 (org-trim (or contents " " ))))))
611 ;;; Keyword
614 (defun org-man-keyword (keyword contents info)
615 "Transcode a KEYWORD element from Org to Man.
616 CONTENTS is nil. INFO is a plist holding contextual information."
617 (let ((key (org-element-property :key keyword))
618 (value (org-element-property :value keyword)))
619 (cond
620 ((string= key "MAN") value)
621 ((string= key "INDEX") nil)
622 ((string= key "TOC" ) nil))))
625 ;;; Line Break
627 (defun org-man-line-break (line-break contents info)
628 "Transcode a LINE-BREAK object from Org to Man.
629 CONTENTS is nil. INFO is a plist holding contextual information."
630 ".br\n")
633 ;;; Link
636 (defun org-man-link (link desc info)
637 "Transcode a LINK object from Org to Man.
639 DESC is the description part of the link, or the empty string.
640 INFO is a plist holding contextual information. See
641 `org-export-data'."
643 (let* ((type (org-element-property :type link))
644 (raw-path (org-element-property :path link))
645 ;; Ensure DESC really exists, or set it to nil.
646 (desc (and (not (string= desc "")) desc))
648 (path (cond
649 ((member type '("http" "https" "ftp" "mailto"))
650 (concat type ":" raw-path))
651 ((string= type "file")
652 (when (string-match "\\(.+\\)::.+" raw-path)
653 (setq raw-path (match-string 1 raw-path)))
654 (if (file-name-absolute-p raw-path)
655 (concat "file://" (expand-file-name raw-path))
656 (concat "file://" raw-path)))
657 (t raw-path)))
658 protocol)
659 (cond
660 ;; External link with a description part.
661 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
662 ;; External link without a description part.
663 (path (format "\\fI%s\\fP" path))
664 ;; No path, only description. Try to do something useful.
665 (t (format "\\fI%s\\fP" desc)))))
667 ;;;; Node Property
669 (defun org-man-node-property (node-property contents info)
670 "Transcode a NODE-PROPERTY element from Org to Man.
671 CONTENTS is nil. INFO is a plist holding contextual
672 information."
673 (format "%s:%s"
674 (org-element-property :key node-property)
675 (let ((value (org-element-property :value node-property)))
676 (if value (concat " " value) ""))))
678 ;;; Paragraph
680 (defun org-man-paragraph (paragraph contents info)
681 "Transcode a PARAGRAPH element from Org to Man.
682 CONTENTS is the contents of the paragraph, as a string. INFO is
683 the plist used as a communication channel."
684 (let ((parent (plist-get (nth 1 paragraph) :parent)))
685 (when parent
686 (let ((parent-type (car parent))
687 (fixed-paragraph ""))
688 (cond ((and (eq parent-type 'item)
689 (plist-get (nth 1 parent) :bullet ))
690 (setq fixed-paragraph (concat "" contents)))
691 ((eq parent-type 'section)
692 (setq fixed-paragraph (concat ".PP\n" contents)))
693 ((eq parent-type 'footnote-definition)
694 (setq fixed-paragraph contents))
695 (t (setq fixed-paragraph (concat "" contents))))
696 fixed-paragraph ))))
699 ;;; Plain List
701 (defun org-man-plain-list (plain-list contents info)
702 "Transcode a PLAIN-LIST element from Org to Man.
703 CONTENTS is the contents of the list. INFO is a plist holding
704 contextual information."
705 contents)
707 ;;; Plain Text
709 (defun org-man-plain-text (text info)
710 "Transcode a TEXT string from Org to Man.
711 TEXT is the string to transcode. INFO is a plist holding
712 contextual information."
713 (let ((output text))
714 ;; Protect various chars.
715 (setq output (replace-regexp-in-string
716 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
717 "$\\" output nil t 1))
718 ;; Activate smart quotes. Be sure to provide original TEXT string
719 ;; since OUTPUT may have been modified.
720 (when (plist-get info :with-smart-quotes)
721 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
722 ;; Handle break preservation if required.
723 (when (plist-get info :preserve-breaks)
724 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
725 output)))
726 ;; Return value.
727 output))
731 ;;; Planning
734 ;;; Property Drawer
736 (defun org-man-property-drawer (property-drawer contents info)
737 "Transcode a PROPERTY-DRAWER element from Org to Man.
738 CONTENTS holds the contents of the drawer. INFO is a plist
739 holding contextual information."
740 (and (org-string-nw-p contents)
741 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
743 ;;; Quote Block
745 (defun org-man-quote-block (quote-block contents info)
746 "Transcode a QUOTE-BLOCK element from Org to Man.
747 CONTENTS holds the contents of the block. INFO is a plist
748 holding contextual information."
749 (org-man--wrap-label
750 quote-block
751 (format ".RS\n%s\n.RE" contents)))
753 ;;; Quote Section
755 (defun org-man-quote-section (quote-section contents info)
756 "Transcode a QUOTE-SECTION element from Org to Man.
757 CONTENTS is nil. INFO is a plist holding contextual information."
758 (let ((value (org-remove-indentation
759 (org-element-property :value quote-section))))
760 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
763 ;;; Radio Target
765 (defun org-man-radio-target (radio-target text info)
766 "Transcode a RADIO-TARGET object from Org to Man.
767 TEXT is the text of the target. INFO is a plist holding
768 contextual information."
769 text )
772 ;;; Section
774 (defun org-man-section (section contents info)
775 "Transcode a SECTION element from Org to Man.
776 CONTENTS holds the contents of the section. INFO is a plist
777 holding contextual information."
778 contents)
781 ;;; Special Block
783 (defun org-man-special-block (special-block contents info)
784 "Transcode a SPECIAL-BLOCK element from Org to Man.
785 CONTENTS holds the contents of the block. INFO is a plist
786 holding contextual information."
787 (let ((type (downcase (org-element-property :type special-block))))
788 (org-man--wrap-label
789 special-block
790 (format "%s\n" contents))))
793 ;;; Src Block
795 (defun org-man-src-block (src-block contents info)
796 "Transcode a SRC-BLOCK element from Org to Man.
797 CONTENTS holds the contents of the item. INFO is a plist holding
798 contextual information."
799 (let* ((lang (org-element-property :language src-block))
800 (code (org-element-property :value src-block))
801 (custom-env (and lang
802 (cadr (assq (intern lang)
803 org-man-custom-lang-environments))))
804 (num-start (case (org-element-property :number-lines src-block)
805 (continued (org-export-get-loc src-block info))
806 (new 0)))
807 (retain-labels (org-element-property :retain-labels src-block)))
808 (cond
809 ;; Case 1. No source fontification.
810 ((not org-man-source-highlight)
811 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
812 (org-export-format-code-default src-block info)))
813 (org-man-source-highlight
814 (let* ((tmpdir (if (featurep 'xemacs)
815 temp-directory
816 temporary-file-directory ))
818 (in-file (make-temp-name
819 (expand-file-name "srchilite" tmpdir)))
820 (out-file (make-temp-name
821 (expand-file-name "reshilite" tmpdir)))
823 (org-lang (org-element-property :language src-block))
824 (lst-lang (cadr (assq (intern org-lang)
825 org-man-source-highlight-langs)))
827 (cmd (concat "source-highlight"
828 " -s " lst-lang
829 " -f groff_man "
830 " -i " in-file
831 " -o " out-file)))
833 (if lst-lang
834 (let ((code-block ""))
835 (with-temp-file in-file (insert code))
836 (shell-command cmd)
837 (setq code-block (org-file-contents out-file))
838 (delete-file in-file)
839 (delete-file out-file)
840 code-block)
841 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
844 ;;; Statistics Cookie
846 (defun org-man-statistics-cookie (statistics-cookie contents info)
847 "Transcode a STATISTICS-COOKIE object from Org to Man.
848 CONTENTS is nil. INFO is a plist holding contextual information."
849 (org-element-property :value statistics-cookie))
852 ;;; Strike-Through
854 (defun org-man-strike-through (strike-through contents info)
855 "Transcode STRIKE-THROUGH from Org to Man.
856 CONTENTS is the text with strike-through markup. INFO is a plist
857 holding contextual information."
858 (format "\\fI%s\\fP" contents))
860 ;;; Subscript
862 (defun org-man-subscript (subscript contents info)
863 "Transcode a SUBSCRIPT object from Org to Man.
864 CONTENTS is the contents of the object. INFO is a plist holding
865 contextual information."
866 (format "\\d\\s-2%s\\s+2\\u" contents))
868 ;;; Superscript "^_%s$
870 (defun org-man-superscript (superscript contents info)
871 "Transcode a SUPERSCRIPT object from Org to Man.
872 CONTENTS is the contents of the object. INFO is a plist holding
873 contextual information."
874 (format "\\u\\s-2%s\\s+2\\d" contents))
877 ;;; Table
879 ;; `org-man-table' is the entry point for table transcoding. It
880 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
881 ;; delegates the job to either `org-man-table--table.el-table' or
882 ;; `org-man-table--org-table' functions, depending of the type of
883 ;; the table.
885 ;; `org-man-table--align-string' is a subroutine used to build
886 ;; alignment string for Org tables.
888 (defun org-man-table (table contents info)
889 "Transcode a TABLE element from Org to Man.
890 CONTENTS is the contents of the table. INFO is a plist holding
891 contextual information."
892 (cond
893 ;; Case 1: verbatim table.
894 ((or org-man-tables-verbatim
895 (let ((attr (read (format "(%s)"
896 (mapconcat
897 #'identity
898 (org-element-property :attr_man table)
899 " ")))))
901 (and attr (plist-get attr :verbatim))))
903 (format ".nf\n\\fC%s\\fP\n.fi"
904 ;; Re-create table, without affiliated keywords.
905 (org-trim
906 (org-element-interpret-data
907 `(table nil ,@(org-element-contents table))))))
908 ;; Case 2: Standard table.
909 (t (org-man-table--org-table table contents info))))
911 (defun org-man-table--align-string (divider table info)
912 "Return an appropriate Man alignment string.
913 TABLE is the considered table. INFO is a plist used as
914 a communication channel."
915 (let (alignment)
916 ;; Extract column groups and alignment from first (non-rule) row.
917 (org-element-map
918 (org-element-map table 'table-row
919 (lambda (row)
920 (and (eq (org-element-property :type row) 'standard) row))
921 info 'first-match)
922 'table-cell
923 (lambda (cell)
924 (let* ((borders (org-export-table-cell-borders cell info))
925 (raw-width (org-export-table-cell-width cell info))
926 (width-cm (when raw-width (/ raw-width 5)))
927 (width (if raw-width (format "w(%dc)"
928 (if (< width-cm 1) 1 width-cm)) "")))
929 ;; Check left border for the first cell only.
930 (when (and (memq 'left borders) (not alignment))
931 (push "|" alignment))
932 (push
933 (case (org-export-table-cell-alignment cell info)
934 (left (concat "l" width divider))
935 (right (concat "r" width divider))
936 (center (concat "c" width divider)))
937 alignment)
938 (when (memq 'right borders) (push "|" alignment))))
939 info)
940 (apply 'concat (reverse alignment))))
942 (defun org-man-table--org-table (table contents info)
943 "Return appropriate Man code for an Org table.
945 TABLE is the table type element to transcode. CONTENTS is its
946 contents, as a string. INFO is a plist used as a communication
947 channel.
949 This function assumes TABLE has `org' as its `:type' attribute."
950 (let* ((attr (org-export-read-attribute :attr_man table))
951 (label (org-element-property :name table))
952 (caption (and (not (plist-get attr :disable-caption))
953 (org-man--caption/label-string table info)))
954 (divider (if (plist-get attr :divider) "|" " "))
956 ;; Determine alignment string.
957 (alignment (org-man-table--align-string divider table info))
958 ;; Extract others display options.
960 (lines (org-split-string contents "\n"))
962 (attr-list
963 (delq nil
964 (list
965 (and (plist-get attr :expand) "expand")
966 (let ((placement (plist-get attr :placement)))
967 (cond ((string= placement 'center) "center")
968 ((string= placement 'left) nil)
969 (t (if org-man-tables-centered "center" ""))))
970 (or (plist-get attr :boxtype) "box"))))
972 (title-line (plist-get attr :title-line))
973 (long-cells (plist-get attr :long-cells))
975 (table-format (concat
976 (format "%s" (or (car attr-list) "" ))
978 (let ((output-list '()))
979 (when (cdr attr-list)
980 (dolist (attr-item (cdr attr-list))
981 (setq output-list (concat output-list (format ",%s" attr-item)))))
982 output-list)
983 "")))
985 (first-line (when lines (org-split-string (car lines) "\t"))))
986 ;; Prepare the final format string for the table.
989 (cond
990 ;; Others.
991 (lines (concat ".TS\n " table-format ";\n"
993 (format "%s.\n"
994 (let ((final-line ""))
995 (when title-line
996 (dotimes (i (length first-line))
997 (setq final-line (concat final-line "cb" divider))))
999 (setq final-line (concat final-line "\n"))
1001 (if alignment
1002 (setq final-line (concat final-line alignment))
1003 (dotimes (i (length first-line))
1004 (setq final-line (concat final-line "c" divider))))
1005 final-line ))
1007 (format "%s.TE\n"
1008 (let ((final-line "")
1009 (long-line "")
1010 (lines (org-split-string contents "\n")))
1012 (dolist (line-item lines)
1013 (setq long-line "")
1015 (if long-cells
1016 (progn
1017 (if (string= line-item "_")
1018 (setq long-line (format "%s\n" line-item))
1019 ;; else string =
1020 (let ((cell-item-list (org-split-string line-item "\t")))
1021 (dolist (cell-item cell-item-list)
1023 (cond ((eq cell-item (car (last cell-item-list)))
1024 (setq long-line (concat long-line
1025 (format "T{\n%s\nT}\t\n" cell-item ))))
1027 (setq long-line (concat long-line
1028 (format "T{\n%s\nT}\t" cell-item ))))))
1029 long-line))
1030 ;; else long cells
1031 (setq final-line (concat final-line long-line )))
1033 (setq final-line (concat final-line line-item "\n"))))
1034 final-line))
1036 (and caption (format ".TB \"%s\"" caption)))))))
1038 ;;; Table Cell
1040 (defun org-man-table-cell (table-cell contents info)
1041 "Transcode a TABLE-CELL element from Org to Man
1042 CONTENTS is the cell contents. INFO is a plist used as
1043 a communication channel."
1044 (concat (if (and contents
1045 org-man-table-scientific-notation
1046 (string-match orgtbl-exp-regexp contents))
1047 ;; Use appropriate format string for scientific
1048 ;; notation.
1049 (format org-man-table-scientific-notation
1050 (match-string 1 contents)
1051 (match-string 2 contents))
1052 contents )
1053 (when (org-export-get-next-element table-cell info) "\t")))
1056 ;;; Table Row
1058 (defun org-man-table-row (table-row contents info)
1059 "Transcode a TABLE-ROW element from Org to Man
1060 CONTENTS is the contents of the row. INFO is a plist used as
1061 a communication channel."
1062 ;; Rules are ignored since table separators are deduced from
1063 ;; borders of the current row.
1064 (when (eq (org-element-property :type table-row) 'standard)
1065 (let* ((attr (mapconcat 'identity
1066 (org-element-property
1067 :attr_man (org-export-get-parent table-row))
1068 " "))
1069 ;; TABLE-ROW's borders are extracted from its first cell.
1070 (borders
1071 (org-export-table-cell-borders
1072 (car (org-element-contents table-row)) info)))
1073 (concat
1074 ;; Mark horizontal lines
1075 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1076 contents
1078 (cond
1079 ;; When BOOKTABS are activated enforce bottom rule even when
1080 ;; no hline was specifically marked.
1081 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1082 ((memq 'below borders) "\n_"))))))
1085 ;;; Target
1087 (defun org-man-target (target contents info)
1088 "Transcode a TARGET object from Org to Man.
1089 CONTENTS is nil. INFO is a plist holding contextual
1090 information."
1091 (format "\\fI%s\\fP"
1092 (org-export-solidify-link-text (org-element-property :value target))))
1095 ;;; Timestamp
1097 (defun org-man-timestamp (timestamp contents info)
1098 "Transcode a TIMESTAMP object from Org to Man.
1099 CONTENTS is nil. INFO is a plist holding contextual
1100 information."
1101 "" )
1104 ;;; Underline
1106 (defun org-man-underline (underline contents info)
1107 "Transcode UNDERLINE from Org to Man.
1108 CONTENTS is the text with underline markup. INFO is a plist
1109 holding contextual information."
1110 (format "\\fI%s\\fP" contents))
1113 ;;; Verbatim
1115 (defun org-man-verbatim (verbatim contents info)
1116 "Transcode a VERBATIM object from Org to Man.
1117 CONTENTS is nil. INFO is a plist used as a communication
1118 channel."
1119 (format ".nf\n%s\n.fi" contents))
1122 ;;; Verse Block
1124 (defun org-man-verse-block (verse-block contents info)
1125 "Transcode a VERSE-BLOCK element from Org to Man.
1126 CONTENTS is verse block contents. INFO is a plist holding
1127 contextual information."
1128 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1132 ;;; Interactive functions
1134 (defun org-man-export-to-man
1135 (&optional async subtreep visible-only body-only ext-plist)
1136 "Export current buffer to a Man file.
1138 If narrowing is active in the current buffer, only export its
1139 narrowed part.
1141 If a region is active, export that region.
1143 A non-nil optional argument ASYNC means the process should happen
1144 asynchronously. The resulting file should be accessible through
1145 the `org-export-stack' interface.
1147 When optional argument SUBTREEP is non-nil, export the sub-tree
1148 at point, extracting information from the headline properties
1149 first.
1151 When optional argument VISIBLE-ONLY is non-nil, don't export
1152 contents of hidden elements.
1154 When optional argument BODY-ONLY is non-nil, only the body
1155 without any markers.
1157 EXT-PLIST, when provided, is a property list with external
1158 parameters overriding Org default settings, but still inferior to
1159 file-local settings.
1161 Return output file's name."
1162 (interactive)
1163 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1164 (org-export-to-file 'man outfile
1165 async subtreep visible-only body-only ext-plist)))
1167 (defun org-man-export-to-pdf
1168 (&optional async subtreep visible-only body-only ext-plist)
1169 "Export current buffer to Groff then process through to PDF.
1171 If narrowing is active in the current buffer, only export its
1172 narrowed part.
1174 If a region is active, export that region.
1176 A non-nil optional argument ASYNC means the process should happen
1177 asynchronously. The resulting file should be accessible through
1178 the `org-export-stack' interface.
1180 When optional argument SUBTREEP is non-nil, export the sub-tree
1181 at point, extracting information from the headline properties
1182 first.
1184 When optional argument VISIBLE-ONLY is non-nil, don't export
1185 contents of hidden elements.
1187 When optional argument BODY-ONLY is non-nil, only write between
1188 markers.
1190 EXT-PLIST, when provided, is a property list with external
1191 parameters overriding Org default settings, but still inferior to
1192 file-local settings.
1194 Return PDF file's name."
1195 (interactive)
1196 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1197 (org-export-to-file 'man outfile
1198 async subtreep visible-only body-only ext-plist
1199 (lambda (file) (org-latex-compile file)))))
1201 (defun org-man-compile (file)
1202 "Compile a Groff file.
1204 FILE is the name of the file being compiled. Processing is done
1205 through the command specified in `org-man-pdf-process'.
1207 Return PDF file name or an error if it couldn't be produced."
1208 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1209 (full-name (file-truename file))
1210 (out-dir (file-name-directory file))
1211 ;; Properly set working directory for compilation.
1212 (default-directory (if (file-name-absolute-p file)
1213 (file-name-directory full-name)
1214 default-directory))
1215 errors)
1216 (message (format "Processing Groff file %s..." file))
1217 (save-window-excursion
1218 (cond
1219 ;; A function is provided: Apply it.
1220 ((functionp org-man-pdf-process)
1221 (funcall org-man-pdf-process (shell-quote-argument file)))
1222 ;; A list is provided: Replace %b, %f and %o with appropriate
1223 ;; values in each command before applying it. Output is
1224 ;; redirected to "*Org PDF Groff Output*" buffer.
1225 ((consp org-man-pdf-process)
1226 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1227 (mapc
1228 (lambda (command)
1229 (shell-command
1230 (replace-regexp-in-string
1231 "%b" (shell-quote-argument base-name)
1232 (replace-regexp-in-string
1233 "%f" (shell-quote-argument full-name)
1234 (replace-regexp-in-string
1235 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1236 outbuf))
1237 org-man-pdf-process)
1238 ;; Collect standard errors from output buffer.
1239 (setq errors (org-man-collect-errors outbuf))))
1240 (t (error "No valid command to process to PDF")))
1241 (let ((pdffile (concat out-dir base-name ".pdf")))
1242 ;; Check for process failure. Provide collected errors if
1243 ;; possible.
1244 (if (not (file-exists-p pdffile))
1245 (error (concat (format "PDF file %s wasn't produced" pdffile)
1246 (when errors (concat ": " errors))))
1247 ;; Else remove log files, when specified, and signal end of
1248 ;; process to user, along with any error encountered.
1249 (when org-man-remove-logfiles
1250 (dolist (ext org-man-logfiles-extensions)
1251 (let ((file (concat out-dir base-name "." ext)))
1252 (when (file-exists-p file) (delete-file file)))))
1253 (message (concat "Process completed"
1254 (if (not errors) "."
1255 (concat " with errors: " errors)))))
1256 ;; Return output file name.
1257 pdffile))))
1259 (defun org-man-collect-errors (buffer)
1260 "Collect some kind of errors from \"groff\" output
1261 BUFFER is the buffer containing output.
1262 Return collected error types as a string, or nil if there was
1263 none."
1264 (with-current-buffer buffer
1265 (save-excursion
1266 (goto-char (point-max))
1267 ;; Find final run
1268 nil )))
1271 (provide 'ox-man)
1273 ;; Local variables:
1274 ;; generated-autoload-file: "org-loaddefs.el"
1275 ;; End:
1277 ;;; ox-man.el ends here