ox: Implement `inner-template' transcoder
[org-mode.git] / lisp / ox-texinfo.el
blob8bc3520099b534cfadee24f9332c9e8a4ee08f61
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2013 Jonathan Leech-Pepin
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;;
7 ;; This file is not part of GNU Emacs.
8 ;;
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This library implements a Texinfo back-end for Org generic
25 ;; exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'texinfo "*Test Texinfo*") RET
31 ;; in an Org mode buffer then switch to the buffer to see the Texinfo
32 ;; export. See ox.el for more details on how this exporter works.
35 ;; It introduces nine new buffer keywords: "TEXINFO_CLASS",
36 ;; "TEXINFO_FILENAME", "TEXINFO_HEADER", "TEXINFO_POST_HEADER",
37 ;; "TEXINFO_DIR_CATEGORY", "TEXINFO_DIR_TITLE", "TEXINFO_DIR_DESC"
38 ;; "SUBTITLE" and "SUBAUTHOR".
41 ;; It introduces 1 new headline property keywords:
42 ;; "TEXINFO_MENU_TITLE" for optional menu titles.
44 ;; To include inline code snippets (for example for generating @kbd{}
45 ;; and @key{} commands), the following export-snippet keys are
46 ;; accepted:
48 ;; texinfo
49 ;; info
51 ;; You can add them for export snippets via any of the below:
53 ;; (add-to-list 'org-export-snippet-translation-alist
54 ;; '("info" . "texinfo"))
57 ;;; Code:
59 (eval-when-compile (require 'cl))
60 (require 'ox)
62 (defvar orgtbl-exp-regexp)
66 ;;; Define Back-End
68 (org-export-define-backend texinfo
69 ((bold . org-texinfo-bold)
70 (center-block . org-texinfo-center-block)
71 (clock . org-texinfo-clock)
72 (code . org-texinfo-code)
73 (comment . org-texinfo-comment)
74 (comment-block . org-texinfo-comment-block)
75 (drawer . org-texinfo-drawer)
76 (dynamic-block . org-texinfo-dynamic-block)
77 (entity . org-texinfo-entity)
78 (example-block . org-texinfo-example-block)
79 (export-block . org-texinfo-export-block)
80 (export-snippet . org-texinfo-export-snippet)
81 (fixed-width . org-texinfo-fixed-width)
82 (footnote-definition . org-texinfo-footnote-definition)
83 (footnote-reference . org-texinfo-footnote-reference)
84 (headline . org-texinfo-headline)
85 (inline-src-block . org-texinfo-inline-src-block)
86 (inlinetask . org-texinfo-inlinetask)
87 (italic . org-texinfo-italic)
88 (item . org-texinfo-item)
89 (keyword . org-texinfo-keyword)
90 (line-break . org-texinfo-line-break)
91 (link . org-texinfo-link)
92 (paragraph . org-texinfo-paragraph)
93 (plain-list . org-texinfo-plain-list)
94 (plain-text . org-texinfo-plain-text)
95 (planning . org-texinfo-planning)
96 (property-drawer . org-texinfo-property-drawer)
97 (quote-block . org-texinfo-quote-block)
98 (quote-section . org-texinfo-quote-section)
99 (radio-target . org-texinfo-radio-target)
100 (section . org-texinfo-section)
101 (special-block . org-texinfo-special-block)
102 (src-block . org-texinfo-src-block)
103 (statistics-cookie . org-texinfo-statistics-cookie)
104 (subscript . org-texinfo-subscript)
105 (superscript . org-texinfo-superscript)
106 (table . org-texinfo-table)
107 (table-cell . org-texinfo-table-cell)
108 (table-row . org-texinfo-table-row)
109 (target . org-texinfo-target)
110 (template . org-texinfo-template)
111 (timestamp . org-texinfo-timestamp)
112 (verbatim . org-texinfo-verbatim)
113 (verse-block . org-texinfo-verse-block))
114 :export-block "TEXINFO"
115 :filters-alist
116 ((:filter-headline . org-texinfo-filter-section-blank-lines)
117 (:filter-section . org-texinfo-filter-section-blank-lines))
118 :menu-entry
119 (?i "Export to Texinfo"
120 ((?t "As TEXI file" org-texinfo-export-to-texinfo)
121 (?i "As INFO file" org-texinfo-export-to-info)))
122 :options-alist
123 ((:texinfo-filename "TEXINFO_FILENAME" nil org-texinfo-filename t)
124 (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
125 (:texinfo-header "TEXINFO_HEADER" nil nil newline)
126 (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
127 (:subtitle "SUBTITLE" nil nil newline)
128 (:subauthor "SUBAUTHOR" nil nil newline)
129 (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
130 (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
131 (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
132 (:texinfo-menu-title "TEXINFO_MENU_TITLE" nil nil newline)))
136 ;;; User Configurable Variables
138 (defgroup org-export-texinfo nil
139 "Options for exporting Org mode files to Texinfo."
140 :tag "Org Export Texinfo"
141 :group 'org-export)
143 ;;; Preamble
145 (defcustom org-texinfo-filename nil
146 "Default filename for texinfo output."
147 :group 'org-export-texinfo
148 :type '(string :tag "Export Filename"))
150 (defcustom org-texinfo-default-class "info"
151 "The default Texinfo class."
152 :group 'org-export-texinfo
153 :type '(string :tag "Texinfo class"))
155 (defcustom org-texinfo-classes
156 '(("info"
157 "\\input texinfo @c -*- texinfo -*-"
158 ("@chapter %s" . "@unnumbered %s")
159 ("@section %s" . "@unnumberedsec %s")
160 ("@subsection %s" . "@unnumberedsubsec %s")
161 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
162 "Alist of Texinfo classes and associated header and structure.
163 If #+Texinfo_CLASS is set in the buffer, use its value and the
164 associated information. Here is the structure of each cell:
166 \(class-name
167 header-string
168 \(numbered-section . unnumbered-section\)
169 ...\)
171 The sectioning structure
172 ------------------------
174 The sectioning structure of the class is given by the elements
175 following the header string. For each sectioning level, a number
176 of strings is specified. A %s formatter is mandatory in each
177 section string and will be replaced by the title of the section.
179 Instead of a list of sectioning commands, you can also specify
180 a function name. That function will be called with two
181 parameters, the \(reduced) level of the headline, and a predicate
182 non-nil when the headline should be numbered. It must return
183 a format string in which the section title will be added."
184 :group 'org-export-texinfo
185 :type '(repeat
186 (list (string :tag "Texinfo class")
187 (string :tag "Texinfo header")
188 (repeat :tag "Levels" :inline t
189 (choice
190 (cons :tag "Heading"
191 (string :tag " numbered")
192 (string :tag "unnumbered"))
193 (function :tag "Hook computing sectioning"))))))
195 ;;; Headline
197 (defcustom org-texinfo-format-headline-function nil
198 "Function to format headline text.
200 This function will be called with 5 arguments:
201 TODO the todo keyword (string or nil).
202 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
203 PRIORITY the priority of the headline (integer or nil)
204 TEXT the main headline text (string).
205 TAGS the tags as a list of strings (list of strings or nil).
207 The function result will be used in the section format string.
209 As an example, one could set the variable to the following, in
210 order to reproduce the default set-up:
212 \(defun org-texinfo-format-headline (todo todo-type priority text tags)
213 \"Default format function for an headline.\"
214 \(concat (when todo
215 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
216 \(when priority
217 \(format \"\\\\framebox{\\\\#%c} \" priority))
218 text
219 \(when tags
220 \(format \"\\\\hfill{}\\\\textsc{%s}\"
221 \(mapconcat 'identity tags \":\"))))"
222 :group 'org-export-texinfo
223 :type 'function)
226 ;;; Footnotes
228 ;; Footnotes are inserted directly
230 ;;; Timestamps
232 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
233 "A printf format string to be applied to active timestamps."
234 :group 'org-export-texinfo
235 :type 'string)
237 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
238 "A printf format string to be applied to inactive timestamps."
239 :group 'org-export-texinfo
240 :type 'string)
242 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
243 "A printf format string to be applied to diary timestamps."
244 :group 'org-export-texinfo
245 :type 'string)
247 ;;; Links
249 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
250 "Format string for links with unknown path type."
251 :group 'org-export-texinfo
252 :type 'string)
254 ;;; Tables
256 (defcustom org-texinfo-tables-verbatim nil
257 "When non-nil, tables are exported verbatim."
258 :group 'org-export-texinfo
259 :type 'boolean)
261 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
262 "Format string to display numbers in scientific notation.
263 The format should have \"%s\" twice, for mantissa and exponent
264 \(i.e. \"%s\\\\times10^{%s}\").
266 When nil, no transformation is made."
267 :group 'org-export-texinfo
268 :type '(choice
269 (string :tag "Format string")
270 (const :tag "No formatting")))
272 (defcustom org-texinfo-def-table-markup "@samp"
273 "Default setting for @table environments.")
275 ;;; Text markup
277 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
278 (code . code)
279 (italic . "@emph{%s}")
280 (verbatim . verb)
281 (comment . "@c %s"))
282 "Alist of Texinfo expressions to convert text markup.
284 The key must be a symbol among `bold', `italic' and `comment'.
285 The value is a formatting string to wrap fontified text with.
287 Value can also be set to the following symbols: `verb' and
288 `code'. For the former, Org will use \"@verb\" to
289 create a format string and select a delimiter character that
290 isn't in the string. For the latter, Org will use \"@code\"
291 to typeset and try to protect special characters.
293 If no association can be found for a given markup, text will be
294 returned as-is."
295 :group 'org-export-texinfo
296 :type 'alist
297 :options '(bold code italic verbatim comment))
299 ;;; Drawers
301 (defcustom org-texinfo-format-drawer-function nil
302 "Function called to format a drawer in Texinfo code.
304 The function must accept two parameters:
305 NAME the drawer name, like \"LOGBOOK\"
306 CONTENTS the contents of the drawer.
308 The function should return the string to be exported.
310 For example, the variable could be set to the following function
311 in order to mimic default behaviour:
313 \(defun org-texinfo-format-drawer-default \(name contents\)
314 \"Format a drawer element for Texinfo export.\"
315 contents\)"
316 :group 'org-export-texinfo
317 :type 'function)
319 ;;; Inlinetasks
321 (defcustom org-texinfo-format-inlinetask-function nil
322 "Function called to format an inlinetask in Texinfo code.
324 The function must accept six parameters:
325 TODO the todo keyword, as a string
326 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
327 PRIORITY the inlinetask priority, as a string
328 NAME the inlinetask name, as a string.
329 TAGS the inlinetask tags, as a list of strings.
330 CONTENTS the contents of the inlinetask, as a string.
332 The function should return the string to be exported.
334 For example, the variable could be set to the following function
335 in order to mimic default behaviour:
337 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
338 \"Format an inline task element for Texinfo export.\"
339 \(let ((full-title
340 \(concat
341 \(when todo
342 \(format \"@strong{%s} \" todo))
343 \(when priority (format \"#%c \" priority))
344 title
345 \(when tags
346 \(format \":%s:\"
347 \(mapconcat 'identity tags \":\")))))
348 \(format (concat \"@center %s\n\n\"
349 \"%s\"
350 \"\n\"))
351 full-title contents))"
352 :group 'org-export-texinfo
353 :type 'function)
355 ;;; Src blocks
357 ;; Src Blocks are example blocks, except for LISP
359 ;;; Compilation
361 (defcustom org-texinfo-info-process
362 '("makeinfo %f")
363 "Commands to process a texinfo file to an INFO file.
364 This is list of strings, each of them will be given to the shell
365 as a command. %f in the command will be replaced by the full
366 file name, %b by the file base name \(i.e without extension) and
367 %o by the base directory of the file."
368 :group 'org-export-texinfo
369 :type '(repeat :tag "Shell command sequence"
370 (string :tag "Shell command")))
373 ;;; Internal Functions
375 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
376 "Filter controlling number of blank lines after a section."
377 (let ((blanks (make-string 2 ?\n)))
378 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
380 (defun org-texinfo--find-verb-separator (s)
381 "Return a character not used in string S.
382 This is used to choose a separator for constructs like \\verb."
383 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
384 (loop for c across ll
385 when (not (string-match (regexp-quote (char-to-string c)) s))
386 return (char-to-string c))))
388 (defun org-texinfo--make-option-string (options)
389 "Return a comma separated string of keywords and values.
390 OPTIONS is an alist where the key is the options keyword as
391 a string, and the value a list containing the keyword value, or
392 nil."
393 (mapconcat (lambda (pair)
394 (concat (first pair)
395 (when (> (length (second pair)) 0)
396 (concat "=" (second pair)))))
397 options
398 ","))
400 (defun org-texinfo--text-markup (text markup)
401 "Format TEXT depending on MARKUP text markup.
402 See `org-texinfo-text-markup-alist' for details."
403 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
404 (cond
405 ;; No format string: Return raw text.
406 ((not fmt) text)
407 ((eq 'verb fmt)
408 (let ((separator (org-texinfo--find-verb-separator text)))
409 (concat "@verb{" separator text separator "}")))
410 ((eq 'code fmt)
411 (let ((start 0)
412 (rtn "")
413 char)
414 (while (string-match "[@{}]" text)
415 (setq char (match-string 0 text))
416 (if (> (match-beginning 0) 0)
417 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
418 (setq text (substring text (1+ (match-beginning 0))))
419 (setq char (concat "@" char)
420 rtn (concat rtn char)))
421 (setq text (concat rtn text)
422 fmt "@code{%s}")
423 (format fmt text)))
424 ;; Else use format string.
425 (t (format fmt text)))))
427 ;;; Headline sanitizing
429 (defun org-texinfo--sanitize-headline (headline info)
430 "Remove all formatting from the text of a headline for use in
431 node and menu listing."
432 (mapconcat 'identity
433 (org-texinfo--sanitize-headline-contents headline info) " "))
435 (defun org-texinfo--sanitize-headline-contents (headline info)
436 "Retrieve the content of the headline.
438 Any content that can contain further formatting is checked
439 recursively, to ensure that nested content is also properly
440 retrieved."
441 (loop for contents in headline append
442 (cond
443 ;; already a string
444 ((stringp contents)
445 (list (replace-regexp-in-string " $" "" contents)))
446 ;; Is exported as-is (value)
447 ((org-element-map contents '(verbatim code)
448 (lambda (value) (org-element-property :value value)) info))
449 ;; Has content and recurse into the content
450 ((org-element-contents contents)
451 (org-texinfo--sanitize-headline-contents
452 (org-element-contents contents) info)))))
454 ;;; Menu sanitizing
456 (defun org-texinfo--sanitize-menu (title)
457 "Remove invalid characters from TITLE for use in menus and
458 nodes.
460 Based on TEXINFO specifications, the following must be removed:
461 @ { } ( ) : . ,"
462 (replace-regexp-in-string "[@{}():,.]" "" title))
464 ;;; Content sanitizing
466 (defun org-texinfo--sanitize-content (text)
467 "Ensure characters are properly escaped when used in headlines or blocks.
469 Escape characters are: @ { }"
470 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
472 ;;; Menu creation
474 (defun org-texinfo--build-menu (tree level info &optional detailed)
475 "Create the @menu/@end menu information from TREE at headline
476 level LEVEL.
478 TREE contains the parse-tree to work with, either of the entire
479 document or of a specific parent headline. LEVEL indicates what
480 level of headlines to look at when generating the menu. INFO is
481 a plist containing contextual information.
483 Detailed determines whether to build a single level of menu, or
484 recurse into all children as well."
485 (let ((menu (org-texinfo--generate-menu-list tree level info))
486 output text-menu)
487 (cond
488 (detailed
489 ;; Looping is done within the menu generation.
490 (setq text-menu (org-texinfo--generate-detailed menu level info)))
492 (setq text-menu (org-texinfo--generate-menu-items menu info))))
493 (when text-menu
494 (setq output (org-texinfo--format-menu text-menu))
495 (mapconcat 'identity output "\n"))))
497 (defun org-texinfo--generate-detailed (menu level info)
498 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
500 MENU is the parse-tree to work with. LEVEL is the starting level
501 for the menu headlines and from which recursion occurs. INFO is
502 a plist containing contextual information."
503 (when level
504 (let ((max-depth (plist-get info :headline-levels)))
505 (when (> max-depth level)
506 (loop for headline in menu append
507 (let* ((title (org-texinfo--menu-headlines headline info))
508 ;; Create list of menu entries for the next level
509 (sublist (org-texinfo--generate-menu-list
510 headline (1+ level) info))
511 ;; Generate the menu items for that level. If
512 ;; there are none omit that heading completely,
513 ;; otherwise join the title to it's related entries.
514 (submenu (if (org-texinfo--generate-menu-items sublist info)
515 (append (list title)
516 (org-texinfo--generate-menu-items sublist info))
517 'nil))
518 ;; Start the process over the next level down.
519 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
520 (setq recursion (append submenu recursion))
521 recursion))))))
523 (defun org-texinfo--generate-menu-list (tree level info)
524 "Generate the list of headlines that are within a given level
525 of the tree for further formatting.
527 TREE is the parse-tree containing the headlines. LEVEL is the
528 headline level to generate a list of. INFO is a plist holding
529 contextual information."
530 (org-element-map tree 'headline
531 (lambda (head)
532 (and (= (org-export-get-relative-level head info) level)
533 ;; Do not take note of footnotes or copying headlines.
534 (not (org-element-property :copying head))
535 (not (org-element-property :footnote-section-p head))
536 ;; Collect headline.
537 head))
538 info))
540 (defun org-texinfo--generate-menu-items (items info)
541 "Generate a list of headline information from the listing ITEMS.
543 ITEMS is a list of the headlines to be converted into entries.
544 INFO is a plist containing contextual information.
546 Returns a list containing the following information from each
547 headline: length, title, description. This is used to format the
548 menu using `org-texinfo--format-menu'."
549 (loop for headline in items collect
550 (let* ((menu-title (org-texinfo--sanitize-menu
551 (org-export-data
552 (org-element-property :texinfo-menu-title headline)
553 info)))
554 (title (org-texinfo--sanitize-menu
555 (org-texinfo--sanitize-headline
556 (org-element-property :title headline) info)))
557 (descr (org-export-data
558 (org-element-property :description headline)
559 info))
560 (menu-entry (if (string= "" menu-title) title menu-title))
561 (len (length menu-entry))
562 (output (list len menu-entry descr)))
563 output)))
565 (defun org-texinfo--menu-headlines (headline info)
566 "Retrieve the title from HEADLINE.
568 INFO is a plist holding contextual information.
570 Return the headline as a list of (length title description) with
571 length of -1 and nil description. This is used in
572 `org-texinfo--format-menu' to identify headlines as opposed to
573 entries."
574 (let ((title (org-export-data
575 (org-element-property :title headline) info)))
576 (list -1 title 'nil)))
578 (defun org-texinfo--format-menu (text-menu)
579 "Format the TEXT-MENU items to be properly printed in the menu.
581 Each entry in the menu should be provided as (length title
582 description).
584 Headlines in the detailed menu are given length -1 to ensure they
585 are never confused with other entries. They also have no
586 description.
588 Other menu items are output as:
589 Title:: description
591 With the spacing between :: and description based on the length
592 of the longest menu entry."
594 (let* ((lengths (mapcar 'car text-menu))
595 (max-length (apply 'max lengths))
596 output)
597 (setq output
598 (mapcar (lambda (name)
599 (let* ((title (nth 1 name))
600 (desc (nth 2 name))
601 (length (nth 0 name)))
602 (if (> length -1)
603 (concat "* " title ":: "
604 (make-string
605 (- (+ 3 max-length) length)
606 ?\s)
607 (if desc
608 (concat desc)))
609 (concat "\n" title "\n"))))
610 text-menu))
611 output))
613 ;;; Template
615 (defun org-texinfo-template (contents info)
616 "Return complete document string after Texinfo conversion.
617 CONTENTS is the transcoded contents string. INFO is a plist
618 holding export options."
619 (let* ((title (org-export-data (plist-get info :title) info))
620 (info-filename (or (plist-get info :texinfo-filename)
621 (file-name-nondirectory
622 (org-export-output-file-name ".info"))))
623 (author (org-export-data (plist-get info :author) info))
624 (texinfo-header (plist-get info :texinfo-header))
625 (texinfo-post-header (plist-get info :texinfo-post-header))
626 (subtitle (plist-get info :subtitle))
627 (subauthor (plist-get info :subauthor))
628 (class (plist-get info :texinfo-class))
629 (header (nth 1 (assoc class org-texinfo-classes)))
630 (copying
631 (org-element-map (plist-get info :parse-tree) 'headline
632 (lambda (hl) (and (org-element-property :copying hl) hl)) info t))
633 (dircat (plist-get info :texinfo-dircat))
634 (dirtitle (plist-get info :texinfo-dirtitle))
635 (dirdesc (plist-get info :texinfo-dirdesc))
636 ;; Spacing to align description (column 32 - 3 for `* ' and
637 ;; `.' in text.
638 (dirspacing (- 29 (length dirtitle)))
639 (menu (org-texinfo-make-menu info 'main))
640 (detail-menu (org-texinfo-make-menu info 'detailed)))
641 (concat
642 ;; Header
643 header "\n"
644 "@c %**start of header\n"
645 ;; Filename and Title
646 "@setfilename " info-filename "\n"
647 "@settitle " title "\n"
648 "\n\n"
649 "@c Version and Contact Info\n"
650 "@set AUTHOR " author "\n"
652 ;; Additional Header Options set by `#+TEXINFO_HEADER
653 (if texinfo-header
654 (concat "\n"
655 texinfo-header
656 "\n"))
658 "@c %**end of header\n"
659 "@finalout\n"
660 "\n\n"
662 ;; Additional Header Options set by #+TEXINFO_POST_HEADER
663 (if texinfo-post-header
664 (concat "\n"
665 texinfo-post-header
666 "\n"))
668 ;; Copying
669 "@copying\n"
670 ;; Only export the content of the headline, do not need the
671 ;; initial headline.
672 (org-export-data (nth 2 copying) info)
673 "@end copying\n"
674 "\n\n"
676 ;; Info directory information
677 ;; Only supply if both title and category are provided
678 (if (and dircat dirtitle)
679 (concat "@dircategory " dircat "\n"
680 "@direntry\n"
681 "* " dirtitle "."
682 (make-string dirspacing ?\s)
683 dirdesc "\n"
684 "@end direntry\n"))
685 "\n\n"
687 ;; Title
688 "@titlepage\n"
689 "@title " title "\n\n"
690 (if subtitle
691 (concat "@subtitle " subtitle "\n"))
692 "@author " author "\n"
693 (if subauthor
694 (concat subauthor "\n"))
695 "\n"
696 "@c The following two commands start the copyright page.\n"
697 "@page\n"
698 "@vskip 0pt plus 1filll\n"
699 "@insertcopying\n"
700 "@end titlepage\n\n"
701 "@c Output the table of contents at the beginning.\n"
702 "@contents\n\n"
704 ;; Configure Top Node when not for Tex
705 "@ifnottex\n"
706 "@node Top\n"
707 "@top " title " Manual\n"
708 "@insertcopying\n"
709 "@end ifnottex\n\n"
711 ;; Do not output menus if they are empty
712 (if menu
713 ;; Menu
714 (concat "@menu\n"
715 menu
716 "\n\n"
717 ;; Detailed Menu
718 (if detail-menu
719 (concat "@detailmenu\n"
720 " --- The Detailed Node Listing ---\n"
721 detail-menu
722 "\n\n"
723 "@end detailmenu\n"))
724 "@end menu\n"))
725 "\n\n"
727 ;; Document's body.
728 contents
729 "\n"
730 ;; Creator.
731 (let ((creator-info (plist-get info :with-creator)))
732 (cond
733 ((not creator-info) "")
734 ((eq creator-info 'comment)
735 (format "@c %s\n" (plist-get info :creator)))
736 (t (concat (plist-get info :creator) "\n"))))
737 ;; Document end.
738 "\n@bye")))
742 ;;; Transcode Functions
744 ;;; Bold
746 (defun org-texinfo-bold (bold contents info)
747 "Transcode BOLD from Org to Texinfo.
748 CONTENTS is the text with bold markup. INFO is a plist holding
749 contextual information."
750 (org-texinfo--text-markup contents 'bold))
752 ;;; Center Block
754 (defun org-texinfo-center-block (center-block contents info)
755 "Transcode a CENTER-BLOCK element from Org to Texinfo.
756 CONTENTS holds the contents of the block. INFO is a plist used
757 as a communication channel."
758 contents)
760 ;;; Clock
762 (defun org-texinfo-clock (clock contents info)
763 "Transcode a CLOCK element from Org to Texinfo.
764 CONTENTS is nil. INFO is a plist holding contextual
765 information."
766 (concat
767 "@noindent"
768 (format "@strong{%s} " org-clock-string)
769 (format org-texinfo-inactive-timestamp-format
770 (concat (org-translate-time
771 (org-element-property :raw-value
772 (org-element-property :value clock)))
773 (let ((time (org-element-property :duration clock)))
774 (and time (format " (%s)" time)))))
775 "@*"))
777 ;;; Code
779 (defun org-texinfo-code (code contents info)
780 "Transcode a CODE object from Org to Texinfo.
781 CONTENTS is nil. INFO is a plist used as a communication
782 channel."
783 (org-texinfo--text-markup (org-element-property :value code) 'code))
785 ;;; Comment
787 (defun org-texinfo-comment (comment contents info)
788 "Transcode a COMMENT object from Org to Texinfo.
789 CONTENTS is the text in the comment. INFO is a plist holding
790 contextual information."
791 (org-texinfo--text-markup (org-element-property :value comment) 'comment))
793 ;;; Comment Block
795 (defun org-texinfo-comment-block (comment-block contents info)
796 "Transcode a COMMENT-BLOCK object from Org to Texinfo.
797 CONTENTS is the text within the block. INFO is a plist holding
798 contextual information."
799 (format "@ignore\n%s@end ignore" (org-element-property :value comment-block)))
801 ;;; Drawer
803 (defun org-texinfo-drawer (drawer contents info)
804 "Transcode a DRAWER element from Org to Texinfo.
805 CONTENTS holds the contents of the block. INFO is a plist
806 holding contextual information."
807 (let* ((name (org-element-property :drawer-name drawer))
808 (output (if (functionp org-texinfo-format-drawer-function)
809 (funcall org-texinfo-format-drawer-function
810 name contents)
811 ;; If there's no user defined function: simply
812 ;; display contents of the drawer.
813 contents)))
814 output))
816 ;;; Dynamic Block
818 (defun org-texinfo-dynamic-block (dynamic-block contents info)
819 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
820 CONTENTS holds the contents of the block. INFO is a plist
821 holding contextual information. See `org-export-data'."
822 contents)
824 ;;; Entity
826 (defun org-texinfo-entity (entity contents info)
827 "Transcode an ENTITY object from Org to Texinfo.
828 CONTENTS are the definition itself. INFO is a plist holding
829 contextual information."
830 (let ((ent (org-element-property :latex entity)))
831 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
833 ;;; Example Block
835 (defun org-texinfo-example-block (example-block contents info)
836 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
837 CONTENTS is nil. INFO is a plist holding contextual
838 information."
839 (format "@verbatim\n%s@end verbatim"
840 (org-export-format-code-default example-block info)))
842 ;;; Export Block
844 (defun org-texinfo-export-block (export-block contents info)
845 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
846 CONTENTS is nil. INFO is a plist holding contextual information."
847 (when (string= (org-element-property :type export-block) "TEXINFO")
848 (org-remove-indentation (org-element-property :value export-block))))
850 ;;; Export Snippet
852 (defun org-texinfo-export-snippet (export-snippet contents info)
853 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
854 CONTENTS is nil. INFO is a plist holding contextual information."
855 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
856 (org-element-property :value export-snippet)))
858 ;;; Fixed Width
860 (defun org-texinfo-fixed-width (fixed-width contents info)
861 "Transcode a FIXED-WIDTH element from Org to Texinfo.
862 CONTENTS is nil. INFO is a plist holding contextual information."
863 (format "@example\n%s\n@end example"
864 (org-remove-indentation
865 (org-texinfo--sanitize-content
866 (org-element-property :value fixed-width)))))
868 ;;; Footnote Reference
871 (defun org-texinfo-footnote-reference (footnote contents info)
872 "Create a footnote reference for FOOTNOTE.
874 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
875 plist holding contextual information."
876 (let ((def (org-export-get-footnote-definition footnote info)))
877 (format "@footnote{%s}"
878 (org-trim (org-export-data def info)))))
880 ;;; Headline
882 (defun org-texinfo-headline (headline contents info)
883 "Transcode an HEADLINE element from Org to Texinfo.
884 CONTENTS holds the contents of the headline. INFO is a plist
885 holding contextual information."
886 (let* ((class (plist-get info :texinfo-class))
887 (level (org-export-get-relative-level headline info))
888 (numberedp (org-export-numbered-headline-p headline info))
889 (class-sectionning (assoc class org-texinfo-classes))
890 ;; Find the index type, if any
891 (index (org-element-property :index headline))
892 ;; Retrieve custom menu title (if any)
893 (menu-title (org-texinfo--sanitize-menu
894 (org-export-data
895 (org-element-property :texinfo-menu-title headline)
896 info)))
897 ;; Retrieve headline text
898 (text (org-texinfo--sanitize-headline
899 (org-element-property :title headline) info))
900 ;; Create node info, to insert it before section formatting.
901 ;; Use custom menu title if present
902 (node (format "@node %s\n"
903 (org-texinfo--sanitize-menu
904 (replace-regexp-in-string "%" "%%"
905 (if (not (string= "" menu-title))
906 menu-title
907 text)))))
908 ;; Menus must be generated with first child, otherwise they
909 ;; will not nest properly
910 (menu (let* ((first (org-export-first-sibling-p headline info))
911 (parent (org-export-get-parent-headline headline))
912 (title (org-texinfo--sanitize-headline
913 (org-element-property :title parent) info))
914 heading listing
915 (tree (plist-get info :parse-tree)))
916 (if first
917 (org-element-map (plist-get info :parse-tree) 'headline
918 (lambda (ref)
919 (if (member title (org-element-property :title ref))
920 (push ref heading)))
921 info t))
922 (setq listing (org-texinfo--build-menu
923 (car heading) level info))
924 (if listing
925 (setq listing (replace-regexp-in-string
926 "%" "%%" listing)
927 listing (format
928 "\n@menu\n%s\n@end menu\n\n" listing))
929 'nil)))
930 ;; Section formatting will set two placeholders: one for the
931 ;; title and the other for the contents.
932 (section-fmt
933 (let ((sec (if (and (symbolp (nth 2 class-sectionning))
934 (fboundp (nth 2 class-sectionning)))
935 (funcall (nth 2 class-sectionning) level numberedp)
936 (nth (1+ level) class-sectionning))))
937 (cond
938 ;; No section available for that LEVEL.
939 ((not sec) nil)
940 ;; Section format directly returned by a function.
941 ((stringp sec) sec)
942 ;; (numbered-section . unnumbered-section)
943 ((not (consp (cdr sec)))
944 ;; If an index, always unnumbered
945 (if index
946 (concat menu node (cdr sec) "\n%s")
947 ;; Otherwise number as needed.
948 (concat menu node
949 (funcall
950 (if numberedp #'car #'cdr) sec) "\n%s"))))))
951 (todo
952 (and (plist-get info :with-todo-keywords)
953 (let ((todo (org-element-property :todo-keyword headline)))
954 (and todo (org-export-data todo info)))))
955 (todo-type (and todo (org-element-property :todo-type headline)))
956 (tags (and (plist-get info :with-tags)
957 (org-export-get-tags headline info)))
958 (priority (and (plist-get info :with-priority)
959 (org-element-property :priority headline)))
960 ;; Create the headline text along with a no-tag version. The
961 ;; latter is required to remove tags from table of contents.
962 (full-text (org-texinfo--sanitize-content
963 (if (functionp org-texinfo-format-headline-function)
964 ;; User-defined formatting function.
965 (funcall org-texinfo-format-headline-function
966 todo todo-type priority text tags)
967 ;; Default formatting.
968 (concat
969 (when todo
970 (format "@strong{%s} " todo))
971 (when priority (format "@emph{#%s} " priority))
972 text
973 (when tags
974 (format ":%s:"
975 (mapconcat 'identity tags ":")))))))
976 (full-text-no-tag
977 (org-texinfo--sanitize-content
978 (if (functionp org-texinfo-format-headline-function)
979 ;; User-defined formatting function.
980 (funcall org-texinfo-format-headline-function
981 todo todo-type priority text nil)
982 ;; Default formatting.
983 (concat
984 (when todo (format "@strong{%s} " todo))
985 (when priority (format "@emph{#%c} " priority))
986 text))))
987 (pre-blanks
988 (make-string (org-element-property :pre-blank headline) 10)))
989 (cond
990 ;; Case 1: This is a footnote section: ignore it.
991 ((org-element-property :footnote-section-p headline) nil)
992 ;; Case 2: This is the `copying' section: ignore it
993 ;; This is used elsewhere.
994 ((org-element-property :copying headline) nil)
995 ;; Case 3: An index. If it matches one of the known indexes,
996 ;; print it as such following the contents, otherwise
997 ;; print the contents and leave the index up to the user.
998 (index
999 (format
1000 section-fmt full-text
1001 (concat pre-blanks contents "\n"
1002 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1003 (concat "@printindex " index)))))
1004 ;; Case 4: This is a deep sub-tree: export it as a list item.
1005 ;; Also export as items headlines for which no section
1006 ;; format has been found.
1007 ((or (not section-fmt) (org-export-low-level-p headline info))
1008 ;; Build the real contents of the sub-tree.
1009 (let ((low-level-body
1010 (concat
1011 ;; If the headline is the first sibling, start a list.
1012 (when (org-export-first-sibling-p headline info)
1013 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1014 ;; Itemize headline
1015 "@item\n" full-text "\n" pre-blanks contents)))
1016 ;; If headline is not the last sibling simply return
1017 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1018 ;; blank line.
1019 (if (not (org-export-last-sibling-p headline info)) low-level-body
1020 (replace-regexp-in-string
1021 "[ \t\n]*\\'"
1022 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1023 low-level-body))))
1024 ;; Case 5: Standard headline. Export it as a section.
1026 (cond
1027 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1028 ;; Regular section. Use specified format string.
1029 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1030 (concat pre-blanks contents)))
1031 ((string-match "\\`@\\(.*?\\){" section-fmt)
1032 ;; If tags should be removed from table of contents, insert
1033 ;; title without tags as an alternative heading in sectioning
1034 ;; command.
1035 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1036 nil nil section-fmt 1)
1037 ;; Replace square brackets with parenthesis since
1038 ;; square brackets are not supported in optional
1039 ;; arguments.
1040 (replace-regexp-in-string
1041 "\\[" "("
1042 (replace-regexp-in-string
1043 "\\]" ")"
1044 full-text-no-tag))
1045 full-text
1046 (concat pre-blanks contents)))
1048 ;; Impossible to add an alternative heading. Fallback to
1049 ;; regular sectioning format string.
1050 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1051 (concat pre-blanks contents))))))))
1053 ;;; Inline Src Block
1055 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1056 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1057 CONTENTS holds the contents of the item. INFO is a plist holding
1058 contextual information."
1059 (let* ((code (org-element-property :value inline-src-block))
1060 (separator (org-texinfo--find-verb-separator code)))
1061 (concat "@verb{" separator code separator "}")))
1063 ;;; Inlinetask
1065 (defun org-texinfo-inlinetask (inlinetask contents info)
1066 "Transcode an INLINETASK element from Org to Texinfo.
1067 CONTENTS holds the contents of the block. INFO is a plist
1068 holding contextual information."
1069 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1070 (todo (and (plist-get info :with-todo-keywords)
1071 (let ((todo (org-element-property :todo-keyword inlinetask)))
1072 (and todo (org-export-data todo info)))))
1073 (todo-type (org-element-property :todo-type inlinetask))
1074 (tags (and (plist-get info :with-tags)
1075 (org-export-get-tags inlinetask info)))
1076 (priority (and (plist-get info :with-priority)
1077 (org-element-property :priority inlinetask))))
1078 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1079 ;; with appropriate arguments.
1080 (if (functionp org-texinfo-format-inlinetask-function)
1081 (funcall org-texinfo-format-inlinetask-function
1082 todo todo-type priority title tags contents)
1083 ;; Otherwise, use a default template.
1084 (let ((full-title
1085 (concat
1086 (when todo (format "@strong{%s} " todo))
1087 (when priority (format "#%c " priority))
1088 title
1089 (when tags (format ":%s:"
1090 (mapconcat 'identity tags ":"))))))
1091 (format (concat "@center %s\n\n"
1092 "%s"
1093 "\n")
1094 full-title contents)))))
1096 ;;; Italic
1098 (defun org-texinfo-italic (italic contents info)
1099 "Transcode ITALIC from Org to Texinfo.
1100 CONTENTS is the text with italic markup. INFO is a plist holding
1101 contextual information."
1102 (org-texinfo--text-markup contents 'italic))
1104 ;;; Item
1106 (defun org-texinfo-item (item contents info)
1107 "Transcode an ITEM element from Org to Texinfo.
1108 CONTENTS holds the contents of the item. INFO is a plist holding
1109 contextual information."
1110 (let* ((tag (org-element-property :tag item))
1111 (desc (org-export-data tag info)))
1112 (concat "\n@item " (if tag desc) "\n"
1113 (org-trim contents) "\n")))
1115 ;;; Keyword
1117 (defun org-texinfo-keyword (keyword contents info)
1118 "Transcode a KEYWORD element from Org to Texinfo.
1119 CONTENTS is nil. INFO is a plist holding contextual information."
1120 (let ((key (org-element-property :key keyword))
1121 (value (org-element-property :value keyword)))
1122 (cond
1123 ((string= key "TEXINFO") value)
1124 ((string= key "CINDEX") (format "@cindex %s" value))
1125 ((string= key "FINDEX") (format "@findex %s" value))
1126 ((string= key "KINDEX") (format "@kindex %s" value))
1127 ((string= key "PINDEX") (format "@pindex %s" value))
1128 ((string= key "TINDEX") (format "@tindex %s" value))
1129 ((string= key "VINDEX") (format "@vindex %s" value)))))
1131 ;;; Line Break
1133 (defun org-texinfo-line-break (line-break contents info)
1134 "Transcode a LINE-BREAK object from Org to Texinfo.
1135 CONTENTS is nil. INFO is a plist holding contextual information."
1136 "@*\n")
1138 ;;; Link
1140 (defun org-texinfo-link (link desc info)
1141 "Transcode a LINK object from Org to Texinfo.
1143 DESC is the description part of the link, or the empty string.
1144 INFO is a plist holding contextual information. See
1145 `org-export-data'."
1146 (let* ((type (org-element-property :type link))
1147 (raw-path (org-element-property :path link))
1148 ;; Ensure DESC really exists, or set it to nil.
1149 (desc (and (not (string= desc "")) desc))
1150 (path (cond
1151 ((member type '("http" "https" "ftp"))
1152 (concat type ":" raw-path))
1153 ((string= type "file")
1154 (if (file-name-absolute-p raw-path)
1155 (concat "file://" (expand-file-name raw-path))
1156 (concat "file://" raw-path)))
1157 (t raw-path)))
1158 (email (if (string= type "mailto")
1159 (let ((text (replace-regexp-in-string
1160 "@" "@@" raw-path)))
1161 (concat text (if desc (concat "," desc))))))
1162 protocol)
1163 (cond
1164 ;; Links pointing to an headline: Find destination and build
1165 ;; appropriate referencing command.
1166 ((member type '("custom-id" "id"))
1167 (let ((destination (org-export-resolve-id-link link info)))
1168 (case (org-element-type destination)
1169 ;; Id link points to an external file.
1170 (plain-text
1171 (if desc (format "@uref{file://%s,%s}" destination desc)
1172 (format "@uref{file://%s}" destination)))
1173 ;; LINK points to an headline. Use the headline as the NODE target
1174 (headline
1175 (format "@ref{%s,%s}"
1176 (or (org-element-property :texinfo-menu-title destination)
1177 (org-element-property :title destination))
1178 (or desc "")))
1179 (otherwise
1180 (let ((path (org-export-solidify-link-text path)))
1181 (if (not desc) (format "@ref{%s}" path)
1182 (format "@ref{%s,,%s}" path desc)))))))
1183 ((member type '("info"))
1184 (let* ((info-path (split-string path ":"))
1185 (info-manual (car info-path))
1186 (info-node (or (cadr info-path) "top"))
1187 (title (or desc "")))
1188 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1189 ((member type '("fuzzy"))
1190 (let ((destination (org-export-resolve-fuzzy-link link info)))
1191 (case (org-element-type destination)
1192 ;; Id link points to an external file.
1193 (plain-text
1194 (if desc (format "@uref{file://%s,%s}" destination desc)
1195 (format "@uref{file://%s}" destination)))
1196 ;; LINK points to an headline. Use the headline as the NODE target
1197 (headline
1198 (format "@ref{%s,%s}"
1199 (or (org-element-property :texinfo-menu-title destination)
1200 (org-element-property :title destination))
1201 (or desc "")))
1202 (otherwise
1203 (let ((path (org-export-solidify-link-text path)))
1204 (if (not desc) (format "@ref{%s}" path)
1205 (format "@ref{%s,,%s}" path desc)))))))
1206 ;; Special case for email addresses
1207 (email
1208 (format "@email{%s}" email))
1209 ;; External link with a description part.
1210 ((and path desc) (format "@uref{%s,%s}" path desc))
1211 ;; External link without a description part.
1212 (path (format "@uref{%s}" path))
1213 ;; No path, only description. Try to do something useful.
1214 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1217 ;;; Menu
1219 (defun org-texinfo-make-menu (info level)
1220 "Create the menu for inclusion in the texifo document.
1222 INFO is the parsed buffer that contains the headlines. LEVEL
1223 determines whether to make the main menu, or the detailed menu.
1225 This is only used for generating the primary menu. In-Node menus
1226 are generated directly."
1227 (let ((parse (plist-get info :parse-tree)))
1228 (cond
1229 ;; Generate the main menu
1230 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1231 ;; Generate the detailed (recursive) menu
1232 ((eq level 'detailed)
1233 ;; Requires recursion
1234 ;;(org-texinfo--build-detailed-menu parse top info)
1235 (org-texinfo--build-menu parse 1 info 'detailed)))))
1237 ;;; Paragraph
1239 (defun org-texinfo-paragraph (paragraph contents info)
1240 "Transcode a PARAGRAPH element from Org to Texinfo.
1241 CONTENTS is the contents of the paragraph, as a string. INFO is
1242 the plist used as a communication channel."
1243 contents)
1245 ;;; Plain List
1247 (defun org-texinfo-plain-list (plain-list contents info)
1248 "Transcode a PLAIN-LIST element from Org to Texinfo.
1249 CONTENTS is the contents of the list. INFO is a plist holding
1250 contextual information."
1251 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1252 (indic (or (plist-get attr :indic)
1253 org-texinfo-def-table-markup))
1254 (type (org-element-property :type plain-list))
1255 (table-type (or (plist-get attr :table-type)
1256 "table"))
1257 ;; Ensure valid texinfo table type.
1258 (table-type (if (memq table-type '("table" "ftable" "vtable"))
1259 table-type
1260 "table"))
1261 (list-type (cond
1262 ((eq type 'ordered) "enumerate")
1263 ((eq type 'unordered) "itemize")
1264 ((eq type 'descriptive) table-type))))
1265 (format "@%s%s\n@end %s"
1266 (if (eq type 'descriptive)
1267 (concat list-type " " indic)
1268 list-type)
1269 contents
1270 list-type)))
1272 ;;; Plain Text
1274 (defun org-texinfo-plain-text (text info)
1275 "Transcode a TEXT string from Org to Texinfo.
1276 TEXT is the string to transcode. INFO is a plist holding
1277 contextual information."
1278 ;; First protect @, { and }.
1279 (let ((output (org-texinfo--sanitize-content text)))
1280 ;; Activate smart quotes. Be sure to provide original TEXT string
1281 ;; since OUTPUT may have been modified.
1282 (when (plist-get info :with-smart-quotes)
1283 (setq output
1284 (org-export-activate-smart-quotes output :texinfo info text)))
1285 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1286 (let ((case-fold-search nil)
1287 (start 0))
1288 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1289 (setq output (replace-match
1290 (format "@%s{}" (match-string 1 output)) nil t output)
1291 start (match-end 0))))
1292 ;; Convert special strings.
1293 (when (plist-get info :with-special-strings)
1294 (while (string-match (regexp-quote "...") output)
1295 (setq output (replace-match "@dots{}" nil t output))))
1296 ;; Handle break preservation if required.
1297 (when (plist-get info :preserve-breaks)
1298 (setq output (replace-regexp-in-string
1299 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1300 ;; Return value.
1301 output))
1303 ;;; Planning
1305 (defun org-texinfo-planning (planning contents info)
1306 "Transcode a PLANNING element from Org to Texinfo.
1307 CONTENTS is nil. INFO is a plist holding contextual
1308 information."
1309 (concat
1310 "@noindent"
1311 (mapconcat
1312 'identity
1313 (delq nil
1314 (list
1315 (let ((closed (org-element-property :closed planning)))
1316 (when closed
1317 (concat
1318 (format "@strong{%s} " org-closed-string)
1319 (format org-texinfo-inactive-timestamp-format
1320 (org-translate-time
1321 (org-element-property :raw-value closed))))))
1322 (let ((deadline (org-element-property :deadline planning)))
1323 (when deadline
1324 (concat
1325 (format "@strong{%s} " org-deadline-string)
1326 (format org-texinfo-active-timestamp-format
1327 (org-translate-time
1328 (org-element-property :raw-value deadline))))))
1329 (let ((scheduled (org-element-property :scheduled planning)))
1330 (when scheduled
1331 (concat
1332 (format "@strong{%s} " org-scheduled-string)
1333 (format org-texinfo-active-timestamp-format
1334 (org-translate-time
1335 (org-element-property :raw-value scheduled))))))))
1336 " ")
1337 "@*"))
1339 ;;; Property Drawer
1341 (defun org-texinfo-property-drawer (property-drawer contents info)
1342 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1343 CONTENTS is nil. INFO is a plist holding contextual
1344 information."
1345 ;; The property drawer isn't exported but we want separating blank
1346 ;; lines nonetheless.
1349 ;;; Quote Block
1351 (defun org-texinfo-quote-block (quote-block contents info)
1352 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1353 CONTENTS holds the contents of the block. INFO is a plist
1354 holding contextual information."
1355 (let* ((title (org-element-property :name quote-block))
1356 (start-quote (concat "@quotation"
1357 (if title
1358 (format " %s" title)))))
1359 (format "%s\n%s@end quotation" start-quote contents)))
1361 ;;; Quote Section
1363 (defun org-texinfo-quote-section (quote-section contents info)
1364 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1365 CONTENTS is nil. INFO is a plist holding contextual information."
1366 (let ((value (org-remove-indentation
1367 (org-element-property :value quote-section))))
1368 (when value (format "@verbatim\n%s@end verbatim" value))))
1370 ;;; Radio Target
1372 (defun org-texinfo-radio-target (radio-target text info)
1373 "Transcode a RADIO-TARGET object from Org to Texinfo.
1374 TEXT is the text of the target. INFO is a plist holding
1375 contextual information."
1376 (format "@anchor{%s}%s"
1377 (org-export-solidify-link-text
1378 (org-element-property :value radio-target))
1379 text))
1381 ;;; Section
1383 (defun org-texinfo-section (section contents info)
1384 "Transcode a SECTION element from Org to Texinfo.
1385 CONTENTS holds the contents of the section. INFO is a plist
1386 holding contextual information."
1387 contents)
1389 ;;; Special Block
1391 (defun org-texinfo-special-block (special-block contents info)
1392 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1393 CONTENTS holds the contents of the block. INFO is a plist used
1394 as a communication channel."
1395 contents)
1397 ;;; Src Block
1399 (defun org-texinfo-src-block (src-block contents info)
1400 "Transcode a SRC-BLOCK element from Org to Texinfo.
1401 CONTENTS holds the contents of the item. INFO is a plist holding
1402 contextual information."
1403 (let* ((lang (org-element-property :language src-block))
1404 (lisp-p (string-match-p "lisp" lang)))
1405 (cond
1406 ;; Case 1. Lisp Block
1407 (lisp-p
1408 (format "@lisp\n%s@end lisp"
1409 (org-export-format-code-default src-block info)))
1410 ;; Case 2. Other blocks
1412 (format "@example\n%s@end example"
1413 (org-export-format-code-default src-block info))))))
1415 ;;; Statistics Cookie
1417 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1418 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1419 CONTENTS is nil. INFO is a plist holding contextual information."
1420 (org-element-property :value statistics-cookie))
1422 ;;; Subscript
1424 (defun org-texinfo-subscript (subscript contents info)
1425 "Transcode a SUBSCRIPT object from Org to Texinfo.
1426 CONTENTS is the contents of the object. INFO is a plist holding
1427 contextual information."
1428 (format "@math{_%s}" contents))
1430 ;;; Superscript
1432 (defun org-texinfo-superscript (superscript contents info)
1433 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1434 CONTENTS is the contents of the object. INFO is a plist holding
1435 contextual information."
1436 (format "@math{^%s}" contents))
1438 ;;; Table
1440 ;; `org-texinfo-table' is the entry point for table transcoding. It
1441 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1442 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1443 ;; `org-texinfo-table--org-table' functions, depending of the type of
1444 ;; the table.
1446 ;; `org-texinfo-table--align-string' is a subroutine used to build
1447 ;; alignment string for Org tables.
1449 (defun org-texinfo-table (table contents info)
1450 "Transcode a TABLE element from Org to Texinfo.
1451 CONTENTS is the contents of the table. INFO is a plist holding
1452 contextual information."
1453 (cond
1454 ;; Case 1: verbatim table.
1455 ((or org-texinfo-tables-verbatim
1456 (let ((attr (mapconcat 'identity
1457 (org-element-property :attr_latex table)
1458 " ")))
1459 (and attr (string-match "\\<verbatim\\>" attr))))
1460 (format "@verbatim \n%s\n@end verbatim"
1461 ;; Re-create table, without affiliated keywords.
1462 (org-trim
1463 (org-element-interpret-data
1464 `(table nil ,@(org-element-contents table))))))
1465 ;; Case 2: table.el table. Convert it using appropriate tools.
1466 ((eq (org-element-property :type table) 'table.el)
1467 (org-texinfo-table--table.el-table table contents info))
1468 ;; Case 3: Standard table.
1469 (t (org-texinfo-table--org-table table contents info))))
1471 (defun org-texinfo-table-column-widths (table info)
1472 "Determine the largest table cell in each column to process alignment.
1474 TABLE is the table element to transcode. INFO is a plist used as
1475 a communication channel."
1476 (let* ((rows (org-element-map table 'table-row 'identity info))
1477 (collected (loop for row in rows collect
1478 (org-element-map row 'table-cell 'identity info)))
1479 (number-cells (length (car collected)))
1480 cells counts)
1481 (loop for row in collected do
1482 (push (mapcar (lambda (ref)
1483 (let* ((start (org-element-property :contents-begin ref))
1484 (end (org-element-property :contents-end ref))
1485 (length (- end start)))
1486 length)) row) cells))
1487 (setq cells (org-remove-if 'null cells))
1488 (push (loop for count from 0 to (- number-cells 1) collect
1489 (loop for item in cells collect
1490 (nth count item))) counts)
1491 (mapconcat (lambda (size)
1492 (make-string size ?a)) (mapcar (lambda (ref)
1493 (apply 'max `,@ref)) (car counts))
1494 "} {")))
1496 (defun org-texinfo-table--org-table (table contents info)
1497 "Return appropriate Texinfo code for an Org table.
1499 TABLE is the table type element to transcode. CONTENTS is its
1500 contents, as a string. INFO is a plist used as a communication
1501 channel.
1503 This function assumes TABLE has `org' as its `:type' attribute."
1504 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1505 (col-width (plist-get attr :columns))
1506 (columns (if col-width
1507 (format "@columnfractions %s"
1508 col-width)
1509 (format "{%s}"
1510 (org-texinfo-table-column-widths
1511 table info)))))
1512 ;; Prepare the final format string for the table.
1513 (cond
1514 ;; Longtable.
1515 ;; Others.
1516 (t (concat
1517 (format "@multitable %s\n%s@end multitable"
1518 columns
1519 contents))))))
1521 (defun org-texinfo-table--table.el-table (table contents info)
1522 "Returns nothing.
1524 Rather than return an invalid table, nothing is returned."
1525 'nil)
1527 ;;; Table Cell
1529 (defun org-texinfo-table-cell (table-cell contents info)
1530 "Transcode a TABLE-CELL element from Org to Texinfo.
1531 CONTENTS is the cell contents. INFO is a plist used as
1532 a communication channel."
1533 (concat (if (and contents
1534 org-texinfo-table-scientific-notation
1535 (string-match orgtbl-exp-regexp contents))
1536 ;; Use appropriate format string for scientific
1537 ;; notation.
1538 (format org-texinfo-table-scientific-notation
1539 (match-string 1 contents)
1540 (match-string 2 contents))
1541 contents)
1542 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1544 ;;; Table Row
1546 (defun org-texinfo-table-row (table-row contents info)
1547 "Transcode a TABLE-ROW element from Org to Texinfo.
1548 CONTENTS is the contents of the row. INFO is a plist used as
1549 a communication channel."
1550 ;; Rules are ignored since table separators are deduced from
1551 ;; borders of the current row.
1552 (when (eq (org-element-property :type table-row) 'standard)
1553 (let ((rowgroup-tag
1554 (cond
1555 ;; Case 1: Belongs to second or subsequent rowgroup.
1556 ((not (= 1 (org-export-table-row-group table-row info)))
1557 "@item ")
1558 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1559 ((org-export-table-has-header-p
1560 (org-export-get-parent-table table-row) info)
1561 "@headitem ")
1562 ;; Case 3: Row is from first and only row group.
1563 (t "@item "))))
1564 (when (eq (org-element-property :type table-row) 'standard)
1565 (concat rowgroup-tag contents "\n")))))
1567 ;;; Target
1569 (defun org-texinfo-target (target contents info)
1570 "Transcode a TARGET object from Org to Texinfo.
1571 CONTENTS is nil. INFO is a plist holding contextual
1572 information."
1573 (format "@anchor{%s}"
1574 (org-export-solidify-link-text (org-element-property :value target))))
1576 ;;; Timestamp
1578 (defun org-texinfo-timestamp (timestamp contents info)
1579 "Transcode a TIMESTAMP object from Org to Texinfo.
1580 CONTENTS is nil. INFO is a plist holding contextual
1581 information."
1582 (let ((value (org-texinfo-plain-text
1583 (org-timestamp-translate timestamp) info)))
1584 (case (org-element-property :type timestamp)
1585 ((active active-range)
1586 (format org-texinfo-active-timestamp-format value))
1587 ((inactive inactive-range)
1588 (format org-texinfo-inactive-timestamp-format value))
1589 (t (format org-texinfo-diary-timestamp-format value)))))
1591 ;;; Verbatim
1593 (defun org-texinfo-verbatim (verbatim contents info)
1594 "Transcode a VERBATIM object from Org to Texinfo.
1595 CONTENTS is nil. INFO is a plist used as a communication
1596 channel."
1597 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1599 ;;; Verse Block
1601 (defun org-texinfo-verse-block (verse-block contents info)
1602 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1603 CONTENTS is verse block contents. INFO is a plist holding
1604 contextual information."
1605 ;; In a verse environment, add a line break to each newline
1606 ;; character and change each white space at beginning of a line
1607 ;; into a space of 1 em. Also change each blank line with
1608 ;; a vertical space of 1 em.
1609 (progn
1610 (setq contents (replace-regexp-in-string
1611 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1612 (replace-regexp-in-string
1613 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1614 (while (string-match "^[ \t]+" contents)
1615 (let ((new-str (format "\\hspace*{%dem}"
1616 (length (match-string 0 contents)))))
1617 (setq contents (replace-match new-str nil t contents))))
1618 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1621 ;;; Interactive functions
1623 (defun org-texinfo-export-to-texinfo
1624 (&optional async subtreep visible-only body-only ext-plist)
1625 "Export current buffer to a Texinfo file.
1627 If narrowing is active in the current buffer, only export its
1628 narrowed part.
1630 If a region is active, export that region.
1632 A non-nil optional argument ASYNC means the process should happen
1633 asynchronously. The resulting file should be accessible through
1634 the `org-export-stack' interface.
1636 When optional argument SUBTREEP is non-nil, export the sub-tree
1637 at point, extracting information from the headline properties
1638 first.
1640 When optional argument VISIBLE-ONLY is non-nil, don't export
1641 contents of hidden elements.
1643 When optional argument BODY-ONLY is non-nil, only write code
1644 between \"\\begin{document}\" and \"\\end{document}\".
1646 EXT-PLIST, when provided, is a property list with external
1647 parameters overriding Org default settings, but still inferior to
1648 file-local settings.
1650 Return output file's name."
1651 (interactive)
1652 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1653 (if async
1654 (org-export-async-start
1655 (lambda (f) (org-export-add-to-stack f 'texinfo))
1656 `(expand-file-name
1657 (org-export-to-file
1658 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1659 ',ext-plist)))
1660 (org-export-to-file
1661 'texinfo outfile subtreep visible-only body-only ext-plist))))
1663 (defun org-texinfo-export-to-info
1664 (&optional async subtreep visible-only body-only ext-plist)
1665 "Export current buffer to Texinfo then process through to INFO.
1667 If narrowing is active in the current buffer, only export its
1668 narrowed part.
1670 If a region is active, export that region.
1672 A non-nil optional argument ASYNC means the process should happen
1673 asynchronously. The resulting file should be accessible through
1674 the `org-export-stack' interface.
1676 When optional argument SUBTREEP is non-nil, export the sub-tree
1677 at point, extracting information from the headline properties
1678 first.
1680 When optional argument VISIBLE-ONLY is non-nil, don't export
1681 contents of hidden elements.
1683 When optional argument BODY-ONLY is non-nil, only write code
1684 between \"\\begin{document}\" and \"\\end{document}\".
1686 EXT-PLIST, when provided, is a property list with external
1687 parameters overriding Org default settings, but still inferior to
1688 file-local settings.
1690 When optional argument PUB-DIR is set, use it as the publishing
1691 directory.
1693 Return INFO file's name."
1694 (interactive)
1695 (if async
1696 (let ((outfile (org-export-output-file-name ".texi" subtreep)))
1697 (org-export-async-start
1698 (lambda (f) (org-export-add-to-stack f 'texinfo))
1699 `(expand-file-name
1700 (org-texinfo-compile
1701 (org-export-to-file
1702 'texinfo ,outfile ,subtreep ,visible-only ,body-only
1703 ',ext-plist)))))
1704 (org-texinfo-compile
1705 (org-texinfo-export-to-texinfo
1706 nil subtreep visible-only body-only ext-plist))))
1708 (defun org-texinfo-compile (file)
1709 "Compile a texinfo file.
1711 FILE is the name of the file being compiled. Processing is
1712 done through the command specified in `org-texinfo-info-process'.
1714 Return INFO file name or an error if it couldn't be produced."
1715 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1716 (full-name (file-truename file))
1717 (out-dir (file-name-directory file))
1718 ;; Make sure `default-directory' is set to FILE directory,
1719 ;; not to whatever value the current buffer may have.
1720 (default-directory (file-name-directory full-name))
1721 errors)
1722 (message (format "Processing Texinfo file %s ..." file))
1723 (save-window-excursion
1724 (cond
1725 ;; A function is provided: Apply it.
1726 ((functionp org-texinfo-info-process)
1727 (funcall org-texinfo-info-process (shell-quote-argument file)))
1728 ;; A list is provided: Replace %b, %f and %o with appropriate
1729 ;; values in each command before applying it. Output is
1730 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1731 ((consp org-texinfo-info-process)
1732 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1733 (mapc
1734 (lambda (command)
1735 (shell-command
1736 (replace-regexp-in-string
1737 "%b" (shell-quote-argument base-name)
1738 (replace-regexp-in-string
1739 "%f" (shell-quote-argument full-name)
1740 (replace-regexp-in-string
1741 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1742 outbuf))
1743 org-texinfo-info-process)
1744 ;; Collect standard errors from output buffer.
1745 (setq errors (org-texinfo-collect-errors outbuf))))
1746 (t (error "No valid command to process to Info")))
1747 (let ((infofile (concat out-dir base-name ".info")))
1748 ;; Check for process failure. Provide collected errors if
1749 ;; possible.
1750 (if (not (file-exists-p infofile))
1751 (error (concat (format "INFO file %s wasn't produced" infofile)
1752 (when errors (concat ": " errors))))
1753 ;; Else remove log files, when specified, and signal end of
1754 ;; process to user, along with any error encountered.
1755 (message (concat "Process completed"
1756 (if (not errors) "."
1757 (concat " with errors: " errors)))))
1758 ;; Return output file name.
1759 infofile))))
1761 (defun org-texinfo-collect-errors (buffer)
1762 "Collect some kind of errors from \"makeinfo\" command output.
1764 BUFFER is the buffer containing output.
1766 Return collected error types as a string, or nil if there was
1767 none."
1768 (with-current-buffer buffer
1769 (save-excursion
1770 (goto-char (point-min))
1771 ;; Find final "makeinfo" run.
1772 (when t
1773 (let ((case-fold-search t)
1774 (errors ""))
1775 (when (save-excursion
1776 (re-search-forward "perhaps incorrect sectioning?" nil t))
1777 (setq errors (concat errors " [incorrect sectioning]")))
1778 (when (save-excursion
1779 (re-search-forward "missing close brace" nil t))
1780 (setq errors (concat errors " [syntax error]")))
1781 (when (save-excursion
1782 (re-search-forward "Unknown command" nil t))
1783 (setq errors (concat errors " [undefined @command]")))
1784 (when (save-excursion
1785 (re-search-forward "No matching @end" nil t))
1786 (setq errors (concat errors " [block incomplete]")))
1787 (when (save-excursion
1788 (re-search-forward "requires a sectioning" nil t))
1789 (setq errors (concat errors " [invalid section command]")))
1790 (when (save-excursion
1791 (re-search-forward "\\[unexpected\]" nil t))
1792 (setq errors (concat errors " [unexpected error]")))
1793 (when (save-excursion
1794 (re-search-forward "misplaced " nil t))
1795 (setq errors (concat errors " [syntax error]")))
1796 (and (org-string-nw-p errors) (org-trim errors)))))))
1799 (provide 'ox-texinfo)
1801 ;; Local variables:
1802 ;; generated-autoload-file: "org-loaddefs.el"
1803 ;; End:
1805 ;;; ox-texinfo.el ends here