ox-texinfo: Remove spurious blank lines in output
[org-mode/org-kjn.git] / lisp / ox-texinfo.el
blob9669c8a8e96878be739118c2fd649e7934de86db
1 ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
4 ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
5 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. 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 . (lambda (&rest args) ""))
74 (comment-block . (lambda (&rest args) ""))
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)))
135 ;;; User Configurable Variables
137 (defgroup org-export-texinfo nil
138 "Options for exporting Org mode files to Texinfo."
139 :tag "Org Export Texinfo"
140 :version "24.4"
141 :package-version '(Org . "8.0")
142 :group 'org-export)
144 ;;; Preamble
146 (defcustom org-texinfo-filename ""
147 "Default filename for Texinfo output."
148 :group 'org-export-texinfo
149 :type '(string :tag "Export Filename"))
151 (defcustom org-texinfo-coding-system nil
152 "Default document encoding for Texinfo output.
154 If `nil' it will default to `buffer-file-coding-system'."
155 :group 'org-export-texinfo
156 :type 'coding-system)
158 (defcustom org-texinfo-default-class "info"
159 "The default Texinfo class."
160 :group 'org-export-texinfo
161 :type '(string :tag "Texinfo class"))
163 (defcustom org-texinfo-classes
164 '(("info"
165 "\\input texinfo @c -*- texinfo -*-"
166 ("@chapter %s" . "@unnumbered %s")
167 ("@section %s" . "@unnumberedsec %s")
168 ("@subsection %s" . "@unnumberedsubsec %s")
169 ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
170 "Alist of Texinfo classes and associated header and structure.
171 If #+Texinfo_CLASS is set in the buffer, use its value and the
172 associated information. Here is the structure of each cell:
174 \(class-name
175 header-string
176 \(numbered-section . unnumbered-section\)
177 ...\)
179 The sectioning structure
180 ------------------------
182 The sectioning structure of the class is given by the elements
183 following the header string. For each sectioning level, a number
184 of strings is specified. A %s formatter is mandatory in each
185 section string and will be replaced by the title of the section.
187 Instead of a list of sectioning commands, you can also specify
188 a function name. That function will be called with two
189 parameters, the \(reduced) level of the headline, and a predicate
190 non-nil when the headline should be numbered. It must return
191 a format string in which the section title will be added."
192 :group 'org-export-texinfo
193 :type '(repeat
194 (list (string :tag "Texinfo class")
195 (string :tag "Texinfo header")
196 (repeat :tag "Levels" :inline t
197 (choice
198 (cons :tag "Heading"
199 (string :tag " numbered")
200 (string :tag "unnumbered"))
201 (function :tag "Hook computing sectioning"))))))
203 ;;; Headline
205 (defcustom org-texinfo-format-headline-function 'ignore
206 "Function to format headline text.
208 This function will be called with 5 arguments:
209 TODO the todo keyword (string or nil).
210 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
211 PRIORITY the priority of the headline (integer or nil)
212 TEXT the main headline text (string).
213 TAGS the tags as a list of strings (list of strings or nil).
215 The function result will be used in the section format string.
217 As an example, one could set the variable to the following, in
218 order to reproduce the default set-up:
220 \(defun org-texinfo-format-headline (todo todo-type priority text tags)
221 \"Default format function for a headline.\"
222 \(concat (when todo
223 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
224 \(when priority
225 \(format \"\\\\framebox{\\\\#%c} \" priority))
226 text
227 \(when tags
228 \(format \"\\\\hfill{}\\\\textsc{%s}\"
229 \(mapconcat 'identity tags \":\"))))"
230 :group 'org-export-texinfo
231 :type 'function)
233 ;;; Node listing (menu)
235 (defcustom org-texinfo-node-description-column 32
236 "Column at which to start the description in the node listings.
237 If a node title is greater than this length, the description will
238 be placed after the end of the title."
239 :group 'org-export-texinfo
240 :type 'integer)
242 ;;; Footnotes
244 ;; Footnotes are inserted directly
246 ;;; Timestamps
248 (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
249 "A printf format string to be applied to active timestamps."
250 :group 'org-export-texinfo
251 :type 'string)
253 (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
254 "A printf format string to be applied to inactive timestamps."
255 :group 'org-export-texinfo
256 :type 'string)
258 (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
259 "A printf format string to be applied to diary timestamps."
260 :group 'org-export-texinfo
261 :type 'string)
263 ;;; Links
265 (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
266 "Format string for links with unknown path type."
267 :group 'org-export-texinfo
268 :type 'string)
270 ;;; Tables
272 (defcustom org-texinfo-tables-verbatim nil
273 "When non-nil, tables are exported verbatim."
274 :group 'org-export-texinfo
275 :type 'boolean)
277 (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
278 "Format string to display numbers in scientific notation.
279 The format should have \"%s\" twice, for mantissa and exponent
280 \(i.e. \"%s\\\\times10^{%s}\").
282 When nil, no transformation is made."
283 :group 'org-export-texinfo
284 :type '(choice
285 (string :tag "Format string")
286 (const :tag "No formatting" nil)))
288 (defcustom org-texinfo-def-table-markup "@samp"
289 "Default setting for @table environments."
290 :group 'org-export-texinfo
291 :type 'string)
293 ;;; Text markup
295 (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
296 (code . code)
297 (italic . "@emph{%s}")
298 (verbatim . verb)
299 (comment . "@c %s"))
300 "Alist of Texinfo expressions to convert text markup.
302 The key must be a symbol among `bold', `italic' and `comment'.
303 The value is a formatting string to wrap fontified text with.
305 Value can also be set to the following symbols: `verb' and
306 `code'. For the former, Org will use \"@verb\" to
307 create a format string and select a delimiter character that
308 isn't in the string. For the latter, Org will use \"@code\"
309 to typeset and try to protect special characters.
311 If no association can be found for a given markup, text will be
312 returned as-is."
313 :group 'org-export-texinfo
314 :type 'alist
315 :options '(bold code italic verbatim comment))
317 ;;; Drawers
319 (defcustom org-texinfo-format-drawer-function
320 (lambda (name contents) contents)
321 "Function called to format a drawer in Texinfo code.
323 The function must accept two parameters:
324 NAME the drawer name, like \"LOGBOOK\"
325 CONTENTS the contents of the drawer.
327 The function should return the string to be exported.
329 The default function simply returns the value of CONTENTS."
330 :group 'org-export-texinfo
331 :version "24.4"
332 :package-version '(Org . "8.2")
333 :type 'function)
335 ;;; Inlinetasks
337 (defcustom org-texinfo-format-inlinetask-function 'ignore
338 "Function called to format an inlinetask in Texinfo code.
340 The function must accept six parameters:
341 TODO the todo keyword, as a string
342 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
343 PRIORITY the inlinetask priority, as a string
344 NAME the inlinetask name, as a string.
345 TAGS the inlinetask tags, as a list of strings.
346 CONTENTS the contents of the inlinetask, as a string.
348 The function should return the string to be exported.
350 For example, the variable could be set to the following function
351 in order to mimic default behavior:
353 \(defun org-texinfo-format-inlinetask \(todo type priority name tags contents\)
354 \"Format an inline task element for Texinfo export.\"
355 \(let ((full-title
356 \(concat
357 \(when todo
358 \(format \"@strong{%s} \" todo))
359 \(when priority (format \"#%c \" priority))
360 title
361 \(when tags
362 \(format \":%s:\"
363 \(mapconcat 'identity tags \":\")))))
364 \(format (concat \"@center %s\n\n\"
365 \"%s\"
366 \"\n\"))
367 full-title contents))"
368 :group 'org-export-texinfo
369 :type 'function)
371 ;;; Src blocks
373 ;; Src Blocks are example blocks, except for LISP
375 ;;; Compilation
377 (defcustom org-texinfo-info-process
378 '("makeinfo %f")
379 "Commands to process a Texinfo file to an INFO file.
380 This is list of strings, each of them will be given to the shell
381 as a command. %f in the command will be replaced by the full
382 file name, %b by the file base name \(i.e without extension) and
383 %o by the base directory of the file."
384 :group 'org-export-texinfo
385 :type '(repeat :tag "Shell command sequence"
386 (string :tag "Shell command")))
388 (defcustom org-texinfo-logfiles-extensions
389 '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
390 "The list of file extensions to consider as Texinfo logfiles.
391 The logfiles will be remove if `org-texinfo-remove-logfiles' is
392 non-nil."
393 :group 'org-export-texinfo
394 :type '(repeat (string :tag "Extension")))
396 (defcustom org-texinfo-remove-logfiles t
397 "Non-nil means remove the logfiles produced by compiling a Texinfo file.
398 By default, logfiles are files with these extensions: .aux, .toc,
399 .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
400 set `org-texinfo-logfiles-extensions'."
401 :group 'org-export-latex
402 :type 'boolean)
405 ;;; Constants
406 (defconst org-texinfo-max-toc-depth 4
407 "Maximum depth for creation of detailed menu listings. Beyond
408 this depth Texinfo will not recognize the nodes and will cause
409 errors. Left as a constant in case this value ever changes.")
411 (defconst org-texinfo-supported-coding-systems
412 '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
413 "List of coding systems supported by Texinfo, as strings.
414 Specified coding system will be matched against these strings.
415 If two strings share the same prefix (e.g. \"ISO-8859-1\" and
416 \"ISO-8859-15\"), the most specific one has to be listed first.")
419 ;;; Internal Functions
421 (defun org-texinfo-filter-section-blank-lines (headline back-end info)
422 "Filter controlling number of blank lines after a section."
423 (let ((blanks (make-string 2 ?\n)))
424 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline)))
426 (defun org-texinfo--find-verb-separator (s)
427 "Return a character not used in string S.
428 This is used to choose a separator for constructs like \\verb."
429 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
430 (loop for c across ll
431 when (not (string-match (regexp-quote (char-to-string c)) s))
432 return (char-to-string c))))
434 (defun org-texinfo--make-option-string (options)
435 "Return a comma separated string of keywords and values.
436 OPTIONS is an alist where the key is the options keyword as
437 a string, and the value a list containing the keyword value, or
438 nil."
439 (mapconcat (lambda (pair)
440 (concat (first pair)
441 (when (> (length (second pair)) 0)
442 (concat "=" (second pair)))))
443 options
444 ","))
446 (defun org-texinfo--text-markup (text markup)
447 "Format TEXT depending on MARKUP text markup.
448 See `org-texinfo-text-markup-alist' for details."
449 (let ((fmt (cdr (assq markup org-texinfo-text-markup-alist))))
450 (cond
451 ;; No format string: Return raw text.
452 ((not fmt) text)
453 ((eq 'verb fmt)
454 (let ((separator (org-texinfo--find-verb-separator text)))
455 (concat "@verb{" separator text separator "}")))
456 ((eq 'code fmt)
457 (let ((start 0)
458 (rtn "")
459 char)
460 (while (string-match "[@{}]" text)
461 (setq char (match-string 0 text))
462 (if (> (match-beginning 0) 0)
463 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
464 (setq text (substring text (1+ (match-beginning 0))))
465 (setq char (concat "@" char)
466 rtn (concat rtn char)))
467 (setq text (concat rtn text)
468 fmt "@code{%s}")
469 (format fmt text)))
470 ;; Else use format string.
471 (t (format fmt text)))))
473 (defun org-texinfo--get-node (headline info)
474 "Return node entry associated to HEADLINE.
475 INFO is a plist used as a communication channel."
476 (let ((menu-title (org-export-get-alt-title headline info)))
477 (org-texinfo--sanitize-menu
478 (replace-regexp-in-string
479 "%" "%%"
480 (if menu-title (org-export-data menu-title info)
481 (org-texinfo--sanitize-headline
482 (org-element-property :title headline) info))))))
484 ;;; Headline sanitizing
486 (defun org-texinfo--sanitize-headline (headline info)
487 "Remove all formatting from the text of a headline for use in
488 node and menu listing."
489 (mapconcat 'identity
490 (org-texinfo--sanitize-headline-contents headline info) " "))
492 (defun org-texinfo--sanitize-headline-contents (headline info)
493 "Retrieve the content of the headline.
495 Any content that can contain further formatting is checked
496 recursively, to ensure that nested content is also properly
497 retrieved."
498 (loop for contents in headline append
499 (cond
500 ;; already a string
501 ((stringp contents)
502 (list (replace-regexp-in-string " $" "" contents)))
503 ;; Is exported as-is (value)
504 ((org-element-map contents '(verbatim code)
505 (lambda (value) (org-element-property :value value)) info))
506 ;; Has content and recurse into the content
507 ((org-element-contents contents)
508 (org-texinfo--sanitize-headline-contents
509 (org-element-contents contents) info)))))
511 ;;; Menu sanitizing
513 (defun org-texinfo--sanitize-menu (title)
514 "Remove invalid characters from TITLE for use in menus and
515 nodes.
517 Based on Texinfo specifications, the following must be removed:
518 @ { } ( ) : . ,"
519 (replace-regexp-in-string "[@{}():,.]" "" title))
521 ;;; Content sanitizing
523 (defun org-texinfo--sanitize-content (text)
524 "Ensure characters are properly escaped when used in headlines or blocks.
526 Escape characters are: @ { }"
527 (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
529 ;;; Menu creation
531 (defun org-texinfo--build-menu (tree level info &optional detailed)
532 "Create the @menu/@end menu information from TREE at headline
533 level LEVEL.
535 TREE contains the parse-tree to work with, either of the entire
536 document or of a specific parent headline. LEVEL indicates what
537 level of headlines to look at when generating the menu. INFO is
538 a plist containing contextual information.
540 Detailed determines whether to build a single level of menu, or
541 recurse into all children as well."
542 (let ((menu (org-texinfo--generate-menu-list tree level info))
543 output text-menu)
544 (cond
545 (detailed
546 ;; Looping is done within the menu generation.
547 (setq text-menu (org-texinfo--generate-detailed menu level info)))
549 (setq text-menu (org-texinfo--generate-menu-items menu info))))
550 (when text-menu
551 (setq output (org-texinfo--format-menu text-menu))
552 (mapconcat 'identity output "\n"))))
554 (defun org-texinfo--generate-detailed (menu level info)
555 "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
557 MENU is the parse-tree to work with. LEVEL is the starting level
558 for the menu headlines and from which recursion occurs. INFO is
559 a plist containing contextual information."
560 (when level
561 (let ((max-depth (min org-texinfo-max-toc-depth
562 (plist-get info :headline-levels))))
563 (when (> max-depth level)
564 (loop for headline in menu append
565 (let* ((title (org-texinfo--menu-headlines headline info))
566 ;; Create list of menu entries for the next level
567 (sublist (org-texinfo--generate-menu-list
568 headline (1+ level) info))
569 ;; Generate the menu items for that level. If
570 ;; there are none omit that heading completely,
571 ;; otherwise join the title to it's related entries.
572 (submenu (if (org-texinfo--generate-menu-items sublist info)
573 (append (list title)
574 (org-texinfo--generate-menu-items sublist info))
575 'nil))
576 ;; Start the process over the next level down.
577 (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
578 (setq recursion (append submenu recursion))
579 recursion))))))
581 (defun org-texinfo--generate-menu-list (tree level info)
582 "Generate the list of headlines that are within a given level
583 of the tree for further formatting.
585 TREE is the parse-tree containing the headlines. LEVEL is the
586 headline level to generate a list of. INFO is a plist holding
587 contextual information."
588 (org-element-map tree 'headline
589 (lambda (head)
590 (and (= (org-export-get-relative-level head info) level)
591 ;; Do not take note of footnotes or copying headlines.
592 (not (org-element-property :COPYING head))
593 (not (org-element-property :footnote-section-p head))
594 ;; Collect headline.
595 head))
596 info))
598 (defun org-texinfo--generate-menu-items (items info)
599 "Generate a list of headline information from the listing ITEMS.
601 ITEMS is a list of the headlines to be converted into entries.
602 INFO is a plist containing contextual information.
604 Returns a list containing the following information from each
605 headline: length, title, description. This is used to format the
606 menu using `org-texinfo--format-menu'."
607 (loop for headline in items collect
608 (let* ((menu-title (org-texinfo--sanitize-menu
609 (org-export-data
610 (org-export-get-alt-title headline info)
611 info)))
612 (title (org-texinfo--sanitize-menu
613 (org-texinfo--sanitize-headline
614 (org-element-property :title headline) info)))
615 (descr (org-export-data
616 (org-element-property :DESCRIPTION headline)
617 info))
618 (menu-entry (if (string= "" menu-title) title menu-title))
619 (len (length menu-entry))
620 (output (list len menu-entry descr)))
621 output)))
623 (defun org-texinfo--menu-headlines (headline info)
624 "Retrieve the title from HEADLINE.
626 INFO is a plist holding contextual information.
628 Return the headline as a list of (length title description) with
629 length of -1 and nil description. This is used in
630 `org-texinfo--format-menu' to identify headlines as opposed to
631 entries."
632 (let ((title (org-export-data
633 (org-element-property :title headline) info)))
634 (list -1 title 'nil)))
636 (defun org-texinfo--format-menu (text-menu)
637 "Format the TEXT-MENU items to be properly printed in the menu.
639 Each entry in the menu should be provided as (length title
640 description).
642 Headlines in the detailed menu are given length -1 to ensure they
643 are never confused with other entries. They also have no
644 description.
646 Other menu items are output as:
647 Title:: description
649 With the spacing between :: and description based on the length
650 of the longest menu entry."
652 (let (output)
653 (setq output
654 (mapcar (lambda (name)
655 (let* ((title (nth 1 name))
656 (desc (nth 2 name))
657 (length (nth 0 name))
658 (column (max
659 ;;6 is "* " ":: " for inserted text
660 length
662 org-texinfo-node-description-column
663 6)))
664 (spacing (- column length)
666 (if (> length -1)
667 (concat "* " title ":: "
668 (make-string spacing ?\s)
669 (if desc
670 (concat desc)))
671 (concat "\n" title "\n"))))
672 text-menu))
673 output))
675 ;;; Template
677 (defun org-texinfo-template (contents info)
678 "Return complete document string after Texinfo conversion.
679 CONTENTS is the transcoded contents string. INFO is a plist
680 holding export options."
681 (let ((title (org-export-data (plist-get info :title) info))
682 (info-filename (or (plist-get info :texinfo-filename)
683 (file-name-nondirectory
684 (org-export-output-file-name ".info"))))
685 (header (nth 1 (assoc (plist-get info :texinfo-class)
686 org-texinfo-classes)))
687 ;; Copying data is the contents of the first headline in
688 ;; parse tree with a non-nil copying property.
689 (copying (org-element-map (plist-get info :parse-tree) 'headline
690 (lambda (hl)
691 (and (org-not-nil (org-element-property :COPYING hl))
692 (org-element-contents hl)))
693 info t)))
694 (concat
695 ;; Header
696 header "\n"
697 "@c %**start of header\n"
698 ;; Filename and Title
699 "@setfilename " info-filename "\n"
700 (format "@settitle %s\n" title)
701 ;; Coding system.
702 (format
703 "@documentencoding %s\n"
704 (catch 'coding-system
705 (let ((case-fold-search t)
706 (name (symbol-name (or org-texinfo-coding-system
707 buffer-file-coding-system))))
708 (dolist (system org-texinfo-supported-coding-systems "UTF-8")
709 (when (org-string-match-p (regexp-quote system) name)
710 (throw 'coding-system system))))))
711 ;; Language.
712 (format "@documentlanguage %s\n" (plist-get info :language))
713 ;; Additional header options set by #+TEXINFO_HEADER.
714 (let ((texinfo-header (plist-get info :texinfo-header)))
715 (and texinfo-header (org-element-normalize-string texinfo-header)))
716 "@c %**end of header\n\n"
717 ;; Additional options set by #+TEXINFO_POST_HEADER.
718 (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
719 (and texinfo-post-header
720 (org-element-normalize-string texinfo-post-header)))
721 ;; Copying.
722 (and copying
723 (format "@copying\n%s@end copying\n\n"
724 (org-element-normalize-string
725 (org-export-data copying info))))
726 ;; Info directory information. Only supply if both title and
727 ;; category are provided.
728 (let ((dircat (plist-get info :texinfo-dircat))
729 (dirtitle
730 (let ((title (plist-get info :texinfo-dirtitle)))
731 (and title
732 (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
733 (format "* %s." (match-string 1 title))))))
734 (when (and dircat dirtitle)
735 (concat "@dircategory " dircat "\n"
736 "@direntry\n"
737 (let ((dirdesc
738 (let ((desc (plist-get info :texinfo-dirdesc)))
739 (cond ((not desc) nil)
740 ((org-string-match-p "\\.$" desc) desc)
741 (t (concat desc "."))))))
742 (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
743 "\n"
744 "@end direntry\n\n")))
745 ;; Title
746 "@finalout\n"
747 "@titlepage\n"
748 "@title " title "\n"
749 (let ((subtitle (plist-get info :subtitle)))
750 (and subtitle
751 (org-element-normalize-string
752 (replace-regexp-in-string "^" "@subtitle " subtitle))))
753 (when (plist-get info :with-author)
754 (concat
755 ;; Primary author.
756 (let ((author (org-string-nw-p
757 (org-export-data (plist-get info :author) info)))
758 (email (and (plist-get info :with-email)
759 (org-string-nw-p
760 (org-export-data (plist-get info :email) info)))))
761 (cond ((and author email)
762 (format "@author %s (@email{%s})\n" author email))
763 (author (format "@author %s\n" author))
764 (email (format "@author @email{%s}\n" email))))
765 ;; Other authors.
766 (let ((subauthor (plist-get info :subauthor)))
767 (and subauthor
768 (org-element-normalize-string
769 (replace-regexp-in-string "^" "@author " subauthor))))))
770 (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
771 "@end titlepage\n\n"
772 ;; Table of contents.
773 (and (plist-get info :with-toc) "@contents\n\n")
774 ;; Configure Top Node when not for Tex
775 "@ifnottex\n"
776 "@node Top\n"
777 "@top " title " Manual\n"
778 (and copying "@insertcopying\n")
779 "@end ifnottex\n\n"
780 ;; Menu.
781 (let ((menu (org-texinfo-make-menu info 'main))
782 (detail-menu (org-texinfo-make-menu info 'detailed)))
783 (and menu
784 (concat "@menu\n"
785 menu "\n"
786 (and detail-menu
787 (concat "\n@detailmenu\n"
788 " --- The Detailed Node Listing ---\n"
789 detail-menu "\n"
790 "@end detailmenu\n"))
791 "@end menu\n\n")))
792 ;; Document's body.
793 contents "\n"
794 ;; Creator.
795 (case (plist-get info :with-creator)
796 ((nil) nil)
797 (comment (format "@c %s\n" (plist-get info :creator)))
798 (otherwise (concat (plist-get info :creator) "\n")))
799 ;; Document end.
800 "@bye")))
804 ;;; Transcode Functions
806 ;;; Bold
808 (defun org-texinfo-bold (bold contents info)
809 "Transcode BOLD from Org to Texinfo.
810 CONTENTS is the text with bold markup. INFO is a plist holding
811 contextual information."
812 (org-texinfo--text-markup contents 'bold))
814 ;;; Center Block
816 (defun org-texinfo-center-block (center-block contents info)
817 "Transcode a CENTER-BLOCK element from Org to Texinfo.
818 CONTENTS holds the contents of the block. INFO is a plist used
819 as a communication channel."
820 contents)
822 ;;; Clock
824 (defun org-texinfo-clock (clock contents info)
825 "Transcode a CLOCK element from Org to Texinfo.
826 CONTENTS is nil. INFO is a plist holding contextual
827 information."
828 (concat
829 "@noindent"
830 (format "@strong{%s} " org-clock-string)
831 (format org-texinfo-inactive-timestamp-format
832 (concat (org-translate-time
833 (org-element-property :raw-value
834 (org-element-property :value clock)))
835 (let ((time (org-element-property :duration clock)))
836 (and time (format " (%s)" time)))))
837 "@*"))
839 ;;; Code
841 (defun org-texinfo-code (code contents info)
842 "Transcode a CODE object from Org to Texinfo.
843 CONTENTS is nil. INFO is a plist used as a communication
844 channel."
845 (org-texinfo--text-markup (org-element-property :value code) 'code))
847 ;;; Drawer
849 (defun org-texinfo-drawer (drawer contents info)
850 "Transcode a DRAWER element from Org to Texinfo.
851 CONTENTS holds the contents of the block. INFO is a plist
852 holding contextual information."
853 (let* ((name (org-element-property :drawer-name drawer))
854 (output (funcall org-texinfo-format-drawer-function
855 name contents)))
856 output))
858 ;;; Dynamic Block
860 (defun org-texinfo-dynamic-block (dynamic-block contents info)
861 "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
862 CONTENTS holds the contents of the block. INFO is a plist
863 holding contextual information. See `org-export-data'."
864 contents)
866 ;;; Entity
868 (defun org-texinfo-entity (entity contents info)
869 "Transcode an ENTITY object from Org to Texinfo.
870 CONTENTS are the definition itself. INFO is a plist holding
871 contextual information."
872 (let ((ent (org-element-property :latex entity)))
873 (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
875 ;;; Example Block
877 (defun org-texinfo-example-block (example-block contents info)
878 "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
879 CONTENTS is nil. INFO is a plist holding contextual
880 information."
881 (format "@verbatim\n%s@end verbatim"
882 (org-export-format-code-default example-block info)))
884 ;;; Export Block
886 (defun org-texinfo-export-block (export-block contents info)
887 "Transcode a EXPORT-BLOCK element from Org to Texinfo.
888 CONTENTS is nil. INFO is a plist holding contextual information."
889 (when (string= (org-element-property :type export-block) "TEXINFO")
890 (org-remove-indentation (org-element-property :value export-block))))
892 ;;; Export Snippet
894 (defun org-texinfo-export-snippet (export-snippet contents info)
895 "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
896 CONTENTS is nil. INFO is a plist holding contextual information."
897 (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
898 (org-element-property :value export-snippet)))
900 ;;; Fixed Width
902 (defun org-texinfo-fixed-width (fixed-width contents info)
903 "Transcode a FIXED-WIDTH element from Org to Texinfo.
904 CONTENTS is nil. INFO is a plist holding contextual information."
905 (format "@example\n%s\n@end example"
906 (org-remove-indentation
907 (org-texinfo--sanitize-content
908 (org-element-property :value fixed-width)))))
910 ;;; Footnote Reference
913 (defun org-texinfo-footnote-reference (footnote contents info)
914 "Create a footnote reference for FOOTNOTE.
916 FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
917 plist holding contextual information."
918 (let ((def (org-export-get-footnote-definition footnote info)))
919 (format "@footnote{%s}"
920 (org-trim (org-export-data def info)))))
922 ;;; Headline
924 (defun org-texinfo-headline (headline contents info)
925 "Transcode a HEADLINE element from Org to Texinfo.
926 CONTENTS holds the contents of the headline. INFO is a plist
927 holding contextual information."
928 (let* ((class (plist-get info :texinfo-class))
929 (level (org-export-get-relative-level headline info))
930 (numberedp (org-export-numbered-headline-p headline info))
931 (class-sectioning (assoc class org-texinfo-classes))
932 ;; Find the index type, if any
933 (index (org-element-property :INDEX headline))
934 ;; Check if it is an appendix
935 (appendix (org-element-property :APPENDIX headline))
936 ;; Retrieve headline text
937 (text (org-texinfo--sanitize-headline
938 (org-element-property :title headline) info))
939 ;; Create node info, to insert it before section formatting.
940 ;; Use custom menu title if present
941 (node (format "@node %s\n" (org-texinfo--get-node headline info)))
942 ;; Menus must be generated with first child, otherwise they
943 ;; will not nest properly
944 (menu (let* ((first (org-export-first-sibling-p headline info))
945 (parent (org-export-get-parent-headline headline))
946 (title (org-texinfo--sanitize-headline
947 (org-element-property :title parent) info))
948 heading listing
949 (tree (plist-get info :parse-tree)))
950 (if first
951 (org-element-map (plist-get info :parse-tree) 'headline
952 (lambda (ref)
953 (if (member title (org-element-property :title ref))
954 (push ref heading)))
955 info t))
956 (setq listing (org-texinfo--build-menu
957 (car heading) level info))
958 (if listing
959 (setq listing (replace-regexp-in-string
960 "%" "%%" listing)
961 listing (format
962 "\n@menu\n%s\n@end menu\n\n" listing))
963 'nil)))
964 ;; Section formatting will set two placeholders: one for the
965 ;; title and the other for the contents.
966 (section-fmt
967 (let ((sec (if (and (symbolp (nth 2 class-sectioning))
968 (fboundp (nth 2 class-sectioning)))
969 (funcall (nth 2 class-sectioning) level numberedp)
970 (nth (1+ level) class-sectioning))))
971 (cond
972 ;; No section available for that LEVEL.
973 ((not sec) nil)
974 ;; Section format directly returned by a function.
975 ((stringp sec) sec)
976 ;; (numbered-section . unnumbered-section)
977 ((not (consp (cdr sec)))
978 (cond
979 ;;If an index, always unnumbered
980 (index
981 (concat menu node (cdr sec) "\n%s"))
982 (appendix
983 (concat menu node (replace-regexp-in-string
984 "unnumbered"
985 "appendix"
986 (cdr sec)) "\n%s"))
987 ;; Otherwise number as needed.
989 (concat menu node
990 (funcall
991 (if numberedp #'car #'cdr) sec) "\n%s")))))))
992 (todo
993 (and (plist-get info :with-todo-keywords)
994 (let ((todo (org-element-property :todo-keyword headline)))
995 (and todo (org-export-data todo info)))))
996 (todo-type (and todo (org-element-property :todo-type headline)))
997 (tags (and (plist-get info :with-tags)
998 (org-export-get-tags headline info)))
999 (priority (and (plist-get info :with-priority)
1000 (org-element-property :priority headline)))
1001 ;; Create the headline text along with a no-tag version. The
1002 ;; latter is required to remove tags from table of contents.
1003 (full-text (org-texinfo--sanitize-content
1004 (if (not (eq org-texinfo-format-headline-function 'ignore))
1005 ;; User-defined formatting function.
1006 (funcall org-texinfo-format-headline-function
1007 todo todo-type priority text tags)
1008 ;; Default formatting.
1009 (concat
1010 (when todo
1011 (format "@strong{%s} " todo))
1012 (when priority (format "@emph{#%s} " priority))
1013 text
1014 (when tags
1015 (format " :%s:"
1016 (mapconcat 'identity tags ":")))))))
1017 (full-text-no-tag
1018 (org-texinfo--sanitize-content
1019 (if (not (eq org-texinfo-format-headline-function 'ignore))
1020 ;; User-defined formatting function.
1021 (funcall org-texinfo-format-headline-function
1022 todo todo-type priority text nil)
1023 ;; Default formatting.
1024 (concat
1025 (when todo (format "@strong{%s} " todo))
1026 (when priority (format "@emph{#%c} " priority))
1027 text))))
1028 (pre-blanks
1029 (make-string (org-element-property :pre-blank headline) 10)))
1030 (cond
1031 ;; Case 1: This is a footnote section: ignore it.
1032 ((org-element-property :footnote-section-p headline) nil)
1033 ;; Case 2: This is the `copying' section: ignore it
1034 ;; This is used elsewhere.
1035 ((org-element-property :COPYING headline) nil)
1036 ;; Case 3: An index. If it matches one of the known indexes,
1037 ;; print it as such following the contents, otherwise
1038 ;; print the contents and leave the index up to the user.
1039 (index
1040 (format
1041 section-fmt full-text
1042 (concat pre-blanks contents "\n"
1043 (if (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
1044 (concat "@printindex " index)))))
1045 ;; Case 4: This is a deep sub-tree: export it as a list item.
1046 ;; Also export as items headlines for which no section
1047 ;; format has been found.
1048 ((or (not section-fmt) (org-export-low-level-p headline info))
1049 ;; Build the real contents of the sub-tree.
1050 (let ((low-level-body
1051 (concat
1052 ;; If the headline is the first sibling, start a list.
1053 (when (org-export-first-sibling-p headline info)
1054 (format "@%s\n" (if numberedp 'enumerate 'itemize)))
1055 ;; Itemize headline
1056 "@item\n" full-text "\n" pre-blanks contents)))
1057 ;; If headline is not the last sibling simply return
1058 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1059 ;; blank line.
1060 (if (not (org-export-last-sibling-p headline info)) low-level-body
1061 (replace-regexp-in-string
1062 "[ \t\n]*\\'"
1063 (format "\n@end %s" (if numberedp 'enumerate 'itemize))
1064 low-level-body))))
1065 ;; Case 5: Standard headline. Export it as a section.
1067 (cond
1068 ((not (and tags (eq (plist-get info :with-tags) 'not-in-toc)))
1069 ;; Regular section. Use specified format string.
1070 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1071 (concat pre-blanks contents)))
1072 ((string-match "\\`@\\(.*?\\){" section-fmt)
1073 ;; If tags should be removed from table of contents, insert
1074 ;; title without tags as an alternative heading in sectioning
1075 ;; command.
1076 (format (replace-match (concat (match-string 1 section-fmt) "[%s]")
1077 nil nil section-fmt 1)
1078 ;; Replace square brackets with parenthesis since
1079 ;; square brackets are not supported in optional
1080 ;; arguments.
1081 (replace-regexp-in-string
1082 "\\[" "("
1083 (replace-regexp-in-string
1084 "\\]" ")"
1085 full-text-no-tag))
1086 full-text
1087 (concat pre-blanks contents)))
1089 ;; Impossible to add an alternative heading. Fallback to
1090 ;; regular sectioning format string.
1091 (format (replace-regexp-in-string "%]" "%%]" section-fmt) full-text
1092 (concat pre-blanks contents))))))))
1094 ;;; Inline Src Block
1096 (defun org-texinfo-inline-src-block (inline-src-block contents info)
1097 "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
1098 CONTENTS holds the contents of the item. INFO is a plist holding
1099 contextual information."
1100 (let* ((code (org-element-property :value inline-src-block))
1101 (separator (org-texinfo--find-verb-separator code)))
1102 (concat "@verb{" separator code separator "}")))
1104 ;;; Inlinetask
1106 (defun org-texinfo-inlinetask (inlinetask contents info)
1107 "Transcode an INLINETASK element from Org to Texinfo.
1108 CONTENTS holds the contents of the block. INFO is a plist
1109 holding contextual information."
1110 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1111 (todo (and (plist-get info :with-todo-keywords)
1112 (let ((todo (org-element-property :todo-keyword inlinetask)))
1113 (and todo (org-export-data todo info)))))
1114 (todo-type (org-element-property :todo-type inlinetask))
1115 (tags (and (plist-get info :with-tags)
1116 (org-export-get-tags inlinetask info)))
1117 (priority (and (plist-get info :with-priority)
1118 (org-element-property :priority inlinetask))))
1119 ;; If `org-texinfo-format-inlinetask-function' is provided, call it
1120 ;; with appropriate arguments.
1121 (if (not (eq org-texinfo-format-inlinetask-function 'ignore))
1122 (funcall org-texinfo-format-inlinetask-function
1123 todo todo-type priority title tags contents)
1124 ;; Otherwise, use a default template.
1125 (let ((full-title
1126 (concat
1127 (when todo (format "@strong{%s} " todo))
1128 (when priority (format "#%c " priority))
1129 title
1130 (when tags (format ":%s:"
1131 (mapconcat 'identity tags ":"))))))
1132 (format (concat "@center %s\n\n"
1133 "%s"
1134 "\n")
1135 full-title contents)))))
1137 ;;; Italic
1139 (defun org-texinfo-italic (italic contents info)
1140 "Transcode ITALIC from Org to Texinfo.
1141 CONTENTS is the text with italic markup. INFO is a plist holding
1142 contextual information."
1143 (org-texinfo--text-markup contents 'italic))
1145 ;;; Item
1147 (defun org-texinfo-item (item contents info)
1148 "Transcode an ITEM element from Org to Texinfo.
1149 CONTENTS holds the contents of the item. INFO is a plist holding
1150 contextual information."
1151 (let* ((tag (org-element-property :tag item))
1152 (desc (org-export-data tag info)))
1153 (concat "\n@item " (if tag desc) "\n"
1154 (and contents (org-trim contents)) "\n")))
1156 ;;; Keyword
1158 (defun org-texinfo-keyword (keyword contents info)
1159 "Transcode a KEYWORD element from Org to Texinfo.
1160 CONTENTS is nil. INFO is a plist holding contextual information."
1161 (let ((key (org-element-property :key keyword))
1162 (value (org-element-property :value keyword)))
1163 (cond
1164 ((string= key "TEXINFO") value)
1165 ((string= key "CINDEX") (format "@cindex %s" value))
1166 ((string= key "FINDEX") (format "@findex %s" value))
1167 ((string= key "KINDEX") (format "@kindex %s" value))
1168 ((string= key "PINDEX") (format "@pindex %s" value))
1169 ((string= key "TINDEX") (format "@tindex %s" value))
1170 ((string= key "VINDEX") (format "@vindex %s" value)))))
1172 ;;; Line Break
1174 (defun org-texinfo-line-break (line-break contents info)
1175 "Transcode a LINE-BREAK object from Org to Texinfo.
1176 CONTENTS is nil. INFO is a plist holding contextual information."
1177 "@*\n")
1179 ;;; Link
1181 (defun org-texinfo-link (link desc info)
1182 "Transcode a LINK object from Org to Texinfo.
1184 DESC is the description part of the link, or the empty string.
1185 INFO is a plist holding contextual information. See
1186 `org-export-data'."
1187 (let* ((type (org-element-property :type link))
1188 (raw-path (org-element-property :path link))
1189 ;; Ensure DESC really exists, or set it to nil.
1190 (desc (and (not (string= desc "")) desc))
1191 (path (cond
1192 ((member type '("http" "https" "ftp"))
1193 (concat type ":" raw-path))
1194 ((and (string= type "file") (file-name-absolute-p raw-path))
1195 (concat "file:" raw-path))
1196 (t raw-path)))
1197 (email (if (string= type "mailto")
1198 (let ((text (replace-regexp-in-string
1199 "@" "@@" raw-path)))
1200 (concat text (if desc (concat "," desc))))))
1201 protocol)
1202 (cond
1203 ;; Links pointing to a headline: Find destination and build
1204 ;; appropriate referencing command.
1205 ((member type '("custom-id" "id"))
1206 (let ((destination (org-export-resolve-id-link link info)))
1207 (case (org-element-type destination)
1208 ;; Id link points to an external file.
1209 (plain-text
1210 (if desc (format "@uref{file://%s,%s}" destination desc)
1211 (format "@uref{file://%s}" destination)))
1212 ;; LINK points to a headline. Use the headline as the NODE target
1213 (headline
1214 (format "@ref{%s,%s}"
1215 (org-texinfo--get-node destination info)
1216 (or desc "")))
1217 (otherwise
1218 (let ((path (org-export-solidify-link-text path)))
1219 (if (not desc) (format "@ref{%s}" path)
1220 (format "@ref{%s,,%s}" path desc)))))))
1221 ((member type '("info"))
1222 (let* ((info-path (split-string path "[:#]"))
1223 (info-manual (car info-path))
1224 (info-node (or (cadr info-path) "top"))
1225 (title (or desc "")))
1226 (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
1227 ((member type '("fuzzy"))
1228 (let ((destination (org-export-resolve-fuzzy-link link info)))
1229 (case (org-element-type destination)
1230 ;; Id link points to an external file.
1231 (plain-text
1232 (if desc (format "@uref{file://%s,%s}" destination desc)
1233 (format "@uref{file://%s}" destination)))
1234 ;; LINK points to a headline. Use the headline as the NODE target
1235 (headline
1236 (format "@ref{%s,%s}"
1237 (org-texinfo--get-node destination info)
1238 (or desc "")))
1239 (otherwise
1240 (let ((path (org-export-solidify-link-text path)))
1241 (if (not desc) (format "@ref{%s}" path)
1242 (format "@ref{%s,,%s}" path desc)))))))
1243 ;; Special case for email addresses
1244 (email
1245 (format "@email{%s}" email))
1246 ;; External link with a description part.
1247 ((and path desc) (format "@uref{%s,%s}" path desc))
1248 ;; External link without a description part.
1249 (path (format "@uref{%s}" path))
1250 ;; No path, only description. Try to do something useful.
1251 (t (format org-texinfo-link-with-unknown-path-format desc)))))
1254 ;;; Menu
1256 (defun org-texinfo-make-menu (info level)
1257 "Create the menu for inclusion in the texifo document.
1259 INFO is the parsed buffer that contains the headlines. LEVEL
1260 determines whether to make the main menu, or the detailed menu.
1262 This is only used for generating the primary menu. In-Node menus
1263 are generated directly."
1264 (let ((parse (plist-get info :parse-tree)))
1265 (cond
1266 ;; Generate the main menu
1267 ((eq level 'main) (org-texinfo--build-menu parse 1 info))
1268 ;; Generate the detailed (recursive) menu
1269 ((eq level 'detailed)
1270 ;; Requires recursion
1271 ;;(org-texinfo--build-detailed-menu parse top info)
1272 (org-texinfo--build-menu parse 1 info 'detailed)))))
1274 ;;; Paragraph
1276 (defun org-texinfo-paragraph (paragraph contents info)
1277 "Transcode a PARAGRAPH element from Org to Texinfo.
1278 CONTENTS is the contents of the paragraph, as a string. INFO is
1279 the plist used as a communication channel."
1280 contents)
1282 ;;; Plain List
1284 (defun org-texinfo-plain-list (plain-list contents info)
1285 "Transcode a PLAIN-LIST element from Org to Texinfo.
1286 CONTENTS is the contents of the list. INFO is a plist holding
1287 contextual information."
1288 (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
1289 (indic (or (plist-get attr :indic)
1290 org-texinfo-def-table-markup))
1291 (type (org-element-property :type plain-list))
1292 (table-type (plist-get attr :table-type))
1293 ;; Ensure valid texinfo table type.
1294 (table-type (if (member table-type '("ftable" "vtable")) table-type
1295 "table"))
1296 (list-type (cond
1297 ((eq type 'ordered) "enumerate")
1298 ((eq type 'unordered) "itemize")
1299 ((eq type 'descriptive) table-type))))
1300 (format "@%s%s\n@end %s"
1301 (if (eq type 'descriptive)
1302 (concat list-type " " indic)
1303 list-type)
1304 contents
1305 list-type)))
1307 ;;; Plain Text
1309 (defun org-texinfo-plain-text (text info)
1310 "Transcode a TEXT string from Org to Texinfo.
1311 TEXT is the string to transcode. INFO is a plist holding
1312 contextual information."
1313 ;; First protect @, { and }.
1314 (let ((output (org-texinfo--sanitize-content text)))
1315 ;; Activate smart quotes. Be sure to provide original TEXT string
1316 ;; since OUTPUT may have been modified.
1317 (when (plist-get info :with-smart-quotes)
1318 (setq output
1319 (org-export-activate-smart-quotes output :texinfo info text)))
1320 ;; LaTeX into @LaTeX{} and TeX into @TeX{}
1321 (let ((case-fold-search nil)
1322 (start 0))
1323 (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
1324 (setq output (replace-match
1325 (format "@%s{}" (match-string 1 output)) nil t output)
1326 start (match-end 0))))
1327 ;; Convert special strings.
1328 (when (plist-get info :with-special-strings)
1329 (while (string-match (regexp-quote "...") output)
1330 (setq output (replace-match "@dots{}" nil t output))))
1331 ;; Handle break preservation if required.
1332 (when (plist-get info :preserve-breaks)
1333 (setq output (replace-regexp-in-string
1334 "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
1335 ;; Return value.
1336 output))
1338 ;;; Planning
1340 (defun org-texinfo-planning (planning contents info)
1341 "Transcode a PLANNING element from Org to Texinfo.
1342 CONTENTS is nil. INFO is a plist holding contextual
1343 information."
1344 (concat
1345 "@noindent"
1346 (mapconcat
1347 'identity
1348 (delq nil
1349 (list
1350 (let ((closed (org-element-property :closed planning)))
1351 (when closed
1352 (concat
1353 (format "@strong{%s} " org-closed-string)
1354 (format org-texinfo-inactive-timestamp-format
1355 (org-translate-time
1356 (org-element-property :raw-value closed))))))
1357 (let ((deadline (org-element-property :deadline planning)))
1358 (when deadline
1359 (concat
1360 (format "@strong{%s} " org-deadline-string)
1361 (format org-texinfo-active-timestamp-format
1362 (org-translate-time
1363 (org-element-property :raw-value deadline))))))
1364 (let ((scheduled (org-element-property :scheduled planning)))
1365 (when scheduled
1366 (concat
1367 (format "@strong{%s} " org-scheduled-string)
1368 (format org-texinfo-active-timestamp-format
1369 (org-translate-time
1370 (org-element-property :raw-value scheduled))))))))
1371 " ")
1372 "@*"))
1374 ;;; Property Drawer
1376 (defun org-texinfo-property-drawer (property-drawer contents info)
1377 "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
1378 CONTENTS is nil. INFO is a plist holding contextual
1379 information."
1380 ;; The property drawer isn't exported but we want separating blank
1381 ;; lines nonetheless.
1384 ;;; Quote Block
1386 (defun org-texinfo-quote-block (quote-block contents info)
1387 "Transcode a QUOTE-BLOCK element from Org to Texinfo.
1388 CONTENTS holds the contents of the block. INFO is a plist
1389 holding contextual information."
1390 (let* ((title (org-element-property :name quote-block))
1391 (start-quote (concat "@quotation"
1392 (if title
1393 (format " %s" title)))))
1394 (format "%s\n%s@end quotation" start-quote contents)))
1396 ;;; Quote Section
1398 (defun org-texinfo-quote-section (quote-section contents info)
1399 "Transcode a QUOTE-SECTION element from Org to Texinfo.
1400 CONTENTS is nil. INFO is a plist holding contextual information."
1401 (let ((value (org-remove-indentation
1402 (org-element-property :value quote-section))))
1403 (when value (format "@verbatim\n%s@end verbatim" value))))
1405 ;;; Radio Target
1407 (defun org-texinfo-radio-target (radio-target text info)
1408 "Transcode a RADIO-TARGET object from Org to Texinfo.
1409 TEXT is the text of the target. INFO is a plist holding
1410 contextual information."
1411 (format "@anchor{%s}%s"
1412 (org-export-solidify-link-text
1413 (org-element-property :value radio-target))
1414 text))
1416 ;;; Section
1418 (defun org-texinfo-section (section contents info)
1419 "Transcode a SECTION element from Org to Texinfo.
1420 CONTENTS holds the contents of the section. INFO is a plist
1421 holding contextual information."
1422 contents)
1424 ;;; Special Block
1426 (defun org-texinfo-special-block (special-block contents info)
1427 "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
1428 CONTENTS holds the contents of the block. INFO is a plist used
1429 as a communication channel."
1430 contents)
1432 ;;; Src Block
1434 (defun org-texinfo-src-block (src-block contents info)
1435 "Transcode a SRC-BLOCK element from Org to Texinfo.
1436 CONTENTS holds the contents of the item. INFO is a plist holding
1437 contextual information."
1438 (let* ((lang (org-element-property :language src-block))
1439 (lisp-p (string-match-p "lisp" lang))
1440 (src-contents (org-texinfo--sanitize-content
1441 (org-export-format-code-default src-block info))))
1442 (cond
1443 ;; Case 1. Lisp Block
1444 (lisp-p
1445 (format "@lisp\n%s@end lisp"
1446 src-contents))
1447 ;; Case 2. Other blocks
1449 (format "@example\n%s@end example"
1450 src-contents)))))
1452 ;;; Statistics Cookie
1454 (defun org-texinfo-statistics-cookie (statistics-cookie contents info)
1455 "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
1456 CONTENTS is nil. INFO is a plist holding contextual information."
1457 (org-element-property :value statistics-cookie))
1459 ;;; Subscript
1461 (defun org-texinfo-subscript (subscript contents info)
1462 "Transcode a SUBSCRIPT object from Org to Texinfo.
1463 CONTENTS is the contents of the object. INFO is a plist holding
1464 contextual information."
1465 (format "@math{_%s}" contents))
1467 ;;; Superscript
1469 (defun org-texinfo-superscript (superscript contents info)
1470 "Transcode a SUPERSCRIPT object from Org to Texinfo.
1471 CONTENTS is the contents of the object. INFO is a plist holding
1472 contextual information."
1473 (format "@math{^%s}" contents))
1475 ;;; Table
1477 ;; `org-texinfo-table' is the entry point for table transcoding. It
1478 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1479 ;; delegates the job to either `org-texinfo-table--table.el-table' or
1480 ;; `org-texinfo-table--org-table' functions, depending of the type of
1481 ;; the table.
1483 ;; `org-texinfo-table--align-string' is a subroutine used to build
1484 ;; alignment string for Org tables.
1486 (defun org-texinfo-table (table contents info)
1487 "Transcode a TABLE element from Org to Texinfo.
1488 CONTENTS is the contents of the table. INFO is a plist holding
1489 contextual information."
1490 (cond
1491 ;; Case 1: verbatim table.
1492 ((or org-texinfo-tables-verbatim
1493 (let ((attr (mapconcat 'identity
1494 (org-element-property :attr_latex table)
1495 " ")))
1496 (and attr (string-match "\\<verbatim\\>" attr))))
1497 (format "@verbatim \n%s\n@end verbatim"
1498 ;; Re-create table, without affiliated keywords.
1499 (org-trim
1500 (org-element-interpret-data
1501 `(table nil ,@(org-element-contents table))))))
1502 ;; Case 2: table.el table. Convert it using appropriate tools.
1503 ((eq (org-element-property :type table) 'table.el)
1504 (org-texinfo-table--table.el-table table contents info))
1505 ;; Case 3: Standard table.
1506 (t (org-texinfo-table--org-table table contents info))))
1508 (defun org-texinfo-table-column-widths (table info)
1509 "Determine the largest table cell in each column to process alignment.
1511 TABLE is the table element to transcode. INFO is a plist used as
1512 a communication channel."
1513 (let* ((rows (org-element-map table 'table-row 'identity info))
1514 (collected (loop for row in rows collect
1515 (org-element-map row 'table-cell 'identity info)))
1516 (number-cells (length (car collected)))
1517 cells counts)
1518 (loop for row in collected do
1519 (push (mapcar (lambda (ref)
1520 (let* ((start (org-element-property :contents-begin ref))
1521 (end (org-element-property :contents-end ref))
1522 (length (- end start)))
1523 length)) row) cells))
1524 (setq cells (org-remove-if 'null cells))
1525 (push (loop for count from 0 to (- number-cells 1) collect
1526 (loop for item in cells collect
1527 (nth count item))) counts)
1528 (mapconcat (lambda (size)
1529 (make-string size ?a)) (mapcar (lambda (ref)
1530 (apply 'max `(,@ref))) (car counts))
1531 "} {")))
1533 (defun org-texinfo-table--org-table (table contents info)
1534 "Return appropriate Texinfo code for an Org table.
1536 TABLE is the table type element to transcode. CONTENTS is its
1537 contents, as a string. INFO is a plist used as a communication
1538 channel.
1540 This function assumes TABLE has `org' as its `:type' attribute."
1541 (let* ((attr (org-export-read-attribute :attr_texinfo table))
1542 (col-width (plist-get attr :columns))
1543 (columns (if col-width
1544 (format "@columnfractions %s"
1545 col-width)
1546 (format "{%s}"
1547 (org-texinfo-table-column-widths
1548 table info)))))
1549 ;; Prepare the final format string for the table.
1550 (cond
1551 ;; Longtable.
1552 ;; Others.
1553 (t (concat
1554 (format "@multitable %s\n%s@end multitable"
1555 columns
1556 contents))))))
1558 (defun org-texinfo-table--table.el-table (table contents info)
1559 "Returns nothing.
1561 Rather than return an invalid table, nothing is returned."
1562 'nil)
1564 ;;; Table Cell
1566 (defun org-texinfo-table-cell (table-cell contents info)
1567 "Transcode a TABLE-CELL element from Org to Texinfo.
1568 CONTENTS is the cell contents. INFO is a plist used as
1569 a communication channel."
1570 (concat (if (and contents
1571 org-texinfo-table-scientific-notation
1572 (string-match orgtbl-exp-regexp contents))
1573 ;; Use appropriate format string for scientific
1574 ;; notation.
1575 (format org-texinfo-table-scientific-notation
1576 (match-string 1 contents)
1577 (match-string 2 contents))
1578 contents)
1579 (when (org-export-get-next-element table-cell info) "\n@tab ")))
1581 ;;; Table Row
1583 (defun org-texinfo-table-row (table-row contents info)
1584 "Transcode a TABLE-ROW element from Org to Texinfo.
1585 CONTENTS is the contents of the row. INFO is a plist used as
1586 a communication channel."
1587 ;; Rules are ignored since table separators are deduced from
1588 ;; borders of the current row.
1589 (when (eq (org-element-property :type table-row) 'standard)
1590 (let ((rowgroup-tag
1591 (cond
1592 ;; Case 1: Belongs to second or subsequent rowgroup.
1593 ((not (= 1 (org-export-table-row-group table-row info)))
1594 "@item ")
1595 ;; Case 2: Row is from first rowgroup. Table has >=1 rowgroups.
1596 ((org-export-table-has-header-p
1597 (org-export-get-parent-table table-row) info)
1598 "@headitem ")
1599 ;; Case 3: Row is from first and only row group.
1600 (t "@item "))))
1601 (when (eq (org-element-property :type table-row) 'standard)
1602 (concat rowgroup-tag contents "\n")))))
1604 ;;; Target
1606 (defun org-texinfo-target (target contents info)
1607 "Transcode a TARGET object from Org to Texinfo.
1608 CONTENTS is nil. INFO is a plist holding contextual
1609 information."
1610 (format "@anchor{%s}"
1611 (org-export-solidify-link-text (org-element-property :value target))))
1613 ;;; Timestamp
1615 (defun org-texinfo-timestamp (timestamp contents info)
1616 "Transcode a TIMESTAMP object from Org to Texinfo.
1617 CONTENTS is nil. INFO is a plist holding contextual
1618 information."
1619 (let ((value (org-texinfo-plain-text
1620 (org-timestamp-translate timestamp) info)))
1621 (case (org-element-property :type timestamp)
1622 ((active active-range)
1623 (format org-texinfo-active-timestamp-format value))
1624 ((inactive inactive-range)
1625 (format org-texinfo-inactive-timestamp-format value))
1626 (t (format org-texinfo-diary-timestamp-format value)))))
1628 ;;; Verbatim
1630 (defun org-texinfo-verbatim (verbatim contents info)
1631 "Transcode a VERBATIM object from Org to Texinfo.
1632 CONTENTS is nil. INFO is a plist used as a communication
1633 channel."
1634 (org-texinfo--text-markup (org-element-property :value verbatim) 'verbatim))
1636 ;;; Verse Block
1638 (defun org-texinfo-verse-block (verse-block contents info)
1639 "Transcode a VERSE-BLOCK element from Org to Texinfo.
1640 CONTENTS is verse block contents. INFO is a plist holding
1641 contextual information."
1642 ;; In a verse environment, add a line break to each newline
1643 ;; character and change each white space at beginning of a line
1644 ;; into a space of 1 em. Also change each blank line with
1645 ;; a vertical space of 1 em.
1646 (progn
1647 (setq contents (replace-regexp-in-string
1648 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
1649 (replace-regexp-in-string
1650 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents)))
1651 (while (string-match "^[ \t]+" contents)
1652 (let ((new-str (format "\\hspace*{%dem}"
1653 (length (match-string 0 contents)))))
1654 (setq contents (replace-match new-str nil t contents))))
1655 (format "\\begin{verse}\n%s\\end{verse}" contents)))
1658 ;;; Interactive functions
1660 (defun org-texinfo-export-to-texinfo
1661 (&optional async subtreep visible-only body-only ext-plist)
1662 "Export current buffer to a Texinfo file.
1664 If narrowing is active in the current buffer, only export its
1665 narrowed part.
1667 If a region is active, export that region.
1669 A non-nil optional argument ASYNC means the process should happen
1670 asynchronously. The resulting file should be accessible through
1671 the `org-export-stack' interface.
1673 When optional argument SUBTREEP is non-nil, export the sub-tree
1674 at point, extracting information from the headline properties
1675 first.
1677 When optional argument VISIBLE-ONLY is non-nil, don't export
1678 contents of hidden elements.
1680 When optional argument BODY-ONLY is non-nil, only write code
1681 between \"\\begin{document}\" and \"\\end{document}\".
1683 EXT-PLIST, when provided, is a property list with external
1684 parameters overriding Org default settings, but still inferior to
1685 file-local settings.
1687 Return output file's name."
1688 (interactive)
1689 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1690 (org-export-coding-system `,org-texinfo-coding-system))
1691 (org-export-to-file 'texinfo outfile
1692 async subtreep visible-only body-only ext-plist)))
1694 (defun org-texinfo-export-to-info
1695 (&optional async subtreep visible-only body-only ext-plist)
1696 "Export current buffer to Texinfo then process through to INFO.
1698 If narrowing is active in the current buffer, only export its
1699 narrowed part.
1701 If a region is active, export that region.
1703 A non-nil optional argument ASYNC means the process should happen
1704 asynchronously. The resulting file should be accessible through
1705 the `org-export-stack' interface.
1707 When optional argument SUBTREEP is non-nil, export the sub-tree
1708 at point, extracting information from the headline properties
1709 first.
1711 When optional argument VISIBLE-ONLY is non-nil, don't export
1712 contents of hidden elements.
1714 When optional argument BODY-ONLY is non-nil, only write code
1715 between \"\\begin{document}\" and \"\\end{document}\".
1717 EXT-PLIST, when provided, is a property list with external
1718 parameters overriding Org default settings, but still inferior to
1719 file-local settings.
1721 When optional argument PUB-DIR is set, use it as the publishing
1722 directory.
1724 Return INFO file's name."
1725 (interactive)
1726 (let ((outfile (org-export-output-file-name ".texi" subtreep))
1727 (org-export-coding-system `,org-texinfo-coding-system))
1728 (org-export-to-file 'texinfo outfile
1729 async subtreep visible-only body-only ext-plist
1730 (lambda (file) (org-texinfo-compile file)))))
1732 ;;;###autoload
1733 (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
1734 "Publish an org file to Texinfo.
1736 FILENAME is the filename of the Org file to be published. PLIST
1737 is the property list for the given project. PUB-DIR is the
1738 publishing directory.
1740 Return output file name."
1741 (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
1743 ;;;###autoload
1744 (defun org-texinfo-convert-region-to-texinfo ()
1745 "Assume the current region has org-mode syntax, and convert it to Texinfo.
1746 This can be used in any buffer. For example, you can write an
1747 itemized list in org-mode syntax in an Texinfo buffer and use
1748 this command to convert it."
1749 (interactive)
1750 (org-export-replace-region-by 'texinfo))
1752 (defun org-texinfo-compile (file)
1753 "Compile a texinfo file.
1755 FILE is the name of the file being compiled. Processing is
1756 done through the command specified in `org-texinfo-info-process'.
1758 Return INFO file name or an error if it couldn't be produced."
1759 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1760 (full-name (file-truename file))
1761 (out-dir (file-name-directory file))
1762 ;; Properly set working directory for compilation.
1763 (default-directory (if (file-name-absolute-p file)
1764 (file-name-directory full-name)
1765 default-directory))
1766 errors)
1767 (message (format "Processing Texinfo file %s..." file))
1768 (save-window-excursion
1769 (cond
1770 ;; A function is provided: Apply it.
1771 ((functionp org-texinfo-info-process)
1772 (funcall org-texinfo-info-process (shell-quote-argument file)))
1773 ;; A list is provided: Replace %b, %f and %o with appropriate
1774 ;; values in each command before applying it. Output is
1775 ;; redirected to "*Org INFO Texinfo Output*" buffer.
1776 ((consp org-texinfo-info-process)
1777 (let ((outbuf (get-buffer-create "*Org INFO Texinfo Output*")))
1778 (mapc
1779 (lambda (command)
1780 (shell-command
1781 (replace-regexp-in-string
1782 "%b" (shell-quote-argument base-name)
1783 (replace-regexp-in-string
1784 "%f" (shell-quote-argument full-name)
1785 (replace-regexp-in-string
1786 "%o" (shell-quote-argument out-dir) command t t) t t) t t)
1787 outbuf))
1788 org-texinfo-info-process)
1789 ;; Collect standard errors from output buffer.
1790 (setq errors (org-texinfo-collect-errors outbuf))))
1791 (t (error "No valid command to process to Info")))
1792 (let ((infofile (concat out-dir base-name ".info")))
1793 ;; Check for process failure. Provide collected errors if
1794 ;; possible.
1795 (if (not (file-exists-p infofile))
1796 (error (concat (format "INFO file %s wasn't produced" infofile)
1797 (when errors (concat ": " errors))))
1798 ;; Else remove log files, when specified, and signal end of
1799 ;; process to user, along with any error encountered.
1800 (when org-texinfo-remove-logfiles
1801 (dolist (ext org-texinfo-logfiles-extensions)
1802 (let ((file (concat out-dir base-name "." ext)))
1803 (when (file-exists-p file) (delete-file file)))))
1804 (message (concat "Process completed"
1805 (if (not errors) "."
1806 (concat " with errors: " errors)))))
1807 ;; Return output file name.
1808 infofile))))
1810 (defun org-texinfo-collect-errors (buffer)
1811 "Collect some kind of errors from \"makeinfo\" command output.
1813 BUFFER is the buffer containing output.
1815 Return collected error types as a string, or nil if there was
1816 none."
1817 (with-current-buffer buffer
1818 (save-excursion
1819 (goto-char (point-min))
1820 ;; Find final "makeinfo" run.
1821 (when t
1822 (let ((case-fold-search t)
1823 (errors ""))
1824 (when (save-excursion
1825 (re-search-forward "perhaps incorrect sectioning?" nil t))
1826 (setq errors (concat errors " [incorrect sectioning]")))
1827 (when (save-excursion
1828 (re-search-forward "missing close brace" nil t))
1829 (setq errors (concat errors " [syntax error]")))
1830 (when (save-excursion
1831 (re-search-forward "Unknown command" nil t))
1832 (setq errors (concat errors " [undefined @command]")))
1833 (when (save-excursion
1834 (re-search-forward "No matching @end" nil t))
1835 (setq errors (concat errors " [block incomplete]")))
1836 (when (save-excursion
1837 (re-search-forward "requires a sectioning" nil t))
1838 (setq errors (concat errors " [invalid section command]")))
1839 (when (save-excursion
1840 (re-search-forward "\\[unexpected\]" nil t))
1841 (setq errors (concat errors " [unexpected error]")))
1842 (when (save-excursion
1843 (re-search-forward "misplaced " nil t))
1844 (setq errors (concat errors " [syntax error]")))
1845 (and (org-string-nw-p errors) (org-trim errors)))))))
1848 (provide 'ox-texinfo)
1850 ;; Local variables:
1851 ;; generated-autoload-file: "org-loaddefs.el"
1852 ;; End:
1854 ;;; ox-texinfo.el ends here