ox-texinfo.el 65 KB

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