org-element: `org-element-parse-secondary-string' accepts nil argument
[org-mode.git] / lisp / ox-man.el
blobb43c7f640a0423c69c721940313f04de89b6c17f
1 ;; ox-man.el --- Man Back-End for Org Export Engine
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements a Man back-end for Org generic exporter.
28 ;; To test it, run
30 ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
32 ;; in an org-mode buffer then switch to the buffer to see the Man
33 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'ox)
42 (eval-when-compile (require 'cl))
44 (defvar org-export-man-default-packages-alist)
45 (defvar org-export-man-packages-alist)
46 (defvar orgtbl-exp-regexp)
50 ;;; Define Back-End
52 (org-export-define-backend 'man
53 '((babel-call . org-man-babel-call)
54 (bold . org-man-bold)
55 (center-block . org-man-center-block)
56 (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)))
125 ;;; User Configurable Variables
127 (defgroup org-export-man nil
128 "Options for exporting Org mode files to Man."
129 :tag "Org Export Man"
130 :group 'org-export)
132 ;;; Tables
134 (defcustom org-man-tables-centered t
135 "When non-nil, tables are exported in a center environment."
136 :group 'org-export-man
137 :version "24.4"
138 :package-version '(Org . "8.0")
139 :type 'boolean)
141 (defcustom org-man-tables-verbatim nil
142 "When non-nil, tables are exported verbatim."
143 :group 'org-export-man
144 :version "24.4"
145 :package-version '(Org . "8.0")
146 :type 'boolean)
149 (defcustom org-man-table-scientific-notation "%sE%s"
150 "Format string to display numbers in scientific notation.
151 The format should have \"%s\" twice, for mantissa and exponent
152 \(i.e. \"%s\\\\times10^{%s}\").
154 When nil, no transformation is made."
155 :group 'org-export-man
156 :version "24.4"
157 :package-version '(Org . "8.0")
158 :type '(choice
159 (string :tag "Format string")
160 (const :tag "No formatting")))
163 ;;; Inlinetasks
164 ;; Src blocks
166 (defcustom org-man-source-highlight nil
167 "Use GNU source highlight to embellish source blocks "
168 :group 'org-export-man
169 :version "24.4"
170 :package-version '(Org . "8.0")
171 :type 'boolean)
174 (defcustom org-man-source-highlight-langs
175 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
176 (scheme "scheme")
177 (c "c") (cc "cpp") (csharp "csharp") (d "d")
178 (fortran "fortran") (cobol "cobol") (pascal "pascal")
179 (ada "ada") (asm "asm")
180 (perl "perl") (cperl "perl")
181 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
182 (java "java") (javascript "javascript")
183 (tex "latex")
184 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
185 (ocaml "caml") (caml "caml")
186 (sql "sql") (sqlite "sql")
187 (html "html") (css "css") (xml "xml")
188 (bat "bat") (bison "bison") (clipper "clipper")
189 (ldap "ldap") (opa "opa")
190 (php "php") (postscript "postscript") (prolog "prolog")
191 (properties "properties") (makefile "makefile")
192 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
193 "Alist mapping languages to their listing language counterpart.
194 The key is a symbol, the major mode symbol without the \"-mode\".
195 The value is the string that should be inserted as the language
196 parameter for the listings package. If the mode name and the
197 listings name are the same, the language does not need an entry
198 in this list - but it does not hurt if it is present."
199 :group 'org-export-man
200 :version "24.4"
201 :package-version '(Org . "8.0")
202 :type '(repeat
203 (list
204 (symbol :tag "Major mode ")
205 (string :tag "Listings language"))))
209 (defvar org-man-custom-lang-environments nil
210 "Alist mapping languages to language-specific Man environments.
212 It is used during export of src blocks by the listings and
213 man packages. For example,
215 \(setq org-man-custom-lang-environments
216 '\(\(python \"pythoncode\"\)\)\)
218 would have the effect that if org encounters begin_src python
219 during man export."
223 ;;; Compilation
225 (defcustom org-man-pdf-process
226 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
227 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
228 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
230 "Commands to process a Man file to a PDF file.
231 This is a list of strings, each of them will be given to the
232 shell as a command. %f in the command will be replaced by the
233 full file name, %b by the file base name (i.e. without directory
234 and extension parts) and %o by the base directory of the file.
237 By default, Org uses 3 runs of to do the processing.
239 Alternatively, this may be a Lisp function that does the
240 processing. This function should accept the file name as
241 its single argument."
242 :group 'org-export-pdf
243 :group 'org-export-man
244 :version "24.4"
245 :package-version '(Org . "8.0")
246 :type '(choice
247 (repeat :tag "Shell command sequence"
248 (string :tag "Shell command"))
249 (const :tag "2 runs of pdfgroff"
250 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
251 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
252 (const :tag "3 runs of pdfgroff"
253 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
254 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
255 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
256 (function)))
258 (defcustom org-man-logfiles-extensions
259 '("log" "out" "toc")
260 "The list of file extensions to consider as Man logfiles."
261 :group 'org-export-man
262 :version "24.4"
263 :package-version '(Org . "8.0")
264 :type '(repeat (string :tag "Extension")))
266 (defcustom org-man-remove-logfiles t
267 "Non-nil means remove the logfiles produced by PDF production.
268 These are the .aux, .log, .out, and .toc files."
269 :group 'org-export-man
270 :version "24.4"
271 :package-version '(Org . "8.0")
272 :type 'boolean)
276 ;;; Internal Functions
278 (defun org-man--caption/label-string (element info)
279 "Return caption and label Man string for ELEMENT.
281 INFO is a plist holding contextual information. If there's no
282 caption nor label, return the empty string.
284 For non-floats, see `org-man--wrap-label'."
285 (let ((label (org-element-property :label element))
286 (main (org-export-get-caption element))
287 (short (org-export-get-caption element t)))
288 (cond ((and (not main) (not label)) "")
289 ((not main) (format "\\fI%s\\fP" label))
290 ;; Option caption format with short name.
291 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
292 (org-export-data short info)
293 (org-export-data main info)))
294 ;; Standard caption format.
295 (t (format "\\fR%s\\fP" (org-export-data main info))))))
297 (defun org-man--wrap-label (element output)
298 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
299 This function shouldn't be used for floats. See
300 `org-man--caption/label-string'."
301 (let ((label (org-element-property :name element)))
302 (if (or (not output) (not label) (string= output "") (string= label ""))
303 output
304 (concat (format "%s\n.br\n" label) output))))
308 ;;; Template
310 (defun org-man-template (contents info)
311 "Return complete document string after Man conversion.
312 CONTENTS is the transcoded contents string. INFO is a plist
313 holding export options."
314 (let* ((title (when (plist-get info :with-title)
315 (org-export-data (plist-get info :title) info)))
316 (attr (read (format "(%s)"
317 (mapconcat
318 #'identity
319 (list (plist-get info :man-class-options))
320 " "))))
321 (section-item (plist-get attr :section-id)))
323 (concat
325 (cond
326 ((and title (stringp section-item))
327 (format ".TH \"%s\" \"%s\" \n" title section-item))
328 ((and (string= "" title) (stringp section-item))
329 (format ".TH \"%s\" \"%s\" \n" " " section-item))
330 (title
331 (format ".TH \"%s\" \"1\" \n" title))
333 ".TH \" \" \"1\" "))
334 contents)))
339 ;;; Transcode Functions
341 ;;; Babel Call
343 ;; Babel Calls are ignored.
346 ;;; Bold
348 (defun org-man-bold (bold contents info)
349 "Transcode BOLD from Org to Man.
350 CONTENTS is the text with bold markup. INFO is a plist holding
351 contextual information."
352 (format "\\fB%s\\fP" contents))
355 ;;; Center Block
357 (defun org-man-center-block (center-block contents info)
358 "Transcode a CENTER-BLOCK element from Org to Man.
359 CONTENTS holds the contents of the center block. INFO is a plist
360 holding contextual information."
361 (org-man--wrap-label
362 center-block
363 (format ".ce %d\n.nf\n%s\n.fi"
364 (- (length (split-string contents "\n")) 1 )
365 contents)))
368 ;;; Clock
370 (defun org-man-clock (clock contents info)
371 "Transcode a CLOCK element from Org to Man.
372 CONTENTS is nil. INFO is a plist holding contextual
373 information."
374 "" )
377 ;;; Code
379 (defun org-man-code (code contents info)
380 "Transcode a CODE object from Org to Man.
381 CONTENTS is nil. INFO is a plist used as a communication
382 channel."
383 (format "\\fC%s\\fP" code))
386 ;;; Comment
388 ;; Comments are ignored.
391 ;;; Comment Block
393 ;; Comment Blocks are ignored.
396 ;;; Drawer
398 (defun org-man-drawer (drawer contents info)
399 "Transcode a DRAWER element from Org to Man.
400 DRAWER holds the drawer information
401 CONTENTS holds the contents of the block.
402 INFO is a plist holding contextual information. "
403 contents)
406 ;;; Dynamic Block
408 (defun org-man-dynamic-block (dynamic-block contents info)
409 "Transcode a DYNAMIC-BLOCK element from Org to Man.
410 CONTENTS holds the contents of the block. INFO is a plist
411 holding contextual information. See `org-export-data'."
412 (org-man--wrap-label dynamic-block contents))
415 ;;; Entity
417 (defun org-man-entity (entity contents info)
418 "Transcode an ENTITY object from Org to Man.
419 CONTENTS are the definition itself. INFO is a plist holding
420 contextual information."
421 (org-element-property :utf-8 entity))
424 ;;; Example Block
426 (defun org-man-example-block (example-block contents info)
427 "Transcode an EXAMPLE-BLOCK element from Org to Man.
428 CONTENTS is nil. INFO is a plist holding contextual
429 information."
430 (org-man--wrap-label
431 example-block
432 (format ".RS\n.nf\n%s\n.fi\n.RE"
433 (org-export-format-code-default example-block info))))
436 ;;; Export Block
438 (defun org-man-export-block (export-block contents info)
439 "Transcode a EXPORT-BLOCK element from Org to Man.
440 CONTENTS is nil. INFO is a plist holding contextual information."
441 (when (string= (org-element-property :type export-block) "MAN")
442 (org-remove-indentation (org-element-property :value export-block))))
445 ;;; Export Snippet
447 (defun org-man-export-snippet (export-snippet contents info)
448 "Transcode a EXPORT-SNIPPET object from Org to Man.
449 CONTENTS is nil. INFO is a plist holding contextual information."
450 (when (eq (org-export-snippet-backend export-snippet) 'man)
451 (org-element-property :value export-snippet)))
454 ;;; Fixed Width
456 (defun org-man-fixed-width (fixed-width contents info)
457 "Transcode a FIXED-WIDTH element from Org to Man.
458 CONTENTS is nil. INFO is a plist holding contextual information."
459 (org-man--wrap-label
460 fixed-width
461 (format "\\fC\n%s\\fP"
462 (org-remove-indentation
463 (org-element-property :value fixed-width)))))
466 ;;; Footnote Definition
468 ;; Footnote Definitions are ignored.
470 ;;; Footnote References
472 ;; Footnote References are Ignored
475 ;;; Headline
477 (defun org-man-headline (headline contents info)
478 "Transcode a HEADLINE element from Org to Man.
479 CONTENTS holds the contents of the headline. INFO is a plist
480 holding contextual information."
481 (let* ((level (org-export-get-relative-level headline info))
482 (numberedp (org-export-numbered-headline-p headline info))
483 ;; Section formatting will set two placeholders: one for the
484 ;; title and the other for the contents.
485 (section-fmt
486 (case level
487 (1 ".SH \"%s\"\n%s")
488 (2 ".SS \"%s\"\n%s")
489 (3 ".SS \"%s\"\n%s")
490 (t nil)))
491 (text (org-export-data (org-element-property :title headline) info)))
493 (cond
494 ;; Case 1: This is a footnote section: ignore it.
495 ((org-element-property :footnote-section-p headline) nil)
497 ;; Case 2. This is a deep sub-tree: export it as a list item.
498 ;; Also export as items headlines for which no section
499 ;; format has been found.
500 ((or (not section-fmt) (org-export-low-level-p headline info))
501 ;; Build the real contents of the sub-tree.
502 (let ((low-level-body
503 (concat
504 ;; If the headline is the first sibling, start a list.
505 (when (org-export-first-sibling-p headline info)
506 (format "%s\n" ".RS"))
507 ;; Itemize headline
508 ".TP\n.ft I\n" text "\n.ft\n"
509 contents ".RE")))
510 ;; If headline is not the last sibling simply return
511 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
512 ;; blank line.
513 (if (not (org-export-last-sibling-p headline info)) low-level-body
514 (replace-regexp-in-string
515 "[ \t\n]*\\'" ""
516 low-level-body))))
518 ;; Case 3. Standard headline. Export it as a section.
519 (t (format section-fmt text contents )))))
521 ;;; Horizontal Rule
522 ;; Not supported
524 ;;; Inline Babel Call
526 ;; Inline Babel Calls are ignored.
528 ;;; Inline Src Block
530 (defun org-man-inline-src-block (inline-src-block contents info)
531 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
532 CONTENTS holds the contents of the item. INFO is a plist holding
533 contextual information."
534 (let* ((code (org-element-property :value inline-src-block)))
535 (cond
536 ((plist-get info :man-source-highlight)
537 (let* ((tmpdir (if (featurep 'xemacs)
538 temp-directory
539 temporary-file-directory ))
540 (in-file (make-temp-name
541 (expand-file-name "srchilite" tmpdir)))
542 (out-file (make-temp-name
543 (expand-file-name "reshilite" tmpdir)))
544 (org-lang (org-element-property :language inline-src-block))
545 (lst-lang
546 (cadr (assq (intern org-lang)
547 (plist-get info :man-source-highlight-langs))))
549 (cmd (concat (expand-file-name "source-highlight")
550 " -s " lst-lang
551 " -f groff_man"
552 " -i " in-file
553 " -o " out-file )))
555 (if lst-lang
556 (let ((code-block "" ))
557 (with-temp-file in-file (insert code))
558 (shell-command cmd)
559 (setq code-block (org-file-contents out-file))
560 (delete-file in-file)
561 (delete-file out-file)
562 code-block)
563 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
564 code))))
566 ;; Do not use a special package: transcode it verbatim.
568 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
569 "\\fP\n.fi\n.RE\n")))))
572 ;;; Inlinetask
573 ;;; Italic
575 (defun org-man-italic (italic contents info)
576 "Transcode ITALIC from Org to Man.
577 CONTENTS is the text with italic markup. INFO is a plist holding
578 contextual information."
579 (format "\\fI%s\\fP" contents))
582 ;;; Item
585 (defun org-man-item (item contents info)
587 "Transcode an ITEM element from Org to Man.
588 CONTENTS holds the contents of the item. INFO is a plist holding
589 contextual information."
591 (let* ((bullet (org-element-property :bullet item))
592 (type (org-element-property :type (org-element-property :parent item)))
593 (checkbox (case (org-element-property :checkbox item)
594 (on "\\o'\\(sq\\(mu'") ;;
595 (off "\\(sq ") ;;
596 (trans "\\o'\\(sq\\(mi'" ))) ;;
598 (tag (let ((tag (org-element-property :tag item)))
599 ;; Check-boxes must belong to the tag.
600 (and tag (format "\\fB%s\\fP"
601 (concat checkbox
602 (org-export-data tag info)))))))
604 (if (and (null tag )
605 (null checkbox))
606 (let* ((bullet (org-trim bullet))
607 (marker (cond ((string= "-" bullet) "\\(em")
608 ((string= "*" bullet) "\\(bu")
609 ((eq type 'ordered)
610 (format "%s " (org-trim bullet)))
611 (t "\\(dg"))))
612 (concat ".IP " marker " 4\n"
613 (org-trim (or contents " " ))))
614 ; else
615 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
616 (org-trim (or contents " " ))))))
618 ;;; Keyword
621 (defun org-man-keyword (keyword contents info)
622 "Transcode a KEYWORD element from Org to Man.
623 CONTENTS is nil. INFO is a plist holding contextual information."
624 (let ((key (org-element-property :key keyword))
625 (value (org-element-property :value keyword)))
626 (cond
627 ((string= key "MAN") value)
628 ((string= key "INDEX") nil)
629 ((string= key "TOC" ) nil))))
632 ;;; Line Break
634 (defun org-man-line-break (line-break contents info)
635 "Transcode a LINE-BREAK object from Org to Man.
636 CONTENTS is nil. INFO is a plist holding contextual information."
637 ".br\n")
640 ;;; Link
643 (defun org-man-link (link desc info)
644 "Transcode a LINK object from Org to Man.
646 DESC is the description part of the link, or the empty string.
647 INFO is a plist holding contextual information. See
648 `org-export-data'."
649 (let* ((type (org-element-property :type link))
650 (raw-path (org-element-property :path link))
651 ;; Ensure DESC really exists, or set it to nil.
652 (desc (and (not (string= desc "")) desc))
653 (path (cond
654 ((member type '("http" "https" "ftp" "mailto"))
655 (concat type ":" raw-path))
656 ((and (string= type "file") (file-name-absolute-p raw-path))
657 (concat "file:" raw-path))
658 (t raw-path)))
659 protocol)
660 (cond
661 ;; Link type is handled by a special function.
662 ((org-export-custom-protocol-maybe link desc 'man))
663 ;; External link with a description part.
664 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
665 ;; External link without a description part.
666 (path (format "\\fI%s\\fP" path))
667 ;; No path, only description. Try to do something useful.
668 (t (format "\\fI%s\\fP" desc)))))
670 ;;;; Node Property
672 (defun org-man-node-property (node-property contents info)
673 "Transcode a NODE-PROPERTY element from Org to Man.
674 CONTENTS is nil. INFO is a plist holding contextual
675 information."
676 (format "%s:%s"
677 (org-element-property :key node-property)
678 (let ((value (org-element-property :value node-property)))
679 (if value (concat " " value) ""))))
681 ;;; Paragraph
683 (defun org-man-paragraph (paragraph contents info)
684 "Transcode a PARAGRAPH element from Org to Man.
685 CONTENTS is the contents of the paragraph, as a string. INFO is
686 the plist used as a communication channel."
687 (let ((parent (plist-get (nth 1 paragraph) :parent)))
688 (when parent
689 (let ((parent-type (car parent))
690 (fixed-paragraph ""))
691 (cond ((and (eq parent-type 'item)
692 (plist-get (nth 1 parent) :bullet ))
693 (setq fixed-paragraph (concat "" contents)))
694 ((eq parent-type 'section)
695 (setq fixed-paragraph (concat ".PP\n" contents)))
696 ((eq parent-type 'footnote-definition)
697 (setq fixed-paragraph contents))
698 (t (setq fixed-paragraph (concat "" contents))))
699 fixed-paragraph ))))
702 ;;; Plain List
704 (defun org-man-plain-list (plain-list contents info)
705 "Transcode a PLAIN-LIST element from Org to Man.
706 CONTENTS is the contents of the list. INFO is a plist holding
707 contextual information."
708 contents)
710 ;;; Plain Text
712 (defun org-man-plain-text (text info)
713 "Transcode a TEXT string from Org to Man.
714 TEXT is the string to transcode. INFO is a plist holding
715 contextual information."
716 (let ((output text))
717 ;; Protect various chars.
718 (setq output (replace-regexp-in-string
719 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
720 "$\\" output nil t 1))
721 ;; Activate smart quotes. Be sure to provide original TEXT string
722 ;; since OUTPUT may have been modified.
723 (when (plist-get info :with-smart-quotes)
724 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
725 ;; Handle break preservation if required.
726 (when (plist-get info :preserve-breaks)
727 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
728 output)))
729 ;; Return value.
730 output))
734 ;;; Planning
737 ;;; Property Drawer
739 (defun org-man-property-drawer (property-drawer contents info)
740 "Transcode a PROPERTY-DRAWER element from Org to Man.
741 CONTENTS holds the contents of the drawer. INFO is a plist
742 holding contextual information."
743 (and (org-string-nw-p contents)
744 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
746 ;;; Quote Block
748 (defun org-man-quote-block (quote-block contents info)
749 "Transcode a QUOTE-BLOCK element from Org to Man.
750 CONTENTS holds the contents of the block. INFO is a plist
751 holding contextual information."
752 (org-man--wrap-label
753 quote-block
754 (format ".RS\n%s\n.RE" contents)))
757 ;;; Radio Target
759 (defun org-man-radio-target (radio-target text info)
760 "Transcode a RADIO-TARGET object from Org to Man.
761 TEXT is the text of the target. INFO is a plist holding
762 contextual information."
763 text )
766 ;;; Section
768 (defun org-man-section (section contents info)
769 "Transcode a SECTION element from Org to Man.
770 CONTENTS holds the contents of the section. INFO is a plist
771 holding contextual information."
772 contents)
775 ;;; Special Block
777 (defun org-man-special-block (special-block contents info)
778 "Transcode a SPECIAL-BLOCK element from Org to Man.
779 CONTENTS holds the contents of the block. INFO is a plist
780 holding contextual information."
781 (let ((type (org-element-property :type special-block)))
782 (org-man--wrap-label
783 special-block
784 (format "%s\n" contents))))
787 ;;; Src Block
789 (defun org-man-src-block (src-block contents info)
790 "Transcode a SRC-BLOCK element from Org to Man.
791 CONTENTS holds the contents of the item. INFO is a plist holding
792 contextual information."
793 (let* ((lang (org-element-property :language src-block))
794 (code (org-element-property :value src-block))
795 (custom-env (and lang
796 (cadr (assq (intern lang)
797 org-man-custom-lang-environments))))
798 (num-start (case (org-element-property :number-lines src-block)
799 (continued (org-export-get-loc src-block info))
800 (new 0)))
801 (retain-labels (org-element-property :retain-labels src-block)))
802 (if (not (plist-get info :man-source-highlight))
803 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
804 (org-export-format-code-default src-block info))
805 (let* ((tmpdir (if (featurep 'xemacs) temp-directory
806 temporary-file-directory))
807 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
808 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
809 (org-lang (org-element-property :language src-block))
810 (lst-lang
811 (cadr (assq (intern org-lang)
812 (plist-get info :man-source-highlight-langs))))
813 (cmd (concat "source-highlight"
814 " -s " lst-lang
815 " -f groff_man "
816 " -i " in-file
817 " -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 (plist-get info :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 (delq nil
949 (list
950 (and (plist-get attr :expand) "expand")
951 (let ((placement (plist-get attr :placement)))
952 (cond ((string= placement 'center) "center")
953 ((string= placement 'left) nil)
954 ((plist-get info :man-tables-centered) "center")
955 (t "")))
956 (or (plist-get attr :boxtype) "box"))))
958 (title-line (plist-get attr :title-line))
959 (long-cells (plist-get attr :long-cells))
961 (table-format (concat
962 (format "%s" (or (car attr-list) "" ))
964 (let ((output-list '()))
965 (when (cdr attr-list)
966 (dolist (attr-item (cdr attr-list))
967 (setq output-list (concat output-list (format ",%s" attr-item)))))
968 output-list)
969 "")))
971 (first-line (when lines (org-split-string (car lines) "\t"))))
972 ;; Prepare the final format string for the table.
975 (cond
976 ;; Others.
977 (lines (concat ".TS\n " table-format ";\n"
979 (format "%s.\n"
980 (let ((final-line ""))
981 (when title-line
982 (dotimes (i (length first-line))
983 (setq final-line (concat final-line "cb" divider))))
985 (setq final-line (concat final-line "\n"))
987 (if alignment
988 (setq final-line (concat final-line alignment))
989 (dotimes (i (length first-line))
990 (setq final-line (concat final-line "c" divider))))
991 final-line ))
993 (format "%s.TE\n"
994 (let ((final-line "")
995 (long-line "")
996 (lines (org-split-string contents "\n")))
998 (dolist (line-item lines)
999 (setq long-line "")
1001 (if long-cells
1002 (progn
1003 (if (string= line-item "_")
1004 (setq long-line (format "%s\n" line-item))
1005 ;; else string =
1006 (let ((cell-item-list (org-split-string line-item "\t")))
1007 (dolist (cell-item cell-item-list)
1009 (cond ((eq cell-item (car (last cell-item-list)))
1010 (setq long-line (concat long-line
1011 (format "T{\n%s\nT}\t\n" cell-item ))))
1013 (setq long-line (concat long-line
1014 (format "T{\n%s\nT}\t" cell-item ))))))
1015 long-line))
1016 ;; else long cells
1017 (setq final-line (concat final-line long-line )))
1019 (setq final-line (concat final-line line-item "\n"))))
1020 final-line))
1022 (and caption (format ".TB \"%s\"" caption)))))))
1024 ;;; Table Cell
1026 (defun org-man-table-cell (table-cell contents info)
1027 "Transcode a TABLE-CELL element from Org to Man
1028 CONTENTS is the cell contents. INFO is a plist used as
1029 a communication channel."
1030 (concat
1031 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
1032 (if (and contents
1033 scientific-format
1034 (string-match orgtbl-exp-regexp contents))
1035 ;; Use appropriate format string for scientific notation.
1036 (format scientific-format
1037 (match-string 1 contents)
1038 (match-string 2 contents))
1039 contents))
1040 (when (org-export-get-next-element table-cell info) "\t")))
1043 ;;; Table Row
1045 (defun org-man-table-row (table-row contents info)
1046 "Transcode a TABLE-ROW element from Org to Man
1047 CONTENTS is the contents of the row. INFO is a plist used as
1048 a communication channel."
1049 ;; Rules are ignored since table separators are deduced from
1050 ;; borders of the current row.
1051 (when (eq (org-element-property :type table-row) 'standard)
1052 (let* ((attr (mapconcat 'identity
1053 (org-element-property
1054 :attr_man (org-export-get-parent table-row))
1055 " "))
1056 ;; TABLE-ROW's borders are extracted from its first cell.
1057 (borders
1058 (org-export-table-cell-borders
1059 (car (org-element-contents table-row)) info)))
1060 (concat
1061 ;; Mark horizontal lines
1062 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1063 contents
1065 (cond
1066 ;; When BOOKTABS are activated enforce bottom rule even when
1067 ;; no hline was specifically marked.
1068 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1069 ((memq 'below borders) "\n_"))))))
1072 ;;; Target
1074 (defun org-man-target (target contents info)
1075 "Transcode a TARGET object from Org to Man.
1076 CONTENTS is nil. INFO is a plist holding contextual
1077 information."
1078 (format "\\fI%s\\fP"
1079 (org-export-solidify-link-text (org-element-property :value target))))
1082 ;;; Timestamp
1084 (defun org-man-timestamp (timestamp contents info)
1085 "Transcode a TIMESTAMP object from Org to Man.
1086 CONTENTS is nil. INFO is a plist holding contextual
1087 information."
1088 "" )
1091 ;;; Underline
1093 (defun org-man-underline (underline contents info)
1094 "Transcode UNDERLINE from Org to Man.
1095 CONTENTS is the text with underline markup. INFO is a plist
1096 holding contextual information."
1097 (format "\\fI%s\\fP" contents))
1100 ;;; Verbatim
1102 (defun org-man-verbatim (verbatim contents info)
1103 "Transcode a VERBATIM object from Org to Man.
1104 CONTENTS is nil. INFO is a plist used as a communication
1105 channel."
1106 (format ".nf\n%s\n.fi" contents))
1109 ;;; Verse Block
1111 (defun org-man-verse-block (verse-block contents info)
1112 "Transcode a VERSE-BLOCK element from Org to Man.
1113 CONTENTS is verse block contents. INFO is a plist holding
1114 contextual information."
1115 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1119 ;;; Interactive functions
1121 (defun org-man-export-to-man
1122 (&optional async subtreep visible-only body-only ext-plist)
1123 "Export current buffer to a Man file.
1125 If narrowing is active in the current buffer, only export its
1126 narrowed part.
1128 If a region is active, export that region.
1130 A non-nil optional argument ASYNC means the process should happen
1131 asynchronously. The resulting file should be accessible through
1132 the `org-export-stack' interface.
1134 When optional argument SUBTREEP is non-nil, export the sub-tree
1135 at point, extracting information from the headline properties
1136 first.
1138 When optional argument VISIBLE-ONLY is non-nil, don't export
1139 contents of hidden elements.
1141 When optional argument BODY-ONLY is non-nil, only the body
1142 without any markers.
1144 EXT-PLIST, when provided, is a property list with external
1145 parameters overriding Org default settings, but still inferior to
1146 file-local settings.
1148 Return output file's name."
1149 (interactive)
1150 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1151 (org-export-to-file 'man outfile
1152 async subtreep visible-only body-only ext-plist)))
1154 (defun org-man-export-to-pdf
1155 (&optional async subtreep visible-only body-only ext-plist)
1156 "Export current buffer to Groff then process through to PDF.
1158 If narrowing is active in the current buffer, only export its
1159 narrowed part.
1161 If a region is active, export that region.
1163 A non-nil optional argument ASYNC means the process should happen
1164 asynchronously. The resulting file should be accessible through
1165 the `org-export-stack' interface.
1167 When optional argument SUBTREEP is non-nil, export the sub-tree
1168 at point, extracting information from the headline properties
1169 first.
1171 When optional argument VISIBLE-ONLY is non-nil, don't export
1172 contents of hidden elements.
1174 When optional argument BODY-ONLY is non-nil, only write between
1175 markers.
1177 EXT-PLIST, when provided, is a property list with external
1178 parameters overriding Org default settings, but still inferior to
1179 file-local settings.
1181 Return PDF file's name."
1182 (interactive)
1183 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1184 (org-export-to-file 'man outfile
1185 async subtreep visible-only body-only ext-plist
1186 (lambda (file) (org-latex-compile file)))))
1188 (defun org-man-compile (file)
1189 "Compile a Groff file.
1191 FILE is the name of the file being compiled. Processing is done
1192 through the command specified in `org-man-pdf-process'.
1194 Return PDF file name or an error if it couldn't be produced."
1195 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1196 (full-name (file-truename file))
1197 (out-dir (file-name-directory file))
1198 ;; Properly set working directory for compilation.
1199 (default-directory (if (file-name-absolute-p file)
1200 (file-name-directory full-name)
1201 default-directory))
1202 errors)
1203 (message (format "Processing Groff file %s..." file))
1204 (save-window-excursion
1205 (cond
1206 ;; A function is provided: Apply it.
1207 ((functionp org-man-pdf-process)
1208 (funcall org-man-pdf-process (shell-quote-argument file)))
1209 ;; A list is provided: Replace %b, %f and %o with appropriate
1210 ;; values in each command before applying it. Output is
1211 ;; redirected to "*Org PDF Groff Output*" buffer.
1212 ((consp org-man-pdf-process)
1213 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1214 (mapc
1215 (lambda (command)
1216 (shell-command
1217 (replace-regexp-in-string
1218 "%b" (shell-quote-argument base-name)
1219 (replace-regexp-in-string
1220 "%f" (shell-quote-argument full-name)
1221 (replace-regexp-in-string
1222 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1223 outbuf))
1224 org-man-pdf-process)
1225 ;; Collect standard errors from output buffer.
1226 (setq errors (org-man-collect-errors outbuf))))
1227 (t (error "No valid command to process to PDF")))
1228 (let ((pdffile (concat out-dir base-name ".pdf")))
1229 ;; Check for process failure. Provide collected errors if
1230 ;; possible.
1231 (if (not (file-exists-p pdffile))
1232 (error (concat (format "PDF file %s wasn't produced" pdffile)
1233 (when errors (concat ": " errors))))
1234 ;; Else remove log files, when specified, and signal end of
1235 ;; process to user, along with any error encountered.
1236 (when org-man-remove-logfiles
1237 (dolist (ext org-man-logfiles-extensions)
1238 (let ((file (concat out-dir base-name "." ext)))
1239 (when (file-exists-p file) (delete-file file)))))
1240 (message (concat "Process completed"
1241 (if (not errors) "."
1242 (concat " with errors: " errors)))))
1243 ;; Return output file name.
1244 pdffile))))
1246 (defun org-man-collect-errors (buffer)
1247 "Collect some kind of errors from \"groff\" output
1248 BUFFER is the buffer containing output.
1249 Return collected error types as a string, or nil if there was
1250 none."
1251 (with-current-buffer buffer
1252 (save-excursion
1253 (goto-char (point-max))
1254 ;; Find final run
1255 nil )))
1258 (provide 'ox-man)
1260 ;; Local variables:
1261 ;; generated-autoload-file: "org-loaddefs.el"
1262 ;; End:
1264 ;;; ox-man.el ends here