org-footnote: Handle un-referenced definitions
[org-mode.git] / lisp / ox-man.el
blob1a646f587397afddd970f249032d02088c0ed860
1 ;; ox-man.el --- Man Back-End for Org Export Engine -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2016 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 (code . org-man-code)
57 (drawer . org-man-drawer)
58 (dynamic-block . org-man-dynamic-block)
59 (entity . org-man-entity)
60 (example-block . org-man-example-block)
61 (export-block . org-man-export-block)
62 (export-snippet . org-man-export-snippet)
63 (fixed-width . org-man-fixed-width)
64 (footnote-definition . org-man-footnote-definition)
65 (footnote-reference . org-man-footnote-reference)
66 (headline . org-man-headline)
67 (horizontal-rule . org-man-horizontal-rule)
68 (inline-babel-call . org-man-inline-babel-call)
69 (inline-src-block . org-man-inline-src-block)
70 (inlinetask . org-man-inlinetask)
71 (italic . org-man-italic)
72 (item . org-man-item)
73 (keyword . org-man-keyword)
74 (line-break . org-man-line-break)
75 (link . org-man-link)
76 (node-property . org-man-node-property)
77 (paragraph . org-man-paragraph)
78 (plain-list . org-man-plain-list)
79 (plain-text . org-man-plain-text)
80 (planning . org-man-planning)
81 (property-drawer . org-man-property-drawer)
82 (quote-block . org-man-quote-block)
83 (radio-target . org-man-radio-target)
84 (section . org-man-section)
85 (special-block . org-man-special-block)
86 (src-block . org-man-src-block)
87 (statistics-cookie . org-man-statistics-cookie)
88 (strike-through . org-man-strike-through)
89 (subscript . org-man-subscript)
90 (superscript . org-man-superscript)
91 (table . org-man-table)
92 (table-cell . org-man-table-cell)
93 (table-row . org-man-table-row)
94 (target . org-man-target)
95 (template . org-man-template)
96 (timestamp . org-man-timestamp)
97 (underline . org-man-underline)
98 (verbatim . org-man-verbatim)
99 (verse-block . org-man-verse-block))
100 :menu-entry
101 '(?M "Export to MAN"
102 ((?m "As MAN file" org-man-export-to-man)
103 (?p "As PDF file" org-man-export-to-pdf)
104 (?o "As PDF file and open"
105 (lambda (a s v b)
106 (if a (org-man-export-to-pdf t s v b)
107 (org-open-file (org-man-export-to-pdf nil s v b)))))))
108 :options-alist
109 '((:man-class "MAN_CLASS" nil nil t)
110 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
111 (:man-header-extra "MAN_HEADER" nil nil newline)
112 ;; Other variables.
113 (:man-tables-centered nil nil org-man-tables-centered)
114 (:man-tables-verbatim nil nil org-man-tables-verbatim)
115 (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
116 (:man-source-highlight nil nil org-man-source-highlight)
117 (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
121 ;;; User Configurable Variables
123 (defgroup org-export-man nil
124 "Options for exporting Org mode files to Man."
125 :tag "Org Export Man"
126 :group 'org-export)
128 ;;; Tables
130 (defcustom org-man-tables-centered t
131 "When non-nil, tables are exported in a center environment."
132 :group 'org-export-man
133 :version "24.4"
134 :package-version '(Org . "8.0")
135 :type 'boolean)
137 (defcustom org-man-tables-verbatim nil
138 "When non-nil, tables are exported verbatim."
139 :group 'org-export-man
140 :version "24.4"
141 :package-version '(Org . "8.0")
142 :type 'boolean)
145 (defcustom org-man-table-scientific-notation "%sE%s"
146 "Format string to display numbers in scientific notation.
147 The format should have \"%s\" twice, for mantissa and exponent
148 \(i.e. \"%s\\\\times10^{%s}\").
150 When nil, no transformation is made."
151 :group 'org-export-man
152 :version "24.4"
153 :package-version '(Org . "8.0")
154 :type '(choice
155 (string :tag "Format string")
156 (const :tag "No formatting")))
159 ;;; Inlinetasks
160 ;; Src blocks
162 (defcustom org-man-source-highlight nil
163 "Use GNU source highlight to embellish source blocks "
164 :group 'org-export-man
165 :version "24.4"
166 :package-version '(Org . "8.0")
167 :type 'boolean)
170 (defcustom org-man-source-highlight-langs
171 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
172 (scheme "scheme")
173 (c "c") (cc "cpp") (csharp "csharp") (d "d")
174 (fortran "fortran") (cobol "cobol") (pascal "pascal")
175 (ada "ada") (asm "asm")
176 (perl "perl") (cperl "perl")
177 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
178 (java "java") (javascript "javascript")
179 (tex "latex")
180 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
181 (ocaml "caml") (caml "caml")
182 (sql "sql") (sqlite "sql")
183 (html "html") (css "css") (xml "xml")
184 (bat "bat") (bison "bison") (clipper "clipper")
185 (ldap "ldap") (opa "opa")
186 (php "php") (postscript "postscript") (prolog "prolog")
187 (properties "properties") (makefile "makefile")
188 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
189 "Alist mapping languages to their listing language counterpart.
190 The key is a symbol, the major mode symbol without the \"-mode\".
191 The value is the string that should be inserted as the language
192 parameter for the listings package. If the mode name and the
193 listings name are the same, the language does not need an entry
194 in this list - but it does not hurt if it is present."
195 :group 'org-export-man
196 :version "24.4"
197 :package-version '(Org . "8.0")
198 :type '(repeat
199 (list
200 (symbol :tag "Major mode ")
201 (string :tag "Listings language"))))
204 ;;; Compilation
206 (defcustom org-man-pdf-process
207 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
208 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
209 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
211 "Commands to process a Man file to a PDF file.
212 This is a list of strings, each of them will be given to the
213 shell as a command. %f in the command will be replaced by the
214 full file name, %b by the file base name (i.e. without directory
215 and extension parts) and %o by the base directory of the file.
218 By default, Org uses 3 runs of to do the processing.
220 Alternatively, this may be a Lisp function that does the
221 processing. This function should accept the file name as
222 its single argument."
223 :group 'org-export-pdf
224 :group 'org-export-man
225 :version "24.4"
226 :package-version '(Org . "8.0")
227 :type '(choice
228 (repeat :tag "Shell command sequence"
229 (string :tag "Shell command"))
230 (const :tag "2 runs of pdfgroff"
231 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
232 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
233 (const :tag "3 runs of pdfgroff"
234 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
235 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
236 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
237 (function)))
239 (defcustom org-man-logfiles-extensions
240 '("log" "out" "toc")
241 "The list of file extensions to consider as Man logfiles."
242 :group 'org-export-man
243 :version "24.4"
244 :package-version '(Org . "8.0")
245 :type '(repeat (string :tag "Extension")))
247 (defcustom org-man-remove-logfiles t
248 "Non-nil means remove the logfiles produced by PDF production.
249 These are the .aux, .log, .out, and .toc files."
250 :group 'org-export-man
251 :version "24.4"
252 :package-version '(Org . "8.0")
253 :type 'boolean)
257 ;;; Internal Functions
259 (defun org-man--caption/label-string (element info)
260 "Return caption and label Man string for ELEMENT.
262 INFO is a plist holding contextual information. If there's no
263 caption nor label, return the empty string.
265 For non-floats, see `org-man--wrap-label'."
266 (let ((label (org-element-property :label element))
267 (main (org-export-get-caption element))
268 (short (org-export-get-caption element t)))
269 (cond ((and (not main) (not label)) "")
270 ((not main) (format "\\fI%s\\fP" label))
271 ;; Option caption format with short name.
272 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
273 (org-export-data short info)
274 (org-export-data main info)))
275 ;; Standard caption format.
276 (t (format "\\fR%s\\fP" (org-export-data main info))))))
278 (defun org-man--wrap-label (element output)
279 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
280 This function shouldn't be used for floats. See
281 `org-man--caption/label-string'."
282 (let ((label (org-element-property :name element)))
283 (if (or (not output) (not label) (string= output "") (string= label ""))
284 output
285 (concat (format "%s\n.br\n" label) output))))
289 ;;; Template
291 (defun org-man-template (contents info)
292 "Return complete document string after Man conversion.
293 CONTENTS is the transcoded contents string. INFO is a plist
294 holding export options."
295 (let* ((title (when (plist-get info :with-title)
296 (org-export-data (plist-get info :title) info)))
297 (attr (read (format "(%s)"
298 (mapconcat
299 #'identity
300 (list (plist-get info :man-class-options))
301 " "))))
302 (section-item (plist-get attr :section-id)))
304 (concat
306 (cond
307 ((and title (stringp section-item))
308 (format ".TH \"%s\" \"%s\" \n" title section-item))
309 ((and (string= "" title) (stringp section-item))
310 (format ".TH \"%s\" \"%s\" \n" " " section-item))
311 (title
312 (format ".TH \"%s\" \"1\" \n" title))
314 ".TH \" \" \"1\" "))
315 contents)))
320 ;;; Transcode Functions
322 ;;; Babel Call
324 ;; Babel Calls are ignored.
327 ;;; Bold
329 (defun org-man-bold (_bold contents _info)
330 "Transcode BOLD from Org to Man.
331 CONTENTS is the text with bold markup. INFO is a plist holding
332 contextual information."
333 (format "\\fB%s\\fP" contents))
336 ;;; Center Block
338 (defun org-man-center-block (center-block contents _info)
339 "Transcode a CENTER-BLOCK element from Org to Man.
340 CONTENTS holds the contents of the center block. INFO is a plist
341 holding contextual information."
342 (org-man--wrap-label
343 center-block
344 (format ".ce %d\n.nf\n%s\n.fi"
345 (- (length (split-string contents "\n")) 1 )
346 contents)))
349 ;;; Code
351 (defun org-man-code (code _contents _info)
352 "Transcode a CODE object from Org to Man.
353 CONTENTS is nil. INFO is a plist used as a communication
354 channel."
355 (format "\\fC%s\\fP" code))
358 ;;; Drawer
360 (defun org-man-drawer (_drawer contents _info)
361 "Transcode a DRAWER element from Org to Man.
362 DRAWER holds the drawer information
363 CONTENTS holds the contents of the block.
364 INFO is a plist holding contextual information. "
365 contents)
368 ;;; Dynamic Block
370 (defun org-man-dynamic-block (dynamic-block contents _info)
371 "Transcode a DYNAMIC-BLOCK element from Org to Man.
372 CONTENTS holds the contents of the block. INFO is a plist
373 holding contextual information. See `org-export-data'."
374 (org-man--wrap-label dynamic-block contents))
377 ;;; Entity
379 (defun org-man-entity (entity _contents _info)
380 "Transcode an ENTITY object from Org to Man.
381 CONTENTS are the definition itself. INFO is a plist holding
382 contextual information."
383 (org-element-property :utf-8 entity))
386 ;;; Example Block
388 (defun org-man-example-block (example-block _contents info)
389 "Transcode an EXAMPLE-BLOCK element from Org to Man.
390 CONTENTS is nil. INFO is a plist holding contextual
391 information."
392 (org-man--wrap-label
393 example-block
394 (format ".RS\n.nf\n%s\n.fi\n.RE"
395 (org-export-format-code-default example-block info))))
398 ;;; Export Block
400 (defun org-man-export-block (export-block _contents _info)
401 "Transcode a EXPORT-BLOCK element from Org to Man.
402 CONTENTS is nil. INFO is a plist holding contextual information."
403 (when (string= (org-element-property :type export-block) "MAN")
404 (org-remove-indentation (org-element-property :value export-block))))
407 ;;; Export Snippet
409 (defun org-man-export-snippet (export-snippet _contents _info)
410 "Transcode a EXPORT-SNIPPET object from Org to Man.
411 CONTENTS is nil. INFO is a plist holding contextual information."
412 (when (eq (org-export-snippet-backend export-snippet) 'man)
413 (org-element-property :value export-snippet)))
416 ;;; Fixed Width
418 (defun org-man-fixed-width (fixed-width _contents _info)
419 "Transcode a FIXED-WIDTH element from Org to Man.
420 CONTENTS is nil. INFO is a plist holding contextual information."
421 (org-man--wrap-label
422 fixed-width
423 (format "\\fC\n%s\\fP"
424 (org-remove-indentation
425 (org-element-property :value fixed-width)))))
428 ;;; Footnote Definition
430 ;; Footnote Definitions are ignored.
432 ;;; Footnote References
434 ;; Footnote References are Ignored
437 ;;; Headline
439 (defun org-man-headline (headline contents info)
440 "Transcode a HEADLINE element from Org to Man.
441 CONTENTS holds the contents of the headline. INFO is a plist
442 holding contextual information."
443 (let* ((level (org-export-get-relative-level headline info))
444 ;; Section formatting will set two placeholders: one for the
445 ;; title and the other for the contents.
446 (section-fmt
447 (case level
448 (1 ".SH \"%s\"\n%s")
449 (2 ".SS \"%s\"\n%s")
450 (3 ".SS \"%s\"\n%s")
451 (t nil)))
452 (text (org-export-data (org-element-property :title headline) info)))
454 (cond
455 ;; Case 1: This is a footnote section: ignore it.
456 ((org-element-property :footnote-section-p headline) nil)
458 ;; Case 2. This is a deep sub-tree: export it as a list item.
459 ;; Also export as items headlines for which no section
460 ;; format has been found.
461 ((or (not section-fmt) (org-export-low-level-p headline info))
462 ;; Build the real contents of the sub-tree.
463 (let ((low-level-body
464 (concat
465 ;; If the headline is the first sibling, start a list.
466 (when (org-export-first-sibling-p headline info)
467 (format "%s\n" ".RS"))
468 ;; Itemize headline
469 ".TP\n.ft I\n" text "\n.ft\n"
470 contents ".RE")))
471 ;; If headline is not the last sibling simply return
472 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
473 ;; blank line.
474 (if (not (org-export-last-sibling-p headline info)) low-level-body
475 (replace-regexp-in-string
476 "[ \t\n]*\\'" ""
477 low-level-body))))
479 ;; Case 3. Standard headline. Export it as a section.
480 (t (format section-fmt text contents )))))
482 ;;; Horizontal Rule
483 ;; Not supported
485 ;;; Inline Babel Call
487 ;; Inline Babel Calls are ignored.
489 ;;; Inline Src Block
491 (defun org-man-inline-src-block (inline-src-block _contents info)
492 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
493 CONTENTS holds the contents of the item. INFO is a plist holding
494 contextual information."
495 (let* ((code (org-element-property :value inline-src-block)))
496 (cond
497 ((plist-get info :man-source-highlight)
498 (let* ((tmpdir (if (featurep 'xemacs)
499 temp-directory
500 temporary-file-directory ))
501 (in-file (make-temp-name
502 (expand-file-name "srchilite" tmpdir)))
503 (out-file (make-temp-name
504 (expand-file-name "reshilite" tmpdir)))
505 (org-lang (org-element-property :language inline-src-block))
506 (lst-lang
507 (cadr (assq (intern org-lang)
508 (plist-get info :man-source-highlight-langs))))
510 (cmd (concat (expand-file-name "source-highlight")
511 " -s " lst-lang
512 " -f groff_man"
513 " -i " in-file
514 " -o " out-file )))
516 (if lst-lang
517 (let ((code-block "" ))
518 (with-temp-file in-file (insert code))
519 (shell-command cmd)
520 (setq code-block (org-file-contents out-file))
521 (delete-file in-file)
522 (delete-file out-file)
523 code-block)
524 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
525 code))))
527 ;; Do not use a special package: transcode it verbatim.
529 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
530 "\\fP\n.fi\n.RE\n")))))
533 ;;; Inlinetask
534 ;;; Italic
536 (defun org-man-italic (_italic contents _info)
537 "Transcode ITALIC from Org to Man.
538 CONTENTS is the text with italic markup. INFO is a plist holding
539 contextual information."
540 (format "\\fI%s\\fP" contents))
543 ;;; Item
546 (defun org-man-item (item contents info)
548 "Transcode an ITEM element from Org to Man.
549 CONTENTS holds the contents of the item. INFO is a plist holding
550 contextual information."
552 (let* ((bullet (org-element-property :bullet item))
553 (type (org-element-property :type (org-element-property :parent item)))
554 (checkbox (case (org-element-property :checkbox item)
555 (on "\\o'\\(sq\\(mu'") ;;
556 (off "\\(sq ") ;;
557 (trans "\\o'\\(sq\\(mi'" ))) ;;
559 (tag (let ((tag (org-element-property :tag item)))
560 ;; Check-boxes must belong to the tag.
561 (and tag (format "\\fB%s\\fP"
562 (concat checkbox
563 (org-export-data tag info)))))))
565 (if (and (null tag )
566 (null checkbox))
567 (let* ((bullet (org-trim bullet))
568 (marker (cond ((string= "-" bullet) "\\(em")
569 ((string= "*" bullet) "\\(bu")
570 ((eq type 'ordered)
571 (format "%s " (org-trim bullet)))
572 (t "\\(dg"))))
573 (concat ".IP " marker " 4\n"
574 (org-trim (or contents " " ))))
575 ; else
576 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
577 (org-trim (or contents " " ))))))
579 ;;; Keyword
582 (defun org-man-keyword (keyword _contents _info)
583 "Transcode a KEYWORD element from Org to Man.
584 CONTENTS is nil. INFO is a plist holding contextual information."
585 (let ((key (org-element-property :key keyword))
586 (value (org-element-property :value keyword)))
587 (cond
588 ((string= key "MAN") value)
589 ((string= key "INDEX") nil)
590 ((string= key "TOC" ) nil))))
593 ;;; Line Break
595 (defun org-man-line-break (_line-break _contents _info)
596 "Transcode a LINE-BREAK object from Org to Man.
597 CONTENTS is nil. INFO is a plist holding contextual information."
598 ".br\n")
601 ;;; Link
604 (defun org-man-link (link desc _info)
605 "Transcode a LINK object from Org to Man.
607 DESC is the description part of the link, or the empty string.
608 INFO is a plist holding contextual information. See
609 `org-export-data'."
610 (let* ((type (org-element-property :type link))
611 (raw-path (org-element-property :path link))
612 ;; Ensure DESC really exists, or set it to nil.
613 (desc (and (not (string= desc "")) desc))
614 (path (cond
615 ((member type '("http" "https" "ftp" "mailto"))
616 (concat type ":" raw-path))
617 ((string= type "file") (org-export-file-uri raw-path))
618 (t raw-path))))
619 (cond
620 ;; Link type is handled by a special function.
621 ((org-export-custom-protocol-maybe link desc 'man))
622 ;; External link with a description part.
623 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
624 ;; External link without a description part.
625 (path (format "\\fI%s\\fP" path))
626 ;; No path, only description. Try to do something useful.
627 (t (format "\\fI%s\\fP" desc)))))
629 ;;;; Node Property
631 (defun org-man-node-property (node-property _contents _info)
632 "Transcode a NODE-PROPERTY element from Org to Man.
633 CONTENTS is nil. INFO is a plist holding contextual
634 information."
635 (format "%s:%s"
636 (org-element-property :key node-property)
637 (let ((value (org-element-property :value node-property)))
638 (if value (concat " " value) ""))))
640 ;;; Paragraph
642 (defun org-man-paragraph (paragraph contents _info)
643 "Transcode a PARAGRAPH element from Org to Man.
644 CONTENTS is the contents of the paragraph, as a string. INFO is
645 the plist used as a communication channel."
646 (let ((parent (plist-get (nth 1 paragraph) :parent)))
647 (when parent
648 (let ((parent-type (car parent))
649 (fixed-paragraph ""))
650 (cond ((and (eq parent-type 'item)
651 (plist-get (nth 1 parent) :bullet ))
652 (setq fixed-paragraph (concat "" contents)))
653 ((eq parent-type 'section)
654 (setq fixed-paragraph (concat ".PP\n" contents)))
655 ((eq parent-type 'footnote-definition)
656 (setq fixed-paragraph contents))
657 (t (setq fixed-paragraph (concat "" contents))))
658 fixed-paragraph ))))
661 ;;; Plain List
663 (defun org-man-plain-list (_plain-list contents _info)
664 "Transcode a PLAIN-LIST element from Org to Man.
665 CONTENTS is the contents of the list. INFO is a plist holding
666 contextual information."
667 contents)
669 ;;; Plain Text
671 (defun org-man-plain-text (text info)
672 "Transcode a TEXT string from Org to Man.
673 TEXT is the string to transcode. INFO is a plist holding
674 contextual information."
675 (let ((output text))
676 ;; Protect various chars.
677 (setq output (replace-regexp-in-string
678 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
679 "$\\" output nil t 1))
680 ;; Activate smart quotes. Be sure to provide original TEXT string
681 ;; since OUTPUT may have been modified.
682 (when (plist-get info :with-smart-quotes)
683 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
684 ;; Handle break preservation if required.
685 (when (plist-get info :preserve-breaks)
686 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
687 output)))
688 ;; Return value.
689 output))
693 ;;; Planning
696 ;;; Property Drawer
698 (defun org-man-property-drawer (_property-drawer contents _info)
699 "Transcode a PROPERTY-DRAWER element from Org to Man.
700 CONTENTS holds the contents of the drawer. INFO is a plist
701 holding contextual information."
702 (and (org-string-nw-p contents)
703 (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
705 ;;; Quote Block
707 (defun org-man-quote-block (quote-block contents _info)
708 "Transcode a QUOTE-BLOCK element from Org to Man.
709 CONTENTS holds the contents of the block. INFO is a plist
710 holding contextual information."
711 (org-man--wrap-label
712 quote-block
713 (format ".RS\n%s\n.RE" contents)))
716 ;;; Radio Target
718 (defun org-man-radio-target (_radio-target text _info)
719 "Transcode a RADIO-TARGET object from Org to Man.
720 TEXT is the text of the target. INFO is a plist holding
721 contextual information."
722 text)
725 ;;; Section
727 (defun org-man-section (_section contents _info)
728 "Transcode a SECTION element from Org to Man.
729 CONTENTS holds the contents of the section. INFO is a plist
730 holding contextual information."
731 contents)
734 ;;; Special Block
736 (defun org-man-special-block (special-block contents _info)
737 "Transcode a SPECIAL-BLOCK element from Org to Man.
738 CONTENTS holds the contents of the block. INFO is a plist
739 holding contextual information."
740 (org-man--wrap-label special-block (format "%s\n" contents)))
743 ;;; Src Block
745 (defun org-man-src-block (src-block _contents info)
746 "Transcode a SRC-BLOCK element from Org to Man.
747 CONTENTS holds the contents of the item. INFO is a plist holding
748 contextual information."
749 (if (not (plist-get info :man-source-highlight))
750 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
751 (org-export-format-code-default src-block info))
752 (let* ((tmpdir temporary-file-directory)
753 (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
754 (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
755 (code (org-element-property :value src-block))
756 (org-lang (org-element-property :language src-block))
757 (lst-lang
758 (cadr (assq (intern org-lang)
759 (plist-get info :man-source-highlight-langs))))
760 (cmd (concat "source-highlight"
761 " -s " lst-lang
762 " -f groff_man "
763 " -i " in-file
764 " -o " out-file)))
765 (if lst-lang
766 (let ((code-block ""))
767 (with-temp-file in-file (insert code))
768 (shell-command cmd)
769 (setq code-block (org-file-contents out-file))
770 (delete-file in-file)
771 (delete-file out-file)
772 code-block)
773 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))
776 ;;; Statistics Cookie
778 (defun org-man-statistics-cookie (statistics-cookie _contents _info)
779 "Transcode a STATISTICS-COOKIE object from Org to Man.
780 CONTENTS is nil. INFO is a plist holding contextual information."
781 (org-element-property :value statistics-cookie))
784 ;;; Strike-Through
786 (defun org-man-strike-through (_strike-through contents _info)
787 "Transcode STRIKE-THROUGH from Org to Man.
788 CONTENTS is the text with strike-through markup. INFO is a plist
789 holding contextual information."
790 (format "\\fI%s\\fP" contents))
792 ;;; Subscript
794 (defun org-man-subscript (_subscript contents _info)
795 "Transcode a SUBSCRIPT object from Org to Man.
796 CONTENTS is the contents of the object. INFO is a plist holding
797 contextual information."
798 (format "\\d\\s-2%s\\s+2\\u" contents))
800 ;;; Superscript "^_%s$
802 (defun org-man-superscript (_superscript contents _info)
803 "Transcode a SUPERSCRIPT object from Org to Man.
804 CONTENTS is the contents of the object. INFO is a plist holding
805 contextual information."
806 (format "\\u\\s-2%s\\s+2\\d" contents))
809 ;;; Table
811 ;; `org-man-table' is the entry point for table transcoding. It
812 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
813 ;; delegates the job to either `org-man-table--table.el-table' or
814 ;; `org-man-table--org-table' functions, depending of the type of
815 ;; the table.
817 ;; `org-man-table--align-string' is a subroutine used to build
818 ;; alignment string for Org tables.
820 (defun org-man-table (table contents info)
821 "Transcode a TABLE element from Org to Man.
822 CONTENTS is the contents of the table. INFO is a plist holding
823 contextual information."
824 (cond
825 ;; Case 1: verbatim table.
826 ((or (plist-get info :man-tables-verbatim)
827 (let ((attr (read (format "(%s)"
828 (mapconcat
829 #'identity
830 (org-element-property :attr_man table)
831 " ")))))
833 (and attr (plist-get attr :verbatim))))
835 (format ".nf\n\\fC%s\\fP\n.fi"
836 ;; Re-create table, without affiliated keywords.
837 (org-trim
838 (org-element-interpret-data
839 `(table nil ,@(org-element-contents table))))))
840 ;; Case 2: Standard table.
841 (t (org-man-table--org-table table contents info))))
843 (defun org-man-table--align-string (divider table info)
844 "Return an appropriate Man alignment string.
845 TABLE is the considered table. INFO is a plist used as
846 a communication channel."
847 (let (alignment)
848 ;; Extract column groups and alignment from first (non-rule) row.
849 (org-element-map
850 (org-element-map table 'table-row
851 (lambda (row)
852 (and (eq (org-element-property :type row) 'standard) row))
853 info 'first-match)
854 'table-cell
855 (lambda (cell)
856 (let* ((borders (org-export-table-cell-borders cell info))
857 (raw-width (org-export-table-cell-width cell info))
858 (width-cm (when raw-width (/ raw-width 5)))
859 (width (if raw-width (format "w(%dc)"
860 (if (< width-cm 1) 1 width-cm)) "")))
861 ;; Check left border for the first cell only.
862 (when (and (memq 'left borders) (not alignment))
863 (push "|" alignment))
864 (push
865 (case (org-export-table-cell-alignment cell info)
866 (left (concat "l" width divider))
867 (right (concat "r" width divider))
868 (center (concat "c" width divider)))
869 alignment)
870 (when (memq 'right borders) (push "|" alignment))))
871 info)
872 (apply 'concat (reverse alignment))))
874 (defun org-man-table--org-table (table contents info)
875 "Return appropriate Man code for an Org table.
877 TABLE is the table type element to transcode. CONTENTS is its
878 contents, as a string. INFO is a plist used as a communication
879 channel.
881 This function assumes TABLE has `org' as its `:type' attribute."
882 (let* ((attr (org-export-read-attribute :attr_man table))
883 (caption (and (not (plist-get attr :disable-caption))
884 (org-man--caption/label-string table info)))
885 (divider (if (plist-get attr :divider) "|" " "))
887 ;; Determine alignment string.
888 (alignment (org-man-table--align-string divider table info))
889 ;; Extract others display options.
891 (lines (org-split-string contents "\n"))
893 (attr-list
894 (delq nil
895 (list
896 (and (plist-get attr :expand) "expand")
897 (let ((placement (plist-get attr :placement)))
898 (cond ((string= placement 'center) "center")
899 ((string= placement 'left) nil)
900 ((plist-get info :man-tables-centered) "center")
901 (t "")))
902 (or (plist-get attr :boxtype) "box"))))
904 (title-line (plist-get attr :title-line))
905 (long-cells (plist-get attr :long-cells))
907 (table-format (concat
908 (format "%s" (or (car attr-list) "" ))
910 (let ((output-list '()))
911 (when (cdr attr-list)
912 (dolist (attr-item (cdr attr-list))
913 (setq output-list (concat output-list (format ",%s" attr-item)))))
914 output-list)
915 "")))
917 (first-line (when lines (org-split-string (car lines) "\t"))))
918 ;; Prepare the final format string for the table.
921 (cond
922 ;; Others.
923 (lines (concat ".TS\n " table-format ";\n"
925 (format "%s.\n"
926 (let ((final-line ""))
927 (when title-line
928 (dotimes (_ (length first-line))
929 (setq final-line (concat final-line "cb" divider))))
931 (setq final-line (concat final-line "\n"))
933 (if alignment
934 (setq final-line (concat final-line alignment))
935 (dotimes (_ (length first-line))
936 (setq final-line (concat final-line "c" divider))))
937 final-line ))
939 (format "%s.TE\n"
940 (let ((final-line "")
941 (long-line "")
942 (lines (org-split-string contents "\n")))
944 (dolist (line-item lines)
945 (setq long-line "")
947 (if long-cells
948 (progn
949 (if (string= line-item "_")
950 (setq long-line (format "%s\n" line-item))
951 ;; else string =
952 (let ((cell-item-list (org-split-string line-item "\t")))
953 (dolist (cell-item cell-item-list)
955 (cond ((eq cell-item (car (last cell-item-list)))
956 (setq long-line (concat long-line
957 (format "T{\n%s\nT}\t\n" cell-item ))))
959 (setq long-line (concat long-line
960 (format "T{\n%s\nT}\t" cell-item ))))))
961 long-line))
962 ;; else long cells
963 (setq final-line (concat final-line long-line )))
965 (setq final-line (concat final-line line-item "\n"))))
966 final-line))
968 (and caption (format ".TB \"%s\"" caption)))))))
970 ;;; Table Cell
972 (defun org-man-table-cell (table-cell contents info)
973 "Transcode a TABLE-CELL element from Org to Man
974 CONTENTS is the cell contents. INFO is a plist used as
975 a communication channel."
976 (concat
977 (let ((scientific-format (plist-get info :man-table-scientific-notation)))
978 (if (and contents
979 scientific-format
980 (string-match orgtbl-exp-regexp contents))
981 ;; Use appropriate format string for scientific notation.
982 (format scientific-format
983 (match-string 1 contents)
984 (match-string 2 contents))
985 contents))
986 (when (org-export-get-next-element table-cell info) "\t")))
989 ;;; Table Row
991 (defun org-man-table-row (table-row contents info)
992 "Transcode a TABLE-ROW element from Org to Man.
993 CONTENTS is the contents of the row. INFO is a plist used as
994 a communication channel."
995 ;; Rules are ignored since table separators are deduced from borders
996 ;; of the current row.
997 (when (eq (org-element-property :type table-row) 'standard)
998 (let ((borders
999 ;; TABLE-ROW's borders are extracted from its first cell.
1000 (org-export-table-cell-borders
1001 (car (org-element-contents table-row)) info)))
1002 (concat
1003 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1004 contents
1005 (cond ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1006 ((memq 'below borders) "\n_"))))))
1009 ;;; Target
1011 (defun org-man-target (target _contents info)
1012 "Transcode a TARGET object from Org to Man.
1013 CONTENTS is nil. INFO is a plist holding contextual
1014 information."
1015 (format "\\fI%s\\fP" (org-export-get-reference target info)))
1018 ;;; Timestamp
1020 (defun org-man-timestamp (_timestamp _contents _info)
1021 "Transcode a TIMESTAMP object from Org to Man.
1022 ONTENTS is nil. INFO is a plist holding contextual information."
1026 ;;; Underline
1028 (defun org-man-underline (_underline contents _info)
1029 "Transcode UNDERLINE from Org to Man.
1030 CONTENTS is the text with underline markup. INFO is a plist
1031 holding contextual information."
1032 (format "\\fI%s\\fP" contents))
1035 ;;; Verbatim
1037 (defun org-man-verbatim (_verbatim contents _info)
1038 "Transcode a VERBATIM object from Org to Man.
1039 CONTENTS is nil. INFO is a plist used as a communication
1040 channel."
1041 (format ".nf\n%s\n.fi" contents))
1044 ;;; Verse Block
1046 (defun org-man-verse-block (_verse-block contents _info)
1047 "Transcode a VERSE-BLOCK element from Org to Man.
1048 CONTENTS is verse block contents. INFO is a plist holding
1049 contextual information."
1050 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1054 ;;; Interactive functions
1056 (defun org-man-export-to-man
1057 (&optional async subtreep visible-only body-only ext-plist)
1058 "Export current buffer to a Man file.
1060 If narrowing is active in the current buffer, only export its
1061 narrowed part.
1063 If a region is active, export that region.
1065 A non-nil optional argument ASYNC means the process should happen
1066 asynchronously. The resulting file should be accessible through
1067 the `org-export-stack' interface.
1069 When optional argument SUBTREEP is non-nil, export the sub-tree
1070 at point, extracting information from the headline properties
1071 first.
1073 When optional argument VISIBLE-ONLY is non-nil, don't export
1074 contents of hidden elements.
1076 When optional argument BODY-ONLY is non-nil, only the body
1077 without any markers.
1079 EXT-PLIST, when provided, is a property list with external
1080 parameters overriding Org default settings, but still inferior to
1081 file-local settings.
1083 Return output file's name."
1084 (interactive)
1085 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1086 (org-export-to-file 'man outfile
1087 async subtreep visible-only body-only ext-plist)))
1089 (defun org-man-export-to-pdf
1090 (&optional async subtreep visible-only body-only ext-plist)
1091 "Export current buffer to Groff then process through to PDF.
1093 If narrowing is active in the current buffer, only export its
1094 narrowed part.
1096 If a region is active, export that region.
1098 A non-nil optional argument ASYNC means the process should happen
1099 asynchronously. The resulting file should be accessible through
1100 the `org-export-stack' interface.
1102 When optional argument SUBTREEP is non-nil, export the sub-tree
1103 at point, extracting information from the headline properties
1104 first.
1106 When optional argument VISIBLE-ONLY is non-nil, don't export
1107 contents of hidden elements.
1109 When optional argument BODY-ONLY is non-nil, only write between
1110 markers.
1112 EXT-PLIST, when provided, is a property list with external
1113 parameters overriding Org default settings, but still inferior to
1114 file-local settings.
1116 Return PDF file's name."
1117 (interactive)
1118 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1119 (org-export-to-file 'man outfile
1120 async subtreep visible-only body-only ext-plist
1121 (lambda (file) (org-latex-compile file)))))
1123 (defun org-man-compile (file)
1124 "Compile a Groff file.
1126 FILE is the name of the file being compiled. Processing is done
1127 through the command specified in `org-man-pdf-process'.
1129 Return PDF file name or an error if it couldn't be produced."
1130 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1131 (full-name (file-truename file))
1132 (out-dir (file-name-directory file))
1133 (time (current-time))
1134 ;; Properly set working directory for compilation.
1135 (default-directory (if (file-name-absolute-p file)
1136 (file-name-directory full-name)
1137 default-directory))
1138 errors)
1139 (message "Processing Groff file %s..." file)
1140 (save-window-excursion
1141 (cond
1142 ;; A function is provided: Apply it.
1143 ((functionp org-man-pdf-process)
1144 (funcall org-man-pdf-process (shell-quote-argument file)))
1145 ;; A list is provided: Replace %b, %f and %o with appropriate
1146 ;; values in each command before applying it. Output is
1147 ;; redirected to "*Org PDF Groff Output*" buffer.
1148 ((consp org-man-pdf-process)
1149 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1150 (dolist (command org-man-pdf-process)
1151 (shell-command
1152 (replace-regexp-in-string
1153 "%b" (shell-quote-argument base-name)
1154 (replace-regexp-in-string
1155 "%f" (shell-quote-argument full-name)
1156 (replace-regexp-in-string
1157 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1158 outbuf))
1159 ;; Collect standard errors from output buffer.
1160 (setq errors (org-man-collect-errors outbuf))))
1161 (t (error "No valid command to process to PDF")))
1162 (let ((pdffile (concat out-dir base-name ".pdf")))
1163 ;; Check for process failure. Provide collected errors if
1164 ;; possible.
1165 (if (or (not (file-exists-p pdffile))
1166 ;; Only compare times up to whole seconds as some
1167 ;; filesystems (e.g. HFS+) do not retain any finer
1168 ;; granularity.
1169 (time-less-p (cl-subseq (nth 5 (file-attributes pdffile)) 0 2)
1170 (cl-subseq time 0 2)))
1171 (error "PDF file %s wasn't produced%s"
1172 pdffile
1173 (if errors (concat ": " errors) ""))
1174 ;; Else remove log files, when specified, and signal end of
1175 ;; process to user, along with any error encountered.
1176 (when org-man-remove-logfiles
1177 (dolist (ext org-man-logfiles-extensions)
1178 (let ((file (concat out-dir base-name "." ext)))
1179 (when (file-exists-p file) (delete-file file)))))
1180 (message (concat "Process completed"
1181 (if (not errors) "."
1182 (concat " with errors: " errors)))))
1183 ;; Return output file name.
1184 pdffile))))
1186 (defun org-man-collect-errors (buffer)
1187 "Collect some kind of errors from \"groff\" output
1188 BUFFER is the buffer containing output.
1189 Return collected error types as a string, or nil if there was
1190 none."
1191 (with-current-buffer buffer
1192 (save-excursion
1193 (goto-char (point-max))
1194 ;; Find final run
1195 nil )))
1198 (provide 'ox-man)
1200 ;; Local variables:
1201 ;; generated-autoload-file: "org-loaddefs.el"
1202 ;; End:
1204 ;;; ox-man.el ends here