ox-texinfo.el 65 KB

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