org.el: Send a warning when org-loaddefs.el could not be found
[org-mode.git] / lisp / ox-man.el
blob473a28223e8b03f776c24cdd975c92a76a64bc06
1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements a Man back-end for Org generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Man
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'ox)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
46 (defvar orgtbl-exp-regexp)
50 ;;; Define Back-End
52 (org-export-define-backend 'man
53 '((babel-call . org-man-babel-call)
54 (bold . org-man-bold)
55 (center-block . org-man-center-block)
56 (clock . org-man-clock)
57 (code . org-man-code)
58 (comment . (lambda (&rest args) ""))
59 (comment-block . (lambda (&rest args) ""))
60 (drawer . org-man-drawer)
61 (dynamic-block . org-man-dynamic-block)
62 (entity . org-man-entity)
63 (example-block . org-man-example-block)
64 (export-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 (node-property . org-man-node-property)
80 (paragraph . org-man-paragraph)
81 (plain-list . org-man-plain-list)
82 (plain-text . org-man-plain-text)
83 (planning . org-man-planning)
84 (property-drawer . org-man-property-drawer)
85 (quote-block . org-man-quote-block)
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)
116 ;; Other variables.
117 (:man-tables-centered nil nil org-man-tables-centered)
118 (:man-tables-verbatim nil nil org-man-tables-verbatim)
119 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
120 (:man-source-highlight nil nil org-man-source-highlight)
121 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)
122 (:man-pdf-process nil nil org-man-pdf-process)
123 (:man-logfiles-extensions nil nil org-man-logfiles-extensions)
124 (:man-remove-logfiles nil nil org-man-remove-logfiles)))
128 ;;; User Configurable Variables
130 (defgroup org-export-man nil
131 "Options for exporting Org mode files to Man."
132 :tag "Org Export Man"
133 :group 'org-export)
135 ;;; Tables
137 (defcustom org-man-tables-centered t
138 "When non-nil, tables are exported in a center environment."
139 :group 'org-export-man
140 :version "24.4"
141 :package-version '(Org . "8.0")
142 :type 'boolean)
144 (defcustom org-man-tables-verbatim nil
145 "When non-nil, tables are exported verbatim."
146 :group 'org-export-man
147 :version "24.4"
148 :package-version '(Org . "8.0")
149 :type 'boolean)
152 (defcustom org-man-table-scientific-notation "%sE%s"
153 "Format string to display numbers in scientific notation.
154 The format should have \"%s\" twice, for mantissa and exponent
155 \(i.e. \"%s\\\\times10^{%s}\").
157 When nil, no transformation is made."
158 :group 'org-export-man
159 :version "24.4"
160 :package-version '(Org . "8.0")
161 :type '(choice
162 (string :tag "Format string")
163 (const :tag "No formatting")))
166 ;;; Inlinetasks
167 ;; Src blocks
169 (defcustom org-man-source-highlight nil
170 "Use GNU source highlight to embellish source blocks "
171 :group 'org-export-man
172 :version "24.4"
173 :package-version '(Org . "8.0")
174 :type 'boolean)
177 (defcustom org-man-source-highlight-langs
178 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
179 (scheme "scheme")
180 (c "c") (cc "cpp") (csharp "csharp") (d "d")
181 (fortran "fortran") (cobol "cobol") (pascal "pascal")
182 (ada "ada") (asm "asm")
183 (perl "perl") (cperl "perl")
184 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
185 (java "java") (javascript "javascript")
186 (tex "latex")
187 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
188 (ocaml "caml") (caml "caml")
189 (sql "sql") (sqlite "sql")
190 (html "html") (css "css") (xml "xml")
191 (bat "bat") (bison "bison") (clipper "clipper")
192 (ldap "ldap") (opa "opa")
193 (php "php") (postscript "postscript") (prolog "prolog")
194 (properties "properties") (makefile "makefile")
195 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
196 "Alist mapping languages to their listing language counterpart.
197 The key is a symbol, the major mode symbol without the \"-mode\".
198 The value is the string that should be inserted as the language
199 parameter for the listings package. If the mode name and the
200 listings name are the same, the language does not need an entry
201 in this list - but it does not hurt if it is present."
202 :group 'org-export-man
203 :version "24.4"
204 :package-version '(Org . "8.0")
205 :type '(repeat
206 (list
207 (symbol :tag "Major mode ")
208 (string :tag "Listings language"))))
212 (defvar org-man-custom-lang-environments nil
213 "Alist mapping languages to language-specific Man environments.
215 It is used during export of src blocks by the listings and
216 man packages. For example,
218 \(setq org-man-custom-lang-environments
219 '\(\(python \"pythoncode\"\)\)\)
221 would have the effect that if org encounters begin_src python
222 during man export."
226 ;;; Compilation
228 (defcustom org-man-pdf-process
229 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
230 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
231 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
233 "Commands to process a Man file to a PDF file.
234 This is a list of strings, each of them will be given to the
235 shell as a command. %f in the command will be replaced by the
236 full file name, %b by the file base name (i.e. without directory
237 and extension parts) and %o by the base directory of the file.
240 By default, Org uses 3 runs of to do the processing.
242 Alternatively, this may be a Lisp function that does the
243 processing. This function should accept the file name as
244 its single argument."
245 :group 'org-export-pdf
246 :group 'org-export-man
247 :version "24.4"
248 :package-version '(Org . "8.0")
249 :type '(choice
250 (repeat :tag "Shell command sequence"
251 (string :tag "Shell command"))
252 (const :tag "2 runs of pdfgroff"
253 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
254 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
255 (const :tag "3 runs of pdfgroff"
256 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
257 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
258 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
259 (function)))
261 (defcustom org-man-logfiles-extensions
262 '("log" "out" "toc")
263 "The list of file extensions to consider as Man logfiles."
264 :group 'org-export-man
265 :version "24.4"
266 :package-version '(Org . "8.0")
267 :type '(repeat (string :tag "Extension")))
269 (defcustom org-man-remove-logfiles t
270 "Non-nil means remove the logfiles produced by PDF production.
271 These are the .aux, .log, .out, and .toc files."
272 :group 'org-export-man
273 :version "24.4"
274 :package-version '(Org . "8.0")
275 :type 'boolean)
279 ;;; Internal Functions
281 (defun org-man--caption/label-string (element info)
282 "Return caption and label Man string for ELEMENT.
284 INFO is a plist holding contextual information. If there's no
285 caption nor label, return the empty string.
287 For non-floats, see `org-man--wrap-label'."
288 (let ((label (org-element-property :label element))
289 (main (org-export-get-caption element))
290 (short (org-export-get-caption element t)))
291 (cond ((and (not main) (not label)) "")
292 ((not main) (format "\\fI%s\\fP" label))
293 ;; Option caption format with short name.
294 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
295 (org-export-data short info)
296 (org-export-data main info)))
297 ;; Standard caption format.
298 (t (format "\\fR%s\\fP" (org-export-data main info))))))
300 (defun org-man--wrap-label (element output)
301 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
302 This function shouldn't be used for floats. See
303 `org-man--caption/label-string'."
304 (let ((label (org-element-property :name element)))
305 (if (or (not output) (not label) (string= output "") (string= label ""))
306 output
307 (concat (format "%s\n.br\n" label) output))))
311 ;;; Template
313 (defun org-man-template (contents info)
314 "Return complete document string after Man conversion.
315 CONTENTS is the transcoded contents string. INFO is a plist
316 holding export options."
317 (let* ((title (org-export-data (plist-get info :title) info))
318 (attr (read (format "(%s)"
319 (mapconcat
320 #'identity
321 (list (plist-get info :man-class-options))
322 " "))))
323 (section-item (plist-get attr :section-id)))
325 (concat
327 (cond
328 ((and title (stringp section-item))
329 (format ".TH \"%s\" \"%s\" \n" title section-item))
330 ((and (string= "" title) (stringp section-item))
331 (format ".TH \"%s\" \"%s\" \n" " " section-item))
332 (title
333 (format ".TH \"%s\" \"1\" \n" title))
335 ".TH \" \" \"1\" "))
336 contents)))
341 ;;; Transcode Functions
343 ;;; Babel Call
345 ;; Babel Calls are ignored.
348 ;;; Bold
350 (defun org-man-bold (bold contents info)
351 "Transcode BOLD from Org to Man.
352 CONTENTS is the text with bold markup. INFO is a plist holding
353 contextual information."
354 (format "\\fB%s\\fP" contents))
357 ;;; Center Block
359 (defun org-man-center-block (center-block contents info)
360 "Transcode a CENTER-BLOCK element from Org to Man.
361 CONTENTS holds the contents of the center block. INFO is a plist
362 holding contextual information."
363 (org-man--wrap-label
364 center-block
365 (format ".ce %d\n.nf\n%s\n.fi"
366 (- (length (split-string contents "\n")) 1 )
367 contents)))
370 ;;; Clock
372 (defun org-man-clock (clock contents info)
373 "Transcode a CLOCK element from Org to Man.
374 CONTENTS is nil. INFO is a plist holding contextual
375 information."
376 "" )
379 ;;; Code
381 (defun org-man-code (code contents info)
382 "Transcode a CODE object from Org to Man.
383 CONTENTS is nil. INFO is a plist used as a communication
384 channel."
385 (format "\\fC%s\\fP" code))
388 ;;; Comment
390 ;; Comments are ignored.
393 ;;; Comment Block
395 ;; Comment Blocks are ignored.
398 ;;; Drawer
400 (defun org-man-drawer (drawer contents info)
401 "Transcode a DRAWER element from Org to Man.
402 DRAWER holds the drawer information
403 CONTENTS holds the contents of the block.
404 INFO is a plist holding contextual information. "
405 contents)
408 ;;; Dynamic Block
410 (defun org-man-dynamic-block (dynamic-block contents info)
411 "Transcode a DYNAMIC-BLOCK element from Org to Man.
412 CONTENTS holds the contents of the block. INFO is a plist
413 holding contextual information. See `org-export-data'."
414 (org-man--wrap-label dynamic-block contents))
417 ;;; Entity
419 (defun org-man-entity (entity contents info)
420 "Transcode an ENTITY object from Org to Man.
421 CONTENTS are the definition itself. INFO is a plist holding
422 contextual information."
423 (org-element-property :utf-8 entity))
426 ;;; Example Block
428 (defun org-man-example-block (example-block contents info)
429 "Transcode an EXAMPLE-BLOCK element from Org to Man.
430 CONTENTS is nil. INFO is a plist holding contextual
431 information."
432 (org-man--wrap-label
433 example-block
434 (format ".RS\n.nf\n%s\n.fi\n.RE"
435 (org-export-format-code-default example-block info))))
438 ;;; Export Block
440 (defun org-man-export-block (export-block contents info)
441 "Transcode a EXPORT-BLOCK element from Org to Man.
442 CONTENTS is nil. INFO is a plist holding contextual information."
443 (when (string= (org-element-property :type export-block) "MAN")
444 (org-remove-indentation (org-element-property :value export-block))))
447 ;;; Export Snippet
449 (defun org-man-export-snippet (export-snippet contents info)
450 "Transcode a EXPORT-SNIPPET object from Org to Man.
451 CONTENTS is nil. INFO is a plist holding contextual information."
452 (when (eq (org-export-snippet-backend export-snippet) 'man)
453 (org-element-property :value export-snippet)))
456 ;;; Fixed Width
458 (defun org-man-fixed-width (fixed-width contents info)
459 "Transcode a FIXED-WIDTH element from Org to Man.
460 CONTENTS is nil. INFO is a plist holding contextual information."
461 (org-man--wrap-label
462 fixed-width
463 (format "\\fC\n%s\\fP"
464 (org-remove-indentation
465 (org-element-property :value fixed-width)))))
468 ;;; Footnote Definition
470 ;; Footnote Definitions are ignored.
472 ;;; Footnote References
474 ;; Footnote References are Ignored
477 ;;; Headline
479 (defun org-man-headline (headline contents info)
480 "Transcode a HEADLINE element from Org to Man.
481 CONTENTS holds the contents of the headline. INFO is a plist
482 holding contextual information."
483 (let* ((level (org-export-get-relative-level headline info))
484 (numberedp (org-export-numbered-headline-p headline info))
485 ;; Section formatting will set two placeholders: one for the
486 ;; title and the other for the contents.
487 (section-fmt
488 (case level
489 (1 ".SH \"%s\"\n%s")
490 (2 ".SS \"%s\"\n%s")
491 (3 ".SS \"%s\"\n%s")
492 (t nil)))
493 (text (org-export-data (org-element-property :title headline) info)))
495 (cond
496 ;; Case 1: This is a footnote section: ignore it.
497 ((org-element-property :footnote-section-p headline) nil)
499 ;; Case 2. This is a deep sub-tree: export it as a list item.
500 ;; Also export as items headlines for which no section
501 ;; format has been found.
502 ((or (not section-fmt) (org-export-low-level-p headline info))
503 ;; Build the real contents of the sub-tree.
504 (let ((low-level-body
505 (concat
506 ;; If the headline is the first sibling, start a list.
507 (when (org-export-first-sibling-p headline info)
508 (format "%s\n" ".RS"))
509 ;; Itemize headline
510 ".TP\n.ft I\n" text "\n.ft\n"
511 contents ".RE")))
512 ;; If headline is not the last sibling simply return
513 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
514 ;; blank line.
515 (if (not (org-export-last-sibling-p headline info)) low-level-body
516 (replace-regexp-in-string
517 "[ \t\n]*\\'" ""
518 low-level-body))))
520 ;; Case 3. Standard headline. Export it as a section.
521 (t (format section-fmt text contents )))))
523 ;;; Horizontal Rule
524 ;; Not supported
526 ;;; Inline Babel Call
528 ;; Inline Babel Calls are ignored.
530 ;;; Inline Src Block
532 (defun org-man-inline-src-block (inline-src-block contents info)
533 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
534 CONTENTS holds the contents of the item. INFO is a plist holding
535 contextual information."
536 (let* ((code (org-element-property :value inline-src-block)))
537 (cond
538 (org-man-source-highlight
539 (let* ((tmpdir (if (featurep 'xemacs)
540 temp-directory
541 temporary-file-directory ))
542 (in-file (make-temp-name
543 (expand-file-name "srchilite" tmpdir)))
544 (out-file (make-temp-name
545 (expand-file-name "reshilite" tmpdir)))
546 (org-lang (org-element-property :language inline-src-block))
547 (lst-lang (cadr (assq (intern org-lang)
548 org-man-source-highlight-langs)))
550 (cmd (concat (expand-file-name "source-highlight")
551 " -s " lst-lang
552 " -f groff_man"
553 " -i " in-file
554 " -o " out-file )))
556 (if lst-lang
557 (let ((code-block "" ))
558 (with-temp-file in-file (insert code))
559 (shell-command cmd)
560 (setq code-block (org-file-contents out-file))
561 (delete-file in-file)
562 (delete-file out-file)
563 code-block)
564 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
565 code))))
567 ;; Do not use a special package: transcode it verbatim.
569 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
570 "\\fP\n.fi\n.RE\n")))))
573 ;;; Inlinetask
574 ;;; Italic
576 (defun org-man-italic (italic contents info)
577 "Transcode ITALIC from Org to Man.
578 CONTENTS is the text with italic markup. INFO is a plist holding
579 contextual information."
580 (format "\\fI%s\\fP" contents))
583 ;;; Item
586 (defun org-man-item (item contents info)
588 "Transcode an ITEM element from Org to Man.
589 CONTENTS holds the contents of the item. INFO is a plist holding
590 contextual information."
592 (let* ((bullet (org-element-property :bullet item))
593 (type (org-element-property :type (org-element-property :parent item)))
594 (checkbox (case (org-element-property :checkbox item)
595 (on "\\o'\\(sq\\(mu'") ;;
596 (off "\\(sq ") ;;
597 (trans "\\o'\\(sq\\(mi'" ))) ;;
599 (tag (let ((tag (org-element-property :tag item)))
600 ;; Check-boxes must belong to the tag.
601 (and tag (format "\\fB%s\\fP"
602 (concat checkbox
603 (org-export-data tag info)))))))
605 (if (and (null tag )
606 (null checkbox))
607 (let* ((bullet (org-trim bullet))
608 (marker (cond ((string= "-" bullet) "\\(em")
609 ((string= "*" bullet) "\\(bu")
610 ((eq type 'ordered)
611 (format "%s " (org-trim bullet)))
612 (t "\\(dg"))))
613 (concat ".IP " marker " 4\n"
614 (org-trim (or contents " " ))))
615 ; else
616 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
617 (org-trim (or contents " " ))))))
619 ;;; Keyword
622 (defun org-man-keyword (keyword contents info)
623 "Transcode a KEYWORD element from Org to Man.
624 CONTENTS is nil. INFO is a plist holding contextual information."
625 (let ((key (org-element-property :key keyword))
626 (value (org-element-property :value keyword)))
627 (cond
628 ((string= key "MAN") value)
629 ((string= key "INDEX") nil)
630 ((string= key "TOC" ) nil))))
633 ;;; Line Break
635 (defun org-man-line-break (line-break contents info)
636 "Transcode a LINE-BREAK object from Org to Man.
637 CONTENTS is nil. INFO is a plist holding contextual information."
638 ".br\n")
641 ;;; Link
644 (defun org-man-link (link desc info)
645 "Transcode a LINK object from Org to Man.
647 DESC is the description part of the link, or the empty string.
648 INFO is a plist holding contextual information. See
649 `org-export-data'."
650 (let* ((type (org-element-property :type link))
651 (raw-path (org-element-property :path link))
652 ;; Ensure DESC really exists, or set it to nil.
653 (desc (and (not (string= desc "")) desc))
654 (path (cond
655 ((member type '("http" "https" "ftp" "mailto"))
656 (concat type ":" raw-path))
657 ((and (string= type "file") (file-name-absolute-p raw-path))
658 (concat "file:" raw-path))
659 (t raw-path)))
660 protocol)
661 (cond
662 ;; External link with a description part.
663 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
664 ;; External link without a description part.
665 (path (format "\\fI%s\\fP" path))
666 ;; No path, only description. Try to do something useful.
667 (t (format "\\fI%s\\fP" desc)))))
669 ;;;; Node Property
671 (defun org-man-node-property (node-property contents info)
672 "Transcode a NODE-PROPERTY element from Org to Man.
673 CONTENTS is nil. INFO is a plist holding contextual
674 information."
675 (format "%s:%s"
676 (org-element-property :key node-property)
677 (let ((value (org-element-property :value node-property)))
678 (if value (concat " " value) ""))))
680 ;;; Paragraph
682 (defun org-man-paragraph (paragraph contents info)
683 "Transcode a PARAGRAPH element from Org to Man.
684 CONTENTS is the contents of the paragraph, as a string. INFO is
685 the plist used as a communication channel."
686 (let ((parent (plist-get (nth 1 paragraph) :parent)))
687 (when parent
688 (let ((parent-type (car parent))
689 (fixed-paragraph ""))
690 (cond ((and (eq parent-type 'item)
691 (plist-get (nth 1 parent) :bullet ))
692 (setq fixed-paragraph (concat "" contents)))
693 ((eq parent-type 'section)
694 (setq fixed-paragraph (concat ".PP\n" contents)))
695 ((eq parent-type 'footnote-definition)
696 (setq fixed-paragraph contents))
697 (t (setq fixed-paragraph (concat "" contents))))
698 fixed-paragraph ))))
701 ;;; Plain List
703 (defun org-man-plain-list (plain-list contents info)
704 "Transcode a PLAIN-LIST element from Org to Man.
705 CONTENTS is the contents of the list. INFO is a plist holding
706 contextual information."
707 contents)
709 ;;; Plain Text
711 (defun org-man-plain-text (text info)
712 "Transcode a TEXT string from Org to Man.
713 TEXT is the string to transcode. INFO is a plist holding
714 contextual information."
715 (let ((output text))
716 ;; Protect various chars.
717 (setq output (replace-regexp-in-string
718 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
719 "$\\" output nil t 1))
720 ;; Activate smart quotes. Be sure to provide original TEXT string
721 ;; since OUTPUT may have been modified.
722 (when (plist-get info :with-smart-quotes)
723 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
724 ;; Handle break preservation if required.
725 (when (plist-get info :preserve-breaks)
726 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
727 output)))
728 ;; Return value.
729 output))
733 ;;; Planning
736 ;;; Property Drawer
738 (defun org-man-property-drawer (property-drawer contents info)
739 "Transcode a PROPERTY-DRAWER element from Org to Man.
740 CONTENTS holds the contents of the drawer. INFO is a plist
741 holding contextual information."
742 (and (org-string-nw-p contents)
743 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
745 ;;; Quote Block
747 (defun org-man-quote-block (quote-block contents info)
748 "Transcode a QUOTE-BLOCK element from Org to Man.
749 CONTENTS holds the contents of the block. INFO is a plist
750 holding contextual information."
751 (org-man--wrap-label
752 quote-block
753 (format ".RS\n%s\n.RE" contents)))
756 ;;; Radio Target
758 (defun org-man-radio-target (radio-target text info)
759 "Transcode a RADIO-TARGET object from Org to Man.
760 TEXT is the text of the target. INFO is a plist holding
761 contextual information."
762 text )
765 ;;; Section
767 (defun org-man-section (section contents info)
768 "Transcode a SECTION element from Org to Man.
769 CONTENTS holds the contents of the section. INFO is a plist
770 holding contextual information."
771 contents)
774 ;;; Special Block
776 (defun org-man-special-block (special-block contents info)
777 "Transcode a SPECIAL-BLOCK element from Org to Man.
778 CONTENTS holds the contents of the block. INFO is a plist
779 holding contextual information."
780 (let ((type (downcase (org-element-property :type special-block))))
781 (org-man--wrap-label
782 special-block
783 (format "%s\n" contents))))
786 ;;; Src Block
788 (defun org-man-src-block (src-block contents info)
789 "Transcode a SRC-BLOCK element from Org to Man.
790 CONTENTS holds the contents of the item. INFO is a plist holding
791 contextual information."
792 (let* ((lang (org-element-property :language src-block))
793 (code (org-element-property :value src-block))
794 (custom-env (and lang
795 (cadr (assq (intern lang)
796 org-man-custom-lang-environments))))
797 (num-start (case (org-element-property :number-lines src-block)
798 (continued (org-export-get-loc src-block info))
799 (new 0)))
800 (retain-labels (org-element-property :retain-labels src-block)))
801 (cond
802 ;; Case 1. No source fontification.
803 ((not org-man-source-highlight)
804 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
805 (org-export-format-code-default src-block info)))
806 (org-man-source-highlight
807 (let* ((tmpdir (if (featurep 'xemacs)
808 temp-directory
809 temporary-file-directory ))
811 (in-file (make-temp-name
812 (expand-file-name "srchilite" tmpdir)))
813 (out-file (make-temp-name
814 (expand-file-name "reshilite" tmpdir)))
816 (org-lang (org-element-property :language src-block))
817 (lst-lang (cadr (assq (intern org-lang)
818 org-man-source-highlight-langs)))
820 (cmd (concat "source-highlight"
821 " -s " lst-lang
822 " -f groff_man "
823 " -i " in-file
824 " -o " out-file)))
826 (if lst-lang
827 (let ((code-block ""))
828 (with-temp-file in-file (insert code))
829 (shell-command cmd)
830 (setq code-block (org-file-contents out-file))
831 (delete-file in-file)
832 (delete-file out-file)
833 code-block)
834 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
837 ;;; Statistics Cookie
839 (defun org-man-statistics-cookie (statistics-cookie contents info)
840 "Transcode a STATISTICS-COOKIE object from Org to Man.
841 CONTENTS is nil. INFO is a plist holding contextual information."
842 (org-element-property :value statistics-cookie))
845 ;;; Strike-Through
847 (defun org-man-strike-through (strike-through contents info)
848 "Transcode STRIKE-THROUGH from Org to Man.
849 CONTENTS is the text with strike-through markup. INFO is a plist
850 holding contextual information."
851 (format "\\fI%s\\fP" contents))
853 ;;; Subscript
855 (defun org-man-subscript (subscript contents info)
856 "Transcode a SUBSCRIPT object from Org to Man.
857 CONTENTS is the contents of the object. INFO is a plist holding
858 contextual information."
859 (format "\\d\\s-2%s\\s+2\\u" contents))
861 ;;; Superscript "^_%s$
863 (defun org-man-superscript (superscript contents info)
864 "Transcode a SUPERSCRIPT object from Org to Man.
865 CONTENTS is the contents of the object. INFO is a plist holding
866 contextual information."
867 (format "\\u\\s-2%s\\s+2\\d" contents))
870 ;;; Table
872 ;; `org-man-table' is the entry point for table transcoding. It
873 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
874 ;; delegates the job to either `org-man-table--table.el-table' or
875 ;; `org-man-table--org-table' functions, depending of the type of
876 ;; the table.
878 ;; `org-man-table--align-string' is a subroutine used to build
879 ;; alignment string for Org tables.
881 (defun org-man-table (table contents info)
882 "Transcode a TABLE element from Org to Man.
883 CONTENTS is the contents of the table. INFO is a plist holding
884 contextual information."
885 (cond
886 ;; Case 1: verbatim table.
887 ((or org-man-tables-verbatim
888 (let ((attr (read (format "(%s)"
889 (mapconcat
890 #'identity
891 (org-element-property :attr_man table)
892 " ")))))
894 (and attr (plist-get attr :verbatim))))
896 (format ".nf\n\\fC%s\\fP\n.fi"
897 ;; Re-create table, without affiliated keywords.
898 (org-trim
899 (org-element-interpret-data
900 `(table nil ,@(org-element-contents table))))))
901 ;; Case 2: Standard table.
902 (t (org-man-table--org-table table contents info))))
904 (defun org-man-table--align-string (divider table info)
905 "Return an appropriate Man alignment string.
906 TABLE is the considered table. INFO is a plist used as
907 a communication channel."
908 (let (alignment)
909 ;; Extract column groups and alignment from first (non-rule) row.
910 (org-element-map
911 (org-element-map table 'table-row
912 (lambda (row)
913 (and (eq (org-element-property :type row) 'standard) row))
914 info 'first-match)
915 'table-cell
916 (lambda (cell)
917 (let* ((borders (org-export-table-cell-borders cell info))
918 (raw-width (org-export-table-cell-width cell info))
919 (width-cm (when raw-width (/ raw-width 5)))
920 (width (if raw-width (format "w(%dc)"
921 (if (< width-cm 1) 1 width-cm)) "")))
922 ;; Check left border for the first cell only.
923 (when (and (memq 'left borders) (not alignment))
924 (push "|" alignment))
925 (push
926 (case (org-export-table-cell-alignment cell info)
927 (left (concat "l" width divider))
928 (right (concat "r" width divider))
929 (center (concat "c" width divider)))
930 alignment)
931 (when (memq 'right borders) (push "|" alignment))))
932 info)
933 (apply 'concat (reverse alignment))))
935 (defun org-man-table--org-table (table contents info)
936 "Return appropriate Man code for an Org table.
938 TABLE is the table type element to transcode. CONTENTS is its
939 contents, as a string. INFO is a plist used as a communication
940 channel.
942 This function assumes TABLE has `org' as its `:type' attribute."
943 (let* ((attr (org-export-read-attribute :attr_man table))
944 (label (org-element-property :name table))
945 (caption (and (not (plist-get attr :disable-caption))
946 (org-man--caption/label-string table info)))
947 (divider (if (plist-get attr :divider) "|" " "))
949 ;; Determine alignment string.
950 (alignment (org-man-table--align-string divider table info))
951 ;; Extract others display options.
953 (lines (org-split-string contents "\n"))
955 (attr-list
956 (delq nil
957 (list
958 (and (plist-get attr :expand) "expand")
959 (let ((placement (plist-get attr :placement)))
960 (cond ((string= placement 'center) "center")
961 ((string= placement 'left) nil)
962 (t (if org-man-tables-centered "center" ""))))
963 (or (plist-get attr :boxtype) "box"))))
965 (title-line (plist-get attr :title-line))
966 (long-cells (plist-get attr :long-cells))
968 (table-format (concat
969 (format "%s" (or (car attr-list) "" ))
971 (let ((output-list '()))
972 (when (cdr attr-list)
973 (dolist (attr-item (cdr attr-list))
974 (setq output-list (concat output-list (format ",%s" attr-item)))))
975 output-list)
976 "")))
978 (first-line (when lines (org-split-string (car lines) "\t"))))
979 ;; Prepare the final format string for the table.
982 (cond
983 ;; Others.
984 (lines (concat ".TS\n " table-format ";\n"
986 (format "%s.\n"
987 (let ((final-line ""))
988 (when title-line
989 (dotimes (i (length first-line))
990 (setq final-line (concat final-line "cb" divider))))
992 (setq final-line (concat final-line "\n"))
994 (if alignment
995 (setq final-line (concat final-line alignment))
996 (dotimes (i (length first-line))
997 (setq final-line (concat final-line "c" divider))))
998 final-line ))
1000 (format "%s.TE\n"
1001 (let ((final-line "")
1002 (long-line "")
1003 (lines (org-split-string contents "\n")))
1005 (dolist (line-item lines)
1006 (setq long-line "")
1008 (if long-cells
1009 (progn
1010 (if (string= line-item "_")
1011 (setq long-line (format "%s\n" line-item))
1012 ;; else string =
1013 (let ((cell-item-list (org-split-string line-item "\t")))
1014 (dolist (cell-item cell-item-list)
1016 (cond ((eq cell-item (car (last cell-item-list)))
1017 (setq long-line (concat long-line
1018 (format "T{\n%s\nT}\t\n" cell-item ))))
1020 (setq long-line (concat long-line
1021 (format "T{\n%s\nT}\t" cell-item ))))))
1022 long-line))
1023 ;; else long cells
1024 (setq final-line (concat final-line long-line )))
1026 (setq final-line (concat final-line line-item "\n"))))
1027 final-line))
1029 (and caption (format ".TB \"%s\"" caption)))))))
1031 ;;; Table Cell
1033 (defun org-man-table-cell (table-cell contents info)
1034 "Transcode a TABLE-CELL element from Org to Man
1035 CONTENTS is the cell contents. INFO is a plist used as
1036 a communication channel."
1037 (concat (if (and contents
1038 org-man-table-scientific-notation
1039 (string-match orgtbl-exp-regexp contents))
1040 ;; Use appropriate format string for scientific
1041 ;; notation.
1042 (format org-man-table-scientific-notation
1043 (match-string 1 contents)
1044 (match-string 2 contents))
1045 contents )
1046 (when (org-export-get-next-element table-cell info) "\t")))
1049 ;;; Table Row
1051 (defun org-man-table-row (table-row contents info)
1052 "Transcode a TABLE-ROW element from Org to Man
1053 CONTENTS is the contents of the row. INFO is a plist used as
1054 a communication channel."
1055 ;; Rules are ignored since table separators are deduced from
1056 ;; borders of the current row.
1057 (when (eq (org-element-property :type table-row) 'standard)
1058 (let* ((attr (mapconcat 'identity
1059 (org-element-property
1060 :attr_man (org-export-get-parent table-row))
1061 " "))
1062 ;; TABLE-ROW's borders are extracted from its first cell.
1063 (borders
1064 (org-export-table-cell-borders
1065 (car (org-element-contents table-row)) info)))
1066 (concat
1067 ;; Mark horizontal lines
1068 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1069 contents
1071 (cond
1072 ;; When BOOKTABS are activated enforce bottom rule even when
1073 ;; no hline was specifically marked.
1074 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1075 ((memq 'below borders) "\n_"))))))
1078 ;;; Target
1080 (defun org-man-target (target contents info)
1081 "Transcode a TARGET object from Org to Man.
1082 CONTENTS is nil. INFO is a plist holding contextual
1083 information."
1084 (format "\\fI%s\\fP"
1085 (org-export-solidify-link-text (org-element-property :value target))))
1088 ;;; Timestamp
1090 (defun org-man-timestamp (timestamp contents info)
1091 "Transcode a TIMESTAMP object from Org to Man.
1092 CONTENTS is nil. INFO is a plist holding contextual
1093 information."
1094 "" )
1097 ;;; Underline
1099 (defun org-man-underline (underline contents info)
1100 "Transcode UNDERLINE from Org to Man.
1101 CONTENTS is the text with underline markup. INFO is a plist
1102 holding contextual information."
1103 (format "\\fI%s\\fP" contents))
1106 ;;; Verbatim
1108 (defun org-man-verbatim (verbatim contents info)
1109 "Transcode a VERBATIM object from Org to Man.
1110 CONTENTS is nil. INFO is a plist used as a communication
1111 channel."
1112 (format ".nf\n%s\n.fi" contents))
1115 ;;; Verse Block
1117 (defun org-man-verse-block (verse-block contents info)
1118 "Transcode a VERSE-BLOCK element from Org to Man.
1119 CONTENTS is verse block contents. INFO is a plist holding
1120 contextual information."
1121 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1125 ;;; Interactive functions
1127 (defun org-man-export-to-man
1128 (&optional async subtreep visible-only body-only ext-plist)
1129 "Export current buffer to a Man file.
1131 If narrowing is active in the current buffer, only export its
1132 narrowed part.
1134 If a region is active, export that region.
1136 A non-nil optional argument ASYNC means the process should happen
1137 asynchronously. The resulting file should be accessible through
1138 the `org-export-stack' interface.
1140 When optional argument SUBTREEP is non-nil, export the sub-tree
1141 at point, extracting information from the headline properties
1142 first.
1144 When optional argument VISIBLE-ONLY is non-nil, don't export
1145 contents of hidden elements.
1147 When optional argument BODY-ONLY is non-nil, only the body
1148 without any markers.
1150 EXT-PLIST, when provided, is a property list with external
1151 parameters overriding Org default settings, but still inferior to
1152 file-local settings.
1154 Return output file's name."
1155 (interactive)
1156 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1157 (org-export-to-file 'man outfile
1158 async subtreep visible-only body-only ext-plist)))
1160 (defun org-man-export-to-pdf
1161 (&optional async subtreep visible-only body-only ext-plist)
1162 "Export current buffer to Groff then process through to PDF.
1164 If narrowing is active in the current buffer, only export its
1165 narrowed part.
1167 If a region is active, export that region.
1169 A non-nil optional argument ASYNC means the process should happen
1170 asynchronously. The resulting file should be accessible through
1171 the `org-export-stack' interface.
1173 When optional argument SUBTREEP is non-nil, export the sub-tree
1174 at point, extracting information from the headline properties
1175 first.
1177 When optional argument VISIBLE-ONLY is non-nil, don't export
1178 contents of hidden elements.
1180 When optional argument BODY-ONLY is non-nil, only write between
1181 markers.
1183 EXT-PLIST, when provided, is a property list with external
1184 parameters overriding Org default settings, but still inferior to
1185 file-local settings.
1187 Return PDF file's name."
1188 (interactive)
1189 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1190 (org-export-to-file 'man outfile
1191 async subtreep visible-only body-only ext-plist
1192 (lambda (file) (org-latex-compile file)))))
1194 (defun org-man-compile (file)
1195 "Compile a Groff file.
1197 FILE is the name of the file being compiled. Processing is done
1198 through the command specified in `org-man-pdf-process'.
1200 Return PDF file name or an error if it couldn't be produced."
1201 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1202 (full-name (file-truename file))
1203 (out-dir (file-name-directory file))
1204 ;; Properly set working directory for compilation.
1205 (default-directory (if (file-name-absolute-p file)
1206 (file-name-directory full-name)
1207 default-directory))
1208 errors)
1209 (message (format "Processing Groff file %s..." file))
1210 (save-window-excursion
1211 (cond
1212 ;; A function is provided: Apply it.
1213 ((functionp org-man-pdf-process)
1214 (funcall org-man-pdf-process (shell-quote-argument file)))
1215 ;; A list is provided: Replace %b, %f and %o with appropriate
1216 ;; values in each command before applying it. Output is
1217 ;; redirected to "*Org PDF Groff Output*" buffer.
1218 ((consp org-man-pdf-process)
1219 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1220 (mapc
1221 (lambda (command)
1222 (shell-command
1223 (replace-regexp-in-string
1224 "%b" (shell-quote-argument base-name)
1225 (replace-regexp-in-string
1226 "%f" (shell-quote-argument full-name)
1227 (replace-regexp-in-string
1228 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1229 outbuf))
1230 org-man-pdf-process)
1231 ;; Collect standard errors from output buffer.
1232 (setq errors (org-man-collect-errors outbuf))))
1233 (t (error "No valid command to process to PDF")))
1234 (let ((pdffile (concat out-dir base-name ".pdf")))
1235 ;; Check for process failure. Provide collected errors if
1236 ;; possible.
1237 (if (not (file-exists-p pdffile))
1238 (error (concat (format "PDF file %s wasn't produced" pdffile)
1239 (when errors (concat ": " errors))))
1240 ;; Else remove log files, when specified, and signal end of
1241 ;; process to user, along with any error encountered.
1242 (when org-man-remove-logfiles
1243 (dolist (ext org-man-logfiles-extensions)
1244 (let ((file (concat out-dir base-name "." ext)))
1245 (when (file-exists-p file) (delete-file file)))))
1246 (message (concat "Process completed"
1247 (if (not errors) "."
1248 (concat " with errors: " errors)))))
1249 ;; Return output file name.
1250 pdffile))))
1252 (defun org-man-collect-errors (buffer)
1253 "Collect some kind of errors from \"groff\" output
1254 BUFFER is the buffer containing output.
1255 Return collected error types as a string, or nil if there was
1256 none."
1257 (with-current-buffer buffer
1258 (save-excursion
1259 (goto-char (point-max))
1260 ;; Find final run
1261 nil )))
1264 (provide 'ox-man)
1266 ;; Local variables:
1267 ;; generated-autoload-file: "org-loaddefs.el"
1268 ;; End:
1270 ;;; ox-man.el ends here