export back-ends: Remove useless pub-dir argument from export commands
[org-mode.git] / contrib / lisp / org-e-man.el
blob2b7037008c3cceacf706478e12ecdf814e9f7c12
1 ;; org-e-man.el --- Man Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;;
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a Man back-end for Org generic exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'e-man "*Test e-Man*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Man
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces one new buffer keywords:
36 ;; "MAN_CLASS_OPTIONS".
38 ;;; Code:
40 (require 'org-export)
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 e-man
53 ((babel-call . org-e-man-babel-call)
54 (bold . org-e-man-bold)
55 (center-block . org-e-man-center-block)
56 (clock . org-e-man-clock)
57 (code . org-e-man-code)
58 (comment . org-e-man-comment)
59 (comment-block . org-e-man-comment-block)
60 (drawer . org-e-man-drawer)
61 (dynamic-block . org-e-man-dynamic-block)
62 (entity . org-e-man-entity)
63 (example-block . org-e-man-example-block)
64 (export-block . org-e-man-export-block)
65 (export-snippet . org-e-man-export-snippet)
66 (fixed-width . org-e-man-fixed-width)
67 (footnote-definition . org-e-man-footnote-definition)
68 (footnote-reference . org-e-man-footnote-reference)
69 (headline . org-e-man-headline)
70 (horizontal-rule . org-e-man-horizontal-rule)
71 (inline-babel-call . org-e-man-inline-babel-call)
72 (inline-src-block . org-e-man-inline-src-block)
73 (inlinetask . org-e-man-inlinetask)
74 (italic . org-e-man-italic)
75 (item . org-e-man-item)
76 (keyword . org-e-man-keyword)
77 (man-environment . org-e-man-man-environment)
78 (man-fragment . org-e-man-man-fragment)
79 (line-break . org-e-man-line-break)
80 (link . org-e-man-link)
81 (paragraph . org-e-man-paragraph)
82 (plain-list . org-e-man-plain-list)
83 (plain-text . org-e-man-plain-text)
84 (planning . org-e-man-planning)
85 (property-drawer . org-e-man-property-drawer)
86 (quote-block . org-e-man-quote-block)
87 (quote-section . org-e-man-quote-section)
88 (radio-target . org-e-man-radio-target)
89 (section . org-e-man-section)
90 (special-block . org-e-man-special-block)
91 (src-block . org-e-man-src-block)
92 (statistics-cookie . org-e-man-statistics-cookie)
93 (strike-through . org-e-man-strike-through)
94 (subscript . org-e-man-subscript)
95 (superscript . org-e-man-superscript)
96 (table . org-e-man-table)
97 (table-cell . org-e-man-table-cell)
98 (table-row . org-e-man-table-row)
99 (target . org-e-man-target)
100 (template . org-e-man-template)
101 (timestamp . org-e-man-timestamp)
102 (underline . org-e-man-underline)
103 (verbatim . org-e-man-verbatim)
104 (verse-block . org-e-man-verse-block))
105 :export-block "MAN"
106 :menu-entry
107 (?m "Export to MAN"
108 ((?m "As MAN file" org-e-man-export-to-man)
109 (?p "As PDF file" org-e-man-export-to-pdf)
110 (?o "As PDF file and open"
111 (lambda (s v b) (org-open-file (org-e-man-export-to-pdf s v b))))))
112 :options-alist
113 ((:man-class "MAN_CLASS" nil nil t)
114 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
115 (:man-header-extra "MAN_HEADER" nil nil newline)))
119 ;;; User Configurable Variables
121 (defgroup org-export-e-man nil
122 "Options for exporting Org mode files to Man."
123 :tag "Org Export Man"
124 :group 'org-export)
126 ;;; Tables
128 (defcustom org-e-man-tables-centered t
129 "When non-nil, tables are exported in a center environment."
130 :group 'org-export-e-man
131 :type 'boolean)
133 (defcustom org-e-man-tables-verbatim nil
134 "When non-nil, tables are exported verbatim."
135 :group 'org-export-e-man
136 :type 'boolean)
139 (defcustom org-e-man-table-scientific-notation "%sE%s"
140 "Format string to display numbers in scientific notation.
141 The format should have \"%s\" twice, for mantissa and exponent
142 \(i.e. \"%s\\\\times10^{%s}\").
144 When nil, no transformation is made."
145 :group 'org-export-e-man
146 :type '(choice
147 (string :tag "Format string")
148 (const :tag "No formatting")))
151 ;;; Inlinetasks
152 ;; Src blocks
154 (defcustom org-e-man-source-highlight nil
155 "Use GNU source highlight to embellish source blocks "
156 :group 'org-export-e-man
157 :type 'boolean)
160 (defcustom org-e-man-source-highlight-langs
161 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
162 (scheme "scheme")
163 (c "c") (cc "cpp") (csharp "csharp") (d "d")
164 (fortran "fortran") (cobol "cobol") (pascal "pascal")
165 (ada "ada") (asm "asm")
166 (perl "perl") (cperl "perl")
167 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
168 (java "java") (javascript "javascript")
169 (tex "latex")
170 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
171 (ocaml "caml") (caml "caml")
172 (sql "sql") (sqlite "sql")
173 (html "html") (css "css") (xml "xml")
174 (bat "bat") (bison "bison") (clipper "clipper")
175 (ldap "ldap") (opa "opa")
176 (php "php") (postscript "postscript") (prolog "prolog")
177 (properties "properties") (makefile "makefile")
178 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
179 "Alist mapping languages to their listing language counterpart.
180 The key is a symbol, the major mode symbol without the \"-mode\".
181 The value is the string that should be inserted as the language
182 parameter for the listings package. If the mode name and the
183 listings name are the same, the language does not need an entry
184 in this list - but it does not hurt if it is present."
185 :group 'org-export-e-man
186 :type '(repeat
187 (list
188 (symbol :tag "Major mode ")
189 (string :tag "Listings language"))))
193 (defvar org-e-man-custom-lang-environments nil
194 "Alist mapping languages to language-specific Man environments.
196 It is used during export of src blocks by the listings and
197 man packages. For example,
199 \(setq org-e-man-custom-lang-environments
200 '\(\(python \"pythoncode\"\)\)\)
202 would have the effect that if org encounters begin_src python
203 during man export."
207 ;;; Compilation
209 (defcustom org-e-man-pdf-process
210 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
211 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
212 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
214 "Commands to process a Man file to a PDF file.
215 This is a list of strings, each of them will be given to the
216 shell as a command. %f in the command will be replaced by the
217 full file name, %b by the file base name (i.e. without directory
218 and extension parts) and %o by the base directory of the file.
221 By default, Org uses 3 runs of to do the processing.
223 Alternatively, this may be a Lisp function that does the
224 processing. This function should accept the file name as
225 its single argument."
226 :group 'org-export-pdf
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-e-man-logfiles-extensions
240 '("log" "out" "toc")
241 "The list of file extensions to consider as Man logfiles."
242 :group 'org-export-e-man
243 :type '(repeat (string :tag "Extension")))
245 (defcustom org-e-man-remove-logfiles t
246 "Non-nil means remove the logfiles produced by PDF production.
247 These are the .aux, .log, .out, and .toc files."
248 :group 'org-export-e-man
249 :type 'boolean)
253 ;;; Internal Functions
255 (defun org-e-man--caption/label-string (element info)
256 "Return caption and label Man string for ELEMENT.
258 INFO is a plist holding contextual information. If there's no
259 caption nor label, return the empty string.
261 For non-floats, see `org-e-man--wrap-label'."
262 (let ((label (org-element-property :label element))
263 (main (org-export-get-caption element))
264 (short (org-export-get-caption element t)))
265 (cond ((and (not main) (not label)) "")
266 ((not main) (format "\\fI%s\\fP" label))
267 ;; Option caption format with short name.
268 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
269 (org-export-data short info)
270 (org-export-data main info)))
271 ;; Standard caption format.
272 (t (format "\\fR%s\\fP" (org-export-data main info))))))
274 (defun org-e-man--wrap-label (element output)
275 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
276 This function shouldn't be used for floats. See
277 `org-e-man--caption/label-string'."
278 (let ((label (org-element-property :name element)))
279 (if (or (not output) (not label) (string= output "") (string= label ""))
280 output
281 (concat (format "%s\n.br\n" label) output))))
285 ;;; Template
287 (defun org-e-man-template (contents info)
288 "Return complete document string after Man conversion.
289 CONTENTS is the transcoded contents string. INFO is a plist
290 holding export options."
291 (let* ((title (org-export-data (plist-get info :title) info))
292 (attr (read (format "(%s)"
293 (mapconcat
294 #'identity
295 (list (plist-get info :man-class-options))
296 " "))))
297 (section-item (plist-get attr :section-id)))
299 (concat
301 (cond
302 ((and title (stringp section-item))
303 (format ".TH \"%s\" \"%s\" \n" title section-item))
304 ((and (string= "" title) (stringp section-item))
305 (format ".TH \"%s\" \"%s\" \n" " " section-item))
306 (title
307 (format ".TH \"%s\" \"1\" \n" title))
309 ".TH \" \" \"1\" "))
310 contents)))
315 ;;; Transcode Functions
317 ;;; Babel Call
319 ;; Babel Calls are ignored.
322 ;;; Bold
324 (defun org-e-man-bold (bold contents info)
325 "Transcode BOLD from Org to Man.
326 CONTENTS is the text with bold markup. INFO is a plist holding
327 contextual information."
328 (format "\\fB%s\\fP" contents))
331 ;;; Center Block
333 (defun org-e-man-center-block (center-block contents info)
334 "Transcode a CENTER-BLOCK element from Org to Man.
335 CONTENTS holds the contents of the center block. INFO is a plist
336 holding contextual information."
337 (org-e-man--wrap-label
338 center-block
339 (format ".ce %d\n.nf\n%s\n.fi"
340 (- (length (split-string contents "\n")) 1 )
341 contents)))
344 ;;; Clock
346 (defun org-e-man-clock (clock contents info)
347 "Transcode a CLOCK element from Org to Man.
348 CONTENTS is nil. INFO is a plist holding contextual
349 information."
350 "" )
353 ;;; Code
355 (defun org-e-man-code (code contents info)
356 "Transcode a CODE object from Org to Man.
357 CONTENTS is nil. INFO is a plist used as a communication
358 channel."
359 (format "\\fC%s\\fP" code))
362 ;;; Comment
364 ;; Comments are ignored.
367 ;;; Comment Block
369 ;; Comment Blocks are ignored.
372 ;;; Drawer
374 (defun org-e-man-drawer (drawer contents info)
375 "Transcode a DRAWER element from Org to Man.
376 DRAWER holds the drawer information
377 CONTENTS holds the contents of the block.
378 INFO is a plist holding contextual information. "
379 contents)
382 ;;; Dynamic Block
384 (defun org-e-man-dynamic-block (dynamic-block contents info)
385 "Transcode a DYNAMIC-BLOCK element from Org to Man.
386 CONTENTS holds the contents of the block. INFO is a plist
387 holding contextual information. See `org-export-data'."
388 (org-e-man--wrap-label dynamic-block contents))
391 ;;; Entity
393 (defun org-e-man-entity (entity contents info)
394 "Transcode an ENTITY object from Org to Man.
395 CONTENTS are the definition itself. INFO is a plist holding
396 contextual information."
397 (org-element-property :utf-8 entity))
400 ;;; Example Block
402 (defun org-e-man-example-block (example-block contents info)
403 "Transcode an EXAMPLE-BLOCK element from Org to Man.
404 CONTENTS is nil. INFO is a plist holding contextual
405 information."
406 (org-e-man--wrap-label
407 example-block
408 (format ".RS\n.nf\n%s\n.fi\n.RE"
409 (org-export-format-code-default example-block info))))
412 ;;; Export Block
414 (defun org-e-man-export-block (export-block contents info)
415 "Transcode a EXPORT-BLOCK element from Org to Man.
416 CONTENTS is nil. INFO is a plist holding contextual information."
417 (when (string= (org-element-property :type export-block) "MAN")
418 (org-remove-indentation (org-element-property :value export-block))))
421 ;;; Export Snippet
423 (defun org-e-man-export-snippet (export-snippet contents info)
424 "Transcode a EXPORT-SNIPPET object from Org to Man.
425 CONTENTS is nil. INFO is a plist holding contextual information."
426 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
427 (org-element-property :value export-snippet)))
430 ;;; Fixed Width
432 (defun org-e-man-fixed-width (fixed-width contents info)
433 "Transcode a FIXED-WIDTH element from Org to Man.
434 CONTENTS is nil. INFO is a plist holding contextual information."
435 (org-e-man--wrap-label
436 fixed-width
437 (format "\\fC\n%s\\fP"
438 (org-remove-indentation
439 (org-element-property :value fixed-width)))))
442 ;;; Footnote Definition
444 ;; Footnote Definitions are ignored.
446 ;;; Footnote References
448 ;; Footnote References are Ignored
451 ;;; Headline
453 (defun org-e-man-headline (headline contents info)
454 "Transcode an HEADLINE element from Org to Man.
455 CONTENTS holds the contents of the headline. INFO is a plist
456 holding contextual information."
457 (let* ((level (org-export-get-relative-level headline info))
458 (numberedp (org-export-numbered-headline-p headline info))
459 ;; Section formatting will set two placeholders: one for the
460 ;; title and the other for the contents.
461 (section-fmt
462 (case level
463 (1 ".SH \"%s\"\n%s")
464 (2 ".SS \"%s\"\n%s")
465 (3 ".SS \"%s\"\n%s")
466 (t nil)))
467 (text (org-export-data (org-element-property :title headline) info)))
469 (cond
470 ;; Case 1: This is a footnote section: ignore it.
471 ((org-element-property :footnote-section-p headline) nil)
473 ;; Case 2. This is a deep sub-tree: export it as a list item.
474 ;; Also export as items headlines for which no section
475 ;; format has been found.
476 ((or (not section-fmt) (org-export-low-level-p headline info))
477 ;; Build the real contents of the sub-tree.
478 (let ((low-level-body
479 (concat
480 ;; If the headline is the first sibling, start a list.
481 (when (org-export-first-sibling-p headline info)
482 (format "%s\n" ".RS"))
483 ;; Itemize headline
484 ".TP\n.ft I\n" text "\n.ft\n"
485 contents ".RE")))
486 ;; If headline is not the last sibling simply return
487 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
488 ;; blank line.
489 (if (not (org-export-last-sibling-p headline info)) low-level-body
490 (replace-regexp-in-string
491 "[ \t\n]*\\'" ""
492 low-level-body))))
494 ;; Case 3. Standard headline. Export it as a section.
495 (t (format section-fmt text contents )))))
497 ;;; Horizontal Rule
498 ;; Not supported
500 ;;; Inline Babel Call
502 ;; Inline Babel Calls are ignored.
504 ;;; Inline Src Block
506 (defun org-e-man-inline-src-block (inline-src-block contents info)
507 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
508 CONTENTS holds the contents of the item. INFO is a plist holding
509 contextual information."
510 (let* ((code (org-element-property :value inline-src-block)))
511 (cond
512 (org-e-man-source-highlight
513 (let* ((tmpdir (if (featurep 'xemacs)
514 temp-directory
515 temporary-file-directory ))
516 (in-file (make-temp-name
517 (expand-file-name "srchilite" tmpdir)))
518 (out-file (make-temp-name
519 (expand-file-name "reshilite" tmpdir)))
520 (org-lang (org-element-property :language inline-src-block))
521 (lst-lang (cadr (assq (intern org-lang)
522 org-e-man-source-highlight-langs)))
524 (cmd (concat (expand-file-name "source-highlight")
525 " -s " lst-lang
526 " -f groff_man"
527 " -i " in-file
528 " -o " out-file )))
530 (if lst-lang
531 (let ((code-block "" ))
532 (with-temp-file in-file (insert code))
533 (shell-command cmd)
534 (setq code-block (org-file-contents out-file))
535 (delete-file in-file)
536 (delete-file out-file)
537 code-block)
538 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
539 code))))
541 ;; Do not use a special package: transcode it verbatim.
543 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
544 "\\fP\n.fi\n.RE\n")))))
547 ;;; Inlinetask
548 ;;; Italic
550 (defun org-e-man-italic (italic contents info)
551 "Transcode ITALIC from Org to Man.
552 CONTENTS is the text with italic markup. INFO is a plist holding
553 contextual information."
554 (format "\\fI%s\\fP" contents))
557 ;;; Item
560 (defun org-e-man-item (item contents info)
562 "Transcode an ITEM element from Org to Man.
563 CONTENTS holds the contents of the item. INFO is a plist holding
564 contextual information."
566 (let* ((bullet (org-element-property :bullet item))
567 (type (org-element-property :type (org-element-property :parent item)))
568 (checkbox (case (org-element-property :checkbox item)
569 (on "\\o'\\(sq\\(mu'") ;;
570 (off "\\(sq ") ;;
571 (trans "\\o'\\(sq\\(mi'" ))) ;;
573 (tag (let ((tag (org-element-property :tag item)))
574 ;; Check-boxes must belong to the tag.
575 (and tag (format "\\fB%s\\fP"
576 (concat checkbox
577 (org-export-data tag info)))))))
579 (if (and (null tag )
580 (null checkbox))
581 (let* ((bullet (org-trim bullet))
582 (marker (cond ((string= "-" bullet) "\\(em")
583 ((string= "*" bullet) "\\(bu")
584 ((eq type 'ordered)
585 (format "%s " (org-trim bullet)))
586 (t "\\(dg"))))
587 (concat ".IP " marker " 4\n"
588 (org-trim (or contents " " ))))
589 ; else
590 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
591 (org-trim (or contents " " ))))))
593 ;;; Keyword
596 (defun org-e-man-keyword (keyword contents info)
597 "Transcode a KEYWORD element from Org to Man.
598 CONTENTS is nil. INFO is a plist holding contextual information."
599 (let ((key (org-element-property :key keyword))
600 (value (org-element-property :value keyword)))
601 (cond
602 ((string= key "MAN") value)
603 ((string= key "INDEX") nil)
604 ;; Invisible targets.
605 ((string= key "TARGET") nil)
606 ((string= key "TOC" ) nil))))
609 ;;; Man Environment
611 (defun org-e-man-man-environment (man-environment contents info)
612 "Transcode a MAN-ENVIRONMENT element from Org to Man.
613 CONTENTS is nil. INFO is a plist holding contextual information."
614 (let ((label (org-element-property :name man-environment))
615 (value (org-remove-indentation
616 (org-element-property :value man-environment))))
617 (if (not (org-string-nw-p label)) value
618 ;; Environment is labelled: label must be within the environment
619 ;; (otherwise, a reference pointing to that element will count
620 ;; the section instead).
621 (with-temp-buffer
622 (insert value)
623 (goto-char (point-min))
624 (forward-line)
625 (insert (format "%s\n" label))
626 (buffer-string)))))
629 ;;; Man Fragment
631 (defun org-e-man-man-fragment (man-fragment contents info)
632 "Transcode a MAN-FRAGMENT object from Org to Man.
633 CONTENTS is nil. INFO is a plist holding contextual information."
634 (org-element-property :value man-fragment))
637 ;;; Line Break
639 (defun org-e-man-line-break (line-break contents info)
640 "Transcode a LINE-BREAK object from Org to Man.
641 CONTENTS is nil. INFO is a plist holding contextual information."
642 ".br\n")
645 ;;; Link
648 (defun org-e-man-link (link desc info)
649 "Transcode a LINK object from Org to Man.
651 DESC is the description part of the link, or the empty string.
652 INFO is a plist holding contextual information. See
653 `org-export-data'."
655 (let* ((type (org-element-property :type link))
656 (raw-path (org-element-property :path link))
657 ;; Ensure DESC really exists, or set it to nil.
658 (desc (and (not (string= desc "")) desc))
660 (path (cond
661 ((member type '("http" "https" "ftp" "mailto"))
662 (concat type ":" raw-path))
663 ((string= type "file")
664 (when (string-match "\\(.+\\)::.+" raw-path)
665 (setq raw-path (match-string 1 raw-path)))
666 (if (file-name-absolute-p raw-path)
667 (concat "file://" (expand-file-name raw-path))
668 (concat "file://" raw-path)))
669 (t raw-path)))
670 protocol)
671 (cond
672 ;; External link with a description part.
673 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
674 ;; External link without a description part.
675 (path (format "\\fI%s\\fP" path))
676 ;; No path, only description. Try to do something useful.
677 (t (format "\\fI%s\\fP" desc)))))
680 ;;; Paragraph
682 (defun org-e-man-paragraph (paragraph contents info)
683 "Transcode a PARAGRAPH element from Org to Man.
684 CONTENTS is the contents of the paragraph, as a string. INFO is
685 the plist used as a communication channel."
686 (let ((parent (plist-get (nth 1 paragraph) :parent)))
687 (when parent
688 (let ((parent-type (car parent))
689 (fixed-paragraph ""))
690 (cond ((and (eq parent-type 'item)
691 (plist-get (nth 1 parent) :bullet ))
692 (setq fixed-paragraph (concat "" contents)))
693 ((eq parent-type 'section)
694 (setq fixed-paragraph (concat ".PP\n" contents)))
695 ((eq parent-type 'footnote-definition)
696 (setq fixed-paragraph contents))
697 (t (setq fixed-paragraph (concat "" contents))))
698 fixed-paragraph ))))
701 ;;; Plain List
703 (defun org-e-man-plain-list (plain-list contents info)
704 "Transcode a PLAIN-LIST element from Org to Man.
705 CONTENTS is the contents of the list. INFO is a plist holding
706 contextual information."
707 contents)
709 ;;; Plain Text
711 (defun org-e-man-plain-text (text info)
712 "Transcode a TEXT string from Org to Man.
713 TEXT is the string to transcode. INFO is a plist holding
714 contextual information."
715 (let ((output text))
716 ;; Protect various chars.
717 (setq output (replace-regexp-in-string
718 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
719 "$\\" output nil t 1))
720 ;; Activate smart quotes. Be sure to provide original TEXT string
721 ;; since OUTPUT may have been modified.
722 (when (plist-get info :with-smart-quotes)
723 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
724 ;; Handle break preservation if required.
725 (when (plist-get info :preserve-breaks)
726 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
727 output)))
728 ;; Return value.
729 output))
733 ;;; Planning
736 ;;; Property Drawer
739 ;;; Quote Block
741 (defun org-e-man-quote-block (quote-block contents info)
742 "Transcode a QUOTE-BLOCK element from Org to Man.
743 CONTENTS holds the contents of the block. INFO is a plist
744 holding contextual information."
745 (org-e-man--wrap-label
746 quote-block
747 (format ".RS\n%s\n.RE" contents)))
749 ;;; Quote Section
751 (defun org-e-man-quote-section (quote-section contents info)
752 "Transcode a QUOTE-SECTION element from Org to Man.
753 CONTENTS is nil. INFO is a plist holding contextual information."
754 (let ((value (org-remove-indentation
755 (org-element-property :value quote-section))))
756 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
759 ;;; Radio Target
761 (defun org-e-man-radio-target (radio-target text info)
762 "Transcode a RADIO-TARGET object from Org to Man.
763 TEXT is the text of the target. INFO is a plist holding
764 contextual information."
765 text )
768 ;;; Section
770 (defun org-e-man-section (section contents info)
771 "Transcode a SECTION element from Org to Man.
772 CONTENTS holds the contents of the section. INFO is a plist
773 holding contextual information."
774 contents)
777 ;;; Special Block
779 (defun org-e-man-special-block (special-block contents info)
780 "Transcode a SPECIAL-BLOCK element from Org to Man.
781 CONTENTS holds the contents of the block. INFO is a plist
782 holding contextual information."
783 (let ((type (downcase (org-element-property :type special-block))))
784 (org-e-man--wrap-label
785 special-block
786 (format "%s\n" contents))))
789 ;;; Src Block
791 (defun org-e-man-src-block (src-block contents info)
792 "Transcode a SRC-BLOCK element from Org to Man.
793 CONTENTS holds the contents of the item. INFO is a plist holding
794 contextual information."
795 (let* ((lang (org-element-property :language src-block))
796 (code (org-element-property :value src-block))
797 (custom-env (and lang
798 (cadr (assq (intern lang)
799 org-e-man-custom-lang-environments))))
800 (num-start (case (org-element-property :number-lines src-block)
801 (continued (org-export-get-loc src-block info))
802 (new 0)))
803 (retain-labels (org-element-property :retain-labels src-block)))
804 (cond
805 ;; Case 1. No source fontification.
806 ((not org-e-man-source-highlight)
807 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
808 (org-export-format-code-default src-block info)))
809 (org-e-man-source-highlight
810 (let* ((tmpdir (if (featurep 'xemacs)
811 temp-directory
812 temporary-file-directory ))
814 (in-file (make-temp-name
815 (expand-file-name "srchilite" tmpdir)))
816 (out-file (make-temp-name
817 (expand-file-name "reshilite" tmpdir)))
819 (org-lang (org-element-property :language src-block))
820 (lst-lang (cadr (assq (intern org-lang)
821 org-e-man-source-highlight-langs)))
823 (cmd (concat "source-highlight"
824 " -s " lst-lang
825 " -f groff_man "
826 " -i " in-file
827 " -o " out-file)))
829 (if lst-lang
830 (let ((code-block ""))
831 (with-temp-file in-file (insert code))
832 (shell-command cmd)
833 (setq code-block (org-file-contents out-file))
834 (delete-file in-file)
835 (delete-file out-file)
836 code-block)
837 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
840 ;;; Statistics Cookie
842 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
843 "Transcode a STATISTICS-COOKIE object from Org to Man.
844 CONTENTS is nil. INFO is a plist holding contextual information."
845 (org-element-property :value statistics-cookie))
848 ;;; Strike-Through
850 (defun org-e-man-strike-through (strike-through contents info)
851 "Transcode STRIKE-THROUGH from Org to Man.
852 CONTENTS is the text with strike-through markup. INFO is a plist
853 holding contextual information."
854 (format "\\fI%s\\fP" contents))
856 ;;; Subscript
858 (defun org-e-man-subscript (subscript contents info)
859 "Transcode a SUBSCRIPT object from Org to Man.
860 CONTENTS is the contents of the object. INFO is a plist holding
861 contextual information."
862 (format "\\d\\s-2%s\\s+2\\u" contents))
864 ;;; Superscript "^_%s$
866 (defun org-e-man-superscript (superscript contents info)
867 "Transcode a SUPERSCRIPT object from Org to Man.
868 CONTENTS is the contents of the object. INFO is a plist holding
869 contextual information."
870 (format "\\u\\s-2%s\\s+2\\d" contents))
873 ;;; Table
875 ;; `org-e-man-table' is the entry point for table transcoding. It
876 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
877 ;; delegates the job to either `org-e-man-table--table.el-table' or
878 ;; `org-e-man-table--org-table' functions, depending of the type of
879 ;; the table.
881 ;; `org-e-man-table--align-string' is a subroutine used to build
882 ;; alignment string for Org tables.
884 (defun org-e-man-table (table contents info)
885 "Transcode a TABLE element from Org to Man.
886 CONTENTS is the contents of the table. INFO is a plist holding
887 contextual information."
888 (cond
889 ;; Case 1: verbatim table.
890 ((or org-e-man-tables-verbatim
891 (let ((attr (read (format "(%s)"
892 (mapconcat
893 #'identity
894 (org-element-property :attr_man table)
895 " ")))))
897 (and attr (plist-get attr :verbatim))))
899 (format ".nf\n\\fC%s\\fP\n.fi"
900 ;; Re-create table, without affiliated keywords.
901 (org-trim
902 (org-element-interpret-data
903 `(table nil ,@(org-element-contents table))))))
904 ;; Case 2: Standard table.
905 (t (org-e-man-table--org-table table contents info))))
907 (defun org-e-man-table--align-string (divider table info)
908 "Return an appropriate Man alignment string.
909 TABLE is the considered table. INFO is a plist used as
910 a communication channel."
911 (let (alignment)
912 ;; Extract column groups and alignment from first (non-rule)
913 ;; row.
914 (org-element-map
915 (org-element-map
916 table 'table-row
917 (lambda (row)
918 (and (eq (org-element-property :type row) 'standard) row))
919 info 'first-match)
920 'table-cell
921 (lambda (cell)
922 (let* ((borders (org-export-table-cell-borders cell info))
923 (raw-width (org-export-table-cell-width cell info))
924 (width-cm (when raw-width (/ raw-width 5)))
925 (width (if raw-width (format "w(%dc)"
926 (if (< width-cm 1) 1 width-cm)) "")))
927 ;; Check left border for the first cell only.
928 (when (and (memq 'left borders) (not alignment))
929 (push "|" alignment))
930 (push
931 (case (org-export-table-cell-alignment cell info)
932 (left (concat "l" width divider))
933 (right (concat "r" width divider))
934 (center (concat "c" width divider)))
935 alignment)
936 (when (memq 'right borders) (push "|" alignment))))
937 info)
938 (apply 'concat (reverse alignment))))
940 (defun org-e-man-table--org-table (table contents info)
941 "Return appropriate Man code for an Org table.
943 TABLE is the table type element to transcode. CONTENTS is its
944 contents, as a string. INFO is a plist used as a communication
945 channel.
947 This function assumes TABLE has `org' as its `:type' attribute."
948 (let* ((attr (org-export-read-attribute :attr_man table))
949 (label (org-element-property :name table))
950 (caption (and (not (plist-get attr :disable-caption))
951 (org-e-man--caption/label-string table info)))
952 (divider (if (plist-get attr :divider) "|" " "))
954 ;; Determine alignment string.
955 (alignment (org-e-man-table--align-string divider table info))
956 ;; Extract others display options.
958 (lines (org-split-string contents "\n"))
960 (attr-list
961 (let ((result-list '()))
962 (dolist (attr-item
963 (list
964 (if (plist-get attr :expand)
965 "expand" nil)
967 (case (plist-get attr :placement)
968 ('center "center")
969 ('left nil)
971 (if org-e-man-tables-centered
972 "center" "")))
974 (case (plist-get attr :boxtype)
975 ('box "box")
976 ('doublebox "doublebox")
977 ('allbox "allbox")
978 ('none nil)
979 (t "box"))))
981 (if attr-item
982 (add-to-list 'result-list attr-item)))
983 result-list ))
986 (title-line (plist-get attr :title-line))
987 (long-cells (plist-get attr :long-cells))
989 (table-format (concat
990 (format "%s" (or (car attr-list) "" ))
992 (let ((output-list '()))
993 (when (cdr attr-list)
994 (dolist (attr-item (cdr attr-list))
995 (setq output-list (concat output-list (format ",%s" attr-item)))))
996 output-list)
997 "")))
999 (first-line (when lines (org-split-string (car lines) "\t"))))
1000 ;; Prepare the final format string for the table.
1003 (cond
1004 ;; Others.
1005 (lines (concat ".TS\n " table-format ";\n"
1007 (format "%s.\n"
1008 (let ((final-line ""))
1009 (when title-line
1010 (dotimes (i (length first-line))
1011 (setq final-line (concat final-line "cb" divider))))
1013 (setq final-line (concat final-line "\n"))
1015 (if alignment
1016 (setq final-line (concat final-line alignment))
1017 (dotimes (i (length first-line))
1018 (setq final-line (concat final-line "c" divider))))
1019 final-line ))
1021 (format "%s.TE\n"
1022 (let ((final-line "")
1023 (long-line "")
1024 (lines (org-split-string contents "\n")))
1026 (dolist (line-item lines)
1027 (setq long-line "")
1029 (if long-cells
1030 (progn
1031 (if (string= line-item "_")
1032 (setq long-line (format "%s\n" line-item))
1033 ;; else string =
1034 (let ((cell-item-list (org-split-string line-item "\t")))
1035 (dolist (cell-item cell-item-list)
1037 (cond ((eq cell-item (car (last cell-item-list)))
1038 (setq long-line (concat long-line
1039 (format "T{\n%s\nT}\t\n" cell-item ))))
1041 (setq long-line (concat long-line
1042 (format "T{\n%s\nT}\t" cell-item ))))))
1043 long-line))
1044 ;; else long cells
1045 (setq final-line (concat final-line long-line )))
1047 (setq final-line (concat final-line line-item "\n"))))
1048 final-line))
1050 (and caption (format ".TB \"%s\"" caption)))))))
1052 ;;; Table Cell
1054 (defun org-e-man-table-cell (table-cell contents info)
1055 "Transcode a TABLE-CELL element from Org to Man
1056 CONTENTS is the cell contents. INFO is a plist used as
1057 a communication channel."
1058 (concat (if (and contents
1059 org-e-man-table-scientific-notation
1060 (string-match orgtbl-exp-regexp contents))
1061 ;; Use appropriate format string for scientific
1062 ;; notation.
1063 (format org-e-man-table-scientific-notation
1064 (match-string 1 contents)
1065 (match-string 2 contents))
1066 contents )
1067 (when (org-export-get-next-element table-cell info) "\t")))
1070 ;;; Table Row
1072 (defun org-e-man-table-row (table-row contents info)
1073 "Transcode a TABLE-ROW element from Org to Man
1074 CONTENTS is the contents of the row. INFO is a plist used as
1075 a communication channel."
1076 ;; Rules are ignored since table separators are deduced from
1077 ;; borders of the current row.
1078 (when (eq (org-element-property :type table-row) 'standard)
1079 (let* ((attr (mapconcat 'identity
1080 (org-element-property
1081 :attr_man (org-export-get-parent table-row))
1082 " "))
1083 ;; TABLE-ROW's borders are extracted from its first cell.
1084 (borders
1085 (org-export-table-cell-borders
1086 (car (org-element-contents table-row)) info)))
1087 (concat
1088 ;; Mark horizontal lines
1089 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1090 contents
1092 (cond
1093 ;; When BOOKTABS are activated enforce bottom rule even when
1094 ;; no hline was specifically marked.
1095 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1096 ((memq 'below borders) "\n_"))))))
1099 ;;; Target
1101 (defun org-e-man-target (target contents info)
1102 "Transcode a TARGET object from Org to Man.
1103 CONTENTS is nil. INFO is a plist holding contextual
1104 information."
1105 (format "\\fI%s\\fP"
1106 (org-export-solidify-link-text (org-element-property :value target))))
1109 ;;; Timestamp
1111 (defun org-e-man-timestamp (timestamp contents info)
1112 "Transcode a TIMESTAMP object from Org to Man.
1113 CONTENTS is nil. INFO is a plist holding contextual
1114 information."
1115 "" )
1118 ;;; Underline
1120 (defun org-e-man-underline (underline contents info)
1121 "Transcode UNDERLINE from Org to Man.
1122 CONTENTS is the text with underline markup. INFO is a plist
1123 holding contextual information."
1124 (format "\\fI%s\\fP" contents))
1127 ;;; Verbatim
1129 (defun org-e-man-verbatim (verbatim contents info)
1130 "Transcode a VERBATIM object from Org to Man.
1131 CONTENTS is nil. INFO is a plist used as a communication
1132 channel."
1133 (format ".nf\n%s\n.fi" contents))
1136 ;;; Verse Block
1138 (defun org-e-man-verse-block (verse-block contents info)
1139 "Transcode a VERSE-BLOCK element from Org to Man.
1140 CONTENTS is verse block contents. INFO is a plist holding
1141 contextual information."
1142 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1146 ;;; Interactive functions
1148 (defun org-e-man-export-to-man
1149 (&optional subtreep visible-only body-only ext-plist)
1150 "Export current buffer to a Man file.
1152 If narrowing is active in the current buffer, only export its
1153 narrowed part.
1155 If a region is active, export that region.
1157 When optional argument SUBTREEP is non-nil, export the sub-tree
1158 at point, extracting information from the headline properties
1159 first.
1161 When optional argument VISIBLE-ONLY is non-nil, don't export
1162 contents of hidden elements.
1164 When optional argument BODY-ONLY is non-nil, only the body
1165 without any markers.
1167 EXT-PLIST, when provided, is a property list with external
1168 parameters overriding Org default settings, but still inferior to
1169 file-local settings.
1171 Return output file's name."
1172 (interactive)
1173 (let ((outfile (org-export-output-file-name ".man" subtreep)))
1174 (org-export-to-file
1175 'e-man outfile subtreep visible-only body-only ext-plist)))
1177 (defun org-e-man-export-to-pdf
1178 (&optional subtreep visible-only body-only ext-plist)
1179 "Export current buffer to Groff then process through to PDF.
1181 If narrowing is active in the current buffer, only export its
1182 narrowed part.
1184 If a region is active, export that region.
1186 When optional argument SUBTREEP is non-nil, export the sub-tree
1187 at point, extracting information from the headline properties
1188 first.
1190 When optional argument VISIBLE-ONLY is non-nil, don't export
1191 contents of hidden elements.
1193 When optional argument BODY-ONLY is non-nil, only write between
1194 markers.
1196 EXT-PLIST, when provided, is a property list with external
1197 parameters overriding Org default settings, but still inferior to
1198 file-local settings.
1200 Return PDF file's name."
1201 (interactive)
1202 (org-e-man-compile
1203 (org-e-man-export-to-man subtreep visible-only body-only ext-plist)))
1205 (defun org-e-man-compile (file)
1206 "Compile a Groff file.
1208 FILE is the name of the file being compiled. Processing is done
1209 through the command specified in `org-e-man-pdf-process'.
1211 Return PDF file name or an error if it couldn't be produced."
1212 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1213 (full-name (file-truename file))
1214 (out-dir (file-name-directory file))
1215 ;; Make sure `default-directory' is set to FILE directory,
1216 ;; not to whatever value the current buffer may have.
1217 (default-directory (file-name-directory full-name))
1218 errors)
1219 (message (format "Processing Groff file %s ..." file))
1220 (save-window-excursion
1221 (cond
1222 ;; A function is provided: Apply it.
1223 ((functionp org-e-man-pdf-process)
1224 (funcall org-e-man-pdf-process (shell-quote-argument file)))
1225 ;; A list is provided: Replace %b, %f and %o with appropriate
1226 ;; values in each command before applying it. Output is
1227 ;; redirected to "*Org PDF Groff Output*" buffer.
1228 ((consp org-e-man-pdf-process)
1229 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1230 (mapc
1231 (lambda (command)
1232 (shell-command
1233 (replace-regexp-in-string
1234 "%b" (shell-quote-argument base-name)
1235 (replace-regexp-in-string
1236 "%f" (shell-quote-argument full-name)
1237 (replace-regexp-in-string
1238 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1239 outbuf))
1240 org-e-man-pdf-process)
1241 ;; Collect standard errors from output buffer.
1242 (setq errors (org-e-man-collect-errors outbuf))))
1243 (t (error "No valid command to process to PDF")))
1244 (let ((pdffile (concat out-dir base-name ".pdf")))
1245 ;; Check for process failure. Provide collected errors if
1246 ;; possible.
1247 (if (not (file-exists-p pdffile))
1248 (error (concat (format "PDF file %s wasn't produced" pdffile)
1249 (when errors (concat ": " errors))))
1250 ;; Else remove log files, when specified, and signal end of
1251 ;; process to user, along with any error encountered.
1252 (when org-e-man-remove-logfiles
1253 (dolist (ext org-e-man-logfiles-extensions)
1254 (let ((file (concat out-dir base-name "." ext)))
1255 (when (file-exists-p file) (delete-file file)))))
1256 (message (concat "Process completed"
1257 (if (not errors) "."
1258 (concat " with errors: " errors)))))
1259 ;; Return output file name.
1260 pdffile))))
1262 (defun org-e-man-collect-errors (buffer)
1263 "Collect some kind of errors from \"groff\" output
1264 BUFFER is the buffer containing output.
1265 Return collected error types as a string, or nil if there was
1266 none."
1267 (with-current-buffer buffer
1268 (save-excursion
1269 (goto-char (point-max))
1270 ;; Find final run
1271 nil )))
1274 (provide 'org-e-man)
1275 ;;; org-e-man.el ends here