ox-texinfo.el 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
  3. ;; Author: Jonathan Leech-Pepin <jonathan.leechpepin at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; See Org manual for details.
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'ox)
  22. (defvar orgtbl-exp-regexp)
  23. ;;; Define Back-End
  24. (org-export-define-backend 'texinfo
  25. '((bold . org-texinfo-bold)
  26. (center-block . org-texinfo-center-block)
  27. (clock . org-texinfo-clock)
  28. (code . org-texinfo-code)
  29. (drawer . org-texinfo-drawer)
  30. (dynamic-block . org-texinfo-dynamic-block)
  31. (entity . org-texinfo-entity)
  32. (example-block . org-texinfo-example-block)
  33. (export-block . org-texinfo-export-block)
  34. (export-snippet . org-texinfo-export-snippet)
  35. (fixed-width . org-texinfo-fixed-width)
  36. (footnote-definition . org-texinfo-footnote-definition)
  37. (footnote-reference . org-texinfo-footnote-reference)
  38. (headline . org-texinfo-headline)
  39. (inline-src-block . org-texinfo-inline-src-block)
  40. (inlinetask . org-texinfo-inlinetask)
  41. (italic . org-texinfo-italic)
  42. (item . org-texinfo-item)
  43. (keyword . org-texinfo-keyword)
  44. (line-break . org-texinfo-line-break)
  45. (link . org-texinfo-link)
  46. (node-property . org-texinfo-node-property)
  47. (paragraph . org-texinfo-paragraph)
  48. (plain-list . org-texinfo-plain-list)
  49. (plain-text . org-texinfo-plain-text)
  50. (planning . org-texinfo-planning)
  51. (property-drawer . org-texinfo-property-drawer)
  52. (quote-block . org-texinfo-quote-block)
  53. (radio-target . org-texinfo-radio-target)
  54. (section . org-texinfo-section)
  55. (special-block . org-texinfo-special-block)
  56. (src-block . org-texinfo-src-block)
  57. (statistics-cookie . org-texinfo-statistics-cookie)
  58. (strike-through . org-texinfo-strike-through)
  59. (subscript . org-texinfo-subscript)
  60. (superscript . org-texinfo-superscript)
  61. (table . org-texinfo-table)
  62. (table-cell . org-texinfo-table-cell)
  63. (table-row . org-texinfo-table-row)
  64. (target . org-texinfo-target)
  65. (template . org-texinfo-template)
  66. (timestamp . org-texinfo-timestamp)
  67. (underline . org-texinfo-underline)
  68. (verbatim . org-texinfo-verbatim)
  69. (verse-block . org-texinfo-verse-block))
  70. :filters-alist
  71. '((:filter-headline . org-texinfo--filter-section-blank-lines)
  72. (:filter-parse-tree . org-texinfo--normalize-headlines)
  73. (:filter-section . org-texinfo--filter-section-blank-lines))
  74. :menu-entry
  75. '(?i "Export to Texinfo"
  76. ((?t "As TEXI file" org-texinfo-export-to-texinfo)
  77. (?i "As INFO file" org-texinfo-export-to-info)
  78. (?o "As INFO file and open"
  79. (lambda (a s v b)
  80. (if a (org-texinfo-export-to-info t s v b)
  81. (org-open-file (org-texinfo-export-to-info nil s v b)))))))
  82. :options-alist
  83. '((:texinfo-filename "TEXINFO_FILENAME" nil nil t)
  84. (:texinfo-class "TEXINFO_CLASS" nil org-texinfo-default-class t)
  85. (:texinfo-header "TEXINFO_HEADER" nil nil newline)
  86. (:texinfo-post-header "TEXINFO_POST_HEADER" nil nil newline)
  87. (:subtitle "SUBTITLE" nil nil parse)
  88. (:subauthor "SUBAUTHOR" nil nil newline)
  89. (:texinfo-dircat "TEXINFO_DIR_CATEGORY" nil nil t)
  90. (:texinfo-dirtitle "TEXINFO_DIR_TITLE" nil nil t)
  91. (:texinfo-dirdesc "TEXINFO_DIR_DESC" nil nil t)
  92. (:texinfo-printed-title "TEXINFO_PRINTED_TITLE" nil nil t)
  93. ;; Other variables.
  94. (:texinfo-classes nil nil org-texinfo-classes)
  95. (:texinfo-format-headline-function nil nil org-texinfo-format-headline-function)
  96. (:texinfo-node-description-column nil nil org-texinfo-node-description-column)
  97. (:texinfo-active-timestamp-format nil nil org-texinfo-active-timestamp-format)
  98. (:texinfo-inactive-timestamp-format nil nil org-texinfo-inactive-timestamp-format)
  99. (:texinfo-diary-timestamp-format nil nil org-texinfo-diary-timestamp-format)
  100. (:texinfo-link-with-unknown-path-format nil nil org-texinfo-link-with-unknown-path-format)
  101. (:texinfo-tables-verbatim nil nil org-texinfo-tables-verbatim)
  102. (:texinfo-table-scientific-notation nil nil org-texinfo-table-scientific-notation)
  103. (:texinfo-def-table-markup nil nil org-texinfo-def-table-markup)
  104. (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
  105. (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
  106. (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)))
  107. ;;; User Configurable Variables
  108. (defgroup org-export-texinfo nil
  109. "Options for exporting Org mode files to Texinfo."
  110. :tag "Org Export Texinfo"
  111. :version "24.4"
  112. :package-version '(Org . "8.0")
  113. :group 'org-export)
  114. ;;;; Preamble
  115. (defcustom org-texinfo-coding-system nil
  116. "Default document encoding for Texinfo output.
  117. If nil it will default to `buffer-file-coding-system'."
  118. :group 'org-export-texinfo
  119. :type 'coding-system)
  120. (defcustom org-texinfo-default-class "info"
  121. "The default Texinfo class."
  122. :group 'org-export-texinfo
  123. :type '(string :tag "Texinfo class"))
  124. (defcustom org-texinfo-classes
  125. '(("info"
  126. "@documentencoding AUTO\n@documentlanguage AUTO"
  127. ("@chapter %s" . "@unnumbered %s")
  128. ("@section %s" . "@unnumberedsec %s")
  129. ("@subsection %s" . "@unnumberedsubsec %s")
  130. ("@subsubsection %s" . "@unnumberedsubsubsec %s")))
  131. "Alist of Texinfo classes and associated header and structure.
  132. If #+TEXINFO_CLASS is set in the buffer, use its value and the
  133. associated information. Here is the structure of each cell:
  134. (class-name
  135. header-string
  136. (numbered-section . unnumbered-section)
  137. ...)
  138. The header string
  139. -----------------
  140. The header string is inserted in the header of the generated
  141. document, right after \"@setfilename\" and \"@settitle\"
  142. commands.
  143. If it contains the special string
  144. \"@documentencoding AUTO\"
  145. \"AUTO\" will be replaced with an appropriate coding system. See
  146. `org-texinfo-coding-system' for more information. Likewise, if
  147. the string contains the special string
  148. \"@documentlanguage AUTO\"
  149. \"AUTO\" will be replaced with the language defined in the
  150. buffer, through #+LANGUAGE keyword, or globally, with
  151. `org-export-default-language', which see.
  152. The sectioning structure
  153. ------------------------
  154. The sectioning structure of the class is given by the elements
  155. following the header string. For each sectioning level, a number
  156. of strings is specified. A %s formatter is mandatory in each
  157. section string and will be replaced by the title of the section.
  158. Instead of a list of sectioning commands, you can also specify
  159. a function name. That function will be called with two
  160. parameters, the reduced) level of the headline, and a predicate
  161. non-nil when the headline should be numbered. It must return
  162. a format string in which the section title will be added."
  163. :group 'org-export-texinfo
  164. :version "24.4"
  165. :package-version '(Org . "8.2")
  166. :type '(repeat
  167. (list (string :tag "Texinfo class")
  168. (string :tag "Texinfo header")
  169. (repeat :tag "Levels" :inline t
  170. (choice
  171. (cons :tag "Heading"
  172. (string :tag " numbered")
  173. (string :tag "unnumbered"))
  174. (function :tag "Hook computing sectioning"))))))
  175. ;;;; Headline
  176. (defcustom org-texinfo-format-headline-function
  177. 'org-texinfo-format-headline-default-function
  178. "Function to format headline text.
  179. This function will be called with 5 arguments:
  180. TODO the todo keyword (string or nil).
  181. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  182. PRIORITY the priority of the headline (integer or nil)
  183. TEXT the main headline text (string).
  184. TAGS the tags as a list of strings (list of strings or nil).
  185. The function result will be used in the section format string."
  186. :group 'org-export-texinfo
  187. :type 'function
  188. :version "25.1"
  189. :package-version '(Org . "8.3"))
  190. ;;;; Node listing (menu)
  191. (defcustom org-texinfo-node-description-column 32
  192. "Column at which to start the description in the node listings.
  193. If a node title is greater than this length, the description will
  194. be placed after the end of the title."
  195. :group 'org-export-texinfo
  196. :type 'integer)
  197. ;;;; Timestamps
  198. (defcustom org-texinfo-active-timestamp-format "@emph{%s}"
  199. "A printf format string to be applied to active timestamps."
  200. :group 'org-export-texinfo
  201. :type 'string)
  202. (defcustom org-texinfo-inactive-timestamp-format "@emph{%s}"
  203. "A printf format string to be applied to inactive timestamps."
  204. :group 'org-export-texinfo
  205. :type 'string)
  206. (defcustom org-texinfo-diary-timestamp-format "@emph{%s}"
  207. "A printf format string to be applied to diary timestamps."
  208. :group 'org-export-texinfo
  209. :type 'string)
  210. ;;;; Links
  211. (defcustom org-texinfo-link-with-unknown-path-format "@indicateurl{%s}"
  212. "Format string for links with unknown path type."
  213. :group 'org-export-texinfo
  214. :type 'string)
  215. ;;;; Tables
  216. (defcustom org-texinfo-tables-verbatim nil
  217. "When non-nil, tables are exported verbatim."
  218. :group 'org-export-texinfo
  219. :type 'boolean)
  220. (defcustom org-texinfo-table-scientific-notation "%s\\,(%s)"
  221. "Format string to display numbers in scientific notation.
  222. The format should have \"%s\" twice, for mantissa and exponent
  223. \(i.e. \"%s\\\\times10^{%s}\").
  224. When nil, no transformation is made."
  225. :group 'org-export-texinfo
  226. :type '(choice
  227. (string :tag "Format string")
  228. (const :tag "No formatting" nil)))
  229. (defcustom org-texinfo-def-table-markup "@samp"
  230. "Default setting for @table environments."
  231. :group 'org-export-texinfo
  232. :type 'string)
  233. ;;;; Text markup
  234. (defcustom org-texinfo-text-markup-alist '((bold . "@strong{%s}")
  235. (code . code)
  236. (italic . "@emph{%s}")
  237. (verbatim . verb))
  238. "Alist of Texinfo expressions to convert text markup.
  239. The key must be a symbol among `bold', `code', `italic',
  240. `strike-through', `underscore' and `verbatim'. The value is
  241. a formatting string to wrap fontified text with.
  242. Value can also be set to the following symbols: `verb' and
  243. `code'. For the former, Org will use \"@verb\" to create
  244. a format string and select a delimiter character that isn't in
  245. the string. For the latter, Org will use \"@code\" to typeset
  246. and try to protect special characters.
  247. If no association can be found for a given markup, text will be
  248. returned as-is."
  249. :group 'org-export-texinfo
  250. :type 'alist
  251. :options '(bold code italic strike-through underscore verbatim))
  252. ;;;; Drawers
  253. (defcustom org-texinfo-format-drawer-function (lambda (_name contents) contents)
  254. "Function called to format a drawer in Texinfo code.
  255. The function must accept two parameters:
  256. NAME the drawer name, like \"LOGBOOK\"
  257. CONTENTS the contents of the drawer.
  258. The function should return the string to be exported.
  259. The default function simply returns the value of CONTENTS."
  260. :group 'org-export-texinfo
  261. :version "24.4"
  262. :package-version '(Org . "8.2")
  263. :type 'function)
  264. ;;;; Inlinetasks
  265. (defcustom org-texinfo-format-inlinetask-function
  266. 'org-texinfo-format-inlinetask-default-function
  267. "Function called to format an inlinetask in Texinfo code.
  268. The function must accept six parameters:
  269. TODO the todo keyword, as a string
  270. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  271. PRIORITY the inlinetask priority, as a string
  272. NAME the inlinetask name, as a string.
  273. TAGS the inlinetask tags, as a list of strings.
  274. CONTENTS the contents of the inlinetask, as a string.
  275. The function should return the string to be exported."
  276. :group 'org-export-texinfo
  277. :type 'function)
  278. ;;;; Compilation
  279. (defcustom org-texinfo-info-process '("makeinfo %f")
  280. "Commands to process a Texinfo file to an INFO file.
  281. This is list of strings, each of them will be given to the shell
  282. as a command. %f in the command will be replaced by the full
  283. file name, %b by the file base name (i.e without extension) and
  284. %o by the base directory of the file."
  285. :group 'org-export-texinfo
  286. :type '(repeat :tag "Shell command sequence"
  287. (string :tag "Shell command")))
  288. (defcustom org-texinfo-logfiles-extensions
  289. '("aux" "toc" "cp" "fn" "ky" "pg" "tp" "vr")
  290. "The list of file extensions to consider as Texinfo logfiles.
  291. The logfiles will be remove if `org-texinfo-remove-logfiles' is
  292. non-nil."
  293. :group 'org-export-texinfo
  294. :type '(repeat (string :tag "Extension")))
  295. (defcustom org-texinfo-remove-logfiles t
  296. "Non-nil means remove the logfiles produced by compiling a Texinfo file.
  297. By default, logfiles are files with these extensions: .aux, .toc,
  298. .cp, .fn, .ky, .pg and .tp. To define the set of logfiles to remove,
  299. set `org-texinfo-logfiles-extensions'."
  300. :group 'org-export-latex
  301. :type 'boolean)
  302. ;;; Constants
  303. (defconst org-texinfo-max-toc-depth 4
  304. "Maximum depth for creation of detailed menu listings.
  305. Beyond this depth, Texinfo will not recognize the nodes and will
  306. cause errors. Left as a constant in case this value ever
  307. changes.")
  308. (defconst org-texinfo-supported-coding-systems
  309. '("US-ASCII" "UTF-8" "ISO-8859-15" "ISO-8859-1" "ISO-8859-2" "koi8-r" "koi8-u")
  310. "List of coding systems supported by Texinfo, as strings.
  311. Specified coding system will be matched against these strings.
  312. If two strings share the same prefix (e.g. \"ISO-8859-1\" and
  313. \"ISO-8859-15\"), the most specific one has to be listed first.")
  314. (defconst org-texinfo-inline-image-rules
  315. (list (cons "file"
  316. (regexp-opt '("eps" "pdf" "png" "jpg" "jpeg" "gif" "svg"))))
  317. "Rules characterizing image files that can be inlined.")
  318. ;;; Internal Functions
  319. (defun org-texinfo--filter-section-blank-lines (headline _backend _info)
  320. "Filter controlling number of blank lines after a section."
  321. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" "\n\n" headline))
  322. (defun org-texinfo--normalize-headlines (tree _backend info)
  323. "Normalize headlines in TREE.
  324. BACK-END is the symbol specifying back-end used for export. INFO
  325. is a plist used as a communication channel.
  326. Make sure every headline in TREE contains a section, since those
  327. are required to install a menu. Also put exactly one blank line
  328. at the end of each section.
  329. Return new tree."
  330. (org-element-map tree 'headline
  331. (lambda (hl)
  332. (org-element-put-property hl :post-blank 1)
  333. (let ((contents (org-element-contents hl)))
  334. (when contents
  335. (let ((first (org-element-map contents '(headline section)
  336. #'identity info t)))
  337. (unless (eq (org-element-type first) 'section)
  338. (apply #'org-element-set-contents
  339. hl
  340. (cons `(section (:parent ,hl)) contents)))))))
  341. info)
  342. tree)
  343. (defun org-texinfo--find-verb-separator (s)
  344. "Return a character not used in string S.
  345. This is used to choose a separator for constructs like \\verb."
  346. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  347. (cl-loop for c across ll
  348. when (not (string-match (regexp-quote (char-to-string c)) s))
  349. return (char-to-string c))))
  350. (defun org-texinfo--text-markup (text markup _info)
  351. "Format TEXT depending on MARKUP text markup.
  352. INFO is a plist used as a communication channel. See
  353. `org-texinfo-text-markup-alist' for details."
  354. (pcase (cdr (assq markup org-texinfo-text-markup-alist))
  355. ;; No format string: Return raw text.
  356. (`nil text)
  357. (`verb
  358. (let ((separator (org-texinfo--find-verb-separator text)))
  359. (concat "@verb{" separator text separator "}")))
  360. (`code
  361. (format "@code{%s}" (replace-regexp-in-string "[@{}]" "@\\&" text)))
  362. ;; Else use format string.
  363. (fmt (format fmt text))))
  364. (defun org-texinfo--get-node (blob info)
  365. "Return node or anchor associated to BLOB.
  366. BLOB is an element or object. INFO is a plist used as
  367. a communication channel. The function guarantees the node or
  368. anchor name is unique."
  369. (let ((cache (plist-get info :texinfo-node-cache)))
  370. (or (cdr (assq blob cache))
  371. (let ((name
  372. (org-texinfo--sanitize-node
  373. (if (eq (org-element-type blob) 'headline)
  374. (org-export-data (org-export-get-alt-title blob info) info)
  375. (org-export-get-reference blob info)))))
  376. ;; Ensure NAME is unique.
  377. (while (rassoc name cache) (setq name (concat name "x")))
  378. (plist-put info :texinfo-node-cache (cons (cons blob name) cache))
  379. name))))
  380. (defun org-texinfo--sanitize-node (title)
  381. "Bend string TITLE to node line requirements.
  382. Trim string and collapse multiple whitespace characters as they
  383. are not significant. Also remove the following characters: @
  384. { } ( ) : . ,"
  385. (replace-regexp-in-string
  386. "[:,.]" ""
  387. (replace-regexp-in-string
  388. "\\`(\\(.*)\\)" "[\\1"
  389. (org-trim (replace-regexp-in-string "[ \t]\\{2,\\}" " " title)))))
  390. (defun org-texinfo--sanitize-content (text)
  391. "Escape special characters in string TEXT.
  392. Special characters are: @ { }"
  393. (replace-regexp-in-string "[@{}]" "@\\&" text))
  394. (defun org-texinfo--wrap-float (value info &optional type label caption short)
  395. "Wrap string VALUE within a @float command.
  396. INFO is the current export state, as a plist. TYPE is float
  397. type, as a string. LABEL is the cross reference label for the
  398. float, as a string. CAPTION and SHORT are, respectively, the
  399. caption and shortcaption used for the float, as secondary
  400. strings (e.g., returned by `org-export-get-caption')."
  401. (let* ((backend
  402. (org-export-create-backend
  403. :parent 'texinfo
  404. :transcoders '((link . (lambda (object c i) c))
  405. (radio-target . (lambda (object c i) c))
  406. (target . ignore))))
  407. (short-backend
  408. (org-export-create-backend
  409. :parent 'texinfo
  410. :transcoders '((footnote-reference . ignore)
  411. (inline-src-block . ignore)
  412. (link . (lambda (object c i) c))
  413. (radio-target . (lambda (object c i) c))
  414. (target . ignore)
  415. (verbatim . ignore))))
  416. (short-str
  417. (if (and short caption)
  418. (format "@shortcaption{%s}\n"
  419. (org-export-data-with-backend short short-backend info))
  420. ""))
  421. (caption-str
  422. (if (or short caption)
  423. (format "@caption{%s}\n"
  424. (org-export-data-with-backend
  425. (or caption short)
  426. (if (equal short-str "") short-backend backend)
  427. info))
  428. "")))
  429. (format "@float %s%s\n%s\n%s%s@end float"
  430. type (if label (concat "," label) "") value caption-str short-str)))
  431. ;;; Template
  432. (defun org-texinfo-template (contents info)
  433. "Return complete document string after Texinfo conversion.
  434. CONTENTS is the transcoded contents string. INFO is a plist
  435. holding export options."
  436. (let ((title (org-export-data (plist-get info :title) info))
  437. ;; Copying data is the contents of the first headline in
  438. ;; parse tree with a non-nil copying property.
  439. (copying (org-element-map (plist-get info :parse-tree) 'headline
  440. (lambda (hl)
  441. (and (org-not-nil (org-element-property :COPYING hl))
  442. (org-element-contents hl)))
  443. info t)))
  444. (concat
  445. "\\input texinfo @c -*- texinfo -*-\n"
  446. "@c %**start of header\n"
  447. (let ((file (or (plist-get info :texinfo-filename)
  448. (let ((f (plist-get info :output-file)))
  449. (and f (concat (file-name-sans-extension f) ".info"))))))
  450. (and file (format "@setfilename %s\n" file)))
  451. (format "@settitle %s\n" title)
  452. ;; Insert class-defined header.
  453. (org-element-normalize-string
  454. (let ((header (nth 1 (assoc (plist-get info :texinfo-class)
  455. org-texinfo-classes)))
  456. (coding
  457. (catch 'coding-system
  458. (let ((case-fold-search t)
  459. (name (symbol-name (or org-texinfo-coding-system
  460. buffer-file-coding-system))))
  461. (dolist (system org-texinfo-supported-coding-systems "UTF-8")
  462. (when (string-match-p (regexp-quote system) name)
  463. (throw 'coding-system system))))))
  464. (language (plist-get info :language))
  465. (case-fold-search nil))
  466. ;; Auto coding system.
  467. (replace-regexp-in-string
  468. "^@documentencoding \\(AUTO\\)$"
  469. coding
  470. (replace-regexp-in-string
  471. "^@documentlanguage \\(AUTO\\)$" language header t nil 1) t nil 1)))
  472. ;; Additional header options set by #+TEXINFO_HEADER.
  473. (let ((texinfo-header (plist-get info :texinfo-header)))
  474. (and texinfo-header (org-element-normalize-string texinfo-header)))
  475. "@c %**end of header\n\n"
  476. ;; Additional options set by #+TEXINFO_POST_HEADER.
  477. (let ((texinfo-post-header (plist-get info :texinfo-post-header)))
  478. (and texinfo-post-header
  479. (org-element-normalize-string texinfo-post-header)))
  480. ;; Copying.
  481. (and copying
  482. (format "@copying\n%s@end copying\n\n"
  483. (org-element-normalize-string
  484. (org-export-data copying info))))
  485. ;; Info directory information. Only supply if both title and
  486. ;; category are provided.
  487. (let ((dircat (plist-get info :texinfo-dircat))
  488. (dirtitle
  489. (let ((title (plist-get info :texinfo-dirtitle)))
  490. (and title
  491. (string-match "^\\(?:\\* \\)?\\(.*?\\)\\(\\.\\)?$" title)
  492. (format "* %s." (match-string 1 title))))))
  493. (when (and dircat dirtitle)
  494. (concat "@dircategory " dircat "\n"
  495. "@direntry\n"
  496. (let ((dirdesc
  497. (let ((desc (plist-get info :texinfo-dirdesc)))
  498. (cond ((not desc) nil)
  499. ((string-match-p "\\.$" desc) desc)
  500. (t (concat desc "."))))))
  501. (if dirdesc (format "%-23s %s" dirtitle dirdesc) dirtitle))
  502. "\n"
  503. "@end direntry\n\n")))
  504. ;; Title
  505. "@finalout\n"
  506. "@titlepage\n"
  507. (when (plist-get info :with-title)
  508. (concat
  509. (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
  510. (let ((subtitle (plist-get info :subtitle)))
  511. (when subtitle
  512. (format "@subtitle %s\n"
  513. (org-export-data subtitle info))))))
  514. (when (plist-get info :with-author)
  515. (concat
  516. ;; Primary author.
  517. (let ((author (org-string-nw-p
  518. (org-export-data (plist-get info :author) info)))
  519. (email (and (plist-get info :with-email)
  520. (org-string-nw-p
  521. (org-export-data (plist-get info :email) info)))))
  522. (cond ((and author email)
  523. (format "@author %s (@email{%s})\n" author email))
  524. (author (format "@author %s\n" author))
  525. (email (format "@author @email{%s}\n" email))))
  526. ;; Other authors.
  527. (let ((subauthor (plist-get info :subauthor)))
  528. (and subauthor
  529. (org-element-normalize-string
  530. (replace-regexp-in-string "^" "@author " subauthor))))))
  531. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  532. "@end titlepage\n\n"
  533. ;; Table of contents.
  534. (and (plist-get info :with-toc) "@contents\n\n")
  535. ;; Configure Top Node when not for Tex
  536. "@ifnottex\n"
  537. "@node Top\n"
  538. (format "@top %s\n" title)
  539. (and copying "@insertcopying\n")
  540. "@end ifnottex\n\n"
  541. ;; Menu.
  542. (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
  543. "\n"
  544. ;; Document's body.
  545. contents "\n"
  546. ;; Creator.
  547. (and (plist-get info :with-creator)
  548. (concat (plist-get info :creator) "\n"))
  549. ;; Document end.
  550. "@bye")))
  551. ;;; Transcode Functions
  552. ;;;; Bold
  553. (defun org-texinfo-bold (_bold contents info)
  554. "Transcode BOLD from Org to Texinfo.
  555. CONTENTS is the text with bold markup. INFO is a plist holding
  556. contextual information."
  557. (org-texinfo--text-markup contents 'bold info))
  558. ;;;; Center Block
  559. (defun org-texinfo-center-block (_center-block contents _info)
  560. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  561. CONTENTS holds the contents of the block. INFO is a plist used
  562. as a communication channel."
  563. contents)
  564. ;;;; Clock
  565. (defun org-texinfo-clock (clock _contents info)
  566. "Transcode a CLOCK element from Org to Texinfo.
  567. CONTENTS is nil. INFO is a plist holding contextual
  568. information."
  569. (concat
  570. "@noindent"
  571. (format "@strong{%s} " org-clock-string)
  572. (format (plist-get info :texinfo-inactive-timestamp-format)
  573. (concat (org-timestamp-translate (org-element-property :value clock))
  574. (let ((time (org-element-property :duration clock)))
  575. (and time (format " (%s)" time)))))
  576. "@*"))
  577. ;;;; Code
  578. (defun org-texinfo-code (code _contents info)
  579. "Transcode a CODE object from Org to Texinfo.
  580. CONTENTS is nil. INFO is a plist used as a communication
  581. channel."
  582. (org-texinfo--text-markup (org-element-property :value code) 'code info))
  583. ;;;; Drawer
  584. (defun org-texinfo-drawer (drawer contents info)
  585. "Transcode a DRAWER element from Org to Texinfo.
  586. CONTENTS holds the contents of the block. INFO is a plist
  587. holding contextual information."
  588. (let* ((name (org-element-property :drawer-name drawer))
  589. (output (funcall (plist-get info :texinfo-format-drawer-function)
  590. name contents)))
  591. output))
  592. ;;;; Dynamic Block
  593. (defun org-texinfo-dynamic-block (_dynamic-block contents _info)
  594. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  595. CONTENTS holds the contents of the block. INFO is a plist
  596. holding contextual information."
  597. contents)
  598. ;;;; Entity
  599. (defun org-texinfo-entity (entity _contents _info)
  600. "Transcode an ENTITY object from Org to Texinfo.
  601. CONTENTS are the definition itself. INFO is a plist holding
  602. contextual information."
  603. (let ((ent (org-element-property :latex entity)))
  604. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  605. ;;;; Example Block
  606. (defun org-texinfo-example-block (example-block _contents info)
  607. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  608. CONTENTS is nil. INFO is a plist holding contextual
  609. information."
  610. (format "@verbatim\n%s@end verbatim"
  611. (org-export-format-code-default example-block info)))
  612. ;;; Export Block
  613. (defun org-texinfo-export-block (export-block _contents _info)
  614. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  615. CONTENTS is nil. INFO is a plist holding contextual information."
  616. (when (string= (org-element-property :type export-block) "TEXINFO")
  617. (org-remove-indentation (org-element-property :value export-block))))
  618. ;;; Export Snippet
  619. (defun org-texinfo-export-snippet (export-snippet _contents _info)
  620. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  621. CONTENTS is nil. INFO is a plist holding contextual information."
  622. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  623. (org-element-property :value export-snippet)))
  624. ;;;; Fixed Width
  625. (defun org-texinfo-fixed-width (fixed-width _contents _info)
  626. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  627. CONTENTS is nil. INFO is a plist holding contextual information."
  628. (format "@example\n%s\n@end example"
  629. (org-remove-indentation
  630. (org-texinfo--sanitize-content
  631. (org-element-property :value fixed-width)))))
  632. ;;;; Footnote Reference
  633. (defun org-texinfo-footnote-reference (footnote _contents info)
  634. "Create a footnote reference for FOOTNOTE.
  635. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  636. plist holding contextual information."
  637. (let ((def (org-export-get-footnote-definition footnote info)))
  638. (format "@footnote{%s}"
  639. (org-trim (org-export-data def info)))))
  640. ;;;; Headline
  641. (defun org-texinfo-headline (headline contents info)
  642. "Transcode a HEADLINE element from Org to Texinfo.
  643. CONTENTS holds the contents of the headline. INFO is a plist
  644. holding contextual information."
  645. (let* ((class (plist-get info :texinfo-class))
  646. (level (org-export-get-relative-level headline info))
  647. (numberedp (org-export-numbered-headline-p headline info))
  648. (class-sectioning (assoc class (plist-get info :texinfo-classes)))
  649. ;; Find the index type, if any.
  650. (index (org-element-property :INDEX headline))
  651. ;; Create node info, to insert it before section formatting.
  652. ;; Use custom menu title if present.
  653. (node (format "@node %s\n" (org-texinfo--get-node headline info)))
  654. ;; Section formatting will set two placeholders: one for the
  655. ;; title and the other for the contents.
  656. (section-fmt
  657. (if (org-not-nil (org-element-property :APPENDIX headline))
  658. "@appendix %s\n%s"
  659. (let ((sec (if (and (symbolp (nth 2 class-sectioning))
  660. (fboundp (nth 2 class-sectioning)))
  661. (funcall (nth 2 class-sectioning) level numberedp)
  662. (nth (1+ level) class-sectioning))))
  663. (cond
  664. ;; No section available for that LEVEL.
  665. ((not sec) nil)
  666. ;; Section format directly returned by a function.
  667. ((stringp sec) sec)
  668. ;; (numbered-section . unnumbered-section)
  669. ((not (consp (cdr sec)))
  670. (concat (if (or index (not numberedp)) (cdr sec) (car sec))
  671. "\n%s"))))))
  672. (todo
  673. (and (plist-get info :with-todo-keywords)
  674. (let ((todo (org-element-property :todo-keyword headline)))
  675. (and todo (org-export-data todo info)))))
  676. (todo-type (and todo (org-element-property :todo-type headline)))
  677. (tags (and (plist-get info :with-tags)
  678. (org-export-get-tags headline info)))
  679. (priority (and (plist-get info :with-priority)
  680. (org-element-property :priority headline)))
  681. (text (org-export-data (org-element-property :title headline) info))
  682. (full-text (funcall (plist-get info :texinfo-format-headline-function)
  683. todo todo-type priority text tags))
  684. (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
  685. (cond
  686. ;; Case 1: This is a footnote section: ignore it.
  687. ((org-element-property :footnote-section-p headline) nil)
  688. ;; Case 2: This is the `copying' section: ignore it
  689. ;; This is used elsewhere.
  690. ((org-not-nil (org-element-property :COPYING headline)) nil)
  691. ;; Case 3: An index. If it matches one of the known indexes,
  692. ;; print it as such following the contents, otherwise
  693. ;; print the contents and leave the index up to the user.
  694. (index
  695. (concat node
  696. (format
  697. section-fmt
  698. full-text
  699. (concat contents
  700. (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  701. (concat "\n@printindex " index))))))
  702. ;; Case 4: This is a deep sub-tree: export it as a list item.
  703. ;; Also export as items headlines for which no section
  704. ;; format has been found.
  705. ((or (not section-fmt) (org-export-low-level-p headline info))
  706. ;; Build the real contents of the sub-tree.
  707. (concat (and (org-export-first-sibling-p headline info)
  708. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  709. "@item\n" full-text "\n"
  710. contents
  711. (if (org-export-last-sibling-p headline info)
  712. (format "@end %s" (if numberedp 'enumerate 'itemize))
  713. "\n")))
  714. ;; Case 5: Standard headline. Export it as a section.
  715. (t (concat node (format section-fmt full-text contents))))))
  716. (defun org-texinfo-format-headline-default-function
  717. (todo _todo-type priority text tags)
  718. "Default format function for a headline.
  719. See `org-texinfo-format-headline-function' for details."
  720. (concat (when todo (format "@strong{%s} " todo))
  721. (when priority (format "@emph{#%s} " priority))
  722. text
  723. (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
  724. ;;;; Inline Src Block
  725. (defun org-texinfo-inline-src-block (inline-src-block _contents _info)
  726. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  727. CONTENTS holds the contents of the item. INFO is a plist holding
  728. contextual information."
  729. (let* ((code (org-element-property :value inline-src-block))
  730. (separator (org-texinfo--find-verb-separator code)))
  731. (concat "@verb{" separator code separator "}")))
  732. ;;;; Inlinetask
  733. (defun org-texinfo-inlinetask (inlinetask contents info)
  734. "Transcode an INLINETASK element from Org to Texinfo.
  735. CONTENTS holds the contents of the block. INFO is a plist
  736. holding contextual information."
  737. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  738. (todo (and (plist-get info :with-todo-keywords)
  739. (let ((todo (org-element-property :todo-keyword inlinetask)))
  740. (and todo (org-export-data todo info)))))
  741. (todo-type (org-element-property :todo-type inlinetask))
  742. (tags (and (plist-get info :with-tags)
  743. (org-export-get-tags inlinetask info)))
  744. (priority (and (plist-get info :with-priority)
  745. (org-element-property :priority inlinetask))))
  746. (funcall (plist-get info :texinfo-format-inlinetask-function)
  747. todo todo-type priority title tags contents)))
  748. (defun org-texinfo-format-inlinetask-default-function
  749. (todo _todo-type priority title tags contents)
  750. "Default format function for a inlinetasks.
  751. See `org-texinfo-format-inlinetask-function' for details."
  752. (let ((full-title
  753. (concat (when todo (format "@strong{%s} " todo))
  754. (when priority (format "#%c " priority))
  755. title
  756. (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
  757. (format "@center %s\n\n%s\n" full-title contents)))
  758. ;;;; Italic
  759. (defun org-texinfo-italic (_italic contents info)
  760. "Transcode ITALIC from Org to Texinfo.
  761. CONTENTS is the text with italic markup. INFO is a plist holding
  762. contextual information."
  763. (org-texinfo--text-markup contents 'italic info))
  764. ;;;; Item
  765. (defun org-texinfo-item (item contents info)
  766. "Transcode an ITEM element from Org to Texinfo.
  767. CONTENTS holds the contents of the item. INFO is a plist holding
  768. contextual information."
  769. (format "@item%s\n%s"
  770. (let ((tag (org-element-property :tag item)))
  771. (if tag (concat " " (org-export-data tag info)) ""))
  772. (or contents "")))
  773. ;;;; Keyword
  774. (defun org-texinfo-keyword (keyword _contents info)
  775. "Transcode a KEYWORD element from Org to Texinfo.
  776. CONTENTS is nil. INFO is a plist holding contextual information."
  777. (let ((key (org-element-property :key keyword))
  778. (value (org-element-property :value keyword)))
  779. (cond
  780. ((string= key "TEXINFO") value)
  781. ((string= key "CINDEX") (format "@cindex %s" value))
  782. ((string= key "FINDEX") (format "@findex %s" value))
  783. ((string= key "KINDEX") (format "@kindex %s" value))
  784. ((string= key "PINDEX") (format "@pindex %s" value))
  785. ((string= key "TINDEX") (format "@tindex %s" value))
  786. ((string= key "VINDEX") (format "@vindex %s" value))
  787. ((string= key "TOC")
  788. (cond ((string-match-p "\\<tables\\>" value)
  789. (concat "@listoffloats "
  790. (org-export-translate "Table" :utf-8 info)))
  791. ((string-match-p "\\<listings\\>" value)
  792. (concat "@listoffloats "
  793. (org-export-translate "Listing" :utf-8 info))))))))
  794. ;;;; Line Break
  795. (defun org-texinfo-line-break (_line-break _contents _info)
  796. "Transcode a LINE-BREAK object from Org to Texinfo.
  797. CONTENTS is nil. INFO is a plist holding contextual information."
  798. "@*\n")
  799. ;;;; Link
  800. (defun org-texinfo-link (link desc info)
  801. "Transcode a LINK object from Org to Texinfo.
  802. DESC is the description part of the link, or the empty string.
  803. INFO is a plist holding contextual information. See
  804. `org-export-data'."
  805. (let* ((type (org-element-property :type link))
  806. (raw-path (org-element-property :path link))
  807. ;; Ensure DESC really exists, or set it to nil.
  808. (desc (and (not (string= desc "")) desc))
  809. (path (cond
  810. ((member type '("http" "https" "ftp"))
  811. (concat type ":" raw-path))
  812. ((string= type "file") (org-export-file-uri raw-path))
  813. (t raw-path))))
  814. (cond
  815. ((org-export-custom-protocol-maybe link desc 'texinfo))
  816. ((org-export-inline-image-p link org-texinfo-inline-image-rules)
  817. (org-texinfo--inline-image link info))
  818. ((equal type "radio")
  819. (let ((destination (org-export-resolve-radio-link link info)))
  820. (if (not destination) desc
  821. (format "@ref{%s,,%s}"
  822. (org-texinfo--get-node destination info)
  823. desc))))
  824. ((member type '("custom-id" "id" "fuzzy"))
  825. (let ((destination
  826. (if (equal type "fuzzy")
  827. (org-export-resolve-fuzzy-link link info)
  828. (org-export-resolve-id-link link info))))
  829. (pcase (org-element-type destination)
  830. (`nil
  831. (format org-texinfo-link-with-unknown-path-format
  832. (org-texinfo--sanitize-content path)))
  833. ;; Id link points to an external file.
  834. (`plain-text
  835. (if desc (format "@uref{file://%s,%s}" destination desc)
  836. (format "@uref{file://%s}" destination)))
  837. (`headline
  838. (format "@ref{%s,%s}"
  839. (org-texinfo--get-node destination info)
  840. (cond
  841. (desc)
  842. ((org-export-numbered-headline-p destination info)
  843. (mapconcat
  844. #'number-to-string
  845. (org-export-get-headline-number destination info) "."))
  846. (t (org-export-data
  847. (org-element-property :title destination) info)))))
  848. (_
  849. (format "@ref{%s,,%s}"
  850. (org-texinfo--get-node destination info)
  851. (cond
  852. (desc)
  853. ;; No description is provided: first try to
  854. ;; associate destination to a number.
  855. ((let ((n (org-export-get-ordinal destination info)))
  856. (cond ((not n) nil)
  857. ((integerp n) n)
  858. (t (mapconcat #'number-to-string n ".")))))
  859. ;; Then grab title of headline containing
  860. ;; DESTINATION.
  861. ((let ((h (org-element-lineage destination '(headline) t)))
  862. (and h
  863. (org-export-data
  864. (org-element-property :title destination) info))))
  865. ;; Eventually, just return "Top" to refer to the
  866. ;; beginning of the info file.
  867. (t "Top")))))))
  868. ((equal type "info")
  869. (let* ((info-path (split-string path "[:#]"))
  870. (info-manual (car info-path))
  871. (info-node (or (cadr info-path) "Top"))
  872. (title (or desc "")))
  873. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  874. ((string= type "mailto")
  875. (format "@email{%s}"
  876. (concat (org-texinfo--sanitize-content path)
  877. (and desc (concat "," desc)))))
  878. ;; External link with a description part.
  879. ((and path desc) (format "@uref{%s,%s}" path desc))
  880. ;; External link without a description part.
  881. (path (format "@uref{%s}" path))
  882. ;; No path, only description. Try to do something useful.
  883. (t
  884. (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
  885. (defun org-texinfo--inline-image (link info)
  886. "Return Texinfo code for an inline image.
  887. LINK is the link pointing to the inline image. INFO is the
  888. current state of the export, as a plist."
  889. (let* ((parent (org-export-get-parent-element link))
  890. (label (and (org-element-property :name parent)
  891. (org-texinfo--get-node parent info)))
  892. (caption (org-export-get-caption parent))
  893. (shortcaption (org-export-get-caption parent t))
  894. (path (org-element-property :path link))
  895. (filename
  896. (file-name-sans-extension
  897. (if (file-name-absolute-p path) (expand-file-name path) path)))
  898. (extension (file-name-extension path))
  899. (attributes (org-export-read-attribute :attr_texinfo parent))
  900. (height (or (plist-get attributes :height) ""))
  901. (width (or (plist-get attributes :width) ""))
  902. (alt (or (plist-get attributes :alt) ""))
  903. (image (format "@image{%s,%s,%s,%s,%s}"
  904. filename width height alt extension)))
  905. (cond ((or caption shortcaption)
  906. (org-texinfo--wrap-float image
  907. info
  908. (org-export-translate "Figure" :utf-8 info)
  909. label
  910. caption
  911. shortcaption))
  912. (label (concat "@anchor{" label "}\n" image))
  913. (t image))))
  914. ;;;; Menu
  915. (defun org-texinfo-make-menu (scope info &optional master)
  916. "Create the menu for inclusion in the Texinfo document.
  917. SCOPE is a headline or a full parse tree. INFO is the
  918. communication channel, as a plist.
  919. When optional argument MASTER is non-nil, generate a master menu,
  920. including detailed node listing."
  921. (let ((menu (org-texinfo--build-menu scope info)))
  922. (when (org-string-nw-p menu)
  923. (org-element-normalize-string
  924. (format
  925. "@menu\n%s@end menu"
  926. (concat menu
  927. (when master
  928. (let ((detailmenu
  929. (org-texinfo--build-menu
  930. scope info
  931. (let ((toc-depth (plist-get info :with-toc)))
  932. (if (wholenump toc-depth) toc-depth
  933. org-texinfo-max-toc-depth)))))
  934. (when (org-string-nw-p detailmenu)
  935. (concat "\n@detailmenu\n"
  936. "--- The Detailed Node Listing ---\n\n"
  937. detailmenu
  938. "@end detailmenu\n"))))))))))
  939. (defun org-texinfo--build-menu (scope info &optional level)
  940. "Build menu for entries within SCOPE.
  941. SCOPE is a headline or a full parse tree. INFO is a plist
  942. containing contextual information. When optional argument LEVEL
  943. is an integer, build the menu recursively, down to this depth."
  944. (cond
  945. ((not level)
  946. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  947. ((zerop level) nil)
  948. (t
  949. (org-element-normalize-string
  950. (mapconcat
  951. (lambda (h)
  952. (let ((entries (org-texinfo--menu-entries h info)))
  953. (when entries
  954. (concat
  955. (format "%s\n\n%s\n"
  956. (org-export-data (org-export-get-alt-title h info) info)
  957. (org-texinfo--format-entries entries info))
  958. (org-texinfo--build-menu h info (1- level))))))
  959. (org-texinfo--menu-entries scope info) "\n")))))
  960. (defun org-texinfo--format-entries (entries info)
  961. "Format all direct menu entries in SCOPE, as a string.
  962. SCOPE is either a headline or a full Org document. INFO is
  963. a plist containing contextual information."
  964. (org-element-normalize-string
  965. (mapconcat
  966. (lambda (h)
  967. (let* ((title (org-export-data
  968. (org-export-get-alt-title h info) info))
  969. (node (org-texinfo--get-node h info))
  970. (entry (concat "* " title ":"
  971. (if (string= title node) ":"
  972. (concat " " node ". "))))
  973. (desc (org-element-property :DESCRIPTION h)))
  974. (if (not desc) entry
  975. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  976. entry desc))))
  977. entries "\n")))
  978. (defun org-texinfo--menu-entries (scope info)
  979. "List direct children in SCOPE needing a menu entry.
  980. SCOPE is a headline or a full parse tree. INFO is a plist
  981. holding contextual information."
  982. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  983. (plist-get (plist-put info :texinfo-entries-cache
  984. (make-hash-table :test #'eq))
  985. :texinfo-entries-cache)))
  986. (cached-entries (gethash scope cache 'no-cache)))
  987. (if (not (eq cached-entries 'no-cache)) cached-entries
  988. (puthash scope
  989. (org-element-map (org-element-contents scope) 'headline
  990. (lambda (h)
  991. (and (not (org-not-nil (org-element-property :COPYING h)))
  992. (not (org-element-property :footnote-section-p h))
  993. (not (org-export-low-level-p h info))
  994. h))
  995. info nil 'headline)
  996. cache))))
  997. ;;;; Node Property
  998. (defun org-texinfo-node-property (node-property _contents _info)
  999. "Transcode a NODE-PROPERTY element from Org to Texinfo.
  1000. CONTENTS is nil. INFO is a plist holding contextual
  1001. information."
  1002. (format "%s:%s"
  1003. (org-element-property :key node-property)
  1004. (let ((value (org-element-property :value node-property)))
  1005. (if value (concat " " value) ""))))
  1006. ;;;; Paragraph
  1007. (defun org-texinfo-paragraph (_paragraph contents _info)
  1008. "Transcode a PARAGRAPH element from Org to Texinfo.
  1009. CONTENTS is the contents of the paragraph, as a string. INFO is
  1010. the plist used as a communication channel."
  1011. contents)
  1012. ;;;; Plain List
  1013. (defun org-texinfo-plain-list (plain-list contents info)
  1014. "Transcode a PLAIN-LIST element from Org to Texinfo.
  1015. CONTENTS is the contents of the list. INFO is a plist holding
  1016. contextual information."
  1017. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  1018. (indic (or (plist-get attr :indic)
  1019. (plist-get info :texinfo-def-table-markup)))
  1020. (table-type (plist-get attr :table-type))
  1021. (type (org-element-property :type plain-list))
  1022. (list-type (cond
  1023. ((eq type 'ordered) "enumerate")
  1024. ((eq type 'unordered) "itemize")
  1025. ((member table-type '("ftable" "vtable")) table-type)
  1026. (t "table"))))
  1027. (format "@%s\n%s@end %s"
  1028. (if (eq type 'descriptive) (concat list-type " " indic) list-type)
  1029. contents
  1030. list-type)))
  1031. ;;;; Plain Text
  1032. (defun org-texinfo-plain-text (text info)
  1033. "Transcode a TEXT string from Org to Texinfo.
  1034. TEXT is the string to transcode. INFO is a plist holding
  1035. contextual information."
  1036. ;; First protect @, { and }.
  1037. (let ((output (org-texinfo--sanitize-content text)))
  1038. ;; Activate smart quotes. Be sure to provide original TEXT string
  1039. ;; since OUTPUT may have been modified.
  1040. (when (plist-get info :with-smart-quotes)
  1041. (setq output
  1042. (org-export-activate-smart-quotes output :texinfo info text)))
  1043. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  1044. (let ((case-fold-search nil)
  1045. (start 0))
  1046. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  1047. (setq output (replace-match
  1048. (format "@%s{}" (match-string 1 output)) nil t output)
  1049. start (match-end 0))))
  1050. ;; Convert special strings.
  1051. (when (plist-get info :with-special-strings)
  1052. (while (string-match (regexp-quote "...") output)
  1053. (setq output (replace-match "@dots{}" nil t output))))
  1054. ;; Handle break preservation if required.
  1055. (when (plist-get info :preserve-breaks)
  1056. (setq output (replace-regexp-in-string
  1057. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  1058. ;; Return value.
  1059. output))
  1060. ;;;; Planning
  1061. (defun org-texinfo-planning (planning _contents info)
  1062. "Transcode a PLANNING element from Org to Texinfo.
  1063. CONTENTS is nil. INFO is a plist holding contextual
  1064. information."
  1065. (concat
  1066. "@noindent"
  1067. (mapconcat
  1068. 'identity
  1069. (delq nil
  1070. (list
  1071. (let ((closed (org-element-property :closed planning)))
  1072. (when closed
  1073. (concat
  1074. (format "@strong{%s} " org-closed-string)
  1075. (format (plist-get info :texinfo-inactive-timestamp-format)
  1076. (org-timestamp-translate closed)))))
  1077. (let ((deadline (org-element-property :deadline planning)))
  1078. (when deadline
  1079. (concat
  1080. (format "@strong{%s} " org-deadline-string)
  1081. (format (plist-get info :texinfo-active-timestamp-format)
  1082. (org-timestamp-translate deadline)))))
  1083. (let ((scheduled (org-element-property :scheduled planning)))
  1084. (when scheduled
  1085. (concat
  1086. (format "@strong{%s} " org-scheduled-string)
  1087. (format (plist-get info :texinfo-active-timestamp-format)
  1088. (org-timestamp-translate scheduled)))))))
  1089. " ")
  1090. "@*"))
  1091. ;;;; Property Drawer
  1092. (defun org-texinfo-property-drawer (_property-drawer contents _info)
  1093. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1094. CONTENTS holds the contents of the drawer. INFO is a plist
  1095. holding contextual information."
  1096. (and (org-string-nw-p contents)
  1097. (format "@verbatim\n%s@end verbatim" contents)))
  1098. ;;;; Quote Block
  1099. (defun org-texinfo-quote-block (quote-block contents _info)
  1100. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1101. CONTENTS holds the contents of the block. INFO is a plist
  1102. holding contextual information."
  1103. (let* ((title (org-element-property :name quote-block))
  1104. (start-quote (concat "@quotation"
  1105. (if title
  1106. (format " %s" title)))))
  1107. (format "%s\n%s@end quotation" start-quote contents)))
  1108. ;;;; Radio Target
  1109. (defun org-texinfo-radio-target (radio-target text info)
  1110. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1111. TEXT is the text of the target. INFO is a plist holding
  1112. contextual information."
  1113. (format "@anchor{%s}%s"
  1114. (org-export-get-reference radio-target info)
  1115. text))
  1116. ;;;; Section
  1117. (defun org-texinfo-section (section contents info)
  1118. "Transcode a SECTION element from Org to Texinfo.
  1119. CONTENTS holds the contents of the section. INFO is a plist
  1120. holding contextual information."
  1121. (org-trim
  1122. (concat contents
  1123. "\n"
  1124. (let ((parent (org-export-get-parent-headline section)))
  1125. (and parent (org-texinfo-make-menu parent info))))))
  1126. ;;;; Special Block
  1127. (defun org-texinfo-special-block (special-block contents _info)
  1128. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1129. CONTENTS holds the contents of the block. INFO is a plist used
  1130. as a communication channel."
  1131. (let ((opt (org-export-read-attribute :attr_texinfo special-block :options))
  1132. (type (org-element-property :type special-block)))
  1133. (format "@%s%s\n%s@end %s"
  1134. type
  1135. (if opt (concat " " opt) opt)
  1136. (or contents "")
  1137. type)))
  1138. ;;;; Src Block
  1139. (defun org-texinfo-src-block (src-block _contents info)
  1140. "Transcode a SRC-BLOCK element from Org to Texinfo.
  1141. CONTENTS holds the contents of the item. INFO is a plist holding
  1142. contextual information."
  1143. (let* ((lisp (string-match-p "lisp"
  1144. (org-element-property :language src-block)))
  1145. (code (org-texinfo--sanitize-content
  1146. (org-export-format-code-default src-block info)))
  1147. (value (format
  1148. (if lisp "@lisp\n%s@end lisp" "@example\n%s@end example")
  1149. code))
  1150. (caption (org-export-get-caption src-block))
  1151. (shortcaption (org-export-get-caption src-block t)))
  1152. (if (not (or caption shortcaption)) value
  1153. (org-texinfo--wrap-float value
  1154. info
  1155. (org-export-translate "Listing" :utf-8 info)
  1156. (org-export-get-reference src-block info)
  1157. caption
  1158. shortcaption))))
  1159. ;;;; Statistics Cookie
  1160. (defun org-texinfo-statistics-cookie (statistics-cookie _contents _info)
  1161. "Transcode a STATISTICS-COOKIE object from Org to Texinfo.
  1162. CONTENTS is nil. INFO is a plist holding contextual information."
  1163. (org-element-property :value statistics-cookie))
  1164. ;;;; Strike-through
  1165. (defun org-texinfo-strike-through (_strike-through contents info)
  1166. "Transcode STRIKE-THROUGH from Org to Texinfo.
  1167. CONTENTS is the text with strike-through markup. INFO is a plist
  1168. holding contextual information."
  1169. (org-texinfo--text-markup contents 'strike-through info))
  1170. ;;;; Subscript
  1171. (defun org-texinfo-subscript (_subscript contents _info)
  1172. "Transcode a SUBSCRIPT object from Org to Texinfo.
  1173. CONTENTS is the contents of the object. INFO is a plist holding
  1174. contextual information."
  1175. (format "@math{_%s}" contents))
  1176. ;;;; Superscript
  1177. (defun org-texinfo-superscript (_superscript contents _info)
  1178. "Transcode a SUPERSCRIPT object from Org to Texinfo.
  1179. CONTENTS is the contents of the object. INFO is a plist holding
  1180. contextual information."
  1181. (format "@math{^%s}" contents))
  1182. ;;;; Table
  1183. (defun org-texinfo-table (table contents info)
  1184. "Transcode a TABLE element from Org to Texinfo.
  1185. CONTENTS is the contents of the table. INFO is a plist holding
  1186. contextual information."
  1187. (if (eq (org-element-property :type table) 'table.el)
  1188. (format "@verbatim\n%s@end verbatim"
  1189. (org-element-normalize-string
  1190. (org-element-property :value table)))
  1191. (let* ((col-width (org-export-read-attribute :attr_texinfo table :columns))
  1192. (columns
  1193. (if col-width (format "@columnfractions %s" col-width)
  1194. (org-texinfo-table-column-widths table info)))
  1195. (caption (org-export-get-caption table))
  1196. (shortcaption (org-export-get-caption table t))
  1197. (table-str (format "@multitable %s\n%s@end multitable"
  1198. columns
  1199. contents)))
  1200. (if (not (or caption shortcaption)) table-str
  1201. (org-texinfo--wrap-float table-str
  1202. info
  1203. (org-export-translate "Table" :utf-8 info)
  1204. (org-export-get-reference table info)
  1205. caption
  1206. shortcaption)))))
  1207. (defun org-texinfo-table-column-widths (table info)
  1208. "Determine the largest table cell in each column to process alignment.
  1209. TABLE is the table element to transcode. INFO is a plist used as
  1210. a communication channel."
  1211. (let ((widths (make-vector (cdr (org-export-table-dimensions table info)) 0)))
  1212. (org-element-map table 'table-row
  1213. (lambda (row)
  1214. (let ((idx 0))
  1215. (org-element-map row 'table-cell
  1216. (lambda (cell)
  1217. ;; Length of the cell in the original buffer is only an
  1218. ;; approximation of the length of the cell in the
  1219. ;; output. It can sometimes fail (e.g. it considers
  1220. ;; "/a/" being larger than "ab").
  1221. (let ((w (- (org-element-property :contents-end cell)
  1222. (org-element-property :contents-begin cell))))
  1223. (aset widths idx (max w (aref widths idx))))
  1224. (cl-incf idx))
  1225. info)))
  1226. info)
  1227. (format "{%s}" (mapconcat (lambda (w) (make-string w ?a)) widths "} {"))))
  1228. ;;;; Table Cell
  1229. (defun org-texinfo-table-cell (table-cell contents info)
  1230. "Transcode a TABLE-CELL element from Org to Texinfo.
  1231. CONTENTS is the cell contents. INFO is a plist used as
  1232. a communication channel."
  1233. (concat
  1234. (let ((scientific-notation
  1235. (plist-get info :texinfo-table-scientific-notation)))
  1236. (if (and contents
  1237. scientific-notation
  1238. (string-match orgtbl-exp-regexp contents))
  1239. ;; Use appropriate format string for scientific notation.
  1240. (format scientific-notation
  1241. (match-string 1 contents)
  1242. (match-string 2 contents))
  1243. contents))
  1244. (when (org-export-get-next-element table-cell info) "\n@tab ")))
  1245. ;;;; Table Row
  1246. (defun org-texinfo-table-row (table-row contents info)
  1247. "Transcode a TABLE-ROW element from Org to Texinfo.
  1248. CONTENTS is the contents of the row. INFO is a plist used as
  1249. a communication channel."
  1250. ;; Rules are ignored since table separators are deduced from
  1251. ;; borders of the current row.
  1252. (when (eq (org-element-property :type table-row) 'standard)
  1253. (let ((rowgroup-tag
  1254. (if (and (= 1 (org-export-table-row-group table-row info))
  1255. (org-export-table-has-header-p
  1256. (org-export-get-parent-table table-row) info))
  1257. "@headitem "
  1258. "@item ")))
  1259. (concat rowgroup-tag contents "\n"))))
  1260. ;;;; Target
  1261. (defun org-texinfo-target (target _contents info)
  1262. "Transcode a TARGET object from Org to Texinfo.
  1263. CONTENTS is nil. INFO is a plist holding contextual
  1264. information."
  1265. (format "@anchor{%s}" (org-export-get-reference target info)))
  1266. ;;;; Timestamp
  1267. (defun org-texinfo-timestamp (timestamp _contents info)
  1268. "Transcode a TIMESTAMP object from Org to Texinfo.
  1269. CONTENTS is nil. INFO is a plist holding contextual
  1270. information."
  1271. (let ((value (org-texinfo-plain-text
  1272. (org-timestamp-translate timestamp) info)))
  1273. (pcase (org-element-property :type timestamp)
  1274. ((or `active `active-range)
  1275. (format (plist-get info :texinfo-active-timestamp-format) value))
  1276. ((or `inactive `inactive-range)
  1277. (format (plist-get info :texinfo-inactive-timestamp-format) value))
  1278. (_ (format (plist-get info :texinfo-diary-timestamp-format) value)))))
  1279. ;;;; Underline
  1280. (defun org-texinfo-underline (_underline contents info)
  1281. "Transcode UNDERLINE from Org to Texinfo.
  1282. CONTENTS is the text with underline markup. INFO is a plist
  1283. holding contextual information."
  1284. (org-texinfo--text-markup contents 'underline info))
  1285. ;;;; Verbatim
  1286. (defun org-texinfo-verbatim (verbatim _contents info)
  1287. "Transcode a VERBATIM object from Org to Texinfo.
  1288. CONTENTS is nil. INFO is a plist used as a communication
  1289. channel."
  1290. (org-texinfo--text-markup
  1291. (org-element-property :value verbatim) 'verbatim info))
  1292. ;;;; Verse Block
  1293. (defun org-texinfo-verse-block (_verse-block contents _info)
  1294. "Transcode a VERSE-BLOCK element from Org to Texinfo.
  1295. CONTENTS is verse block contents. INFO is a plist holding
  1296. contextual information."
  1297. (format "@display\n%s@end display" contents))
  1298. ;;; Interactive functions
  1299. (defun org-texinfo-export-to-texinfo
  1300. (&optional async subtreep visible-only body-only ext-plist)
  1301. "Export current buffer to a Texinfo file.
  1302. If narrowing is active in the current buffer, only export its
  1303. narrowed part.
  1304. If a region is active, export that region.
  1305. A non-nil optional argument ASYNC means the process should happen
  1306. asynchronously. The resulting file should be accessible through
  1307. the `org-export-stack' interface.
  1308. When optional argument SUBTREEP is non-nil, export the sub-tree
  1309. at point, extracting information from the headline properties
  1310. first.
  1311. When optional argument VISIBLE-ONLY is non-nil, don't export
  1312. contents of hidden elements.
  1313. When optional argument BODY-ONLY is non-nil, only write code
  1314. between \"\\begin{document}\" and \"\\end{document}\".
  1315. EXT-PLIST, when provided, is a property list with external
  1316. parameters overriding Org default settings, but still inferior to
  1317. file-local settings.
  1318. Return output file's name."
  1319. (interactive)
  1320. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1321. (org-export-coding-system org-texinfo-coding-system))
  1322. (org-export-to-file 'texinfo outfile
  1323. async subtreep visible-only body-only ext-plist)))
  1324. (defun org-texinfo-export-to-info
  1325. (&optional async subtreep visible-only body-only ext-plist)
  1326. "Export current buffer to Texinfo then process through to INFO.
  1327. If narrowing is active in the current buffer, only export its
  1328. narrowed part.
  1329. If a region is active, export that region.
  1330. A non-nil optional argument ASYNC means the process should happen
  1331. asynchronously. The resulting file should be accessible through
  1332. the `org-export-stack' interface.
  1333. When optional argument SUBTREEP is non-nil, export the sub-tree
  1334. at point, extracting information from the headline properties
  1335. first.
  1336. When optional argument VISIBLE-ONLY is non-nil, don't export
  1337. contents of hidden elements.
  1338. When optional argument BODY-ONLY is non-nil, only write code
  1339. between \"\\begin{document}\" and \"\\end{document}\".
  1340. EXT-PLIST, when provided, is a property list with external
  1341. parameters overriding Org default settings, but still inferior to
  1342. file-local settings.
  1343. When optional argument PUB-DIR is set, use it as the publishing
  1344. directory.
  1345. Return INFO file's name."
  1346. (interactive)
  1347. (let ((outfile (org-export-output-file-name ".texi" subtreep))
  1348. (org-export-coding-system org-texinfo-coding-system))
  1349. (org-export-to-file 'texinfo outfile
  1350. async subtreep visible-only body-only ext-plist
  1351. (lambda (file) (org-texinfo-compile file)))))
  1352. ;;;###autoload
  1353. (defun org-texinfo-publish-to-texinfo (plist filename pub-dir)
  1354. "Publish an org file to Texinfo.
  1355. FILENAME is the filename of the Org file to be published. PLIST
  1356. is the property list for the given project. PUB-DIR is the
  1357. publishing directory.
  1358. Return output file name."
  1359. (org-publish-org-to 'texinfo filename ".texi" plist pub-dir))
  1360. ;;;###autoload
  1361. (defun org-texinfo-convert-region-to-texinfo ()
  1362. "Assume the current region has org-mode syntax, and convert it to Texinfo.
  1363. This can be used in any buffer. For example, you can write an
  1364. itemized list in org-mode syntax in an Texinfo buffer and use
  1365. this command to convert it."
  1366. (interactive)
  1367. (org-export-replace-region-by 'texinfo))
  1368. (defun org-texinfo-compile (file)
  1369. "Compile a texinfo file.
  1370. FILE is the name of the file being compiled. Processing is done
  1371. through the command specified in `org-texinfo-info-process',
  1372. which see. Output is redirected to \"*Org INFO Texinfo Output*\"
  1373. buffer.
  1374. Return INFO file name or an error if it couldn't be produced."
  1375. (message "Processing Texinfo file %s..." file)
  1376. (let* ((log-name "*Org INFO Texinfo Output*")
  1377. (log (get-buffer-create log-name))
  1378. (output
  1379. (org-compile-file file org-texinfo-info-process "info"
  1380. (format "See %S for details" log-name)
  1381. log)))
  1382. (when org-texinfo-remove-logfiles
  1383. (let ((base (file-name-sans-extension output)))
  1384. (dolist (ext org-texinfo-logfiles-extensions)
  1385. (let ((file (concat base "." ext)))
  1386. (when (file-exists-p file) (delete-file file))))))
  1387. (message "Process completed.")
  1388. output))
  1389. (provide 'ox-texinfo)
  1390. ;; Local variables:
  1391. ;; generated-autoload-file: "org-loaddefs.el"
  1392. ;; End:
  1393. ;;; ox-texinfo.el ends here