1 ;; ox-man.el --- Man Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2016 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/>.
26 ;; This library implements a Man back-end for Org generic exporter.
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".
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
)
52 (org-export-define-backend 'man
53 '((babel-call . org-man-babel-call
)
55 (center-block . org-man-center-block
)
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
)
73 (keyword . org-man-keyword
)
74 (line-break . org-man-line-break
)
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
))
102 ((?m
"As MAN file" org-man-export-to-man
)
103 (?p
"As PDF file" org-man-export-to-pdf
)
104 (?o
"As PDF file and open"
106 (if a
(org-man-export-to-pdf t s v b
)
107 (org-open-file (org-man-export-to-pdf nil s v b
)))))))
109 '((:man-class
"MAN_CLASS" nil nil t
)
110 (:man-class-options
"MAN_CLASS_OPTIONS" nil nil t
)
111 (:man-header-extra
"MAN_HEADER" nil nil newline
)
113 (:man-tables-centered nil nil org-man-tables-centered
)
114 (:man-tables-verbatim nil nil org-man-tables-verbatim
)
115 (:man-table-scientific-notation nil nil org-man-table-scientific-notation
)
116 (:man-source-highlight nil nil org-man-source-highlight
)
117 (:man-source-highlight-langs nil nil org-man-source-highlight-langs
)))
121 ;;; User Configurable Variables
123 (defgroup org-export-man nil
124 "Options for exporting Org mode files to Man."
125 :tag
"Org Export Man"
130 (defcustom org-man-tables-centered t
131 "When non-nil, tables are exported in a center environment."
132 :group
'org-export-man
134 :package-version
'(Org .
"8.0")
137 (defcustom org-man-tables-verbatim nil
138 "When non-nil, tables are exported verbatim."
139 :group
'org-export-man
141 :package-version
'(Org .
"8.0")
145 (defcustom org-man-table-scientific-notation
"%sE%s"
146 "Format string to display numbers in scientific notation.
147 The format should have \"%s\" twice, for mantissa and exponent
148 \(i.e. \"%s\\\\times10^{%s}\").
150 When nil, no transformation is made."
151 :group
'org-export-man
153 :package-version
'(Org .
"8.0")
155 (string :tag
"Format string")
156 (const :tag
"No formatting")))
162 (defcustom org-man-source-highlight nil
163 "Use GNU source highlight to embellish source blocks "
164 :group
'org-export-man
166 :package-version
'(Org .
"8.0")
170 (defcustom org-man-source-highlight-langs
171 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
173 (c "c") (cc "cpp") (csharp "csharp") (d "d")
174 (fortran "fortran") (cobol "cobol") (pascal "pascal")
175 (ada "ada") (asm "asm")
176 (perl "perl") (cperl "perl")
177 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
178 (java "java") (javascript "javascript")
180 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
181 (ocaml "caml") (caml "caml")
182 (sql "sql") (sqlite "sql")
183 (html "html") (css "css") (xml "xml")
184 (bat "bat") (bison "bison") (clipper "clipper")
185 (ldap "ldap") (opa "opa")
186 (php "php") (postscript "postscript") (prolog "prolog")
187 (properties "properties") (makefile "makefile")
188 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
189 "Alist mapping languages to their listing language counterpart.
190 The key is a symbol, the major mode symbol without the \"-mode\".
191 The value is the string that should be inserted as the language
192 parameter for the listings package. If the mode name and the
193 listings name are the same, the language does not need an entry
194 in this list - but it does not hurt if it is present."
195 :group
'org-export-man
197 :package-version
'(Org .
"8.0")
200 (symbol :tag
"Major mode ")
201 (string :tag
"Listings language"))))
206 (defcustom org-man-pdf-process
207 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
208 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
209 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
211 "Commands to process a Man file to a PDF file.
212 This is a list of strings, each of them will be given to the
213 shell as a command. %f in the command will be replaced by the
214 full file name, %b by the file base name (i.e. without directory
215 and extension parts) and %o by the base directory of the file.
218 By default, Org uses 3 runs of to do the processing.
220 Alternatively, this may be a Lisp function that does the
221 processing. This function should accept the file name as
222 its single argument."
223 :group
'org-export-pdf
224 :group
'org-export-man
226 :package-version
'(Org .
"8.0")
228 (repeat :tag
"Shell command sequence"
229 (string :tag
"Shell command"))
230 (const :tag
"2 runs of pdfgroff"
231 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
232 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
233 (const :tag
"3 runs of pdfgroff"
234 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
235 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
236 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
239 (defcustom org-man-logfiles-extensions
241 "The list of file extensions to consider as Man logfiles."
242 :group
'org-export-man
244 :package-version
'(Org .
"8.0")
245 :type
'(repeat (string :tag
"Extension")))
247 (defcustom org-man-remove-logfiles t
248 "Non-nil means remove the logfiles produced by PDF production.
249 These are the .aux, .log, .out, and .toc files."
250 :group
'org-export-man
252 :package-version
'(Org .
"8.0")
257 ;;; Internal Functions
259 (defun org-man--caption/label-string
(element info
)
260 "Return caption and label Man string for ELEMENT.
262 INFO is a plist holding contextual information. If there's no
263 caption nor label, return the empty string.
265 For non-floats, see `org-man--wrap-label'."
266 (let ((label (org-element-property :label element
))
267 (main (org-export-get-caption element
))
268 (short (org-export-get-caption element t
)))
269 (cond ((and (not main
) (not label
)) "")
270 ((not main
) (format "\\fI%s\\fP" label
))
271 ;; Option caption format with short name.
272 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
273 (org-export-data short info
)
274 (org-export-data main info
)))
275 ;; Standard caption format.
276 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
278 (defun org-man--wrap-label (element output
)
279 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
280 This function shouldn't be used for floats. See
281 `org-man--caption/label-string'."
282 (let ((label (org-element-property :name element
)))
283 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
285 (concat (format "%s\n.br\n" label
) output
))))
291 (defun org-man-template (contents info
)
292 "Return complete document string after Man conversion.
293 CONTENTS is the transcoded contents string. INFO is a plist
294 holding export options."
295 (let* ((title (when (plist-get info
:with-title
)
296 (org-export-data (plist-get info
:title
) info
)))
297 (attr (read (format "(%s)"
300 (list (plist-get info
:man-class-options
))
302 (section-item (plist-get attr
:section-id
)))
307 ((and title
(stringp section-item
))
308 (format ".TH \"%s\" \"%s\" \n" title section-item
))
309 ((and (string= "" title
) (stringp section-item
))
310 (format ".TH \"%s\" \"%s\" \n" " " section-item
))
312 (format ".TH \"%s\" \"1\" \n" title
))
320 ;;; Transcode Functions
324 ;; Babel Calls are ignored.
329 (defun org-man-bold (_bold contents _info
)
330 "Transcode BOLD from Org to Man.
331 CONTENTS is the text with bold markup. INFO is a plist holding
332 contextual information."
333 (format "\\fB%s\\fP" contents
))
338 (defun org-man-center-block (center-block contents _info
)
339 "Transcode a CENTER-BLOCK element from Org to Man.
340 CONTENTS holds the contents of the center block. INFO is a plist
341 holding contextual information."
344 (format ".ce %d\n.nf\n%s\n.fi"
345 (- (length (split-string contents
"\n")) 1 )
351 (defun org-man-code (code _contents _info
)
352 "Transcode a CODE object from Org to Man.
353 CONTENTS is nil. INFO is a plist used as a communication
355 (format "\\fC%s\\fP" code
))
360 (defun org-man-drawer (_drawer contents _info
)
361 "Transcode a DRAWER element from Org to Man.
362 DRAWER holds the drawer information
363 CONTENTS holds the contents of the block.
364 INFO is a plist holding contextual information. "
370 (defun org-man-dynamic-block (dynamic-block contents _info
)
371 "Transcode a DYNAMIC-BLOCK element from Org to Man.
372 CONTENTS holds the contents of the block. INFO is a plist
373 holding contextual information. See `org-export-data'."
374 (org-man--wrap-label dynamic-block contents
))
379 (defun org-man-entity (entity _contents _info
)
380 "Transcode an ENTITY object from Org to Man.
381 CONTENTS are the definition itself. INFO is a plist holding
382 contextual information."
383 (org-element-property :utf-8 entity
))
388 (defun org-man-example-block (example-block _contents info
)
389 "Transcode an EXAMPLE-BLOCK element from Org to Man.
390 CONTENTS is nil. INFO is a plist holding contextual
394 (format ".RS\n.nf\n%s\n.fi\n.RE"
395 (org-export-format-code-default example-block info
))))
400 (defun org-man-export-block (export-block _contents _info
)
401 "Transcode a EXPORT-BLOCK element from Org to Man.
402 CONTENTS is nil. INFO is a plist holding contextual information."
403 (when (string= (org-element-property :type export-block
) "MAN")
404 (org-remove-indentation (org-element-property :value export-block
))))
409 (defun org-man-export-snippet (export-snippet _contents _info
)
410 "Transcode a EXPORT-SNIPPET object from Org to Man.
411 CONTENTS is nil. INFO is a plist holding contextual information."
412 (when (eq (org-export-snippet-backend export-snippet
) 'man
)
413 (org-element-property :value export-snippet
)))
418 (defun org-man-fixed-width (fixed-width _contents _info
)
419 "Transcode a FIXED-WIDTH element from Org to Man.
420 CONTENTS is nil. INFO is a plist holding contextual information."
423 (format "\\fC\n%s\\fP"
424 (org-remove-indentation
425 (org-element-property :value fixed-width
)))))
428 ;;; Footnote Definition
430 ;; Footnote Definitions are ignored.
432 ;;; Footnote References
434 ;; Footnote References are Ignored
439 (defun org-man-headline (headline contents info
)
440 "Transcode a HEADLINE element from Org to Man.
441 CONTENTS holds the contents of the headline. INFO is a plist
442 holding contextual information."
443 (let* ((level (org-export-get-relative-level headline info
))
444 ;; Section formatting will set two placeholders: one for the
445 ;; title and the other for the contents.
452 (text (org-export-data (org-element-property :title headline
) info
)))
455 ;; Case 1: This is a footnote section: ignore it.
456 ((org-element-property :footnote-section-p headline
) nil
)
458 ;; Case 2. This is a deep sub-tree: export it as a list item.
459 ;; Also export as items headlines for which no section
460 ;; format has been found.
461 ((or (not section-fmt
) (org-export-low-level-p headline info
))
462 ;; Build the real contents of the sub-tree.
463 (let ((low-level-body
465 ;; If the headline is the first sibling, start a list.
466 (when (org-export-first-sibling-p headline info
)
467 (format "%s\n" ".RS"))
469 ".TP\n.ft I\n" text
"\n.ft\n"
471 ;; If headline is not the last sibling simply return
472 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
474 (if (not (org-export-last-sibling-p headline info
)) low-level-body
475 (replace-regexp-in-string
479 ;; Case 3. Standard headline. Export it as a section.
480 (t (format section-fmt text contents
)))))
485 ;;; Inline Babel Call
487 ;; Inline Babel Calls are ignored.
491 (defun org-man-inline-src-block (inline-src-block _contents info
)
492 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
493 CONTENTS holds the contents of the item. INFO is a plist holding
494 contextual information."
495 (let* ((code (org-element-property :value inline-src-block
)))
497 ((plist-get info
:man-source-highlight
)
498 (let* ((tmpdir temporary-file-directory
)
499 (in-file (make-temp-name
500 (expand-file-name "srchilite" tmpdir
)))
501 (out-file (make-temp-name
502 (expand-file-name "reshilite" tmpdir
)))
503 (org-lang (org-element-property :language inline-src-block
))
505 (cadr (assq (intern org-lang
)
506 (plist-get info
:man-source-highlight-langs
))))
508 (cmd (concat (expand-file-name "source-highlight")
515 (let ((code-block "" ))
516 (with-temp-file in-file
(insert code
))
518 (setq code-block
(org-file-contents out-file
))
519 (delete-file in-file
)
520 (delete-file out-file
)
522 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
525 ;; Do not use a special package: transcode it verbatim.
527 (concat ".RS\n.nf\n" "\\fC" "\n" code
"\n"
528 "\\fP\n.fi\n.RE\n")))))
534 (defun org-man-italic (_italic contents _info
)
535 "Transcode ITALIC from Org to Man.
536 CONTENTS is the text with italic markup. INFO is a plist holding
537 contextual information."
538 (format "\\fI%s\\fP" contents
))
544 (defun org-man-item (item contents info
)
546 "Transcode an ITEM element from Org to Man.
547 CONTENTS holds the contents of the item. INFO is a plist holding
548 contextual information."
550 (let* ((bullet (org-element-property :bullet item
))
551 (type (org-element-property :type
(org-element-property :parent item
)))
552 (checkbox (case (org-element-property :checkbox item
)
553 (on "\\o'\\(sq\\(mu'") ;;
555 (trans "\\o'\\(sq\\(mi'" ))) ;;
557 (tag (let ((tag (org-element-property :tag item
)))
558 ;; Check-boxes must belong to the tag.
559 (and tag
(format "\\fB%s\\fP"
561 (org-export-data tag info
)))))))
565 (let* ((bullet (org-trim bullet
))
566 (marker (cond ((string= "-" bullet
) "\\(em")
567 ((string= "*" bullet
) "\\(bu")
569 (format "%s " (org-trim bullet
)))
571 (concat ".IP " marker
" 4\n"
572 (org-trim (or contents
" " ))))
574 (concat ".TP\n" (or tag
(concat " " checkbox
)) "\n"
575 (org-trim (or contents
" " ))))))
580 (defun org-man-keyword (keyword _contents _info
)
581 "Transcode a KEYWORD element from Org to Man.
582 CONTENTS is nil. INFO is a plist holding contextual information."
583 (let ((key (org-element-property :key keyword
))
584 (value (org-element-property :value keyword
)))
586 ((string= key
"MAN") value
)
587 ((string= key
"INDEX") nil
)
588 ((string= key
"TOC" ) nil
))))
593 (defun org-man-line-break (_line-break _contents _info
)
594 "Transcode a LINE-BREAK object from Org to Man.
595 CONTENTS is nil. INFO is a plist holding contextual information."
602 (defun org-man-link (link desc _info
)
603 "Transcode a LINK object from Org to Man.
605 DESC is the description part of the link, or the empty string.
606 INFO is a plist holding contextual information. See
608 (let* ((type (org-element-property :type link
))
609 (raw-path (org-element-property :path link
))
610 ;; Ensure DESC really exists, or set it to nil.
611 (desc (and (not (string= desc
"")) desc
))
613 ((member type
'("http" "https" "ftp" "mailto"))
614 (concat type
":" raw-path
))
615 ((string= type
"file") (org-export-file-uri raw-path
))
618 ;; Link type is handled by a special function.
619 ((org-export-custom-protocol-maybe link desc
'man
))
620 ;; External link with a description part.
621 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
622 ;; External link without a description part.
623 (path (format "\\fI%s\\fP" path
))
624 ;; No path, only description. Try to do something useful.
625 (t (format "\\fI%s\\fP" desc
)))))
629 (defun org-man-node-property (node-property _contents _info
)
630 "Transcode a NODE-PROPERTY element from Org to Man.
631 CONTENTS is nil. INFO is a plist holding contextual
634 (org-element-property :key node-property
)
635 (let ((value (org-element-property :value node-property
)))
636 (if value
(concat " " value
) ""))))
640 (defun org-man-paragraph (paragraph contents _info
)
641 "Transcode a PARAGRAPH element from Org to Man.
642 CONTENTS is the contents of the paragraph, as a string. INFO is
643 the plist used as a communication channel."
644 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
646 (let ((parent-type (car parent
))
647 (fixed-paragraph ""))
648 (cond ((and (eq parent-type
'item
)
649 (plist-get (nth 1 parent
) :bullet
))
650 (setq fixed-paragraph
(concat "" contents
)))
651 ((eq parent-type
'section
)
652 (setq fixed-paragraph
(concat ".PP\n" contents
)))
653 ((eq parent-type
'footnote-definition
)
654 (setq fixed-paragraph contents
))
655 (t (setq fixed-paragraph
(concat "" contents
))))
661 (defun org-man-plain-list (_plain-list contents _info
)
662 "Transcode a PLAIN-LIST element from Org to Man.
663 CONTENTS is the contents of the list. INFO is a plist holding
664 contextual information."
669 (defun org-man-plain-text (text info
)
670 "Transcode a TEXT string from Org to Man.
671 TEXT is the string to transcode. INFO is a plist holding
672 contextual information."
674 ;; Protect various chars.
675 (setq output
(replace-regexp-in-string
676 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
677 "$\\" output nil t
1))
678 ;; Activate smart quotes. Be sure to provide original TEXT string
679 ;; since OUTPUT may have been modified.
680 (when (plist-get info
:with-smart-quotes
)
681 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
682 ;; Handle break preservation if required.
683 (when (plist-get info
:preserve-breaks
)
684 (setq output
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
696 (defun org-man-property-drawer (_property-drawer contents _info
)
697 "Transcode a PROPERTY-DRAWER element from Org to Man.
698 CONTENTS holds the contents of the drawer. INFO is a plist
699 holding contextual information."
700 (and (org-string-nw-p contents
)
701 (format ".RS\n.nf\n%s\n.fi\n.RE" contents
)))
705 (defun org-man-quote-block (quote-block contents _info
)
706 "Transcode a QUOTE-BLOCK element from Org to Man.
707 CONTENTS holds the contents of the block. INFO is a plist
708 holding contextual information."
711 (format ".RS\n%s\n.RE" contents
)))
716 (defun org-man-radio-target (_radio-target text _info
)
717 "Transcode a RADIO-TARGET object from Org to Man.
718 TEXT is the text of the target. INFO is a plist holding
719 contextual information."
725 (defun org-man-section (_section contents _info
)
726 "Transcode a SECTION element from Org to Man.
727 CONTENTS holds the contents of the section. INFO is a plist
728 holding contextual information."
734 (defun org-man-special-block (special-block contents _info
)
735 "Transcode a SPECIAL-BLOCK element from Org to Man.
736 CONTENTS holds the contents of the block. INFO is a plist
737 holding contextual information."
738 (org-man--wrap-label special-block
(format "%s\n" contents
)))
743 (defun org-man-src-block (src-block _contents info
)
744 "Transcode a SRC-BLOCK element from Org to Man.
745 CONTENTS holds the contents of the item. INFO is a plist holding
746 contextual information."
747 (if (not (plist-get info
:man-source-highlight
))
748 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
749 (org-export-format-code-default src-block info
))
750 (let* ((tmpdir temporary-file-directory
)
751 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir
)))
752 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir
)))
753 (code (org-element-property :value src-block
))
754 (org-lang (org-element-property :language src-block
))
756 (cadr (assq (intern org-lang
)
757 (plist-get info
:man-source-highlight-langs
))))
758 (cmd (concat "source-highlight"
764 (let ((code-block ""))
765 (with-temp-file in-file
(insert code
))
767 (setq code-block
(org-file-contents out-file
))
768 (delete-file in-file
)
769 (delete-file out-file
)
771 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code
)))))
774 ;;; Statistics Cookie
776 (defun org-man-statistics-cookie (statistics-cookie _contents _info
)
777 "Transcode a STATISTICS-COOKIE object from Org to Man.
778 CONTENTS is nil. INFO is a plist holding contextual information."
779 (org-element-property :value statistics-cookie
))
784 (defun org-man-strike-through (_strike-through contents _info
)
785 "Transcode STRIKE-THROUGH from Org to Man.
786 CONTENTS is the text with strike-through markup. INFO is a plist
787 holding contextual information."
788 (format "\\fI%s\\fP" contents
))
792 (defun org-man-subscript (_subscript contents _info
)
793 "Transcode a SUBSCRIPT object from Org to Man.
794 CONTENTS is the contents of the object. INFO is a plist holding
795 contextual information."
796 (format "\\d\\s-2%s\\s+2\\u" contents
))
798 ;;; Superscript "^_%s$
800 (defun org-man-superscript (_superscript contents _info
)
801 "Transcode a SUPERSCRIPT object from Org to Man.
802 CONTENTS is the contents of the object. INFO is a plist holding
803 contextual information."
804 (format "\\u\\s-2%s\\s+2\\d" contents
))
809 ;; `org-man-table' is the entry point for table transcoding. It
810 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
811 ;; delegates the job to either `org-man-table--table.el-table' or
812 ;; `org-man-table--org-table' functions, depending of the type of
815 ;; `org-man-table--align-string' is a subroutine used to build
816 ;; alignment string for Org tables.
818 (defun org-man-table (table contents info
)
819 "Transcode a TABLE element from Org to Man.
820 CONTENTS is the contents of the table. INFO is a plist holding
821 contextual information."
823 ;; Case 1: verbatim table.
824 ((or (plist-get info
:man-tables-verbatim
)
825 (let ((attr (read (format "(%s)"
828 (org-element-property :attr_man table
)
831 (and attr
(plist-get attr
:verbatim
))))
833 (format ".nf\n\\fC%s\\fP\n.fi"
834 ;; Re-create table, without affiliated keywords.
836 (org-element-interpret-data
837 `(table nil
,@(org-element-contents table
))))))
838 ;; Case 2: Standard table.
839 (t (org-man-table--org-table table contents info
))))
841 (defun org-man-table--align-string (divider table info
)
842 "Return an appropriate Man alignment string.
843 TABLE is the considered table. INFO is a plist used as
844 a communication channel."
846 ;; Extract column groups and alignment from first (non-rule) row.
848 (org-element-map table
'table-row
850 (and (eq (org-element-property :type row
) 'standard
) row
))
854 (let* ((borders (org-export-table-cell-borders cell info
))
855 (raw-width (org-export-table-cell-width cell info
))
856 (width-cm (when raw-width
(/ raw-width
5)))
857 (width (if raw-width
(format "w(%dc)"
858 (if (< width-cm
1) 1 width-cm
)) "")))
859 ;; Check left border for the first cell only.
860 (when (and (memq 'left borders
) (not alignment
))
861 (push "|" alignment
))
863 (case (org-export-table-cell-alignment cell info
)
864 (left (concat "l" width divider
))
865 (right (concat "r" width divider
))
866 (center (concat "c" width divider
)))
868 (when (memq 'right borders
) (push "|" alignment
))))
870 (apply 'concat
(reverse alignment
))))
872 (defun org-man-table--org-table (table contents info
)
873 "Return appropriate Man code for an Org table.
875 TABLE is the table type element to transcode. CONTENTS is its
876 contents, as a string. INFO is a plist used as a communication
879 This function assumes TABLE has `org' as its `:type' attribute."
880 (let* ((attr (org-export-read-attribute :attr_man table
))
881 (caption (and (not (plist-get attr
:disable-caption
))
882 (org-man--caption/label-string table info
)))
883 (divider (if (plist-get attr
:divider
) "|" " "))
885 ;; Determine alignment string.
886 (alignment (org-man-table--align-string divider table info
))
887 ;; Extract others display options.
889 (lines (org-split-string contents
"\n"))
894 (and (plist-get attr
:expand
) "expand")
895 (let ((placement (plist-get attr
:placement
)))
896 (cond ((string= placement
'center
) "center")
897 ((string= placement
'left
) nil
)
898 ((plist-get info
:man-tables-centered
) "center")
900 (or (plist-get attr
:boxtype
) "box"))))
902 (title-line (plist-get attr
:title-line
))
903 (long-cells (plist-get attr
:long-cells
))
905 (table-format (concat
906 (format "%s" (or (car attr-list
) "" ))
908 (let ((output-list '()))
909 (when (cdr attr-list
)
910 (dolist (attr-item (cdr attr-list
))
911 (setq output-list
(concat output-list
(format ",%s" attr-item
)))))
915 (first-line (when lines
(org-split-string (car lines
) "\t"))))
916 ;; Prepare the final format string for the table.
921 (lines (concat ".TS\n " table-format
";\n"
924 (let ((final-line ""))
926 (dotimes (_ (length first-line
))
927 (setq final-line
(concat final-line
"cb" divider
))))
929 (setq final-line
(concat final-line
"\n"))
932 (setq final-line
(concat final-line alignment
))
933 (dotimes (_ (length first-line
))
934 (setq final-line
(concat final-line
"c" divider
))))
938 (let ((final-line "")
940 (lines (org-split-string contents
"\n")))
942 (dolist (line-item lines
)
947 (if (string= line-item
"_")
948 (setq long-line
(format "%s\n" line-item
))
950 (let ((cell-item-list (org-split-string line-item
"\t")))
951 (dolist (cell-item cell-item-list
)
953 (cond ((eq cell-item
(car (last cell-item-list
)))
954 (setq long-line
(concat long-line
955 (format "T{\n%s\nT}\t\n" cell-item
))))
957 (setq long-line
(concat long-line
958 (format "T{\n%s\nT}\t" cell-item
))))))
961 (setq final-line
(concat final-line long-line
)))
963 (setq final-line
(concat final-line line-item
"\n"))))
966 (and caption
(format ".TB \"%s\"" caption
)))))))
970 (defun org-man-table-cell (table-cell contents info
)
971 "Transcode a TABLE-CELL element from Org to Man
972 CONTENTS is the cell contents. INFO is a plist used as
973 a communication channel."
975 (let ((scientific-format (plist-get info
:man-table-scientific-notation
)))
978 (string-match orgtbl-exp-regexp contents
))
979 ;; Use appropriate format string for scientific notation.
980 (format scientific-format
981 (match-string 1 contents
)
982 (match-string 2 contents
))
984 (when (org-export-get-next-element table-cell info
) "\t")))
989 (defun org-man-table-row (table-row contents info
)
990 "Transcode a TABLE-ROW element from Org to Man.
991 CONTENTS is the contents of the row. INFO is a plist used as
992 a communication channel."
993 ;; Rules are ignored since table separators are deduced from borders
994 ;; of the current row.
995 (when (eq (org-element-property :type table-row
) 'standard
)
997 ;; TABLE-ROW's borders are extracted from its first cell.
998 (org-export-table-cell-borders
999 (car (org-element-contents table-row
)) info
)))
1001 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1003 (cond ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1004 ((memq 'below borders
) "\n_"))))))
1009 (defun org-man-target (target _contents info
)
1010 "Transcode a TARGET object from Org to Man.
1011 CONTENTS is nil. INFO is a plist holding contextual
1013 (format "\\fI%s\\fP" (org-export-get-reference target info
)))
1018 (defun org-man-timestamp (_timestamp _contents _info
)
1019 "Transcode a TIMESTAMP object from Org to Man.
1020 ONTENTS is nil. INFO is a plist holding contextual information."
1026 (defun org-man-underline (_underline contents _info
)
1027 "Transcode UNDERLINE from Org to Man.
1028 CONTENTS is the text with underline markup. INFO is a plist
1029 holding contextual information."
1030 (format "\\fI%s\\fP" contents
))
1035 (defun org-man-verbatim (_verbatim contents _info
)
1036 "Transcode a VERBATIM object from Org to Man.
1037 CONTENTS is nil. INFO is a plist used as a communication
1039 (format ".nf\n%s\n.fi" contents
))
1044 (defun org-man-verse-block (_verse-block contents _info
)
1045 "Transcode a VERSE-BLOCK element from Org to Man.
1046 CONTENTS is verse block contents. INFO is a plist holding
1047 contextual information."
1048 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents
))
1052 ;;; Interactive functions
1054 (defun org-man-export-to-man
1055 (&optional async subtreep visible-only body-only ext-plist
)
1056 "Export current buffer to a Man file.
1058 If narrowing is active in the current buffer, only export its
1061 If a region is active, export that region.
1063 A non-nil optional argument ASYNC means the process should happen
1064 asynchronously. The resulting file should be accessible through
1065 the `org-export-stack' interface.
1067 When optional argument SUBTREEP is non-nil, export the sub-tree
1068 at point, extracting information from the headline properties
1071 When optional argument VISIBLE-ONLY is non-nil, don't export
1072 contents of hidden elements.
1074 When optional argument BODY-ONLY is non-nil, only the body
1075 without any markers.
1077 EXT-PLIST, when provided, is a property list with external
1078 parameters overriding Org default settings, but still inferior to
1079 file-local settings.
1081 Return output file's name."
1083 (let ((outfile (org-export-output-file-name ".man" subtreep
)))
1084 (org-export-to-file 'man outfile
1085 async subtreep visible-only body-only ext-plist
)))
1087 (defun org-man-export-to-pdf
1088 (&optional async subtreep visible-only body-only ext-plist
)
1089 "Export current buffer to Groff then process through to PDF.
1091 If narrowing is active in the current buffer, only export its
1094 If a region is active, export that region.
1096 A non-nil optional argument ASYNC means the process should happen
1097 asynchronously. The resulting file should be accessible through
1098 the `org-export-stack' interface.
1100 When optional argument SUBTREEP is non-nil, export the sub-tree
1101 at point, extracting information from the headline properties
1104 When optional argument VISIBLE-ONLY is non-nil, don't export
1105 contents of hidden elements.
1107 When optional argument BODY-ONLY is non-nil, only write between
1110 EXT-PLIST, when provided, is a property list with external
1111 parameters overriding Org default settings, but still inferior to
1112 file-local settings.
1114 Return PDF file's name."
1116 (let ((outfile (org-export-output-file-name ".man" subtreep
)))
1117 (org-export-to-file 'man outfile
1118 async subtreep visible-only body-only ext-plist
1119 (lambda (file) (org-latex-compile file
)))))
1121 (defun org-man-compile (file)
1122 "Compile a Groff file.
1124 FILE is the name of the file being compiled. Processing is done
1125 through the command specified in `org-man-pdf-process'.
1127 Return PDF file name or an error if it couldn't be produced."
1128 (message "Processing Groff file %s..." file
)
1129 (let ((output (org-compile-file file org-man-pdf-process
"pdf")))
1130 (when org-man-remove-logfiles
1131 (let ((base (file-name-sans-extension output
)))
1132 (dolist (ext org-man-logfiles-extensions
)
1133 (let ((file (concat base
"." ext
)))
1134 (when (file-exists-p file
) (delete-file file
))))))
1135 (message "Process completed.")
1141 ;; generated-autoload-file: "org-loaddefs.el"
1144 ;;; ox-man.el ends here