ox-texinfo.el 56 KB

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