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