Merge branch 'maint'
[org-mode/org-kjn.git] / lisp / ox-man.el
blob4b27ab6c3847132703d0918d8210e4ace1edc5b1
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)))
124 ;;; User Configurable Variables
126 (defgroup org-export-man nil
127 "Options for exporting Org mode files to Man."
128 :tag "Org Export Man"
129 :group 'org-export)
131 ;;; Tables
133 (defcustom org-man-tables-centered t
134 "When non-nil, tables are exported in a center environment."
135 :group 'org-export-man
136 :version "24.4"
137 :package-version '(Org . "8.0")
138 :type 'boolean)
140 (defcustom org-man-tables-verbatim nil
141 "When non-nil, tables are exported verbatim."
142 :group 'org-export-man
143 :version "24.4"
144 :package-version '(Org . "8.0")
145 :type 'boolean)
148 (defcustom org-man-table-scientific-notation "%sE%s"
149 "Format string to display numbers in scientific notation.
150 The format should have \"%s\" twice, for mantissa and exponent
151 \(i.e. \"%s\\\\times10^{%s}\").
153 When nil, no transformation is made."
154 :group 'org-export-man
155 :version "24.4"
156 :package-version '(Org . "8.0")
157 :type '(choice
158 (string :tag "Format string")
159 (const :tag "No formatting")))
162 ;;; Inlinetasks
163 ;; Src blocks
165 (defcustom org-man-source-highlight nil
166 "Use GNU source highlight to embellish source blocks "
167 :group 'org-export-man
168 :version "24.4"
169 :package-version '(Org . "8.0")
170 :type 'boolean)
173 (defcustom org-man-source-highlight-langs
174 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
175 (scheme "scheme")
176 (c "c") (cc "cpp") (csharp "csharp") (d "d")
177 (fortran "fortran") (cobol "cobol") (pascal "pascal")
178 (ada "ada") (asm "asm")
179 (perl "perl") (cperl "perl")
180 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
181 (java "java") (javascript "javascript")
182 (tex "latex")
183 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
184 (ocaml "caml") (caml "caml")
185 (sql "sql") (sqlite "sql")
186 (html "html") (css "css") (xml "xml")
187 (bat "bat") (bison "bison") (clipper "clipper")
188 (ldap "ldap") (opa "opa")
189 (php "php") (postscript "postscript") (prolog "prolog")
190 (properties "properties") (makefile "makefile")
191 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
192 "Alist mapping languages to their listing language counterpart.
193 The key is a symbol, the major mode symbol without the \"-mode\".
194 The value is the string that should be inserted as the language
195 parameter for the listings package. If the mode name and the
196 listings name are the same, the language does not need an entry
197 in this list - but it does not hurt if it is present."
198 :group 'org-export-man
199 :version "24.4"
200 :package-version '(Org . "8.0")
201 :type '(repeat
202 (list
203 (symbol :tag "Major mode ")
204 (string :tag "Listings language"))))
208 (defvar org-man-custom-lang-environments nil
209 "Alist mapping languages to language-specific Man environments.
211 It is used during export of src blocks by the listings and
212 man packages. For example,
214 \(setq org-man-custom-lang-environments
215 '\(\(python \"pythoncode\"\)\)\)
217 would have the effect that if org encounters begin_src python
218 during man export."
222 ;;; Compilation
224 (defcustom org-man-pdf-process
225 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
226 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
227 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
229 "Commands to process a Man file to a PDF file.
230 This is a list of strings, each of them will be given to the
231 shell as a command. %f in the command will be replaced by the
232 full file name, %b by the file base name (i.e. without directory
233 and extension parts) and %o by the base directory of the file.
236 By default, Org uses 3 runs of to do the processing.
238 Alternatively, this may be a Lisp function that does the
239 processing. This function should accept the file name as
240 its single argument."
241 :group 'org-export-pdf
242 :group 'org-export-man
243 :version "24.4"
244 :package-version '(Org . "8.0")
245 :type '(choice
246 (repeat :tag "Shell command sequence"
247 (string :tag "Shell command"))
248 (const :tag "2 runs of pdfgroff"
249 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
250 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
251 (const :tag "3 runs of pdfgroff"
252 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
253 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
254 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
255 (function)))
257 (defcustom org-man-logfiles-extensions
258 '("log" "out" "toc")
259 "The list of file extensions to consider as Man logfiles."
260 :group 'org-export-man
261 :version "24.4"
262 :package-version '(Org . "8.0")
263 :type '(repeat (string :tag "Extension")))
265 (defcustom org-man-remove-logfiles t
266 "Non-nil means remove the logfiles produced by PDF production.
267 These are the .aux, .log, .out, and .toc files."
268 :group 'org-export-man
269 :version "24.4"
270 :package-version '(Org . "8.0")
271 :type 'boolean)
275 ;;; Internal Functions
277 (defun org-man--caption/label-string (element info)
278 "Return caption and label Man string for ELEMENT.
280 INFO is a plist holding contextual information. If there's no
281 caption nor label, return the empty string.
283 For non-floats, see `org-man--wrap-label'."
284 (let ((label (org-element-property :label element))
285 (main (org-export-get-caption element))
286 (short (org-export-get-caption element t)))
287 (cond ((and (not main) (not label)) "")
288 ((not main) (format "\\fI%s\\fP" label))
289 ;; Option caption format with short name.
290 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
291 (org-export-data short info)
292 (org-export-data main info)))
293 ;; Standard caption format.
294 (t (format "\\fR%s\\fP" (org-export-data main info))))))
296 (defun org-man--wrap-label (element output)
297 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
298 This function shouldn't be used for floats. See
299 `org-man--caption/label-string'."
300 (let ((label (org-element-property :name element)))
301 (if (or (not output) (not label) (string= output "") (string= label ""))
302 output
303 (concat (format "%s\n.br\n" label) output))))
307 ;;; Template
309 (defun org-man-template (contents info)
310 "Return complete document string after Man conversion.
311 CONTENTS is the transcoded contents string. INFO is a plist
312 holding export options."
313 (let* ((title (org-export-data (plist-get info :title) info))
314 (attr (read (format "(%s)"
315 (mapconcat
316 #'identity
317 (list (plist-get info :man-class-options))
318 " "))))
319 (section-item (plist-get attr :section-id)))
321 (concat
323 (cond
324 ((and title (stringp section-item))
325 (format ".TH \"%s\" \"%s\" \n" title section-item))
326 ((and (string= "" title) (stringp section-item))
327 (format ".TH \"%s\" \"%s\" \n" " " section-item))
328 (title
329 (format ".TH \"%s\" \"1\" \n" title))
331 ".TH \" \" \"1\" "))
332 contents)))
337 ;;; Transcode Functions
339 ;;; Babel Call
341 ;; Babel Calls are ignored.
344 ;;; Bold
346 (defun org-man-bold (bold contents info)
347 "Transcode BOLD from Org to Man.
348 CONTENTS is the text with bold markup. INFO is a plist holding
349 contextual information."
350 (format "\\fB%s\\fP" contents))
353 ;;; Center Block
355 (defun org-man-center-block (center-block contents info)
356 "Transcode a CENTER-BLOCK element from Org to Man.
357 CONTENTS holds the contents of the center block. INFO is a plist
358 holding contextual information."
359 (org-man--wrap-label
360 center-block
361 (format ".ce %d\n.nf\n%s\n.fi"
362 (- (length (split-string contents "\n")) 1 )
363 contents)))
366 ;;; Clock
368 (defun org-man-clock (clock contents info)
369 "Transcode a CLOCK element from Org to Man.
370 CONTENTS is nil. INFO is a plist holding contextual
371 information."
372 "" )
375 ;;; Code
377 (defun org-man-code (code contents info)
378 "Transcode a CODE object from Org to Man.
379 CONTENTS is nil. INFO is a plist used as a communication
380 channel."
381 (format "\\fC%s\\fP" code))
384 ;;; Comment
386 ;; Comments are ignored.
389 ;;; Comment Block
391 ;; Comment Blocks are ignored.
394 ;;; Drawer
396 (defun org-man-drawer (drawer contents info)
397 "Transcode a DRAWER element from Org to Man.
398 DRAWER holds the drawer information
399 CONTENTS holds the contents of the block.
400 INFO is a plist holding contextual information. "
401 contents)
404 ;;; Dynamic Block
406 (defun org-man-dynamic-block (dynamic-block contents info)
407 "Transcode a DYNAMIC-BLOCK element from Org to Man.
408 CONTENTS holds the contents of the block. INFO is a plist
409 holding contextual information. See `org-export-data'."
410 (org-man--wrap-label dynamic-block contents))
413 ;;; Entity
415 (defun org-man-entity (entity contents info)
416 "Transcode an ENTITY object from Org to Man.
417 CONTENTS are the definition itself. INFO is a plist holding
418 contextual information."
419 (org-element-property :utf-8 entity))
422 ;;; Example Block
424 (defun org-man-example-block (example-block contents info)
425 "Transcode an EXAMPLE-BLOCK element from Org to Man.
426 CONTENTS is nil. INFO is a plist holding contextual
427 information."
428 (org-man--wrap-label
429 example-block
430 (format ".RS\n.nf\n%s\n.fi\n.RE"
431 (org-export-format-code-default example-block info))))
434 ;;; Export Snippet
436 (defun org-man-export-snippet (export-snippet contents info)
437 "Transcode a EXPORT-SNIPPET object from Org to Man.
438 CONTENTS is nil. INFO is a plist holding contextual information."
439 (when (eq (org-export-snippet-backend export-snippet) 'man)
440 (org-element-property :value export-snippet)))
443 ;;; Fixed Width
445 (defun org-man-fixed-width (fixed-width contents info)
446 "Transcode a FIXED-WIDTH element from Org to Man.
447 CONTENTS is nil. INFO is a plist holding contextual information."
448 (org-man--wrap-label
449 fixed-width
450 (format "\\fC\n%s\\fP"
451 (org-remove-indentation
452 (org-element-property :value fixed-width)))))
455 ;;; Footnote Definition
457 ;; Footnote Definitions are ignored.
459 ;;; Footnote References
461 ;; Footnote References are Ignored
464 ;;; Headline
466 (defun org-man-headline (headline contents info)
467 "Transcode a HEADLINE element from Org to Man.
468 CONTENTS holds the contents of the headline. INFO is a plist
469 holding contextual information."
470 (let* ((level (org-export-get-relative-level headline info))
471 (numberedp (org-export-numbered-headline-p headline info))
472 ;; Section formatting will set two placeholders: one for the
473 ;; title and the other for the contents.
474 (section-fmt
475 (case level
476 (1 ".SH \"%s\"\n%s")
477 (2 ".SS \"%s\"\n%s")
478 (3 ".SS \"%s\"\n%s")
479 (t nil)))
480 (text (org-export-data (org-element-property :title headline) info)))
482 (cond
483 ;; Case 1: This is a footnote section: ignore it.
484 ((org-element-property :footnote-section-p headline) nil)
486 ;; Case 2. This is a deep sub-tree: export it as a list item.
487 ;; Also export as items headlines for which no section
488 ;; format has been found.
489 ((or (not section-fmt) (org-export-low-level-p headline info))
490 ;; Build the real contents of the sub-tree.
491 (let ((low-level-body
492 (concat
493 ;; If the headline is the first sibling, start a list.
494 (when (org-export-first-sibling-p headline info)
495 (format "%s\n" ".RS"))
496 ;; Itemize headline
497 ".TP\n.ft I\n" text "\n.ft\n"
498 contents ".RE")))
499 ;; If headline is not the last sibling simply return
500 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
501 ;; blank line.
502 (if (not (org-export-last-sibling-p headline info)) low-level-body
503 (replace-regexp-in-string
504 "[ \t\n]*\\'" ""
505 low-level-body))))
507 ;; Case 3. Standard headline. Export it as a section.
508 (t (format section-fmt text contents )))))
510 ;;; Horizontal Rule
511 ;; Not supported
513 ;;; Inline Babel Call
515 ;; Inline Babel Calls are ignored.
517 ;;; Inline Src Block
519 (defun org-man-inline-src-block (inline-src-block contents info)
520 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
521 CONTENTS holds the contents of the item. INFO is a plist holding
522 contextual information."
523 (let* ((code (org-element-property :value inline-src-block)))
524 (cond
525 ((plist-get info :man-source-highlight)
526 (let* ((tmpdir (if (featurep 'xemacs)
527 temp-directory
528 temporary-file-directory ))
529 (in-file (make-temp-name
530 (expand-file-name "srchilite" tmpdir)))
531 (out-file (make-temp-name
532 (expand-file-name "reshilite" tmpdir)))
533 (org-lang (org-element-property :language inline-src-block))
534 (lst-lang
535 (cadr (assq (intern org-lang)
536 (plist-get info :man-source-highlight-langs))))
538 (cmd (concat (expand-file-name "source-highlight")
539 " -s " lst-lang
540 " -f groff_man"
541 " -i " in-file
542 " -o " out-file )))
544 (if lst-lang
545 (let ((code-block "" ))
546 (with-temp-file in-file (insert code))
547 (shell-command cmd)
548 (setq code-block (org-file-contents out-file))
549 (delete-file in-file)
550 (delete-file out-file)
551 code-block)
552 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
553 code))))
555 ;; Do not use a special package: transcode it verbatim.
557 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
558 "\\fP\n.fi\n.RE\n")))))
561 ;;; Inlinetask
562 ;;; Italic
564 (defun org-man-italic (italic contents info)
565 "Transcode ITALIC from Org to Man.
566 CONTENTS is the text with italic markup. INFO is a plist holding
567 contextual information."
568 (format "\\fI%s\\fP" contents))
571 ;;; Item
574 (defun org-man-item (item contents info)
576 "Transcode an ITEM element from Org to Man.
577 CONTENTS holds the contents of the item. INFO is a plist holding
578 contextual information."
580 (let* ((bullet (org-element-property :bullet item))
581 (type (org-element-property :type (org-element-property :parent item)))
582 (checkbox (case (org-element-property :checkbox item)
583 (on "\\o'\\(sq\\(mu'") ;;
584 (off "\\(sq ") ;;
585 (trans "\\o'\\(sq\\(mi'" ))) ;;
587 (tag (let ((tag (org-element-property :tag item)))
588 ;; Check-boxes must belong to the tag.
589 (and tag (format "\\fB%s\\fP"
590 (concat checkbox
591 (org-export-data tag info)))))))
593 (if (and (null tag )
594 (null checkbox))
595 (let* ((bullet (org-trim bullet))
596 (marker (cond ((string= "-" bullet) "\\(em")
597 ((string= "*" bullet) "\\(bu")
598 ((eq type 'ordered)
599 (format "%s " (org-trim bullet)))
600 (t "\\(dg"))))
601 (concat ".IP " marker " 4\n"
602 (org-trim (or contents " " ))))
603 ; else
604 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
605 (org-trim (or contents " " ))))))
607 ;;; Keyword
610 (defun org-man-keyword (keyword contents info)
611 "Transcode a KEYWORD element from Org to Man.
612 CONTENTS is nil. INFO is a plist holding contextual information."
613 (let ((key (org-element-property :key keyword))
614 (value (org-element-property :value keyword)))
615 (cond
616 ((string= key "MAN") value)
617 ((string= key "INDEX") nil)
618 ((string= key "TOC" ) nil))))
621 ;;; Line Break
623 (defun org-man-line-break (line-break contents info)
624 "Transcode a LINE-BREAK object from Org to Man.
625 CONTENTS is nil. INFO is a plist holding contextual information."
626 ".br\n")
629 ;;; Link
632 (defun org-man-link (link desc info)
633 "Transcode a LINK object from Org to Man.
635 DESC is the description part of the link, or the empty string.
636 INFO is a plist holding contextual information. See
637 `org-export-data'."
638 (let* ((type (org-element-property :type link))
639 (raw-path (org-element-property :path link))
640 ;; Ensure DESC really exists, or set it to nil.
641 (desc (and (not (string= desc "")) desc))
642 (path (cond
643 ((member type '("http" "https" "ftp" "mailto"))
644 (concat type ":" raw-path))
645 ((and (string= type "file") (file-name-absolute-p raw-path))
646 (concat "file:" raw-path))
647 (t raw-path)))
648 protocol)
649 (cond
650 ;; External link with a description part.
651 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
652 ;; External link without a description part.
653 (path (format "\\fI%s\\fP" path))
654 ;; No path, only description. Try to do something useful.
655 (t (format "\\fI%s\\fP" desc)))))
657 ;;;; Node Property
659 (defun org-man-node-property (node-property contents info)
660 "Transcode a NODE-PROPERTY element from Org to Man.
661 CONTENTS is nil. INFO is a plist holding contextual
662 information."
663 (format "%s:%s"
664 (org-element-property :key node-property)
665 (let ((value (org-element-property :value node-property)))
666 (if value (concat " " value) ""))))
668 ;;; Paragraph
670 (defun org-man-paragraph (paragraph contents info)
671 "Transcode a PARAGRAPH element from Org to Man.
672 CONTENTS is the contents of the paragraph, as a string. INFO is
673 the plist used as a communication channel."
674 (let ((parent (plist-get (nth 1 paragraph) :parent)))
675 (when parent
676 (let ((parent-type (car parent))
677 (fixed-paragraph ""))
678 (cond ((and (eq parent-type 'item)
679 (plist-get (nth 1 parent) :bullet ))
680 (setq fixed-paragraph (concat "" contents)))
681 ((eq parent-type 'section)
682 (setq fixed-paragraph (concat ".PP\n" contents)))
683 ((eq parent-type 'footnote-definition)
684 (setq fixed-paragraph contents))
685 (t (setq fixed-paragraph (concat "" contents))))
686 fixed-paragraph ))))
689 ;;; Plain List
691 (defun org-man-plain-list (plain-list contents info)
692 "Transcode a PLAIN-LIST element from Org to Man.
693 CONTENTS is the contents of the list. INFO is a plist holding
694 contextual information."
695 contents)
697 ;;; Plain Text
699 (defun org-man-plain-text (text info)
700 "Transcode a TEXT string from Org to Man.
701 TEXT is the string to transcode. INFO is a plist holding
702 contextual information."
703 (let ((output text))
704 ;; Protect various chars.
705 (setq output (replace-regexp-in-string
706 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
707 "$\\" output nil t 1))
708 ;; Activate smart quotes. Be sure to provide original TEXT string
709 ;; since OUTPUT may have been modified.
710 (when (plist-get info :with-smart-quotes)
711 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
712 ;; Handle break preservation if required.
713 (when (plist-get info :preserve-breaks)
714 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
715 output)))
716 ;; Return value.
717 output))
721 ;;; Planning
724 ;;; Property Drawer
726 (defun org-man-property-drawer (property-drawer contents info)
727 "Transcode a PROPERTY-DRAWER element from Org to Man.
728 CONTENTS holds the contents of the drawer. INFO is a plist
729 holding contextual information."
730 (and (org-string-nw-p contents)
731 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
733 ;;; Quote Block
735 (defun org-man-quote-block (quote-block contents info)
736 "Transcode a QUOTE-BLOCK element from Org to Man.
737 CONTENTS holds the contents of the block. INFO is a plist
738 holding contextual information."
739 (org-man--wrap-label
740 quote-block
741 (format ".RS\n%s\n.RE" contents)))
744 ;;; Radio Target
746 (defun org-man-radio-target (radio-target text info)
747 "Transcode a RADIO-TARGET object from Org to Man.
748 TEXT is the text of the target. INFO is a plist holding
749 contextual information."
750 text )
753 ;;; Section
755 (defun org-man-section (section contents info)
756 "Transcode a SECTION element from Org to Man.
757 CONTENTS holds the contents of the section. INFO is a plist
758 holding contextual information."
759 contents)
762 ;;; Special Block
764 (defun org-man-special-block (special-block contents info)
765 "Transcode a SPECIAL-BLOCK element from Org to Man.
766 CONTENTS holds the contents of the block. INFO is a plist
767 holding contextual information."
768 (if (org-export-raw-special-block-p special-block info)
769 (org-remove-indentation (org-element-property :raw-value special-block))
770 (let ((type (downcase (org-element-property :type special-block))))
771 (org-man--wrap-label special-block (format "%s\n" contents)))))
774 ;;; Src Block
776 (defun org-man-src-block (src-block contents info)
777 "Transcode a SRC-BLOCK element from Org to Man.
778 CONTENTS holds the contents of the item. INFO is a plist holding
779 contextual information."
780 (let* ((lang (org-element-property :language src-block))
781 (code (org-element-property :value src-block))
782 (custom-env (and lang
783 (cadr (assq (intern lang)
784 org-man-custom-lang-environments))))
785 (num-start (case (org-element-property :number-lines src-block)
786 (continued (org-export-get-loc src-block info))
787 (new 0)))
788 (retain-labels (org-element-property :retain-labels src-block)))
789 (if (not (plist-get info :man-source-highlight))
790 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
791 (org-export-format-code-default src-block info))
792 (let* ((tmpdir (if (featurep 'xemacs) temp-directory
793 temporary-file-directory))
794 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
795 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
796 (org-lang (org-element-property :language src-block))
797 (lst-lang
798 (cadr (assq (intern org-lang)
799 (plist-get info :man-source-highlight-langs))))
800 (cmd (concat "source-highlight"
801 " -s " lst-lang
802 " -f groff_man "
803 " -i " in-file
804 " -o " out-file)))
805 (if lst-lang
806 (let ((code-block ""))
807 (with-temp-file in-file (insert code))
808 (shell-command cmd)
809 (setq code-block (org-file-contents out-file))
810 (delete-file in-file)
811 (delete-file out-file)
812 code-block)
813 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code))))))
816 ;;; Statistics Cookie
818 (defun org-man-statistics-cookie (statistics-cookie contents info)
819 "Transcode a STATISTICS-COOKIE object from Org to Man.
820 CONTENTS is nil. INFO is a plist holding contextual information."
821 (org-element-property :value statistics-cookie))
824 ;;; Strike-Through
826 (defun org-man-strike-through (strike-through contents info)
827 "Transcode STRIKE-THROUGH from Org to Man.
828 CONTENTS is the text with strike-through markup. INFO is a plist
829 holding contextual information."
830 (format "\\fI%s\\fP" contents))
832 ;;; Subscript
834 (defun org-man-subscript (subscript contents info)
835 "Transcode a SUBSCRIPT object from Org to Man.
836 CONTENTS is the contents of the object. INFO is a plist holding
837 contextual information."
838 (format "\\d\\s-2%s\\s+2\\u" contents))
840 ;;; Superscript "^_%s$
842 (defun org-man-superscript (superscript contents info)
843 "Transcode a SUPERSCRIPT object from Org to Man.
844 CONTENTS is the contents of the object. INFO is a plist holding
845 contextual information."
846 (format "\\u\\s-2%s\\s+2\\d" contents))
849 ;;; Table
851 ;; `org-man-table' is the entry point for table transcoding. It
852 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
853 ;; delegates the job to either `org-man-table--table.el-table' or
854 ;; `org-man-table--org-table' functions, depending of the type of
855 ;; the table.
857 ;; `org-man-table--align-string' is a subroutine used to build
858 ;; alignment string for Org tables.
860 (defun org-man-table (table contents info)
861 "Transcode a TABLE element from Org to Man.
862 CONTENTS is the contents of the table. INFO is a plist holding
863 contextual information."
864 (cond
865 ;; Case 1: verbatim table.
866 ((or (plist-get info :man-tables-verbatim)
867 (let ((attr (read (format "(%s)"
868 (mapconcat
869 #'identity
870 (org-element-property :attr_man table)
871 " ")))))
873 (and attr (plist-get attr :verbatim))))
875 (format ".nf\n\\fC%s\\fP\n.fi"
876 ;; Re-create table, without affiliated keywords.
877 (org-trim
878 (org-element-interpret-data
879 `(table nil ,@(org-element-contents table))))))
880 ;; Case 2: Standard table.
881 (t (org-man-table--org-table table contents info))))
883 (defun org-man-table--align-string (divider table info)
884 "Return an appropriate Man alignment string.
885 TABLE is the considered table. INFO is a plist used as
886 a communication channel."
887 (let (alignment)
888 ;; Extract column groups and alignment from first (non-rule) row.
889 (org-element-map
890 (org-element-map table 'table-row
891 (lambda (row)
892 (and (eq (org-element-property :type row) 'standard) row))
893 info 'first-match)
894 'table-cell
895 (lambda (cell)
896 (let* ((borders (org-export-table-cell-borders cell info))
897 (raw-width (org-export-table-cell-width cell info))
898 (width-cm (when raw-width (/ raw-width 5)))
899 (width (if raw-width (format "w(%dc)"
900 (if (< width-cm 1) 1 width-cm)) "")))
901 ;; Check left border for the first cell only.
902 (when (and (memq 'left borders) (not alignment))
903 (push "|" alignment))
904 (push
905 (case (org-export-table-cell-alignment cell info)
906 (left (concat "l" width divider))
907 (right (concat "r" width divider))
908 (center (concat "c" width divider)))
909 alignment)
910 (when (memq 'right borders) (push "|" alignment))))
911 info)
912 (apply 'concat (reverse alignment))))
914 (defun org-man-table--org-table (table contents info)
915 "Return appropriate Man code for an Org table.
917 TABLE is the table type element to transcode. CONTENTS is its
918 contents, as a string. INFO is a plist used as a communication
919 channel.
921 This function assumes TABLE has `org' as its `:type' attribute."
922 (let* ((attr (org-export-read-attribute :attr_man table))
923 (label (org-element-property :name table))
924 (caption (and (not (plist-get attr :disable-caption))
925 (org-man--caption/label-string table info)))
926 (divider (if (plist-get attr :divider) "|" " "))
928 ;; Determine alignment string.
929 (alignment (org-man-table--align-string divider table info))
930 ;; Extract others display options.
932 (lines (org-split-string contents "\n"))
934 (attr-list
935 (delq nil
936 (list
937 (and (plist-get attr :expand) "expand")
938 (let ((placement (plist-get attr :placement)))
939 (cond ((string= placement 'center) "center")
940 ((string= placement 'left) nil)
941 ((plist-get info :man-tables-centered) "center")
942 (t "")))
943 (or (plist-get attr :boxtype) "box"))))
945 (title-line (plist-get attr :title-line))
946 (long-cells (plist-get attr :long-cells))
948 (table-format (concat
949 (format "%s" (or (car attr-list) "" ))
951 (let ((output-list '()))
952 (when (cdr attr-list)
953 (dolist (attr-item (cdr attr-list))
954 (setq output-list (concat output-list (format ",%s" attr-item)))))
955 output-list)
956 "")))
958 (first-line (when lines (org-split-string (car lines) "\t"))))
959 ;; Prepare the final format string for the table.
962 (cond
963 ;; Others.
964 (lines (concat ".TS\n " table-format ";\n"
966 (format "%s.\n"
967 (let ((final-line ""))
968 (when title-line
969 (dotimes (i (length first-line))
970 (setq final-line (concat final-line "cb" divider))))
972 (setq final-line (concat final-line "\n"))
974 (if alignment
975 (setq final-line (concat final-line alignment))
976 (dotimes (i (length first-line))
977 (setq final-line (concat final-line "c" divider))))
978 final-line ))
980 (format "%s.TE\n"
981 (let ((final-line "")
982 (long-line "")
983 (lines (org-split-string contents "\n")))
985 (dolist (line-item lines)
986 (setq long-line "")
988 (if long-cells
989 (progn
990 (if (string= line-item "_")
991 (setq long-line (format "%s\n" line-item))
992 ;; else string =
993 (let ((cell-item-list (org-split-string line-item "\t")))
994 (dolist (cell-item cell-item-list)
996 (cond ((eq cell-item (car (last cell-item-list)))
997 (setq long-line (concat long-line
998 (format "T{\n%s\nT}\t\n" cell-item ))))
1000 (setq long-line (concat long-line
1001 (format "T{\n%s\nT}\t" cell-item ))))))
1002 long-line))
1003 ;; else long cells
1004 (setq final-line (concat final-line long-line )))
1006 (setq final-line (concat final-line line-item "\n"))))
1007 final-line))
1009 (and caption (format ".TB \"%s\"" caption)))))))
1011 ;;; Table Cell
1013 (defun org-man-table-cell (table-cell contents info)
1014 "Transcode a TABLE-CELL element from Org to Man
1015 CONTENTS is the cell contents. INFO is a plist used as
1016 a communication channel."
1017 (concat
1018 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
1019 (if (and contents
1020 scientific-format
1021 (string-match orgtbl-exp-regexp contents))
1022 ;; Use appropriate format string for scientific notation.
1023 (format scientific-format
1024 (match-string 1 contents)
1025 (match-string 2 contents))
1026 contents))
1027 (when (org-export-get-next-element table-cell info) "\t")))
1030 ;;; Table Row
1032 (defun org-man-table-row (table-row contents info)
1033 "Transcode a TABLE-ROW element from Org to Man
1034 CONTENTS is the contents of the row. INFO is a plist used as
1035 a communication channel."
1036 ;; Rules are ignored since table separators are deduced from
1037 ;; borders of the current row.
1038 (when (eq (org-element-property :type table-row) 'standard)
1039 (let* ((attr (mapconcat 'identity
1040 (org-element-property
1041 :attr_man (org-export-get-parent table-row))
1042 " "))
1043 ;; TABLE-ROW's borders are extracted from its first cell.
1044 (borders
1045 (org-export-table-cell-borders
1046 (car (org-element-contents table-row)) info)))
1047 (concat
1048 ;; Mark horizontal lines
1049 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1050 contents
1052 (cond
1053 ;; When BOOKTABS are activated enforce bottom rule even when
1054 ;; no hline was specifically marked.
1055 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1056 ((memq 'below borders) "\n_"))))))
1059 ;;; Target
1061 (defun org-man-target (target contents info)
1062 "Transcode a TARGET object from Org to Man.
1063 CONTENTS is nil. INFO is a plist holding contextual
1064 information."
1065 (format "\\fI%s\\fP"
1066 (org-export-solidify-link-text (org-element-property :value target))))
1069 ;;; Timestamp
1071 (defun org-man-timestamp (timestamp contents info)
1072 "Transcode a TIMESTAMP object from Org to Man.
1073 CONTENTS is nil. INFO is a plist holding contextual
1074 information."
1075 "" )
1078 ;;; Underline
1080 (defun org-man-underline (underline contents info)
1081 "Transcode UNDERLINE from Org to Man.
1082 CONTENTS is the text with underline markup. INFO is a plist
1083 holding contextual information."
1084 (format "\\fI%s\\fP" contents))
1087 ;;; Verbatim
1089 (defun org-man-verbatim (verbatim contents info)
1090 "Transcode a VERBATIM object from Org to Man.
1091 CONTENTS is nil. INFO is a plist used as a communication
1092 channel."
1093 (format ".nf\n%s\n.fi" contents))
1096 ;;; Verse Block
1098 (defun org-man-verse-block (verse-block contents info)
1099 "Transcode a VERSE-BLOCK element from Org to Man.
1100 CONTENTS is verse block contents. INFO is a plist holding
1101 contextual information."
1102 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1106 ;;; Interactive functions
1108 (defun org-man-export-to-man
1109 (&optional async subtreep visible-only body-only ext-plist)
1110 "Export current buffer to a Man file.
1112 If narrowing is active in the current buffer, only export its
1113 narrowed part.
1115 If a region is active, export that region.
1117 A non-nil optional argument ASYNC means the process should happen
1118 asynchronously. The resulting file should be accessible through
1119 the `org-export-stack' interface.
1121 When optional argument SUBTREEP is non-nil, export the sub-tree
1122 at point, extracting information from the headline properties
1123 first.
1125 When optional argument VISIBLE-ONLY is non-nil, don't export
1126 contents of hidden elements.
1128 When optional argument BODY-ONLY is non-nil, only the body
1129 without any markers.
1131 EXT-PLIST, when provided, is a property list with external
1132 parameters overriding Org default settings, but still inferior to
1133 file-local settings.
1135 Return output file's name."
1136 (interactive)
1137 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1138 (org-export-to-file 'man outfile
1139 async subtreep visible-only body-only ext-plist)))
1141 (defun org-man-export-to-pdf
1142 (&optional async subtreep visible-only body-only ext-plist)
1143 "Export current buffer to Groff then process through to PDF.
1145 If narrowing is active in the current buffer, only export its
1146 narrowed part.
1148 If a region is active, export that region.
1150 A non-nil optional argument ASYNC means the process should happen
1151 asynchronously. The resulting file should be accessible through
1152 the `org-export-stack' interface.
1154 When optional argument SUBTREEP is non-nil, export the sub-tree
1155 at point, extracting information from the headline properties
1156 first.
1158 When optional argument VISIBLE-ONLY is non-nil, don't export
1159 contents of hidden elements.
1161 When optional argument BODY-ONLY is non-nil, only write between
1162 markers.
1164 EXT-PLIST, when provided, is a property list with external
1165 parameters overriding Org default settings, but still inferior to
1166 file-local settings.
1168 Return PDF file's name."
1169 (interactive)
1170 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1171 (org-export-to-file 'man outfile
1172 async subtreep visible-only body-only ext-plist
1173 (lambda (file) (org-latex-compile file)))))
1175 (defun org-man-compile (file)
1176 "Compile a Groff file.
1178 FILE is the name of the file being compiled. Processing is done
1179 through the command specified in `org-man-pdf-process'.
1181 Return PDF file name or an error if it couldn't be produced."
1182 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1183 (full-name (file-truename file))
1184 (out-dir (file-name-directory file))
1185 ;; Properly set working directory for compilation.
1186 (default-directory (if (file-name-absolute-p file)
1187 (file-name-directory full-name)
1188 default-directory))
1189 errors)
1190 (message (format "Processing Groff file %s..." file))
1191 (save-window-excursion
1192 (cond
1193 ;; A function is provided: Apply it.
1194 ((functionp org-man-pdf-process)
1195 (funcall org-man-pdf-process (shell-quote-argument file)))
1196 ;; A list is provided: Replace %b, %f and %o with appropriate
1197 ;; values in each command before applying it. Output is
1198 ;; redirected to "*Org PDF Groff Output*" buffer.
1199 ((consp org-man-pdf-process)
1200 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1201 (mapc
1202 (lambda (command)
1203 (shell-command
1204 (replace-regexp-in-string
1205 "%b" (shell-quote-argument base-name)
1206 (replace-regexp-in-string
1207 "%f" (shell-quote-argument full-name)
1208 (replace-regexp-in-string
1209 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1210 outbuf))
1211 org-man-pdf-process)
1212 ;; Collect standard errors from output buffer.
1213 (setq errors (org-man-collect-errors outbuf))))
1214 (t (error "No valid command to process to PDF")))
1215 (let ((pdffile (concat out-dir base-name ".pdf")))
1216 ;; Check for process failure. Provide collected errors if
1217 ;; possible.
1218 (if (not (file-exists-p pdffile))
1219 (error (concat (format "PDF file %s wasn't produced" pdffile)
1220 (when errors (concat ": " errors))))
1221 ;; Else remove log files, when specified, and signal end of
1222 ;; process to user, along with any error encountered.
1223 (when org-man-remove-logfiles
1224 (dolist (ext org-man-logfiles-extensions)
1225 (let ((file (concat out-dir base-name "." ext)))
1226 (when (file-exists-p file) (delete-file file)))))
1227 (message (concat "Process completed"
1228 (if (not errors) "."
1229 (concat " with errors: " errors)))))
1230 ;; Return output file name.
1231 pdffile))))
1233 (defun org-man-collect-errors (buffer)
1234 "Collect some kind of errors from \"groff\" output
1235 BUFFER is the buffer containing output.
1236 Return collected error types as a string, or nil if there was
1237 none."
1238 (with-current-buffer buffer
1239 (save-excursion
1240 (goto-char (point-max))
1241 ;; Find final run
1242 nil )))
1245 (provide 'ox-man)
1247 ;; Local variables:
1248 ;; generated-autoload-file: "org-loaddefs.el"
1249 ;; End:
1251 ;;; ox-man.el ends here