ox-texinfo.el 56 KB

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