Merge branch 'maint'
[org-mode.git] / lisp / ox-man.el
blob136689dad9610b7c0e094fd322b877cf2f384d1d
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-snippet . org-man-export-snippet)
65 (fixed-width . org-man-fixed-width)
66 (footnote-definition . org-man-footnote-definition)
67 (footnote-reference . org-man-footnote-reference)
68 (headline . org-man-headline)
69 (horizontal-rule . org-man-horizontal-rule)
70 (inline-babel-call . org-man-inline-babel-call)
71 (inline-src-block . org-man-inline-src-block)
72 (inlinetask . org-man-inlinetask)
73 (italic . org-man-italic)
74 (item . org-man-item)
75 (keyword . org-man-keyword)
76 (line-break . org-man-line-break)
77 (link . org-man-link)
78 (node-property . org-man-node-property)
79 (paragraph . org-man-paragraph)
80 (plain-list . org-man-plain-list)
81 (plain-text . org-man-plain-text)
82 (planning . org-man-planning)
83 (property-drawer . org-man-property-drawer)
84 (quote-block . org-man-quote-block)
85 (radio-target . org-man-radio-target)
86 (section . org-man-section)
87 (special-block . org-man-special-block)
88 (src-block . org-man-src-block)
89 (statistics-cookie . org-man-statistics-cookie)
90 (strike-through . org-man-strike-through)
91 (subscript . org-man-subscript)
92 (superscript . org-man-superscript)
93 (table . org-man-table)
94 (table-cell . org-man-table-cell)
95 (table-row . org-man-table-row)
96 (target . org-man-target)
97 (template . org-man-template)
98 (timestamp . org-man-timestamp)
99 (underline . org-man-underline)
100 (verbatim . org-man-verbatim)
101 (verse-block . org-man-verse-block))
102 :export-block "MAN"
103 :menu-entry
104 '(?m "Export to MAN"
105 ((?m "As MAN file" org-man-export-to-man)
106 (?p "As PDF file" org-man-export-to-pdf)
107 (?o "As PDF file and open"
108 (lambda (a s v b)
109 (if a (org-man-export-to-pdf t s v b)
110 (org-open-file (org-man-export-to-pdf nil s v b)))))))
111 :options-alist
112 '((:man-class "MAN_CLASS" nil nil t)
113 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
114 (:man-header-extra "MAN_HEADER" nil nil newline)
115 ;; Other variables.
116 (:man-tables-centered nil nil org-man-tables-centered)
117 (:man-tables-verbatim nil nil org-man-tables-verbatim)
118 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
119 (:man-source-highlight nil nil org-man-source-highlight)
120 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)
121 (:man-pdf-process nil nil org-man-pdf-process)
122 (:man-logfiles-extensions nil nil org-man-logfiles-extensions)
123 (:man-remove-logfiles nil nil org-man-remove-logfiles)))
127 ;;; User Configurable Variables
129 (defgroup org-export-man nil
130 "Options for exporting Org mode files to Man."
131 :tag "Org Export Man"
132 :group 'org-export)
134 ;;; Tables
136 (defcustom org-man-tables-centered t
137 "When non-nil, tables are exported in a center environment."
138 :group 'org-export-man
139 :version "24.4"
140 :package-version '(Org . "8.0")
141 :type 'boolean)
143 (defcustom org-man-tables-verbatim nil
144 "When non-nil, tables are exported verbatim."
145 :group 'org-export-man
146 :version "24.4"
147 :package-version '(Org . "8.0")
148 :type 'boolean)
151 (defcustom org-man-table-scientific-notation "%sE%s"
152 "Format string to display numbers in scientific notation.
153 The format should have \"%s\" twice, for mantissa and exponent
154 \(i.e. \"%s\\\\times10^{%s}\").
156 When nil, no transformation is made."
157 :group 'org-export-man
158 :version "24.4"
159 :package-version '(Org . "8.0")
160 :type '(choice
161 (string :tag "Format string")
162 (const :tag "No formatting")))
165 ;;; Inlinetasks
166 ;; Src blocks
168 (defcustom org-man-source-highlight nil
169 "Use GNU source highlight to embellish source blocks "
170 :group 'org-export-man
171 :version "24.4"
172 :package-version '(Org . "8.0")
173 :type 'boolean)
176 (defcustom org-man-source-highlight-langs
177 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
178 (scheme "scheme")
179 (c "c") (cc "cpp") (csharp "csharp") (d "d")
180 (fortran "fortran") (cobol "cobol") (pascal "pascal")
181 (ada "ada") (asm "asm")
182 (perl "perl") (cperl "perl")
183 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
184 (java "java") (javascript "javascript")
185 (tex "latex")
186 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
187 (ocaml "caml") (caml "caml")
188 (sql "sql") (sqlite "sql")
189 (html "html") (css "css") (xml "xml")
190 (bat "bat") (bison "bison") (clipper "clipper")
191 (ldap "ldap") (opa "opa")
192 (php "php") (postscript "postscript") (prolog "prolog")
193 (properties "properties") (makefile "makefile")
194 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
195 "Alist mapping languages to their listing language counterpart.
196 The key is a symbol, the major mode symbol without the \"-mode\".
197 The value is the string that should be inserted as the language
198 parameter for the listings package. If the mode name and the
199 listings name are the same, the language does not need an entry
200 in this list - but it does not hurt if it is present."
201 :group 'org-export-man
202 :version "24.4"
203 :package-version '(Org . "8.0")
204 :type '(repeat
205 (list
206 (symbol :tag "Major mode ")
207 (string :tag "Listings language"))))
211 (defvar org-man-custom-lang-environments nil
212 "Alist mapping languages to language-specific Man environments.
214 It is used during export of src blocks by the listings and
215 man packages. For example,
217 \(setq org-man-custom-lang-environments
218 '\(\(python \"pythoncode\"\)\)\)
220 would have the effect that if org encounters begin_src python
221 during man export."
225 ;;; Compilation
227 (defcustom org-man-pdf-process
228 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
229 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
230 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
232 "Commands to process a Man file to a PDF file.
233 This is a list of strings, each of them will be given to the
234 shell as a command. %f in the command will be replaced by the
235 full file name, %b by the file base name (i.e. without directory
236 and extension parts) and %o by the base directory of the file.
239 By default, Org uses 3 runs of to do the processing.
241 Alternatively, this may be a Lisp function that does the
242 processing. This function should accept the file name as
243 its single argument."
244 :group 'org-export-pdf
245 :group 'org-export-man
246 :version "24.4"
247 :package-version '(Org . "8.0")
248 :type '(choice
249 (repeat :tag "Shell command sequence"
250 (string :tag "Shell command"))
251 (const :tag "2 runs of pdfgroff"
252 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
253 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
254 (const :tag "3 runs of pdfgroff"
255 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
256 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
257 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
258 (function)))
260 (defcustom org-man-logfiles-extensions
261 '("log" "out" "toc")
262 "The list of file extensions to consider as Man logfiles."
263 :group 'org-export-man
264 :version "24.4"
265 :package-version '(Org . "8.0")
266 :type '(repeat (string :tag "Extension")))
268 (defcustom org-man-remove-logfiles t
269 "Non-nil means remove the logfiles produced by PDF production.
270 These are the .aux, .log, .out, and .toc files."
271 :group 'org-export-man
272 :version "24.4"
273 :package-version '(Org . "8.0")
274 :type 'boolean)
278 ;;; Internal Functions
280 (defun org-man--caption/label-string (element info)
281 "Return caption and label Man string for ELEMENT.
283 INFO is a plist holding contextual information. If there's no
284 caption nor label, return the empty string.
286 For non-floats, see `org-man--wrap-label'."
287 (let ((label (org-element-property :label element))
288 (main (org-export-get-caption element))
289 (short (org-export-get-caption element t)))
290 (cond ((and (not main) (not label)) "")
291 ((not main) (format "\\fI%s\\fP" label))
292 ;; Option caption format with short name.
293 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
294 (org-export-data short info)
295 (org-export-data main info)))
296 ;; Standard caption format.
297 (t (format "\\fR%s\\fP" (org-export-data main info))))))
299 (defun org-man--wrap-label (element output)
300 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
301 This function shouldn't be used for floats. See
302 `org-man--caption/label-string'."
303 (let ((label (org-element-property :name element)))
304 (if (or (not output) (not label) (string= output "") (string= label ""))
305 output
306 (concat (format "%s\n.br\n" label) output))))
310 ;;; Template
312 (defun org-man-template (contents info)
313 "Return complete document string after Man conversion.
314 CONTENTS is the transcoded contents string. INFO is a plist
315 holding export options."
316 (let* ((title (org-export-data (plist-get info :title) info))
317 (attr (read (format "(%s)"
318 (mapconcat
319 #'identity
320 (list (plist-get info :man-class-options))
321 " "))))
322 (section-item (plist-get attr :section-id)))
324 (concat
326 (cond
327 ((and title (stringp section-item))
328 (format ".TH \"%s\" \"%s\" \n" title section-item))
329 ((and (string= "" title) (stringp section-item))
330 (format ".TH \"%s\" \"%s\" \n" " " section-item))
331 (title
332 (format ".TH \"%s\" \"1\" \n" title))
334 ".TH \" \" \"1\" "))
335 contents)))
340 ;;; Transcode Functions
342 ;;; Babel Call
344 ;; Babel Calls are ignored.
347 ;;; Bold
349 (defun org-man-bold (bold contents info)
350 "Transcode BOLD from Org to Man.
351 CONTENTS is the text with bold markup. INFO is a plist holding
352 contextual information."
353 (format "\\fB%s\\fP" contents))
356 ;;; Center Block
358 (defun org-man-center-block (center-block contents info)
359 "Transcode a CENTER-BLOCK element from Org to Man.
360 CONTENTS holds the contents of the center block. INFO is a plist
361 holding contextual information."
362 (org-man--wrap-label
363 center-block
364 (format ".ce %d\n.nf\n%s\n.fi"
365 (- (length (split-string contents "\n")) 1 )
366 contents)))
369 ;;; Clock
371 (defun org-man-clock (clock contents info)
372 "Transcode a CLOCK element from Org to Man.
373 CONTENTS is nil. INFO is a plist holding contextual
374 information."
375 "" )
378 ;;; Code
380 (defun org-man-code (code contents info)
381 "Transcode a CODE object from Org to Man.
382 CONTENTS is nil. INFO is a plist used as a communication
383 channel."
384 (format "\\fC%s\\fP" code))
387 ;;; Comment
389 ;; Comments are ignored.
392 ;;; Comment Block
394 ;; Comment Blocks are ignored.
397 ;;; Drawer
399 (defun org-man-drawer (drawer contents info)
400 "Transcode a DRAWER element from Org to Man.
401 DRAWER holds the drawer information
402 CONTENTS holds the contents of the block.
403 INFO is a plist holding contextual information. "
404 contents)
407 ;;; Dynamic Block
409 (defun org-man-dynamic-block (dynamic-block contents info)
410 "Transcode a DYNAMIC-BLOCK element from Org to Man.
411 CONTENTS holds the contents of the block. INFO is a plist
412 holding contextual information. See `org-export-data'."
413 (org-man--wrap-label dynamic-block contents))
416 ;;; Entity
418 (defun org-man-entity (entity contents info)
419 "Transcode an ENTITY object from Org to Man.
420 CONTENTS are the definition itself. INFO is a plist holding
421 contextual information."
422 (org-element-property :utf-8 entity))
425 ;;; Example Block
427 (defun org-man-example-block (example-block contents info)
428 "Transcode an EXAMPLE-BLOCK element from Org to Man.
429 CONTENTS is nil. INFO is a plist holding contextual
430 information."
431 (org-man--wrap-label
432 example-block
433 (format ".RS\n.nf\n%s\n.fi\n.RE"
434 (org-export-format-code-default example-block info))))
437 ;;; Export Snippet
439 (defun org-man-export-snippet (export-snippet contents info)
440 "Transcode a EXPORT-SNIPPET object from Org to Man.
441 CONTENTS is nil. INFO is a plist holding contextual information."
442 (when (eq (org-export-snippet-backend export-snippet) 'man)
443 (org-element-property :value export-snippet)))
446 ;;; Fixed Width
448 (defun org-man-fixed-width (fixed-width contents info)
449 "Transcode a FIXED-WIDTH element from Org to Man.
450 CONTENTS is nil. INFO is a plist holding contextual information."
451 (org-man--wrap-label
452 fixed-width
453 (format "\\fC\n%s\\fP"
454 (org-remove-indentation
455 (org-element-property :value fixed-width)))))
458 ;;; Footnote Definition
460 ;; Footnote Definitions are ignored.
462 ;;; Footnote References
464 ;; Footnote References are Ignored
467 ;;; Headline
469 (defun org-man-headline (headline contents info)
470 "Transcode a HEADLINE element from Org to Man.
471 CONTENTS holds the contents of the headline. INFO is a plist
472 holding contextual information."
473 (let* ((level (org-export-get-relative-level headline info))
474 (numberedp (org-export-numbered-headline-p headline info))
475 ;; Section formatting will set two placeholders: one for the
476 ;; title and the other for the contents.
477 (section-fmt
478 (case level
479 (1 ".SH \"%s\"\n%s")
480 (2 ".SS \"%s\"\n%s")
481 (3 ".SS \"%s\"\n%s")
482 (t nil)))
483 (text (org-export-data (org-element-property :title headline) info)))
485 (cond
486 ;; Case 1: This is a footnote section: ignore it.
487 ((org-element-property :footnote-section-p headline) nil)
489 ;; Case 2. This is a deep sub-tree: export it as a list item.
490 ;; Also export as items headlines for which no section
491 ;; format has been found.
492 ((or (not section-fmt) (org-export-low-level-p headline info))
493 ;; Build the real contents of the sub-tree.
494 (let ((low-level-body
495 (concat
496 ;; If the headline is the first sibling, start a list.
497 (when (org-export-first-sibling-p headline info)
498 (format "%s\n" ".RS"))
499 ;; Itemize headline
500 ".TP\n.ft I\n" text "\n.ft\n"
501 contents ".RE")))
502 ;; If headline is not the last sibling simply return
503 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
504 ;; blank line.
505 (if (not (org-export-last-sibling-p headline info)) low-level-body
506 (replace-regexp-in-string
507 "[ \t\n]*\\'" ""
508 low-level-body))))
510 ;; Case 3. Standard headline. Export it as a section.
511 (t (format section-fmt text contents )))))
513 ;;; Horizontal Rule
514 ;; Not supported
516 ;;; Inline Babel Call
518 ;; Inline Babel Calls are ignored.
520 ;;; Inline Src Block
522 (defun org-man-inline-src-block (inline-src-block contents info)
523 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
524 CONTENTS holds the contents of the item. INFO is a plist holding
525 contextual information."
526 (let* ((code (org-element-property :value inline-src-block)))
527 (cond
528 (org-man-source-highlight
529 (let* ((tmpdir (if (featurep 'xemacs)
530 temp-directory
531 temporary-file-directory ))
532 (in-file (make-temp-name
533 (expand-file-name "srchilite" tmpdir)))
534 (out-file (make-temp-name
535 (expand-file-name "reshilite" tmpdir)))
536 (org-lang (org-element-property :language inline-src-block))
537 (lst-lang (cadr (assq (intern org-lang)
538 org-man-source-highlight-langs)))
540 (cmd (concat (expand-file-name "source-highlight")
541 " -s " lst-lang
542 " -f groff_man"
543 " -i " in-file
544 " -o " out-file )))
546 (if lst-lang
547 (let ((code-block "" ))
548 (with-temp-file in-file (insert code))
549 (shell-command cmd)
550 (setq code-block (org-file-contents out-file))
551 (delete-file in-file)
552 (delete-file out-file)
553 code-block)
554 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
555 code))))
557 ;; Do not use a special package: transcode it verbatim.
559 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
560 "\\fP\n.fi\n.RE\n")))))
563 ;;; Inlinetask
564 ;;; Italic
566 (defun org-man-italic (italic contents info)
567 "Transcode ITALIC from Org to Man.
568 CONTENTS is the text with italic markup. INFO is a plist holding
569 contextual information."
570 (format "\\fI%s\\fP" contents))
573 ;;; Item
576 (defun org-man-item (item contents info)
578 "Transcode an ITEM element from Org to Man.
579 CONTENTS holds the contents of the item. INFO is a plist holding
580 contextual information."
582 (let* ((bullet (org-element-property :bullet item))
583 (type (org-element-property :type (org-element-property :parent item)))
584 (checkbox (case (org-element-property :checkbox item)
585 (on "\\o'\\(sq\\(mu'") ;;
586 (off "\\(sq ") ;;
587 (trans "\\o'\\(sq\\(mi'" ))) ;;
589 (tag (let ((tag (org-element-property :tag item)))
590 ;; Check-boxes must belong to the tag.
591 (and tag (format "\\fB%s\\fP"
592 (concat checkbox
593 (org-export-data tag info)))))))
595 (if (and (null tag )
596 (null checkbox))
597 (let* ((bullet (org-trim bullet))
598 (marker (cond ((string= "-" bullet) "\\(em")
599 ((string= "*" bullet) "\\(bu")
600 ((eq type 'ordered)
601 (format "%s " (org-trim bullet)))
602 (t "\\(dg"))))
603 (concat ".IP " marker " 4\n"
604 (org-trim (or contents " " ))))
605 ; else
606 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
607 (org-trim (or contents " " ))))))
609 ;;; Keyword
612 (defun org-man-keyword (keyword contents info)
613 "Transcode a KEYWORD element from Org to Man.
614 CONTENTS is nil. INFO is a plist holding contextual information."
615 (let ((key (org-element-property :key keyword))
616 (value (org-element-property :value keyword)))
617 (cond
618 ((string= key "MAN") value)
619 ((string= key "INDEX") nil)
620 ((string= key "TOC" ) nil))))
623 ;;; Line Break
625 (defun org-man-line-break (line-break contents info)
626 "Transcode a LINE-BREAK object from Org to Man.
627 CONTENTS is nil. INFO is a plist holding contextual information."
628 ".br\n")
631 ;;; Link
634 (defun org-man-link (link desc info)
635 "Transcode a LINK object from Org to Man.
637 DESC is the description part of the link, or the empty string.
638 INFO is a plist holding contextual information. See
639 `org-export-data'."
640 (let* ((type (org-element-property :type link))
641 (raw-path (org-element-property :path link))
642 ;; Ensure DESC really exists, or set it to nil.
643 (desc (and (not (string= desc "")) desc))
644 (path (cond
645 ((member type '("http" "https" "ftp" "mailto"))
646 (concat type ":" raw-path))
647 ((and (string= type "file") (file-name-absolute-p raw-path))
648 (concat "file:" raw-path))
649 (t raw-path)))
650 protocol)
651 (cond
652 ;; External link with a description part.
653 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
654 ;; External link without a description part.
655 (path (format "\\fI%s\\fP" path))
656 ;; No path, only description. Try to do something useful.
657 (t (format "\\fI%s\\fP" desc)))))
659 ;;;; Node Property
661 (defun org-man-node-property (node-property contents info)
662 "Transcode a NODE-PROPERTY element from Org to Man.
663 CONTENTS is nil. INFO is a plist holding contextual
664 information."
665 (format "%s:%s"
666 (org-element-property :key node-property)
667 (let ((value (org-element-property :value node-property)))
668 (if value (concat " " value) ""))))
670 ;;; Paragraph
672 (defun org-man-paragraph (paragraph contents info)
673 "Transcode a PARAGRAPH element from Org to Man.
674 CONTENTS is the contents of the paragraph, as a string. INFO is
675 the plist used as a communication channel."
676 (let ((parent (plist-get (nth 1 paragraph) :parent)))
677 (when parent
678 (let ((parent-type (car parent))
679 (fixed-paragraph ""))
680 (cond ((and (eq parent-type 'item)
681 (plist-get (nth 1 parent) :bullet ))
682 (setq fixed-paragraph (concat "" contents)))
683 ((eq parent-type 'section)
684 (setq fixed-paragraph (concat ".PP\n" contents)))
685 ((eq parent-type 'footnote-definition)
686 (setq fixed-paragraph contents))
687 (t (setq fixed-paragraph (concat "" contents))))
688 fixed-paragraph ))))
691 ;;; Plain List
693 (defun org-man-plain-list (plain-list contents info)
694 "Transcode a PLAIN-LIST element from Org to Man.
695 CONTENTS is the contents of the list. INFO is a plist holding
696 contextual information."
697 contents)
699 ;;; Plain Text
701 (defun org-man-plain-text (text info)
702 "Transcode a TEXT string from Org to Man.
703 TEXT is the string to transcode. INFO is a plist holding
704 contextual information."
705 (let ((output text))
706 ;; Protect various chars.
707 (setq output (replace-regexp-in-string
708 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
709 "$\\" output nil t 1))
710 ;; Activate smart quotes. Be sure to provide original TEXT string
711 ;; since OUTPUT may have been modified.
712 (when (plist-get info :with-smart-quotes)
713 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
714 ;; Handle break preservation if required.
715 (when (plist-get info :preserve-breaks)
716 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
717 output)))
718 ;; Return value.
719 output))
723 ;;; Planning
726 ;;; Property Drawer
728 (defun org-man-property-drawer (property-drawer contents info)
729 "Transcode a PROPERTY-DRAWER element from Org to Man.
730 CONTENTS holds the contents of the drawer. INFO is a plist
731 holding contextual information."
732 (and (org-string-nw-p contents)
733 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
735 ;;; Quote Block
737 (defun org-man-quote-block (quote-block contents info)
738 "Transcode a QUOTE-BLOCK element from Org to Man.
739 CONTENTS holds the contents of the block. INFO is a plist
740 holding contextual information."
741 (org-man--wrap-label
742 quote-block
743 (format ".RS\n%s\n.RE" contents)))
746 ;;; Radio Target
748 (defun org-man-radio-target (radio-target text info)
749 "Transcode a RADIO-TARGET object from Org to Man.
750 TEXT is the text of the target. INFO is a plist holding
751 contextual information."
752 text )
755 ;;; Section
757 (defun org-man-section (section contents info)
758 "Transcode a SECTION element from Org to Man.
759 CONTENTS holds the contents of the section. INFO is a plist
760 holding contextual information."
761 contents)
764 ;;; Special Block
766 (defun org-man-special-block (special-block contents info)
767 "Transcode a SPECIAL-BLOCK element from Org to Man.
768 CONTENTS holds the contents of the block. INFO is a plist
769 holding contextual information."
770 (if (org-export-raw-special-block-p special-block info)
771 (org-remove-indentation (org-element-property :raw-value special-block))
772 (let ((type (downcase (org-element-property :type special-block))))
773 (org-man--wrap-label special-block (format "%s\n" contents)))))
776 ;;; Src Block
778 (defun org-man-src-block (src-block contents info)
779 "Transcode a SRC-BLOCK element from Org to Man.
780 CONTENTS holds the contents of the item. INFO is a plist holding
781 contextual information."
782 (let* ((lang (org-element-property :language src-block))
783 (code (org-element-property :value src-block))
784 (custom-env (and lang
785 (cadr (assq (intern lang)
786 org-man-custom-lang-environments))))
787 (num-start (case (org-element-property :number-lines src-block)
788 (continued (org-export-get-loc src-block info))
789 (new 0)))
790 (retain-labels (org-element-property :retain-labels src-block)))
791 (cond
792 ;; Case 1. No source fontification.
793 ((not org-man-source-highlight)
794 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
795 (org-export-format-code-default src-block info)))
796 (org-man-source-highlight
797 (let* ((tmpdir (if (featurep 'xemacs)
798 temp-directory
799 temporary-file-directory ))
801 (in-file (make-temp-name
802 (expand-file-name "srchilite" tmpdir)))
803 (out-file (make-temp-name
804 (expand-file-name "reshilite" tmpdir)))
806 (org-lang (org-element-property :language src-block))
807 (lst-lang (cadr (assq (intern org-lang)
808 org-man-source-highlight-langs)))
810 (cmd (concat "source-highlight"
811 " -s " lst-lang
812 " -f groff_man "
813 " -i " in-file
814 " -o " out-file)))
816 (if lst-lang
817 (let ((code-block ""))
818 (with-temp-file in-file (insert code))
819 (shell-command cmd)
820 (setq code-block (org-file-contents out-file))
821 (delete-file in-file)
822 (delete-file out-file)
823 code-block)
824 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
827 ;;; Statistics Cookie
829 (defun org-man-statistics-cookie (statistics-cookie contents info)
830 "Transcode a STATISTICS-COOKIE object from Org to Man.
831 CONTENTS is nil. INFO is a plist holding contextual information."
832 (org-element-property :value statistics-cookie))
835 ;;; Strike-Through
837 (defun org-man-strike-through (strike-through contents info)
838 "Transcode STRIKE-THROUGH from Org to Man.
839 CONTENTS is the text with strike-through markup. INFO is a plist
840 holding contextual information."
841 (format "\\fI%s\\fP" contents))
843 ;;; Subscript
845 (defun org-man-subscript (subscript contents info)
846 "Transcode a SUBSCRIPT object from Org to Man.
847 CONTENTS is the contents of the object. INFO is a plist holding
848 contextual information."
849 (format "\\d\\s-2%s\\s+2\\u" contents))
851 ;;; Superscript "^_%s$
853 (defun org-man-superscript (superscript contents info)
854 "Transcode a SUPERSCRIPT object from Org to Man.
855 CONTENTS is the contents of the object. INFO is a plist holding
856 contextual information."
857 (format "\\u\\s-2%s\\s+2\\d" contents))
860 ;;; Table
862 ;; `org-man-table' is the entry point for table transcoding. It
863 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
864 ;; delegates the job to either `org-man-table--table.el-table' or
865 ;; `org-man-table--org-table' functions, depending of the type of
866 ;; the table.
868 ;; `org-man-table--align-string' is a subroutine used to build
869 ;; alignment string for Org tables.
871 (defun org-man-table (table contents info)
872 "Transcode a TABLE element from Org to Man.
873 CONTENTS is the contents of the table. INFO is a plist holding
874 contextual information."
875 (cond
876 ;; Case 1: verbatim table.
877 ((or org-man-tables-verbatim
878 (let ((attr (read (format "(%s)"
879 (mapconcat
880 #'identity
881 (org-element-property :attr_man table)
882 " ")))))
884 (and attr (plist-get attr :verbatim))))
886 (format ".nf\n\\fC%s\\fP\n.fi"
887 ;; Re-create table, without affiliated keywords.
888 (org-trim
889 (org-element-interpret-data
890 `(table nil ,@(org-element-contents table))))))
891 ;; Case 2: Standard table.
892 (t (org-man-table--org-table table contents info))))
894 (defun org-man-table--align-string (divider table info)
895 "Return an appropriate Man alignment string.
896 TABLE is the considered table. INFO is a plist used as
897 a communication channel."
898 (let (alignment)
899 ;; Extract column groups and alignment from first (non-rule) row.
900 (org-element-map
901 (org-element-map table 'table-row
902 (lambda (row)
903 (and (eq (org-element-property :type row) 'standard) row))
904 info 'first-match)
905 'table-cell
906 (lambda (cell)
907 (let* ((borders (org-export-table-cell-borders cell info))
908 (raw-width (org-export-table-cell-width cell info))
909 (width-cm (when raw-width (/ raw-width 5)))
910 (width (if raw-width (format "w(%dc)"
911 (if (< width-cm 1) 1 width-cm)) "")))
912 ;; Check left border for the first cell only.
913 (when (and (memq 'left borders) (not alignment))
914 (push "|" alignment))
915 (push
916 (case (org-export-table-cell-alignment cell info)
917 (left (concat "l" width divider))
918 (right (concat "r" width divider))
919 (center (concat "c" width divider)))
920 alignment)
921 (when (memq 'right borders) (push "|" alignment))))
922 info)
923 (apply 'concat (reverse alignment))))
925 (defun org-man-table--org-table (table contents info)
926 "Return appropriate Man code for an Org table.
928 TABLE is the table type element to transcode. CONTENTS is its
929 contents, as a string. INFO is a plist used as a communication
930 channel.
932 This function assumes TABLE has `org' as its `:type' attribute."
933 (let* ((attr (org-export-read-attribute :attr_man table))
934 (label (org-element-property :name table))
935 (caption (and (not (plist-get attr :disable-caption))
936 (org-man--caption/label-string table info)))
937 (divider (if (plist-get attr :divider) "|" " "))
939 ;; Determine alignment string.
940 (alignment (org-man-table--align-string divider table info))
941 ;; Extract others display options.
943 (lines (org-split-string contents "\n"))
945 (attr-list
946 (delq nil
947 (list
948 (and (plist-get attr :expand) "expand")
949 (let ((placement (plist-get attr :placement)))
950 (cond ((string= placement 'center) "center")
951 ((string= placement 'left) nil)
952 (t (if org-man-tables-centered "center" ""))))
953 (or (plist-get attr :boxtype) "box"))))
955 (title-line (plist-get attr :title-line))
956 (long-cells (plist-get attr :long-cells))
958 (table-format (concat
959 (format "%s" (or (car attr-list) "" ))
961 (let ((output-list '()))
962 (when (cdr attr-list)
963 (dolist (attr-item (cdr attr-list))
964 (setq output-list (concat output-list (format ",%s" attr-item)))))
965 output-list)
966 "")))
968 (first-line (when lines (org-split-string (car lines) "\t"))))
969 ;; Prepare the final format string for the table.
972 (cond
973 ;; Others.
974 (lines (concat ".TS\n " table-format ";\n"
976 (format "%s.\n"
977 (let ((final-line ""))
978 (when title-line
979 (dotimes (i (length first-line))
980 (setq final-line (concat final-line "cb" divider))))
982 (setq final-line (concat final-line "\n"))
984 (if alignment
985 (setq final-line (concat final-line alignment))
986 (dotimes (i (length first-line))
987 (setq final-line (concat final-line "c" divider))))
988 final-line ))
990 (format "%s.TE\n"
991 (let ((final-line "")
992 (long-line "")
993 (lines (org-split-string contents "\n")))
995 (dolist (line-item lines)
996 (setq long-line "")
998 (if long-cells
999 (progn
1000 (if (string= line-item "_")
1001 (setq long-line (format "%s\n" line-item))
1002 ;; else string =
1003 (let ((cell-item-list (org-split-string line-item "\t")))
1004 (dolist (cell-item cell-item-list)
1006 (cond ((eq cell-item (car (last cell-item-list)))
1007 (setq long-line (concat long-line
1008 (format "T{\n%s\nT}\t\n" cell-item ))))
1010 (setq long-line (concat long-line
1011 (format "T{\n%s\nT}\t" cell-item ))))))
1012 long-line))
1013 ;; else long cells
1014 (setq final-line (concat final-line long-line )))
1016 (setq final-line (concat final-line line-item "\n"))))
1017 final-line))
1019 (and caption (format ".TB \"%s\"" caption)))))))
1021 ;;; Table Cell
1023 (defun org-man-table-cell (table-cell contents info)
1024 "Transcode a TABLE-CELL element from Org to Man
1025 CONTENTS is the cell contents. INFO is a plist used as
1026 a communication channel."
1027 (concat (if (and contents
1028 org-man-table-scientific-notation
1029 (string-match orgtbl-exp-regexp contents))
1030 ;; Use appropriate format string for scientific
1031 ;; notation.
1032 (format org-man-table-scientific-notation
1033 (match-string 1 contents)
1034 (match-string 2 contents))
1035 contents )
1036 (when (org-export-get-next-element table-cell info) "\t")))
1039 ;;; Table Row
1041 (defun org-man-table-row (table-row contents info)
1042 "Transcode a TABLE-ROW element from Org to Man
1043 CONTENTS is the contents of the row. INFO is a plist used as
1044 a communication channel."
1045 ;; Rules are ignored since table separators are deduced from
1046 ;; borders of the current row.
1047 (when (eq (org-element-property :type table-row) 'standard)
1048 (let* ((attr (mapconcat 'identity
1049 (org-element-property
1050 :attr_man (org-export-get-parent table-row))
1051 " "))
1052 ;; TABLE-ROW's borders are extracted from its first cell.
1053 (borders
1054 (org-export-table-cell-borders
1055 (car (org-element-contents table-row)) info)))
1056 (concat
1057 ;; Mark horizontal lines
1058 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1059 contents
1061 (cond
1062 ;; When BOOKTABS are activated enforce bottom rule even when
1063 ;; no hline was specifically marked.
1064 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1065 ((memq 'below borders) "\n_"))))))
1068 ;;; Target
1070 (defun org-man-target (target contents info)
1071 "Transcode a TARGET object from Org to Man.
1072 CONTENTS is nil. INFO is a plist holding contextual
1073 information."
1074 (format "\\fI%s\\fP"
1075 (org-export-solidify-link-text (org-element-property :value target))))
1078 ;;; Timestamp
1080 (defun org-man-timestamp (timestamp contents info)
1081 "Transcode a TIMESTAMP object from Org to Man.
1082 CONTENTS is nil. INFO is a plist holding contextual
1083 information."
1084 "" )
1087 ;;; Underline
1089 (defun org-man-underline (underline contents info)
1090 "Transcode UNDERLINE from Org to Man.
1091 CONTENTS is the text with underline markup. INFO is a plist
1092 holding contextual information."
1093 (format "\\fI%s\\fP" contents))
1096 ;;; Verbatim
1098 (defun org-man-verbatim (verbatim contents info)
1099 "Transcode a VERBATIM object from Org to Man.
1100 CONTENTS is nil. INFO is a plist used as a communication
1101 channel."
1102 (format ".nf\n%s\n.fi" contents))
1105 ;;; Verse Block
1107 (defun org-man-verse-block (verse-block contents info)
1108 "Transcode a VERSE-BLOCK element from Org to Man.
1109 CONTENTS is verse block contents. INFO is a plist holding
1110 contextual information."
1111 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1115 ;;; Interactive functions
1117 (defun org-man-export-to-man
1118 (&optional async subtreep visible-only body-only ext-plist)
1119 "Export current buffer to a Man file.
1121 If narrowing is active in the current buffer, only export its
1122 narrowed part.
1124 If a region is active, export that region.
1126 A non-nil optional argument ASYNC means the process should happen
1127 asynchronously. The resulting file should be accessible through
1128 the `org-export-stack' interface.
1130 When optional argument SUBTREEP is non-nil, export the sub-tree
1131 at point, extracting information from the headline properties
1132 first.
1134 When optional argument VISIBLE-ONLY is non-nil, don't export
1135 contents of hidden elements.
1137 When optional argument BODY-ONLY is non-nil, only the body
1138 without any markers.
1140 EXT-PLIST, when provided, is a property list with external
1141 parameters overriding Org default settings, but still inferior to
1142 file-local settings.
1144 Return output file's name."
1145 (interactive)
1146 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1147 (org-export-to-file 'man outfile
1148 async subtreep visible-only body-only ext-plist)))
1150 (defun org-man-export-to-pdf
1151 (&optional async subtreep visible-only body-only ext-plist)
1152 "Export current buffer to Groff then process through to PDF.
1154 If narrowing is active in the current buffer, only export its
1155 narrowed part.
1157 If a region is active, export that region.
1159 A non-nil optional argument ASYNC means the process should happen
1160 asynchronously. The resulting file should be accessible through
1161 the `org-export-stack' interface.
1163 When optional argument SUBTREEP is non-nil, export the sub-tree
1164 at point, extracting information from the headline properties
1165 first.
1167 When optional argument VISIBLE-ONLY is non-nil, don't export
1168 contents of hidden elements.
1170 When optional argument BODY-ONLY is non-nil, only write between
1171 markers.
1173 EXT-PLIST, when provided, is a property list with external
1174 parameters overriding Org default settings, but still inferior to
1175 file-local settings.
1177 Return PDF file's name."
1178 (interactive)
1179 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1180 (org-export-to-file 'man outfile
1181 async subtreep visible-only body-only ext-plist
1182 (lambda (file) (org-latex-compile file)))))
1184 (defun org-man-compile (file)
1185 "Compile a Groff file.
1187 FILE is the name of the file being compiled. Processing is done
1188 through the command specified in `org-man-pdf-process'.
1190 Return PDF file name or an error if it couldn't be produced."
1191 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1192 (full-name (file-truename file))
1193 (out-dir (file-name-directory file))
1194 ;; Properly set working directory for compilation.
1195 (default-directory (if (file-name-absolute-p file)
1196 (file-name-directory full-name)
1197 default-directory))
1198 errors)
1199 (message (format "Processing Groff file %s..." file))
1200 (save-window-excursion
1201 (cond
1202 ;; A function is provided: Apply it.
1203 ((functionp org-man-pdf-process)
1204 (funcall org-man-pdf-process (shell-quote-argument file)))
1205 ;; A list is provided: Replace %b, %f and %o with appropriate
1206 ;; values in each command before applying it. Output is
1207 ;; redirected to "*Org PDF Groff Output*" buffer.
1208 ((consp org-man-pdf-process)
1209 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1210 (mapc
1211 (lambda (command)
1212 (shell-command
1213 (replace-regexp-in-string
1214 "%b" (shell-quote-argument base-name)
1215 (replace-regexp-in-string
1216 "%f" (shell-quote-argument full-name)
1217 (replace-regexp-in-string
1218 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1219 outbuf))
1220 org-man-pdf-process)
1221 ;; Collect standard errors from output buffer.
1222 (setq errors (org-man-collect-errors outbuf))))
1223 (t (error "No valid command to process to PDF")))
1224 (let ((pdffile (concat out-dir base-name ".pdf")))
1225 ;; Check for process failure. Provide collected errors if
1226 ;; possible.
1227 (if (not (file-exists-p pdffile))
1228 (error (concat (format "PDF file %s wasn't produced" pdffile)
1229 (when errors (concat ": " errors))))
1230 ;; Else remove log files, when specified, and signal end of
1231 ;; process to user, along with any error encountered.
1232 (when org-man-remove-logfiles
1233 (dolist (ext org-man-logfiles-extensions)
1234 (let ((file (concat out-dir base-name "." ext)))
1235 (when (file-exists-p file) (delete-file file)))))
1236 (message (concat "Process completed"
1237 (if (not errors) "."
1238 (concat " with errors: " errors)))))
1239 ;; Return output file name.
1240 pdffile))))
1242 (defun org-man-collect-errors (buffer)
1243 "Collect some kind of errors from \"groff\" output
1244 BUFFER is the buffer containing output.
1245 Return collected error types as a string, or nil if there was
1246 none."
1247 (with-current-buffer buffer
1248 (save-excursion
1249 (goto-char (point-max))
1250 ;; Find final run
1251 nil )))
1254 (provide 'ox-man)
1256 ;; Local variables:
1257 ;; generated-autoload-file: "org-loaddefs.el"
1258 ;; End:
1260 ;;; ox-man.el ends here