contrib/lisp/org-e-texinfo: Remove markup from headlines when
[org-mode.git] / contrib / lisp / org-e-man.el
blob981f831d727d77f19650694afe503636773b70df
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)
52 ;;;; Define Back-End
54 (defvar org-e-man-translate-alist
55 '((babel-call . org-e-man-babel-call)
56 (bold . org-e-man-bold)
57 (center-block . org-e-man-center-block)
58 (clock . org-e-man-clock)
59 (code . org-e-man-code)
60 (comment . org-e-man-comment)
61 (comment-block . org-e-man-comment-block)
62 (drawer . org-e-man-drawer)
63 (dynamic-block . org-e-man-dynamic-block)
64 (entity . org-e-man-entity)
65 (example-block . org-e-man-example-block)
66 (export-block . org-e-man-export-block)
67 (export-snippet . org-e-man-export-snippet)
68 (fixed-width . org-e-man-fixed-width)
69 (footnote-definition . org-e-man-footnote-definition)
70 (footnote-reference . org-e-man-footnote-reference)
71 (headline . org-e-man-headline)
72 (horizontal-rule . org-e-man-horizontal-rule)
73 (inline-babel-call . org-e-man-inline-babel-call)
74 (inline-src-block . org-e-man-inline-src-block)
75 (inlinetask . org-e-man-inlinetask)
76 (italic . org-e-man-italic)
77 (item . org-e-man-item)
78 (keyword . org-e-man-keyword)
79 (man-environment . org-e-man-man-environment)
80 (man-fragment . org-e-man-man-fragment)
81 (line-break . org-e-man-line-break)
82 (link . org-e-man-link)
83 (macro . org-e-man-macro)
84 (paragraph . org-e-man-paragraph)
85 (plain-list . org-e-man-plain-list)
86 (plain-text . org-e-man-plain-text)
87 (planning . org-e-man-planning)
88 (property-drawer . org-e-man-property-drawer)
89 (quote-block . org-e-man-quote-block)
90 (quote-section . org-e-man-quote-section)
91 (radio-target . org-e-man-radio-target)
92 (section . org-e-man-section)
93 (special-block . org-e-man-special-block)
94 (src-block . org-e-man-src-block)
95 (statistics-cookie . org-e-man-statistics-cookie)
96 (strike-through . org-e-man-strike-through)
97 (subscript . org-e-man-subscript)
98 (superscript . org-e-man-superscript)
99 (table . org-e-man-table)
100 (table-cell . org-e-man-table-cell)
101 (table-row . org-e-man-table-row)
102 (target . org-e-man-target)
103 (template . org-e-man-template)
104 (timestamp . org-e-man-timestamp)
105 (underline . org-e-man-underline)
106 (verbatim . org-e-man-verbatim)
107 (verse-block . org-e-man-verse-block))
108 "Alist between element or object types and translators.")
110 (defconst org-e-man-options-alist
111 '((:date "DATE" nil nil t)
112 (:man-class "MAN_CLASS" nil nil t)
113 (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
114 (:man-header-extra "MAN_HEADER" nil nil newline))
115 "Alist between Man export properties and ways to set them.
116 See `org-export-options-alist' for more information on the
117 structure of the values.")
122 ;;; User Configurable Variables
125 (defgroup org-export-e-man nil
126 "Options for exporting Org mode files to Man."
127 :tag "Org Export Man"
128 :group 'org-export)
131 ;;;; Tables
134 (defcustom org-e-man-tables-centered t
135 "When non-nil, tables are exported in a center environment."
136 :group 'org-export-e-man
137 :type 'boolean)
139 (defcustom org-e-man-tables-verbatim nil
140 "When non-nil, tables are exported verbatim."
141 :group 'org-export-e-man
142 :type 'boolean)
144 (defcustom org-e-man-table-scientific-notation "%sE%s"
145 "Format string to display numbers in scientific notation.
146 The format should have \"%s\" twice, for mantissa and exponent
147 \(i.e. \"%s\\\\times10^{%s}\").
149 When nil, no transformation is made."
150 :group 'org-export-e-man
151 :type '(choice
152 (string :tag "Format string")
153 (const :tag "No formatting")))
156 ;;;; Inlinetasks
159 ;; Src blocks
161 (defcustom org-e-man-source-highlight nil
162 "Use GNU source highlight to embellish source blocks "
163 :group 'org-export-e-man
164 :type 'boolean)
166 (defcustom org-e-man-source-highlight-langs
167 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
168 (scheme "scheme")
169 (c "c") (cc "cpp") (csharp "csharp") (d "d")
170 (fortran "fortran") (cobol "cobol") (pascal "pascal")
171 (ada "ada") (asm "asm")
172 (perl "perl") (cperl "perl")
173 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
174 (java "java") (javascript "javascript")
175 (tex "latex")
176 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
177 (ocaml "caml") (caml "caml")
178 (sql "sql") (sqlite "sql")
179 (html "html") (css "css") (xml "xml")
180 (bat "bat") (bison "bison") (clipper "clipper")
181 (ldap "ldap") (opa "opa")
182 (php "php") (postscript "postscript") (prolog "prolog")
183 (properties "properties") (makefile "makefile")
184 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
185 "Alist mapping languages to their listing language counterpart.
186 The key is a symbol, the major mode symbol without the \"-mode\".
187 The value is the string that should be inserted as the language
188 parameter for the listings package. If the mode name and the
189 listings name are the same, the language does not need an entry
190 in this list - but it does not hurt if it is present."
191 :group 'org-export-e-man
192 :type '(repeat
193 (list
194 (symbol :tag "Major mode ")
195 (string :tag "Listings language"))))
198 (defvar org-e-man-custom-lang-environments nil
199 "Alist mapping languages to language-specific Man environments.
201 It is used during export of src blocks by the listings and
202 man packages. For example,
204 \(setq org-e-man-custom-lang-environments
205 '\(\(python \"pythoncode\"\)\)\)
207 would have the effect that if org encounters begin_src python
208 during man export."
212 ;;;; Plain text
214 (defcustom org-e-man-quotes
215 '(("fr"
216 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
217 ("\\(\\S-\\)\"" . "~»")
218 ("\\(\\s-\\|(\\|^\\)'" . "'"))
219 ("en"
220 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
221 ("\\(\\S-\\)\"" . "''")
222 ("\\(\\s-\\|(\\|^\\)'" . "`")))
224 "Alist for quotes to use when converting english double-quotes.
226 The CAR of each item in this alist is the language code.
227 The CDR of each item in this alist is a list of three CONS:
228 - the first CONS defines the opening quote;
229 - the second CONS defines the closing quote;
230 - the last CONS defines single quotes.
232 For each item in a CONS, the first string is a regexp
233 for allowed characters before/after the quote, the second
234 string defines the replacement string for this quote."
235 :group 'org-export-e-man
236 :type '(list
237 (cons :tag "Opening quote"
238 (string :tag "Regexp for char before")
239 (string :tag "Replacement quote "))
240 (cons :tag "Closing quote"
241 (string :tag "Regexp for char after ")
242 (string :tag "Replacement quote "))
243 (cons :tag "Single quote"
244 (string :tag "Regexp for char before")
245 (string :tag "Replacement quote "))))
248 ;;;; Compilation
250 (defcustom org-e-man-pdf-process
251 '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
252 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
253 "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
255 "Commands to process a Man file to a PDF file.
256 This is a list of strings, each of them will be given to the
257 shell as a command. %f in the command will be replaced by the
258 full file name, %b by the file base name \(i.e. without
259 extension) and %o by the base directory of the file.
262 By default, Org uses 3 runs of to do the processing.
264 Alternatively, this may be a Lisp function that does the
265 processing. This function should accept the file name as
266 its single argument."
267 :group 'org-export-pdf
268 :type '(choice
269 (repeat :tag "Shell command sequence"
270 (string :tag "Shell command"))
271 (const :tag "2 runs of pdfgroff"
272 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
273 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
274 (const :tag "3 runs of pdfgroff"
275 ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
276 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
277 "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
278 (function)))
280 (defcustom org-e-man-logfiles-extensions
281 '("log" "out" "toc")
282 "The list of file extensions to consider as Man logfiles."
283 :group 'org-export-e-man
284 :type '(repeat (string :tag "Extension")))
286 (defcustom org-e-man-remove-logfiles t
287 "Non-nil means remove the logfiles produced by PDF production.
288 These are the .aux, .log, .out, and .toc files."
289 :group 'org-export-e-man
290 :type 'boolean)
294 ;; Preamble
297 ;; Adding MAN as a block parser to make sure that its contents
298 ;; does not execute
300 (add-to-list 'org-element-block-name-alist
301 '("MAN" . org-element-export-block-parser))
307 ;;; Internal Functions
309 (defun org-e-man--caption/label-string (caption label info)
310 "Return caption and label Man string for floats.
312 CAPTION is a cons cell of secondary strings, the car being the
313 standard caption and the cdr its short form. LABEL is a string
314 representing the label. INFO is a plist holding contextual
315 information.
317 If there's no caption nor label, return the empty string.
319 For non-floats, see `org-e-man--wrap-label'."
320 (let ((label-str ""))
321 (cond
322 ((and (not caption) (not label)) "")
323 ((not caption) (format "\\fI%s\\fP" label))
324 ;; Option caption format with short name.
325 ((cdr caption)
326 (format "\\fR%s\\fP - \\fI%s\\P - %s\n"
327 (org-export-data (cdr caption) info)
328 label-str
329 (org-export-data (car caption) info)))
330 ;; Standard caption format.
331 (t (format "\\fR%s\\fP"
332 (org-export-data (car caption) info))))))
334 (defun org-e-man--quotation-marks (text info)
335 "Export quotation marks depending on language conventions.
336 TEXT is a string containing quotation marks to be replaced. INFO
337 is a plist used as a communication channel."
338 (mapc (lambda(l)
339 (let ((start 0))
340 (while (setq start (string-match (car l) text start))
341 (let ((new-quote (concat (match-string 1 text) (cdr l))))
342 (setq text (replace-match new-quote t t text))))))
343 (cdr (or (assoc (plist-get info :language) org-e-man-quotes)
344 ;; Falls back on English.
345 (assoc "en" org-e-man-quotes))))
346 text)
348 (defun org-e-man--wrap-label (element output)
349 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
350 This function shouldn't be used for floats. See
351 `org-e-man--caption/label-string'."
352 (let ((label (org-element-property :name element)))
353 (if (or (not output) (not label) (string= output "") (string= label ""))
354 output
355 (concat (format "%s\n.br\n" label) output))))
360 ;;; Template
362 (defun org-e-man-template (contents info)
363 "Return complete document string after Man conversion.
364 CONTENTS is the transcoded contents string. INFO is a plist
365 holding export options."
366 (let* ((title (org-export-data (plist-get info :title) info))
367 (attr
368 (read (format "(%s)"
369 (mapconcat
370 #'identity
371 (list (plist-get info :man-class-options))
372 " "))))
373 (section-item (plist-get attr :section-id)))
375 (concat
376 (cond
377 ((and title (stringp section-item))
378 (format ".TH \"%s\" \"%s\" \n" title section-item))
379 ((and (string= "" title) (stringp section-item))
380 (format ".TH \"%s\" \"%s\" \n" " " section-item))
381 (title
382 (format ".TH \"%s\" \"1\" \n" title))
384 ".TH \" \" \"1\" "))
385 contents)))
390 ;;; Transcode Functions
392 ;;;; Babel Call
394 ;; Babel Calls are ignored.
397 ;;;; Bold
399 (defun org-e-man-bold (bold contents info)
400 "Transcode BOLD from Org to Man.
401 CONTENTS is the text with bold markup. INFO is a plist holding
402 contextual information."
403 (format "\\fB%s\\fP" contents))
406 ;;;; Center Block
408 (defun org-e-man-center-block (center-block contents info)
409 "Transcode a CENTER-BLOCK element from Org to Man.
410 CONTENTS holds the contents of the center block. INFO is a plist
411 holding contextual information."
412 (org-e-man--wrap-label
413 center-block
414 (format ".ce %d\n.nf\n%s\n.fi"
415 (- (length (split-string contents "\n")) 1)
416 contents)))
419 ;;;; Clock
421 (defun org-e-man-clock (clock contents info)
422 "Transcode a CLOCK element from Org to Man.
423 CONTENTS is nil. INFO is a plist holding contextual
424 information."
428 ;;;; Code
430 (defun org-e-man-code (code contents info)
431 "Transcode a CODE object from Org to Man.
432 CONTENTS is nil. INFO is a plist used as a communication
433 channel."
434 (format "\\fC%s\\fP" code))
437 ;;;; Comment
438 ;; Comments are ignored.
441 ;;;; Comment Block
442 ;; Comment Blocks are ignored.
445 ;;;; Drawer
447 (defun org-e-man-drawer (drawer contents info)
448 "Transcode a DRAWER element from Org to Man.
449 DRAWER holds the drawer information
450 CONTENTS holds the contents of the block.
451 INFO is a plist holding contextual information. "
452 contents)
455 ;;;; Dynamic Block
457 (defun org-e-man-dynamic-block (dynamic-block contents info)
458 "Transcode a DYNAMIC-BLOCK element from Org to Man.
459 CONTENTS holds the contents of the block. INFO is a plist
460 holding contextual information. See `org-export-data'."
461 (org-e-man--wrap-label dynamic-block contents))
464 ;;;; Entity
466 (defun org-e-man-entity (entity contents info)
467 "Transcode an ENTITY object from Org to Man.
468 CONTENTS are the definition itself. INFO is a plist holding
469 contextual information."
470 (let ((ent (org-element-property :utf8 entity))) ent))
473 ;;;; Example Block
475 (defun org-e-man-example-block (example-block contents info)
476 "Transcode an EXAMPLE-BLOCK element from Org to Man.
477 CONTENTS is nil. INFO is a plist holding contextual
478 information."
479 (org-e-man--wrap-label
480 example-block
481 (format ".RS\n.nf\n%s\n.fi\n.RE"
482 (org-export-format-code-default example-block info))))
484 ;;;; Export Block
486 (defun org-e-man-export-block (export-block contents info)
487 "Transcode a EXPORT-BLOCK element from Org to Man.
488 CONTENTS is nil. INFO is a plist holding contextual information."
489 (when (string= (org-element-property :type export-block) "MAN")
490 (org-remove-indentation (org-element-property :value export-block))))
493 ;;;; Export Snippet
495 (defun org-e-man-export-snippet (export-snippet contents info)
496 "Transcode a EXPORT-SNIPPET object from Org to Man.
497 CONTENTS is nil. INFO is a plist holding contextual information."
498 (when (eq (org-export-snippet-backend export-snippet) 'e-man)
499 (org-element-property :value export-snippet)))
502 ;;;; Fixed Width
504 (defun org-e-man-fixed-width (fixed-width contents info)
505 "Transcode a FIXED-WIDTH element from Org to Man.
506 CONTENTS is nil. INFO is a plist holding contextual information."
507 (org-e-man--wrap-label
508 fixed-width
509 (format "\\fC\n%s\\fP"
510 (org-remove-indentation
511 (org-element-property :value fixed-width)))))
514 ;;;; Footnote Definition
515 ;; Footnote Definitions are ignored.
517 ;;;; Footnote References
518 ;; Footnote References are Ignored
521 ;;;; Headline
523 (defun org-e-man-headline (headline contents info)
524 "Transcode an HEADLINE element from Org to Man.
525 CONTENTS holds the contents of the headline. INFO is a plist
526 holding contextual information."
527 (let* ((level (org-export-get-relative-level headline info))
528 (numberedp (org-export-numbered-headline-p headline info))
529 ;; Section formatting will set two placeholders: one for the
530 ;; title and the other for the contents.
531 (section-fmt
532 (case level
533 (1 ".SH \"%s\"\n%s")
534 (2 ".SS \"%s\"\n%s")
535 (3 ".SS \"%s\"\n%s")
536 (t nil)))
537 (text (org-export-data (org-element-property :title headline) info)))
539 (cond
540 ;; Case 1: This is a footnote section: ignore it.
541 ((org-element-property :footnote-section-p headline) nil)
543 ;; Case 2. This is a deep sub-tree: export it as a list item.
544 ;; Also export as items headlines for which no section
545 ;; format has been found.
546 ((or (not section-fmt) (org-export-low-level-p headline info))
547 ;; Build the real contents of the sub-tree.
548 (let ((low-level-body
549 (concat
550 ;; If the headline is the first sibling, start a list.
551 (when (org-export-first-sibling-p headline info)
552 (format "%s\n" ".RS"))
553 ;; Itemize headline
554 ".TP\n.ft I\n" text "\n.ft\n"
555 contents ".RE")))
556 ;; If headline is not the last sibling simply return
557 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
558 ;; blank line.
559 (if (not (org-export-last-sibling-p headline info)) low-level-body
560 (replace-regexp-in-string
561 "[ \t\n]*\\'" ""
562 low-level-body))))
564 ;; Case 3. Standard headline. Export it as a section.
565 (t (format section-fmt text contents)))))
568 ;;;; Horizontal Rule
569 ;; Not supported
572 ;;;; Inline Babel Call
573 ;; Inline Babel Calls are ignored.
576 ;;;; Inline Src Block
578 (defun org-e-man-inline-src-block (inline-src-block contents info)
579 "Transcode an INLINE-SRC-BLOCK element from Org to Man.
580 CONTENTS holds the contents of the item. INFO is a plist holding
581 contextual information."
582 (let* ((code (org-element-property :value inline-src-block)))
583 (cond
584 (org-e-man-source-highlight
585 (let* ((tmpdir (if (featurep 'xemacs)
586 temp-directory
587 temporary-file-directory))
588 (in-file (make-temp-name
589 (expand-file-name "srchilite" tmpdir)))
590 (out-file (make-temp-name
591 (expand-file-name "reshilite" tmpdir)))
592 (org-lang (org-element-property :language inline-src-block))
593 (lst-lang (cadr (assq (intern org-lang)
594 org-e-man-source-highlight-langs)))
596 (cmd (concat (expand-file-name "source-highlight")
597 " -s " lst-lang
598 " -f groff_man"
599 " -i " in-file
600 " -o " out-file)))
602 (if lst-lang
603 (let ((code-block ""))
604 (with-temp-file in-file (insert code))
605 (shell-command cmd)
606 (setq code-block (org-file-contents out-file))
607 (delete-file in-file)
608 (delete-file out-file)
609 code-block)
610 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
611 code))))
613 ;; Do not use a special package: transcode it verbatim.
615 (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
616 "\\fP\n.fi\n.RE\n")))))
619 ;;;; Inlinetask
620 ;;;; Italic
622 (defun org-e-man-italic (italic contents info)
623 "Transcode ITALIC from Org to Man.
624 CONTENTS is the text with italic markup. INFO is a plist holding
625 contextual information."
626 (format "\\fI%s\\fP" contents))
629 ;;;; Item
631 (defun org-e-man-item (item contents info)
633 "Transcode an ITEM element from Org to Man.
634 CONTENTS holds the contents of the item. INFO is a plist holding
635 contextual information."
637 (let* ((bullet (org-element-property :bullet item))
638 (type (org-element-property :type (org-element-property :parent item)))
639 (checkbox (case (org-element-property :checkbox item)
640 (on "\\o'\\(sq\\(mu'") ;;
641 (off "\\(sq ") ;;
642 (trans "\\o'\\(sq\\(mi'"))) ;;
644 (tag (let ((tag (org-element-property :tag item)))
645 ;; Check-boxes must belong to the tag.
646 (and tag (format "\\fB%s\\fP"
647 (concat checkbox
648 (org-export-data tag info)))))))
650 (if (and (null tag)
651 (null checkbox))
652 (let* ((bullet (org-trim bullet))
653 (marker (cond ((string= "-" bullet) "\\(em")
654 ((string= "*" bullet) "\\(bu")
655 ((eq type 'ordered)
656 (format "%s " (org-trim bullet)))
657 (t "\\(dg"))))
658 (concat ".IP " marker " 4\n"
659 (org-trim (or contents " "))))
660 ; else
661 (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
662 (org-trim (or contents " "))))))
665 ;;;; Keyword
667 (defun org-e-man-keyword (keyword contents info)
668 "Transcode a KEYWORD element from Org to Man.
669 CONTENTS is nil. INFO is a plist holding contextual information."
670 (let ((key (org-element-property :key keyword))
671 (value (org-element-property :value keyword)))
672 (cond
673 ((string= key "MAN") value)
674 ((string= key "INDEX") nil)
675 ;; Invisible targets.
676 ((string= key "TARGET") nil)
677 ((string= key "TOC") nil))))
680 ;;;; Man Environment
682 (defun org-e-man-man-environment (man-environment contents info)
683 "Transcode a MAN-ENVIRONMENT element from Org to Man.
684 CONTENTS is nil. INFO is a plist holding contextual information."
685 (let ((label (org-element-property :name man-environment))
686 (value (org-remove-indentation
687 (org-element-property :value man-environment))))
688 (if (not (org-string-nw-p label)) value
689 ;; Environment is labelled: label must be within the environment
690 ;; (otherwise, a reference pointing to that element will count
691 ;; the section instead).
692 (with-temp-buffer
693 (insert value)
694 (goto-char (point-min))
695 (forward-line)
696 (insert (format "%s\n" label))
697 (buffer-string)))))
700 ;;;; Man Fragment
702 (defun org-e-man-man-fragment (man-fragment contents info)
703 "Transcode a MAN-FRAGMENT object from Org to Man.
704 CONTENTS is nil. INFO is a plist holding contextual information."
705 (org-element-property :value man-fragment))
708 ;;;; Line Break
710 (defun org-e-man-line-break (line-break contents info)
711 "Transcode a LINE-BREAK object from Org to Man.
712 CONTENTS is nil. INFO is a plist holding contextual information."
713 ".br\n")
716 ;;;; Link
718 (defun org-e-man-link (link desc info)
719 "Transcode a LINK object from Org to Man.
721 DESC is the description part of the link, or the empty string.
722 INFO is a plist holding contextual information. See
723 `org-export-data'."
725 (let* ((type (org-element-property :type link))
726 (raw-path (org-element-property :path link))
727 ;; Ensure DESC really exists, or set it to nil.
728 (desc (and (not (string= desc "")) desc))
730 (path (cond
731 ((member type '("http" "https" "ftp" "mailto"))
732 (concat type ":" raw-path))
733 ((string= type "file")
734 (when (string-match "\\(.+\\)::.+" raw-path)
735 (setq raw-path (match-string 1 raw-path)))
736 (if (file-name-absolute-p raw-path)
737 (concat "file://" (expand-file-name raw-path))
738 (concat "file://" raw-path)))
739 (t raw-path)))
740 protocol)
741 (cond
742 ;; External link with a description part.
743 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
744 ;; External link without a description part.
745 (path (format "\\fI%s\\fP" path))
746 ;; No path, only description. Try to do something useful.
747 (t (format "\\fI%s\\fP" desc)))))
750 ;;;; Macro
752 (defun org-e-man-macro (macro contents info)
753 "Transcode a MACRO element from Org to Man.
754 CONTENTS is nil. INFO is a plist holding contextual information."
755 ;; Use available tools.
756 (org-export-expand-macro macro info))
759 ;;;; Paragraph
761 (defun org-e-man-paragraph (paragraph contents info)
762 "Transcode a PARAGRAPH element from Org to Man.
763 CONTENTS is the contents of the paragraph, as a string. INFO is
764 the plist used as a communication channel."
765 (let ((parent (plist-get (nth 1 paragraph) :parent)))
766 (when parent
767 (let ((parent-type (car parent))
768 (fixed-paragraph ""))
769 (cond ((and (eq parent-type 'item)
770 (plist-get (nth 1 parent) :bullet))
771 (setq fixed-paragraph (concat "" contents)))
772 ((eq parent-type 'section)
773 (setq fixed-paragraph (concat ".PP\n" contents)))
774 ((eq parent-type 'footnote-definition)
775 (setq fixed-paragraph contents))
776 (t (setq fixed-paragraph (concat "" contents))))
777 fixed-paragraph))))
780 ;;;; Plain List
782 (defun org-e-man-plain-list (plain-list contents info)
783 "Transcode a PLAIN-LIST element from Org to Man.
784 CONTENTS is the contents of the list. INFO is a plist holding
785 contextual information."
786 contents)
789 ;;;; Plain Text
791 (defun org-e-man-plain-text (text info)
792 "Transcode a TEXT string from Org to Man.
793 TEXT is the string to transcode. INFO is a plist holding
794 contextual information."
795 ;; Protect
796 (setq text (replace-regexp-in-string
797 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
798 "$\\" text nil t 1))
800 ;; Handle quotation marks
801 (setq text (org-e-man--quotation-marks text info))
803 ;; Handle break preservation if required.
805 (when (plist-get info :preserve-breaks)
806 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
807 text)))
808 ;; Return value.
809 text)
812 ;;;; Planning
814 ;;;; Property Drawer
817 ;;;; Quote Block
819 (defun org-e-man-quote-block (quote-block contents info)
820 "Transcode a QUOTE-BLOCK element from Org to Man.
821 CONTENTS holds the contents of the block. INFO is a plist
822 holding contextual information."
823 (org-e-man--wrap-label
824 quote-block
825 (format ".RS\n%s\n.RE" contents)))
828 ;;;; Quote Section
830 (defun org-e-man-quote-section (quote-section contents info)
831 "Transcode a QUOTE-SECTION element from Org to Man.
832 CONTENTS is nil. INFO is a plist holding contextual information."
833 (let ((value (org-remove-indentation
834 (org-element-property :value quote-section))))
835 (when value (format ".RS\\fI%s\\fP\n.RE\n" value))))
838 ;;;; Radio Target
840 (defun org-e-man-radio-target (radio-target text info)
841 "Transcode a RADIO-TARGET object from Org to Man.
842 TEXT is the text of the target. INFO is a plist holding
843 contextual information."
844 text)
847 ;;;; Section
849 (defun org-e-man-section (section contents info)
850 "Transcode a SECTION element from Org to Man.
851 CONTENTS holds the contents of the section. INFO is a plist
852 holding contextual information."
853 contents)
856 ;;;; Special Block
858 (defun org-e-man-special-block (special-block contents info)
859 "Transcode a SPECIAL-BLOCK element from Org to Man.
860 CONTENTS holds the contents of the block. INFO is a plist
861 holding contextual information."
862 (let ((type (downcase (org-element-property :type special-block))))
863 (org-e-man--wrap-label
864 special-block
865 (format "%s\n" contents))))
868 ;;;; Src Block
870 (defun org-e-man-src-block (src-block contents info)
871 "Transcode a SRC-BLOCK element from Org to Man.
872 CONTENTS holds the contents of the item. INFO is a plist holding
873 contextual information."
875 (let* ((lang (org-element-property :language src-block))
876 (caption (org-element-property :caption src-block))
877 (label (org-element-property :name src-block))
878 (code (org-element-property :value src-block))
879 (custom-env (and lang
880 (cadr (assq (intern lang)
881 org-e-man-custom-lang-environments))))
882 (num-start (case (org-element-property :number-lines src-block)
883 (continued (org-export-get-loc src-block info))
884 (new 0)))
885 (retain-labels (org-element-property :retain-labels src-block)))
886 (cond
887 ;; Case 1. No source fontification.
888 ((not org-e-man-source-highlight)
889 (let ((caption-str (org-e-man--caption/label-string caption label info)))
890 (concat
891 (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
892 (org-export-format-code-default src-block info)))))
893 ((and org-e-man-source-highlight)
894 (let* ((tmpdir (if (featurep 'xemacs)
895 temp-directory
896 temporary-file-directory))
898 (in-file (make-temp-name
899 (expand-file-name "srchilite" tmpdir)))
900 (out-file (make-temp-name
901 (expand-file-name "reshilite" tmpdir)))
903 (org-lang (org-element-property :language src-block))
904 (lst-lang (cadr (assq (intern org-lang)
905 org-e-man-source-highlight-langs)))
907 (cmd (concat "source-highlight"
908 " -s " lst-lang
909 " -f groff_man "
910 " -i " in-file
911 " -o " out-file)))
913 (if lst-lang
914 (let ((code-block ""))
915 (with-temp-file in-file (insert code))
916 (shell-command cmd)
917 (setq code-block (org-file-contents out-file))
918 (delete-file in-file)
919 (delete-file out-file)
920 code-block)
921 (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE"
922 code)))))))
925 ;;;; Statistics Cookie
927 (defun org-e-man-statistics-cookie (statistics-cookie contents info)
928 "Transcode a STATISTICS-COOKIE object from Org to Man.
929 CONTENTS is nil. INFO is a plist holding contextual information."
930 (org-element-property :value statistics-cookie))
933 ;;;; Strike-Through
935 (defun org-e-man-strike-through (strike-through contents info)
936 "Transcode STRIKE-THROUGH from Org to Man.
937 CONTENTS is the text with strike-through markup. INFO is a plist
938 holding contextual information."
939 (format "\\fI%s\\fP" contents))
942 ;;;; Subscript
944 (defun org-e-man-subscript (subscript contents info)
945 "Transcode a SUBSCRIPT object from Org to Man.
946 CONTENTS is the contents of the object. INFO is a plist holding
947 contextual information."
948 (format "\\d\\s-2%s\\s+2\\u" contents))
951 ;;;; Superscript "^_%s$
953 (defun org-e-man-superscript (superscript contents info)
954 "Transcode a SUPERSCRIPT object from Org to Man.
955 CONTENTS is the contents of the object. INFO is a plist holding
956 contextual information."
957 (format "\\u\\s-2%s\\s+2\\d" contents))
960 ;;;; Table
962 ;; `org-e-man-table' is the entry point for table transcoding. It
963 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
964 ;; delegates the job to either `org-e-man-table--table.el-table' or
965 ;; `org-e-man-table--org-table' functions, depending of the type of
966 ;; the table.
968 ;; `org-e-man-table--align-string' is a subroutine used to build
969 ;; alignment string for Org tables.
971 (defun org-e-man-table (table contents info)
972 "Transcode a TABLE element from Org to Man.
973 CONTENTS is the contents of the table. INFO is a plist holding
974 contextual information."
975 (cond
976 ;; Case 1: verbatim table.
977 ((or org-e-man-tables-verbatim
978 (let ((attr
979 (read
980 (format
981 "(%s)"
982 (mapconcat
983 #'identity
984 (org-element-property :attr_man table)
985 " ")))))
987 (and attr (plist-get attr :verbatim))))
989 (format ".nf\n\\fC%s\\fP\n.fi"
990 ;; Re-create table, without affiliated keywords.
991 (org-trim
992 (org-element-interpret-data
993 `(table nil ,@(org-element-contents table))))))
994 ;; Case 2: Standard table.
995 (t (org-e-man-table--org-table table contents info))))
997 (defun org-e-man-table--align-string (divider table info)
998 "Return an appropriate Man alignment string.
999 TABLE is the considered table. INFO is a plist used as
1000 a communication channel."
1001 (let (alignment)
1002 ;; Extract column groups and alignment from first (non-rule)
1003 ;; row.
1004 (org-element-map
1005 (org-element-map
1006 table 'table-row
1007 (lambda (row)
1008 (and (eq (org-element-property :type row) 'standard) row))
1009 info 'first-match)
1010 'table-cell
1011 (lambda (cell)
1012 (let* ((borders (org-export-table-cell-borders cell info))
1013 (raw-width (org-export-table-cell-width cell info))
1014 (width-cm (when raw-width (/ raw-width 5)))
1015 (width (if raw-width (format "w(%dc)"
1016 (if (< width-cm 1) 1 width-cm)) "")))
1017 ;; Check left border for the first cell only.
1018 (when (and (memq 'left borders) (not alignment))
1019 (push "|" alignment))
1020 (push
1021 (case (org-export-table-cell-alignment cell info)
1022 (left (concat "l" width divider))
1023 (right (concat "r" width divider))
1024 (center (concat "c" width divider)))
1025 alignment)
1026 (when (memq 'right borders) (push "|" alignment))))
1027 info)
1028 (apply 'concat (reverse alignment))))
1030 (defun org-e-man-table--org-table (table contents info)
1031 "Return appropriate Man code for an Org table.
1033 TABLE is the table type element to transcode. CONTENTS is its
1034 contents, as a string. INFO is a plist used as a communication
1035 channel.
1037 This function assumes TABLE has `org' as its `:type' attribute."
1038 (let* ((label (org-element-property :name table))
1039 (caption (org-e-man--caption/label-string
1040 (org-element-property :caption table) label info))
1041 (attr
1042 (read
1043 (format
1044 "(%s)"
1045 (mapconcat
1046 #'identity
1047 (org-element-property :attr_man table)
1048 " "))))
1050 (divider (if (plist-get attr :divider)
1052 " "))
1054 ;; Determine alignment string.
1055 (alignment (org-e-man-table--align-string divider table info))
1056 ;; Extract others display options.
1057 (lines (org-split-string contents "\n"))
1059 (attr-list
1060 (let ((result-list '()))
1061 (dolist (attr-item
1062 (list
1063 (if (plist-get attr :expand)
1064 "expand"
1065 nil)
1067 (case (plist-get attr :placement)
1068 ('center "center")
1069 ('left nil)
1071 (if org-e-man-tables-centered
1072 "center" "")))
1074 (case (plist-get attr :boxtype)
1075 ('box "box")
1076 ('doublebox "doublebox")
1077 ('allbox "allbox")
1078 ('none nil)
1079 (t "box"))))
1081 (if attr-item
1082 (add-to-list 'result-list attr-item)))
1083 result-list))
1086 (title-line (plist-get attr :title-line))
1088 (table-format
1089 (concat
1090 (format "%s"
1091 (or (car attr-list) ""))
1093 (let ((output-list '()))
1094 (when (cdr attr-list)
1095 (dolist (attr-item (cdr attr-list))
1096 (setq output-list (concat output-list (format ",%s" attr-item)))))
1097 output-list)
1098 "")))
1100 (first-line
1101 (when lines (org-split-string (car lines) "\t"))))
1102 ;; Prepare the final format string for the table.
1104 (cond
1105 ;; Others.
1106 (lines (concat ".TS\n " table-format ";\n"
1108 (format "%s.\n"
1109 (let ((final-line ""))
1111 (when title-line
1112 (dotimes (i (length first-line))
1113 (setq final-line (concat final-line "cb" divider))))
1115 (setq final-line (concat final-line "\n"))
1116 (if alignment
1117 (setq final-line (concat final-line alignment))
1118 (dotimes (i (length first-line))
1119 (setq final-line (concat final-line "c" divider))))
1120 final-line))
1122 (format "%s.TE"
1123 (let ((final-line ""))
1124 (dolist (line-item lines)
1125 (cond
1127 (setq lines (org-split-string contents "\n"))
1129 (setq final-line (concat final-line
1130 (car (org-split-string line-item "\\\\")) "\n")))))
1131 final-line)))))))
1134 ;;;; Table Cell
1136 (defun org-e-man-table-cell (table-cell contents info)
1137 "Transcode a TABLE-CELL element from Org to Man
1138 CONTENTS is the cell contents. INFO is a plist used as
1139 a communication channel."
1140 (concat (if (and contents
1141 org-e-man-table-scientific-notation
1142 (string-match orgtbl-exp-regexp contents))
1143 ;; Use appropriate format string for scientific
1144 ;; notation.
1145 (format org-e-man-table-scientific-notation
1146 (match-string 1 contents)
1147 (match-string 2 contents))
1148 contents)
1149 (when (org-export-get-next-element table-cell info) " \t ")))
1152 ;;;; Table Row
1154 (defun org-e-man-table-row (table-row contents info)
1155 "Transcode a TABLE-ROW element from Org to Man
1156 CONTENTS is the contents of the row. INFO is a plist used as
1157 a communication channel."
1158 ;; Rules are ignored since table separators are deduced from
1159 ;; borders of the current row.
1160 (when (eq (org-element-property :type table-row) 'standard)
1161 (let* ((attr (mapconcat 'identity
1162 (org-element-property
1163 :attr_man (org-export-get-parent table-row))
1164 " "))
1165 ;; TABLE-ROW's borders are extracted from its first cell.
1166 (borders
1167 (org-export-table-cell-borders
1168 (car (org-element-contents table-row)) info)))
1169 (concat
1170 ;; Mark "hline" for horizontal lines.
1171 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1172 contents "\\\\\n"
1173 (cond
1174 ;; When BOOKTABS are activated enforce bottom rule even when
1175 ;; no hline was specifically marked.
1176 ((and (memq 'bottom borders) (memq 'below borders)) "_\n")
1177 ((memq 'below borders) "_"))))))
1180 ;;;; Target
1182 (defun org-e-man-target (target contents info)
1183 "Transcode a TARGET object from Org to Man.
1184 CONTENTS is nil. INFO is a plist holding contextual
1185 information."
1186 (format "\\fI%s\\fP"
1187 (org-export-solidify-link-text (org-element-property :value target))))
1190 ;;;; Timestamp
1192 (defun org-e-man-timestamp (timestamp contents info)
1193 "Transcode a TIMESTAMP object from Org to Man.
1194 CONTENTS is nil. INFO is a plist holding contextual
1195 information."
1199 ;;;; Underline
1201 (defun org-e-man-underline (underline contents info)
1202 "Transcode UNDERLINE from Org to Man.
1203 CONTENTS is the text with underline markup. INFO is a plist
1204 holding contextual information."
1205 (format "\\fI%s\\fP" contents))
1208 ;;;; Verbatim
1210 (defun org-e-man-verbatim (verbatim contents info)
1211 "Transcode a VERBATIM object from Org to Man.
1212 CONTENTS is nil. INFO is a plist used as a communication
1213 channel."
1214 (format ".nf\n%s\n.fi" contents))
1217 ;;;; Verse Block
1219 (defun org-e-man-verse-block (verse-block contents info)
1220 "Transcode a VERSE-BLOCK element from Org to Man.
1221 CONTENTS is verse block contents. INFO is a plist holding
1222 contextual information."
1223 (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
1227 ;;; Interactive functions
1229 (defun org-e-man-export-to-man
1230 (&optional subtreep visible-only body-only ext-plist pub-dir)
1231 "Export current buffer to a Man file.
1233 If narrowing is active in the current buffer, only export its
1234 narrowed part.
1236 If a region is active, export that region.
1238 When optional argument SUBTREEP is non-nil, export the sub-tree
1239 at point, extracting information from the headline properties
1240 first.
1242 When optional argument VISIBLE-ONLY is non-nil, don't export
1243 contents of hidden elements.
1245 When optional argument BODY-ONLY is non-nil, only the body
1246 without any markers.
1248 EXT-PLIST, when provided, is a property list with external
1249 parameters overriding Org default settings, but still inferior to
1250 file-local settings.
1252 When optional argument PUB-DIR is set, use it as the publishing
1253 directory.
1255 Return output file's name."
1256 (interactive)
1257 (let ((outfile (org-export-output-file-name ".man" subtreep pub-dir)))
1258 (org-export-to-file
1259 'e-man outfile subtreep visible-only body-only ext-plist)))
1261 (defun org-e-man-export-to-pdf
1262 (&optional subtreep visible-only body-only ext-plist pub-dir)
1263 "Export current buffer to Groff then process through to PDF.
1265 If narrowing is active in the current buffer, only export its
1266 narrowed part.
1268 If a region is active, export that region.
1270 When optional argument SUBTREEP is non-nil, export the sub-tree
1271 at point, extracting information from the headline properties
1272 first.
1274 When optional argument VISIBLE-ONLY is non-nil, don't export
1275 contents of hidden elements.
1277 When optional argument BODY-ONLY is non-nil, only write between
1278 markers.
1280 EXT-PLIST, when provided, is a property list with external
1281 parameters overriding Org default settings, but still inferior to
1282 file-local settings.
1284 When optional argument PUB-DIR is set, use it as the publishing
1285 directory.
1287 Return PDF file's name."
1288 (interactive)
1289 (org-e-man-compile
1290 (org-e-man-export-to-man
1291 subtreep visible-only body-only ext-plist pub-dir)))
1293 (defun org-e-man-compile (grofffile)
1294 "Compile a Groff file.
1296 GROFFFILE is the name of the file being compiled. Processing is
1297 done through the command specified in `org-e-man-pdf-process'.
1299 Return PDF file name or an error if it couldn't be produced."
1300 (let* ((wconfig (current-window-configuration))
1301 (grofffile (file-truename grofffile))
1302 (base (file-name-sans-extension grofffile))
1303 errors)
1304 (message (format "Processing Groff file %s ..." grofffile))
1305 (unwind-protect
1306 (progn
1307 (cond
1308 ;; A function is provided: Apply it.
1309 ((functionp org-e-man-pdf-process)
1310 (funcall org-e-man-pdf-process (shell-quote-argument grofffile)))
1311 ;; A list is provided: Replace %b, %f and %o with appropriate
1312 ;; values in each command before applying it. Output is
1313 ;; redirected to "*Org PDF Groff Output*" buffer.
1314 ((consp org-e-man-pdf-process)
1315 (let* ((out-dir (or (file-name-directory grofffile) "./"))
1316 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
1317 (mapc
1318 (lambda (command)
1319 (shell-command
1320 (replace-regexp-in-string
1321 "%b" (shell-quote-argument base)
1322 (replace-regexp-in-string
1323 "%f" (shell-quote-argument grofffile)
1324 (replace-regexp-in-string
1325 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1326 outbuf))
1327 org-e-man-pdf-process)
1328 ;; Collect standard errors from output buffer.
1329 (setq errors (org-e-man-collect-errors outbuf))))
1330 (t (error "No valid command to process to PDF")))
1331 (let ((pdffile (concat base ".pdf")))
1332 ;; Check for process failure. Provide collected errors if
1333 ;; possible.
1334 (if (not (file-exists-p pdffile))
1335 (error (concat (format "PDF file %s wasn't produced" pdffile)
1336 (when errors (concat ": " errors))))
1337 ;; Else remove log files, when specified, and signal end of
1338 ;; process to user, along with any error encountered.
1339 (when org-e-man-remove-logfiles
1340 (dolist (ext org-e-man-logfiles-extensions)
1341 (let ((file (concat base "." ext)))
1342 (when (file-exists-p file) (delete-file file)))))
1343 (message (concat "Process completed"
1344 (if (not errors) "."
1345 (concat " with errors: " errors)))))
1346 ;; Return output file name.
1347 pdffile))
1348 (set-window-configuration wconfig))))
1350 (defun org-e-man-collect-errors (buffer)
1351 "Collect some kind of errors from \"groff\" output
1352 BUFFER is the buffer containing output.
1353 Return collected error types as a string, or nil if there was
1354 none."
1355 (with-current-buffer buffer
1356 (save-excursion
1357 (goto-char (point-max))
1358 ;; Find final run
1359 nil)))
1362 (provide 'org-e-man)
1363 ;;; org-e-man.el ends here