org-export: Fix inclusion of files with no final newline
[org-mode.git] / contrib / lisp / org-e-man.el
blobf8e7f5bb723f5d3c639f7f730bafca4f512ad844
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 ((:date "DATE" nil nil t)
114 (:man-class "MAN_CLASS" nil nil t)
115 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
116 (:man-header-extra "MAN_HEADER" nil nil newline)))
120 ;;; User Configurable Variables
122 (defgroup org-export-e-man nil
123 "Options for exporting Org mode files to Man."
124 :tag "Org Export Man"
125 :group 'org-export)
127 ;;; Tables
129 (defcustom org-e-man-tables-centered t
130 "When non-nil, tables are exported in a center environment."
131 :group 'org-export-e-man
132 :type 'boolean)
134 (defcustom org-e-man-tables-verbatim nil
135 "When non-nil, tables are exported verbatim."
136 :group 'org-export-e-man
137 :type 'boolean)
140 (defcustom org-e-man-table-scientific-notation "%sE%s"
141 "Format string to display numbers in scientific notation.
142 The format should have \"%s\" twice, for mantissa and exponent
143 \(i.e. \"%s\\\\times10^{%s}\").
145 When nil, no transformation is made."
146 :group 'org-export-e-man
147 :type '(choice
148 (string :tag "Format string")
149 (const :tag "No formatting")))
152 ;;; Inlinetasks
153 ;; Src blocks
155 (defcustom org-e-man-source-highlight nil
156 "Use GNU source highlight to embellish source blocks "
157 :group 'org-export-e-man
158 :type 'boolean)
161 (defcustom org-e-man-source-highlight-langs
162 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
163 (scheme "scheme")
164 (c "c") (cc "cpp") (csharp "csharp") (d "d")
165 (fortran "fortran") (cobol "cobol") (pascal "pascal")
166 (ada "ada") (asm "asm")
167 (perl "perl") (cperl "perl")
168 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
169 (java "java") (javascript "javascript")
170 (tex "latex")
171 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
172 (ocaml "caml") (caml "caml")
173 (sql "sql") (sqlite "sql")
174 (html "html") (css "css") (xml "xml")
175 (bat "bat") (bison "bison") (clipper "clipper")
176 (ldap "ldap") (opa "opa")
177 (php "php") (postscript "postscript") (prolog "prolog")
178 (properties "properties") (makefile "makefile")
179 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
180 "Alist mapping languages to their listing language counterpart.
181 The key is a symbol, the major mode symbol without the \"-mode\".
182 The value is the string that should be inserted as the language
183 parameter for the listings package. If the mode name and the
184 listings name are the same, the language does not need an entry
185 in this list - but it does not hurt if it is present."
186 :group 'org-export-e-man
187 :type '(repeat
188 (list
189 (symbol :tag "Major mode ")
190 (string :tag "Listings language"))))
194 (defvar org-e-man-custom-lang-environments nil
195 "Alist mapping languages to language-specific Man environments.
197 It is used during export of src blocks by the listings and
198 man packages. For example,
200 \(setq org-e-man-custom-lang-environments
201 '\(\(python \"pythoncode\"\)\)\)
203 would have the effect that if org encounters begin_src python
204 during man export."
208 ;;; Compilation
210 (defcustom org-e-man-pdf-process
211 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
212 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
213 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
215 "Commands to process a Man file to a PDF file.
216 This is a list of strings, each of them will be given to the
217 shell as a command. %f in the command will be replaced by the
218 full file name, %b by the file base name (i.e. without directory
219 and extension parts) and %o by the base directory of the file.
222 By default, Org uses 3 runs of to do the processing.
224 Alternatively, this may be a Lisp function that does the
225 processing. This function should accept the file name as
226 its single argument."
227 :group 'org-export-pdf
228 :type '(choice
229 (repeat :tag "Shell command sequence"
230 (string :tag "Shell command"))
231 (const :tag "2 runs of pdfgroff"
232 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
233 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
234 (const :tag "3 runs of pdfgroff"
235 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
236 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
237 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
238 (function)))
240 (defcustom org-e-man-logfiles-extensions
241 '("log" "out" "toc")
242 "The list of file extensions to consider as Man logfiles."
243 :group 'org-export-e-man
244 :type '(repeat (string :tag "Extension")))
246 (defcustom org-e-man-remove-logfiles t
247 "Non-nil means remove the logfiles produced by PDF production.
248 These are the .aux, .log, .out, and .toc files."
249 :group 'org-export-e-man
250 :type 'boolean)
254 ;;; Internal Functions
256 (defun org-e-man--caption/label-string (element info)
257 "Return caption and label Man string for ELEMENT.
259 INFO is a plist holding contextual information. If there's no
260 caption nor label, return the empty string.
262 For non-floats, see `org-e-man--wrap-label'."
263 (let ((label (org-element-property :label element))
264 (main (org-export-get-caption element))
265 (short (org-export-get-caption element t)))
266 (cond ((and (not main) (not label)) "")
267 ((not main) (format "\\fI%s\\fP" label))
268 ;; Option caption format with short name.
269 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
270 (org-export-data short info)
271 (org-export-data main info)))
272 ;; Standard caption format.
273 (t (format "\\fR%s\\fP" (org-export-data main info))))))
275 (defun org-e-man--wrap-label (element output)
276 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
277 This function shouldn't be used for floats. See
278 `org-e-man--caption/label-string'."
279 (let ((label (org-element-property :name element)))
280 (if (or (not output) (not label) (string= output "") (string= label ""))
281 output
282 (concat (format "%s\n.br\n" label) output))))
286 ;;; Template
288 (defun org-e-man-template (contents info)
289 "Return complete document string after Man conversion.
290 CONTENTS is the transcoded contents string. INFO is a plist
291 holding export options."
292 (let* ((title (org-export-data (plist-get info :title) info))
293 (attr (read (format "(%s)"
294 (mapconcat
295 #'identity
296 (list (plist-get info :man-class-options))
297 " "))))
298 (section-item (plist-get attr :section-id)))
300 (concat
302 (cond
303 ((and title (stringp section-item))
304 (format ".TH \"%s\" \"%s\" \n" title section-item))
305 ((and (string= "" title) (stringp section-item))
306 (format ".TH \"%s\" \"%s\" \n" " " section-item))
307 (title
308 (format ".TH \"%s\" \"1\" \n" title))
310 ".TH \" \" \"1\" "))
311 contents)))
316 ;;; Transcode Functions
318 ;;; Babel Call
320 ;; Babel Calls are ignored.
323 ;;; Bold
325 (defun org-e-man-bold (bold contents info)
326 "Transcode BOLD from Org to Man.
327 CONTENTS is the text with bold markup. INFO is a plist holding
328 contextual information."
329 (format "\\fB%s\\fP" contents))
332 ;;; Center Block
334 (defun org-e-man-center-block (center-block contents info)
335 "Transcode a CENTER-BLOCK element from Org to Man.
336 CONTENTS holds the contents of the center block. INFO is a plist
337 holding contextual information."
338 (org-e-man--wrap-label
339 center-block
340 (format ".ce %d\n.nf\n%s\n.fi"
341 (- (length (split-string contents "\n")) 1 )
342 contents)))
345 ;;; Clock
347 (defun org-e-man-clock (clock contents info)
348 "Transcode a CLOCK element from Org to Man.
349 CONTENTS is nil. INFO is a plist holding contextual
350 information."
351 "" )
354 ;;; Code
356 (defun org-e-man-code (code contents info)
357 "Transcode a CODE object from Org to Man.
358 CONTENTS is nil. INFO is a plist used as a communication
359 channel."
360 (format "\\fC%s\\fP" code))
363 ;;; Comment
365 ;; Comments are ignored.
368 ;;; Comment Block
370 ;; Comment Blocks are ignored.
373 ;;; Drawer
375 (defun org-e-man-drawer (drawer contents info)
376 "Transcode a DRAWER element from Org to Man.
377 DRAWER holds the drawer information
378 CONTENTS holds the contents of the block.
379 INFO is a plist holding contextual information. "
380 contents)
383 ;;; Dynamic Block
385 (defun org-e-man-dynamic-block (dynamic-block contents info)
386 "Transcode a DYNAMIC-BLOCK element from Org to Man.
387 CONTENTS holds the contents of the block. INFO is a plist
388 holding contextual information. See `org-export-data'."
389 (org-e-man--wrap-label dynamic-block contents))
392 ;;; Entity
394 (defun org-e-man-entity (entity contents info)
395 "Transcode an ENTITY object from Org to Man.
396 CONTENTS are the definition itself. INFO is a plist holding
397 contextual information."
398 (org-element-property :utf-8 entity))
401 ;;; Example Block
403 (defun org-e-man-example-block (example-block contents info)
404 "Transcode an EXAMPLE-BLOCK element from Org to Man.
405 CONTENTS is nil. INFO is a plist holding contextual
406 information."
407 (org-e-man--wrap-label
408 example-block
409 (format ".RS\n.nf\n%s\n.fi\n.RE"
410 (org-export-format-code-default example-block info))))
413 ;;; Export Block
415 (defun org-e-man-export-block (export-block contents info)
416 "Transcode a EXPORT-BLOCK element from Org to Man.
417 CONTENTS is nil. INFO is a plist holding contextual information."
418 (when (string= (org-element-property :type export-block) "MAN")
419 (org-remove-indentation (org-element-property :value export-block))))
422 ;;; Export Snippet
424 (defun org-e-man-export-snippet (export-snippet contents info)
425 "Transcode a EXPORT-SNIPPET object from Org to Man.
426 CONTENTS is nil. INFO is a plist holding contextual information."
427 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
428 (org-element-property :value export-snippet)))
431 ;;; Fixed Width
433 (defun org-e-man-fixed-width (fixed-width contents info)
434 "Transcode a FIXED-WIDTH element from Org to Man.
435 CONTENTS is nil. INFO is a plist holding contextual information."
436 (org-e-man--wrap-label
437 fixed-width
438 (format "\\fC\n%s\\fP"
439 (org-remove-indentation
440 (org-element-property :value fixed-width)))))
443 ;;; Footnote Definition
445 ;; Footnote Definitions are ignored.
447 ;;; Footnote References
449 ;; Footnote References are Ignored
452 ;;; Headline
454 (defun org-e-man-headline (headline contents info)
455 "Transcode an HEADLINE element from Org to Man.
456 CONTENTS holds the contents of the headline. INFO is a plist
457 holding contextual information."
458 (let* ((level (org-export-get-relative-level headline info))
459 (numberedp (org-export-numbered-headline-p headline info))
460 ;; Section formatting will set two placeholders: one for the
461 ;; title and the other for the contents.
462 (section-fmt
463 (case level
464 (1 ".SH \"%s\"\n%s")
465 (2 ".SS \"%s\"\n%s")
466 (3 ".SS \"%s\"\n%s")
467 (t nil)))
468 (text (org-export-data (org-element-property :title headline) info)))
470 (cond
471 ;; Case 1: This is a footnote section: ignore it.
472 ((org-element-property :footnote-section-p headline) nil)
474 ;; Case 2. This is a deep sub-tree: export it as a list item.
475 ;; Also export as items headlines for which no section
476 ;; format has been found.
477 ((or (not section-fmt) (org-export-low-level-p headline info))
478 ;; Build the real contents of the sub-tree.
479 (let ((low-level-body
480 (concat
481 ;; If the headline is the first sibling, start a list.
482 (when (org-export-first-sibling-p headline info)
483 (format "%s\n" ".RS"))
484 ;; Itemize headline
485 ".TP\n.ft I\n" text "\n.ft\n"
486 contents ".RE")))
487 ;; If headline is not the last sibling simply return
488 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
489 ;; blank line.
490 (if (not (org-export-last-sibling-p headline info)) low-level-body
491 (replace-regexp-in-string
492 "[ \t\n]*\\'" ""
493 low-level-body))))
495 ;; Case 3. Standard headline. Export it as a section.
496 (t (format section-fmt text contents )))))
498 ;;; Horizontal Rule
499 ;; Not supported
501 ;;; Inline Babel Call
503 ;; Inline Babel Calls are ignored.
505 ;;; Inline Src Block
507 (defun org-e-man-inline-src-block (inline-src-block contents info)
508 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
509 CONTENTS holds the contents of the item. INFO is a plist holding
510 contextual information."
511 (let* ((code (org-element-property :value inline-src-block)))
512 (cond
513 (org-e-man-source-highlight
514 (let* ((tmpdir (if (featurep 'xemacs)
515 temp-directory
516 temporary-file-directory ))
517 (in-file (make-temp-name
518 (expand-file-name "srchilite" tmpdir)))
519 (out-file (make-temp-name
520 (expand-file-name "reshilite" tmpdir)))
521 (org-lang (org-element-property :language inline-src-block))
522 (lst-lang (cadr (assq (intern org-lang)
523 org-e-man-source-highlight-langs)))
525 (cmd (concat (expand-file-name "source-highlight")
526 " -s " lst-lang
527 " -f groff_man"
528 " -i " in-file
529 " -o " out-file )))
531 (if lst-lang
532 (let ((code-block "" ))
533 (with-temp-file in-file (insert code))
534 (shell-command cmd)
535 (setq code-block (org-file-contents out-file))
536 (delete-file in-file)
537 (delete-file out-file)
538 code-block)
539 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
540 code))))
542 ;; Do not use a special package: transcode it verbatim.
544 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
545 "\\fP\n.fi\n.RE\n")))))
548 ;;; Inlinetask
549 ;;; Italic
551 (defun org-e-man-italic (italic contents info)
552 "Transcode ITALIC from Org to Man.
553 CONTENTS is the text with italic markup. INFO is a plist holding
554 contextual information."
555 (format "\\fI%s\\fP" contents))
558 ;;; Item
561 (defun org-e-man-item (item contents info)
563 "Transcode an ITEM element from Org to Man.
564 CONTENTS holds the contents of the item. INFO is a plist holding
565 contextual information."
567 (let* ((bullet (org-element-property :bullet item))
568 (type (org-element-property :type (org-element-property :parent item)))
569 (checkbox (case (org-element-property :checkbox item)
570 (on "\\o'\\(sq\\(mu'") ;;
571 (off "\\(sq ") ;;
572 (trans "\\o'\\(sq\\(mi'" ))) ;;
574 (tag (let ((tag (org-element-property :tag item)))
575 ;; Check-boxes must belong to the tag.
576 (and tag (format "\\fB%s\\fP"
577 (concat checkbox
578 (org-export-data tag info)))))))
580 (if (and (null tag )
581 (null checkbox))
582 (let* ((bullet (org-trim bullet))
583 (marker (cond ((string= "-" bullet) "\\(em")
584 ((string= "*" bullet) "\\(bu")
585 ((eq type 'ordered)
586 (format "%s " (org-trim bullet)))
587 (t "\\(dg"))))
588 (concat ".IP " marker " 4\n"
589 (org-trim (or contents " " ))))
590 ; else
591 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
592 (org-trim (or contents " " ))))))
594 ;;; Keyword
597 (defun org-e-man-keyword (keyword contents info)
598 "Transcode a KEYWORD element from Org to Man.
599 CONTENTS is nil. INFO is a plist holding contextual information."
600 (let ((key (org-element-property :key keyword))
601 (value (org-element-property :value keyword)))
602 (cond
603 ((string= key "MAN") value)
604 ((string= key "INDEX") nil)
605 ;; Invisible targets.
606 ((string= key "TARGET") nil)
607 ((string= key "TOC" ) nil))))
610 ;;; Man Environment
612 (defun org-e-man-man-environment (man-environment contents info)
613 "Transcode a MAN-ENVIRONMENT element from Org to Man.
614 CONTENTS is nil. INFO is a plist holding contextual information."
615 (let ((label (org-element-property :name man-environment))
616 (value (org-remove-indentation
617 (org-element-property :value man-environment))))
618 (if (not (org-string-nw-p label)) value
619 ;; Environment is labelled: label must be within the environment
620 ;; (otherwise, a reference pointing to that element will count
621 ;; the section instead).
622 (with-temp-buffer
623 (insert value)
624 (goto-char (point-min))
625 (forward-line)
626 (insert (format "%s\n" label))
627 (buffer-string)))))
630 ;;; Man Fragment
632 (defun org-e-man-man-fragment (man-fragment contents info)
633 "Transcode a MAN-FRAGMENT object from Org to Man.
634 CONTENTS is nil. INFO is a plist holding contextual information."
635 (org-element-property :value man-fragment))
638 ;;; Line Break
640 (defun org-e-man-line-break (line-break contents info)
641 "Transcode a LINE-BREAK object from Org to Man.
642 CONTENTS is nil. INFO is a plist holding contextual information."
643 ".br\n")
646 ;;; Link
649 (defun org-e-man-link (link desc info)
650 "Transcode a LINK object from Org to Man.
652 DESC is the description part of the link, or the empty string.
653 INFO is a plist holding contextual information. See
654 `org-export-data'."
656 (let* ((type (org-element-property :type link))
657 (raw-path (org-element-property :path link))
658 ;; Ensure DESC really exists, or set it to nil.
659 (desc (and (not (string= desc "")) desc))
661 (path (cond
662 ((member type '("http" "https" "ftp" "mailto"))
663 (concat type ":" raw-path))
664 ((string= type "file")
665 (when (string-match "\\(.+\\)::.+" raw-path)
666 (setq raw-path (match-string 1 raw-path)))
667 (if (file-name-absolute-p raw-path)
668 (concat "file://" (expand-file-name raw-path))
669 (concat "file://" raw-path)))
670 (t raw-path)))
671 protocol)
672 (cond
673 ;; External link with a description part.
674 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
675 ;; External link without a description part.
676 (path (format "\\fI%s\\fP" path))
677 ;; No path, only description. Try to do something useful.
678 (t (format "\\fI%s\\fP" desc)))))
681 ;;; Paragraph
683 (defun org-e-man-paragraph (paragraph contents info)
684 "Transcode a PARAGRAPH element from Org to Man.
685 CONTENTS is the contents of the paragraph, as a string. INFO is
686 the plist used as a communication channel."
687 (let ((parent (plist-get (nth 1 paragraph) :parent)))
688 (when parent
689 (let ((parent-type (car parent))
690 (fixed-paragraph ""))
691 (cond ((and (eq parent-type 'item)
692 (plist-get (nth 1 parent) :bullet ))
693 (setq fixed-paragraph (concat "" contents)))
694 ((eq parent-type 'section)
695 (setq fixed-paragraph (concat ".PP\n" contents)))
696 ((eq parent-type 'footnote-definition)
697 (setq fixed-paragraph contents))
698 (t (setq fixed-paragraph (concat "" contents))))
699 fixed-paragraph ))))
702 ;;; Plain List
704 (defun org-e-man-plain-list (plain-list contents info)
705 "Transcode a PLAIN-LIST element from Org to Man.
706 CONTENTS is the contents of the list. INFO is a plist holding
707 contextual information."
708 contents)
710 ;;; Plain Text
712 (defun org-e-man-plain-text (text info)
713 "Transcode a TEXT string from Org to Man.
714 TEXT is the string to transcode. INFO is a plist holding
715 contextual information."
716 (let ((output text))
717 ;; Protect various chars.
718 (setq output (replace-regexp-in-string
719 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
720 "$\\" output nil t 1))
721 ;; Activate smart quotes. Be sure to provide original TEXT string
722 ;; since OUTPUT may have been modified.
723 (when (plist-get info :with-smart-quotes)
724 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
725 ;; Handle break preservation if required.
726 (when (plist-get info :preserve-breaks)
727 (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
728 output)))
729 ;; Return value.
730 output))
734 ;;; Planning
737 ;;; Property Drawer
740 ;;; Quote Block
742 (defun org-e-man-quote-block (quote-block contents info)
743 "Transcode a QUOTE-BLOCK element from Org to Man.
744 CONTENTS holds the contents of the block. INFO is a plist
745 holding contextual information."
746 (org-e-man--wrap-label
747 quote-block
748 (format ".RS\n%s\n.RE" contents)))
750 ;;; Quote Section
752 (defun org-e-man-quote-section (quote-section contents info)
753 "Transcode a QUOTE-SECTION element from Org to Man.
754 CONTENTS is nil. INFO is a plist holding contextual information."
755 (let ((value (org-remove-indentation
756 (org-element-property :value quote-section))))
757 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
760 ;;; Radio Target
762 (defun org-e-man-radio-target (radio-target text info)
763 "Transcode a RADIO-TARGET object from Org to Man.
764 TEXT is the text of the target. INFO is a plist holding
765 contextual information."
766 text )
769 ;;; Section
771 (defun org-e-man-section (section contents info)
772 "Transcode a SECTION element from Org to Man.
773 CONTENTS holds the contents of the section. INFO is a plist
774 holding contextual information."
775 contents)
778 ;;; Special Block
780 (defun org-e-man-special-block (special-block contents info)
781 "Transcode a SPECIAL-BLOCK element from Org to Man.
782 CONTENTS holds the contents of the block. INFO is a plist
783 holding contextual information."
784 (let ((type (downcase (org-element-property :type special-block))))
785 (org-e-man--wrap-label
786 special-block
787 (format "%s\n" contents))))
790 ;;; Src Block
792 (defun org-e-man-src-block (src-block contents info)
793 "Transcode a SRC-BLOCK element from Org to Man.
794 CONTENTS holds the contents of the item. INFO is a plist holding
795 contextual information."
796 (let* ((lang (org-element-property :language src-block))
797 (code (org-element-property :value src-block))
798 (custom-env (and lang
799 (cadr (assq (intern lang)
800 org-e-man-custom-lang-environments))))
801 (num-start (case (org-element-property :number-lines src-block)
802 (continued (org-export-get-loc src-block info))
803 (new 0)))
804 (retain-labels (org-element-property :retain-labels src-block)))
805 (cond
806 ;; Case 1. No source fontification.
807 ((not org-e-man-source-highlight)
808 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
809 (org-export-format-code-default src-block info)))
810 (org-e-man-source-highlight
811 (let* ((tmpdir (if (featurep 'xemacs)
812 temp-directory
813 temporary-file-directory ))
815 (in-file (make-temp-name
816 (expand-file-name "srchilite" tmpdir)))
817 (out-file (make-temp-name
818 (expand-file-name "reshilite" tmpdir)))
820 (org-lang (org-element-property :language src-block))
821 (lst-lang (cadr (assq (intern org-lang)
822 org-e-man-source-highlight-langs)))
824 (cmd (concat "source-highlight"
825 " -s " lst-lang
826 " -f groff_man "
827 " -i " in-file
828 " -o " out-file)))
830 (if lst-lang
831 (let ((code-block ""))
832 (with-temp-file in-file (insert code))
833 (shell-command cmd)
834 (setq code-block (org-file-contents out-file))
835 (delete-file in-file)
836 (delete-file out-file)
837 code-block)
838 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
841 ;;; Statistics Cookie
843 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
844 "Transcode a STATISTICS-COOKIE object from Org to Man.
845 CONTENTS is nil. INFO is a plist holding contextual information."
846 (org-element-property :value statistics-cookie))
849 ;;; Strike-Through
851 (defun org-e-man-strike-through (strike-through contents info)
852 "Transcode STRIKE-THROUGH from Org to Man.
853 CONTENTS is the text with strike-through markup. INFO is a plist
854 holding contextual information."
855 (format "\\fI%s\\fP" contents))
857 ;;; Subscript
859 (defun org-e-man-subscript (subscript contents info)
860 "Transcode a SUBSCRIPT object from Org to Man.
861 CONTENTS is the contents of the object. INFO is a plist holding
862 contextual information."
863 (format "\\d\\s-2%s\\s+2\\u" contents))
865 ;;; Superscript "^_%s$
867 (defun org-e-man-superscript (superscript contents info)
868 "Transcode a SUPERSCRIPT object from Org to Man.
869 CONTENTS is the contents of the object. INFO is a plist holding
870 contextual information."
871 (format "\\u\\s-2%s\\s+2\\d" contents))
874 ;;; Table
876 ;; `org-e-man-table' is the entry point for table transcoding. It
877 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
878 ;; delegates the job to either `org-e-man-table--table.el-table' or
879 ;; `org-e-man-table--org-table' functions, depending of the type of
880 ;; the table.
882 ;; `org-e-man-table--align-string' is a subroutine used to build
883 ;; alignment string for Org tables.
885 (defun org-e-man-table (table contents info)
886 "Transcode a TABLE element from Org to Man.
887 CONTENTS is the contents of the table. INFO is a plist holding
888 contextual information."
889 (cond
890 ;; Case 1: verbatim table.
891 ((or org-e-man-tables-verbatim
892 (let ((attr (read (format "(%s)"
893 (mapconcat
894 #'identity
895 (org-element-property :attr_man table)
896 " ")))))
898 (and attr (plist-get attr :verbatim))))
900 (format ".nf\n\\fC%s\\fP\n.fi"
901 ;; Re-create table, without affiliated keywords.
902 (org-trim
903 (org-element-interpret-data
904 `(table nil ,@(org-element-contents table))))))
905 ;; Case 2: Standard table.
906 (t (org-e-man-table--org-table table contents info))))
908 (defun org-e-man-table--align-string (divider table info)
909 "Return an appropriate Man alignment string.
910 TABLE is the considered table. INFO is a plist used as
911 a communication channel."
912 (let (alignment)
913 ;; Extract column groups and alignment from first (non-rule)
914 ;; row.
915 (org-element-map
916 (org-element-map
917 table 'table-row
918 (lambda (row)
919 (and (eq (org-element-property :type row) 'standard) row))
920 info 'first-match)
921 'table-cell
922 (lambda (cell)
923 (let* ((borders (org-export-table-cell-borders cell info))
924 (raw-width (org-export-table-cell-width cell info))
925 (width-cm (when raw-width (/ raw-width 5)))
926 (width (if raw-width (format "w(%dc)"
927 (if (< width-cm 1) 1 width-cm)) "")))
928 ;; Check left border for the first cell only.
929 (when (and (memq 'left borders) (not alignment))
930 (push "|" alignment))
931 (push
932 (case (org-export-table-cell-alignment cell info)
933 (left (concat "l" width divider))
934 (right (concat "r" width divider))
935 (center (concat "c" width divider)))
936 alignment)
937 (when (memq 'right borders) (push "|" alignment))))
938 info)
939 (apply 'concat (reverse alignment))))
941 (defun org-e-man-table--org-table (table contents info)
942 "Return appropriate Man code for an Org table.
944 TABLE is the table type element to transcode. CONTENTS is its
945 contents, as a string. INFO is a plist used as a communication
946 channel.
948 This function assumes TABLE has `org' as its `:type' attribute."
949 (let* ((attr (org-export-read-attribute :attr_man table))
950 (label (org-element-property :name table))
951 (caption (and (not (plist-get attr :disable-caption))
952 (org-e-man--caption/label-string table info)))
953 (divider (if (plist-get attr :divider) "|" " "))
955 ;; Determine alignment string.
956 (alignment (org-e-man-table--align-string divider table info))
957 ;; Extract others display options.
959 (lines (org-split-string contents "\n"))
961 (attr-list
962 (let ((result-list '()))
963 (dolist (attr-item
964 (list
965 (if (plist-get attr :expand)
966 "expand" nil)
968 (case (plist-get attr :placement)
969 ('center "center")
970 ('left nil)
972 (if org-e-man-tables-centered
973 "center" "")))
975 (case (plist-get attr :boxtype)
976 ('box "box")
977 ('doublebox "doublebox")
978 ('allbox "allbox")
979 ('none nil)
980 (t "box"))))
982 (if attr-item
983 (add-to-list 'result-list attr-item)))
984 result-list ))
987 (title-line (plist-get attr :title-line))
988 (long-cells (plist-get attr :long-cells))
990 (table-format (concat
991 (format "%s" (or (car attr-list) "" ))
993 (let ((output-list '()))
994 (when (cdr attr-list)
995 (dolist (attr-item (cdr attr-list))
996 (setq output-list (concat output-list (format ",%s" attr-item)))))
997 output-list)
998 "")))
1000 (first-line (when lines (org-split-string (car lines) "\t"))))
1001 ;; Prepare the final format string for the table.
1004 (cond
1005 ;; Others.
1006 (lines (concat ".TS\n " table-format ";\n"
1008 (format "%s.\n"
1009 (let ((final-line ""))
1010 (when title-line
1011 (dotimes (i (length first-line))
1012 (setq final-line (concat final-line "cb" divider))))
1014 (setq final-line (concat final-line "\n"))
1016 (if alignment
1017 (setq final-line (concat final-line alignment))
1018 (dotimes (i (length first-line))
1019 (setq final-line (concat final-line "c" divider))))
1020 final-line ))
1022 (format "%s.TE\n"
1023 (let ((final-line "")
1024 (long-line "")
1025 (lines (org-split-string contents "\n")))
1027 (dolist (line-item lines)
1028 (setq long-line "")
1030 (if long-cells
1031 (progn
1032 (if (string= line-item "_")
1033 (setq long-line (format "%s\n" line-item))
1034 ;; else string =
1035 (let ((cell-item-list (org-split-string line-item "\t")))
1036 (dolist (cell-item cell-item-list)
1038 (cond ((eq cell-item (car (last cell-item-list)))
1039 (setq long-line (concat long-line
1040 (format "T{\n%s\nT}\t\n" cell-item ))))
1042 (setq long-line (concat long-line
1043 (format "T{\n%s\nT}\t" cell-item ))))))
1044 long-line))
1045 ;; else long cells
1046 (setq final-line (concat final-line long-line )))
1048 (setq final-line (concat final-line line-item "\n"))))
1049 final-line))
1051 (and caption (format ".TB \"%s\"" caption)))))))
1053 ;;; Table Cell
1055 (defun org-e-man-table-cell (table-cell contents info)
1056 "Transcode a TABLE-CELL element from Org to Man
1057 CONTENTS is the cell contents. INFO is a plist used as
1058 a communication channel."
1059 (concat (if (and contents
1060 org-e-man-table-scientific-notation
1061 (string-match orgtbl-exp-regexp contents))
1062 ;; Use appropriate format string for scientific
1063 ;; notation.
1064 (format org-e-man-table-scientific-notation
1065 (match-string 1 contents)
1066 (match-string 2 contents))
1067 contents )
1068 (when (org-export-get-next-element table-cell info) "\t")))
1071 ;;; Table Row
1073 (defun org-e-man-table-row (table-row contents info)
1074 "Transcode a TABLE-ROW element from Org to Man
1075 CONTENTS is the contents of the row. INFO is a plist used as
1076 a communication channel."
1077 ;; Rules are ignored since table separators are deduced from
1078 ;; borders of the current row.
1079 (when (eq (org-element-property :type table-row) 'standard)
1080 (let* ((attr (mapconcat 'identity
1081 (org-element-property
1082 :attr_man (org-export-get-parent table-row))
1083 " "))
1084 ;; TABLE-ROW's borders are extracted from its first cell.
1085 (borders
1086 (org-export-table-cell-borders
1087 (car (org-element-contents table-row)) info)))
1088 (concat
1089 ;; Mark horizontal lines
1090 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1091 contents
1093 (cond
1094 ;; When BOOKTABS are activated enforce bottom rule even when
1095 ;; no hline was specifically marked.
1096 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1097 ((memq 'below borders) "\n_"))))))
1100 ;;; Target
1102 (defun org-e-man-target (target contents info)
1103 "Transcode a TARGET object from Org to Man.
1104 CONTENTS is nil. INFO is a plist holding contextual
1105 information."
1106 (format "\\fI%s\\fP"
1107 (org-export-solidify-link-text (org-element-property :value target))))
1110 ;;; Timestamp
1112 (defun org-e-man-timestamp (timestamp contents info)
1113 "Transcode a TIMESTAMP object from Org to Man.
1114 CONTENTS is nil. INFO is a plist holding contextual
1115 information."
1116 "" )
1119 ;;; Underline
1121 (defun org-e-man-underline (underline contents info)
1122 "Transcode UNDERLINE from Org to Man.
1123 CONTENTS is the text with underline markup. INFO is a plist
1124 holding contextual information."
1125 (format "\\fI%s\\fP" contents))
1128 ;;; Verbatim
1130 (defun org-e-man-verbatim (verbatim contents info)
1131 "Transcode a VERBATIM object from Org to Man.
1132 CONTENTS is nil. INFO is a plist used as a communication
1133 channel."
1134 (format ".nf\n%s\n.fi" contents))
1137 ;;; Verse Block
1139 (defun org-e-man-verse-block (verse-block contents info)
1140 "Transcode a VERSE-BLOCK element from Org to Man.
1141 CONTENTS is verse block contents. INFO is a plist holding
1142 contextual information."
1143 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1147 ;;; Interactive functions
1149 (defun org-e-man-export-to-man
1150 (&optional subtreep visible-only body-only ext-plist pub-dir)
1151 "Export current buffer to a Man file.
1153 If narrowing is active in the current buffer, only export its
1154 narrowed part.
1156 If a region is active, export that region.
1158 When optional argument SUBTREEP is non-nil, export the sub-tree
1159 at point, extracting information from the headline properties
1160 first.
1162 When optional argument VISIBLE-ONLY is non-nil, don't export
1163 contents of hidden elements.
1165 When optional argument BODY-ONLY is non-nil, only the body
1166 without any markers.
1168 EXT-PLIST, when provided, is a property list with external
1169 parameters overriding Org default settings, but still inferior to
1170 file-local settings.
1172 When optional argument PUB-DIR is set, use it as the publishing
1173 directory.
1175 Return output file's name."
1176 (interactive)
1177 (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
1178 (org-export-to-file
1179 'e-man outfile subtreep visible-only body-only ext-plist)))
1181 (defun org-e-man-export-to-pdf
1182 (&optional subtreep visible-only body-only ext-plist pub-dir)
1183 "Export current buffer to Groff then process through to PDF.
1185 If narrowing is active in the current buffer, only export its
1186 narrowed part.
1188 If a region is active, export that region.
1190 When optional argument SUBTREEP is non-nil, export the sub-tree
1191 at point, extracting information from the headline properties
1192 first.
1194 When optional argument VISIBLE-ONLY is non-nil, don't export
1195 contents of hidden elements.
1197 When optional argument BODY-ONLY is non-nil, only write between
1198 markers.
1200 EXT-PLIST, when provided, is a property list with external
1201 parameters overriding Org default settings, but still inferior to
1202 file-local settings.
1204 When optional argument PUB-DIR is set, use it as the publishing
1205 directory.
1207 Return PDF file's name."
1208 (interactive)
1209 (org-e-man-compile
1210 (org-e-man-export-to-man
1211 subtreep visible-only body-only ext-plist pub-dir)))
1213 (defun org-e-man-compile (file)
1214 "Compile a Groff file.
1216 FILE is the name of the file being compiled. Processing is done
1217 through the command specified in `org-e-man-pdf-process'.
1219 Return PDF file name or an error if it couldn't be produced."
1220 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1221 (full-name (file-truename file))
1222 (out-dir (file-name-directory file))
1223 ;; Make sure `default-directory' is set to FILE directory,
1224 ;; not to whatever value the current buffer may have.
1225 (default-directory (file-name-directory full-name))
1226 errors)
1227 (message (format "Processing Groff file %s ..." file))
1228 (save-window-excursion
1229 (cond
1230 ;; A function is provided: Apply it.
1231 ((functionp org-e-man-pdf-process)
1232 (funcall org-e-man-pdf-process (shell-quote-argument file)))
1233 ;; A list is provided: Replace %b, %f and %o with appropriate
1234 ;; values in each command before applying it. Output is
1235 ;; redirected to "*Org PDF Groff Output*" buffer.
1236 ((consp org-e-man-pdf-process)
1237 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1238 (mapc
1239 (lambda (command)
1240 (shell-command
1241 (replace-regexp-in-string
1242 "%b" (shell-quote-argument base-name)
1243 (replace-regexp-in-string
1244 "%f" (shell-quote-argument full-name)
1245 (replace-regexp-in-string
1246 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1247 outbuf))
1248 org-e-man-pdf-process)
1249 ;; Collect standard errors from output buffer.
1250 (setq errors (org-e-man-collect-errors outbuf))))
1251 (t (error "No valid command to process to PDF")))
1252 (let ((pdffile (concat out-dir base-name ".pdf")))
1253 ;; Check for process failure. Provide collected errors if
1254 ;; possible.
1255 (if (not (file-exists-p pdffile))
1256 (error (concat (format "PDF file %s wasn't produced" pdffile)
1257 (when errors (concat ": " errors))))
1258 ;; Else remove log files, when specified, and signal end of
1259 ;; process to user, along with any error encountered.
1260 (when org-e-man-remove-logfiles
1261 (dolist (ext org-e-man-logfiles-extensions)
1262 (let ((file (concat out-dir base-name "." ext)))
1263 (when (file-exists-p file) (delete-file file)))))
1264 (message (concat "Process completed"
1265 (if (not errors) "."
1266 (concat " with errors: " errors)))))
1267 ;; Return output file name.
1268 pdffile))))
1270 (defun org-e-man-collect-errors (buffer)
1271 "Collect some kind of errors from \"groff\" output
1272 BUFFER is the buffer containing output.
1273 Return collected error types as a string, or nil if there was
1274 none."
1275 (with-current-buffer buffer
1276 (save-excursion
1277 (goto-char (point-max))
1278 ;; Find final run
1279 nil )))
1282 (provide 'org-e-man)
1283 ;;; org-e-man.el ends here