ox-texinfo.el 63 KB

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