ox-latex, ox-koma-letter: Use new `parse' syntax
[org-mode/org-tableheadings.git] / lisp / ox-man.el
blobc283f7de81071a397ea977d1d9c034ba54404586
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 (drawer . org-man-drawer)
59 (dynamic-block . org-man-dynamic-block)
60 (entity . org-man-entity)
61 (example-block . org-man-example-block)
62 (export-block . org-man-export-block)
63 (export-snippet . org-man-export-snippet)
64 (fixed-width . org-man-fixed-width)
65 (footnote-definition . org-man-footnote-definition)
66 (footnote-reference . org-man-footnote-reference)
67 (headline . org-man-headline)
68 (horizontal-rule . org-man-horizontal-rule)
69 (inline-babel-call . org-man-inline-babel-call)
70 (inline-src-block . org-man-inline-src-block)
71 (inlinetask . org-man-inlinetask)
72 (italic . org-man-italic)
73 (item . org-man-item)
74 (keyword . org-man-keyword)
75 (line-break . org-man-line-break)
76 (link . org-man-link)
77 (node-property . org-man-node-property)
78 (paragraph . org-man-paragraph)
79 (plain-list . org-man-plain-list)
80 (plain-text . org-man-plain-text)
81 (planning . org-man-planning)
82 (property-drawer . org-man-property-drawer)
83 (quote-block . org-man-quote-block)
84 (radio-target . org-man-radio-target)
85 (section . org-man-section)
86 (special-block . org-man-special-block)
87 (src-block . org-man-src-block)
88 (statistics-cookie . org-man-statistics-cookie)
89 (strike-through . org-man-strike-through)
90 (subscript . org-man-subscript)
91 (superscript . org-man-superscript)
92 (table . org-man-table)
93 (table-cell . org-man-table-cell)
94 (table-row . org-man-table-row)
95 (target . org-man-target)
96 (template . org-man-template)
97 (timestamp . org-man-timestamp)
98 (underline . org-man-underline)
99 (verbatim . org-man-verbatim)
100 (verse-block . org-man-verse-block))
101 :export-block "MAN"
102 :menu-entry
103 '(?M "Export to MAN"
104 ((?m "As MAN file" org-man-export-to-man)
105 (?p "As PDF file" org-man-export-to-pdf)
106 (?o "As PDF file and open"
107 (lambda (a s v b)
108 (if a (org-man-export-to-pdf t s v b)
109 (org-open-file (org-man-export-to-pdf nil s v b)))))))
110 :options-alist
111 '((:man-class "MAN_CLASS" nil nil t)
112 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
113 (:man-header-extra "MAN_HEADER" nil nil newline)
114 ;; Other variables.
115 (:man-tables-centered nil nil org-man-tables-centered)
116 (:man-tables-verbatim nil nil org-man-tables-verbatim)
117 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
118 (:man-source-highlight nil nil org-man-source-highlight)
119 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
123 ;;; User Configurable Variables
125 (defgroup org-export-man nil
126 "Options for exporting Org mode files to Man."
127 :tag "Org Export Man"
128 :group 'org-export)
130 ;;; Tables
132 (defcustom org-man-tables-centered t
133 "When non-nil, tables are exported in a center environment."
134 :group 'org-export-man
135 :version "24.4"
136 :package-version '(Org . "8.0")
137 :type 'boolean)
139 (defcustom org-man-tables-verbatim nil
140 "When non-nil, tables are exported verbatim."
141 :group 'org-export-man
142 :version "24.4"
143 :package-version '(Org . "8.0")
144 :type 'boolean)
147 (defcustom org-man-table-scientific-notation "%sE%s"
148 "Format string to display numbers in scientific notation.
149 The format should have \"%s\" twice, for mantissa and exponent
150 \(i.e. \"%s\\\\times10^{%s}\").
152 When nil, no transformation is made."
153 :group 'org-export-man
154 :version "24.4"
155 :package-version '(Org . "8.0")
156 :type '(choice
157 (string :tag "Format string")
158 (const :tag "No formatting")))
161 ;;; Inlinetasks
162 ;; Src blocks
164 (defcustom org-man-source-highlight nil
165 "Use GNU source highlight to embellish source blocks "
166 :group 'org-export-man
167 :version "24.4"
168 :package-version '(Org . "8.0")
169 :type 'boolean)
172 (defcustom org-man-source-highlight-langs
173 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
174 (scheme "scheme")
175 (c "c") (cc "cpp") (csharp "csharp") (d "d")
176 (fortran "fortran") (cobol "cobol") (pascal "pascal")
177 (ada "ada") (asm "asm")
178 (perl "perl") (cperl "perl")
179 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
180 (java "java") (javascript "javascript")
181 (tex "latex")
182 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
183 (ocaml "caml") (caml "caml")
184 (sql "sql") (sqlite "sql")
185 (html "html") (css "css") (xml "xml")
186 (bat "bat") (bison "bison") (clipper "clipper")
187 (ldap "ldap") (opa "opa")
188 (php "php") (postscript "postscript") (prolog "prolog")
189 (properties "properties") (makefile "makefile")
190 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
191 "Alist mapping languages to their listing language counterpart.
192 The key is a symbol, the major mode symbol without the \"-mode\".
193 The value is the string that should be inserted as the language
194 parameter for the listings package. If the mode name and the
195 listings name are the same, the language does not need an entry
196 in this list - but it does not hurt if it is present."
197 :group 'org-export-man
198 :version "24.4"
199 :package-version '(Org . "8.0")
200 :type '(repeat
201 (list
202 (symbol :tag "Major mode ")
203 (string :tag "Listings language"))))
207 (defvar org-man-custom-lang-environments nil
208 "Alist mapping languages to language-specific Man environments.
210 It is used during export of src blocks by the listings and
211 man packages. For example,
213 \(setq org-man-custom-lang-environments
214 '\(\(python \"pythoncode\"\)\)\)
216 would have the effect that if org encounters begin_src python
217 during man export."
221 ;;; Compilation
223 (defcustom org-man-pdf-process
224 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
225 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
226 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
228 "Commands to process a Man file to a PDF file.
229 This is a list of strings, each of them will be given to the
230 shell as a command. %f in the command will be replaced by the
231 full file name, %b by the file base name (i.e. without directory
232 and extension parts) and %o by the base directory of the file.
235 By default, Org uses 3 runs of to do the processing.
237 Alternatively, this may be a Lisp function that does the
238 processing. This function should accept the file name as
239 its single argument."
240 :group 'org-export-pdf
241 :group 'org-export-man
242 :version "24.4"
243 :package-version '(Org . "8.0")
244 :type '(choice
245 (repeat :tag "Shell command sequence"
246 (string :tag "Shell command"))
247 (const :tag "2 runs of pdfgroff"
248 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
249 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
250 (const :tag "3 runs of pdfgroff"
251 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
252 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
253 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
254 (function)))
256 (defcustom org-man-logfiles-extensions
257 '("log" "out" "toc")
258 "The list of file extensions to consider as Man logfiles."
259 :group 'org-export-man
260 :version "24.4"
261 :package-version '(Org . "8.0")
262 :type '(repeat (string :tag "Extension")))
264 (defcustom org-man-remove-logfiles t
265 "Non-nil means remove the logfiles produced by PDF production.
266 These are the .aux, .log, .out, and .toc files."
267 :group 'org-export-man
268 :version "24.4"
269 :package-version '(Org . "8.0")
270 :type 'boolean)
274 ;;; Internal Functions
276 (defun org-man--caption/label-string (element info)
277 "Return caption and label Man string for ELEMENT.
279 INFO is a plist holding contextual information. If there's no
280 caption nor label, return the empty string.
282 For non-floats, see `org-man--wrap-label'."
283 (let ((label (org-element-property :label element))
284 (main (org-export-get-caption element))
285 (short (org-export-get-caption element t)))
286 (cond ((and (not main) (not label)) "")
287 ((not main) (format "\\fI%s\\fP" label))
288 ;; Option caption format with short name.
289 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
290 (org-export-data short info)
291 (org-export-data main info)))
292 ;; Standard caption format.
293 (t (format "\\fR%s\\fP" (org-export-data main info))))))
295 (defun org-man--wrap-label (element output)
296 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
297 This function shouldn't be used for floats. See
298 `org-man--caption/label-string'."
299 (let ((label (org-element-property :name element)))
300 (if (or (not output) (not label) (string= output "") (string= label ""))
301 output
302 (concat (format "%s\n.br\n" label) output))))
306 ;;; Template
308 (defun org-man-template (contents info)
309 "Return complete document string after Man conversion.
310 CONTENTS is the transcoded contents string. INFO is a plist
311 holding export options."
312 (let* ((title (when (plist-get info :with-title)
313 (org-export-data (plist-get info :title) info)))
314 (attr (read (format "(%s)"
315 (mapconcat
316 #'identity
317 (list (plist-get info :man-class-options))
318 " "))))
319 (section-item (plist-get attr :section-id)))
321 (concat
323 (cond
324 ((and title (stringp section-item))
325 (format ".TH \"%s\" \"%s\" \n" title section-item))
326 ((and (string= "" title) (stringp section-item))
327 (format ".TH \"%s\" \"%s\" \n" " " section-item))
328 (title
329 (format ".TH \"%s\" \"1\" \n" title))
331 ".TH \" \" \"1\" "))
332 contents)))
337 ;;; Transcode Functions
339 ;;; Babel Call
341 ;; Babel Calls are ignored.
344 ;;; Bold
346 (defun org-man-bold (bold contents info)
347 "Transcode BOLD from Org to Man.
348 CONTENTS is the text with bold markup. INFO is a plist holding
349 contextual information."
350 (format "\\fB%s\\fP" contents))
353 ;;; Center Block
355 (defun org-man-center-block (center-block contents info)
356 "Transcode a CENTER-BLOCK element from Org to Man.
357 CONTENTS holds the contents of the center block. INFO is a plist
358 holding contextual information."
359 (org-man--wrap-label
360 center-block
361 (format ".ce %d\n.nf\n%s\n.fi"
362 (- (length (split-string contents "\n")) 1 )
363 contents)))
366 ;;; Clock
368 (defun org-man-clock (clock contents info)
369 "Transcode a CLOCK element from Org to Man.
370 CONTENTS is nil. INFO is a plist holding contextual
371 information."
372 "" )
375 ;;; Code
377 (defun org-man-code (code contents info)
378 "Transcode a CODE object from Org to Man.
379 CONTENTS is nil. INFO is a plist used as a communication
380 channel."
381 (format "\\fC%s\\fP" code))
384 ;;; Comment
386 ;; Comments are ignored.
389 ;;; Comment Block
391 ;; Comment Blocks are ignored.
394 ;;; Drawer
396 (defun org-man-drawer (drawer contents info)
397 "Transcode a DRAWER element from Org to Man.
398 DRAWER holds the drawer information
399 CONTENTS holds the contents of the block.
400 INFO is a plist holding contextual information. "
401 contents)
404 ;;; Dynamic Block
406 (defun org-man-dynamic-block (dynamic-block contents info)
407 "Transcode a DYNAMIC-BLOCK element from Org to Man.
408 CONTENTS holds the contents of the block. INFO is a plist
409 holding contextual information. See `org-export-data'."
410 (org-man--wrap-label dynamic-block contents))
413 ;;; Entity
415 (defun org-man-entity (entity contents info)
416 "Transcode an ENTITY object from Org to Man.
417 CONTENTS are the definition itself. INFO is a plist holding
418 contextual information."
419 (org-element-property :utf-8 entity))
422 ;;; Example Block
424 (defun org-man-example-block (example-block contents info)
425 "Transcode an EXAMPLE-BLOCK element from Org to Man.
426 CONTENTS is nil. INFO is a plist holding contextual
427 information."
428 (org-man--wrap-label
429 example-block
430 (format ".RS\n.nf\n%s\n.fi\n.RE"
431 (org-export-format-code-default example-block info))))
434 ;;; Export Block
436 (defun org-man-export-block (export-block contents info)
437 "Transcode a EXPORT-BLOCK element from Org to Man.
438 CONTENTS is nil. INFO is a plist holding contextual information."
439 (when (string= (org-element-property :type export-block) "MAN")
440 (org-remove-indentation (org-element-property :value export-block))))
443 ;;; Export Snippet
445 (defun org-man-export-snippet (export-snippet contents info)
446 "Transcode a EXPORT-SNIPPET object from Org to Man.
447 CONTENTS is nil. INFO is a plist holding contextual information."
448 (when (eq (org-export-snippet-backend export-snippet) 'man)
449 (org-element-property :value export-snippet)))
452 ;;; Fixed Width
454 (defun org-man-fixed-width (fixed-width contents info)
455 "Transcode a FIXED-WIDTH element from Org to Man.
456 CONTENTS is nil. INFO is a plist holding contextual information."
457 (org-man--wrap-label
458 fixed-width
459 (format "\\fC\n%s\\fP"
460 (org-remove-indentation
461 (org-element-property :value fixed-width)))))
464 ;;; Footnote Definition
466 ;; Footnote Definitions are ignored.
468 ;;; Footnote References
470 ;; Footnote References are Ignored
473 ;;; Headline
475 (defun org-man-headline (headline contents info)
476 "Transcode a HEADLINE element from Org to Man.
477 CONTENTS holds the contents of the headline. INFO is a plist
478 holding contextual information."
479 (let* ((level (org-export-get-relative-level headline info))
480 (numberedp (org-export-numbered-headline-p headline info))
481 ;; Section formatting will set two placeholders: one for the
482 ;; title and the other for the contents.
483 (section-fmt
484 (case level
485 (1 ".SH \"%s\"\n%s")
486 (2 ".SS \"%s\"\n%s")
487 (3 ".SS \"%s\"\n%s")
488 (t nil)))
489 (text (org-export-data (org-element-property :title headline) info)))
491 (cond
492 ;; Case 1: This is a footnote section: ignore it.
493 ((org-element-property :footnote-section-p headline) nil)
495 ;; Case 2. This is a deep sub-tree: export it as a list item.
496 ;; Also export as items headlines for which no section
497 ;; format has been found.
498 ((or (not section-fmt) (org-export-low-level-p headline info))
499 ;; Build the real contents of the sub-tree.
500 (let ((low-level-body
501 (concat
502 ;; If the headline is the first sibling, start a list.
503 (when (org-export-first-sibling-p headline info)
504 (format "%s\n" ".RS"))
505 ;; Itemize headline
506 ".TP\n.ft I\n" text "\n.ft\n"
507 contents ".RE")))
508 ;; If headline is not the last sibling simply return
509 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
510 ;; blank line.
511 (if (not (org-export-last-sibling-p headline info)) low-level-body
512 (replace-regexp-in-string
513 "[ \t\n]*\\'" ""
514 low-level-body))))
516 ;; Case 3. Standard headline. Export it as a section.
517 (t (format section-fmt text contents )))))
519 ;;; Horizontal Rule
520 ;; Not supported
522 ;;; Inline Babel Call
524 ;; Inline Babel Calls are ignored.
526 ;;; Inline Src Block
528 (defun org-man-inline-src-block (inline-src-block contents info)
529 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
530 CONTENTS holds the contents of the item. INFO is a plist holding
531 contextual information."
532 (let* ((code (org-element-property :value inline-src-block)))
533 (cond
534 ((plist-get info :man-source-highlight)
535 (let* ((tmpdir (if (featurep 'xemacs)
536 temp-directory
537 temporary-file-directory ))
538 (in-file (make-temp-name
539 (expand-file-name "srchilite" tmpdir)))
540 (out-file (make-temp-name
541 (expand-file-name "reshilite" tmpdir)))
542 (org-lang (org-element-property :language inline-src-block))
543 (lst-lang
544 (cadr (assq (intern org-lang)
545 (plist-get info :man-source-highlight-langs))))
547 (cmd (concat (expand-file-name "source-highlight")
548 " -s " lst-lang
549 " -f groff_man"
550 " -i " in-file
551 " -o " out-file )))
553 (if lst-lang
554 (let ((code-block "" ))
555 (with-temp-file in-file (insert code))
556 (shell-command cmd)
557 (setq code-block (org-file-contents out-file))
558 (delete-file in-file)
559 (delete-file out-file)
560 code-block)
561 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
562 code))))
564 ;; Do not use a special package: transcode it verbatim.
566 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
567 "\\fP\n.fi\n.RE\n")))))
570 ;;; Inlinetask
571 ;;; Italic
573 (defun org-man-italic (italic contents info)
574 "Transcode ITALIC from Org to Man.
575 CONTENTS is the text with italic markup. INFO is a plist holding
576 contextual information."
577 (format "\\fI%s\\fP" contents))
580 ;;; Item
583 (defun org-man-item (item contents info)
585 "Transcode an ITEM element from Org to Man.
586 CONTENTS holds the contents of the item. INFO is a plist holding
587 contextual information."
589 (let* ((bullet (org-element-property :bullet item))
590 (type (org-element-property :type (org-element-property :parent item)))
591 (checkbox (case (org-element-property :checkbox item)
592 (on "\\o'\\(sq\\(mu'") ;;
593 (off "\\(sq ") ;;
594 (trans "\\o'\\(sq\\(mi'" ))) ;;
596 (tag (let ((tag (org-element-property :tag item)))
597 ;; Check-boxes must belong to the tag.
598 (and tag (format "\\fB%s\\fP"
599 (concat checkbox
600 (org-export-data tag info)))))))
602 (if (and (null tag )
603 (null checkbox))
604 (let* ((bullet (org-trim bullet))
605 (marker (cond ((string= "-" bullet) "\\(em")
606 ((string= "*" bullet) "\\(bu")
607 ((eq type 'ordered)
608 (format "%s " (org-trim bullet)))
609 (t "\\(dg"))))
610 (concat ".IP " marker " 4\n"
611 (org-trim (or contents " " ))))
612 ; else
613 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
614 (org-trim (or contents " " ))))))
616 ;;; Keyword
619 (defun org-man-keyword (keyword contents info)
620 "Transcode a KEYWORD element from Org to Man.
621 CONTENTS is nil. INFO is a plist holding contextual information."
622 (let ((key (org-element-property :key keyword))
623 (value (org-element-property :value keyword)))
624 (cond
625 ((string= key "MAN") value)
626 ((string= key "INDEX") nil)
627 ((string= key "TOC" ) nil))))
630 ;;; Line Break
632 (defun org-man-line-break (line-break contents info)
633 "Transcode a LINE-BREAK object from Org to Man.
634 CONTENTS is nil. INFO is a plist holding contextual information."
635 ".br\n")
638 ;;; Link
641 (defun org-man-link (link desc info)
642 "Transcode a LINK object from Org to Man.
644 DESC is the description part of the link, or the empty string.
645 INFO is a plist holding contextual information. See
646 `org-export-data'."
647 (let* ((type (org-element-property :type link))
648 (raw-path (org-element-property :path link))
649 ;; Ensure DESC really exists, or set it to nil.
650 (desc (and (not (string= desc "")) desc))
651 (path (cond
652 ((member type '("http" "https" "ftp" "mailto"))
653 (concat type ":" raw-path))
654 ((and (string= type "file") (file-name-absolute-p raw-path))
655 (concat "file:" raw-path))
656 (t raw-path)))
657 protocol)
658 (cond
659 ;; Link type is handled by a special function.
660 ((org-export-custom-protocol-maybe link desc 'man))
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)))))
668 ;;;; Node Property
670 (defun org-man-node-property (node-property contents info)
671 "Transcode a NODE-PROPERTY element from Org to Man.
672 CONTENTS is nil. INFO is a plist holding contextual
673 information."
674 (format "%s:%s"
675 (org-element-property :key node-property)
676 (let ((value (org-element-property :value node-property)))
677 (if value (concat " " value) ""))))
679 ;;; Paragraph
681 (defun org-man-paragraph (paragraph contents info)
682 "Transcode a PARAGRAPH element from Org to Man.
683 CONTENTS is the contents of the paragraph, as a string. INFO is
684 the plist used as a communication channel."
685 (let ((parent (plist-get (nth 1 paragraph) :parent)))
686 (when parent
687 (let ((parent-type (car parent))
688 (fixed-paragraph ""))
689 (cond ((and (eq parent-type 'item)
690 (plist-get (nth 1 parent) :bullet ))
691 (setq fixed-paragraph (concat "" contents)))
692 ((eq parent-type 'section)
693 (setq fixed-paragraph (concat ".PP\n" contents)))
694 ((eq parent-type 'footnote-definition)
695 (setq fixed-paragraph contents))
696 (t (setq fixed-paragraph (concat "" contents))))
697 fixed-paragraph ))))
700 ;;; Plain List
702 (defun org-man-plain-list (plain-list contents info)
703 "Transcode a PLAIN-LIST element from Org to Man.
704 CONTENTS is the contents of the list. INFO is a plist holding
705 contextual information."
706 contents)
708 ;;; Plain Text
710 (defun org-man-plain-text (text info)
711 "Transcode a TEXT string from Org to Man.
712 TEXT is the string to transcode. INFO is a plist holding
713 contextual information."
714 (let ((output text))
715 ;; Protect various chars.
716 (setq output (replace-regexp-in-string
717 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
718 "$\\" output nil t 1))
719 ;; Activate smart quotes. Be sure to provide original TEXT string
720 ;; since OUTPUT may have been modified.
721 (when (plist-get info :with-smart-quotes)
722 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
723 ;; Handle break preservation if required.
724 (when (plist-get info :preserve-breaks)
725 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
726 output)))
727 ;; Return value.
728 output))
732 ;;; Planning
735 ;;; Property Drawer
737 (defun org-man-property-drawer (property-drawer contents info)
738 "Transcode a PROPERTY-DRAWER element from Org to Man.
739 CONTENTS holds the contents of the drawer. INFO is a plist
740 holding contextual information."
741 (and (org-string-nw-p contents)
742 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
744 ;;; Quote Block
746 (defun org-man-quote-block (quote-block contents info)
747 "Transcode a QUOTE-BLOCK element from Org to Man.
748 CONTENTS holds the contents of the block. INFO is a plist
749 holding contextual information."
750 (org-man--wrap-label
751 quote-block
752 (format ".RS\n%s\n.RE" contents)))
755 ;;; Radio Target
757 (defun org-man-radio-target (radio-target text info)
758 "Transcode a RADIO-TARGET object from Org to Man.
759 TEXT is the text of the target. INFO is a plist holding
760 contextual information."
761 text )
764 ;;; Section
766 (defun org-man-section (section contents info)
767 "Transcode a SECTION element from Org to Man.
768 CONTENTS holds the contents of the section. INFO is a plist
769 holding contextual information."
770 contents)
773 ;;; Special Block
775 (defun org-man-special-block (special-block contents info)
776 "Transcode a SPECIAL-BLOCK element from Org to Man.
777 CONTENTS holds the contents of the block. INFO is a plist
778 holding contextual information."
779 (let ((type (org-element-property :type special-block)))
780 (org-man--wrap-label
781 special-block
782 (format "%s\n" contents))))
785 ;;; Src Block
787 (defun org-man-src-block (src-block contents info)
788 "Transcode a SRC-BLOCK element from Org to Man.
789 CONTENTS holds the contents of the item. INFO is a plist holding
790 contextual information."
791 (let* ((lang (org-element-property :language src-block))
792 (code (org-element-property :value src-block))
793 (custom-env (and lang
794 (cadr (assq (intern lang)
795 org-man-custom-lang-environments))))
796 (num-start (case (org-element-property :number-lines src-block)
797 (continued (org-export-get-loc src-block info))
798 (new 0)))
799 (retain-labels (org-element-property :retain-labels src-block)))
800 (if (not (plist-get info :man-source-highlight))
801 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
802 (org-export-format-code-default src-block info))
803 (let* ((tmpdir (if (featurep 'xemacs) temp-directory
804 temporary-file-directory))
805 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
806 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
807 (org-lang (org-element-property :language src-block))
808 (lst-lang
809 (cadr (assq (intern org-lang)
810 (plist-get info :man-source-highlight-langs))))
811 (cmd (concat "source-highlight"
812 " -s " lst-lang
813 " -f groff_man "
814 " -i " in-file
815 " -o " out-file)))
816 (if lst-lang
817 (let ((code-block ""))
818 (with-temp-file in-file (insert code))
819 (shell-command cmd)
820 (setq code-block (org-file-contents out-file))
821 (delete-file in-file)
822 (delete-file out-file)
823 code-block)
824 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code))))))
827 ;;; Statistics Cookie
829 (defun org-man-statistics-cookie (statistics-cookie contents info)
830 "Transcode a STATISTICS-COOKIE object from Org to Man.
831 CONTENTS is nil. INFO is a plist holding contextual information."
832 (org-element-property :value statistics-cookie))
835 ;;; Strike-Through
837 (defun org-man-strike-through (strike-through contents info)
838 "Transcode STRIKE-THROUGH from Org to Man.
839 CONTENTS is the text with strike-through markup. INFO is a plist
840 holding contextual information."
841 (format "\\fI%s\\fP" contents))
843 ;;; Subscript
845 (defun org-man-subscript (subscript contents info)
846 "Transcode a SUBSCRIPT object from Org to Man.
847 CONTENTS is the contents of the object. INFO is a plist holding
848 contextual information."
849 (format "\\d\\s-2%s\\s+2\\u" contents))
851 ;;; Superscript "^_%s$
853 (defun org-man-superscript (superscript contents info)
854 "Transcode a SUPERSCRIPT object from Org to Man.
855 CONTENTS is the contents of the object. INFO is a plist holding
856 contextual information."
857 (format "\\u\\s-2%s\\s+2\\d" contents))
860 ;;; Table
862 ;; `org-man-table' is the entry point for table transcoding. It
863 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
864 ;; delegates the job to either `org-man-table--table.el-table' or
865 ;; `org-man-table--org-table' functions, depending of the type of
866 ;; the table.
868 ;; `org-man-table--align-string' is a subroutine used to build
869 ;; alignment string for Org tables.
871 (defun org-man-table (table contents info)
872 "Transcode a TABLE element from Org to Man.
873 CONTENTS is the contents of the table. INFO is a plist holding
874 contextual information."
875 (cond
876 ;; Case 1: verbatim table.
877 ((or (plist-get info :man-tables-verbatim)
878 (let ((attr (read (format "(%s)"
879 (mapconcat
880 #'identity
881 (org-element-property :attr_man table)
882 " ")))))
884 (and attr (plist-get attr :verbatim))))
886 (format ".nf\n\\fC%s\\fP\n.fi"
887 ;; Re-create table, without affiliated keywords.
888 (org-trim
889 (org-element-interpret-data
890 `(table nil ,@(org-element-contents table))))))
891 ;; Case 2: Standard table.
892 (t (org-man-table--org-table table contents info))))
894 (defun org-man-table--align-string (divider table info)
895 "Return an appropriate Man alignment string.
896 TABLE is the considered table. INFO is a plist used as
897 a communication channel."
898 (let (alignment)
899 ;; Extract column groups and alignment from first (non-rule) row.
900 (org-element-map
901 (org-element-map table 'table-row
902 (lambda (row)
903 (and (eq (org-element-property :type row) 'standard) row))
904 info 'first-match)
905 'table-cell
906 (lambda (cell)
907 (let* ((borders (org-export-table-cell-borders cell info))
908 (raw-width (org-export-table-cell-width cell info))
909 (width-cm (when raw-width (/ raw-width 5)))
910 (width (if raw-width (format "w(%dc)"
911 (if (< width-cm 1) 1 width-cm)) "")))
912 ;; Check left border for the first cell only.
913 (when (and (memq 'left borders) (not alignment))
914 (push "|" alignment))
915 (push
916 (case (org-export-table-cell-alignment cell info)
917 (left (concat "l" width divider))
918 (right (concat "r" width divider))
919 (center (concat "c" width divider)))
920 alignment)
921 (when (memq 'right borders) (push "|" alignment))))
922 info)
923 (apply 'concat (reverse alignment))))
925 (defun org-man-table--org-table (table contents info)
926 "Return appropriate Man code for an Org table.
928 TABLE is the table type element to transcode. CONTENTS is its
929 contents, as a string. INFO is a plist used as a communication
930 channel.
932 This function assumes TABLE has `org' as its `:type' attribute."
933 (let* ((attr (org-export-read-attribute :attr_man table))
934 (label (org-element-property :name table))
935 (caption (and (not (plist-get attr :disable-caption))
936 (org-man--caption/label-string table info)))
937 (divider (if (plist-get attr :divider) "|" " "))
939 ;; Determine alignment string.
940 (alignment (org-man-table--align-string divider table info))
941 ;; Extract others display options.
943 (lines (org-split-string contents "\n"))
945 (attr-list
946 (delq nil
947 (list
948 (and (plist-get attr :expand) "expand")
949 (let ((placement (plist-get attr :placement)))
950 (cond ((string= placement 'center) "center")
951 ((string= placement 'left) nil)
952 ((plist-get info :man-tables-centered) "center")
953 (t "")))
954 (or (plist-get attr :boxtype) "box"))))
956 (title-line (plist-get attr :title-line))
957 (long-cells (plist-get attr :long-cells))
959 (table-format (concat
960 (format "%s" (or (car attr-list) "" ))
962 (let ((output-list '()))
963 (when (cdr attr-list)
964 (dolist (attr-item (cdr attr-list))
965 (setq output-list (concat output-list (format ",%s" attr-item)))))
966 output-list)
967 "")))
969 (first-line (when lines (org-split-string (car lines) "\t"))))
970 ;; Prepare the final format string for the table.
973 (cond
974 ;; Others.
975 (lines (concat ".TS\n " table-format ";\n"
977 (format "%s.\n"
978 (let ((final-line ""))
979 (when title-line
980 (dotimes (i (length first-line))
981 (setq final-line (concat final-line "cb" divider))))
983 (setq final-line (concat final-line "\n"))
985 (if alignment
986 (setq final-line (concat final-line alignment))
987 (dotimes (i (length first-line))
988 (setq final-line (concat final-line "c" divider))))
989 final-line ))
991 (format "%s.TE\n"
992 (let ((final-line "")
993 (long-line "")
994 (lines (org-split-string contents "\n")))
996 (dolist (line-item lines)
997 (setq long-line "")
999 (if long-cells
1000 (progn
1001 (if (string= line-item "_")
1002 (setq long-line (format "%s\n" line-item))
1003 ;; else string =
1004 (let ((cell-item-list (org-split-string line-item "\t")))
1005 (dolist (cell-item cell-item-list)
1007 (cond ((eq cell-item (car (last cell-item-list)))
1008 (setq long-line (concat long-line
1009 (format "T{\n%s\nT}\t\n" cell-item ))))
1011 (setq long-line (concat long-line
1012 (format "T{\n%s\nT}\t" cell-item ))))))
1013 long-line))
1014 ;; else long cells
1015 (setq final-line (concat final-line long-line )))
1017 (setq final-line (concat final-line line-item "\n"))))
1018 final-line))
1020 (and caption (format ".TB \"%s\"" caption)))))))
1022 ;;; Table Cell
1024 (defun org-man-table-cell (table-cell contents info)
1025 "Transcode a TABLE-CELL element from Org to Man
1026 CONTENTS is the cell contents. INFO is a plist used as
1027 a communication channel."
1028 (concat
1029 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
1030 (if (and contents
1031 scientific-format
1032 (string-match orgtbl-exp-regexp contents))
1033 ;; Use appropriate format string for scientific notation.
1034 (format scientific-format
1035 (match-string 1 contents)
1036 (match-string 2 contents))
1037 contents))
1038 (when (org-export-get-next-element table-cell info) "\t")))
1041 ;;; Table Row
1043 (defun org-man-table-row (table-row contents info)
1044 "Transcode a TABLE-ROW element from Org to Man
1045 CONTENTS is the contents of the row. INFO is a plist used as
1046 a communication channel."
1047 ;; Rules are ignored since table separators are deduced from
1048 ;; borders of the current row.
1049 (when (eq (org-element-property :type table-row) 'standard)
1050 (let* ((attr (mapconcat 'identity
1051 (org-element-property
1052 :attr_man (org-export-get-parent table-row))
1053 " "))
1054 ;; TABLE-ROW's borders are extracted from its first cell.
1055 (borders
1056 (org-export-table-cell-borders
1057 (car (org-element-contents table-row)) info)))
1058 (concat
1059 ;; Mark horizontal lines
1060 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1061 contents
1063 (cond
1064 ;; When BOOKTABS are activated enforce bottom rule even when
1065 ;; no hline was specifically marked.
1066 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1067 ((memq 'below borders) "\n_"))))))
1070 ;;; Target
1072 (defun org-man-target (target contents info)
1073 "Transcode a TARGET object from Org to Man.
1074 CONTENTS is nil. INFO is a plist holding contextual
1075 information."
1076 (format "\\fI%s\\fP"
1077 (org-export-solidify-link-text (org-element-property :value target))))
1080 ;;; Timestamp
1082 (defun org-man-timestamp (timestamp contents info)
1083 "Transcode a TIMESTAMP object from Org to Man.
1084 CONTENTS is nil. INFO is a plist holding contextual
1085 information."
1086 "" )
1089 ;;; Underline
1091 (defun org-man-underline (underline contents info)
1092 "Transcode UNDERLINE from Org to Man.
1093 CONTENTS is the text with underline markup. INFO is a plist
1094 holding contextual information."
1095 (format "\\fI%s\\fP" contents))
1098 ;;; Verbatim
1100 (defun org-man-verbatim (verbatim contents info)
1101 "Transcode a VERBATIM object from Org to Man.
1102 CONTENTS is nil. INFO is a plist used as a communication
1103 channel."
1104 (format ".nf\n%s\n.fi" contents))
1107 ;;; Verse Block
1109 (defun org-man-verse-block (verse-block contents info)
1110 "Transcode a VERSE-BLOCK element from Org to Man.
1111 CONTENTS is verse block contents. INFO is a plist holding
1112 contextual information."
1113 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1117 ;;; Interactive functions
1119 (defun org-man-export-to-man
1120 (&optional async subtreep visible-only body-only ext-plist)
1121 "Export current buffer to a Man file.
1123 If narrowing is active in the current buffer, only export its
1124 narrowed part.
1126 If a region is active, export that region.
1128 A non-nil optional argument ASYNC means the process should happen
1129 asynchronously. The resulting file should be accessible through
1130 the `org-export-stack' interface.
1132 When optional argument SUBTREEP is non-nil, export the sub-tree
1133 at point, extracting information from the headline properties
1134 first.
1136 When optional argument VISIBLE-ONLY is non-nil, don't export
1137 contents of hidden elements.
1139 When optional argument BODY-ONLY is non-nil, only the body
1140 without any markers.
1142 EXT-PLIST, when provided, is a property list with external
1143 parameters overriding Org default settings, but still inferior to
1144 file-local settings.
1146 Return output file's name."
1147 (interactive)
1148 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1149 (org-export-to-file 'man outfile
1150 async subtreep visible-only body-only ext-plist)))
1152 (defun org-man-export-to-pdf
1153 (&optional async subtreep visible-only body-only ext-plist)
1154 "Export current buffer to Groff then process through to PDF.
1156 If narrowing is active in the current buffer, only export its
1157 narrowed part.
1159 If a region is active, export that region.
1161 A non-nil optional argument ASYNC means the process should happen
1162 asynchronously. The resulting file should be accessible through
1163 the `org-export-stack' interface.
1165 When optional argument SUBTREEP is non-nil, export the sub-tree
1166 at point, extracting information from the headline properties
1167 first.
1169 When optional argument VISIBLE-ONLY is non-nil, don't export
1170 contents of hidden elements.
1172 When optional argument BODY-ONLY is non-nil, only write between
1173 markers.
1175 EXT-PLIST, when provided, is a property list with external
1176 parameters overriding Org default settings, but still inferior to
1177 file-local settings.
1179 Return PDF file's name."
1180 (interactive)
1181 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1182 (org-export-to-file 'man outfile
1183 async subtreep visible-only body-only ext-plist
1184 (lambda (file) (org-latex-compile file)))))
1186 (defun org-man-compile (file)
1187 "Compile a Groff file.
1189 FILE is the name of the file being compiled. Processing is done
1190 through the command specified in `org-man-pdf-process'.
1192 Return PDF file name or an error if it couldn't be produced."
1193 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1194 (full-name (file-truename file))
1195 (out-dir (file-name-directory file))
1196 ;; Properly set working directory for compilation.
1197 (default-directory (if (file-name-absolute-p file)
1198 (file-name-directory full-name)
1199 default-directory))
1200 errors)
1201 (message (format "Processing Groff file %s..." file))
1202 (save-window-excursion
1203 (cond
1204 ;; A function is provided: Apply it.
1205 ((functionp org-man-pdf-process)
1206 (funcall org-man-pdf-process (shell-quote-argument file)))
1207 ;; A list is provided: Replace %b, %f and %o with appropriate
1208 ;; values in each command before applying it. Output is
1209 ;; redirected to "*Org PDF Groff Output*" buffer.
1210 ((consp org-man-pdf-process)
1211 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1212 (mapc
1213 (lambda (command)
1214 (shell-command
1215 (replace-regexp-in-string
1216 "%b" (shell-quote-argument base-name)
1217 (replace-regexp-in-string
1218 "%f" (shell-quote-argument full-name)
1219 (replace-regexp-in-string
1220 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1221 outbuf))
1222 org-man-pdf-process)
1223 ;; Collect standard errors from output buffer.
1224 (setq errors (org-man-collect-errors outbuf))))
1225 (t (error "No valid command to process to PDF")))
1226 (let ((pdffile (concat out-dir base-name ".pdf")))
1227 ;; Check for process failure. Provide collected errors if
1228 ;; possible.
1229 (if (not (file-exists-p pdffile))
1230 (error (concat (format "PDF file %s wasn't produced" pdffile)
1231 (when errors (concat ": " errors))))
1232 ;; Else remove log files, when specified, and signal end of
1233 ;; process to user, along with any error encountered.
1234 (when org-man-remove-logfiles
1235 (dolist (ext org-man-logfiles-extensions)
1236 (let ((file (concat out-dir base-name "." ext)))
1237 (when (file-exists-p file) (delete-file file)))))
1238 (message (concat "Process completed"
1239 (if (not errors) "."
1240 (concat " with errors: " errors)))))
1241 ;; Return output file name.
1242 pdffile))))
1244 (defun org-man-collect-errors (buffer)
1245 "Collect some kind of errors from \"groff\" output
1246 BUFFER is the buffer containing output.
1247 Return collected error types as a string, or nil if there was
1248 none."
1249 (with-current-buffer buffer
1250 (save-excursion
1251 (goto-char (point-max))
1252 ;; Find final run
1253 nil )))
1256 (provide 'ox-man)
1258 ;; Local variables:
1259 ;; generated-autoload-file: "org-loaddefs.el"
1260 ;; End:
1262 ;;; ox-man.el ends here