ox-texinfo.el 62 KB

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