ox-texinfo.el 56 KB

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