ox-texinfo.el 59 KB

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