ox-texinfo.el 64 KB

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