Add :version and :package-version
[org-mode.git] / lisp / ox-man.el
blob4c17e49f5ec0e1f30d71288d40d6a149ab8dbc87
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/>.
24 ;;; Commentary:
26 ;; This library implements a Man back-end for Org generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Man
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'ox)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
46 (defvar orgtbl-exp-regexp)
50 ;;; Define Back-End
52 (org-export-define-backend man
53 ((babel-call . org-man-babel-call)
54 (bold . org-man-bold)
55 (center-block . org-man-center-block)
56 (clock . org-man-clock)
57 (code . org-man-code)
58 (comment . (lambda (&rest args) ""))
59 (comment-block . (lambda (&rest args) ""))
60 (drawer . org-man-drawer)
61 (dynamic-block . org-man-dynamic-block)
62 (entity . org-man-entity)
63 (example-block . org-man-example-block)
64 (export-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)
75 (item . org-man-item)
76 (keyword . org-man-keyword)
77 (line-break . org-man-line-break)
78 (link . org-man-link)
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))
103 :export-block "MAN"
104 :menu-entry
105 (?m "Export to MAN"
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"
109 (lambda (a s v b)
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)))))))
112 :options-alist
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"
124 :group 'org-export)
126 ;;; Tables
128 (defcustom org-man-tables-centered t
129 "When non-nil, tables are exported in a center environment."
130 :group 'org-export-man
131 :version "24.4"
132 :package-version '(Org . "8.0")
133 :type 'boolean)
135 (defcustom org-man-tables-verbatim nil
136 "When non-nil, tables are exported verbatim."
137 :group 'org-export-man
138 :version "24.4"
139 :package-version '(Org . "8.0")
140 :type 'boolean)
143 (defcustom org-man-table-scientific-notation "%sE%s"
144 "Format string to display numbers in scientific notation.
145 The format should have \"%s\" twice, for mantissa and exponent
146 \(i.e. \"%s\\\\times10^{%s}\").
148 When nil, no transformation is made."
149 :group 'org-export-man
150 :version "24.4"
151 :package-version '(Org . "8.0")
152 :type '(choice
153 (string :tag "Format string")
154 (const :tag "No formatting")))
157 ;;; Inlinetasks
158 ;; Src blocks
160 (defcustom org-man-source-highlight nil
161 "Use GNU source highlight to embellish source blocks "
162 :group 'org-export-man
163 :version "24.4"
164 :package-version '(Org . "8.0")
165 :type 'boolean)
168 (defcustom org-man-source-highlight-langs
169 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
170 (scheme "scheme")
171 (c "c") (cc "cpp") (csharp "csharp") (d "d")
172 (fortran "fortran") (cobol "cobol") (pascal "pascal")
173 (ada "ada") (asm "asm")
174 (perl "perl") (cperl "perl")
175 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
176 (java "java") (javascript "javascript")
177 (tex "latex")
178 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
179 (ocaml "caml") (caml "caml")
180 (sql "sql") (sqlite "sql")
181 (html "html") (css "css") (xml "xml")
182 (bat "bat") (bison "bison") (clipper "clipper")
183 (ldap "ldap") (opa "opa")
184 (php "php") (postscript "postscript") (prolog "prolog")
185 (properties "properties") (makefile "makefile")
186 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
187 "Alist mapping languages to their listing language counterpart.
188 The key is a symbol, the major mode symbol without the \"-mode\".
189 The value is the string that should be inserted as the language
190 parameter for the listings package. If the mode name and the
191 listings name are the same, the language does not need an entry
192 in this list - but it does not hurt if it is present."
193 :group 'org-export-man
194 :version "24.4"
195 :package-version '(Org . "8.0")
196 :type '(repeat
197 (list
198 (symbol :tag "Major mode ")
199 (string :tag "Listings language"))))
203 (defvar org-man-custom-lang-environments nil
204 "Alist mapping languages to language-specific Man environments.
206 It is used during export of src blocks by the listings and
207 man packages. For example,
209 \(setq org-man-custom-lang-environments
210 '\(\(python \"pythoncode\"\)\)\)
212 would have the effect that if org encounters begin_src python
213 during man export."
217 ;;; Compilation
219 (defcustom org-man-pdf-process
220 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
221 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
222 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
224 "Commands to process a Man file to a PDF file.
225 This is a list of strings, each of them will be given to the
226 shell as a command. %f in the command will be replaced by the
227 full file name, %b by the file base name (i.e. without directory
228 and extension parts) and %o by the base directory of the file.
231 By default, Org uses 3 runs of to do the processing.
233 Alternatively, this may be a Lisp function that does the
234 processing. This function should accept the file name as
235 its single argument."
236 :group 'org-export-pdf
237 :group 'org-export-man
238 :version "24.4"
239 :package-version '(Org . "8.0")
240 :type '(choice
241 (repeat :tag "Shell command sequence"
242 (string :tag "Shell command"))
243 (const :tag "2 runs of pdfgroff"
244 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
245 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
246 (const :tag "3 runs of pdfgroff"
247 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
248 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
249 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
250 (function)))
252 (defcustom org-man-logfiles-extensions
253 '("log" "out" "toc")
254 "The list of file extensions to consider as Man logfiles."
255 :group 'org-export-man
256 :version "24.4"
257 :package-version '(Org . "8.0")
258 :type '(repeat (string :tag "Extension")))
260 (defcustom org-man-remove-logfiles t
261 "Non-nil means remove the logfiles produced by PDF production.
262 These are the .aux, .log, .out, and .toc files."
263 :group 'org-export-man
264 :version "24.4"
265 :package-version '(Org . "8.0")
266 :type 'boolean)
270 ;;; Internal Functions
272 (defun org-man--caption/label-string (element info)
273 "Return caption and label Man string for ELEMENT.
275 INFO is a plist holding contextual information. If there's no
276 caption nor label, return the empty string.
278 For non-floats, see `org-man--wrap-label'."
279 (let ((label (org-element-property :label element))
280 (main (org-export-get-caption element))
281 (short (org-export-get-caption element t)))
282 (cond ((and (not main) (not label)) "")
283 ((not main) (format "\\fI%s\\fP" label))
284 ;; Option caption format with short name.
285 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
286 (org-export-data short info)
287 (org-export-data main info)))
288 ;; Standard caption format.
289 (t (format "\\fR%s\\fP" (org-export-data main info))))))
291 (defun org-man--wrap-label (element output)
292 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
293 This function shouldn't be used for floats. See
294 `org-man--caption/label-string'."
295 (let ((label (org-element-property :name element)))
296 (if (or (not output) (not label) (string= output "") (string= label ""))
297 output
298 (concat (format "%s\n.br\n" label) output))))
302 ;;; Template
304 (defun org-man-template (contents info)
305 "Return complete document string after Man conversion.
306 CONTENTS is the transcoded contents string. INFO is a plist
307 holding export options."
308 (let* ((title (org-export-data (plist-get info :title) info))
309 (attr (read (format "(%s)"
310 (mapconcat
311 #'identity
312 (list (plist-get info :man-class-options))
313 " "))))
314 (section-item (plist-get attr :section-id)))
316 (concat
318 (cond
319 ((and title (stringp section-item))
320 (format ".TH \"%s\" \"%s\" \n" title section-item))
321 ((and (string= "" title) (stringp section-item))
322 (format ".TH \"%s\" \"%s\" \n" " " section-item))
323 (title
324 (format ".TH \"%s\" \"1\" \n" title))
326 ".TH \" \" \"1\" "))
327 contents)))
332 ;;; Transcode Functions
334 ;;; Babel Call
336 ;; Babel Calls are ignored.
339 ;;; Bold
341 (defun org-man-bold (bold contents info)
342 "Transcode BOLD from Org to Man.
343 CONTENTS is the text with bold markup. INFO is a plist holding
344 contextual information."
345 (format "\\fB%s\\fP" contents))
348 ;;; Center Block
350 (defun org-man-center-block (center-block contents info)
351 "Transcode a CENTER-BLOCK element from Org to Man.
352 CONTENTS holds the contents of the center block. INFO is a plist
353 holding contextual information."
354 (org-man--wrap-label
355 center-block
356 (format ".ce %d\n.nf\n%s\n.fi"
357 (- (length (split-string contents "\n")) 1 )
358 contents)))
361 ;;; Clock
363 (defun org-man-clock (clock contents info)
364 "Transcode a CLOCK element from Org to Man.
365 CONTENTS is nil. INFO is a plist holding contextual
366 information."
367 "" )
370 ;;; Code
372 (defun org-man-code (code contents info)
373 "Transcode a CODE object from Org to Man.
374 CONTENTS is nil. INFO is a plist used as a communication
375 channel."
376 (format "\\fC%s\\fP" code))
379 ;;; Comment
381 ;; Comments are ignored.
384 ;;; Comment Block
386 ;; Comment Blocks are ignored.
389 ;;; Drawer
391 (defun org-man-drawer (drawer contents info)
392 "Transcode a DRAWER element from Org to Man.
393 DRAWER holds the drawer information
394 CONTENTS holds the contents of the block.
395 INFO is a plist holding contextual information. "
396 contents)
399 ;;; Dynamic Block
401 (defun org-man-dynamic-block (dynamic-block contents info)
402 "Transcode a DYNAMIC-BLOCK element from Org to Man.
403 CONTENTS holds the contents of the block. INFO is a plist
404 holding contextual information. See `org-export-data'."
405 (org-man--wrap-label dynamic-block contents))
408 ;;; Entity
410 (defun org-man-entity (entity contents info)
411 "Transcode an ENTITY object from Org to Man.
412 CONTENTS are the definition itself. INFO is a plist holding
413 contextual information."
414 (org-element-property :utf-8 entity))
417 ;;; Example Block
419 (defun org-man-example-block (example-block contents info)
420 "Transcode an EXAMPLE-BLOCK element from Org to Man.
421 CONTENTS is nil. INFO is a plist holding contextual
422 information."
423 (org-man--wrap-label
424 example-block
425 (format ".RS\n.nf\n%s\n.fi\n.RE"
426 (org-export-format-code-default example-block info))))
429 ;;; Export Block
431 (defun org-man-export-block (export-block contents info)
432 "Transcode a EXPORT-BLOCK element from Org to Man.
433 CONTENTS is nil. INFO is a plist holding contextual information."
434 (when (string= (org-element-property :type export-block) "MAN")
435 (org-remove-indentation (org-element-property :value export-block))))
438 ;;; Export Snippet
440 (defun org-man-export-snippet (export-snippet contents info)
441 "Transcode a EXPORT-SNIPPET object from Org to Man.
442 CONTENTS is nil. INFO is a plist holding contextual information."
443 (when (eq (org-export-snippet-backend export-snippet) 'man)
444 (org-element-property :value export-snippet)))
447 ;;; Fixed Width
449 (defun org-man-fixed-width (fixed-width contents info)
450 "Transcode a FIXED-WIDTH element from Org to Man.
451 CONTENTS is nil. INFO is a plist holding contextual information."
452 (org-man--wrap-label
453 fixed-width
454 (format "\\fC\n%s\\fP"
455 (org-remove-indentation
456 (org-element-property :value fixed-width)))))
459 ;;; Footnote Definition
461 ;; Footnote Definitions are ignored.
463 ;;; Footnote References
465 ;; Footnote References are Ignored
468 ;;; Headline
470 (defun org-man-headline (headline contents info)
471 "Transcode a HEADLINE element from Org to Man.
472 CONTENTS holds the contents of the headline. INFO is a plist
473 holding contextual information."
474 (let* ((level (org-export-get-relative-level headline info))
475 (numberedp (org-export-numbered-headline-p headline info))
476 ;; Section formatting will set two placeholders: one for the
477 ;; title and the other for the contents.
478 (section-fmt
479 (case level
480 (1 ".SH \"%s\"\n%s")
481 (2 ".SS \"%s\"\n%s")
482 (3 ".SS \"%s\"\n%s")
483 (t nil)))
484 (text (org-export-data (org-element-property :title headline) info)))
486 (cond
487 ;; Case 1: This is a footnote section: ignore it.
488 ((org-element-property :footnote-section-p headline) nil)
490 ;; Case 2. This is a deep sub-tree: export it as a list item.
491 ;; Also export as items headlines for which no section
492 ;; format has been found.
493 ((or (not section-fmt) (org-export-low-level-p headline info))
494 ;; Build the real contents of the sub-tree.
495 (let ((low-level-body
496 (concat
497 ;; If the headline is the first sibling, start a list.
498 (when (org-export-first-sibling-p headline info)
499 (format "%s\n" ".RS"))
500 ;; Itemize headline
501 ".TP\n.ft I\n" text "\n.ft\n"
502 contents ".RE")))
503 ;; If headline is not the last sibling simply return
504 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
505 ;; blank line.
506 (if (not (org-export-last-sibling-p headline info)) low-level-body
507 (replace-regexp-in-string
508 "[ \t\n]*\\'" ""
509 low-level-body))))
511 ;; Case 3. Standard headline. Export it as a section.
512 (t (format section-fmt text contents )))))
514 ;;; Horizontal Rule
515 ;; Not supported
517 ;;; Inline Babel Call
519 ;; Inline Babel Calls are ignored.
521 ;;; Inline Src Block
523 (defun org-man-inline-src-block (inline-src-block contents info)
524 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
525 CONTENTS holds the contents of the item. INFO is a plist holding
526 contextual information."
527 (let* ((code (org-element-property :value inline-src-block)))
528 (cond
529 (org-man-source-highlight
530 (let* ((tmpdir (if (featurep 'xemacs)
531 temp-directory
532 temporary-file-directory ))
533 (in-file (make-temp-name
534 (expand-file-name "srchilite" tmpdir)))
535 (out-file (make-temp-name
536 (expand-file-name "reshilite" tmpdir)))
537 (org-lang (org-element-property :language inline-src-block))
538 (lst-lang (cadr (assq (intern org-lang)
539 org-man-source-highlight-langs)))
541 (cmd (concat (expand-file-name "source-highlight")
542 " -s " lst-lang
543 " -f groff_man"
544 " -i " in-file
545 " -o " out-file )))
547 (if lst-lang
548 (let ((code-block "" ))
549 (with-temp-file in-file (insert code))
550 (shell-command cmd)
551 (setq code-block (org-file-contents out-file))
552 (delete-file in-file)
553 (delete-file out-file)
554 code-block)
555 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
556 code))))
558 ;; Do not use a special package: transcode it verbatim.
560 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
561 "\\fP\n.fi\n.RE\n")))))
564 ;;; Inlinetask
565 ;;; Italic
567 (defun org-man-italic (italic contents info)
568 "Transcode ITALIC from Org to Man.
569 CONTENTS is the text with italic markup. INFO is a plist holding
570 contextual information."
571 (format "\\fI%s\\fP" contents))
574 ;;; Item
577 (defun org-man-item (item contents info)
579 "Transcode an ITEM element from Org to Man.
580 CONTENTS holds the contents of the item. INFO is a plist holding
581 contextual information."
583 (let* ((bullet (org-element-property :bullet item))
584 (type (org-element-property :type (org-element-property :parent item)))
585 (checkbox (case (org-element-property :checkbox item)
586 (on "\\o'\\(sq\\(mu'") ;;
587 (off "\\(sq ") ;;
588 (trans "\\o'\\(sq\\(mi'" ))) ;;
590 (tag (let ((tag (org-element-property :tag item)))
591 ;; Check-boxes must belong to the tag.
592 (and tag (format "\\fB%s\\fP"
593 (concat checkbox
594 (org-export-data tag info)))))))
596 (if (and (null tag )
597 (null checkbox))
598 (let* ((bullet (org-trim bullet))
599 (marker (cond ((string= "-" bullet) "\\(em")
600 ((string= "*" bullet) "\\(bu")
601 ((eq type 'ordered)
602 (format "%s " (org-trim bullet)))
603 (t "\\(dg"))))
604 (concat ".IP " marker " 4\n"
605 (org-trim (or contents " " ))))
606 ; else
607 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
608 (org-trim (or contents " " ))))))
610 ;;; Keyword
613 (defun org-man-keyword (keyword contents info)
614 "Transcode a KEYWORD element from Org to Man.
615 CONTENTS is nil. INFO is a plist holding contextual information."
616 (let ((key (org-element-property :key keyword))
617 (value (org-element-property :value keyword)))
618 (cond
619 ((string= key "MAN") value)
620 ((string= key "INDEX") nil)
621 ;; Invisible targets.
622 ((string= key "TARGET") nil)
623 ((string= key "TOC" ) nil))))
626 ;;; Line Break
628 (defun org-man-line-break (line-break contents info)
629 "Transcode a LINE-BREAK object from Org to Man.
630 CONTENTS is nil. INFO is a plist holding contextual information."
631 ".br\n")
634 ;;; Link
637 (defun org-man-link (link desc info)
638 "Transcode a LINK object from Org to Man.
640 DESC is the description part of the link, or the empty string.
641 INFO is a plist holding contextual information. See
642 `org-export-data'."
644 (let* ((type (org-element-property :type link))
645 (raw-path (org-element-property :path link))
646 ;; Ensure DESC really exists, or set it to nil.
647 (desc (and (not (string= desc "")) desc))
649 (path (cond
650 ((member type '("http" "https" "ftp" "mailto"))
651 (concat type ":" raw-path))
652 ((string= type "file")
653 (when (string-match "\\(.+\\)::.+" raw-path)
654 (setq raw-path (match-string 1 raw-path)))
655 (if (file-name-absolute-p raw-path)
656 (concat "file://" (expand-file-name raw-path))
657 (concat "file://" raw-path)))
658 (t raw-path)))
659 protocol)
660 (cond
661 ;; External link with a description part.
662 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
663 ;; External link without a description part.
664 (path (format "\\fI%s\\fP" path))
665 ;; No path, only description. Try to do something useful.
666 (t (format "\\fI%s\\fP" desc)))))
669 ;;; Paragraph
671 (defun org-man-paragraph (paragraph contents info)
672 "Transcode a PARAGRAPH element from Org to Man.
673 CONTENTS is the contents of the paragraph, as a string. INFO is
674 the plist used as a communication channel."
675 (let ((parent (plist-get (nth 1 paragraph) :parent)))
676 (when parent
677 (let ((parent-type (car parent))
678 (fixed-paragraph ""))
679 (cond ((and (eq parent-type 'item)
680 (plist-get (nth 1 parent) :bullet ))
681 (setq fixed-paragraph (concat "" contents)))
682 ((eq parent-type 'section)
683 (setq fixed-paragraph (concat ".PP\n" contents)))
684 ((eq parent-type 'footnote-definition)
685 (setq fixed-paragraph contents))
686 (t (setq fixed-paragraph (concat "" contents))))
687 fixed-paragraph ))))
690 ;;; Plain List
692 (defun org-man-plain-list (plain-list contents info)
693 "Transcode a PLAIN-LIST element from Org to Man.
694 CONTENTS is the contents of the list. INFO is a plist holding
695 contextual information."
696 contents)
698 ;;; Plain Text
700 (defun org-man-plain-text (text info)
701 "Transcode a TEXT string from Org to Man.
702 TEXT is the string to transcode. INFO is a plist holding
703 contextual information."
704 (let ((output text))
705 ;; Protect various chars.
706 (setq output (replace-regexp-in-string
707 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
708 "$\\" output nil t 1))
709 ;; Activate smart quotes. Be sure to provide original TEXT string
710 ;; since OUTPUT may have been modified.
711 (when (plist-get info :with-smart-quotes)
712 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
713 ;; Handle break preservation if required.
714 (when (plist-get info :preserve-breaks)
715 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
716 output)))
717 ;; Return value.
718 output))
722 ;;; Planning
725 ;;; Property Drawer
728 ;;; Quote Block
730 (defun org-man-quote-block (quote-block contents info)
731 "Transcode a QUOTE-BLOCK element from Org to Man.
732 CONTENTS holds the contents of the block. INFO is a plist
733 holding contextual information."
734 (org-man--wrap-label
735 quote-block
736 (format ".RS\n%s\n.RE" contents)))
738 ;;; Quote Section
740 (defun org-man-quote-section (quote-section contents info)
741 "Transcode a QUOTE-SECTION element from Org to Man.
742 CONTENTS is nil. INFO is a plist holding contextual information."
743 (let ((value (org-remove-indentation
744 (org-element-property :value quote-section))))
745 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
748 ;;; Radio Target
750 (defun org-man-radio-target (radio-target text info)
751 "Transcode a RADIO-TARGET object from Org to Man.
752 TEXT is the text of the target. INFO is a plist holding
753 contextual information."
754 text )
757 ;;; Section
759 (defun org-man-section (section contents info)
760 "Transcode a SECTION element from Org to Man.
761 CONTENTS holds the contents of the section. INFO is a plist
762 holding contextual information."
763 contents)
766 ;;; Special Block
768 (defun org-man-special-block (special-block contents info)
769 "Transcode a SPECIAL-BLOCK element from Org to Man.
770 CONTENTS holds the contents of the block. INFO is a plist
771 holding contextual information."
772 (let ((type (downcase (org-element-property :type special-block))))
773 (org-man--wrap-label
774 special-block
775 (format "%s\n" contents))))
778 ;;; Src Block
780 (defun org-man-src-block (src-block contents info)
781 "Transcode a SRC-BLOCK element from Org to Man.
782 CONTENTS holds the contents of the item. INFO is a plist holding
783 contextual information."
784 (let* ((lang (org-element-property :language src-block))
785 (code (org-element-property :value src-block))
786 (custom-env (and lang
787 (cadr (assq (intern lang)
788 org-man-custom-lang-environments))))
789 (num-start (case (org-element-property :number-lines src-block)
790 (continued (org-export-get-loc src-block info))
791 (new 0)))
792 (retain-labels (org-element-property :retain-labels src-block)))
793 (cond
794 ;; Case 1. No source fontification.
795 ((not org-man-source-highlight)
796 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
797 (org-export-format-code-default src-block info)))
798 (org-man-source-highlight
799 (let* ((tmpdir (if (featurep 'xemacs)
800 temp-directory
801 temporary-file-directory ))
803 (in-file (make-temp-name
804 (expand-file-name "srchilite" tmpdir)))
805 (out-file (make-temp-name
806 (expand-file-name "reshilite" tmpdir)))
808 (org-lang (org-element-property :language src-block))
809 (lst-lang (cadr (assq (intern org-lang)
810 org-man-source-highlight-langs)))
812 (cmd (concat "source-highlight"
813 " -s " lst-lang
814 " -f groff_man "
815 " -i " in-file
816 " -o " out-file)))
818 (if lst-lang
819 (let ((code-block ""))
820 (with-temp-file in-file (insert code))
821 (shell-command cmd)
822 (setq code-block (org-file-contents out-file))
823 (delete-file in-file)
824 (delete-file out-file)
825 code-block)
826 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
829 ;;; Statistics Cookie
831 (defun org-man-statistics-cookie (statistics-cookie contents info)
832 "Transcode a STATISTICS-COOKIE object from Org to Man.
833 CONTENTS is nil. INFO is a plist holding contextual information."
834 (org-element-property :value statistics-cookie))
837 ;;; Strike-Through
839 (defun org-man-strike-through (strike-through contents info)
840 "Transcode STRIKE-THROUGH from Org to Man.
841 CONTENTS is the text with strike-through markup. INFO is a plist
842 holding contextual information."
843 (format "\\fI%s\\fP" contents))
845 ;;; Subscript
847 (defun org-man-subscript (subscript contents info)
848 "Transcode a SUBSCRIPT object from Org to Man.
849 CONTENTS is the contents of the object. INFO is a plist holding
850 contextual information."
851 (format "\\d\\s-2%s\\s+2\\u" contents))
853 ;;; Superscript "^_%s$
855 (defun org-man-superscript (superscript contents info)
856 "Transcode a SUPERSCRIPT object from Org to Man.
857 CONTENTS is the contents of the object. INFO is a plist holding
858 contextual information."
859 (format "\\u\\s-2%s\\s+2\\d" contents))
862 ;;; Table
864 ;; `org-man-table' is the entry point for table transcoding. It
865 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
866 ;; delegates the job to either `org-man-table--table.el-table' or
867 ;; `org-man-table--org-table' functions, depending of the type of
868 ;; the table.
870 ;; `org-man-table--align-string' is a subroutine used to build
871 ;; alignment string for Org tables.
873 (defun org-man-table (table contents info)
874 "Transcode a TABLE element from Org to Man.
875 CONTENTS is the contents of the table. INFO is a plist holding
876 contextual information."
877 (cond
878 ;; Case 1: verbatim table.
879 ((or org-man-tables-verbatim
880 (let ((attr (read (format "(%s)"
881 (mapconcat
882 #'identity
883 (org-element-property :attr_man table)
884 " ")))))
886 (and attr (plist-get attr :verbatim))))
888 (format ".nf\n\\fC%s\\fP\n.fi"
889 ;; Re-create table, without affiliated keywords.
890 (org-trim
891 (org-element-interpret-data
892 `(table nil ,@(org-element-contents table))))))
893 ;; Case 2: Standard table.
894 (t (org-man-table--org-table table contents info))))
896 (defun org-man-table--align-string (divider table info)
897 "Return an appropriate Man alignment string.
898 TABLE is the considered table. INFO is a plist used as
899 a communication channel."
900 (let (alignment)
901 ;; Extract column groups and alignment from first (non-rule) row.
902 (org-element-map
903 (org-element-map table 'table-row
904 (lambda (row)
905 (and (eq (org-element-property :type row) 'standard) row))
906 info 'first-match)
907 'table-cell
908 (lambda (cell)
909 (let* ((borders (org-export-table-cell-borders cell info))
910 (raw-width (org-export-table-cell-width cell info))
911 (width-cm (when raw-width (/ raw-width 5)))
912 (width (if raw-width (format "w(%dc)"
913 (if (< width-cm 1) 1 width-cm)) "")))
914 ;; Check left border for the first cell only.
915 (when (and (memq 'left borders) (not alignment))
916 (push "|" alignment))
917 (push
918 (case (org-export-table-cell-alignment cell info)
919 (left (concat "l" width divider))
920 (right (concat "r" width divider))
921 (center (concat "c" width divider)))
922 alignment)
923 (when (memq 'right borders) (push "|" alignment))))
924 info)
925 (apply 'concat (reverse alignment))))
927 (defun org-man-table--org-table (table contents info)
928 "Return appropriate Man code for an Org table.
930 TABLE is the table type element to transcode. CONTENTS is its
931 contents, as a string. INFO is a plist used as a communication
932 channel.
934 This function assumes TABLE has `org' as its `:type' attribute."
935 (let* ((attr (org-export-read-attribute :attr_man table))
936 (label (org-element-property :name table))
937 (caption (and (not (plist-get attr :disable-caption))
938 (org-man--caption/label-string table info)))
939 (divider (if (plist-get attr :divider) "|" " "))
941 ;; Determine alignment string.
942 (alignment (org-man-table--align-string divider table info))
943 ;; Extract others display options.
945 (lines (org-split-string contents "\n"))
947 (attr-list
948 (let ((result-list '()))
949 (dolist (attr-item
950 (list
951 (if (plist-get attr :expand)
952 "expand" nil)
954 (case (plist-get attr :placement)
955 ('center "center")
956 ('left nil)
957 (t (if org-man-tables-centered "center" "")))
959 (case (plist-get attr :boxtype)
960 ('box "box")
961 ('doublebox "doublebox")
962 ('allbox "allbox")
963 ('none nil)
964 (t "box"))))
966 (if attr-item
967 (add-to-list 'result-list attr-item)))
968 result-list ))
971 (title-line (plist-get attr :title-line))
972 (long-cells (plist-get attr :long-cells))
974 (table-format (concat
975 (format "%s" (or (car attr-list) "" ))
977 (let ((output-list '()))
978 (when (cdr attr-list)
979 (dolist (attr-item (cdr attr-list))
980 (setq output-list (concat output-list (format ",%s" attr-item)))))
981 output-list)
982 "")))
984 (first-line (when lines (org-split-string (car lines) "\t"))))
985 ;; Prepare the final format string for the table.
988 (cond
989 ;; Others.
990 (lines (concat ".TS\n " table-format ";\n"
992 (format "%s.\n"
993 (let ((final-line ""))
994 (when title-line
995 (dotimes (i (length first-line))
996 (setq final-line (concat final-line "cb" divider))))
998 (setq final-line (concat final-line "\n"))
1000 (if alignment
1001 (setq final-line (concat final-line alignment))
1002 (dotimes (i (length first-line))
1003 (setq final-line (concat final-line "c" divider))))
1004 final-line ))
1006 (format "%s.TE\n"
1007 (let ((final-line "")
1008 (long-line "")
1009 (lines (org-split-string contents "\n")))
1011 (dolist (line-item lines)
1012 (setq long-line "")
1014 (if long-cells
1015 (progn
1016 (if (string= line-item "_")
1017 (setq long-line (format "%s\n" line-item))
1018 ;; else string =
1019 (let ((cell-item-list (org-split-string line-item "\t")))
1020 (dolist (cell-item cell-item-list)
1022 (cond ((eq cell-item (car (last cell-item-list)))
1023 (setq long-line (concat long-line
1024 (format "T{\n%s\nT}\t\n" cell-item ))))
1026 (setq long-line (concat long-line
1027 (format "T{\n%s\nT}\t" cell-item ))))))
1028 long-line))
1029 ;; else long cells
1030 (setq final-line (concat final-line long-line )))
1032 (setq final-line (concat final-line line-item "\n"))))
1033 final-line))
1035 (and caption (format ".TB \"%s\"" caption)))))))
1037 ;;; Table Cell
1039 (defun org-man-table-cell (table-cell contents info)
1040 "Transcode a TABLE-CELL element from Org to Man
1041 CONTENTS is the cell contents. INFO is a plist used as
1042 a communication channel."
1043 (concat (if (and contents
1044 org-man-table-scientific-notation
1045 (string-match orgtbl-exp-regexp contents))
1046 ;; Use appropriate format string for scientific
1047 ;; notation.
1048 (format org-man-table-scientific-notation
1049 (match-string 1 contents)
1050 (match-string 2 contents))
1051 contents )
1052 (when (org-export-get-next-element table-cell info) "\t")))
1055 ;;; Table Row
1057 (defun org-man-table-row (table-row contents info)
1058 "Transcode a TABLE-ROW element from Org to Man
1059 CONTENTS is the contents of the row. INFO is a plist used as
1060 a communication channel."
1061 ;; Rules are ignored since table separators are deduced from
1062 ;; borders of the current row.
1063 (when (eq (org-element-property :type table-row) 'standard)
1064 (let* ((attr (mapconcat 'identity
1065 (org-element-property
1066 :attr_man (org-export-get-parent table-row))
1067 " "))
1068 ;; TABLE-ROW's borders are extracted from its first cell.
1069 (borders
1070 (org-export-table-cell-borders
1071 (car (org-element-contents table-row)) info)))
1072 (concat
1073 ;; Mark horizontal lines
1074 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1075 contents
1077 (cond
1078 ;; When BOOKTABS are activated enforce bottom rule even when
1079 ;; no hline was specifically marked.
1080 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1081 ((memq 'below borders) "\n_"))))))
1084 ;;; Target
1086 (defun org-man-target (target contents info)
1087 "Transcode a TARGET object from Org to Man.
1088 CONTENTS is nil. INFO is a plist holding contextual
1089 information."
1090 (format "\\fI%s\\fP"
1091 (org-export-solidify-link-text (org-element-property :value target))))
1094 ;;; Timestamp
1096 (defun org-man-timestamp (timestamp contents info)
1097 "Transcode a TIMESTAMP object from Org to Man.
1098 CONTENTS is nil. INFO is a plist holding contextual
1099 information."
1100 "" )
1103 ;;; Underline
1105 (defun org-man-underline (underline contents info)
1106 "Transcode UNDERLINE from Org to Man.
1107 CONTENTS is the text with underline markup. INFO is a plist
1108 holding contextual information."
1109 (format "\\fI%s\\fP" contents))
1112 ;;; Verbatim
1114 (defun org-man-verbatim (verbatim contents info)
1115 "Transcode a VERBATIM object from Org to Man.
1116 CONTENTS is nil. INFO is a plist used as a communication
1117 channel."
1118 (format ".nf\n%s\n.fi" contents))
1121 ;;; Verse Block
1123 (defun org-man-verse-block (verse-block contents info)
1124 "Transcode a VERSE-BLOCK element from Org to Man.
1125 CONTENTS is verse block contents. INFO is a plist holding
1126 contextual information."
1127 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1131 ;;; Interactive functions
1133 (defun org-man-export-to-man
1134 (&optional async subtreep visible-only body-only ext-plist)
1135 "Export current buffer to a Man file.
1137 If narrowing is active in the current buffer, only export its
1138 narrowed part.
1140 If a region is active, export that region.
1142 A non-nil optional argument ASYNC means the process should happen
1143 asynchronously. The resulting file should be accessible through
1144 the `org-export-stack' interface.
1146 When optional argument SUBTREEP is non-nil, export the sub-tree
1147 at point, extracting information from the headline properties
1148 first.
1150 When optional argument VISIBLE-ONLY is non-nil, don't export
1151 contents of hidden elements.
1153 When optional argument BODY-ONLY is non-nil, only the body
1154 without any markers.
1156 EXT-PLIST, when provided, is a property list with external
1157 parameters overriding Org default settings, but still inferior to
1158 file-local settings.
1160 Return output file's name."
1161 (interactive)
1162 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1163 (if async
1164 (org-export-async-start
1165 (lambda (f) (org-export-add-to-stack f 'man))
1166 `(expand-file-name
1167 (org-export-to-file
1168 'man ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
1169 (org-export-to-file
1170 'man outfile subtreep visible-only body-only ext-plist))))
1172 (defun org-man-export-to-pdf
1173 (&optional async subtreep visible-only body-only ext-plist)
1174 "Export current buffer to Groff then process through to PDF.
1176 If narrowing is active in the current buffer, only export its
1177 narrowed part.
1179 If a region is active, export that region.
1181 A non-nil optional argument ASYNC means the process should happen
1182 asynchronously. The resulting file should be accessible through
1183 the `org-export-stack' interface.
1185 When optional argument SUBTREEP is non-nil, export the sub-tree
1186 at point, extracting information from the headline properties
1187 first.
1189 When optional argument VISIBLE-ONLY is non-nil, don't export
1190 contents of hidden elements.
1192 When optional argument BODY-ONLY is non-nil, only write between
1193 markers.
1195 EXT-PLIST, when provided, is a property list with external
1196 parameters overriding Org default settings, but still inferior to
1197 file-local settings.
1199 Return PDF file's name."
1200 (interactive)
1201 (if async
1202 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1203 (org-export-async-start
1204 (lambda (f) (org-export-add-to-stack f 'man))
1205 `(expand-file-name
1206 (org-man-compile
1207 (org-export-to-file
1208 'man ,outfile ,subtreep ,visible-only ,body-only
1209 ',ext-plist)))))
1210 (org-man-compile
1211 (org-man-export-to-man nil subtreep visible-only body-only ext-plist))))
1213 (defun org-man-compile (file)
1214 "Compile a Groff file.
1216 FILE is the name of the file being compiled. Processing is done
1217 through the command specified in `org-man-pdf-process'.
1219 Return PDF file name or an error if it couldn't be produced."
1220 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1221 (full-name (file-truename file))
1222 (out-dir (file-name-directory file))
1223 ;; Make sure `default-directory' is set to FILE directory,
1224 ;; not to whatever value the current buffer may have.
1225 (default-directory (file-name-directory full-name))
1226 errors)
1227 (message (format "Processing Groff file %s ..." file))
1228 (save-window-excursion
1229 (cond
1230 ;; A function is provided: Apply it.
1231 ((functionp org-man-pdf-process)
1232 (funcall org-man-pdf-process (shell-quote-argument file)))
1233 ;; A list is provided: Replace %b, %f and %o with appropriate
1234 ;; values in each command before applying it. Output is
1235 ;; redirected to "*Org PDF Groff Output*" buffer.
1236 ((consp org-man-pdf-process)
1237 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1238 (mapc
1239 (lambda (command)
1240 (shell-command
1241 (replace-regexp-in-string
1242 "%b" (shell-quote-argument base-name)
1243 (replace-regexp-in-string
1244 "%f" (shell-quote-argument full-name)
1245 (replace-regexp-in-string
1246 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1247 outbuf))
1248 org-man-pdf-process)
1249 ;; Collect standard errors from output buffer.
1250 (setq errors (org-man-collect-errors outbuf))))
1251 (t (error "No valid command to process to PDF")))
1252 (let ((pdffile (concat out-dir base-name ".pdf")))
1253 ;; Check for process failure. Provide collected errors if
1254 ;; possible.
1255 (if (not (file-exists-p pdffile))
1256 (error (concat (format "PDF file %s wasn't produced" pdffile)
1257 (when errors (concat ": " errors))))
1258 ;; Else remove log files, when specified, and signal end of
1259 ;; process to user, along with any error encountered.
1260 (when org-man-remove-logfiles
1261 (dolist (ext org-man-logfiles-extensions)
1262 (let ((file (concat out-dir base-name "." ext)))
1263 (when (file-exists-p file) (delete-file file)))))
1264 (message (concat "Process completed"
1265 (if (not errors) "."
1266 (concat " with errors: " errors)))))
1267 ;; Return output file name.
1268 pdffile))))
1270 (defun org-man-collect-errors (buffer)
1271 "Collect some kind of errors from \"groff\" output
1272 BUFFER is the buffer containing output.
1273 Return collected error types as a string, or nil if there was
1274 none."
1275 (with-current-buffer buffer
1276 (save-excursion
1277 (goto-char (point-max))
1278 ;; Find final run
1279 nil )))
1282 (provide 'ox-man)
1284 ;; Local variables:
1285 ;; generated-autoload-file: "org-loaddefs.el"
1286 ;; End:
1288 ;;; ox-man.el ends here