ox-texinfo.el 59 KB

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