org-e-groff, org-e-man, org-e-texinfo: Silence byte-compiler
[org-mode.git] / contrib / lisp / org-e-man.el
blob97ee6e89ad0cb966a3dcd8e499901d97226361b1
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."
210 ;;; Plain text
212 (defcustom org-e-man-quotes
213 '(("fr"
214 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
215 ("\\(\\S-\\)\"" . "~»")
216 ("\\(\\s-\\|(\\|^\\)'" . "'"))
217 ("en"
218 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
219 ("\\(\\S-\\)\"" . "''")
220 ("\\(\\s-\\|(\\|^\\)'" . "`")))
222 "Alist for quotes to use when converting english double-quotes.
224 The CAR of each item in this alist is the language code.
225 The CDR of each item in this alist is a list of three CONS:
226 - the first CONS defines the opening quote;
227 - the second CONS defines the closing quote;
228 - the last CONS defines single quotes.
230 For each item in a CONS, the first string is a regexp
231 for allowed characters before/after the quote, the second
232 string defines the replacement string for this quote."
233 :group 'org-export-e-man
234 :type '(list
235 (cons :tag "Opening quote"
236 (string :tag "Regexp for char before")
237 (string :tag "Replacement quote "))
238 (cons :tag "Closing quote"
239 (string :tag "Regexp for char after ")
240 (string :tag "Replacement quote "))
241 (cons :tag "Single quote"
242 (string :tag "Regexp for char before")
243 (string :tag "Replacement quote "))))
246 ;;; Compilation
248 (defcustom org-e-man-pdf-process
249 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
250 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
251 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
253 "Commands to process a Man file to a PDF file.
254 This is a list of strings, each of them will be given to the
255 shell as a command. %f in the command will be replaced by the
256 full file name, %b by the file base name \(i.e. without
257 extension) and %o by the base directory of the file.
260 By default, Org uses 3 runs of to do the processing.
262 Alternatively, this may be a Lisp function that does the
263 processing. This function should accept the file name as
264 its single argument."
265 :group 'org-export-pdf
266 :type '(choice
267 (repeat :tag "Shell command sequence"
268 (string :tag "Shell command"))
269 (const :tag "2 runs of pdfgroff"
270 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
271 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
272 (const :tag "3 runs of pdfgroff"
273 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
274 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
275 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
276 (function)))
278 (defcustom org-e-man-logfiles-extensions
279 '("log" "out" "toc")
280 "The list of file extensions to consider as Man logfiles."
281 :group 'org-export-e-man
282 :type '(repeat (string :tag "Extension")))
284 (defcustom org-e-man-remove-logfiles t
285 "Non-nil means remove the logfiles produced by PDF production.
286 These are the .aux, .log, .out, and .toc files."
287 :group 'org-export-e-man
288 :type 'boolean)
292 ;;; Internal Functions
295 (defun org-e-man--caption/label-string (element info)
296 "Return caption and label Man string for ELEMENT.
298 INFO is a plist holding contextual information. If there's no
299 caption nor label, return the empty string.
301 For non-floats, see `org-e-man--wrap-label'."
302 (let ((label (org-element-property :label element))
303 (main (org-export-get-caption element))
304 (short (org-export-get-caption element t)))
305 (cond ((and (not main) (not label)) "")
306 ((not main) (format "\\fI%s\\fP" label))
307 ;; Option caption format with short name.
308 (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
309 (org-export-data short info)
310 (org-export-data main info)))
311 ;; Standard caption format.
312 (t (format "\\fR%s\\fP" (org-export-data main info))))))
316 (defun org-e-man--quotation-marks (text info)
317 "Export quotation marks depending on language conventions.
318 TEXT is a string containing quotation marks to be replaced. INFO
319 is a plist used as a communication channel."
320 (mapc (lambda(l)
321 (let ((start 0))
322 (while (setq start (string-match (car l) text start))
323 (let ((new-quote (concat (match-string 1 text) (cdr l))))
324 (setq text (replace-match new-quote t t text))))))
325 (cdr (or (assoc (plist-get info :language) org-e-man-quotes)
326 ;; Falls back on English.
327 (assoc "en" org-e-man-quotes)))) text)
329 (defun org-e-man--wrap-label (element output)
330 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
331 This function shouldn't be used for floats. See
332 `org-e-man--caption/label-string'."
333 (let ((label (org-element-property :name element)))
334 (if (or (not output) (not label) (string= output "") (string= label ""))
335 output
336 (concat (format "%s\n.br\n" label) output))))
339 ;;; Template
341 (defun org-e-man-template (contents info)
342 "Return complete document string after Man conversion.
343 CONTENTS is the transcoded contents string. INFO is a plist
344 holding export options."
345 (let* ((title (org-export-data (plist-get info :title) info))
346 (attr (read (format "(%s)"
347 (mapconcat
348 #'identity
349 (list (plist-get info :man-class-options))
350 " "))))
351 (section-item (plist-get attr :section-id)))
353 (concat
355 (cond
356 ((and title (stringp section-item))
357 (format ".TH \"%s\" \"%s\" \n" title section-item))
358 ((and (string= "" title) (stringp section-item))
359 (format ".TH \"%s\" \"%s\" \n" " " section-item))
360 (title
361 (format ".TH \"%s\" \"1\" \n" title))
363 ".TH \" \" \"1\" "))
364 contents)))
369 ;;; Transcode Functions
371 ;;; Babel Call
373 ;; Babel Calls are ignored.
376 ;;; Bold
378 (defun org-e-man-bold (bold contents info)
379 "Transcode BOLD from Org to Man.
380 CONTENTS is the text with bold markup. INFO is a plist holding
381 contextual information."
382 (format "\\fB%s\\fP" contents))
385 ;;; Center Block
387 (defun org-e-man-center-block (center-block contents info)
388 "Transcode a CENTER-BLOCK element from Org to Man.
389 CONTENTS holds the contents of the center block. INFO is a plist
390 holding contextual information."
391 (org-e-man--wrap-label
392 center-block
393 (format ".ce %d\n.nf\n%s\n.fi"
394 (- (length (split-string contents "\n")) 1 )
395 contents)))
398 ;;; Clock
400 (defun org-e-man-clock (clock contents info)
401 "Transcode a CLOCK element from Org to Man.
402 CONTENTS is nil. INFO is a plist holding contextual
403 information."
404 "" )
407 ;;; Code
409 (defun org-e-man-code (code contents info)
410 "Transcode a CODE object from Org to Man.
411 CONTENTS is nil. INFO is a plist used as a communication
412 channel."
413 (format "\\fC%s\\fP" code))
416 ;;; Comment
418 ;; Comments are ignored.
421 ;;; Comment Block
423 ;; Comment Blocks are ignored.
426 ;;; Drawer
428 (defun org-e-man-drawer (drawer contents info)
429 "Transcode a DRAWER element from Org to Man.
430 DRAWER holds the drawer information
431 CONTENTS holds the contents of the block.
432 INFO is a plist holding contextual information. "
433 contents)
436 ;;; Dynamic Block
438 (defun org-e-man-dynamic-block (dynamic-block contents info)
439 "Transcode a DYNAMIC-BLOCK element from Org to Man.
440 CONTENTS holds the contents of the block. INFO is a plist
441 holding contextual information. See `org-export-data'."
442 (org-e-man--wrap-label dynamic-block contents))
445 ;;; Entity
447 (defun org-e-man-entity (entity contents info)
448 "Transcode an ENTITY object from Org to Man.
449 CONTENTS are the definition itself. INFO is a plist holding
450 contextual information."
451 (let ((ent (org-element-property :utf8 entity))) ent))
454 ;;; Example Block
456 (defun org-e-man-example-block (example-block contents info)
457 "Transcode an EXAMPLE-BLOCK element from Org to Man.
458 CONTENTS is nil. INFO is a plist holding contextual
459 information."
460 (org-e-man--wrap-label
461 example-block
462 (format ".RS\n.nf\n%s\n.fi\n.RE"
463 (org-export-format-code-default example-block info))))
464 ;;; Export Block
466 (defun org-e-man-export-block (export-block contents info)
467 "Transcode a EXPORT-BLOCK element from Org to Man.
468 CONTENTS is nil. INFO is a plist holding contextual information."
469 (when (string= (org-element-property :type export-block) "MAN")
470 (org-remove-indentation (org-element-property :value export-block))))
473 ;;; Export Snippet
475 (defun org-e-man-export-snippet (export-snippet contents info)
476 "Transcode a EXPORT-SNIPPET object from Org to Man.
477 CONTENTS is nil. INFO is a plist holding contextual information."
478 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
479 (org-element-property :value export-snippet)))
482 ;;; Fixed Width
484 (defun org-e-man-fixed-width (fixed-width contents info)
485 "Transcode a FIXED-WIDTH element from Org to Man.
486 CONTENTS is nil. INFO is a plist holding contextual information."
487 (org-e-man--wrap-label
488 fixed-width
489 (format "\\fC\n%s\\fP"
490 (org-remove-indentation
491 (org-element-property :value fixed-width)))))
494 ;;; Footnote Definition
496 ;; Footnote Definitions are ignored.
498 ;;; Footnote References
500 ;; Footnote References are Ignored
503 ;;; Headline
505 (defun org-e-man-headline (headline contents info)
506 "Transcode an HEADLINE element from Org to Man.
507 CONTENTS holds the contents of the headline. INFO is a plist
508 holding contextual information."
509 (let* ((level (org-export-get-relative-level headline info))
510 (numberedp (org-export-numbered-headline-p headline info))
511 ;; Section formatting will set two placeholders: one for the
512 ;; title and the other for the contents.
513 (section-fmt
514 (case level
515 (1 ".SH \"%s\"\n%s")
516 (2 ".SS \"%s\"\n%s")
517 (3 ".SS \"%s\"\n%s")
518 (t nil)))
519 (text (org-export-data (org-element-property :title headline) info)))
521 (cond
522 ;; Case 1: This is a footnote section: ignore it.
523 ((org-element-property :footnote-section-p headline) nil)
525 ;; Case 2. This is a deep sub-tree: export it as a list item.
526 ;; Also export as items headlines for which no section
527 ;; format has been found.
528 ((or (not section-fmt) (org-export-low-level-p headline info))
529 ;; Build the real contents of the sub-tree.
530 (let ((low-level-body
531 (concat
532 ;; If the headline is the first sibling, start a list.
533 (when (org-export-first-sibling-p headline info)
534 (format "%s\n" ".RS"))
535 ;; Itemize headline
536 ".TP\n.ft I\n" text "\n.ft\n"
537 contents ".RE")))
538 ;; If headline is not the last sibling simply return
539 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
540 ;; blank line.
541 (if (not (org-export-last-sibling-p headline info)) low-level-body
542 (replace-regexp-in-string
543 "[ \t\n]*\\'" ""
544 low-level-body))))
546 ;; Case 3. Standard headline. Export it as a section.
547 (t (format section-fmt text contents )))))
549 ;;; Horizontal Rule
550 ;; Not supported
552 ;;; Inline Babel Call
554 ;; Inline Babel Calls are ignored.
556 ;;; Inline Src Block
558 (defun org-e-man-inline-src-block (inline-src-block contents info)
559 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
560 CONTENTS holds the contents of the item. INFO is a plist holding
561 contextual information."
562 (let* ((code (org-element-property :value inline-src-block)))
563 (cond
564 (org-e-man-source-highlight
565 (let* ((tmpdir (if (featurep 'xemacs)
566 temp-directory
567 temporary-file-directory ))
568 (in-file (make-temp-name
569 (expand-file-name "srchilite" tmpdir)))
570 (out-file (make-temp-name
571 (expand-file-name "reshilite" tmpdir)))
572 (org-lang (org-element-property :language inline-src-block))
573 (lst-lang (cadr (assq (intern org-lang)
574 org-e-man-source-highlight-langs)))
576 (cmd (concat (expand-file-name "source-highlight")
577 " -s " lst-lang
578 " -f groff_man"
579 " -i " in-file
580 " -o " out-file )))
582 (if lst-lang
583 (let ((code-block "" ))
584 (with-temp-file in-file (insert code))
585 (shell-command cmd)
586 (setq code-block (org-file-contents out-file))
587 (delete-file in-file)
588 (delete-file out-file)
589 code-block)
590 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
591 code))))
593 ;; Do not use a special package: transcode it verbatim.
595 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
596 "\\fP\n.fi\n.RE\n")))))
599 ;;; Inlinetask
600 ;;; Italic
602 (defun org-e-man-italic (italic contents info)
603 "Transcode ITALIC from Org to Man.
604 CONTENTS is the text with italic markup. INFO is a plist holding
605 contextual information."
606 (format "\\fI%s\\fP" contents))
609 ;;; Item
612 (defun org-e-man-item (item contents info)
614 "Transcode an ITEM element from Org to Man.
615 CONTENTS holds the contents of the item. INFO is a plist holding
616 contextual information."
618 (let* ((bullet (org-element-property :bullet item))
619 (type (org-element-property :type (org-element-property :parent item)))
620 (checkbox (case (org-element-property :checkbox item)
621 (on "\\o'\\(sq\\(mu'") ;;
622 (off "\\(sq ") ;;
623 (trans "\\o'\\(sq\\(mi'" ))) ;;
625 (tag (let ((tag (org-element-property :tag item)))
626 ;; Check-boxes must belong to the tag.
627 (and tag (format "\\fB%s\\fP"
628 (concat checkbox
629 (org-export-data tag info)))))))
631 (if (and (null tag )
632 (null checkbox))
633 (let* ((bullet (org-trim bullet))
634 (marker (cond ((string= "-" bullet) "\\(em")
635 ((string= "*" bullet) "\\(bu")
636 ((eq type 'ordered)
637 (format "%s " (org-trim bullet)))
638 (t "\\(dg"))))
639 (concat ".IP " marker " 4\n"
640 (org-trim (or contents " " ))))
641 ; else
642 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
643 (org-trim (or contents " " ))))))
645 ;;; Keyword
648 (defun org-e-man-keyword (keyword contents info)
649 "Transcode a KEYWORD element from Org to Man.
650 CONTENTS is nil. INFO is a plist holding contextual information."
651 (let ((key (org-element-property :key keyword))
652 (value (org-element-property :value keyword)))
653 (cond
654 ((string= key "MAN") value)
655 ((string= key "INDEX") nil)
656 ;; Invisible targets.
657 ((string= key "TARGET") nil)
658 ((string= key "TOC" ) nil))))
661 ;;; Man Environment
663 (defun org-e-man-man-environment (man-environment contents info)
664 "Transcode a MAN-ENVIRONMENT element from Org to Man.
665 CONTENTS is nil. INFO is a plist holding contextual information."
666 (let ((label (org-element-property :name man-environment))
667 (value (org-remove-indentation
668 (org-element-property :value man-environment))))
669 (if (not (org-string-nw-p label)) value
670 ;; Environment is labelled: label must be within the environment
671 ;; (otherwise, a reference pointing to that element will count
672 ;; the section instead).
673 (with-temp-buffer
674 (insert value)
675 (goto-char (point-min))
676 (forward-line)
677 (insert (format "%s\n" label))
678 (buffer-string)))))
681 ;;; Man Fragment
683 (defun org-e-man-man-fragment (man-fragment contents info)
684 "Transcode a MAN-FRAGMENT object from Org to Man.
685 CONTENTS is nil. INFO is a plist holding contextual information."
686 (org-element-property :value man-fragment))
689 ;;; Line Break
691 (defun org-e-man-line-break (line-break contents info)
692 "Transcode a LINE-BREAK object from Org to Man.
693 CONTENTS is nil. INFO is a plist holding contextual information."
694 ".br\n")
697 ;;; Link
700 (defun org-e-man-link (link desc info)
701 "Transcode a LINK object from Org to Man.
703 DESC is the description part of the link, or the empty string.
704 INFO is a plist holding contextual information. See
705 `org-export-data'."
707 (let* ((type (org-element-property :type link))
708 (raw-path (org-element-property :path link))
709 ;; Ensure DESC really exists, or set it to nil.
710 (desc (and (not (string= desc "")) desc))
712 (path (cond
713 ((member type '("http" "https" "ftp" "mailto"))
714 (concat type ":" raw-path))
715 ((string= type "file")
716 (when (string-match "\\(.+\\)::.+" raw-path)
717 (setq raw-path (match-string 1 raw-path)))
718 (if (file-name-absolute-p raw-path)
719 (concat "file://" (expand-file-name raw-path))
720 (concat "file://" raw-path)))
721 (t raw-path)))
722 protocol)
723 (cond
724 ;; External link with a description part.
725 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
726 ;; External link without a description part.
727 (path (format "\\fI%s\\fP" path))
728 ;; No path, only description. Try to do something useful.
729 (t (format "\\fI%s\\fP" desc)))))
732 ;;; Paragraph
734 (defun org-e-man-paragraph (paragraph contents info)
735 "Transcode a PARAGRAPH element from Org to Man.
736 CONTENTS is the contents of the paragraph, as a string. INFO is
737 the plist used as a communication channel."
738 (let ((parent (plist-get (nth 1 paragraph) :parent)))
739 (when parent
740 (let ((parent-type (car parent))
741 (fixed-paragraph ""))
742 (cond ((and (eq parent-type 'item)
743 (plist-get (nth 1 parent) :bullet ))
744 (setq fixed-paragraph (concat "" contents)))
745 ((eq parent-type 'section)
746 (setq fixed-paragraph (concat ".PP\n" contents)))
747 ((eq parent-type 'footnote-definition)
748 (setq fixed-paragraph contents))
749 (t (setq fixed-paragraph (concat "" contents))))
750 fixed-paragraph ))))
753 ;;; Plain List
755 (defun org-e-man-plain-list (plain-list contents info)
756 "Transcode a PLAIN-LIST element from Org to Man.
757 CONTENTS is the contents of the list. INFO is a plist holding
758 contextual information."
759 contents)
761 ;;; Plain Text
763 (defun org-e-man-plain-text (text info)
764 "Transcode a TEXT string from Org to Man.
765 TEXT is the string to transcode. INFO is a plist holding
766 contextual information."
767 ;; Protect
768 (setq text (replace-regexp-in-string
769 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
770 "$\\" text nil t 1))
772 ;; Handle quotation marks
773 (setq text (org-e-man--quotation-marks text info))
775 ;; Handle break preservation if required.
777 (when (plist-get info :preserve-breaks)
778 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
779 text)))
780 ;; Return value.
781 text)
785 ;;; Planning
788 ;;; Property Drawer
791 ;;; Quote Block
793 (defun org-e-man-quote-block (quote-block contents info)
794 "Transcode a QUOTE-BLOCK element from Org to Man.
795 CONTENTS holds the contents of the block. INFO is a plist
796 holding contextual information."
797 (org-e-man--wrap-label
798 quote-block
799 (format ".RS\n%s\n.RE" contents)))
801 ;;; Quote Section
803 (defun org-e-man-quote-section (quote-section contents info)
804 "Transcode a QUOTE-SECTION element from Org to Man.
805 CONTENTS is nil. INFO is a plist holding contextual information."
806 (let ((value (org-remove-indentation
807 (org-element-property :value quote-section))))
808 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
811 ;;; Radio Target
813 (defun org-e-man-radio-target (radio-target text info)
814 "Transcode a RADIO-TARGET object from Org to Man.
815 TEXT is the text of the target. INFO is a plist holding
816 contextual information."
817 text )
820 ;;; Section
822 (defun org-e-man-section (section contents info)
823 "Transcode a SECTION element from Org to Man.
824 CONTENTS holds the contents of the section. INFO is a plist
825 holding contextual information."
826 contents)
829 ;;; Special Block
831 (defun org-e-man-special-block (special-block contents info)
832 "Transcode a SPECIAL-BLOCK element from Org to Man.
833 CONTENTS holds the contents of the block. INFO is a plist
834 holding contextual information."
835 (let ((type (downcase (org-element-property :type special-block))))
836 (org-e-man--wrap-label
837 special-block
838 (format "%s\n" contents))))
841 ;;; Src Block
843 (defun org-e-man-src-block (src-block contents info)
844 "Transcode a SRC-BLOCK element from Org to Man.
845 CONTENTS holds the contents of the item. INFO is a plist holding
846 contextual information."
847 (let* ((lang (org-element-property :language src-block))
848 (code (org-element-property :value src-block))
849 (custom-env (and lang
850 (cadr (assq (intern lang)
851 org-e-man-custom-lang-environments))))
852 (num-start (case (org-element-property :number-lines src-block)
853 (continued (org-export-get-loc src-block info))
854 (new 0)))
855 (retain-labels (org-element-property :retain-labels src-block)))
856 (cond
857 ;; Case 1. No source fontification.
858 ((not org-e-man-source-highlight)
859 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
860 (org-export-format-code-default src-block info)))
861 (org-e-man-source-highlight
862 (let* ((tmpdir (if (featurep 'xemacs)
863 temp-directory
864 temporary-file-directory ))
866 (in-file (make-temp-name
867 (expand-file-name "srchilite" tmpdir)))
868 (out-file (make-temp-name
869 (expand-file-name "reshilite" tmpdir)))
871 (org-lang (org-element-property :language src-block))
872 (lst-lang (cadr (assq (intern org-lang)
873 org-e-man-source-highlight-langs)))
875 (cmd (concat "source-highlight"
876 " -s " lst-lang
877 " -f groff_man "
878 " -i " in-file
879 " -o " out-file)))
881 (if lst-lang
882 (let ((code-block ""))
883 (with-temp-file in-file (insert code))
884 (shell-command cmd)
885 (setq code-block (org-file-contents out-file))
886 (delete-file in-file)
887 (delete-file out-file)
888 code-block)
889 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))))
892 ;;; Statistics Cookie
894 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
895 "Transcode a STATISTICS-COOKIE object from Org to Man.
896 CONTENTS is nil. INFO is a plist holding contextual information."
897 (org-element-property :value statistics-cookie))
900 ;;; Strike-Through
902 (defun org-e-man-strike-through (strike-through contents info)
903 "Transcode STRIKE-THROUGH from Org to Man.
904 CONTENTS is the text with strike-through markup. INFO is a plist
905 holding contextual information."
906 (format "\\fI%s\\fP" contents))
908 ;;; Subscript
910 (defun org-e-man-subscript (subscript contents info)
911 "Transcode a SUBSCRIPT object from Org to Man.
912 CONTENTS is the contents of the object. INFO is a plist holding
913 contextual information."
914 (format "\\d\\s-2%s\\s+2\\u" contents))
916 ;;; Superscript "^_%s$
918 (defun org-e-man-superscript (superscript contents info)
919 "Transcode a SUPERSCRIPT object from Org to Man.
920 CONTENTS is the contents of the object. INFO is a plist holding
921 contextual information."
922 (format "\\u\\s-2%s\\s+2\\d" contents))
925 ;;; Table
927 ;; `org-e-man-table' is the entry point for table transcoding. It
928 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
929 ;; delegates the job to either `org-e-man-table--table.el-table' or
930 ;; `org-e-man-table--org-table' functions, depending of the type of
931 ;; the table.
933 ;; `org-e-man-table--align-string' is a subroutine used to build
934 ;; alignment string for Org tables.
936 (defun org-e-man-table (table contents info)
937 "Transcode a TABLE element from Org to Man.
938 CONTENTS is the contents of the table. INFO is a plist holding
939 contextual information."
940 (cond
941 ;; Case 1: verbatim table.
942 ((or org-e-man-tables-verbatim
943 (let ((attr (read (format "(%s)"
944 (mapconcat
945 #'identity
946 (org-element-property :attr_man table)
947 " ")))))
949 (and attr (plist-get attr :verbatim))))
951 (format ".nf\n\\fC%s\\fP\n.fi"
952 ;; Re-create table, without affiliated keywords.
953 (org-trim
954 (org-element-interpret-data
955 `(table nil ,@(org-element-contents table))))))
956 ;; Case 2: Standard table.
957 (t (org-e-man-table--org-table table contents info))))
959 (defun org-e-man-table--align-string (divider table info)
960 "Return an appropriate Man alignment string.
961 TABLE is the considered table. INFO is a plist used as
962 a communication channel."
963 (let (alignment)
964 ;; Extract column groups and alignment from first (non-rule)
965 ;; row.
966 (org-element-map
967 (org-element-map
968 table 'table-row
969 (lambda (row)
970 (and (eq (org-element-property :type row) 'standard) row))
971 info 'first-match)
972 'table-cell
973 (lambda (cell)
974 (let* ((borders (org-export-table-cell-borders cell info))
975 (raw-width (org-export-table-cell-width cell info))
976 (width-cm (when raw-width (/ raw-width 5)))
977 (width (if raw-width (format "w(%dc)"
978 (if (< width-cm 1) 1 width-cm)) "")))
979 ;; Check left border for the first cell only.
980 (when (and (memq 'left borders) (not alignment))
981 (push "|" alignment))
982 (push
983 (case (org-export-table-cell-alignment cell info)
984 (left (concat "l" width divider))
985 (right (concat "r" width divider))
986 (center (concat "c" width divider)))
987 alignment)
988 (when (memq 'right borders) (push "|" alignment))))
989 info)
990 (apply 'concat (reverse alignment))))
992 (defun org-e-man-table--org-table (table contents info)
993 "Return appropriate Man code for an Org table.
995 TABLE is the table type element to transcode. CONTENTS is its
996 contents, as a string. INFO is a plist used as a communication
997 channel.
999 This function assumes TABLE has `org' as its `:type' attribute."
1000 (let* ((attr (org-export-read-attribute :attr_man table))
1001 (label (org-element-property :name table))
1002 (caption (and (not (plist-get attr :disable-caption))
1003 (org-e-man--caption/label-string table info)))
1004 (divider (if (plist-get attr :divider) "|" " "))
1006 ;; Determine alignment string.
1007 (alignment (org-e-man-table--align-string divider table info))
1008 ;; Extract others display options.
1010 (lines (org-split-string contents "\n"))
1012 (attr-list
1013 (let ((result-list '()))
1014 (dolist (attr-item
1015 (list
1016 (if (plist-get attr :expand)
1017 "expand" nil)
1019 (case (plist-get attr :placement)
1020 ('center "center")
1021 ('left nil)
1023 (if org-e-man-tables-centered
1024 "center" "")))
1026 (case (plist-get attr :boxtype)
1027 ('box "box")
1028 ('doublebox "doublebox")
1029 ('allbox "allbox")
1030 ('none nil)
1031 (t "box"))))
1033 (if attr-item
1034 (add-to-list 'result-list attr-item)))
1035 result-list ))
1038 (title-line (plist-get attr :title-line))
1039 (long-cells (plist-get attr :long-cells))
1041 (table-format (concat
1042 (format "%s" (or (car attr-list) "" ))
1044 (let ((output-list '()))
1045 (when (cdr attr-list)
1046 (dolist (attr-item (cdr attr-list))
1047 (setq output-list (concat output-list (format ",%s" attr-item)))))
1048 output-list)
1049 "")))
1051 (first-line (when lines (org-split-string (car lines) "\t"))))
1052 ;; Prepare the final format string for the table.
1055 (cond
1056 ;; Others.
1057 (lines (concat ".TS\n " table-format ";\n"
1059 (format "%s.\n"
1060 (let ((final-line ""))
1061 (when title-line
1062 (dotimes (i (length first-line))
1063 (setq final-line (concat final-line "cb" divider))))
1065 (setq final-line (concat final-line "\n"))
1067 (if alignment
1068 (setq final-line (concat final-line alignment))
1069 (dotimes (i (length first-line))
1070 (setq final-line (concat final-line "c" divider))))
1071 final-line ))
1073 (format "%s.TE\n"
1074 (let ((final-line "")
1075 (long-line "")
1076 (lines (org-split-string contents "\n")))
1078 (dolist (line-item lines)
1079 (setq long-line "")
1081 (if long-cells
1082 (progn
1083 (if (string= line-item "_")
1084 (setq long-line (format "%s\n" line-item))
1085 ;; else string =
1086 (let ((cell-item-list (org-split-string line-item "\t")))
1087 (dolist (cell-item cell-item-list)
1089 (cond ((eq cell-item (car (last cell-item-list)))
1090 (setq long-line (concat long-line
1091 (format "T{\n%s\nT}\t\n" cell-item ))))
1093 (setq long-line (concat long-line
1094 (format "T{\n%s\nT}\t" cell-item ))))))
1095 long-line))
1096 ;; else long cells
1097 (setq final-line (concat final-line long-line )))
1099 (setq final-line (concat final-line line-item "\n"))))
1100 final-line))
1102 (and caption (format ".TB \"%s\"" caption)))))))
1104 ;;; Table Cell
1106 (defun org-e-man-table-cell (table-cell contents info)
1107 "Transcode a TABLE-CELL element from Org to Man
1108 CONTENTS is the cell contents. INFO is a plist used as
1109 a communication channel."
1110 (concat (if (and contents
1111 org-e-man-table-scientific-notation
1112 (string-match orgtbl-exp-regexp contents))
1113 ;; Use appropriate format string for scientific
1114 ;; notation.
1115 (format org-e-man-table-scientific-notation
1116 (match-string 1 contents)
1117 (match-string 2 contents))
1118 contents )
1119 (when (org-export-get-next-element table-cell info) "\t")))
1122 ;;; Table Row
1124 (defun org-e-man-table-row (table-row contents info)
1125 "Transcode a TABLE-ROW element from Org to Man
1126 CONTENTS is the contents of the row. INFO is a plist used as
1127 a communication channel."
1128 ;; Rules are ignored since table separators are deduced from
1129 ;; borders of the current row.
1130 (when (eq (org-element-property :type table-row) 'standard)
1131 (let* ((attr (mapconcat 'identity
1132 (org-element-property
1133 :attr_man (org-export-get-parent table-row))
1134 " "))
1135 ;; TABLE-ROW's borders are extracted from its first cell.
1136 (borders
1137 (org-export-table-cell-borders
1138 (car (org-element-contents table-row)) info)))
1139 (concat
1140 ;; Mark horizontal lines
1141 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1142 contents
1144 (cond
1145 ;; When BOOKTABS are activated enforce bottom rule even when
1146 ;; no hline was specifically marked.
1147 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1148 ((memq 'below borders) "\n_"))))))
1151 ;;; Target
1153 (defun org-e-man-target (target contents info)
1154 "Transcode a TARGET object from Org to Man.
1155 CONTENTS is nil. INFO is a plist holding contextual
1156 information."
1157 (format "\\fI%s\\fP"
1158 (org-export-solidify-link-text (org-element-property :value target))))
1161 ;;; Timestamp
1163 (defun org-e-man-timestamp (timestamp contents info)
1164 "Transcode a TIMESTAMP object from Org to Man.
1165 CONTENTS is nil. INFO is a plist holding contextual
1166 information."
1167 "" )
1170 ;;; Underline
1172 (defun org-e-man-underline (underline contents info)
1173 "Transcode UNDERLINE from Org to Man.
1174 CONTENTS is the text with underline markup. INFO is a plist
1175 holding contextual information."
1176 (format "\\fI%s\\fP" contents))
1179 ;;; Verbatim
1181 (defun org-e-man-verbatim (verbatim contents info)
1182 "Transcode a VERBATIM object from Org to Man.
1183 CONTENTS is nil. INFO is a plist used as a communication
1184 channel."
1185 (format ".nf\n%s\n.fi" contents))
1188 ;;; Verse Block
1190 (defun org-e-man-verse-block (verse-block contents info)
1191 "Transcode a VERSE-BLOCK element from Org to Man.
1192 CONTENTS is verse block contents. INFO is a plist holding
1193 contextual information."
1194 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1198 ;;; Interactive functions
1200 (defun org-e-man-export-to-man
1201 (&optional subtreep visible-only body-only ext-plist pub-dir)
1202 "Export current buffer to a Man file.
1204 If narrowing is active in the current buffer, only export its
1205 narrowed part.
1207 If a region is active, export that region.
1209 When optional argument SUBTREEP is non-nil, export the sub-tree
1210 at point, extracting information from the headline properties
1211 first.
1213 When optional argument VISIBLE-ONLY is non-nil, don't export
1214 contents of hidden elements.
1216 When optional argument BODY-ONLY is non-nil, only the body
1217 without any markers.
1219 EXT-PLIST, when provided, is a property list with external
1220 parameters overriding Org default settings, but still inferior to
1221 file-local settings.
1223 When optional argument PUB-DIR is set, use it as the publishing
1224 directory.
1226 Return output file's name."
1227 (interactive)
1228 (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
1229 (org-export-to-file
1230 'e-man outfile subtreep visible-only body-only ext-plist)))
1232 (defun org-e-man-export-to-pdf
1233 (&optional subtreep visible-only body-only ext-plist pub-dir)
1234 "Export current buffer to Groff then process through to PDF.
1236 If narrowing is active in the current buffer, only export its
1237 narrowed part.
1239 If a region is active, export that region.
1241 When optional argument SUBTREEP is non-nil, export the sub-tree
1242 at point, extracting information from the headline properties
1243 first.
1245 When optional argument VISIBLE-ONLY is non-nil, don't export
1246 contents of hidden elements.
1248 When optional argument BODY-ONLY is non-nil, only write between
1249 markers.
1251 EXT-PLIST, when provided, is a property list with external
1252 parameters overriding Org default settings, but still inferior to
1253 file-local settings.
1255 When optional argument PUB-DIR is set, use it as the publishing
1256 directory.
1258 Return PDF file's name."
1259 (interactive)
1260 (org-e-man-compile
1261 (org-e-man-export-to-man
1262 subtreep visible-only body-only ext-plist pub-dir)))
1264 (defun org-e-man-compile (grofffile)
1265 "Compile a Groff file.
1267 GROFFFILE is the name of the file being compiled. Processing is
1268 done through the command specified in `org-e-man-pdf-process'.
1270 Return PDF file name or an error if it couldn't be produced."
1271 (let* ((wconfig (current-window-configuration))
1272 (grofffile (file-truename grofffile))
1273 (base (file-name-sans-extension grofffile))
1274 errors)
1275 (message (format "Processing Groff file %s ..." grofffile))
1276 (unwind-protect
1277 (progn
1278 (cond
1279 ;; A function is provided: Apply it.
1280 ((functionp org-e-man-pdf-process)
1281 (funcall org-e-man-pdf-process (shell-quote-argument grofffile)))
1282 ;; A list is provided: Replace %b, %f and %o with appropriate
1283 ;; values in each command before applying it. Output is
1284 ;; redirected to "*Org PDF Groff Output*" buffer.
1285 ((consp org-e-man-pdf-process)
1286 (let* ((out-dir (or (file-name-directory grofffile) "./"))
1287 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
1288 (mapc
1289 (lambda (command)
1290 (shell-command
1291 (replace-regexp-in-string
1292 "%b" (shell-quote-argument base)
1293 (replace-regexp-in-string
1294 "%f" (shell-quote-argument grofffile)
1295 (replace-regexp-in-string
1296 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1297 outbuf))
1298 org-e-man-pdf-process)
1299 ;; Collect standard errors from output buffer.
1300 (setq errors (org-e-man-collect-errors outbuf))))
1301 (t (error "No valid command to process to PDF")))
1302 (let ((pdffile (concat base ".pdf")))
1303 ;; Check for process failure. Provide collected errors if
1304 ;; possible.
1305 (if (not (file-exists-p pdffile))
1306 (error (concat (format "PDF file %s wasn't produced" pdffile)
1307 (when errors (concat ": " errors))))
1308 ;; Else remove log files, when specified, and signal end of
1309 ;; process to user, along with any error encountered.
1310 (when org-e-man-remove-logfiles
1311 (dolist (ext org-e-man-logfiles-extensions)
1312 (let ((file (concat base "." ext)))
1313 (when (file-exists-p file) (delete-file file)))))
1314 (message (concat "Process completed"
1315 (if (not errors) "."
1316 (concat " with errors: " errors)))))
1317 ;; Return output file name.
1318 pdffile))
1319 (set-window-configuration wconfig))))
1321 (defun org-e-man-collect-errors (buffer)
1322 "Collect some kind of errors from \"groff\" output
1323 BUFFER is the buffer containing output.
1324 Return collected error types as a string, or nil if there was
1325 none."
1326 (with-current-buffer buffer
1327 (save-excursion
1328 (goto-char (point-max))
1329 ;; Find final run
1330 nil )))
1333 (provide 'org-e-man)
1334 ;;; org-e-man.el ends here