ox-texinfo.el 56 KB

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