ox-texinfo.el 65 KB

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