1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2013 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
)
56 (clock . org-man-clock
)
58 (comment .
(lambda (&rest args
) ""))
59 (comment-block .
(lambda (&rest args
) ""))
60 (drawer . org-man-drawer
)
61 (dynamic-block . org-man-dynamic-block
)
62 (entity . org-man-entity
)
63 (example-block . org-man-example-block
)
64 (export-block . org-man-export-block
)
65 (export-snippet . org-man-export-snippet
)
66 (fixed-width . org-man-fixed-width
)
67 (footnote-definition . org-man-footnote-definition
)
68 (footnote-reference . org-man-footnote-reference
)
69 (headline . org-man-headline
)
70 (horizontal-rule . org-man-horizontal-rule
)
71 (inline-babel-call . org-man-inline-babel-call
)
72 (inline-src-block . org-man-inline-src-block
)
73 (inlinetask . org-man-inlinetask
)
74 (italic . org-man-italic
)
76 (keyword . org-man-keyword
)
77 (line-break . org-man-line-break
)
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 .
(lambda (&rest args
) ""))
84 (quote-block . org-man-quote-block
)
85 (quote-section . org-man-quote-section
)
86 (radio-target . org-man-radio-target
)
87 (section . org-man-section
)
88 (special-block . org-man-special-block
)
89 (src-block . org-man-src-block
)
90 (statistics-cookie . org-man-statistics-cookie
)
91 (strike-through . org-man-strike-through
)
92 (subscript . org-man-subscript
)
93 (superscript . org-man-superscript
)
94 (table . org-man-table
)
95 (table-cell . org-man-table-cell
)
96 (table-row . org-man-table-row
)
97 (target . org-man-target
)
98 (template . org-man-template
)
99 (timestamp . org-man-timestamp
)
100 (underline . org-man-underline
)
101 (verbatim . org-man-verbatim
)
102 (verse-block . org-man-verse-block
))
106 ((?m
"As MAN file" org-man-export-to-man
)
107 (?p
"As PDF file" org-man-export-to-pdf
)
108 (?o
"As PDF file and open"
110 (if a
(org-man-export-to-pdf t s v b
)
111 (org-open-file (org-man-export-to-pdf nil s v b
)))))))
113 ((:man-class
"MAN_CLASS" nil nil t
)
114 (:man-class-options
"MAN_CLASS_OPTIONS" nil nil t
)
115 (:man-header-extra
"MAN_HEADER" nil nil newline
)))
119 ;;; User Configurable Variables
121 (defgroup org-export-man nil
122 "Options for exporting Org mode files to Man."
123 :tag
"Org Export Man"
128 (defcustom org-man-tables-centered t
129 "When non-nil, tables are exported in a center environment."
130 :group
'org-export-man
133 (defcustom org-man-tables-verbatim nil
134 "When non-nil, tables are exported verbatim."
135 :group
'org-export-man
139 (defcustom org-man-table-scientific-notation
"%sE%s"
140 "Format string to display numbers in scientific notation.
141 The format should have \"%s\" twice, for mantissa and exponent
142 \(i.e. \"%s\\\\times10^{%s}\").
144 When nil, no transformation is made."
145 :group
'org-export-man
147 (string :tag
"Format string")
148 (const :tag
"No formatting")))
154 (defcustom org-man-source-highlight nil
155 "Use GNU source highlight to embellish source blocks "
156 :group
'org-export-man
160 (defcustom org-man-source-highlight-langs
161 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
163 (c "c") (cc "cpp") (csharp "csharp") (d "d")
164 (fortran "fortran") (cobol "cobol") (pascal "pascal")
165 (ada "ada") (asm "asm")
166 (perl "perl") (cperl "perl")
167 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
168 (java "java") (javascript "javascript")
170 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
171 (ocaml "caml") (caml "caml")
172 (sql "sql") (sqlite "sql")
173 (html "html") (css "css") (xml "xml")
174 (bat "bat") (bison "bison") (clipper "clipper")
175 (ldap "ldap") (opa "opa")
176 (php "php") (postscript "postscript") (prolog "prolog")
177 (properties "properties") (makefile "makefile")
178 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
179 "Alist mapping languages to their listing language counterpart.
180 The key is a symbol, the major mode symbol without the \"-mode\".
181 The value is the string that should be inserted as the language
182 parameter for the listings package. If the mode name and the
183 listings name are the same, the language does not need an entry
184 in this list - but it does not hurt if it is present."
185 :group
'org-export-man
188 (symbol :tag
"Major mode ")
189 (string :tag
"Listings language"))))
193 (defvar org-man-custom-lang-environments nil
194 "Alist mapping languages to language-specific Man environments.
196 It is used during export of src blocks by the listings and
197 man packages. For example,
199 \(setq org-man-custom-lang-environments
200 '\(\(python \"pythoncode\"\)\)\)
202 would have the effect that if org encounters begin_src python
209 (defcustom org-man-pdf-process
210 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
211 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
212 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
214 "Commands to process a Man file to a PDF file.
215 This is a list of strings, each of them will be given to the
216 shell as a command. %f in the command will be replaced by the
217 full file name, %b by the file base name (i.e. without directory
218 and extension parts) and %o by the base directory of the file.
221 By default, Org uses 3 runs of to do the processing.
223 Alternatively, this may be a Lisp function that does the
224 processing. This function should accept the file name as
225 its single argument."
226 :group
'org-export-pdf
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
243 :type
'(repeat (string :tag
"Extension")))
245 (defcustom org-man-remove-logfiles t
246 "Non-nil means remove the logfiles produced by PDF production.
247 These are the .aux, .log, .out, and .toc files."
248 :group
'org-export-man
253 ;;; Internal Functions
255 (defun org-man--caption/label-string
(element info
)
256 "Return caption and label Man string for ELEMENT.
258 INFO is a plist holding contextual information. If there's no
259 caption nor label, return the empty string.
261 For non-floats, see `org-man--wrap-label'."
262 (let ((label (org-element-property :label element
))
263 (main (org-export-get-caption element
))
264 (short (org-export-get-caption element t
)))
265 (cond ((and (not main
) (not label
)) "")
266 ((not main
) (format "\\fI%s\\fP" label
))
267 ;; Option caption format with short name.
268 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
269 (org-export-data short info
)
270 (org-export-data main info
)))
271 ;; Standard caption format.
272 (t (format "\\fR%s\\fP" (org-export-data main info
))))))
274 (defun org-man--wrap-label (element output
)
275 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
276 This function shouldn't be used for floats. See
277 `org-man--caption/label-string'."
278 (let ((label (org-element-property :name element
)))
279 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
281 (concat (format "%s\n.br\n" label
) output
))))
287 (defun org-man-template (contents info
)
288 "Return complete document string after Man conversion.
289 CONTENTS is the transcoded contents string. INFO is a plist
290 holding export options."
291 (let* ((title (org-export-data (plist-get info
:title
) info
))
292 (attr (read (format "(%s)"
295 (list (plist-get info
:man-class-options
))
297 (section-item (plist-get attr
:section-id
)))
302 ((and title
(stringp section-item
))
303 (format ".TH \"%s\" \"%s\" \n" title section-item
))
304 ((and (string= "" title
) (stringp section-item
))
305 (format ".TH \"%s\" \"%s\" \n" " " section-item
))
307 (format ".TH \"%s\" \"1\" \n" title
))
315 ;;; Transcode Functions
319 ;; Babel Calls are ignored.
324 (defun org-man-bold (bold contents info
)
325 "Transcode BOLD from Org to Man.
326 CONTENTS is the text with bold markup. INFO is a plist holding
327 contextual information."
328 (format "\\fB%s\\fP" contents
))
333 (defun org-man-center-block (center-block contents info
)
334 "Transcode a CENTER-BLOCK element from Org to Man.
335 CONTENTS holds the contents of the center block. INFO is a plist
336 holding contextual information."
339 (format ".ce %d\n.nf\n%s\n.fi"
340 (- (length (split-string contents
"\n")) 1 )
346 (defun org-man-clock (clock contents info
)
347 "Transcode a CLOCK element from Org to Man.
348 CONTENTS is nil. INFO is a plist holding contextual
355 (defun org-man-code (code contents info
)
356 "Transcode a CODE object from Org to Man.
357 CONTENTS is nil. INFO is a plist used as a communication
359 (format "\\fC%s\\fP" code
))
364 ;; Comments are ignored.
369 ;; Comment Blocks are ignored.
374 (defun org-man-drawer (drawer contents info
)
375 "Transcode a DRAWER element from Org to Man.
376 DRAWER holds the drawer information
377 CONTENTS holds the contents of the block.
378 INFO is a plist holding contextual information. "
384 (defun org-man-dynamic-block (dynamic-block contents info
)
385 "Transcode a DYNAMIC-BLOCK element from Org to Man.
386 CONTENTS holds the contents of the block. INFO is a plist
387 holding contextual information. See `org-export-data'."
388 (org-man--wrap-label dynamic-block contents
))
393 (defun org-man-entity (entity contents info
)
394 "Transcode an ENTITY object from Org to Man.
395 CONTENTS are the definition itself. INFO is a plist holding
396 contextual information."
397 (org-element-property :utf-8 entity
))
402 (defun org-man-example-block (example-block contents info
)
403 "Transcode an EXAMPLE-BLOCK element from Org to Man.
404 CONTENTS is nil. INFO is a plist holding contextual
408 (format ".RS\n.nf\n%s\n.fi\n.RE"
409 (org-export-format-code-default example-block info
))))
414 (defun org-man-export-block (export-block contents info
)
415 "Transcode a EXPORT-BLOCK element from Org to Man.
416 CONTENTS is nil. INFO is a plist holding contextual information."
417 (when (string= (org-element-property :type export-block
) "MAN")
418 (org-remove-indentation (org-element-property :value export-block
))))
423 (defun org-man-export-snippet (export-snippet contents info
)
424 "Transcode a EXPORT-SNIPPET object from Org to Man.
425 CONTENTS is nil. INFO is a plist holding contextual information."
426 (when (eq (org-export-snippet-backend export-snippet
) 'man
)
427 (org-element-property :value export-snippet
)))
432 (defun org-man-fixed-width (fixed-width contents info
)
433 "Transcode a FIXED-WIDTH element from Org to Man.
434 CONTENTS is nil. INFO is a plist holding contextual information."
437 (format "\\fC\n%s\\fP"
438 (org-remove-indentation
439 (org-element-property :value fixed-width
)))))
442 ;;; Footnote Definition
444 ;; Footnote Definitions are ignored.
446 ;;; Footnote References
448 ;; Footnote References are Ignored
453 (defun org-man-headline (headline contents info
)
454 "Transcode a HEADLINE element from Org to Man.
455 CONTENTS holds the contents of the headline. INFO is a plist
456 holding contextual information."
457 (let* ((level (org-export-get-relative-level headline info
))
458 (numberedp (org-export-numbered-headline-p headline info
))
459 ;; Section formatting will set two placeholders: one for the
460 ;; title and the other for the contents.
467 (text (org-export-data (org-element-property :title headline
) info
)))
470 ;; Case 1: This is a footnote section: ignore it.
471 ((org-element-property :footnote-section-p headline
) nil
)
473 ;; Case 2. This is a deep sub-tree: export it as a list item.
474 ;; Also export as items headlines for which no section
475 ;; format has been found.
476 ((or (not section-fmt
) (org-export-low-level-p headline info
))
477 ;; Build the real contents of the sub-tree.
478 (let ((low-level-body
480 ;; If the headline is the first sibling, start a list.
481 (when (org-export-first-sibling-p headline info
)
482 (format "%s\n" ".RS"))
484 ".TP\n.ft I\n" text
"\n.ft\n"
486 ;; If headline is not the last sibling simply return
487 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
489 (if (not (org-export-last-sibling-p headline info
)) low-level-body
490 (replace-regexp-in-string
494 ;; Case 3. Standard headline. Export it as a section.
495 (t (format section-fmt text contents
)))))
500 ;;; Inline Babel Call
502 ;; Inline Babel Calls are ignored.
506 (defun org-man-inline-src-block (inline-src-block contents info
)
507 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
508 CONTENTS holds the contents of the item. INFO is a plist holding
509 contextual information."
510 (let* ((code (org-element-property :value inline-src-block
)))
512 (org-man-source-highlight
513 (let* ((tmpdir (if (featurep 'xemacs
)
515 temporary-file-directory
))
516 (in-file (make-temp-name
517 (expand-file-name "srchilite" tmpdir
)))
518 (out-file (make-temp-name
519 (expand-file-name "reshilite" tmpdir
)))
520 (org-lang (org-element-property :language inline-src-block
))
521 (lst-lang (cadr (assq (intern org-lang
)
522 org-man-source-highlight-langs
)))
524 (cmd (concat (expand-file-name "source-highlight")
531 (let ((code-block "" ))
532 (with-temp-file in-file
(insert code
))
534 (setq code-block
(org-file-contents out-file
))
535 (delete-file in-file
)
536 (delete-file out-file
)
538 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
541 ;; Do not use a special package: transcode it verbatim.
543 (concat ".RS\n.nf\n" "\\fC" "\n" code
"\n"
544 "\\fP\n.fi\n.RE\n")))))
550 (defun org-man-italic (italic contents info
)
551 "Transcode ITALIC from Org to Man.
552 CONTENTS is the text with italic markup. INFO is a plist holding
553 contextual information."
554 (format "\\fI%s\\fP" contents
))
560 (defun org-man-item (item contents info
)
562 "Transcode an ITEM element from Org to Man.
563 CONTENTS holds the contents of the item. INFO is a plist holding
564 contextual information."
566 (let* ((bullet (org-element-property :bullet item
))
567 (type (org-element-property :type
(org-element-property :parent item
)))
568 (checkbox (case (org-element-property :checkbox item
)
569 (on "\\o'\\(sq\\(mu'") ;;
571 (trans "\\o'\\(sq\\(mi'" ))) ;;
573 (tag (let ((tag (org-element-property :tag item
)))
574 ;; Check-boxes must belong to the tag.
575 (and tag
(format "\\fB%s\\fP"
577 (org-export-data tag info
)))))))
581 (let* ((bullet (org-trim bullet
))
582 (marker (cond ((string= "-" bullet
) "\\(em")
583 ((string= "*" bullet
) "\\(bu")
585 (format "%s " (org-trim bullet
)))
587 (concat ".IP " marker
" 4\n"
588 (org-trim (or contents
" " ))))
590 (concat ".TP\n" (or tag
(concat " " checkbox
)) "\n"
591 (org-trim (or contents
" " ))))))
596 (defun org-man-keyword (keyword contents info
)
597 "Transcode a KEYWORD element from Org to Man.
598 CONTENTS is nil. INFO is a plist holding contextual information."
599 (let ((key (org-element-property :key keyword
))
600 (value (org-element-property :value keyword
)))
602 ((string= key
"MAN") value
)
603 ((string= key
"INDEX") nil
)
604 ;; Invisible targets.
605 ((string= key
"TARGET") nil
)
606 ((string= key
"TOC" ) nil
))))
611 (defun org-man-line-break (line-break contents info
)
612 "Transcode a LINE-BREAK object from Org to Man.
613 CONTENTS is nil. INFO is a plist holding contextual information."
620 (defun org-man-link (link desc info
)
621 "Transcode a LINK object from Org to Man.
623 DESC is the description part of the link, or the empty string.
624 INFO is a plist holding contextual information. See
627 (let* ((type (org-element-property :type link
))
628 (raw-path (org-element-property :path link
))
629 ;; Ensure DESC really exists, or set it to nil.
630 (desc (and (not (string= desc
"")) desc
))
633 ((member type
'("http" "https" "ftp" "mailto"))
634 (concat type
":" raw-path
))
635 ((string= type
"file")
636 (when (string-match "\\(.+\\)::.+" raw-path
)
637 (setq raw-path
(match-string 1 raw-path
)))
638 (if (file-name-absolute-p raw-path
)
639 (concat "file://" (expand-file-name raw-path
))
640 (concat "file://" raw-path
)))
644 ;; External link with a description part.
645 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
646 ;; External link without a description part.
647 (path (format "\\fI%s\\fP" path
))
648 ;; No path, only description. Try to do something useful.
649 (t (format "\\fI%s\\fP" desc
)))))
654 (defun org-man-paragraph (paragraph contents info
)
655 "Transcode a PARAGRAPH element from Org to Man.
656 CONTENTS is the contents of the paragraph, as a string. INFO is
657 the plist used as a communication channel."
658 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
660 (let ((parent-type (car parent
))
661 (fixed-paragraph ""))
662 (cond ((and (eq parent-type
'item
)
663 (plist-get (nth 1 parent
) :bullet
))
664 (setq fixed-paragraph
(concat "" contents
)))
665 ((eq parent-type
'section
)
666 (setq fixed-paragraph
(concat ".PP\n" contents
)))
667 ((eq parent-type
'footnote-definition
)
668 (setq fixed-paragraph contents
))
669 (t (setq fixed-paragraph
(concat "" contents
))))
675 (defun org-man-plain-list (plain-list contents info
)
676 "Transcode a PLAIN-LIST element from Org to Man.
677 CONTENTS is the contents of the list. INFO is a plist holding
678 contextual information."
683 (defun org-man-plain-text (text info
)
684 "Transcode a TEXT string from Org to Man.
685 TEXT is the string to transcode. INFO is a plist holding
686 contextual information."
688 ;; Protect various chars.
689 (setq output
(replace-regexp-in-string
690 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
691 "$\\" output nil t
1))
692 ;; Activate smart quotes. Be sure to provide original TEXT string
693 ;; since OUTPUT may have been modified.
694 (when (plist-get info
:with-smart-quotes
)
695 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
696 ;; Handle break preservation if required.
697 (when (plist-get info
:preserve-breaks
)
698 (setq output
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
713 (defun org-man-quote-block (quote-block contents info
)
714 "Transcode a QUOTE-BLOCK element from Org to Man.
715 CONTENTS holds the contents of the block. INFO is a plist
716 holding contextual information."
719 (format ".RS\n%s\n.RE" contents
)))
723 (defun org-man-quote-section (quote-section contents info
)
724 "Transcode a QUOTE-SECTION element from Org to Man.
725 CONTENTS is nil. INFO is a plist holding contextual information."
726 (let ((value (org-remove-indentation
727 (org-element-property :value quote-section
))))
728 (when value
(format ".RS\\fI%s\\fP\n.RE\n" value
))))
733 (defun org-man-radio-target (radio-target text info
)
734 "Transcode a RADIO-TARGET object from Org to Man.
735 TEXT is the text of the target. INFO is a plist holding
736 contextual information."
742 (defun org-man-section (section contents info
)
743 "Transcode a SECTION element from Org to Man.
744 CONTENTS holds the contents of the section. INFO is a plist
745 holding contextual information."
751 (defun org-man-special-block (special-block contents info
)
752 "Transcode a SPECIAL-BLOCK element from Org to Man.
753 CONTENTS holds the contents of the block. INFO is a plist
754 holding contextual information."
755 (let ((type (downcase (org-element-property :type special-block
))))
758 (format "%s\n" contents
))))
763 (defun org-man-src-block (src-block contents info
)
764 "Transcode a SRC-BLOCK element from Org to Man.
765 CONTENTS holds the contents of the item. INFO is a plist holding
766 contextual information."
767 (let* ((lang (org-element-property :language src-block
))
768 (code (org-element-property :value src-block
))
769 (custom-env (and lang
770 (cadr (assq (intern lang
)
771 org-man-custom-lang-environments
))))
772 (num-start (case (org-element-property :number-lines src-block
)
773 (continued (org-export-get-loc src-block info
))
775 (retain-labels (org-element-property :retain-labels src-block
)))
777 ;; Case 1. No source fontification.
778 ((not org-man-source-highlight
)
779 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
780 (org-export-format-code-default src-block info
)))
781 (org-man-source-highlight
782 (let* ((tmpdir (if (featurep 'xemacs
)
784 temporary-file-directory
))
786 (in-file (make-temp-name
787 (expand-file-name "srchilite" tmpdir
)))
788 (out-file (make-temp-name
789 (expand-file-name "reshilite" tmpdir
)))
791 (org-lang (org-element-property :language src-block
))
792 (lst-lang (cadr (assq (intern org-lang
)
793 org-man-source-highlight-langs
)))
795 (cmd (concat "source-highlight"
802 (let ((code-block ""))
803 (with-temp-file in-file
(insert code
))
805 (setq code-block
(org-file-contents out-file
))
806 (delete-file in-file
)
807 (delete-file out-file
)
809 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code
)))))))
812 ;;; Statistics Cookie
814 (defun org-man-statistics-cookie (statistics-cookie contents info
)
815 "Transcode a STATISTICS-COOKIE object from Org to Man.
816 CONTENTS is nil. INFO is a plist holding contextual information."
817 (org-element-property :value statistics-cookie
))
822 (defun org-man-strike-through (strike-through contents info
)
823 "Transcode STRIKE-THROUGH from Org to Man.
824 CONTENTS is the text with strike-through markup. INFO is a plist
825 holding contextual information."
826 (format "\\fI%s\\fP" contents
))
830 (defun org-man-subscript (subscript contents info
)
831 "Transcode a SUBSCRIPT object from Org to Man.
832 CONTENTS is the contents of the object. INFO is a plist holding
833 contextual information."
834 (format "\\d\\s-2%s\\s+2\\u" contents
))
836 ;;; Superscript "^_%s$
838 (defun org-man-superscript (superscript contents info
)
839 "Transcode a SUPERSCRIPT object from Org to Man.
840 CONTENTS is the contents of the object. INFO is a plist holding
841 contextual information."
842 (format "\\u\\s-2%s\\s+2\\d" contents
))
847 ;; `org-man-table' is the entry point for table transcoding. It
848 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
849 ;; delegates the job to either `org-man-table--table.el-table' or
850 ;; `org-man-table--org-table' functions, depending of the type of
853 ;; `org-man-table--align-string' is a subroutine used to build
854 ;; alignment string for Org tables.
856 (defun org-man-table (table contents info
)
857 "Transcode a TABLE element from Org to Man.
858 CONTENTS is the contents of the table. INFO is a plist holding
859 contextual information."
861 ;; Case 1: verbatim table.
862 ((or org-man-tables-verbatim
863 (let ((attr (read (format "(%s)"
866 (org-element-property :attr_man table
)
869 (and attr
(plist-get attr
:verbatim
))))
871 (format ".nf\n\\fC%s\\fP\n.fi"
872 ;; Re-create table, without affiliated keywords.
874 (org-element-interpret-data
875 `(table nil
,@(org-element-contents table
))))))
876 ;; Case 2: Standard table.
877 (t (org-man-table--org-table table contents info
))))
879 (defun org-man-table--align-string (divider table info
)
880 "Return an appropriate Man alignment string.
881 TABLE is the considered table. INFO is a plist used as
882 a communication channel."
884 ;; Extract column groups and alignment from first (non-rule) row.
886 (org-element-map table
'table-row
888 (and (eq (org-element-property :type row
) 'standard
) row
))
892 (let* ((borders (org-export-table-cell-borders cell info
))
893 (raw-width (org-export-table-cell-width cell info
))
894 (width-cm (when raw-width
(/ raw-width
5)))
895 (width (if raw-width
(format "w(%dc)"
896 (if (< width-cm
1) 1 width-cm
)) "")))
897 ;; Check left border for the first cell only.
898 (when (and (memq 'left borders
) (not alignment
))
899 (push "|" alignment
))
901 (case (org-export-table-cell-alignment cell info
)
902 (left (concat "l" width divider
))
903 (right (concat "r" width divider
))
904 (center (concat "c" width divider
)))
906 (when (memq 'right borders
) (push "|" alignment
))))
908 (apply 'concat
(reverse alignment
))))
910 (defun org-man-table--org-table (table contents info
)
911 "Return appropriate Man code for an Org table.
913 TABLE is the table type element to transcode. CONTENTS is its
914 contents, as a string. INFO is a plist used as a communication
917 This function assumes TABLE has `org' as its `:type' attribute."
918 (let* ((attr (org-export-read-attribute :attr_man table
))
919 (label (org-element-property :name table
))
920 (caption (and (not (plist-get attr
:disable-caption
))
921 (org-man--caption/label-string table info
)))
922 (divider (if (plist-get attr
:divider
) "|" " "))
924 ;; Determine alignment string.
925 (alignment (org-man-table--align-string divider table info
))
926 ;; Extract others display options.
928 (lines (org-split-string contents
"\n"))
931 (let ((result-list '()))
934 (if (plist-get attr
:expand
)
937 (case (plist-get attr
:placement
)
940 (t (if org-man-tables-centered
"center" "")))
942 (case (plist-get attr
:boxtype
)
944 ('doublebox
"doublebox")
950 (add-to-list 'result-list attr-item
)))
954 (title-line (plist-get attr
:title-line
))
955 (long-cells (plist-get attr
:long-cells
))
957 (table-format (concat
958 (format "%s" (or (car attr-list
) "" ))
960 (let ((output-list '()))
961 (when (cdr attr-list
)
962 (dolist (attr-item (cdr attr-list
))
963 (setq output-list
(concat output-list
(format ",%s" attr-item
)))))
967 (first-line (when lines
(org-split-string (car lines
) "\t"))))
968 ;; Prepare the final format string for the table.
973 (lines (concat ".TS\n " table-format
";\n"
976 (let ((final-line ""))
978 (dotimes (i (length first-line
))
979 (setq final-line
(concat final-line
"cb" divider
))))
981 (setq final-line
(concat final-line
"\n"))
984 (setq final-line
(concat final-line alignment
))
985 (dotimes (i (length first-line
))
986 (setq final-line
(concat final-line
"c" divider
))))
990 (let ((final-line "")
992 (lines (org-split-string contents
"\n")))
994 (dolist (line-item lines
)
999 (if (string= line-item
"_")
1000 (setq long-line
(format "%s\n" line-item
))
1002 (let ((cell-item-list (org-split-string line-item
"\t")))
1003 (dolist (cell-item cell-item-list
)
1005 (cond ((eq cell-item
(car (last cell-item-list
)))
1006 (setq long-line
(concat long-line
1007 (format "T{\n%s\nT}\t\n" cell-item
))))
1009 (setq long-line
(concat long-line
1010 (format "T{\n%s\nT}\t" cell-item
))))))
1013 (setq final-line
(concat final-line long-line
)))
1015 (setq final-line
(concat final-line line-item
"\n"))))
1018 (and caption
(format ".TB \"%s\"" caption
)))))))
1022 (defun org-man-table-cell (table-cell contents info
)
1023 "Transcode a TABLE-CELL element from Org to Man
1024 CONTENTS is the cell contents. INFO is a plist used as
1025 a communication channel."
1026 (concat (if (and contents
1027 org-man-table-scientific-notation
1028 (string-match orgtbl-exp-regexp contents
))
1029 ;; Use appropriate format string for scientific
1031 (format org-man-table-scientific-notation
1032 (match-string 1 contents
)
1033 (match-string 2 contents
))
1035 (when (org-export-get-next-element table-cell info
) "\t")))
1040 (defun org-man-table-row (table-row contents info
)
1041 "Transcode a TABLE-ROW element from Org to Man
1042 CONTENTS is the contents of the row. INFO is a plist used as
1043 a communication channel."
1044 ;; Rules are ignored since table separators are deduced from
1045 ;; borders of the current row.
1046 (when (eq (org-element-property :type table-row
) 'standard
)
1047 (let* ((attr (mapconcat 'identity
1048 (org-element-property
1049 :attr_man
(org-export-get-parent table-row
))
1051 ;; TABLE-ROW's borders are extracted from its first cell.
1053 (org-export-table-cell-borders
1054 (car (org-element-contents table-row
)) info
)))
1056 ;; Mark horizontal lines
1057 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1061 ;; When BOOKTABS are activated enforce bottom rule even when
1062 ;; no hline was specifically marked.
1063 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1064 ((memq 'below borders
) "\n_"))))))
1069 (defun org-man-target (target contents info
)
1070 "Transcode a TARGET object from Org to Man.
1071 CONTENTS is nil. INFO is a plist holding contextual
1073 (format "\\fI%s\\fP"
1074 (org-export-solidify-link-text (org-element-property :value target
))))
1079 (defun org-man-timestamp (timestamp contents info
)
1080 "Transcode a TIMESTAMP object from Org to Man.
1081 CONTENTS is nil. INFO is a plist holding contextual
1088 (defun org-man-underline (underline contents info
)
1089 "Transcode UNDERLINE from Org to Man.
1090 CONTENTS is the text with underline markup. INFO is a plist
1091 holding contextual information."
1092 (format "\\fI%s\\fP" contents
))
1097 (defun org-man-verbatim (verbatim contents info
)
1098 "Transcode a VERBATIM object from Org to Man.
1099 CONTENTS is nil. INFO is a plist used as a communication
1101 (format ".nf\n%s\n.fi" contents
))
1106 (defun org-man-verse-block (verse-block contents info
)
1107 "Transcode a VERSE-BLOCK element from Org to Man.
1108 CONTENTS is verse block contents. INFO is a plist holding
1109 contextual information."
1110 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents
))
1114 ;;; Interactive functions
1116 (defun org-man-export-to-man
1117 (&optional async subtreep visible-only body-only ext-plist
)
1118 "Export current buffer to a Man file.
1120 If narrowing is active in the current buffer, only export its
1123 If a region is active, export that region.
1125 A non-nil optional argument ASYNC means the process should happen
1126 asynchronously. The resulting file should be accessible through
1127 the `org-export-stack' interface.
1129 When optional argument SUBTREEP is non-nil, export the sub-tree
1130 at point, extracting information from the headline properties
1133 When optional argument VISIBLE-ONLY is non-nil, don't export
1134 contents of hidden elements.
1136 When optional argument BODY-ONLY is non-nil, only the body
1137 without any markers.
1139 EXT-PLIST, when provided, is a property list with external
1140 parameters overriding Org default settings, but still inferior to
1141 file-local settings.
1143 Return output file's name."
1145 (let ((outfile (org-export-output-file-name ".man" subtreep
)))
1147 (org-export-async-start
1148 (lambda (f) (org-export-add-to-stack f
'man
))
1151 'man
,outfile
,subtreep
,visible-only
,body-only
',ext-plist
)))
1153 'man outfile subtreep visible-only body-only ext-plist
))))
1155 (defun org-man-export-to-pdf
1156 (&optional async subtreep visible-only body-only ext-plist
)
1157 "Export current buffer to Groff then process through to PDF.
1159 If narrowing is active in the current buffer, only export its
1162 If a region is active, export that region.
1164 A non-nil optional argument ASYNC means the process should happen
1165 asynchronously. The resulting file should be accessible through
1166 the `org-export-stack' interface.
1168 When optional argument SUBTREEP is non-nil, export the sub-tree
1169 at point, extracting information from the headline properties
1172 When optional argument VISIBLE-ONLY is non-nil, don't export
1173 contents of hidden elements.
1175 When optional argument BODY-ONLY is non-nil, only write between
1178 EXT-PLIST, when provided, is a property list with external
1179 parameters overriding Org default settings, but still inferior to
1180 file-local settings.
1182 Return PDF file's name."
1185 (let ((outfile (org-export-output-file-name ".man" subtreep
)))
1186 (org-export-async-start
1187 (lambda (f) (org-export-add-to-stack f
'man
))
1191 'man
,outfile
,subtreep
,visible-only
,body-only
1194 (org-man-export-to-man nil subtreep visible-only body-only ext-plist
))))
1196 (defun org-man-compile (file)
1197 "Compile a Groff file.
1199 FILE is the name of the file being compiled. Processing is done
1200 through the command specified in `org-man-pdf-process'.
1202 Return PDF file name or an error if it couldn't be produced."
1203 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file
)))
1204 (full-name (file-truename file
))
1205 (out-dir (file-name-directory file
))
1206 ;; Make sure `default-directory' is set to FILE directory,
1207 ;; not to whatever value the current buffer may have.
1208 (default-directory (file-name-directory full-name
))
1210 (message (format "Processing Groff file %s ..." file
))
1211 (save-window-excursion
1213 ;; A function is provided: Apply it.
1214 ((functionp org-man-pdf-process
)
1215 (funcall org-man-pdf-process
(shell-quote-argument file
)))
1216 ;; A list is provided: Replace %b, %f and %o with appropriate
1217 ;; values in each command before applying it. Output is
1218 ;; redirected to "*Org PDF Groff Output*" buffer.
1219 ((consp org-man-pdf-process
)
1220 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1224 (replace-regexp-in-string
1225 "%b" (shell-quote-argument base-name
)
1226 (replace-regexp-in-string
1227 "%f" (shell-quote-argument full-name
)
1228 (replace-regexp-in-string
1229 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
1231 org-man-pdf-process
)
1232 ;; Collect standard errors from output buffer.
1233 (setq errors
(org-man-collect-errors outbuf
))))
1234 (t (error "No valid command to process to PDF")))
1235 (let ((pdffile (concat out-dir base-name
".pdf")))
1236 ;; Check for process failure. Provide collected errors if
1238 (if (not (file-exists-p pdffile
))
1239 (error (concat (format "PDF file %s wasn't produced" pdffile
)
1240 (when errors
(concat ": " errors
))))
1241 ;; Else remove log files, when specified, and signal end of
1242 ;; process to user, along with any error encountered.
1243 (when org-man-remove-logfiles
1244 (dolist (ext org-man-logfiles-extensions
)
1245 (let ((file (concat out-dir base-name
"." ext
)))
1246 (when (file-exists-p file
) (delete-file file
)))))
1247 (message (concat "Process completed"
1248 (if (not errors
) "."
1249 (concat " with errors: " errors
)))))
1250 ;; Return output file name.
1253 (defun org-man-collect-errors (buffer)
1254 "Collect some kind of errors from \"groff\" output
1255 BUFFER is the buffer containing output.
1256 Return collected error types as a string, or nil if there was
1258 (with-current-buffer buffer
1260 (goto-char (point-max))
1268 ;; generated-autoload-file: "org-loaddefs.el"
1271 ;;; ox-man.el ends here