ox-texinfo.el 57 KB

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