ox-texinfo.el 56 KB

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