ox-texinfo.el 59 KB

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