ox-texinfo.el 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. ;;; ox-texinfo.el --- Texinfo Back-End for Org Export Engine
  2. ;; Copyright (C) 2012-2015 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. (when (plist-get info :with-title)
  483. (concat
  484. (format "@title %s\n" (or (plist-get info :texinfo-printed-title) title ""))
  485. (let ((subtitle (plist-get info :subtitle)))
  486. (and subtitle
  487. (org-element-normalize-string
  488. (replace-regexp-in-string "^" "@subtitle " subtitle))))))
  489. (when (plist-get info :with-author)
  490. (concat
  491. ;; Primary author.
  492. (let ((author (org-string-nw-p
  493. (org-export-data (plist-get info :author) info)))
  494. (email (and (plist-get info :with-email)
  495. (org-string-nw-p
  496. (org-export-data (plist-get info :email) info)))))
  497. (cond ((and author email)
  498. (format "@author %s (@email{%s})\n" author email))
  499. (author (format "@author %s\n" author))
  500. (email (format "@author @email{%s}\n" email))))
  501. ;; Other authors.
  502. (let ((subauthor (plist-get info :subauthor)))
  503. (and subauthor
  504. (org-element-normalize-string
  505. (replace-regexp-in-string "^" "@author " subauthor))))))
  506. (and copying "@page\n@vskip 0pt plus 1filll\n@insertcopying\n")
  507. "@end titlepage\n\n"
  508. ;; Table of contents.
  509. (and (plist-get info :with-toc) "@contents\n\n")
  510. ;; Configure Top Node when not for Tex
  511. "@ifnottex\n"
  512. "@node Top\n"
  513. (format "@top %s\n" title)
  514. (and copying "@insertcopying\n")
  515. "@end ifnottex\n\n"
  516. ;; Menu.
  517. (org-texinfo-make-menu (plist-get info :parse-tree) info 'master)
  518. "\n"
  519. ;; Document's body.
  520. contents "\n"
  521. ;; Creator.
  522. (case (plist-get info :with-creator)
  523. ((nil) nil)
  524. (comment (format "@c %s\n" (plist-get info :creator)))
  525. (otherwise (concat (plist-get info :creator) "\n")))
  526. ;; Document end.
  527. "@bye")))
  528. ;;; Transcode Functions
  529. ;;;; Bold
  530. (defun org-texinfo-bold (bold contents info)
  531. "Transcode BOLD from Org to Texinfo.
  532. CONTENTS is the text with bold markup. INFO is a plist holding
  533. contextual information."
  534. (org-texinfo--text-markup contents 'bold info))
  535. ;;;; Center Block
  536. (defun org-texinfo-center-block (center-block contents info)
  537. "Transcode a CENTER-BLOCK element from Org to Texinfo.
  538. CONTENTS holds the contents of the block. INFO is a plist used
  539. as a communication channel."
  540. contents)
  541. ;;;; Clock
  542. (defun org-texinfo-clock (clock contents info)
  543. "Transcode a CLOCK element from Org to Texinfo.
  544. CONTENTS is nil. INFO is a plist holding contextual
  545. information."
  546. (concat
  547. "@noindent"
  548. (format "@strong{%s} " org-clock-string)
  549. (format (plist-get info :texinfo-inactive-timestamp-format)
  550. (concat (org-timestamp-translate (org-element-property :value clock))
  551. (let ((time (org-element-property :duration clock)))
  552. (and time (format " (%s)" time)))))
  553. "@*"))
  554. ;;;; Code
  555. (defun org-texinfo-code (code contents info)
  556. "Transcode a CODE object from Org to Texinfo.
  557. CONTENTS is nil. INFO is a plist used as a communication
  558. channel."
  559. (org-texinfo--text-markup (org-element-property :value code) 'code info))
  560. ;;;; Drawer
  561. (defun org-texinfo-drawer (drawer contents info)
  562. "Transcode a DRAWER element from Org to Texinfo.
  563. CONTENTS holds the contents of the block. INFO is a plist
  564. holding contextual information."
  565. (let* ((name (org-element-property :drawer-name drawer))
  566. (output (funcall (plist-get info :texinfo-format-drawer-function)
  567. name contents)))
  568. output))
  569. ;;;; Dynamic Block
  570. (defun org-texinfo-dynamic-block (dynamic-block contents info)
  571. "Transcode a DYNAMIC-BLOCK element from Org to Texinfo.
  572. CONTENTS holds the contents of the block. INFO is a plist
  573. holding contextual information."
  574. contents)
  575. ;;;; Entity
  576. (defun org-texinfo-entity (entity contents info)
  577. "Transcode an ENTITY object from Org to Texinfo.
  578. CONTENTS are the definition itself. INFO is a plist holding
  579. contextual information."
  580. (let ((ent (org-element-property :latex entity)))
  581. (if (org-element-property :latex-math-p entity) (format "@math{%s}" ent) ent)))
  582. ;;;; Example Block
  583. (defun org-texinfo-example-block (example-block contents info)
  584. "Transcode an EXAMPLE-BLOCK element from Org to Texinfo.
  585. CONTENTS is nil. INFO is a plist holding contextual
  586. information."
  587. (format "@verbatim\n%s@end verbatim"
  588. (org-export-format-code-default example-block info)))
  589. ;;; Export Block
  590. (defun org-texinfo-export-block (export-block contents info)
  591. "Transcode a EXPORT-BLOCK element from Org to Texinfo.
  592. CONTENTS is nil. INFO is a plist holding contextual information."
  593. (when (string= (org-element-property :type export-block) "TEXINFO")
  594. (org-remove-indentation (org-element-property :value export-block))))
  595. ;;; Export Snippet
  596. (defun org-texinfo-export-snippet (export-snippet contents info)
  597. "Transcode a EXPORT-SNIPPET object from Org to Texinfo.
  598. CONTENTS is nil. INFO is a plist holding contextual information."
  599. (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
  600. (org-element-property :value export-snippet)))
  601. ;;;; Fixed Width
  602. (defun org-texinfo-fixed-width (fixed-width contents info)
  603. "Transcode a FIXED-WIDTH element from Org to Texinfo.
  604. CONTENTS is nil. INFO is a plist holding contextual information."
  605. (format "@example\n%s\n@end example"
  606. (org-remove-indentation
  607. (org-texinfo--sanitize-content
  608. (org-element-property :value fixed-width)))))
  609. ;;;; Footnote Reference
  610. (defun org-texinfo-footnote-reference (footnote contents info)
  611. "Create a footnote reference for FOOTNOTE.
  612. FOOTNOTE is the footnote to define. CONTENTS is nil. INFO is a
  613. plist holding contextual information."
  614. (let ((def (org-export-get-footnote-definition footnote info)))
  615. (format "@footnote{%s}"
  616. (org-trim (org-export-data def info)))))
  617. ;;;; Headline
  618. (defun org-texinfo-headline (headline contents info)
  619. "Transcode a HEADLINE element from Org to Texinfo.
  620. CONTENTS holds the contents of the headline. INFO is a plist
  621. holding contextual information."
  622. (let* ((class (plist-get info :texinfo-class))
  623. (level (org-export-get-relative-level headline info))
  624. (numberedp (org-export-numbered-headline-p headline info))
  625. (class-sectioning (assoc class (plist-get info :texinfo-classes)))
  626. ;; Find the index type, if any.
  627. (index (org-element-property :INDEX headline))
  628. ;; Create node info, to insert it before section formatting.
  629. ;; Use custom menu title if present.
  630. (node (format "@node %s\n" (org-texinfo--get-node headline info)))
  631. ;; Section formatting will set two placeholders: one for the
  632. ;; title and the other for the contents.
  633. (section-fmt
  634. (if (org-not-nil (org-element-property :APPENDIX headline))
  635. "@appendix %s\n%s"
  636. (let ((sec (if (and (symbolp (nth 2 class-sectioning))
  637. (fboundp (nth 2 class-sectioning)))
  638. (funcall (nth 2 class-sectioning) level numberedp)
  639. (nth (1+ level) class-sectioning))))
  640. (cond
  641. ;; No section available for that LEVEL.
  642. ((not sec) nil)
  643. ;; Section format directly returned by a function.
  644. ((stringp sec) sec)
  645. ;; (numbered-section . unnumbered-section)
  646. ((not (consp (cdr sec)))
  647. (concat (if (or index (not numberedp)) (cdr sec) (car sec))
  648. "\n%s"))))))
  649. (todo
  650. (and (plist-get info :with-todo-keywords)
  651. (let ((todo (org-element-property :todo-keyword headline)))
  652. (and todo (org-export-data todo info)))))
  653. (todo-type (and todo (org-element-property :todo-type headline)))
  654. (tags (and (plist-get info :with-tags)
  655. (org-export-get-tags headline info)))
  656. (priority (and (plist-get info :with-priority)
  657. (org-element-property :priority headline)))
  658. (text (org-export-data (org-element-property :title headline) info))
  659. (full-text (funcall (plist-get info :texinfo-format-headline-function)
  660. todo todo-type priority text tags))
  661. (contents (if (org-string-nw-p contents) (concat "\n" contents) "")))
  662. (cond
  663. ;; Case 1: This is a footnote section: ignore it.
  664. ((org-element-property :footnote-section-p headline) nil)
  665. ;; Case 2: This is the `copying' section: ignore it
  666. ;; This is used elsewhere.
  667. ((org-not-nil (org-element-property :COPYING headline)) nil)
  668. ;; Case 3: An index. If it matches one of the known indexes,
  669. ;; print it as such following the contents, otherwise
  670. ;; print the contents and leave the index up to the user.
  671. (index
  672. (concat node
  673. (format
  674. section-fmt
  675. full-text
  676. (concat contents
  677. (and (member index '("cp" "fn" "ky" "pg" "tp" "vr"))
  678. (concat "\n@printindex " index))))))
  679. ;; Case 4: This is a deep sub-tree: export it as a list item.
  680. ;; Also export as items headlines for which no section
  681. ;; format has been found.
  682. ((or (not section-fmt) (org-export-low-level-p headline info))
  683. ;; Build the real contents of the sub-tree.
  684. (concat (and (org-export-first-sibling-p headline info)
  685. (format "@%s\n" (if numberedp 'enumerate 'itemize)))
  686. "@item\n" full-text "\n"
  687. contents
  688. (if (org-export-last-sibling-p headline info)
  689. (format "@end %s" (if numberedp 'enumerate 'itemize))
  690. "\n")))
  691. ;; Case 5: Standard headline. Export it as a section.
  692. (t (concat node (format section-fmt full-text contents))))))
  693. (defun org-texinfo-format-headline-default-function
  694. (todo todo-type priority text tags)
  695. "Default format function for a headline.
  696. See `org-texinfo-format-headline-function' for details."
  697. (concat (when todo (format "@strong{%s} " todo))
  698. (when priority (format "@emph{#%s} " priority))
  699. text
  700. (when tags (format " :%s:" (mapconcat 'identity tags ":")))))
  701. ;;;; Inline Src Block
  702. (defun org-texinfo-inline-src-block (inline-src-block contents info)
  703. "Transcode an INLINE-SRC-BLOCK element from Org to Texinfo.
  704. CONTENTS holds the contents of the item. INFO is a plist holding
  705. contextual information."
  706. (let* ((code (org-element-property :value inline-src-block))
  707. (separator (org-texinfo--find-verb-separator code)))
  708. (concat "@verb{" separator code separator "}")))
  709. ;;;; Inlinetask
  710. (defun org-texinfo-inlinetask (inlinetask contents info)
  711. "Transcode an INLINETASK element from Org to Texinfo.
  712. CONTENTS holds the contents of the block. INFO is a plist
  713. holding contextual information."
  714. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  715. (todo (and (plist-get info :with-todo-keywords)
  716. (let ((todo (org-element-property :todo-keyword inlinetask)))
  717. (and todo (org-export-data todo info)))))
  718. (todo-type (org-element-property :todo-type inlinetask))
  719. (tags (and (plist-get info :with-tags)
  720. (org-export-get-tags inlinetask info)))
  721. (priority (and (plist-get info :with-priority)
  722. (org-element-property :priority inlinetask))))
  723. (funcall (plist-get info :texinfo-format-inlinetask-function)
  724. todo todo-type priority title tags contents)))
  725. (defun org-texinfo-format-inlinetask-default-function
  726. (todo todo-type priority title tags contents)
  727. "Default format function for a inlinetasks.
  728. See `org-texinfo-format-inlinetask-function' for details."
  729. (let ((full-title
  730. (concat (when todo (format "@strong{%s} " todo))
  731. (when priority (format "#%c " priority))
  732. title
  733. (when tags (format ":%s:" (mapconcat #'identity tags ":"))))))
  734. (format "@center %s\n\n%s\n" full-title contents)))
  735. ;;;; Italic
  736. (defun org-texinfo-italic (italic contents info)
  737. "Transcode ITALIC from Org to Texinfo.
  738. CONTENTS is the text with italic markup. INFO is a plist holding
  739. contextual information."
  740. (org-texinfo--text-markup contents 'italic info))
  741. ;;;; Item
  742. (defun org-texinfo-item (item contents info)
  743. "Transcode an ITEM element from Org to Texinfo.
  744. CONTENTS holds the contents of the item. INFO is a plist holding
  745. contextual information."
  746. (format "@item%s\n%s"
  747. (let ((tag (org-element-property :tag item)))
  748. (if tag (concat " " (org-export-data tag info)) ""))
  749. (or contents "")))
  750. ;;;; Keyword
  751. (defun org-texinfo-keyword (keyword contents info)
  752. "Transcode a KEYWORD element from Org to Texinfo.
  753. CONTENTS is nil. INFO is a plist holding contextual information."
  754. (let ((key (org-element-property :key keyword))
  755. (value (org-element-property :value keyword)))
  756. (cond
  757. ((string= key "TEXINFO") value)
  758. ((string= key "CINDEX") (format "@cindex %s" value))
  759. ((string= key "FINDEX") (format "@findex %s" value))
  760. ((string= key "KINDEX") (format "@kindex %s" value))
  761. ((string= key "PINDEX") (format "@pindex %s" value))
  762. ((string= key "TINDEX") (format "@tindex %s" value))
  763. ((string= key "VINDEX") (format "@vindex %s" value)))))
  764. ;;;; Line Break
  765. (defun org-texinfo-line-break (line-break contents info)
  766. "Transcode a LINE-BREAK object from Org to Texinfo.
  767. CONTENTS is nil. INFO is a plist holding contextual information."
  768. "@*\n")
  769. ;;;; Link
  770. (defun org-texinfo-link (link desc info)
  771. "Transcode a LINK object from Org to Texinfo.
  772. DESC is the description part of the link, or the empty string.
  773. INFO is a plist holding contextual information. See
  774. `org-export-data'."
  775. (let* ((type (org-element-property :type link))
  776. (raw-path (org-element-property :path link))
  777. ;; Ensure DESC really exists, or set it to nil.
  778. (desc (and (not (string= desc "")) desc))
  779. (path (cond
  780. ((member type '("http" "https" "ftp"))
  781. (concat type ":" raw-path))
  782. ((and (string= type "file") (file-name-absolute-p raw-path))
  783. (concat "file:" raw-path))
  784. (t raw-path))))
  785. (cond
  786. ((org-export-custom-protocol-maybe link desc info))
  787. ((equal type "radio")
  788. (let ((destination (org-export-resolve-radio-link link info)))
  789. (if (not destination) desc
  790. (format "@ref{%s,,%s}"
  791. (org-texinfo--get-node destination info)
  792. desc))))
  793. ((member type '("custom-id" "id" "fuzzy"))
  794. (let ((destination
  795. (if (equal type "fuzzy")
  796. (org-export-resolve-fuzzy-link link info)
  797. (org-export-resolve-id-link link info))))
  798. (case (org-element-type destination)
  799. ((nil)
  800. (format org-texinfo-link-with-unknown-path-format
  801. (org-texinfo--sanitize-content path)))
  802. ;; Id link points to an external file.
  803. (plain-text
  804. (if desc (format "@uref{file://%s,%s}" destination desc)
  805. (format "@uref{file://%s}" destination)))
  806. (headline
  807. (format "@ref{%s,%s}"
  808. (org-texinfo--get-node destination info)
  809. (cond
  810. (desc)
  811. ((org-export-numbered-headline-p destination info)
  812. (org-export-data
  813. (org-element-property :title destination) info))
  814. (t
  815. (mapconcat
  816. #'number-to-string
  817. (org-export-get-headline-number destination info) ".")))))
  818. (otherwise
  819. (let ((topic
  820. (or desc
  821. (if (and (eq (org-element-type destination) 'headline)
  822. (not (org-export-numbered-headline-p
  823. destination info)))
  824. (org-export-data
  825. (org-element-property :title destination) info))
  826. (let ((n (org-export-get-ordinal destination info)))
  827. (cond
  828. ((not n) nil)
  829. ((integerp n) n)
  830. (t (mapconcat #'number-to-string n ".")))))))
  831. (when topic
  832. (format "@ref{%s,,%s}"
  833. (org-texinfo--get-node destination info)
  834. topic)))))))
  835. ((equal type "info")
  836. (let* ((info-path (split-string path "[:#]"))
  837. (info-manual (car info-path))
  838. (info-node (or (cadr info-path) "top"))
  839. (title (or desc "")))
  840. (format "@ref{%s,%s,,%s,}" info-node title info-manual)))
  841. ((string= type "mailto")
  842. (format "@email{%s}"
  843. (concat (org-texinfo--sanitize-content path)
  844. (and desc (concat "," desc)))))
  845. ;; External link with a description part.
  846. ((and path desc) (format "@uref{%s,%s}" path desc))
  847. ;; External link without a description part.
  848. (path (format "@uref{%s}" path))
  849. ;; No path, only description. Try to do something useful.
  850. (t
  851. (format (plist-get info :texinfo-link-with-unknown-path-format) desc)))))
  852. ;;;; Menu
  853. (defun org-texinfo-make-menu (scope info &optional master)
  854. "Create the menu for inclusion in the Texinfo document.
  855. SCOPE is a headline or a full parse tree. INFO is the
  856. communication channel, as a plist.
  857. When optional argument MASTER is non-nil, generate a master menu,
  858. including detailed node listing."
  859. (let ((menu (org-texinfo--build-menu scope info)))
  860. (when (org-string-nw-p menu)
  861. (org-element-normalize-string
  862. (format
  863. "@menu\n%s@end menu"
  864. (concat menu
  865. (when master
  866. (let ((detailmenu
  867. (org-texinfo--build-menu
  868. scope info
  869. (let ((toc-depth (plist-get info :with-toc)))
  870. (if (wholenump toc-depth) toc-depth
  871. org-texinfo-max-toc-depth)))))
  872. (when (org-string-nw-p detailmenu)
  873. (concat "\n@detailmenu\n"
  874. "--- The Detailed Node Listing ---\n\n"
  875. detailmenu
  876. "@end detailmenu\n"))))))))))
  877. (defun org-texinfo--build-menu (scope info &optional level)
  878. "Build menu for entries within SCOPE.
  879. SCOPE is a headline or a full parse tree. INFO is a plist
  880. containing contextual information. When optional argument LEVEL
  881. is an integer, build the menu recursively, down to this depth."
  882. (cond
  883. ((not level)
  884. (org-texinfo--format-entries (org-texinfo--menu-entries scope info) info))
  885. ((zerop level) nil)
  886. (t
  887. (org-element-normalize-string
  888. (mapconcat
  889. (lambda (h)
  890. (let ((entries (org-texinfo--menu-entries h info)))
  891. (when entries
  892. (concat
  893. (format "%s\n\n%s\n"
  894. (org-export-data (org-export-get-alt-title h info) info)
  895. (org-texinfo--format-entries entries info))
  896. (org-texinfo--build-menu h info (1- level))))))
  897. (org-texinfo--menu-entries scope info) "")))))
  898. (defun org-texinfo--format-entries (entries info)
  899. "Format all direct menu entries in SCOPE, as a string.
  900. SCOPE is either a headline or a full Org document. INFO is
  901. a plist containing contextual information."
  902. (org-element-normalize-string
  903. (mapconcat
  904. (lambda (h)
  905. (let* ((title (org-export-data
  906. (org-export-get-alt-title h info) info))
  907. (node (org-texinfo--get-node h info))
  908. (entry (concat "* " title ":"
  909. (if (string= title node) ":"
  910. (concat " " node ". "))))
  911. (desc (org-element-property :DESCRIPTION h)))
  912. (if (not desc) entry
  913. (format (format "%%-%ds %%s" org-texinfo-node-description-column)
  914. entry desc))))
  915. entries "\n")))
  916. (defun org-texinfo--menu-entries (scope info)
  917. "List direct children in SCOPE needing a menu entry.
  918. SCOPE is a headline or a full parse tree. INFO is a plist
  919. holding contextual information."
  920. (let* ((cache (or (plist-get info :texinfo-entries-cache)
  921. (plist-get (plist-put info :texinfo-entries-cache
  922. (make-hash-table :test #'eq))
  923. :texinfo-entries-cache)))
  924. (cached-entries (gethash scope cache 'no-cache)))
  925. (if (not (eq cached-entries 'no-cache)) cached-entries
  926. (puthash scope
  927. (org-element-map (org-element-contents scope) 'headline
  928. (lambda (h)
  929. (and (not (org-not-nil (org-element-property :COPYING h)))
  930. (not (org-element-property :footnote-section-p h))
  931. (not (org-export-low-level-p h info))
  932. h))
  933. info nil 'headline)
  934. cache))))
  935. ;;;; Node Property
  936. (defun org-texinfo-node-property (node-property contents info)
  937. "Transcode a NODE-PROPERTY element from Org to Texinfo.
  938. CONTENTS is nil. INFO is a plist holding contextual
  939. information."
  940. (format "%s:%s"
  941. (org-element-property :key node-property)
  942. (let ((value (org-element-property :value node-property)))
  943. (if value (concat " " value) ""))))
  944. ;;;; Paragraph
  945. (defun org-texinfo-paragraph (paragraph contents info)
  946. "Transcode a PARAGRAPH element from Org to Texinfo.
  947. CONTENTS is the contents of the paragraph, as a string. INFO is
  948. the plist used as a communication channel."
  949. contents)
  950. ;;;; Plain List
  951. (defun org-texinfo-plain-list (plain-list contents info)
  952. "Transcode a PLAIN-LIST element from Org to Texinfo.
  953. CONTENTS is the contents of the list. INFO is a plist holding
  954. contextual information."
  955. (let* ((attr (org-export-read-attribute :attr_texinfo plain-list))
  956. (indic (or (plist-get attr :indic)
  957. (plist-get info :texinfo-def-table-markup)))
  958. (table-type (plist-get attr :table-type))
  959. (type (org-element-property :type plain-list))
  960. (list-type (cond
  961. ((eq type 'ordered) "enumerate")
  962. ((eq type 'unordered) "itemize")
  963. ((member table-type '("ftable" "vtable")) table-type)
  964. (t "table"))))
  965. (format "@%s\n%s@end %s"
  966. (if (eq type 'descriptive) (concat list-type " " indic) list-type)
  967. contents
  968. list-type)))
  969. ;;;; Plain Text
  970. (defun org-texinfo-plain-text (text info)
  971. "Transcode a TEXT string from Org to Texinfo.
  972. TEXT is the string to transcode. INFO is a plist holding
  973. contextual information."
  974. ;; First protect @, { and }.
  975. (let ((output (org-texinfo--sanitize-content text)))
  976. ;; Activate smart quotes. Be sure to provide original TEXT string
  977. ;; since OUTPUT may have been modified.
  978. (when (plist-get info :with-smart-quotes)
  979. (setq output
  980. (org-export-activate-smart-quotes output :texinfo info text)))
  981. ;; LaTeX into @LaTeX{} and TeX into @TeX{}
  982. (let ((case-fold-search nil)
  983. (start 0))
  984. (while (string-match "\\(\\(?:La\\)?TeX\\)" output start)
  985. (setq output (replace-match
  986. (format "@%s{}" (match-string 1 output)) nil t output)
  987. start (match-end 0))))
  988. ;; Convert special strings.
  989. (when (plist-get info :with-special-strings)
  990. (while (string-match (regexp-quote "...") output)
  991. (setq output (replace-match "@dots{}" nil t output))))
  992. ;; Handle break preservation if required.
  993. (when (plist-get info :preserve-breaks)
  994. (setq output (replace-regexp-in-string
  995. "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" output)))
  996. ;; Return value.
  997. output))
  998. ;;;; Planning
  999. (defun org-texinfo-planning (planning contents info)
  1000. "Transcode a PLANNING element from Org to Texinfo.
  1001. CONTENTS is nil. INFO is a plist holding contextual
  1002. information."
  1003. (concat
  1004. "@noindent"
  1005. (mapconcat
  1006. 'identity
  1007. (delq nil
  1008. (list
  1009. (let ((closed (org-element-property :closed planning)))
  1010. (when closed
  1011. (concat
  1012. (format "@strong{%s} " org-closed-string)
  1013. (format (plist-get info :texinfo-inactive-timestamp-format)
  1014. (org-timestamp-translate closed)))))
  1015. (let ((deadline (org-element-property :deadline planning)))
  1016. (when deadline
  1017. (concat
  1018. (format "@strong{%s} " org-deadline-string)
  1019. (format (plist-get info :texinfo-active-timestamp-format)
  1020. (org-timestamp-translate deadline)))))
  1021. (let ((scheduled (org-element-property :scheduled planning)))
  1022. (when scheduled
  1023. (concat
  1024. (format "@strong{%s} " org-scheduled-string)
  1025. (format (plist-get info :texinfo-active-timestamp-format)
  1026. (org-timestamp-translate scheduled)))))))
  1027. " ")
  1028. "@*"))
  1029. ;;;; Property Drawer
  1030. (defun org-texinfo-property-drawer (property-drawer contents info)
  1031. "Transcode a PROPERTY-DRAWER element from Org to Texinfo.
  1032. CONTENTS holds the contents of the drawer. INFO is a plist
  1033. holding contextual information."
  1034. (and (org-string-nw-p contents)
  1035. (format "@verbatim\n%s@end verbatim" contents)))
  1036. ;;;; Quote Block
  1037. (defun org-texinfo-quote-block (quote-block contents info)
  1038. "Transcode a QUOTE-BLOCK element from Org to Texinfo.
  1039. CONTENTS holds the contents of the block. INFO is a plist
  1040. holding contextual information."
  1041. (let* ((title (org-element-property :name quote-block))
  1042. (start-quote (concat "@quotation"
  1043. (if title
  1044. (format " %s" title)))))
  1045. (format "%s\n%s@end quotation" start-quote contents)))
  1046. ;;;; Radio Target
  1047. (defun org-texinfo-radio-target (radio-target text info)
  1048. "Transcode a RADIO-TARGET object from Org to Texinfo.
  1049. TEXT is the text of the target. INFO is a plist holding
  1050. contextual information."
  1051. (format "@anchor{%s}%s"
  1052. (org-export-solidify-link-text
  1053. (org-element-property :value radio-target))
  1054. text))
  1055. ;;;; Section
  1056. (defun org-texinfo-section (section contents info)
  1057. "Transcode a SECTION element from Org to Texinfo.
  1058. CONTENTS holds the contents of the section. INFO is a plist
  1059. holding contextual information."
  1060. (concat contents
  1061. (let ((parent (org-export-get-parent-headline section)))
  1062. (and parent (org-texinfo-make-menu parent info)))))
  1063. ;;;; Special Block
  1064. (defun org-texinfo-special-block (special-block contents info)
  1065. "Transcode a SPECIAL-BLOCK element from Org to Texinfo.
  1066. CONTENTS holds the contents of the block. INFO is a plist used
  1067. as a communication channel."
  1068. (let ((type (org-element-property :type special-block)))
  1069. (format "@%s\n%s@end %s" type contents type)))
  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