Merge branch 'maint'
[org-mode.git] / lisp / ox-man.el
blob9ee30932ba155b1dde667c1c048e02ab52b7fa4c
1 ;; ox-man.el --- Man Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2015 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 (code . org-man-code)
57 (drawer . org-man-drawer)
58 (dynamic-block . org-man-dynamic-block)
59 (entity . org-man-entity)
60 (example-block . org-man-example-block)
61 (export-block . org-man-export-block)
62 (export-snippet . org-man-export-snippet)
63 (fixed-width . org-man-fixed-width)
64 (footnote-definition . org-man-footnote-definition)
65 (footnote-reference . org-man-footnote-reference)
66 (headline . org-man-headline)
67 (horizontal-rule . org-man-horizontal-rule)
68 (inline-babel-call . org-man-inline-babel-call)
69 (inline-src-block . org-man-inline-src-block)
70 (inlinetask . org-man-inlinetask)
71 (italic . org-man-italic)
72 (item . org-man-item)
73 (keyword . org-man-keyword)
74 (line-break . org-man-line-break)
75 (link . org-man-link)
76 (node-property . org-man-node-property)
77 (paragraph . org-man-paragraph)
78 (plain-list . org-man-plain-list)
79 (plain-text . org-man-plain-text)
80 (planning . org-man-planning)
81 (property-drawer . org-man-property-drawer)
82 (quote-block . org-man-quote-block)
83 (radio-target . org-man-radio-target)
84 (section . org-man-section)
85 (special-block . org-man-special-block)
86 (src-block . org-man-src-block)
87 (statistics-cookie . org-man-statistics-cookie)
88 (strike-through . org-man-strike-through)
89 (subscript . org-man-subscript)
90 (superscript . org-man-superscript)
91 (table . org-man-table)
92 (table-cell . org-man-table-cell)
93 (table-row . org-man-table-row)
94 (target . org-man-target)
95 (template . org-man-template)
96 (timestamp . org-man-timestamp)
97 (underline . org-man-underline)
98 (verbatim . org-man-verbatim)
99 (verse-block . org-man-verse-block))
100 :export-block "MAN"
101 :menu-entry
102 '(?M "Export to MAN"
103 ((?m "As MAN file" org-man-export-to-man)
104 (?p "As PDF file" org-man-export-to-pdf)
105 (?o "As PDF file and open"
106 (lambda (a s v b)
107 (if a (org-man-export-to-pdf t s v b)
108 (org-open-file (org-man-export-to-pdf nil s v b)))))))
109 :options-alist
110 '((:man-class "MAN_CLASS" nil nil t)
111 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
112 (:man-header-extra "MAN_HEADER" nil nil newline)
113 ;; Other variables.
114 (:man-tables-centered nil nil org-man-tables-centered)
115 (:man-tables-verbatim nil nil org-man-tables-verbatim)
116 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
117 (:man-source-highlight nil nil org-man-source-highlight)
118 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
122 ;;; User Configurable Variables
124 (defgroup org-export-man nil
125 "Options for exporting Org mode files to Man."
126 :tag "Org Export Man"
127 :group 'org-export)
129 ;;; Tables
131 (defcustom org-man-tables-centered t
132 "When non-nil, tables are exported in a center environment."
133 :group 'org-export-man
134 :version "24.4"
135 :package-version '(Org . "8.0")
136 :type 'boolean)
138 (defcustom org-man-tables-verbatim nil
139 "When non-nil, tables are exported verbatim."
140 :group 'org-export-man
141 :version "24.4"
142 :package-version '(Org . "8.0")
143 :type 'boolean)
146 (defcustom org-man-table-scientific-notation "%sE%s"
147 "Format string to display numbers in scientific notation.
148 The format should have \"%s\" twice, for mantissa and exponent
149 \(i.e. \"%s\\\\times10^{%s}\").
151 When nil, no transformation is made."
152 :group 'org-export-man
153 :version "24.4"
154 :package-version '(Org . "8.0")
155 :type '(choice
156 (string :tag "Format string")
157 (const :tag "No formatting")))
160 ;;; Inlinetasks
161 ;; Src blocks
163 (defcustom org-man-source-highlight nil
164 "Use GNU source highlight to embellish source blocks "
165 :group 'org-export-man
166 :version "24.4"
167 :package-version '(Org . "8.0")
168 :type 'boolean)
171 (defcustom org-man-source-highlight-langs
172 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
173 (scheme "scheme")
174 (c "c") (cc "cpp") (csharp "csharp") (d "d")
175 (fortran "fortran") (cobol "cobol") (pascal "pascal")
176 (ada "ada") (asm "asm")
177 (perl "perl") (cperl "perl")
178 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
179 (java "java") (javascript "javascript")
180 (tex "latex")
181 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
182 (ocaml "caml") (caml "caml")
183 (sql "sql") (sqlite "sql")
184 (html "html") (css "css") (xml "xml")
185 (bat "bat") (bison "bison") (clipper "clipper")
186 (ldap "ldap") (opa "opa")
187 (php "php") (postscript "postscript") (prolog "prolog")
188 (properties "properties") (makefile "makefile")
189 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
190 "Alist mapping languages to their listing language counterpart.
191 The key is a symbol, the major mode symbol without the \"-mode\".
192 The value is the string that should be inserted as the language
193 parameter for the listings package. If the mode name and the
194 listings name are the same, the language does not need an entry
195 in this list - but it does not hurt if it is present."
196 :group 'org-export-man
197 :version "24.4"
198 :package-version '(Org . "8.0")
199 :type '(repeat
200 (list
201 (symbol :tag "Major mode ")
202 (string :tag "Listings language"))))
205 ;;; Compilation
207 (defcustom org-man-pdf-process
208 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
209 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
210 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
212 "Commands to process a Man file to a PDF file.
213 This is a list of strings, each of them will be given to the
214 shell as a command. %f in the command will be replaced by the
215 full file name, %b by the file base name (i.e. without directory
216 and extension parts) and %o by the base directory of the file.
219 By default, Org uses 3 runs of to do the processing.
221 Alternatively, this may be a Lisp function that does the
222 processing. This function should accept the file name as
223 its single argument."
224 :group 'org-export-pdf
225 :group 'org-export-man
226 :version "24.4"
227 :package-version '(Org . "8.0")
228 :type '(choice
229 (repeat :tag "Shell command sequence"
230 (string :tag "Shell command"))
231 (const :tag "2 runs of pdfgroff"
232 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
233 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
234 (const :tag "3 runs of pdfgroff"
235 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
236 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
237 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
238 (function)))
240 (defcustom org-man-logfiles-extensions
241 '("log" "out" "toc")
242 "The list of file extensions to consider as Man logfiles."
243 :group 'org-export-man
244 :version "24.4"
245 :package-version '(Org . "8.0")
246 :type '(repeat (string :tag "Extension")))
248 (defcustom org-man-remove-logfiles t
249 "Non-nil means remove the logfiles produced by PDF production.
250 These are the .aux, .log, .out, and .toc files."
251 :group 'org-export-man
252 :version "24.4"
253 :package-version '(Org . "8.0")
254 :type 'boolean)
258 ;;; Internal Functions
260 (defun org-man--caption/label-string (element info)
261 "Return caption and label Man string for ELEMENT.
263 INFO is a plist holding contextual information. If there's no
264 caption nor label, return the empty string.
266 For non-floats, see `org-man--wrap-label'."
267 (let ((label (org-element-property :label element))
268 (main (org-export-get-caption element))
269 (short (org-export-get-caption element t)))
270 (cond ((and (not main) (not label)) "")
271 ((not main) (format "\\fI%s\\fP" label))
272 ;; Option caption format with short name.
273 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
274 (org-export-data short info)
275 (org-export-data main info)))
276 ;; Standard caption format.
277 (t (format "\\fR%s\\fP" (org-export-data main info))))))
279 (defun org-man--wrap-label (element output)
280 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
281 This function shouldn't be used for floats. See
282 `org-man--caption/label-string'."
283 (let ((label (org-element-property :name element)))
284 (if (or (not output) (not label) (string= output "") (string= label ""))
285 output
286 (concat (format "%s\n.br\n" label) output))))
290 ;;; Template
292 (defun org-man-template (contents info)
293 "Return complete document string after Man conversion.
294 CONTENTS is the transcoded contents string. INFO is a plist
295 holding export options."
296 (let* ((title (when (plist-get info :with-title)
297 (org-export-data (plist-get info :title) info)))
298 (attr (read (format "(%s)"
299 (mapconcat
300 #'identity
301 (list (plist-get info :man-class-options))
302 " "))))
303 (section-item (plist-get attr :section-id)))
305 (concat
307 (cond
308 ((and title (stringp section-item))
309 (format ".TH \"%s\" \"%s\" \n" title section-item))
310 ((and (string= "" title) (stringp section-item))
311 (format ".TH \"%s\" \"%s\" \n" " " section-item))
312 (title
313 (format ".TH \"%s\" \"1\" \n" title))
315 ".TH \" \" \"1\" "))
316 contents)))
321 ;;; Transcode Functions
323 ;;; Babel Call
325 ;; Babel Calls are ignored.
328 ;;; Bold
330 (defun org-man-bold (_bold contents _info)
331 "Transcode BOLD from Org to Man.
332 CONTENTS is the text with bold markup. INFO is a plist holding
333 contextual information."
334 (format "\\fB%s\\fP" contents))
337 ;;; Center Block
339 (defun org-man-center-block (center-block contents _info)
340 "Transcode a CENTER-BLOCK element from Org to Man.
341 CONTENTS holds the contents of the center block. INFO is a plist
342 holding contextual information."
343 (org-man--wrap-label
344 center-block
345 (format ".ce %d\n.nf\n%s\n.fi"
346 (- (length (split-string contents "\n")) 1 )
347 contents)))
350 ;;; Code
352 (defun org-man-code (code _contents _info)
353 "Transcode a CODE object from Org to Man.
354 CONTENTS is nil. INFO is a plist used as a communication
355 channel."
356 (format "\\fC%s\\fP" code))
359 ;;; Drawer
361 (defun org-man-drawer (_drawer contents _info)
362 "Transcode a DRAWER element from Org to Man.
363 DRAWER holds the drawer information
364 CONTENTS holds the contents of the block.
365 INFO is a plist holding contextual information. "
366 contents)
369 ;;; Dynamic Block
371 (defun org-man-dynamic-block (dynamic-block contents _info)
372 "Transcode a DYNAMIC-BLOCK element from Org to Man.
373 CONTENTS holds the contents of the block. INFO is a plist
374 holding contextual information. See `org-export-data'."
375 (org-man--wrap-label dynamic-block contents))
378 ;;; Entity
380 (defun org-man-entity (entity _contents _info)
381 "Transcode an ENTITY object from Org to Man.
382 CONTENTS are the definition itself. INFO is a plist holding
383 contextual information."
384 (org-element-property :utf-8 entity))
387 ;;; Example Block
389 (defun org-man-example-block (example-block _contents info)
390 "Transcode an EXAMPLE-BLOCK element from Org to Man.
391 CONTENTS is nil. INFO is a plist holding contextual
392 information."
393 (org-man--wrap-label
394 example-block
395 (format ".RS\n.nf\n%s\n.fi\n.RE"
396 (org-export-format-code-default example-block info))))
399 ;;; Export Block
401 (defun org-man-export-block (export-block _contents _info)
402 "Transcode a EXPORT-BLOCK element from Org to Man.
403 CONTENTS is nil. INFO is a plist holding contextual information."
404 (when (string= (org-element-property :type export-block) "MAN")
405 (org-remove-indentation (org-element-property :value export-block))))
408 ;;; Export Snippet
410 (defun org-man-export-snippet (export-snippet _contents _info)
411 "Transcode a EXPORT-SNIPPET object from Org to Man.
412 CONTENTS is nil. INFO is a plist holding contextual information."
413 (when (eq (org-export-snippet-backend export-snippet) 'man)
414 (org-element-property :value export-snippet)))
417 ;;; Fixed Width
419 (defun org-man-fixed-width (fixed-width _contents _info)
420 "Transcode a FIXED-WIDTH element from Org to Man.
421 CONTENTS is nil. INFO is a plist holding contextual information."
422 (org-man--wrap-label
423 fixed-width
424 (format "\\fC\n%s\\fP"
425 (org-remove-indentation
426 (org-element-property :value fixed-width)))))
429 ;;; Footnote Definition
431 ;; Footnote Definitions are ignored.
433 ;;; Footnote References
435 ;; Footnote References are Ignored
438 ;;; Headline
440 (defun org-man-headline (headline contents info)
441 "Transcode a HEADLINE element from Org to Man.
442 CONTENTS holds the contents of the headline. INFO is a plist
443 holding contextual information."
444 (let* ((level (org-export-get-relative-level headline info))
445 ;; Section formatting will set two placeholders: one for the
446 ;; title and the other for the contents.
447 (section-fmt
448 (case level
449 (1 ".SH \"%s\"\n%s")
450 (2 ".SS \"%s\"\n%s")
451 (3 ".SS \"%s\"\n%s")
452 (t nil)))
453 (text (org-export-data (org-element-property :title headline) info)))
455 (cond
456 ;; Case 1: This is a footnote section: ignore it.
457 ((org-element-property :footnote-section-p headline) nil)
459 ;; Case 2. This is a deep sub-tree: export it as a list item.
460 ;; Also export as items headlines for which no section
461 ;; format has been found.
462 ((or (not section-fmt) (org-export-low-level-p headline info))
463 ;; Build the real contents of the sub-tree.
464 (let ((low-level-body
465 (concat
466 ;; If the headline is the first sibling, start a list.
467 (when (org-export-first-sibling-p headline info)
468 (format "%s\n" ".RS"))
469 ;; Itemize headline
470 ".TP\n.ft I\n" text "\n.ft\n"
471 contents ".RE")))
472 ;; If headline is not the last sibling simply return
473 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
474 ;; blank line.
475 (if (not (org-export-last-sibling-p headline info)) low-level-body
476 (replace-regexp-in-string
477 "[ \t\n]*\\'" ""
478 low-level-body))))
480 ;; Case 3. Standard headline. Export it as a section.
481 (t (format section-fmt text contents )))))
483 ;;; Horizontal Rule
484 ;; Not supported
486 ;;; Inline Babel Call
488 ;; Inline Babel Calls are ignored.
490 ;;; Inline Src Block
492 (defun org-man-inline-src-block (inline-src-block _contents info)
493 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
494 CONTENTS holds the contents of the item. INFO is a plist holding
495 contextual information."
496 (let* ((code (org-element-property :value inline-src-block)))
497 (cond
498 ((plist-get info :man-source-highlight)
499 (let* ((tmpdir (if (featurep 'xemacs)
500 temp-directory
501 temporary-file-directory ))
502 (in-file (make-temp-name
503 (expand-file-name "srchilite" tmpdir)))
504 (out-file (make-temp-name
505 (expand-file-name "reshilite" tmpdir)))
506 (org-lang (org-element-property :language inline-src-block))
507 (lst-lang
508 (cadr (assq (intern org-lang)
509 (plist-get info :man-source-highlight-langs))))
511 (cmd (concat (expand-file-name "source-highlight")
512 " -s " lst-lang
513 " -f groff_man"
514 " -i " in-file
515 " -o " out-file )))
517 (if lst-lang
518 (let ((code-block "" ))
519 (with-temp-file in-file (insert code))
520 (shell-command cmd)
521 (setq code-block (org-file-contents out-file))
522 (delete-file in-file)
523 (delete-file out-file)
524 code-block)
525 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
526 code))))
528 ;; Do not use a special package: transcode it verbatim.
530 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
531 "\\fP\n.fi\n.RE\n")))))
534 ;;; Inlinetask
535 ;;; Italic
537 (defun org-man-italic (_italic contents _info)
538 "Transcode ITALIC from Org to Man.
539 CONTENTS is the text with italic markup. INFO is a plist holding
540 contextual information."
541 (format "\\fI%s\\fP" contents))
544 ;;; Item
547 (defun org-man-item (item contents info)
549 "Transcode an ITEM element from Org to Man.
550 CONTENTS holds the contents of the item. INFO is a plist holding
551 contextual information."
553 (let* ((bullet (org-element-property :bullet item))
554 (type (org-element-property :type (org-element-property :parent item)))
555 (checkbox (case (org-element-property :checkbox item)
556 (on "\\o'\\(sq\\(mu'") ;;
557 (off "\\(sq ") ;;
558 (trans "\\o'\\(sq\\(mi'" ))) ;;
560 (tag (let ((tag (org-element-property :tag item)))
561 ;; Check-boxes must belong to the tag.
562 (and tag (format "\\fB%s\\fP"
563 (concat checkbox
564 (org-export-data tag info)))))))
566 (if (and (null tag )
567 (null checkbox))
568 (let* ((bullet (org-trim bullet))
569 (marker (cond ((string= "-" bullet) "\\(em")
570 ((string= "*" bullet) "\\(bu")
571 ((eq type 'ordered)
572 (format "%s " (org-trim bullet)))
573 (t "\\(dg"))))
574 (concat ".IP " marker " 4\n"
575 (org-trim (or contents " " ))))
576 ; else
577 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
578 (org-trim (or contents " " ))))))
580 ;;; Keyword
583 (defun org-man-keyword (keyword _contents _info)
584 "Transcode a KEYWORD element from Org to Man.
585 CONTENTS is nil. INFO is a plist holding contextual information."
586 (let ((key (org-element-property :key keyword))
587 (value (org-element-property :value keyword)))
588 (cond
589 ((string= key "MAN") value)
590 ((string= key "INDEX") nil)
591 ((string= key "TOC" ) nil))))
594 ;;; Line Break
596 (defun org-man-line-break (_line-break _contents _info)
597 "Transcode a LINE-BREAK object from Org to Man.
598 CONTENTS is nil. INFO is a plist holding contextual information."
599 ".br\n")
602 ;;; Link
605 (defun org-man-link (link desc _info)
606 "Transcode a LINK object from Org to Man.
608 DESC is the description part of the link, or the empty string.
609 INFO is a plist holding contextual information. See
610 `org-export-data'."
611 (let* ((type (org-element-property :type link))
612 (raw-path (org-element-property :path link))
613 ;; Ensure DESC really exists, or set it to nil.
614 (desc (and (not (string= desc "")) desc))
615 (path (cond
616 ((member type '("http" "https" "ftp" "mailto"))
617 (concat type ":" raw-path))
618 ((string= type "file") (org-export-file-uri raw-path))
619 (t raw-path))))
620 (cond
621 ;; Link type is handled by a special function.
622 ((org-export-custom-protocol-maybe link desc 'man))
623 ;; External link with a description part.
624 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
625 ;; External link without a description part.
626 (path (format "\\fI%s\\fP" path))
627 ;; No path, only description. Try to do something useful.
628 (t (format "\\fI%s\\fP" desc)))))
630 ;;;; Node Property
632 (defun org-man-node-property (node-property _contents _info)
633 "Transcode a NODE-PROPERTY element from Org to Man.
634 CONTENTS is nil. INFO is a plist holding contextual
635 information."
636 (format "%s:%s"
637 (org-element-property :key node-property)
638 (let ((value (org-element-property :value node-property)))
639 (if value (concat " " value) ""))))
641 ;;; Paragraph
643 (defun org-man-paragraph (paragraph contents _info)
644 "Transcode a PARAGRAPH element from Org to Man.
645 CONTENTS is the contents of the paragraph, as a string. INFO is
646 the plist used as a communication channel."
647 (let ((parent (plist-get (nth 1 paragraph) :parent)))
648 (when parent
649 (let ((parent-type (car parent))
650 (fixed-paragraph ""))
651 (cond ((and (eq parent-type 'item)
652 (plist-get (nth 1 parent) :bullet ))
653 (setq fixed-paragraph (concat "" contents)))
654 ((eq parent-type 'section)
655 (setq fixed-paragraph (concat ".PP\n" contents)))
656 ((eq parent-type 'footnote-definition)
657 (setq fixed-paragraph contents))
658 (t (setq fixed-paragraph (concat "" contents))))
659 fixed-paragraph ))))
662 ;;; Plain List
664 (defun org-man-plain-list (_plain-list contents _info)
665 "Transcode a PLAIN-LIST element from Org to Man.
666 CONTENTS is the contents of the list. INFO is a plist holding
667 contextual information."
668 contents)
670 ;;; Plain Text
672 (defun org-man-plain-text (text info)
673 "Transcode a TEXT string from Org to Man.
674 TEXT is the string to transcode. INFO is a plist holding
675 contextual information."
676 (let ((output text))
677 ;; Protect various chars.
678 (setq output (replace-regexp-in-string
679 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
680 "$\\" output nil t 1))
681 ;; Activate smart quotes. Be sure to provide original TEXT string
682 ;; since OUTPUT may have been modified.
683 (when (plist-get info :with-smart-quotes)
684 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
685 ;; Handle break preservation if required.
686 (when (plist-get info :preserve-breaks)
687 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
688 output)))
689 ;; Return value.
690 output))
694 ;;; Planning
697 ;;; Property Drawer
699 (defun org-man-property-drawer (_property-drawer contents _info)
700 "Transcode a PROPERTY-DRAWER element from Org to Man.
701 CONTENTS holds the contents of the drawer. INFO is a plist
702 holding contextual information."
703 (and (org-string-nw-p contents)
704 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
706 ;;; Quote Block
708 (defun org-man-quote-block (quote-block contents _info)
709 "Transcode a QUOTE-BLOCK element from Org to Man.
710 CONTENTS holds the contents of the block. INFO is a plist
711 holding contextual information."
712 (org-man--wrap-label
713 quote-block
714 (format ".RS\n%s\n.RE" contents)))
717 ;;; Radio Target
719 (defun org-man-radio-target (_radio-target text _info)
720 "Transcode a RADIO-TARGET object from Org to Man.
721 TEXT is the text of the target. INFO is a plist holding
722 contextual information."
723 text)
726 ;;; Section
728 (defun org-man-section (_section contents _info)
729 "Transcode a SECTION element from Org to Man.
730 CONTENTS holds the contents of the section. INFO is a plist
731 holding contextual information."
732 contents)
735 ;;; Special Block
737 (defun org-man-special-block (special-block contents _info)
738 "Transcode a SPECIAL-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 special-block (format "%s\n" contents)))
744 ;;; Src Block
746 (defun org-man-src-block (src-block _contents info)
747 "Transcode a SRC-BLOCK element from Org to Man.
748 CONTENTS holds the contents of the item. INFO is a plist holding
749 contextual information."
750 (if (not (plist-get info :man-source-highlight))
751 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
752 (org-export-format-code-default src-block info))
753 (let* ((tmpdir temporary-file-directory)
754 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
755 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
756 (code (org-element-property :value src-block))
757 (org-lang (org-element-property :language src-block))
758 (lst-lang
759 (cadr (assq (intern org-lang)
760 (plist-get info :man-source-highlight-langs))))
761 (cmd (concat "source-highlight"
762 " -s " lst-lang
763 " -f groff_man "
764 " -i " in-file
765 " -o " out-file)))
766 (if lst-lang
767 (let ((code-block ""))
768 (with-temp-file in-file (insert code))
769 (shell-command cmd)
770 (setq code-block (org-file-contents out-file))
771 (delete-file in-file)
772 (delete-file out-file)
773 code-block)
774 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))
777 ;;; Statistics Cookie
779 (defun org-man-statistics-cookie (statistics-cookie _contents _info)
780 "Transcode a STATISTICS-COOKIE object from Org to Man.
781 CONTENTS is nil. INFO is a plist holding contextual information."
782 (org-element-property :value statistics-cookie))
785 ;;; Strike-Through
787 (defun org-man-strike-through (_strike-through contents _info)
788 "Transcode STRIKE-THROUGH from Org to Man.
789 CONTENTS is the text with strike-through markup. INFO is a plist
790 holding contextual information."
791 (format "\\fI%s\\fP" contents))
793 ;;; Subscript
795 (defun org-man-subscript (_subscript contents _info)
796 "Transcode a SUBSCRIPT object from Org to Man.
797 CONTENTS is the contents of the object. INFO is a plist holding
798 contextual information."
799 (format "\\d\\s-2%s\\s+2\\u" contents))
801 ;;; Superscript "^_%s$
803 (defun org-man-superscript (_superscript contents _info)
804 "Transcode a SUPERSCRIPT object from Org to Man.
805 CONTENTS is the contents of the object. INFO is a plist holding
806 contextual information."
807 (format "\\u\\s-2%s\\s+2\\d" contents))
810 ;;; Table
812 ;; `org-man-table' is the entry point for table transcoding. It
813 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
814 ;; delegates the job to either `org-man-table--table.el-table' or
815 ;; `org-man-table--org-table' functions, depending of the type of
816 ;; the table.
818 ;; `org-man-table--align-string' is a subroutine used to build
819 ;; alignment string for Org tables.
821 (defun org-man-table (table contents info)
822 "Transcode a TABLE element from Org to Man.
823 CONTENTS is the contents of the table. INFO is a plist holding
824 contextual information."
825 (cond
826 ;; Case 1: verbatim table.
827 ((or (plist-get info :man-tables-verbatim)
828 (let ((attr (read (format "(%s)"
829 (mapconcat
830 #'identity
831 (org-element-property :attr_man table)
832 " ")))))
834 (and attr (plist-get attr :verbatim))))
836 (format ".nf\n\\fC%s\\fP\n.fi"
837 ;; Re-create table, without affiliated keywords.
838 (org-trim
839 (org-element-interpret-data
840 `(table nil ,@(org-element-contents table))))))
841 ;; Case 2: Standard table.
842 (t (org-man-table--org-table table contents info))))
844 (defun org-man-table--align-string (divider table info)
845 "Return an appropriate Man alignment string.
846 TABLE is the considered table. INFO is a plist used as
847 a communication channel."
848 (let (alignment)
849 ;; Extract column groups and alignment from first (non-rule) row.
850 (org-element-map
851 (org-element-map table 'table-row
852 (lambda (row)
853 (and (eq (org-element-property :type row) 'standard) row))
854 info 'first-match)
855 'table-cell
856 (lambda (cell)
857 (let* ((borders (org-export-table-cell-borders cell info))
858 (raw-width (org-export-table-cell-width cell info))
859 (width-cm (when raw-width (/ raw-width 5)))
860 (width (if raw-width (format "w(%dc)"
861 (if (< width-cm 1) 1 width-cm)) "")))
862 ;; Check left border for the first cell only.
863 (when (and (memq 'left borders) (not alignment))
864 (push "|" alignment))
865 (push
866 (case (org-export-table-cell-alignment cell info)
867 (left (concat "l" width divider))
868 (right (concat "r" width divider))
869 (center (concat "c" width divider)))
870 alignment)
871 (when (memq 'right borders) (push "|" alignment))))
872 info)
873 (apply 'concat (reverse alignment))))
875 (defun org-man-table--org-table (table contents info)
876 "Return appropriate Man code for an Org table.
878 TABLE is the table type element to transcode. CONTENTS is its
879 contents, as a string. INFO is a plist used as a communication
880 channel.
882 This function assumes TABLE has `org' as its `:type' attribute."
883 (let* ((attr (org-export-read-attribute :attr_man table))
884 (caption (and (not (plist-get attr :disable-caption))
885 (org-man--caption/label-string table info)))
886 (divider (if (plist-get attr :divider) "|" " "))
888 ;; Determine alignment string.
889 (alignment (org-man-table--align-string divider table info))
890 ;; Extract others display options.
892 (lines (org-split-string contents "\n"))
894 (attr-list
895 (delq nil
896 (list
897 (and (plist-get attr :expand) "expand")
898 (let ((placement (plist-get attr :placement)))
899 (cond ((string= placement 'center) "center")
900 ((string= placement 'left) nil)
901 ((plist-get info :man-tables-centered) "center")
902 (t "")))
903 (or (plist-get attr :boxtype) "box"))))
905 (title-line (plist-get attr :title-line))
906 (long-cells (plist-get attr :long-cells))
908 (table-format (concat
909 (format "%s" (or (car attr-list) "" ))
911 (let ((output-list '()))
912 (when (cdr attr-list)
913 (dolist (attr-item (cdr attr-list))
914 (setq output-list (concat output-list (format ",%s" attr-item)))))
915 output-list)
916 "")))
918 (first-line (when lines (org-split-string (car lines) "\t"))))
919 ;; Prepare the final format string for the table.
922 (cond
923 ;; Others.
924 (lines (concat ".TS\n " table-format ";\n"
926 (format "%s.\n"
927 (let ((final-line ""))
928 (when title-line
929 (dotimes (_ (length first-line))
930 (setq final-line (concat final-line "cb" divider))))
932 (setq final-line (concat final-line "\n"))
934 (if alignment
935 (setq final-line (concat final-line alignment))
936 (dotimes (_ (length first-line))
937 (setq final-line (concat final-line "c" divider))))
938 final-line ))
940 (format "%s.TE\n"
941 (let ((final-line "")
942 (long-line "")
943 (lines (org-split-string contents "\n")))
945 (dolist (line-item lines)
946 (setq long-line "")
948 (if long-cells
949 (progn
950 (if (string= line-item "_")
951 (setq long-line (format "%s\n" line-item))
952 ;; else string =
953 (let ((cell-item-list (org-split-string line-item "\t")))
954 (dolist (cell-item cell-item-list)
956 (cond ((eq cell-item (car (last cell-item-list)))
957 (setq long-line (concat long-line
958 (format "T{\n%s\nT}\t\n" cell-item ))))
960 (setq long-line (concat long-line
961 (format "T{\n%s\nT}\t" cell-item ))))))
962 long-line))
963 ;; else long cells
964 (setq final-line (concat final-line long-line )))
966 (setq final-line (concat final-line line-item "\n"))))
967 final-line))
969 (and caption (format ".TB \"%s\"" caption)))))))
971 ;;; Table Cell
973 (defun org-man-table-cell (table-cell contents info)
974 "Transcode a TABLE-CELL element from Org to Man
975 CONTENTS is the cell contents. INFO is a plist used as
976 a communication channel."
977 (concat
978 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
979 (if (and contents
980 scientific-format
981 (string-match orgtbl-exp-regexp contents))
982 ;; Use appropriate format string for scientific notation.
983 (format scientific-format
984 (match-string 1 contents)
985 (match-string 2 contents))
986 contents))
987 (when (org-export-get-next-element table-cell info) "\t")))
990 ;;; Table Row
992 (defun org-man-table-row (table-row contents info)
993 "Transcode a TABLE-ROW element from Org to Man.
994 CONTENTS is the contents of the row. INFO is a plist used as
995 a communication channel."
996 ;; Rules are ignored since table separators are deduced from borders
997 ;; of the current row.
998 (when (eq (org-element-property :type table-row) 'standard)
999 (let ((borders
1000 ;; TABLE-ROW's borders are extracted from its first cell.
1001 (org-export-table-cell-borders
1002 (car (org-element-contents table-row)) info)))
1003 (concat
1004 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1005 contents
1006 (cond ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1007 ((memq 'below borders) "\n_"))))))
1010 ;;; Target
1012 (defun org-man-target (target _contents info)
1013 "Transcode a TARGET object from Org to Man.
1014 CONTENTS is nil. INFO is a plist holding contextual
1015 information."
1016 (format "\\fI%s\\fP" (org-export-get-reference target info)))
1019 ;;; Timestamp
1021 (defun org-man-timestamp (_timestamp _contents _info)
1022 "Transcode a TIMESTAMP object from Org to Man.
1023 ONTENTS is nil. INFO is a plist holding contextual information."
1027 ;;; Underline
1029 (defun org-man-underline (_underline contents _info)
1030 "Transcode UNDERLINE from Org to Man.
1031 CONTENTS is the text with underline markup. INFO is a plist
1032 holding contextual information."
1033 (format "\\fI%s\\fP" contents))
1036 ;;; Verbatim
1038 (defun org-man-verbatim (_verbatim contents _info)
1039 "Transcode a VERBATIM object from Org to Man.
1040 CONTENTS is nil. INFO is a plist used as a communication
1041 channel."
1042 (format ".nf\n%s\n.fi" contents))
1045 ;;; Verse Block
1047 (defun org-man-verse-block (_verse-block contents _info)
1048 "Transcode a VERSE-BLOCK element from Org to Man.
1049 CONTENTS is verse block contents. INFO is a plist holding
1050 contextual information."
1051 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1055 ;;; Interactive functions
1057 (defun org-man-export-to-man
1058 (&optional async subtreep visible-only body-only ext-plist)
1059 "Export current buffer to a Man file.
1061 If narrowing is active in the current buffer, only export its
1062 narrowed part.
1064 If a region is active, export that region.
1066 A non-nil optional argument ASYNC means the process should happen
1067 asynchronously. The resulting file should be accessible through
1068 the `org-export-stack' interface.
1070 When optional argument SUBTREEP is non-nil, export the sub-tree
1071 at point, extracting information from the headline properties
1072 first.
1074 When optional argument VISIBLE-ONLY is non-nil, don't export
1075 contents of hidden elements.
1077 When optional argument BODY-ONLY is non-nil, only the body
1078 without any markers.
1080 EXT-PLIST, when provided, is a property list with external
1081 parameters overriding Org default settings, but still inferior to
1082 file-local settings.
1084 Return output file's name."
1085 (interactive)
1086 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1087 (org-export-to-file 'man outfile
1088 async subtreep visible-only body-only ext-plist)))
1090 (defun org-man-export-to-pdf
1091 (&optional async subtreep visible-only body-only ext-plist)
1092 "Export current buffer to Groff then process through to PDF.
1094 If narrowing is active in the current buffer, only export its
1095 narrowed part.
1097 If a region is active, export that region.
1099 A non-nil optional argument ASYNC means the process should happen
1100 asynchronously. The resulting file should be accessible through
1101 the `org-export-stack' interface.
1103 When optional argument SUBTREEP is non-nil, export the sub-tree
1104 at point, extracting information from the headline properties
1105 first.
1107 When optional argument VISIBLE-ONLY is non-nil, don't export
1108 contents of hidden elements.
1110 When optional argument BODY-ONLY is non-nil, only write between
1111 markers.
1113 EXT-PLIST, when provided, is a property list with external
1114 parameters overriding Org default settings, but still inferior to
1115 file-local settings.
1117 Return PDF file's name."
1118 (interactive)
1119 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1120 (org-export-to-file 'man outfile
1121 async subtreep visible-only body-only ext-plist
1122 (lambda (file) (org-latex-compile file)))))
1124 (defun org-man-compile (file)
1125 "Compile a Groff file.
1127 FILE is the name of the file being compiled. Processing is done
1128 through the command specified in `org-man-pdf-process'.
1130 Return PDF file name or an error if it couldn't be produced."
1131 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1132 (full-name (file-truename file))
1133 (out-dir (file-name-directory file))
1134 ;; Properly set working directory for compilation.
1135 (default-directory (if (file-name-absolute-p file)
1136 (file-name-directory full-name)
1137 default-directory))
1138 errors)
1139 (message "Processing Groff file %s..." file)
1140 (save-window-excursion
1141 (cond
1142 ;; A function is provided: Apply it.
1143 ((functionp org-man-pdf-process)
1144 (funcall org-man-pdf-process (shell-quote-argument file)))
1145 ;; A list is provided: Replace %b, %f and %o with appropriate
1146 ;; values in each command before applying it. Output is
1147 ;; redirected to "*Org PDF Groff Output*" buffer.
1148 ((consp org-man-pdf-process)
1149 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1150 (mapc
1151 (lambda (command)
1152 (shell-command
1153 (replace-regexp-in-string
1154 "%b" (shell-quote-argument base-name)
1155 (replace-regexp-in-string
1156 "%f" (shell-quote-argument full-name)
1157 (replace-regexp-in-string
1158 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1159 outbuf))
1160 org-man-pdf-process)
1161 ;; Collect standard errors from output buffer.
1162 (setq errors (org-man-collect-errors outbuf))))
1163 (t (error "No valid command to process to PDF")))
1164 (let ((pdffile (concat out-dir base-name ".pdf")))
1165 ;; Check for process failure. Provide collected errors if
1166 ;; possible.
1167 (if (not (file-exists-p pdffile))
1168 (error "PDF file %s wasn't produced%s" pdffile
1169 (if errors (concat ": " errors) ""))
1170 ;; Else remove log files, when specified, and signal end of
1171 ;; process to user, along with any error encountered.
1172 (when org-man-remove-logfiles
1173 (dolist (ext org-man-logfiles-extensions)
1174 (let ((file (concat out-dir base-name "." ext)))
1175 (when (file-exists-p file) (delete-file file)))))
1176 (message (concat "Process completed"
1177 (if (not errors) "."
1178 (concat " with errors: " errors)))))
1179 ;; Return output file name.
1180 pdffile))))
1182 (defun org-man-collect-errors (buffer)
1183 "Collect some kind of errors from \"groff\" output
1184 BUFFER is the buffer containing output.
1185 Return collected error types as a string, or nil if there was
1186 none."
1187 (with-current-buffer buffer
1188 (save-excursion
1189 (goto-char (point-max))
1190 ;; Find final run
1191 nil )))
1194 (provide 'ox-man)
1196 ;; Local variables:
1197 ;; generated-autoload-file: "org-loaddefs.el"
1198 ;; End:
1200 ;;; ox-man.el ends here