org-element: Fix docstring
[org-mode.git] / lisp / ox-man.el
blob4f3991bd66342e8c56b7c0e1e33afc2ea8c0a01f
1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 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 (radio-target . org-man-radio-target)
87 (section . org-man-section)
88 (special-block . org-man-special-block)
89 (src-block . org-man-src-block)
90 (statistics-cookie . org-man-statistics-cookie)
91 (strike-through . org-man-strike-through)
92 (subscript . org-man-subscript)
93 (superscript . org-man-superscript)
94 (table . org-man-table)
95 (table-cell . org-man-table-cell)
96 (table-row . org-man-table-row)
97 (target . org-man-target)
98 (template . org-man-template)
99 (timestamp . org-man-timestamp)
100 (underline . org-man-underline)
101 (verbatim . org-man-verbatim)
102 (verse-block . org-man-verse-block))
103 :export-block "MAN"
104 :menu-entry
105 '(?m "Export to MAN"
106 ((?m "As MAN file" org-man-export-to-man)
107 (?p "As PDF file" org-man-export-to-pdf)
108 (?o "As PDF file and open"
109 (lambda (a s v b)
110 (if a (org-man-export-to-pdf t s v b)
111 (org-open-file (org-man-export-to-pdf nil s v b)))))))
112 :options-alist
113 '((:man-class "MAN_CLASS" nil nil t)
114 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
115 (:man-header-extra "MAN_HEADER" nil nil newline)
116 ;; Other variables.
117 (:man-tables-centered nil nil org-man-tables-centered)
118 (:man-tables-verbatim nil nil org-man-tables-verbatim)
119 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
120 (:man-source-highlight nil nil org-man-source-highlight)
121 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
125 ;;; User Configurable Variables
127 (defgroup org-export-man nil
128 "Options for exporting Org mode files to Man."
129 :tag "Org Export Man"
130 :group 'org-export)
132 ;;; Tables
134 (defcustom org-man-tables-centered t
135 "When non-nil, tables are exported in a center environment."
136 :group 'org-export-man
137 :version "24.4"
138 :package-version '(Org . "8.0")
139 :type 'boolean)
141 (defcustom org-man-tables-verbatim nil
142 "When non-nil, tables are exported verbatim."
143 :group 'org-export-man
144 :version "24.4"
145 :package-version '(Org . "8.0")
146 :type 'boolean)
149 (defcustom org-man-table-scientific-notation "%sE%s"
150 "Format string to display numbers in scientific notation.
151 The format should have \"%s\" twice, for mantissa and exponent
152 \(i.e. \"%s\\\\times10^{%s}\").
154 When nil, no transformation is made."
155 :group 'org-export-man
156 :version "24.4"
157 :package-version '(Org . "8.0")
158 :type '(choice
159 (string :tag "Format string")
160 (const :tag "No formatting")))
163 ;;; Inlinetasks
164 ;; Src blocks
166 (defcustom org-man-source-highlight nil
167 "Use GNU source highlight to embellish source blocks "
168 :group 'org-export-man
169 :version "24.4"
170 :package-version '(Org . "8.0")
171 :type 'boolean)
174 (defcustom org-man-source-highlight-langs
175 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
176 (scheme "scheme")
177 (c "c") (cc "cpp") (csharp "csharp") (d "d")
178 (fortran "fortran") (cobol "cobol") (pascal "pascal")
179 (ada "ada") (asm "asm")
180 (perl "perl") (cperl "perl")
181 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
182 (java "java") (javascript "javascript")
183 (tex "latex")
184 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
185 (ocaml "caml") (caml "caml")
186 (sql "sql") (sqlite "sql")
187 (html "html") (css "css") (xml "xml")
188 (bat "bat") (bison "bison") (clipper "clipper")
189 (ldap "ldap") (opa "opa")
190 (php "php") (postscript "postscript") (prolog "prolog")
191 (properties "properties") (makefile "makefile")
192 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
193 "Alist mapping languages to their listing language counterpart.
194 The key is a symbol, the major mode symbol without the \"-mode\".
195 The value is the string that should be inserted as the language
196 parameter for the listings package. If the mode name and the
197 listings name are the same, the language does not need an entry
198 in this list - but it does not hurt if it is present."
199 :group 'org-export-man
200 :version "24.4"
201 :package-version '(Org . "8.0")
202 :type '(repeat
203 (list
204 (symbol :tag "Major mode ")
205 (string :tag "Listings language"))))
209 (defvar org-man-custom-lang-environments nil
210 "Alist mapping languages to language-specific Man environments.
212 It is used during export of src blocks by the listings and
213 man packages. For example,
215 \(setq org-man-custom-lang-environments
216 '\(\(python \"pythoncode\"\)\)\)
218 would have the effect that if org encounters begin_src python
219 during man export."
223 ;;; Compilation
225 (defcustom org-man-pdf-process
226 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
227 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
228 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
230 "Commands to process a Man file to a PDF file.
231 This is a list of strings, each of them will be given to the
232 shell as a command. %f in the command will be replaced by the
233 full file name, %b by the file base name (i.e. without directory
234 and extension parts) and %o by the base directory of the file.
237 By default, Org uses 3 runs of to do the processing.
239 Alternatively, this may be a Lisp function that does the
240 processing. This function should accept the file name as
241 its single argument."
242 :group 'org-export-pdf
243 :group 'org-export-man
244 :version "24.4"
245 :package-version '(Org . "8.0")
246 :type '(choice
247 (repeat :tag "Shell command sequence"
248 (string :tag "Shell command"))
249 (const :tag "2 runs of pdfgroff"
250 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
251 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
252 (const :tag "3 runs of pdfgroff"
253 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
254 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
255 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
256 (function)))
258 (defcustom org-man-logfiles-extensions
259 '("log" "out" "toc")
260 "The list of file extensions to consider as Man logfiles."
261 :group 'org-export-man
262 :version "24.4"
263 :package-version '(Org . "8.0")
264 :type '(repeat (string :tag "Extension")))
266 (defcustom org-man-remove-logfiles t
267 "Non-nil means remove the logfiles produced by PDF production.
268 These are the .aux, .log, .out, and .toc files."
269 :group 'org-export-man
270 :version "24.4"
271 :package-version '(Org . "8.0")
272 :type 'boolean)
276 ;;; Internal Functions
278 (defun org-man--caption/label-string (element info)
279 "Return caption and label Man string for ELEMENT.
281 INFO is a plist holding contextual information. If there's no
282 caption nor label, return the empty string.
284 For non-floats, see `org-man--wrap-label'."
285 (let ((label (org-element-property :label element))
286 (main (org-export-get-caption element))
287 (short (org-export-get-caption element t)))
288 (cond ((and (not main) (not label)) "")
289 ((not main) (format "\\fI%s\\fP" label))
290 ;; Option caption format with short name.
291 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
292 (org-export-data short info)
293 (org-export-data main info)))
294 ;; Standard caption format.
295 (t (format "\\fR%s\\fP" (org-export-data main info))))))
297 (defun org-man--wrap-label (element output)
298 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
299 This function shouldn't be used for floats. See
300 `org-man--caption/label-string'."
301 (let ((label (org-element-property :name element)))
302 (if (or (not output) (not label) (string= output "") (string= label ""))
303 output
304 (concat (format "%s\n.br\n" label) output))))
308 ;;; Template
310 (defun org-man-template (contents info)
311 "Return complete document string after Man conversion.
312 CONTENTS is the transcoded contents string. INFO is a plist
313 holding export options."
314 (let* ((title (org-export-data (plist-get info :title) info))
315 (attr (read (format "(%s)"
316 (mapconcat
317 #'identity
318 (list (plist-get info :man-class-options))
319 " "))))
320 (section-item (plist-get attr :section-id)))
322 (concat
324 (cond
325 ((and title (stringp section-item))
326 (format ".TH \"%s\" \"%s\" \n" title section-item))
327 ((and (string= "" title) (stringp section-item))
328 (format ".TH \"%s\" \"%s\" \n" " " section-item))
329 (title
330 (format ".TH \"%s\" \"1\" \n" title))
332 ".TH \" \" \"1\" "))
333 contents)))
338 ;;; Transcode Functions
340 ;;; Babel Call
342 ;; Babel Calls are ignored.
345 ;;; Bold
347 (defun org-man-bold (bold contents info)
348 "Transcode BOLD from Org to Man.
349 CONTENTS is the text with bold markup. INFO is a plist holding
350 contextual information."
351 (format "\\fB%s\\fP" contents))
354 ;;; Center Block
356 (defun org-man-center-block (center-block contents info)
357 "Transcode a CENTER-BLOCK element from Org to Man.
358 CONTENTS holds the contents of the center block. INFO is a plist
359 holding contextual information."
360 (org-man--wrap-label
361 center-block
362 (format ".ce %d\n.nf\n%s\n.fi"
363 (- (length (split-string contents "\n")) 1 )
364 contents)))
367 ;;; Clock
369 (defun org-man-clock (clock contents info)
370 "Transcode a CLOCK element from Org to Man.
371 CONTENTS is nil. INFO is a plist holding contextual
372 information."
373 "" )
376 ;;; Code
378 (defun org-man-code (code contents info)
379 "Transcode a CODE object from Org to Man.
380 CONTENTS is nil. INFO is a plist used as a communication
381 channel."
382 (format "\\fC%s\\fP" code))
385 ;;; Comment
387 ;; Comments are ignored.
390 ;;; Comment Block
392 ;; Comment Blocks are ignored.
395 ;;; Drawer
397 (defun org-man-drawer (drawer contents info)
398 "Transcode a DRAWER element from Org to Man.
399 DRAWER holds the drawer information
400 CONTENTS holds the contents of the block.
401 INFO is a plist holding contextual information. "
402 contents)
405 ;;; Dynamic Block
407 (defun org-man-dynamic-block (dynamic-block contents info)
408 "Transcode a DYNAMIC-BLOCK element from Org to Man.
409 CONTENTS holds the contents of the block. INFO is a plist
410 holding contextual information. See `org-export-data'."
411 (org-man--wrap-label dynamic-block contents))
414 ;;; Entity
416 (defun org-man-entity (entity contents info)
417 "Transcode an ENTITY object from Org to Man.
418 CONTENTS are the definition itself. INFO is a plist holding
419 contextual information."
420 (org-element-property :utf-8 entity))
423 ;;; Example Block
425 (defun org-man-example-block (example-block contents info)
426 "Transcode an EXAMPLE-BLOCK element from Org to Man.
427 CONTENTS is nil. INFO is a plist holding contextual
428 information."
429 (org-man--wrap-label
430 example-block
431 (format ".RS\n.nf\n%s\n.fi\n.RE"
432 (org-export-format-code-default example-block info))))
435 ;;; Export Block
437 (defun org-man-export-block (export-block contents info)
438 "Transcode a EXPORT-BLOCK element from Org to Man.
439 CONTENTS is nil. INFO is a plist holding contextual information."
440 (when (string= (org-element-property :type export-block) "MAN")
441 (org-remove-indentation (org-element-property :value export-block))))
444 ;;; Export Snippet
446 (defun org-man-export-snippet (export-snippet contents info)
447 "Transcode a EXPORT-SNIPPET object from Org to Man.
448 CONTENTS is nil. INFO is a plist holding contextual information."
449 (when (eq (org-export-snippet-backend export-snippet) 'man)
450 (org-element-property :value export-snippet)))
453 ;;; Fixed Width
455 (defun org-man-fixed-width (fixed-width contents info)
456 "Transcode a FIXED-WIDTH element from Org to Man.
457 CONTENTS is nil. INFO is a plist holding contextual information."
458 (org-man--wrap-label
459 fixed-width
460 (format "\\fC\n%s\\fP"
461 (org-remove-indentation
462 (org-element-property :value fixed-width)))))
465 ;;; Footnote Definition
467 ;; Footnote Definitions are ignored.
469 ;;; Footnote References
471 ;; Footnote References are Ignored
474 ;;; Headline
476 (defun org-man-headline (headline contents info)
477 "Transcode a HEADLINE element from Org to Man.
478 CONTENTS holds the contents of the headline. INFO is a plist
479 holding contextual information."
480 (let* ((level (org-export-get-relative-level headline info))
481 (numberedp (org-export-numbered-headline-p headline info))
482 ;; Section formatting will set two placeholders: one for the
483 ;; title and the other for the contents.
484 (section-fmt
485 (case level
486 (1 ".SH \"%s\"\n%s")
487 (2 ".SS \"%s\"\n%s")
488 (3 ".SS \"%s\"\n%s")
489 (t nil)))
490 (text (org-export-data (org-element-property :title headline) info)))
492 (cond
493 ;; Case 1: This is a footnote section: ignore it.
494 ((org-element-property :footnote-section-p headline) nil)
496 ;; Case 2. This is a deep sub-tree: export it as a list item.
497 ;; Also export as items headlines for which no section
498 ;; format has been found.
499 ((or (not section-fmt) (org-export-low-level-p headline info))
500 ;; Build the real contents of the sub-tree.
501 (let ((low-level-body
502 (concat
503 ;; If the headline is the first sibling, start a list.
504 (when (org-export-first-sibling-p headline info)
505 (format "%s\n" ".RS"))
506 ;; Itemize headline
507 ".TP\n.ft I\n" text "\n.ft\n"
508 contents ".RE")))
509 ;; If headline is not the last sibling simply return
510 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
511 ;; blank line.
512 (if (not (org-export-last-sibling-p headline info)) low-level-body
513 (replace-regexp-in-string
514 "[ \t\n]*\\'" ""
515 low-level-body))))
517 ;; Case 3. Standard headline. Export it as a section.
518 (t (format section-fmt text contents )))))
520 ;;; Horizontal Rule
521 ;; Not supported
523 ;;; Inline Babel Call
525 ;; Inline Babel Calls are ignored.
527 ;;; Inline Src Block
529 (defun org-man-inline-src-block (inline-src-block contents info)
530 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
531 CONTENTS holds the contents of the item. INFO is a plist holding
532 contextual information."
533 (let* ((code (org-element-property :value inline-src-block)))
534 (cond
535 ((plist-get info :man-source-highlight)
536 (let* ((tmpdir (if (featurep 'xemacs)
537 temp-directory
538 temporary-file-directory ))
539 (in-file (make-temp-name
540 (expand-file-name "srchilite" tmpdir)))
541 (out-file (make-temp-name
542 (expand-file-name "reshilite" tmpdir)))
543 (org-lang (org-element-property :language inline-src-block))
544 (lst-lang
545 (cadr (assq (intern org-lang)
546 (plist-get info :man-source-highlight-langs))))
548 (cmd (concat (expand-file-name "source-highlight")
549 " -s " lst-lang
550 " -f groff_man"
551 " -i " in-file
552 " -o " out-file )))
554 (if lst-lang
555 (let ((code-block "" ))
556 (with-temp-file in-file (insert code))
557 (shell-command cmd)
558 (setq code-block (org-file-contents out-file))
559 (delete-file in-file)
560 (delete-file out-file)
561 code-block)
562 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
563 code))))
565 ;; Do not use a special package: transcode it verbatim.
567 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
568 "\\fP\n.fi\n.RE\n")))))
571 ;;; Inlinetask
572 ;;; Italic
574 (defun org-man-italic (italic contents info)
575 "Transcode ITALIC from Org to Man.
576 CONTENTS is the text with italic markup. INFO is a plist holding
577 contextual information."
578 (format "\\fI%s\\fP" contents))
581 ;;; Item
584 (defun org-man-item (item contents info)
586 "Transcode an ITEM element from Org to Man.
587 CONTENTS holds the contents of the item. INFO is a plist holding
588 contextual information."
590 (let* ((bullet (org-element-property :bullet item))
591 (type (org-element-property :type (org-element-property :parent item)))
592 (checkbox (case (org-element-property :checkbox item)
593 (on "\\o'\\(sq\\(mu'") ;;
594 (off "\\(sq ") ;;
595 (trans "\\o'\\(sq\\(mi'" ))) ;;
597 (tag (let ((tag (org-element-property :tag item)))
598 ;; Check-boxes must belong to the tag.
599 (and tag (format "\\fB%s\\fP"
600 (concat checkbox
601 (org-export-data tag info)))))))
603 (if (and (null tag )
604 (null checkbox))
605 (let* ((bullet (org-trim bullet))
606 (marker (cond ((string= "-" bullet) "\\(em")
607 ((string= "*" bullet) "\\(bu")
608 ((eq type 'ordered)
609 (format "%s " (org-trim bullet)))
610 (t "\\(dg"))))
611 (concat ".IP " marker " 4\n"
612 (org-trim (or contents " " ))))
613 ; else
614 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
615 (org-trim (or contents " " ))))))
617 ;;; Keyword
620 (defun org-man-keyword (keyword contents info)
621 "Transcode a KEYWORD element from Org to Man.
622 CONTENTS is nil. INFO is a plist holding contextual information."
623 (let ((key (org-element-property :key keyword))
624 (value (org-element-property :value keyword)))
625 (cond
626 ((string= key "MAN") value)
627 ((string= key "INDEX") nil)
628 ((string= key "TOC" ) nil))))
631 ;;; Line Break
633 (defun org-man-line-break (line-break contents info)
634 "Transcode a LINE-BREAK object from Org to Man.
635 CONTENTS is nil. INFO is a plist holding contextual information."
636 ".br\n")
639 ;;; Link
642 (defun org-man-link (link desc info)
643 "Transcode a LINK object from Org to Man.
645 DESC is the description part of the link, or the empty string.
646 INFO is a plist holding contextual information. See
647 `org-export-data'."
648 (let* ((type (org-element-property :type link))
649 (raw-path (org-element-property :path link))
650 ;; Ensure DESC really exists, or set it to nil.
651 (desc (and (not (string= desc "")) desc))
652 (path (cond
653 ((member type '("http" "https" "ftp" "mailto"))
654 (concat type ":" raw-path))
655 ((and (string= type "file") (file-name-absolute-p raw-path))
656 (concat "file:" raw-path))
657 (t raw-path)))
658 protocol)
659 (cond
660 ;; Link type is handled by a special function.
661 ((org-export-custom-protocol-maybe link desc info))
662 ;; External link with a description part.
663 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
664 ;; External link without a description part.
665 (path (format "\\fI%s\\fP" path))
666 ;; No path, only description. Try to do something useful.
667 (t (format "\\fI%s\\fP" desc)))))
669 ;;;; Node Property
671 (defun org-man-node-property (node-property contents info)
672 "Transcode a NODE-PROPERTY element from Org to Man.
673 CONTENTS is nil. INFO is a plist holding contextual
674 information."
675 (format "%s:%s"
676 (org-element-property :key node-property)
677 (let ((value (org-element-property :value node-property)))
678 (if value (concat " " value) ""))))
680 ;;; Paragraph
682 (defun org-man-paragraph (paragraph contents info)
683 "Transcode a PARAGRAPH element from Org to Man.
684 CONTENTS is the contents of the paragraph, as a string. INFO is
685 the plist used as a communication channel."
686 (let ((parent (plist-get (nth 1 paragraph) :parent)))
687 (when parent
688 (let ((parent-type (car parent))
689 (fixed-paragraph ""))
690 (cond ((and (eq parent-type 'item)
691 (plist-get (nth 1 parent) :bullet ))
692 (setq fixed-paragraph (concat "" contents)))
693 ((eq parent-type 'section)
694 (setq fixed-paragraph (concat ".PP\n" contents)))
695 ((eq parent-type 'footnote-definition)
696 (setq fixed-paragraph contents))
697 (t (setq fixed-paragraph (concat "" contents))))
698 fixed-paragraph ))))
701 ;;; Plain List
703 (defun org-man-plain-list (plain-list contents info)
704 "Transcode a PLAIN-LIST element from Org to Man.
705 CONTENTS is the contents of the list. INFO is a plist holding
706 contextual information."
707 contents)
709 ;;; Plain Text
711 (defun org-man-plain-text (text info)
712 "Transcode a TEXT string from Org to Man.
713 TEXT is the string to transcode. INFO is a plist holding
714 contextual information."
715 (let ((output text))
716 ;; Protect various chars.
717 (setq output (replace-regexp-in-string
718 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
719 "$\\" output nil t 1))
720 ;; Activate smart quotes. Be sure to provide original TEXT string
721 ;; since OUTPUT may have been modified.
722 (when (plist-get info :with-smart-quotes)
723 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
724 ;; Handle break preservation if required.
725 (when (plist-get info :preserve-breaks)
726 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
727 output)))
728 ;; Return value.
729 output))
733 ;;; Planning
736 ;;; Property Drawer
738 (defun org-man-property-drawer (property-drawer contents info)
739 "Transcode a PROPERTY-DRAWER element from Org to Man.
740 CONTENTS holds the contents of the drawer. INFO is a plist
741 holding contextual information."
742 (and (org-string-nw-p contents)
743 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
745 ;;; Quote Block
747 (defun org-man-quote-block (quote-block contents info)
748 "Transcode a QUOTE-BLOCK element from Org to Man.
749 CONTENTS holds the contents of the block. INFO is a plist
750 holding contextual information."
751 (org-man--wrap-label
752 quote-block
753 (format ".RS\n%s\n.RE" contents)))
756 ;;; Radio Target
758 (defun org-man-radio-target (radio-target text info)
759 "Transcode a RADIO-TARGET object from Org to Man.
760 TEXT is the text of the target. INFO is a plist holding
761 contextual information."
762 text )
765 ;;; Section
767 (defun org-man-section (section contents info)
768 "Transcode a SECTION element from Org to Man.
769 CONTENTS holds the contents of the section. INFO is a plist
770 holding contextual information."
771 contents)
774 ;;; Special Block
776 (defun org-man-special-block (special-block contents info)
777 "Transcode a SPECIAL-BLOCK element from Org to Man.
778 CONTENTS holds the contents of the block. INFO is a plist
779 holding contextual information."
780 (let ((type (org-element-property :type special-block)))
781 (org-man--wrap-label
782 special-block
783 (format "%s\n" contents))))
786 ;;; Src Block
788 (defun org-man-src-block (src-block contents info)
789 "Transcode a SRC-BLOCK element from Org to Man.
790 CONTENTS holds the contents of the item. INFO is a plist holding
791 contextual information."
792 (let* ((lang (org-element-property :language src-block))
793 (code (org-element-property :value src-block))
794 (custom-env (and lang
795 (cadr (assq (intern lang)
796 org-man-custom-lang-environments))))
797 (num-start (case (org-element-property :number-lines src-block)
798 (continued (org-export-get-loc src-block info))
799 (new 0)))
800 (retain-labels (org-element-property :retain-labels src-block)))
801 (if (not (plist-get info :man-source-highlight))
802 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
803 (org-export-format-code-default src-block info))
804 (let* ((tmpdir (if (featurep 'xemacs) temp-directory
805 temporary-file-directory))
806 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
807 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
808 (org-lang (org-element-property :language src-block))
809 (lst-lang
810 (cadr (assq (intern org-lang)
811 (plist-get info :man-source-highlight-langs))))
812 (cmd (concat "source-highlight"
813 " -s " lst-lang
814 " -f groff_man "
815 " -i " in-file
816 " -o " out-file)))
817 (if lst-lang
818 (let ((code-block ""))
819 (with-temp-file in-file (insert code))
820 (shell-command cmd)
821 (setq code-block (org-file-contents out-file))
822 (delete-file in-file)
823 (delete-file out-file)
824 code-block)
825 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code))))))
828 ;;; Statistics Cookie
830 (defun org-man-statistics-cookie (statistics-cookie contents info)
831 "Transcode a STATISTICS-COOKIE object from Org to Man.
832 CONTENTS is nil. INFO is a plist holding contextual information."
833 (org-element-property :value statistics-cookie))
836 ;;; Strike-Through
838 (defun org-man-strike-through (strike-through contents info)
839 "Transcode STRIKE-THROUGH from Org to Man.
840 CONTENTS is the text with strike-through markup. INFO is a plist
841 holding contextual information."
842 (format "\\fI%s\\fP" contents))
844 ;;; Subscript
846 (defun org-man-subscript (subscript contents info)
847 "Transcode a SUBSCRIPT object from Org to Man.
848 CONTENTS is the contents of the object. INFO is a plist holding
849 contextual information."
850 (format "\\d\\s-2%s\\s+2\\u" contents))
852 ;;; Superscript "^_%s$
854 (defun org-man-superscript (superscript contents info)
855 "Transcode a SUPERSCRIPT object from Org to Man.
856 CONTENTS is the contents of the object. INFO is a plist holding
857 contextual information."
858 (format "\\u\\s-2%s\\s+2\\d" contents))
861 ;;; Table
863 ;; `org-man-table' is the entry point for table transcoding. It
864 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
865 ;; delegates the job to either `org-man-table--table.el-table' or
866 ;; `org-man-table--org-table' functions, depending of the type of
867 ;; the table.
869 ;; `org-man-table--align-string' is a subroutine used to build
870 ;; alignment string for Org tables.
872 (defun org-man-table (table contents info)
873 "Transcode a TABLE element from Org to Man.
874 CONTENTS is the contents of the table. INFO is a plist holding
875 contextual information."
876 (cond
877 ;; Case 1: verbatim table.
878 ((or (plist-get info :man-tables-verbatim)
879 (let ((attr (read (format "(%s)"
880 (mapconcat
881 #'identity
882 (org-element-property :attr_man table)
883 " ")))))
885 (and attr (plist-get attr :verbatim))))
887 (format ".nf\n\\fC%s\\fP\n.fi"
888 ;; Re-create table, without affiliated keywords.
889 (org-trim
890 (org-element-interpret-data
891 `(table nil ,@(org-element-contents table))))))
892 ;; Case 2: Standard table.
893 (t (org-man-table--org-table table contents info))))
895 (defun org-man-table--align-string (divider table info)
896 "Return an appropriate Man alignment string.
897 TABLE is the considered table. INFO is a plist used as
898 a communication channel."
899 (let (alignment)
900 ;; Extract column groups and alignment from first (non-rule) row.
901 (org-element-map
902 (org-element-map table 'table-row
903 (lambda (row)
904 (and (eq (org-element-property :type row) 'standard) row))
905 info 'first-match)
906 'table-cell
907 (lambda (cell)
908 (let* ((borders (org-export-table-cell-borders cell info))
909 (raw-width (org-export-table-cell-width cell info))
910 (width-cm (when raw-width (/ raw-width 5)))
911 (width (if raw-width (format "w(%dc)"
912 (if (< width-cm 1) 1 width-cm)) "")))
913 ;; Check left border for the first cell only.
914 (when (and (memq 'left borders) (not alignment))
915 (push "|" alignment))
916 (push
917 (case (org-export-table-cell-alignment cell info)
918 (left (concat "l" width divider))
919 (right (concat "r" width divider))
920 (center (concat "c" width divider)))
921 alignment)
922 (when (memq 'right borders) (push "|" alignment))))
923 info)
924 (apply 'concat (reverse alignment))))
926 (defun org-man-table--org-table (table contents info)
927 "Return appropriate Man code for an Org table.
929 TABLE is the table type element to transcode. CONTENTS is its
930 contents, as a string. INFO is a plist used as a communication
931 channel.
933 This function assumes TABLE has `org' as its `:type' attribute."
934 (let* ((attr (org-export-read-attribute :attr_man table))
935 (label (org-element-property :name table))
936 (caption (and (not (plist-get attr :disable-caption))
937 (org-man--caption/label-string table info)))
938 (divider (if (plist-get attr :divider) "|" " "))
940 ;; Determine alignment string.
941 (alignment (org-man-table--align-string divider table info))
942 ;; Extract others display options.
944 (lines (org-split-string contents "\n"))
946 (attr-list
947 (delq nil
948 (list
949 (and (plist-get attr :expand) "expand")
950 (let ((placement (plist-get attr :placement)))
951 (cond ((string= placement 'center) "center")
952 ((string= placement 'left) nil)
953 ((plist-get info :man-tables-centered) "center")
954 (t "")))
955 (or (plist-get attr :boxtype) "box"))))
957 (title-line (plist-get attr :title-line))
958 (long-cells (plist-get attr :long-cells))
960 (table-format (concat
961 (format "%s" (or (car attr-list) "" ))
963 (let ((output-list '()))
964 (when (cdr attr-list)
965 (dolist (attr-item (cdr attr-list))
966 (setq output-list (concat output-list (format ",%s" attr-item)))))
967 output-list)
968 "")))
970 (first-line (when lines (org-split-string (car lines) "\t"))))
971 ;; Prepare the final format string for the table.
974 (cond
975 ;; Others.
976 (lines (concat ".TS\n " table-format ";\n"
978 (format "%s.\n"
979 (let ((final-line ""))
980 (when title-line
981 (dotimes (i (length first-line))
982 (setq final-line (concat final-line "cb" divider))))
984 (setq final-line (concat final-line "\n"))
986 (if alignment
987 (setq final-line (concat final-line alignment))
988 (dotimes (i (length first-line))
989 (setq final-line (concat final-line "c" divider))))
990 final-line ))
992 (format "%s.TE\n"
993 (let ((final-line "")
994 (long-line "")
995 (lines (org-split-string contents "\n")))
997 (dolist (line-item lines)
998 (setq long-line "")
1000 (if long-cells
1001 (progn
1002 (if (string= line-item "_")
1003 (setq long-line (format "%s\n" line-item))
1004 ;; else string =
1005 (let ((cell-item-list (org-split-string line-item "\t")))
1006 (dolist (cell-item cell-item-list)
1008 (cond ((eq cell-item (car (last cell-item-list)))
1009 (setq long-line (concat long-line
1010 (format "T{\n%s\nT}\t\n" cell-item ))))
1012 (setq long-line (concat long-line
1013 (format "T{\n%s\nT}\t" cell-item ))))))
1014 long-line))
1015 ;; else long cells
1016 (setq final-line (concat final-line long-line )))
1018 (setq final-line (concat final-line line-item "\n"))))
1019 final-line))
1021 (and caption (format ".TB \"%s\"" caption)))))))
1023 ;;; Table Cell
1025 (defun org-man-table-cell (table-cell contents info)
1026 "Transcode a TABLE-CELL element from Org to Man
1027 CONTENTS is the cell contents. INFO is a plist used as
1028 a communication channel."
1029 (concat
1030 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
1031 (if (and contents
1032 scientific-format
1033 (string-match orgtbl-exp-regexp contents))
1034 ;; Use appropriate format string for scientific notation.
1035 (format scientific-format
1036 (match-string 1 contents)
1037 (match-string 2 contents))
1038 contents))
1039 (when (org-export-get-next-element table-cell info) "\t")))
1042 ;;; Table Row
1044 (defun org-man-table-row (table-row contents info)
1045 "Transcode a TABLE-ROW element from Org to Man
1046 CONTENTS is the contents of the row. INFO is a plist used as
1047 a communication channel."
1048 ;; Rules are ignored since table separators are deduced from
1049 ;; borders of the current row.
1050 (when (eq (org-element-property :type table-row) 'standard)
1051 (let* ((attr (mapconcat 'identity
1052 (org-element-property
1053 :attr_man (org-export-get-parent table-row))
1054 " "))
1055 ;; TABLE-ROW's borders are extracted from its first cell.
1056 (borders
1057 (org-export-table-cell-borders
1058 (car (org-element-contents table-row)) info)))
1059 (concat
1060 ;; Mark horizontal lines
1061 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1062 contents
1064 (cond
1065 ;; When BOOKTABS are activated enforce bottom rule even when
1066 ;; no hline was specifically marked.
1067 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1068 ((memq 'below borders) "\n_"))))))
1071 ;;; Target
1073 (defun org-man-target (target contents info)
1074 "Transcode a TARGET object from Org to Man.
1075 CONTENTS is nil. INFO is a plist holding contextual
1076 information."
1077 (format "\\fI%s\\fP"
1078 (org-export-solidify-link-text (org-element-property :value target))))
1081 ;;; Timestamp
1083 (defun org-man-timestamp (timestamp contents info)
1084 "Transcode a TIMESTAMP object from Org to Man.
1085 CONTENTS is nil. INFO is a plist holding contextual
1086 information."
1087 "" )
1090 ;;; Underline
1092 (defun org-man-underline (underline contents info)
1093 "Transcode UNDERLINE from Org to Man.
1094 CONTENTS is the text with underline markup. INFO is a plist
1095 holding contextual information."
1096 (format "\\fI%s\\fP" contents))
1099 ;;; Verbatim
1101 (defun org-man-verbatim (verbatim contents info)
1102 "Transcode a VERBATIM object from Org to Man.
1103 CONTENTS is nil. INFO is a plist used as a communication
1104 channel."
1105 (format ".nf\n%s\n.fi" contents))
1108 ;;; Verse Block
1110 (defun org-man-verse-block (verse-block contents info)
1111 "Transcode a VERSE-BLOCK element from Org to Man.
1112 CONTENTS is verse block contents. INFO is a plist holding
1113 contextual information."
1114 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1118 ;;; Interactive functions
1120 (defun org-man-export-to-man
1121 (&optional async subtreep visible-only body-only ext-plist)
1122 "Export current buffer to a Man file.
1124 If narrowing is active in the current buffer, only export its
1125 narrowed part.
1127 If a region is active, export that region.
1129 A non-nil optional argument ASYNC means the process should happen
1130 asynchronously. The resulting file should be accessible through
1131 the `org-export-stack' interface.
1133 When optional argument SUBTREEP is non-nil, export the sub-tree
1134 at point, extracting information from the headline properties
1135 first.
1137 When optional argument VISIBLE-ONLY is non-nil, don't export
1138 contents of hidden elements.
1140 When optional argument BODY-ONLY is non-nil, only the body
1141 without any markers.
1143 EXT-PLIST, when provided, is a property list with external
1144 parameters overriding Org default settings, but still inferior to
1145 file-local settings.
1147 Return output file's name."
1148 (interactive)
1149 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1150 (org-export-to-file 'man outfile
1151 async subtreep visible-only body-only ext-plist)))
1153 (defun org-man-export-to-pdf
1154 (&optional async subtreep visible-only body-only ext-plist)
1155 "Export current buffer to Groff then process through to PDF.
1157 If narrowing is active in the current buffer, only export its
1158 narrowed part.
1160 If a region is active, export that region.
1162 A non-nil optional argument ASYNC means the process should happen
1163 asynchronously. The resulting file should be accessible through
1164 the `org-export-stack' interface.
1166 When optional argument SUBTREEP is non-nil, export the sub-tree
1167 at point, extracting information from the headline properties
1168 first.
1170 When optional argument VISIBLE-ONLY is non-nil, don't export
1171 contents of hidden elements.
1173 When optional argument BODY-ONLY is non-nil, only write between
1174 markers.
1176 EXT-PLIST, when provided, is a property list with external
1177 parameters overriding Org default settings, but still inferior to
1178 file-local settings.
1180 Return PDF file's name."
1181 (interactive)
1182 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1183 (org-export-to-file 'man outfile
1184 async subtreep visible-only body-only ext-plist
1185 (lambda (file) (org-latex-compile file)))))
1187 (defun org-man-compile (file)
1188 "Compile a Groff file.
1190 FILE is the name of the file being compiled. Processing is done
1191 through the command specified in `org-man-pdf-process'.
1193 Return PDF file name or an error if it couldn't be produced."
1194 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1195 (full-name (file-truename file))
1196 (out-dir (file-name-directory file))
1197 ;; Properly set working directory for compilation.
1198 (default-directory (if (file-name-absolute-p file)
1199 (file-name-directory full-name)
1200 default-directory))
1201 errors)
1202 (message (format "Processing Groff file %s..." file))
1203 (save-window-excursion
1204 (cond
1205 ;; A function is provided: Apply it.
1206 ((functionp org-man-pdf-process)
1207 (funcall org-man-pdf-process (shell-quote-argument file)))
1208 ;; A list is provided: Replace %b, %f and %o with appropriate
1209 ;; values in each command before applying it. Output is
1210 ;; redirected to "*Org PDF Groff Output*" buffer.
1211 ((consp org-man-pdf-process)
1212 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1213 (mapc
1214 (lambda (command)
1215 (shell-command
1216 (replace-regexp-in-string
1217 "%b" (shell-quote-argument base-name)
1218 (replace-regexp-in-string
1219 "%f" (shell-quote-argument full-name)
1220 (replace-regexp-in-string
1221 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1222 outbuf))
1223 org-man-pdf-process)
1224 ;; Collect standard errors from output buffer.
1225 (setq errors (org-man-collect-errors outbuf))))
1226 (t (error "No valid command to process to PDF")))
1227 (let ((pdffile (concat out-dir base-name ".pdf")))
1228 ;; Check for process failure. Provide collected errors if
1229 ;; possible.
1230 (if (not (file-exists-p pdffile))
1231 (error (concat (format "PDF file %s wasn't produced" pdffile)
1232 (when errors (concat ": " errors))))
1233 ;; Else remove log files, when specified, and signal end of
1234 ;; process to user, along with any error encountered.
1235 (when org-man-remove-logfiles
1236 (dolist (ext org-man-logfiles-extensions)
1237 (let ((file (concat out-dir base-name "." ext)))
1238 (when (file-exists-p file) (delete-file file)))))
1239 (message (concat "Process completed"
1240 (if (not errors) "."
1241 (concat " with errors: " errors)))))
1242 ;; Return output file name.
1243 pdffile))))
1245 (defun org-man-collect-errors (buffer)
1246 "Collect some kind of errors from \"groff\" output
1247 BUFFER is the buffer containing output.
1248 Return collected error types as a string, or nil if there was
1249 none."
1250 (with-current-buffer buffer
1251 (save-excursion
1252 (goto-char (point-max))
1253 ;; Find final run
1254 nil )))
1257 (provide 'ox-man)
1259 ;; Local variables:
1260 ;; generated-autoload-file: "org-loaddefs.el"
1261 ;; End:
1263 ;;; ox-man.el ends here