ox-texinfo.el 66 KB

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