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