ox-texinfo.el 58 KB

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