ox-texinfo.el 59 KB

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