org-export.el 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  1. ;;; org-export.el --- Generic Export Engine For Org
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a generic export engine for Org, built on
  17. ;; its syntactical parser: Org Elements.
  18. ;; Besides that parser, the generic exporter is made of three distinct
  19. ;; parts:
  20. ;; - The communication channel consists in a property list, which is
  21. ;; created and updated during the process. Its use is to offer
  22. ;; every piece of information, would it be export options or
  23. ;; contextual data, all in a single place. The exhaustive list of
  24. ;; properties is given in "The Communication Channel" section of
  25. ;; this file.
  26. ;; - The transcoder walks the parse tree, ignores or treat as plain
  27. ;; text elements and objects according to export options, and
  28. ;; eventually calls back-end specific functions to do the real
  29. ;; transcoding, concatenating their return value along the way.
  30. ;; - The filter system is activated at the very beginning and the very
  31. ;; end of the export process, and each time an element or an object
  32. ;; has been converted. It is the entry point to fine-tune standard
  33. ;; output from back-end transcoders.
  34. ;; The core function is `org-export-as'. It returns the transcoded
  35. ;; buffer as a string.
  36. ;; In order to derive an exporter out of this generic implementation,
  37. ;; one can define a transcode function for each element or object.
  38. ;; Such function should return a string for the corresponding element,
  39. ;; without any trailing space, or nil. It must accept three
  40. ;; arguments:
  41. ;; 1. the element or object itself,
  42. ;; 2. its contents, or nil when it isn't recursive,
  43. ;; 3. the property list used as a communication channel.
  44. ;; If no such function is found, that element or object type will
  45. ;; simply be ignored, along with any separating blank line. The same
  46. ;; will happen if the function returns the nil value. If that
  47. ;; function returns the empty string, the type will be ignored, but
  48. ;; the blank lines will be kept.
  49. ;; Contents, when not nil, are stripped from any global indentation
  50. ;; (although the relative one is preserved). They also always end
  51. ;; with a single newline character.
  52. ;; These functions must follow a strict naming convention:
  53. ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
  54. ;; export back-end and TYPE the type of the element or object handled.
  55. ;; Moreover, two additional functions can be defined. On the one
  56. ;; hand, `org-BACKEND-template' returns the final transcoded string,
  57. ;; and can be used to add a preamble and a postamble to document's
  58. ;; body. It must accept two arguments: the transcoded string and the
  59. ;; property list containing export options. On the other hand,
  60. ;; `org-BACKEND-plain-text', when defined, is to be called on every
  61. ;; text not recognized as an element or an object. It must accept two
  62. ;; arguments: the text string and the information channel.
  63. ;; Any back-end can define its own variables. Among them, those
  64. ;; customizables should belong to the `org-export-BACKEND' group.
  65. ;; Also, a special variable, `org-BACKEND-option-alist', allows to
  66. ;; define buffer keywords and "#+options:" items specific to that
  67. ;; back-end. See `org-export-option-alist' for supported defaults and
  68. ;; syntax.
  69. ;; Tools for common tasks across back-ends are implemented in the
  70. ;; penultimate part of this file. A dispatcher for standard back-ends
  71. ;; is provided in the last one.
  72. ;;; Code:
  73. (eval-when-compile (require 'cl))
  74. (require 'org-element)
  75. ;; Require major back-ends
  76. (require 'org-e-ascii "../../EXPERIMENTAL/org-e-ascii.el")
  77. (require 'org-e-latex "../../EXPERIMENTAL/org-e-latex.el")
  78. ;;; Internal Variables
  79. ;; Among internal variables, the most important is
  80. ;; `org-export-option-alist'. This variable define the global export
  81. ;; options, shared between every exporter, and how they are acquired.
  82. (defconst org-export-max-depth 19
  83. "Maximum nesting depth for headlines, counting from 0.")
  84. (defconst org-export-option-alist
  85. '((:author "AUTHOR" nil user-full-name t)
  86. (:creator "CREATOR" nil org-export-creator-string)
  87. (:date "DATE" nil nil t)
  88. (:description "DESCRIPTION" nil nil newline)
  89. (:email "EMAIL" nil user-mail-address t)
  90. (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
  91. (:filter-babel-call nil nil org-export-filter-babel-call-functions)
  92. (:filter-center-block nil nil org-export-filter-center-block-functions)
  93. (:filter-comment nil nil org-export-filter-comment-functions)
  94. (:filter-comment-block nil nil org-export-filter-comment-block-functions)
  95. (:filter-drawer nil nil org-export-filter-drawer-functions)
  96. (:filter-dynamic-block nil nil org-export-filter-dynamic-block-functions)
  97. (:filter-emphasis nil nil org-export-filter-emphasis-functions)
  98. (:filter-entity nil nil org-export-filter-entity-functions)
  99. (:filter-example-block nil nil org-export-filter-example-block-functions)
  100. (:filter-export-block nil nil org-export-filter-export-block-functions)
  101. (:filter-export-snippet nil nil org-export-filter-export-snippet-functions)
  102. (:filter-final-output nil nil org-export-filter-final-output-functions)
  103. (:filter-fixed-width nil nil org-export-filter-fixed-width-functions)
  104. (:filter-footnote-definition nil nil org-export-filter-footnote-definition-functions)
  105. (:filter-footnote-reference nil nil org-export-filter-footnote-reference-functions)
  106. (:filter-headline nil nil org-export-filter-headline-functions)
  107. (:filter-horizontal-rule nil nil org-export-filter-horizontal-rule-functions)
  108. (:filter-inline-babel-call nil nil org-export-filter-inline-babel-call-functions)
  109. (:filter-inline-src-block nil nil org-export-filter-inline-src-block-functions)
  110. (:filter-inlinetask nil nil org-export-filter-inlinetask-functions)
  111. (:filter-item nil nil org-export-filter-item-functions)
  112. (:filter-keyword nil nil org-export-filter-keyword-functions)
  113. (:filter-latex-environment nil nil org-export-filter-latex-environment-functions)
  114. (:filter-latex-fragment nil nil org-export-filter-latex-fragment-functions)
  115. (:filter-line-break nil nil org-export-filter-line-break-functions)
  116. (:filter-link nil nil org-export-filter-link-functions)
  117. (:filter-macro nil nil org-export-filter-macro-functions)
  118. (:filter-paragraph nil nil org-export-filter-paragraph-functions)
  119. (:filter-parse-tree nil nil org-export-filter-parse-tree-functions)
  120. (:filter-plain-list nil nil org-export-filter-plain-list-functions)
  121. (:filter-plain-text nil nil org-export-filter-plain-text-functions)
  122. (:filter-property-drawer nil nil org-export-filter-property-drawer-functions)
  123. (:filter-quote-block nil nil org-export-filter-quote-block-functions)
  124. (:filter-quote-section nil nil org-export-filter-quote-section-functions)
  125. (:filter-radio-target nil nil org-export-filter-radio-target-functions)
  126. (:filter-section nil nil org-export-filter-section-functions)
  127. (:filter-special-block nil nil org-export-filter-special-block-functions)
  128. (:filter-src-block nil nil org-export-filter-src-block-functions)
  129. (:filter-statistics-cookie nil nil org-export-filter-statistics-cookie-functions)
  130. (:filter-subscript nil nil org-export-filter-subscript-functions)
  131. (:filter-superscript nil nil org-export-filter-superscript-functions)
  132. (:filter-table nil nil org-export-filter-table-functions)
  133. (:filter-target nil nil org-export-filter-target-functions)
  134. (:filter-time-stamp nil nil org-export-filter-time-stamp-functions)
  135. (:filter-verbatim nil nil org-export-filter-verbatim-functions)
  136. (:filter-verse-block nil nil org-export-filter-verse-block-functions)
  137. (:headline-levels nil "H" org-export-headline-levels)
  138. (:keywords "KEYWORDS" nil nil space)
  139. (:language "LANGUAGE" nil org-export-default-language t)
  140. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  141. (:section-numbers nil "num" org-export-with-section-numbers)
  142. (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
  143. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  144. (:title "TITLE" nil nil space)
  145. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  146. (:with-author nil "author" org-export-with-author)
  147. (:with-creator nil "creator" org-export-with-creator)
  148. (:with-drawers nil "d" org-export-with-drawers)
  149. (:with-email nil "email" org-export-with-email)
  150. (:with-emphasize nil "*" org-export-with-emphasize)
  151. (:with-entities nil "e" org-export-with-entities)
  152. (:with-fixed-width nil ":" org-export-with-fixed-width)
  153. (:with-footnotes nil "f" org-export-with-footnotes)
  154. (:with-priority nil "pri" org-export-with-priority)
  155. (:with-special-strings nil "-" org-export-with-special-strings)
  156. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  157. (:with-toc nil "toc" org-export-with-toc)
  158. (:with-tables nil "|" org-export-with-tables)
  159. (:with-tags nil "tags" org-export-with-tags)
  160. (:with-tasks nil "tasks" org-export-with-tasks)
  161. (:with-timestamps nil "<" org-export-with-timestamps)
  162. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  163. "Alist between export properties and ways to set them.
  164. The car of the alist is the property name, and the cdr is a list
  165. like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
  166. KEYWORD is a string representing a buffer keyword, or nil.
  167. OPTION is a string that could be found in an #+OPTIONS: line.
  168. DEFAULT is the default value for the property.
  169. BEHAVIOUR determine how Org should handle multiple keywords for
  170. the same property. It is a symbol among:
  171. nil Keep old value and discard the new one.
  172. t Replace old value with the new one.
  173. `space' Concatenate the values, separating them with a space.
  174. `newline' Concatenate the values, separating them with
  175. a newline.
  176. `split' Split values at white spaces, and cons them to the
  177. previous list.
  178. KEYWORD and OPTION have precedence over DEFAULT.
  179. All these properties should be back-end agnostic. For back-end
  180. specific properties, define a similar variable named
  181. `org-BACKEND-option-alist', replacing BACKEND with the name of
  182. the appropriate back-end. You can also redefine properties
  183. there, as they have precedence over these.")
  184. (defconst org-export-special-keywords
  185. '("SETUP_FILE" "OPTIONS" "MACRO")
  186. "List of in-buffer keywords that require special treatment.
  187. These keywords are not directly associated to a property. The
  188. way they are handled must be hard-coded into
  189. `org-export-get-inbuffer-options' function.")
  190. ;;; User-configurable Variables
  191. ;; Configuration for the masses.
  192. ;; They should never be evaled directly, as their value is to be
  193. ;; stored in a property list (cf. `org-export-option-alist').
  194. (defgroup org-export nil
  195. "Options for exporting Org mode files."
  196. :tag "Org Export"
  197. :group 'org)
  198. (defgroup org-export-general nil
  199. "General options for export engine."
  200. :tag "Org Export General"
  201. :group 'org-export)
  202. (defcustom org-export-with-archived-trees 'headline
  203. "Whether sub-trees with the ARCHIVE tag should be exported.
  204. This can have three different values:
  205. nil Do not export, pretend this tree is not present.
  206. t Do export the entire tree.
  207. `headline' Only export the headline, but skip the tree below it.
  208. This option can also be set with the #+OPTIONS line,
  209. e.g. \"arch:nil\"."
  210. :group 'org-export-general
  211. :type '(choice
  212. (const :tag "Not at all" nil)
  213. (const :tag "Headline only" 'headline)
  214. (const :tag "Entirely" t)))
  215. (defcustom org-export-with-author t
  216. "Non-nil means insert author name into the exported file.
  217. This option can also be set with the #+OPTIONS line,
  218. e.g. \"author:nil\"."
  219. :group 'org-export-general
  220. :type 'boolean)
  221. (defcustom org-export-with-creator 'comment
  222. "Non-nil means the postamble should contain a creator sentence.
  223. The sentence can be set in `org-export-creator-string' and
  224. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  225. If the value is `comment' insert it as a comment."
  226. :group 'org-export-general
  227. :type '(choice
  228. (const :tag "No creator sentence" nil)
  229. (const :tag "Sentence as a comment" 'comment)
  230. (const :tag "Insert the sentence" t)))
  231. (defcustom org-export-creator-string
  232. (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
  233. "String to insert at the end of the generated document."
  234. :group 'org-export-general
  235. :type '(string :tag "Creator string"))
  236. (defcustom org-export-with-drawers t
  237. "Non-nil means export contents of standard drawers.
  238. When t, all drawers are exported. This may also be a list of
  239. drawer names to export. This variable doesn't apply to
  240. properties drawers.
  241. This option can also be set with the #+OPTIONS line,
  242. e.g. \"d:nil\"."
  243. :group 'org-export-general
  244. :type '(choice
  245. (const :tag "All drawers" t)
  246. (const :tag "None" nil)
  247. (repeat :tag "Selected drawers"
  248. (string :tag "Drawer name"))))
  249. (defcustom org-export-with-email nil
  250. "Non-nil means insert author email into the exported file.
  251. This option can also be set with the #+OPTIONS line,
  252. e.g. \"email:t\"."
  253. :group 'org-export-general
  254. :type 'boolean)
  255. (defcustom org-export-with-emphasize t
  256. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  257. If the export target supports emphasizing text, the word will be
  258. typeset in bold, italic, or underlined, respectively. Not all
  259. export backends support this.
  260. This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
  261. :group 'org-export-general
  262. :type 'boolean)
  263. (defcustom org-export-exclude-tags '("noexport")
  264. "Tags that exclude a tree from export.
  265. All trees carrying any of these tags will be excluded from
  266. export. This is without condition, so even subtrees inside that
  267. carry one of the `org-export-select-tags' will be removed."
  268. :group 'org-export-general
  269. :type '(repeat (string :tag "Tag")))
  270. (defcustom org-export-with-fixed-width t
  271. "Non-nil means lines starting with \":\" will be in fixed width font.
  272. This can be used to have pre-formatted text, fragments of code
  273. etc. For example:
  274. : ;; Some Lisp examples
  275. : (while (defc cnt)
  276. : (ding))
  277. will be looking just like this in also HTML. See also the QUOTE
  278. keyword. Not all export backends support this.
  279. This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
  280. :group 'org-export-translation
  281. :type 'boolean)
  282. (defcustom org-export-with-footnotes t
  283. "Non-nil means Org footnotes should be exported.
  284. This option can also be set with the #+OPTIONS line,
  285. e.g. \"f:nil\"."
  286. :group 'org-export-general
  287. :type 'boolean)
  288. (defcustom org-export-headline-levels 3
  289. "The last level which is still exported as a headline.
  290. Inferior levels will produce itemize lists when exported. Note
  291. that a numeric prefix argument to an exporter function overrides
  292. this setting.
  293. This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
  294. :group 'org-export-general
  295. :type 'integer)
  296. (defcustom org-export-default-language "en"
  297. "The default language for export and clocktable translations, as a string.
  298. This may have an association in
  299. `org-clock-clocktable-language-setup'."
  300. :group 'org-export-general
  301. :type '(string :tag "Language"))
  302. (defcustom org-export-preserve-breaks nil
  303. "Non-nil means preserve all line breaks when exporting.
  304. Normally, in HTML output paragraphs will be reformatted.
  305. This option can also be set with the #+OPTIONS line,
  306. e.g. \"\\n:t\"."
  307. :group 'org-export-general
  308. :type 'boolean)
  309. (defcustom org-export-with-entities t
  310. "Non-nil means interpret entities when exporting.
  311. For example, HTML export converts \\alpha to &alpha; and \\AA to
  312. &Aring;.
  313. For a list of supported names, see the constant `org-entities'
  314. and the user option `org-entities-user'.
  315. This option can also be set with the #+OPTIONS line,
  316. e.g. \"e:nil\"."
  317. :group 'org-export-general
  318. :type 'boolean)
  319. (defcustom org-export-with-priority nil
  320. "Non-nil means include priority cookies in export.
  321. When nil, remove priority cookies for export."
  322. :group 'org-export-general
  323. :type 'boolean)
  324. (defcustom org-export-with-section-numbers t
  325. "Non-nil means add section numbers to headlines when exporting.
  326. When set to an integer n, numbering will only happen for
  327. headlines whose relative level is higher or equal to n.
  328. This option can also be set with the #+OPTIONS line,
  329. e.g. \"num:t\"."
  330. :group 'org-export-general
  331. :type 'boolean)
  332. (defcustom org-export-select-tags '("export")
  333. "Tags that select a tree for export.
  334. If any such tag is found in a buffer, all trees that do not carry
  335. one of these tags will be deleted before export. Inside trees
  336. that are selected like this, you can still deselect a subtree by
  337. tagging it with one of the `org-export-exclude-tags'."
  338. :group 'org-export-general
  339. :type '(repeat (string :tag "Tag")))
  340. (defcustom org-export-with-special-strings t
  341. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  342. When this option is turned on, these strings will be exported as:
  343. Org HTML LaTeX
  344. -----+----------+--------
  345. \\- &shy; \\-
  346. -- &ndash; --
  347. --- &mdash; ---
  348. ... &hellip; \ldots
  349. This option can also be set with the #+OPTIONS line,
  350. e.g. \"-:nil\"."
  351. :group 'org-export-general
  352. :type 'boolean)
  353. (defcustom org-export-with-sub-superscripts t
  354. "Non-nil means interpret \"_\" and \"^\" for export.
  355. When this option is turned on, you can use TeX-like syntax for
  356. sub- and superscripts. Several characters after \"_\" or \"^\"
  357. will be considered as a single item - so grouping with {} is
  358. normally not needed. For example, the following things will be
  359. parsed as single sub- or superscripts.
  360. 10^24 or 10^tau several digits will be considered 1 item.
  361. 10^-12 or 10^-tau a leading sign with digits or a word
  362. x^2-y^3 will be read as x^2 - y^3, because items are
  363. terminated by almost any nonword/nondigit char.
  364. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  365. Still, ambiguity is possible - so when in doubt use {} to enclose
  366. the sub/superscript. If you set this variable to the symbol
  367. `{}', the braces are *required* in order to trigger
  368. interpretations as sub/superscript. This can be helpful in
  369. documents that need \"_\" frequently in plain text.
  370. This option can also be set with the #+OPTIONS line,
  371. e.g. \"^:nil\"."
  372. :group 'org-export-general
  373. :type '(choice
  374. (const :tag "Interpret them" t)
  375. (const :tag "Curly brackets only" {})
  376. (const :tag "Do not interpret them" nil)))
  377. (defcustom org-export-with-toc t
  378. "Non-nil means create a table of contents in exported files.
  379. The TOC contains headlines with levels up
  380. to`org-export-headline-levels'. When an integer, include levels
  381. up to N in the toc, this may then be different from
  382. `org-export-headline-levels', but it will not be allowed to be
  383. larger than the number of headline levels. When nil, no table of
  384. contents is made.
  385. This option can also be set with the #+OPTIONS line,
  386. e.g. \"toc:nil\" or \"toc:3\"."
  387. :group 'org-export-general
  388. :type '(choice
  389. (const :tag "No Table of Contents" nil)
  390. (const :tag "Full Table of Contents" t)
  391. (integer :tag "TOC to level")))
  392. (defcustom org-export-with-tables t
  393. "If non-nil, lines starting with \"|\" define a table.
  394. For example:
  395. | Name | Address | Birthday |
  396. |-------------+----------+-----------|
  397. | Arthur Dent | England | 29.2.2100 |
  398. This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
  399. :group 'org-export-general
  400. :type 'boolean)
  401. (defcustom org-export-with-tags t
  402. "If nil, do not export tags, just remove them from headlines.
  403. If this is the symbol `not-in-toc', tags will be removed from
  404. table of contents entries, but still be shown in the headlines of
  405. the document.
  406. This option can also be set with the #+OPTIONS line,
  407. e.g. \"tags:nil\"."
  408. :group 'org-export-general
  409. :type '(choice
  410. (const :tag "Off" nil)
  411. (const :tag "Not in TOC" not-in-toc)
  412. (const :tag "On" t)))
  413. (defcustom org-export-with-tasks t
  414. "Non-nil means include TODO items for export.
  415. This may have the following values:
  416. t include tasks independent of state.
  417. todo include only tasks that are not yet done.
  418. done include only tasks that are already done.
  419. nil remove all tasks before export
  420. list of keywords keep only tasks with these keywords"
  421. :group 'org-export-general
  422. :type '(choice
  423. (const :tag "All tasks" t)
  424. (const :tag "No tasks" nil)
  425. (const :tag "Not-done tasks" todo)
  426. (const :tag "Only done tasks" done)
  427. (repeat :tag "Specific TODO keywords"
  428. (string :tag "Keyword"))))
  429. (defcustom org-export-time-stamp-file t
  430. "Non-nil means insert a time stamp into the exported file.
  431. The time stamp shows when the file was created.
  432. This option can also be set with the #+OPTIONS line,
  433. e.g. \"timestamp:nil\"."
  434. :group 'org-export-general
  435. :type 'boolean)
  436. (defcustom org-export-with-timestamps t
  437. "If nil, do not export time stamps and associated keywords."
  438. :group 'org-export-general
  439. :type 'boolean)
  440. (defcustom org-export-with-todo-keywords t
  441. "Non-nil means include TODO keywords in export.
  442. When nil, remove all these keywords from the export.")
  443. (defcustom org-export-allow-BIND 'confirm
  444. "Non-nil means allow #+BIND to define local variable values for export.
  445. This is a potential security risk, which is why the user must
  446. confirm the use of these lines."
  447. :group 'org-export-general
  448. :type '(choice
  449. (const :tag "Never" nil)
  450. (const :tag "Always" t)
  451. (const :tag "Ask a confirmation for each file" confirm)))
  452. (defcustom org-export-snippet-translation-alist nil
  453. "Alist between export snippets back-ends and exporter back-ends.
  454. This variable allows to provide shortcuts for export snippets.
  455. For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
  456. back-end will recognize the contents of \"@h{<b>}\" as HTML code
  457. while every other back-end will ignore it."
  458. :group 'org-export-general
  459. :type '(repeat
  460. (cons
  461. (string :tag "Shortcut")
  462. (string :tag "Back-end"))))
  463. (defcustom org-export-coding-system nil
  464. "Coding system for the exported file."
  465. :group 'org-export-general
  466. :type 'coding-system)
  467. (defcustom org-export-copy-to-kill-ring t
  468. "Non-nil means exported stuff will also be pushed onto the kill ring."
  469. :group 'org-export-general
  470. :type 'boolean)
  471. (defcustom org-export-initial-scope 'buffer
  472. "The initial scope when exporting with `org-export-dispatch'.
  473. This variable can be either set to `buffer' or `subtree'."
  474. :group 'org-export-general
  475. :type '(choice
  476. (const :tag "Export current buffer" 'buffer)
  477. (const :tag "Export current subtree" 'subtree)))
  478. (defcustom org-export-show-temporary-export-buffer t
  479. "Non-nil means show buffer after exporting to temp buffer.
  480. When Org exports to a file, the buffer visiting that file is ever
  481. shown, but remains buried. However, when exporting to a temporary
  482. buffer, that buffer is popped up in a second window. When this variable
  483. is nil, the buffer remains buried also in these cases."
  484. :group 'org-export-general
  485. :type 'boolean)
  486. (defcustom org-export-dispatch-use-expert-ui nil
  487. "Non-nil means using a non-intrusive `org-export-dispatch'.
  488. In that case, no help buffer is displayed. Though, an indicator
  489. for current export scope is added to the prompt \(i.e. \"b\" when
  490. output is restricted to body only, \"s\" when it is restricted to
  491. the current subtree and \"v\" when only visible elements are
  492. considered for export\). Also, \[?] allows to switch back to
  493. standard mode."
  494. :group 'org-export-general
  495. :type 'boolean)
  496. ;;; The Communication Channel
  497. ;; During export process, every function has access to a number of
  498. ;; properties. They are of three types:
  499. ;; 1. Export options are collected once at the very beginning of the
  500. ;; process, out of the original buffer and environment. The task
  501. ;; is handled by `org-export-collect-options' function.
  502. ;;
  503. ;; All export options are defined through the
  504. ;; `org-export-option-alist' variable.
  505. ;;
  506. ;; 2. Tree properties are extracted directly from the parsed tree, by
  507. ;; `org-export-collect-tree-properties' and depend on export
  508. ;; options (whole trees may be filtered out of the export process).
  509. ;;
  510. ;; 3. Local options are updated during parsing, and their value
  511. ;; depends on the level of recursion. For now, only `:genealogy'
  512. ;; belongs to that category.
  513. ;; Here is the full list of properties available during transcode
  514. ;; process, with their category (option, tree or local), their
  515. ;; value type and the function updating them, when appropriate.
  516. ;; + `author' :: Author's name.
  517. ;; - category :: option
  518. ;; - type :: string
  519. ;; + `back-end' :: Current back-end used for transcoding.
  520. ;; - category :: tree
  521. ;; - type :: symbol
  522. ;; + `creator' :: String to write as creation information.
  523. ;; - category :: option
  524. ;; - type :: string
  525. ;; + `date' :: String to use as date.
  526. ;; - category :: option
  527. ;; - type :: string
  528. ;; + `description' :: Description text for the current data.
  529. ;; - category :: option
  530. ;; - type :: string
  531. ;; + `email' :: Author's email.
  532. ;; - category :: option
  533. ;; - type :: string
  534. ;; + `exclude-tags' :: Tags for exclusion of subtrees from export
  535. ;; process.
  536. ;; - category :: option
  537. ;; - type :: list of strings
  538. ;; + `footnote-definition-alist' :: Alist between footnote labels and
  539. ;; their definition, as parsed data. Only non-inlined footnotes
  540. ;; are represented in this alist. Also, every definition isn't
  541. ;; guaranteed to be referenced in the parse tree. The purpose of
  542. ;; this property is to preserve definitions from oblivion
  543. ;; (i.e. when the parse tree comes from a part of the original
  544. ;; buffer), it isn't meant for direct use in a back-end. To
  545. ;; retrieve a definition relative to a reference, use
  546. ;; `org-export-get-footnote-definition' instead.
  547. ;; - category :: option
  548. ;; - type :: alist (STRING . LIST)
  549. ;; + `genealogy' :: Flat list of current object or element's parents
  550. ;; from closest to farthest.
  551. ;; - category :: local
  552. ;; - type :: list of elements and objects
  553. ;; + `headline-levels' :: Maximum level being exported as an
  554. ;; headline. Comparison is done with the relative level of
  555. ;; headlines in the parse tree, not necessarily with their
  556. ;; actual level.
  557. ;; - category :: option
  558. ;; - type :: integer
  559. ;; + `headline-offset' :: Difference between relative and real level
  560. ;; of headlines in the parse tree. For example, a value of -1
  561. ;; means a level 2 headline should be considered as level
  562. ;; 1 (cf. `org-export-get-relative-level').
  563. ;; - category :: tree
  564. ;; - type :: integer
  565. ;; + `headline-numbering' :: Alist between headlines' beginning
  566. ;; position and their numbering, as a list of numbers
  567. ;; (cf. `org-export-get-headline-number').
  568. ;; - category :: tree
  569. ;; - type :: alist (INTEGER . LIST)
  570. ;; + `input-file' :: Full path to input file, if any.
  571. ;; - category :: option
  572. ;; - type :: string or nil
  573. ;; + `keywords' :: List of keywords attached to data.
  574. ;; - category :: option
  575. ;; - type :: string
  576. ;; + `language' :: Default language used for translations.
  577. ;; - category :: option
  578. ;; - type :: string
  579. ;; + `macro-input-file' :: Macro returning file name of input file, or
  580. ;; nil.
  581. ;; - category :: option
  582. ;; - type :: string or nil
  583. ;; + `parse-tree' :: Whole parse tree, available at any time during
  584. ;; transcoding.
  585. ;; - category :: global
  586. ;; - type :: list (as returned by `org-element-parse-buffer')
  587. ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
  588. ;; all line breaks.
  589. ;; - category :: option
  590. ;; - type :: symbol (nil, t)
  591. ;; + `section-numbers' :: Non-nil means transcoding should add
  592. ;; section numbers to headlines.
  593. ;; - category :: option
  594. ;; - type :: symbol (nil, t)
  595. ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
  596. ;; transcoding. When such a tag is present,
  597. ;; subtrees without it are de facto excluded from
  598. ;; the process. See `use-select-tags'.
  599. ;; - category :: option
  600. ;; - type :: list of strings
  601. ;; + `target-list' :: List of targets encountered in the parse tree.
  602. ;; This is used to partly resolve "fuzzy" links
  603. ;; (cf. `org-export-resolve-fuzzy-link').
  604. ;; - category :: tree
  605. ;; - type :: list of strings
  606. ;; + `time-stamp-file' :: Non-nil means transcoding should insert
  607. ;; a time stamp in the output.
  608. ;; - category :: option
  609. ;; - type :: symbol (nil, t)
  610. ;; + `use-select-tags' :: When non-nil, a select tags has been found
  611. ;; in the parse tree. Thus, any headline without one will be
  612. ;; filtered out. See `select-tags'.
  613. ;; - category :: tree
  614. ;; - type :: interger or nil
  615. ;; + `with-archived-trees' :: Non-nil when archived subtrees should
  616. ;; also be transcoded. If it is set to the `headline' symbol,
  617. ;; only the archived headline's name is retained.
  618. ;; - category :: option
  619. ;; - type :: symbol (nil, t, `headline')
  620. ;; + `with-author' :: Non-nil means author's name should be included
  621. ;; in the output.
  622. ;; - category :: option
  623. ;; - type :: symbol (nil, t)
  624. ;; + `with-creator' :: Non-nild means a creation sentence should be
  625. ;; inserted at the end of the transcoded string. If the value
  626. ;; is `comment', it should be commented.
  627. ;; - category :: option
  628. ;; - type :: symbol (`comment', nil, t)
  629. ;; + `with-drawers' :: Non-nil means drawers should be exported. If
  630. ;; its value is a list of names, only drawers with such names
  631. ;; will be transcoded.
  632. ;; - category :: option
  633. ;; - type :: symbol (nil, t) or list of strings
  634. ;; + `with-email' :: Non-nil means output should contain author's
  635. ;; email.
  636. ;; - category :: option
  637. ;; - type :: symbol (nil, t)
  638. ;; + `with-emphasize' :: Non-nil means emphasized text should be
  639. ;; interpreted.
  640. ;; - category :: option
  641. ;; - type :: symbol (nil, t)
  642. ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
  643. ;; strings starting with a colon as a fixed-with (verbatim)
  644. ;; area.
  645. ;; - category :: option
  646. ;; - type :: symbol (nil, t)
  647. ;; + `with-footnotes' :: Non-nil if transcoder should interpret
  648. ;; footnotes.
  649. ;; - category :: option
  650. ;; - type :: symbol (nil, t)
  651. ;; + `with-priority' :: Non-nil means transcoding should include
  652. ;; priority cookies.
  653. ;; - category :: option
  654. ;; - type :: symbol (nil, t)
  655. ;; + `with-special-strings' :: Non-nil means transcoding should
  656. ;; interpret special strings in plain text.
  657. ;; - category :: option
  658. ;; - type :: symbol (nil, t)
  659. ;; + `with-sub-superscript' :: Non-nil means transcoding should
  660. ;; interpret subscript and superscript. With a value of "{}",
  661. ;; only interpret those using curly brackets.
  662. ;; - category :: option
  663. ;; - type :: symbol (nil, {}, t)
  664. ;; + `with-tables' :: Non-nil means transcoding should interpret
  665. ;; tables.
  666. ;; - category :: option
  667. ;; - type :: symbol (nil, t)
  668. ;; + `with-tags' :: Non-nil means transcoding should keep tags in
  669. ;; headlines. A `not-in-toc' value will remove them
  670. ;; from the table of contents, if any, nonetheless.
  671. ;; - category :: option
  672. ;; - type :: symbol (nil, t, `not-in-toc')
  673. ;; + `with-tasks' :: Non-nil means transcoding should include
  674. ;; headlines with a TODO keyword. A `todo' value
  675. ;; will only include headlines with a todo type
  676. ;; keyword while a `done' value will do the
  677. ;; contrary. If a list of strings is provided, only
  678. ;; tasks with keywords belonging to that list will
  679. ;; be kept.
  680. ;; - category :: option
  681. ;; - type :: symbol (t, todo, done, nil) or list of strings
  682. ;; + `with-timestamps' :: Non-nil means transcoding should include
  683. ;; time stamps and associated keywords. Otherwise, completely
  684. ;; remove them.
  685. ;; - category :: option
  686. ;; - type :: symbol: (t, nil)
  687. ;; + `with-toc' :: Non-nil means that a table of contents has to be
  688. ;; added to the output. An integer value limits its
  689. ;; depth.
  690. ;; - category :: option
  691. ;; - type :: symbol (nil, t or integer)
  692. ;; + `with-todo-keywords' :: Non-nil means transcoding should
  693. ;; include TODO keywords.
  694. ;; - category :: option
  695. ;; - type :: symbol (nil, t)
  696. ;;;; Export Options
  697. ;; Export options come from five sources, in increasing precedence
  698. ;; order:
  699. ;; - Global variables,
  700. ;; - External options provided at export time,
  701. ;; - Options keyword symbols,
  702. ;; - Buffer keywords,
  703. ;; - Subtree properties.
  704. ;; The central internal function with regards to export options is
  705. ;; `org-export-collect-options'. It updates global variables with
  706. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  707. ;; the different sources.
  708. ;; The internal functions doing the retrieval are:
  709. ;; `org-export-parse-option-keyword' ,
  710. ;; `org-export-get-subtree-options' ,
  711. ;; `org-export-get-inbuffer-options' and
  712. ;; `org-export-get-global-options'.
  713. ;;
  714. ;; Some properties do not rely on the previous sources but still
  715. ;; depend on the original buffer are taken care of in
  716. ;; `org-export-initial-options'.
  717. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  718. ;; take care of the part relative to "#+BIND:" keywords.
  719. (defun org-export-collect-options (backend subtreep ext-plist)
  720. "Collect export options from the current buffer.
  721. BACKEND is a symbol specifying the back-end to use.
  722. When SUBTREEP is non-nil, assume the export is done against the
  723. current sub-tree.
  724. EXT-PLIST is a property list with external parameters overriding
  725. org-mode's default settings, but still inferior to file-local
  726. settings."
  727. ;; First install #+BIND variables.
  728. (org-export-install-letbind-maybe)
  729. ;; Get and prioritize export options...
  730. (let ((options (org-combine-plists
  731. ;; ... from global variables...
  732. (org-export-get-global-options backend)
  733. ;; ... from buffer's name (default title)...
  734. `(:title
  735. ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
  736. (and file
  737. (file-name-sans-extension
  738. (file-name-nondirectory file))))
  739. (buffer-name (buffer-base-buffer))))
  740. ;; ... from an external property list...
  741. ext-plist
  742. ;; ... from in-buffer settings...
  743. (org-export-get-inbuffer-options
  744. (org-with-wide-buffer (buffer-string)) backend
  745. (and buffer-file-name
  746. (org-remove-double-quotes buffer-file-name)))
  747. ;; ... and from subtree, when appropriate.
  748. (and subtreep
  749. (org-export-get-subtree-options)))))
  750. ;; Add initial options.
  751. (setq options (append (org-export-initial-options) options))
  752. ;; Return plist.
  753. options))
  754. (defun org-export-parse-option-keyword (options backend)
  755. "Parse an OPTIONS line and return values as a plist.
  756. BACKEND is a symbol specifying the back-end to use."
  757. (let* ((all (append org-export-option-alist
  758. (let ((var (intern
  759. (format "org-%s-option-alist" backend))))
  760. (and (boundp var) (eval var)))))
  761. ;; Build an alist between #+OPTION: item and property-name.
  762. (alist (delq nil
  763. (mapcar (lambda (e)
  764. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  765. (car e))))
  766. all)))
  767. plist)
  768. (mapc (lambda (e)
  769. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  770. (car e)
  771. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  772. options)
  773. (setq plist (plist-put plist
  774. (cdr e)
  775. (car (read-from-string
  776. (match-string 2 options)))))))
  777. alist)
  778. plist))
  779. (defun org-export-get-subtree-options ()
  780. "Get export options in subtree at point.
  781. Assume point is at subtree's beginning.
  782. Return options as a plist."
  783. (let (prop plist)
  784. (when (setq prop (progn (looking-at org-todo-line-regexp)
  785. (or (save-match-data
  786. (org-entry-get (point) "EXPORT_TITLE"))
  787. (org-match-string-no-properties 3))))
  788. (setq plist
  789. (plist-put
  790. plist :title
  791. (org-element-parse-secondary-string
  792. prop
  793. (cdr (assq 'keyword org-element-string-restrictions))))))
  794. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  795. (setq plist (plist-put plist :text prop)))
  796. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  797. (setq plist (plist-put plist :author prop)))
  798. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  799. (setq plist (plist-put plist :date prop)))
  800. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  801. (setq plist (org-export-add-options-to-plist plist prop)))
  802. plist))
  803. (defun org-export-get-inbuffer-options (buffer-string backend files)
  804. "Return in-buffer options as a plist.
  805. BUFFER-STRING is the string of the buffer. BACKEND is a symbol
  806. specifying which back-end should be used. FILES is a list of
  807. setup files names read so far, used to avoid circular
  808. dependencies."
  809. (let ((case-fold-search t) plist)
  810. ;; 1. Special keywords, as in `org-export-special-keywords'.
  811. (let ((start 0)
  812. (special-re (org-make-options-regexp org-export-special-keywords)))
  813. (while (string-match special-re buffer-string start)
  814. (setq start (match-end 0))
  815. (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
  816. ;; Special keywords do not have their value expanded.
  817. (val (org-match-string-no-properties 2 buffer-string)))
  818. (setq plist
  819. (org-combine-plists
  820. (cond
  821. ((string= key "SETUP_FILE")
  822. (let ((file (expand-file-name
  823. (org-remove-double-quotes (org-trim val)))))
  824. ;; Avoid circular dependencies.
  825. (unless (member file files)
  826. (org-export-get-inbuffer-options
  827. (org-file-contents file 'noerror)
  828. backend
  829. (cons file files)))))
  830. ((string= key "OPTIONS")
  831. (org-export-parse-option-keyword val backend))
  832. ((string= key "MACRO")
  833. (when (string-match
  834. "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
  835. val)
  836. (let ((key (intern
  837. (concat ":macro-"
  838. (downcase (match-string 1 val)))))
  839. (value (match-string 2 val)))
  840. (cond
  841. ((not value) "")
  842. ((string-match "\\`(eval\\>" value) (list key value))
  843. (t
  844. (list
  845. key
  846. ;; If user explicitly asks for a newline, be
  847. ;; sure to preserve it from further filling
  848. ;; with `hard-newline'.
  849. (replace-regexp-in-string
  850. "\\\\n" hard-newline value))))))))
  851. plist)))))
  852. ;; 2. Standard options, as in `org-export-option-alist'.
  853. (let* ((all (append org-export-option-alist
  854. (let ((var (intern
  855. (format "org-%s-option-alist" backend))))
  856. (and (boundp var) (eval var)))))
  857. ;; Build alist between keyword name and property name.
  858. (alist (delq nil (mapcar (lambda (e)
  859. (when (nth 1 e) (cons (nth 1 e) (car e))))
  860. all)))
  861. ;; Build regexp matching all keywords associated to export
  862. ;; options. Note: the search is case insensitive.
  863. (opt-re (org-make-options-regexp
  864. (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
  865. (start 0))
  866. (while (string-match opt-re buffer-string start)
  867. (setq start (match-end 0))
  868. (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
  869. ;; Expand value, applying restrictions for keywords.
  870. (val (org-match-string-no-properties 2 buffer-string))
  871. (prop (cdr (assoc key alist)))
  872. (behaviour (nth 4 (assq prop all))))
  873. (setq plist
  874. (plist-put
  875. plist prop
  876. ;; Handle value depending on specified BEHAVIOUR.
  877. (case behaviour
  878. (space (if (plist-get plist prop)
  879. (concat (plist-get plist prop) " " (org-trim val))
  880. (org-trim val)))
  881. (newline (org-trim
  882. (concat
  883. (plist-get plist prop) "\n" (org-trim val))))
  884. (split `(,@(plist-get plist prop) ,@(org-split-string val)))
  885. ('t val)
  886. (otherwise (plist-get plist prop)))))))
  887. ;; Parse keywords specified in `org-element-parsed-keywords'.
  888. (mapc
  889. (lambda (key)
  890. (let* ((prop (cdr (assoc (upcase key) alist)))
  891. (value (and prop (plist-get plist prop))))
  892. (when (stringp value)
  893. (setq plist
  894. (plist-put
  895. plist prop
  896. (org-element-parse-secondary-string
  897. value
  898. (cdr (assq 'keyword org-element-string-restrictions))))))))
  899. org-element-parsed-keywords))
  900. ;; Return final value.
  901. plist))
  902. (defun org-export-get-global-options (backend)
  903. "Return global export options as a plist.
  904. BACKEND is a symbol specifying which back-end should be used."
  905. (let ((all (append org-export-option-alist
  906. (let ((var (intern
  907. (format "org-%s-option-alist" backend))))
  908. (and (boundp var) (eval var)))))
  909. ;; Output value.
  910. plist)
  911. (mapc (lambda (cell)
  912. (setq plist
  913. (plist-put plist (car cell) (eval (nth 3 cell)))))
  914. all)
  915. ;; Return value.
  916. plist))
  917. (defun org-export-initial-options ()
  918. "Return a plist with properties related to input buffer."
  919. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  920. (list
  921. ;; Store full path of input file name, or nil. For internal use.
  922. :input-file visited-file
  923. ;; `:macro-date', `:macro-time' and `:macro-property' could as well
  924. ;; be initialized as tree properties, since they don't depend on
  925. ;; initial environment. Though, it may be more logical to keep
  926. ;; them close to other ":macro-" properties.
  927. :macro-date "(eval (format-time-string \"$1\"))"
  928. :macro-time "(eval (format-time-string \"$1\"))"
  929. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
  930. :macro-modification-time
  931. (and visited-file
  932. (file-exists-p visited-file)
  933. (concat "(eval (format-time-string \"$1\" '"
  934. (prin1-to-string (nth 5 (file-attributes visited-file)))
  935. "))"))
  936. ;; Store input file name as a macro.
  937. :macro-input-file (and visited-file (file-name-nondirectory visited-file))
  938. ;; Footnotes definitions must be collected in the original buffer,
  939. ;; as there's no insurance that they will still be in the parse
  940. ;; tree, due to some narrowing.
  941. :footnote-definition-alist
  942. (let (alist)
  943. (org-with-wide-buffer
  944. (goto-char (point-min))
  945. (while (re-search-forward org-footnote-definition-re nil t)
  946. (let ((def (org-footnote-at-definition-p)))
  947. (when def
  948. (org-skip-whitespace)
  949. (push (cons (car def)
  950. (save-restriction
  951. (narrow-to-region (point) (nth 2 def))
  952. ;; Like `org-element-parse-buffer', but
  953. ;; makes sure the definition doesn't start
  954. ;; with a section element.
  955. (nconc
  956. (list 'org-data nil)
  957. (org-element-parse-elements
  958. (point-min) (point-max) nil nil nil nil nil))))
  959. alist))))
  960. alist)))))
  961. (defvar org-export-allow-BIND-local nil)
  962. (defun org-export-confirm-letbind ()
  963. "Can we use #+BIND values during export?
  964. By default this will ask for confirmation by the user, to divert
  965. possible security risks."
  966. (cond
  967. ((not org-export-allow-BIND) nil)
  968. ((eq org-export-allow-BIND t) t)
  969. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  970. (t (org-set-local 'org-export-allow-BIND-local
  971. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  972. (defun org-export-install-letbind-maybe ()
  973. "Install the values from #+BIND lines as local variables.
  974. Variables must be installed before in-buffer options are
  975. retrieved."
  976. (let (letbind pair)
  977. (org-with-wide-buffer
  978. (goto-char (point-min))
  979. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  980. (when (org-export-confirm-letbind)
  981. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  982. letbind))))
  983. (while (setq pair (pop letbind))
  984. (org-set-local (car pair) (nth 1 pair)))))
  985. ;;;; Tree Properties
  986. ;; They are initialized at the beginning of the transcoding process by
  987. ;; `org-export-collect-tree-properties'.
  988. ;; Dedicated functions focus on computing the value of specific tree
  989. ;; properties during initialization. Thus,
  990. ;; `org-export-use-select-tag-p' determines if an headline makes use
  991. ;; of an export tag enforcing inclusion. `org-export-get-min-level'
  992. ;; gets the minimal exportable level, used as a basis to compute
  993. ;; relative level for headlines. `org-export-get-point-max' returns
  994. ;; the maximum exportable ending position in the parse tree.
  995. ;; Eventually `org-export-collect-headline-numbering' builds an alist
  996. ;; between headlines' beginning position and their numbering.
  997. (defun org-export-collect-tree-properties (data info backend)
  998. "Extract tree properties from parse tree.
  999. DATA is the parse tree from which information is retrieved. INFO
  1000. is a list holding export options. BACKEND is the back-end called
  1001. for transcoding, as a symbol.
  1002. Following tree properties are set:
  1003. `:back-end' Back-end used for transcoding.
  1004. `:headline-offset' Offset between true level of headlines and
  1005. local level. An offset of -1 means an headline
  1006. of level 2 should be considered as a level
  1007. 1 headline in the context.
  1008. `:headline-numbering' Alist of all headlines' beginning position
  1009. as key an the associated numbering as value.
  1010. `:parse-tree' Whole parse tree.
  1011. `:target-list' List of all targets in the parse tree.
  1012. `:use-select-tags' Non-nil when parsed tree use a special tag to
  1013. enforce transcoding of the headline."
  1014. ;; First, set `:use-select-tags' property, as it will be required
  1015. ;; for further computations.
  1016. (setq info
  1017. (org-combine-plists
  1018. info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
  1019. ;; Then get `:headline-offset' in order to be able to use
  1020. ;; `org-export-get-relative-level'.
  1021. (setq info
  1022. (org-combine-plists
  1023. info `(:headline-offset ,(- 1 (org-export-get-min-level data info)))))
  1024. ;; Now, get the rest of the tree properties, now `:use-select-tags'
  1025. ;; is set...
  1026. (nconc
  1027. `(:parse-tree
  1028. ,data
  1029. :target-list
  1030. ,(org-element-map data 'target (lambda (target local) target) info)
  1031. :headline-numbering ,(org-export-collect-headline-numbering data info)
  1032. :back-end ,backend)
  1033. info))
  1034. (defun org-export-use-select-tags-p (data options)
  1035. "Non-nil when data use a tag enforcing transcoding.
  1036. DATA is parsed data as returned by `org-element-parse-buffer'.
  1037. OPTIONS is a plist holding export options."
  1038. (org-element-map
  1039. data
  1040. 'headline
  1041. (lambda (headline info)
  1042. (let ((tags (org-element-get-property :with-tags headline)))
  1043. (and tags (string-match
  1044. (format ":%s:" (plist-get info :select-tags)) tags))))
  1045. options
  1046. 'stop-at-first-match))
  1047. (defun org-export-get-min-level (data options)
  1048. "Return minimum exportable headline's level in DATA.
  1049. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1050. OPTIONS is a plist holding export options."
  1051. (catch 'exit
  1052. (let ((min-level 10000))
  1053. (mapc (lambda (blob)
  1054. (when (and (eq (car blob) 'headline)
  1055. (not (org-export-skip-p blob options)))
  1056. (setq min-level
  1057. (min (org-element-get-property :level blob) min-level)))
  1058. (when (= min-level 1) (throw 'exit 1)))
  1059. (org-element-get-contents data))
  1060. ;; If no headline was found, for the sake of consistency, set
  1061. ;; minimum level to 1 nonetheless.
  1062. (if (= min-level 10000) 1 min-level))))
  1063. (defun org-export-collect-headline-numbering (data options)
  1064. "Return numbering of all exportable headlines in a parse tree.
  1065. DATA is the parse tree. OPTIONS is the plist holding export
  1066. options.
  1067. Return an alist whose key is an headline and value is its
  1068. associated numbering \(in the shape of a list of numbers\)."
  1069. (let ((numbering (make-vector org-export-max-depth 0)))
  1070. (org-element-map
  1071. data
  1072. 'headline
  1073. (lambda (headline info)
  1074. (let ((relative-level
  1075. (1- (org-export-get-relative-level headline info))))
  1076. (cons
  1077. headline
  1078. (loop for n across numbering
  1079. for idx from 0 to org-export-max-depth
  1080. when (< idx relative-level) collect n
  1081. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1082. when (> idx relative-level) do (aset numbering idx 0)))))
  1083. options)))
  1084. ;;; The Transcoder
  1085. ;; This function reads Org data (obtained with, i.e.
  1086. ;; `org-element-parse-buffer') and transcodes it into a specified
  1087. ;; back-end output. It takes care of updating local properties,
  1088. ;; filtering out elements or objects according to export options and
  1089. ;; organizing the output blank lines and white space are preserved.
  1090. ;; Though, this function is inapropriate for secondary strings, which
  1091. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1092. ;; `org-export-secondary-string' is provided for that specific task.
  1093. ;; Internally, three functions handle the filtering of objects and
  1094. ;; elements during the export. More precisely, `org-export-skip-p'
  1095. ;; determines if the considered object or element should be ignored
  1096. ;; altogether, `org-export-interpret-p' tells which elements or
  1097. ;; objects should be seen as real Org syntax and `org-export-expand'
  1098. ;; transforms the others back into their original shape.
  1099. (defun org-export-data (data backend info)
  1100. "Convert DATA to a string into BACKEND format.
  1101. DATA is a nested list as returned by `org-element-parse-buffer'.
  1102. BACKEND is a symbol among supported exporters.
  1103. INFO is a plist holding export options and also used as
  1104. a communication channel between elements when walking the nested
  1105. list. See `org-export-update-info' function for more
  1106. details.
  1107. Return transcoded string."
  1108. (mapconcat
  1109. ;; BLOB can be an element, an object, a string, or nil.
  1110. (lambda (blob)
  1111. (cond
  1112. ((not blob) nil)
  1113. ;; BLOB is a string. Check if the optional transcoder for plain
  1114. ;; text exists, and call it in that case. Otherwise, simply
  1115. ;; return string. Also update INFO and call
  1116. ;; `org-export-filter-plain-text-functions'.
  1117. ((stringp blob)
  1118. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1119. (org-export-filter-apply-functions
  1120. (plist-get info :filter-plain-text)
  1121. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1122. backend info)))
  1123. ;; BLOB is an element or an object.
  1124. (t
  1125. (let* ((type (if (stringp blob) 'plain-text (car blob)))
  1126. ;; 1. Determine the appropriate TRANSCODER.
  1127. (transcoder
  1128. (cond
  1129. ;; 1.0 A full Org document is inserted.
  1130. ((eq type 'org-data) 'identity)
  1131. ;; 1.1. BLOB should be ignored.
  1132. ((org-export-skip-p blob info) nil)
  1133. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1134. ;; back into Org syntax.
  1135. ((not (org-export-interpret-p blob info))
  1136. 'org-export-expand)
  1137. ;; 1.3. Else apply naming convention.
  1138. (t (let ((trans (intern
  1139. (format "org-%s-%s" backend type))))
  1140. (and (fboundp trans) trans)))))
  1141. ;; 2. Compute CONTENTS of BLOB.
  1142. (contents
  1143. (cond
  1144. ;; Case 0. No transcoder defined: ignore BLOB.
  1145. ((not transcoder) nil)
  1146. ;; Case 1. Transparently export an Org document.
  1147. ((eq type 'org-data) (org-export-data blob backend info))
  1148. ;; Case 2. For a recursive object.
  1149. ((memq type org-element-recursive-objects)
  1150. (org-export-data
  1151. blob backend
  1152. (org-combine-plists
  1153. info
  1154. `(:genealogy ,(cons blob (plist-get info :genealogy))))))
  1155. ;; Case 3. For a recursive element.
  1156. ((memq type org-element-greater-elements)
  1157. ;; Ignore contents of an archived tree
  1158. ;; when `:with-archived-trees' is `headline'.
  1159. (unless (and
  1160. (eq type 'headline)
  1161. (eq (plist-get info :with-archived-trees) 'headline)
  1162. (org-element-get-property :archivedp blob))
  1163. (org-element-normalize-string
  1164. (org-export-data
  1165. blob backend
  1166. (org-combine-plists
  1167. info `(:genealogy
  1168. ,(cons blob (plist-get info :genealogy))))))))
  1169. ;; Case 4. For a paragraph.
  1170. ((eq type 'paragraph)
  1171. (let ((paragraph
  1172. (org-element-normalize-contents
  1173. blob
  1174. ;; When normalizing contents of an item or
  1175. ;; a footnote definition, ignore first line's
  1176. ;; indentation: there is none and it might be
  1177. ;; misleading.
  1178. (and (not (org-export-get-previous-element blob info))
  1179. (let ((parent (caar (plist-get info :genealogy))))
  1180. (memq parent '(footnote-definition item)))))))
  1181. (org-export-data
  1182. paragraph backend
  1183. (org-combine-plists
  1184. info `(:genealogy
  1185. ,(cons paragraph (plist-get info :genealogy)))))))))
  1186. ;; 3. Transcode BLOB into RESULTS string.
  1187. (results (cond
  1188. ((not transcoder) nil)
  1189. ((eq transcoder 'org-export-expand)
  1190. (org-export-data
  1191. `(org-data nil ,(funcall transcoder blob contents))
  1192. backend info))
  1193. (t (funcall transcoder blob contents info)))))
  1194. ;; 4. Discard nil results. Otherwise, update INFO, append
  1195. ;; the same white space between elements or objects as in
  1196. ;; the original buffer, and call appropriate filters.
  1197. (when results
  1198. ;; No filter for a full document.
  1199. (if (eq type 'org-data) results
  1200. (org-export-filter-apply-functions
  1201. (plist-get info (intern (format ":filter-%s" type)))
  1202. (let ((post-blank (org-element-get-property :post-blank blob)))
  1203. (if (memq type org-element-all-elements)
  1204. (concat (org-element-normalize-string results)
  1205. (make-string post-blank ?\n))
  1206. (concat results (make-string post-blank ? ))))
  1207. backend info)))))))
  1208. (org-element-get-contents data) ""))
  1209. (defun org-export-secondary-string (secondary backend info)
  1210. "Convert SECONDARY string into BACKEND format.
  1211. SECONDARY is a nested list as returned by
  1212. `org-element-parse-secondary-string'.
  1213. BACKEND is a symbol among supported exporters. INFO is a plist
  1214. used as a communication channel.
  1215. Return transcoded string."
  1216. ;; Make SECONDARY acceptable for `org-export-data'.
  1217. (let ((s (if (listp secondary) secondary (list secondary))))
  1218. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1219. (defun org-export-skip-p (blob info)
  1220. "Non-nil when element or object BLOB should be skipped during export.
  1221. INFO is the plist holding export options."
  1222. ;; Check headline.
  1223. (unless (stringp blob)
  1224. (case (car blob)
  1225. ('headline
  1226. (let ((with-tasks (plist-get info :with-tasks))
  1227. (todo (org-element-get-property :todo-keyword blob))
  1228. (todo-type (org-element-get-property :todo-type blob))
  1229. (archived (plist-get info :with-archived-trees))
  1230. (tag-list (let ((tags (org-element-get-property :tags blob)))
  1231. (and tags (org-split-string tags ":")))))
  1232. (or
  1233. ;; Ignore subtrees with an exclude tag.
  1234. (loop for k in (plist-get info :exclude-tags)
  1235. thereis (member k tag-list))
  1236. ;; Ignore subtrees without a select tag, when such tag is found
  1237. ;; in the buffer.
  1238. (and (plist-get info :use-select-tags)
  1239. (loop for k in (plist-get info :select-tags)
  1240. never (member k tag-list)))
  1241. ;; Ignore commented sub-trees.
  1242. (org-element-get-property :commentedp blob)
  1243. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1244. (and (not archived) (org-element-get-property :archivedp blob))
  1245. ;; Ignore tasks, if specified by `:with-tasks' property.
  1246. (and todo (not with-tasks))
  1247. (and todo
  1248. (memq with-tasks '(todo done))
  1249. (not (eq todo-type with-tasks)))
  1250. (and todo
  1251. (consp with-tasks)
  1252. (not (member todo with-tasks))))))
  1253. ;; Check time-stamp.
  1254. ('time-stamp (not (plist-get info :with-timestamps)))
  1255. ;; Check drawer.
  1256. ('drawer
  1257. (or (not (plist-get info :with-drawers))
  1258. (and (consp (plist-get info :with-drawers))
  1259. (not (member (org-element-get-property :drawer-name blob)
  1260. (plist-get info :with-drawers))))))
  1261. ;; Check export snippet.
  1262. ('export-snippet
  1263. (let* ((raw-back-end (org-element-get-property :back-end blob))
  1264. (true-back-end
  1265. (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
  1266. raw-back-end)))
  1267. (not (string= (symbol-name (plist-get info :back-end))
  1268. true-back-end)))))))
  1269. (defun org-export-interpret-p (blob info)
  1270. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1271. Check is done according to export options INFO, stored as
  1272. a plist."
  1273. (case (car blob)
  1274. ;; ... entities...
  1275. (entity (plist-get info :with-entities))
  1276. ;; ... emphasis...
  1277. (emphasis (plist-get info :with-emphasize))
  1278. ;; ... fixed-width areas.
  1279. (fixed-width (plist-get info :with-fixed-width))
  1280. ;; ... footnotes...
  1281. ((footnote-definition footnote-reference)
  1282. (plist-get info :with-footnotes))
  1283. ;; ... sub/superscripts...
  1284. ((subscript superscript)
  1285. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1286. (if (eq sub/super-p '{})
  1287. (org-element-get-property :use-brackets-p blob)
  1288. sub/super-p)))
  1289. ;; ... tables...
  1290. (table (plist-get info :with-tables))
  1291. (otherwise t)))
  1292. (defsubst org-export-expand (blob contents)
  1293. "Expand a parsed element or object to its original state.
  1294. BLOB is either an element or an object. CONTENTS is its
  1295. contents, as a string or nil."
  1296. (funcall
  1297. (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
  1298. ;;; The Filter System
  1299. ;; Filters allow end-users to tweak easily the transcoded output.
  1300. ;; They are the functional counterpart of hooks, as every filter in
  1301. ;; a set is applied to the return value of the previous one.
  1302. ;; Every set is back-end agnostic. Although, a filter is always
  1303. ;; called, in addition to the string it applies to, with the back-end
  1304. ;; used as argument, so it's easy enough for the end-user to add
  1305. ;; back-end specific filters in the set. The communication channel,
  1306. ;; as a plist, is required as the third argument.
  1307. ;; Filters sets are defined below. There are of four types:
  1308. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1309. ;; complete parsed tree. It's the only filters set that doesn't
  1310. ;; apply to a string.
  1311. ;; - `org-export-filter-final-output-functions' applies to the final
  1312. ;; transcoded string.
  1313. ;; - `org-export-filter-plain-text-functions' applies to any string
  1314. ;; not recognized as Org syntax.
  1315. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1316. ;; after an element or object of type TYPE has been transcoded.
  1317. ;; All filters sets are applied through
  1318. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1319. ;; applied in a LIFO fashion. It allows developers to be sure that
  1320. ;; their filters will be applied first.
  1321. ;;;; Special Filters
  1322. (defvar org-export-filter-parse-tree-functions nil
  1323. "Filter, or list of filters, applied to the parsed tree.
  1324. Each filter is called with three arguments: the parse tree, as
  1325. returned by `org-element-parse-buffer', the back-end, as
  1326. a symbol, and the communication channel, as a plist. It must
  1327. return the modified parse tree to transcode.")
  1328. (defvar org-export-filter-final-output-functions nil
  1329. "Filter, or list of filters, applied to the transcoded string.
  1330. Each filter is called with three arguments: the full transcoded
  1331. string, the back-end, as a symbol, and the communication channel,
  1332. as a plist. It must return a string that will be used as the
  1333. final export output.")
  1334. (defvar org-export-filter-plain-text-functions nil
  1335. "Filter, or list of filters, applied to plain text.
  1336. Each filter is called with three arguments: a string which
  1337. contains no Org syntax, the back-end, as a symbol, and the
  1338. communication channel, as a plist. It must return a string or
  1339. nil.")
  1340. ;;;; Elements Filters
  1341. (defvar org-export-filter-center-block-functions nil
  1342. "List of functions applied to a transcoded center block.
  1343. Each filter is called with three arguments: the transcoded center
  1344. block, as a string, the back-end, as a symbol, and the
  1345. communication channel, as a plist. It must return a string or
  1346. nil.")
  1347. (defvar org-export-filter-drawer-functions nil
  1348. "List of functions applied to a transcoded drawer.
  1349. Each filter is called with three arguments: the transcoded
  1350. drawer, as a string, the back-end, as a symbol, and the
  1351. communication channel, as a plist. It must return a string or
  1352. nil.")
  1353. (defvar org-export-filter-dynamic-block-functions nil
  1354. "List of functions applied to a transcoded dynamic-block.
  1355. Each filter is called with three arguments: the transcoded
  1356. dynamic-block, as a string, the back-end, as a symbol, and the
  1357. communication channel, as a plist. It must return a string or
  1358. nil.")
  1359. (defvar org-export-filter-headline-functions nil
  1360. "List of functions applied to a transcoded headline.
  1361. Each filter is called with three arguments: the transcoded
  1362. headline, as a string, the back-end, as a symbol, and the
  1363. communication channel, as a plist. It must return a string or
  1364. nil.")
  1365. (defvar org-export-filter-inlinetask-functions nil
  1366. "List of functions applied to a transcoded inlinetask.
  1367. Each filter is called with three arguments: the transcoded
  1368. inlinetask, as a string, the back-end, as a symbol, and the
  1369. communication channel, as a plist. It must return a string or
  1370. nil.")
  1371. (defvar org-export-filter-plain-list-functions nil
  1372. "List of functions applied to a transcoded plain-list.
  1373. Each filter is called with three arguments: the transcoded
  1374. plain-list, as a string, the back-end, as a symbol, and the
  1375. communication channel, as a plist. It must return a string or
  1376. nil.")
  1377. (defvar org-export-filter-item-functions nil
  1378. "List of functions applied to a transcoded item.
  1379. Each filter is called with three arguments: the transcoded item,
  1380. as a string, the back-end, as a symbol, and the communication
  1381. channel, as a plist. It must return a string or nil.")
  1382. (defvar org-export-filter-comment-functions nil
  1383. "List of functions applied to a transcoded comment.
  1384. Each filter is called with three arguments: the transcoded
  1385. comment, as a string, the back-end, as a symbol, and the
  1386. communication channel, as a plist. It must return a string or
  1387. nil.")
  1388. (defvar org-export-filter-comment-block-functions nil
  1389. "List of functions applied to a transcoded comment-comment.
  1390. Each filter is called with three arguments: the transcoded
  1391. comment-block, as a string, the back-end, as a symbol, and the
  1392. communication channel, as a plist. It must return a string or
  1393. nil.")
  1394. (defvar org-export-filter-example-block-functions nil
  1395. "List of functions applied to a transcoded example-block.
  1396. Each filter is called with three arguments: the transcoded
  1397. example-block, as a string, the back-end, as a symbol, and the
  1398. communication channel, as a plist. It must return a string or
  1399. nil.")
  1400. (defvar org-export-filter-export-block-functions nil
  1401. "List of functions applied to a transcoded export-block.
  1402. Each filter is called with three arguments: the transcoded
  1403. export-block, as a string, the back-end, as a symbol, and the
  1404. communication channel, as a plist. It must return a string or
  1405. nil.")
  1406. (defvar org-export-filter-fixed-width-functions nil
  1407. "List of functions applied to a transcoded fixed-width.
  1408. Each filter is called with three arguments: the transcoded
  1409. fixed-width, as a string, the back-end, as a symbol, and the
  1410. communication channel, as a plist. It must return a string or
  1411. nil.")
  1412. (defvar org-export-filter-footnote-definition-functions nil
  1413. "List of functions applied to a transcoded footnote-definition.
  1414. Each filter is called with three arguments: the transcoded
  1415. footnote-definition, as a string, the back-end, as a symbol, and
  1416. the communication channel, as a plist. It must return a string
  1417. or nil.")
  1418. (defvar org-export-filter-horizontal-rule-functions nil
  1419. "List of functions applied to a transcoded horizontal-rule.
  1420. Each filter is called with three arguments: the transcoded
  1421. horizontal-rule, as a string, the back-end, as a symbol, and the
  1422. communication channel, as a plist. It must return a string or
  1423. nil.")
  1424. (defvar org-export-filter-keyword-functions nil
  1425. "List of functions applied to a transcoded keyword.
  1426. Each filter is called with three arguments: the transcoded
  1427. keyword, as a string, the back-end, as a symbol, and the
  1428. communication channel, as a plist. It must return a string or
  1429. nil.")
  1430. (defvar org-export-filter-latex-environment-functions nil
  1431. "List of functions applied to a transcoded latex-environment.
  1432. Each filter is called with three arguments: the transcoded
  1433. latex-environment, as a string, the back-end, as a symbol, and
  1434. the communication channel, as a plist. It must return a string
  1435. or nil.")
  1436. (defvar org-export-filter-babel-call-functions nil
  1437. "List of functions applied to a transcoded babel-call.
  1438. Each filter is called with three arguments: the transcoded
  1439. babel-call, as a string, the back-end, as a symbol, and the
  1440. communication channel, as a plist. It must return a string or
  1441. nil.")
  1442. (defvar org-export-filter-paragraph-functions nil
  1443. "List of functions applied to a transcoded paragraph.
  1444. Each filter is called with three arguments: the transcoded
  1445. paragraph, as a string, the back-end, as a symbol, and the
  1446. communication channel, as a plist. It must return a string or
  1447. nil.")
  1448. (defvar org-export-filter-property-drawer-functions nil
  1449. "List of functions applied to a transcoded property-drawer.
  1450. Each filter is called with three arguments: the transcoded
  1451. property-drawer, as a string, the back-end, as a symbol, and the
  1452. communication channel, as a plist. It must return a string or
  1453. nil.")
  1454. (defvar org-export-filter-quote-block-functions nil
  1455. "List of functions applied to a transcoded quote block.
  1456. Each filter is called with three arguments: the transcoded quote
  1457. block, as a string, the back-end, as a symbol, and the
  1458. communication channel, as a plist. It must return a string or
  1459. nil.")
  1460. (defvar org-export-filter-quote-section-functions nil
  1461. "List of functions applied to a transcoded quote-section.
  1462. Each filter is called with three arguments: the transcoded
  1463. quote-section, as a string, the back-end, as a symbol, and the
  1464. communication channel, as a plist. It must return a string or
  1465. nil.")
  1466. (defvar org-export-filter-section-functions nil
  1467. "List of functions applied to a transcoded section.
  1468. Each filter is called with three arguments: the transcoded
  1469. section, as a string, the back-end, as a symbol, and the
  1470. communication channel, as a plist. It must return a string or
  1471. nil.")
  1472. (defvar org-export-filter-special-block-functions nil
  1473. "List of functions applied to a transcoded special block.
  1474. Each filter is called with three arguments: the transcoded
  1475. special block, as a string, the back-end, as a symbol, and the
  1476. communication channel, as a plist. It must return a string or
  1477. nil.")
  1478. (defvar org-export-filter-src-block-functions nil
  1479. "List of functions applied to a transcoded src-block.
  1480. Each filter is called with three arguments: the transcoded
  1481. src-block, as a string, the back-end, as a symbol, and the
  1482. communication channel, as a plist. It must return a string or
  1483. nil.")
  1484. (defvar org-export-filter-table-functions nil
  1485. "List of functions applied to a transcoded table.
  1486. Each filter is called with three arguments: the transcoded table,
  1487. as a string, the back-end, as a symbol, and the communication
  1488. channel, as a plist. It must return a string or nil.")
  1489. (defvar org-export-filter-verse-block-functions nil
  1490. "List of functions applied to a transcoded verse block.
  1491. Each filter is called with three arguments: the transcoded verse
  1492. block, as a string, the back-end, as a symbol, and the
  1493. communication channel, as a plist. It must return a string or
  1494. nil.")
  1495. ;;;; Objects Filters
  1496. (defvar org-export-filter-emphasis-functions nil
  1497. "List of functions applied to a transcoded emphasis.
  1498. Each filter is called with three arguments: the transcoded
  1499. emphasis, as a string, the back-end, as a symbol, and the
  1500. communication channel, as a plist. It must return a string or
  1501. nil.")
  1502. (defvar org-export-filter-entity-functions nil
  1503. "List of functions applied to a transcoded entity.
  1504. Each filter is called with three arguments: the transcoded
  1505. entity, as a string, the back-end, as a symbol, and the
  1506. communication channel, as a plist. It must return a string or
  1507. nil.")
  1508. (defvar org-export-filter-export-snippet-functions nil
  1509. "List of functions applied to a transcoded export-snippet.
  1510. Each filter is called with three arguments: the transcoded
  1511. export-snippet, as a string, the back-end, as a symbol, and the
  1512. communication channel, as a plist. It must return a string or
  1513. nil.")
  1514. (defvar org-export-filter-footnote-reference-functions nil
  1515. "List of functions applied to a transcoded footnote-reference.
  1516. Each filter is called with three arguments: the transcoded
  1517. footnote-reference, as a string, the back-end, as a symbol, and
  1518. the communication channel, as a plist. It must return a string
  1519. or nil.")
  1520. (defvar org-export-filter-inline-babel-call-functions nil
  1521. "List of functions applied to a transcoded inline-babel-call.
  1522. Each filter is called with three arguments: the transcoded
  1523. inline-babel-call, as a string, the back-end, as a symbol, and
  1524. the communication channel, as a plist. It must return a string
  1525. or nil.")
  1526. (defvar org-export-filter-inline-src-block-functions nil
  1527. "List of functions applied to a transcoded inline-src-block.
  1528. Each filter is called with three arguments: the transcoded
  1529. inline-src-block, as a string, the back-end, as a symbol, and the
  1530. communication channel, as a plist. It must return a string or
  1531. nil.")
  1532. (defvar org-export-filter-latex-fragment-functions nil
  1533. "List of functions applied to a transcoded latex-fragment.
  1534. Each filter is called with three arguments: the transcoded
  1535. latex-fragment, as a string, the back-end, as a symbol, and the
  1536. communication channel, as a plist. It must return a string or
  1537. nil.")
  1538. (defvar org-export-filter-line-break-functions nil
  1539. "List of functions applied to a transcoded line-break.
  1540. Each filter is called with three arguments: the transcoded
  1541. line-break, as a string, the back-end, as a symbol, and the
  1542. communication channel, as a plist. It must return a string or
  1543. nil.")
  1544. (defvar org-export-filter-link-functions nil
  1545. "List of functions applied to a transcoded link.
  1546. Each filter is called with three arguments: the transcoded link,
  1547. as a string, the back-end, as a symbol, and the communication
  1548. channel, as a plist. It must return a string or nil.")
  1549. (defvar org-export-filter-macro-functions nil
  1550. "List of functions applied to a transcoded macro.
  1551. Each filter is called with three arguments: the transcoded macro,
  1552. as a string, the back-end, as a symbol, and the communication
  1553. channel, as a plist. It must return a string or nil.")
  1554. (defvar org-export-filter-radio-target-functions nil
  1555. "List of functions applied to a transcoded radio-target.
  1556. Each filter is called with three arguments: the transcoded
  1557. radio-target, as a string, the back-end, as a symbol, and the
  1558. communication channel, as a plist. It must return a string or
  1559. nil.")
  1560. (defvar org-export-filter-statistics-cookie-functions nil
  1561. "List of functions applied to a transcoded statistics-cookie.
  1562. Each filter is called with three arguments: the transcoded
  1563. statistics-cookie, as a string, the back-end, as a symbol, and
  1564. the communication channel, as a plist. It must return a string
  1565. or nil.")
  1566. (defvar org-export-filter-subscript-functions nil
  1567. "List of functions applied to a transcoded subscript.
  1568. Each filter is called with three arguments: the transcoded
  1569. subscript, as a string, the back-end, as a symbol, and the
  1570. communication channel, as a plist. It must return a string or
  1571. nil.")
  1572. (defvar org-export-filter-superscript-functions nil
  1573. "List of functions applied to a transcoded superscript.
  1574. Each filter is called with three arguments: the transcoded
  1575. superscript, as a string, the back-end, as a symbol, and the
  1576. communication channel, as a plist. It must return a string or
  1577. nil.")
  1578. (defvar org-export-filter-target-functions nil
  1579. "List of functions applied to a transcoded target.
  1580. Each filter is called with three arguments: the transcoded
  1581. target, as a string, the back-end, as a symbol, and the
  1582. communication channel, as a plist. It must return a string or
  1583. nil.")
  1584. (defvar org-export-filter-time-stamp-functions nil
  1585. "List of functions applied to a transcoded time-stamp.
  1586. Each filter is called with three arguments: the transcoded
  1587. time-stamp, as a string, the back-end, as a symbol, and the
  1588. communication channel, as a plist. It must return a string or
  1589. nil.")
  1590. (defvar org-export-filter-verbatim-functions nil
  1591. "List of functions applied to a transcoded verbatim.
  1592. Each filter is called with three arguments: the transcoded
  1593. verbatim, as a string, the back-end, as a symbol, and the
  1594. communication channel, as a plist. It must return a string or
  1595. nil.")
  1596. (defun org-export-filter-apply-functions (filters value backend info)
  1597. "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
  1598. Functions are called in a LIFO fashion, to be sure that developer
  1599. specified filters, if any, are called first."
  1600. (loop for filter in filters
  1601. if (not value) return nil else
  1602. do (setq value (funcall filter value backend info)))
  1603. value)
  1604. ;;; Core functions
  1605. ;; This is the room for the main function, `org-export-as', along with
  1606. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1607. ;; They differ only by the way they output the resulting code.
  1608. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1609. ;; used with `org-export-to-file'. With a given extension, it tries
  1610. ;; to provide a canonical file name to write export output to.
  1611. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1612. ;; but a copy of it (with the same buffer-local variables and
  1613. ;; visibility), where include keywords are expanded and Babel blocks
  1614. ;; are executed, if appropriate.
  1615. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1616. ;; File inclusion is taken care of by
  1617. ;; `org-export-expand-include-keyword' and
  1618. ;; `org-export-prepare-file-contents'. Structure wise, including
  1619. ;; a whole Org file in a buffer often makes little sense. For
  1620. ;; example, if the file contains an headline and the include keyword
  1621. ;; was within an item, the item should contain the headline. That's
  1622. ;; why file inclusion should be done before any structure can be
  1623. ;; associated to the file, that is before parsing.
  1624. (defun org-export-as (backend
  1625. &optional subtreep visible-only body-only ext-plist)
  1626. "Transcode current Org buffer into BACKEND code.
  1627. If narrowing is active in the current buffer, only transcode its
  1628. narrowed part.
  1629. If a region is active, transcode that region.
  1630. When optional argument SUBTREEP is non-nil, transcode the
  1631. sub-tree at point, extracting information from the headline
  1632. properties first.
  1633. When optional argument VISIBLE-ONLY is non-nil, don't export
  1634. contents of hidden elements.
  1635. When optional argument BODY-ONLY is non-nil, only return body
  1636. code, without preamble nor postamble.
  1637. EXT-PLIST, when provided, is a property list with external
  1638. parameters overriding Org default settings, but still inferior to
  1639. file-local settings.
  1640. Return code as a string."
  1641. (save-excursion
  1642. (save-restriction
  1643. ;; Narrow buffer to an appropriate region for parsing.
  1644. (when (org-region-active-p)
  1645. (narrow-to-region (region-beginning) (region-end))
  1646. (goto-char (point-min)))
  1647. (when (and subtreep (not (org-at-heading-p)))
  1648. ;; Ensure point is at sub-tree's beginning.
  1649. (org-with-limited-levels (org-back-to-heading (not visible-only))))
  1650. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
  1651. ;; Then options can be completed with tree properties. Note:
  1652. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1653. ;; created, where include keywords are expanded and code blocks
  1654. ;; are evaluated. RAW-DATA is the parsed tree of the buffer
  1655. ;; resulting from that process. Eventually call
  1656. ;; `org-export-filter-parse-tree-functions'.
  1657. (let* ((info (org-export-collect-options backend subtreep ext-plist))
  1658. (raw-data (progn
  1659. (when subtreep ; Only parse subtree contents.
  1660. (let ((end (save-excursion (org-end-of-subtree t))))
  1661. (narrow-to-region
  1662. (progn (forward-line) (point)) end)))
  1663. (org-export-filter-apply-functions
  1664. (plist-get info :filter-parse-tree)
  1665. (org-export-with-current-buffer-copy
  1666. (org-export-expand-include-keyword nil)
  1667. (let ((org-current-export-file (current-buffer)))
  1668. (org-export-blocks-preprocess))
  1669. (org-element-parse-buffer nil visible-only))
  1670. backend info))))
  1671. ;; Now get full initial options with tree properties.
  1672. (setq info
  1673. (org-combine-plists
  1674. info
  1675. (org-export-collect-tree-properties raw-data info backend)))
  1676. ;; Now transcode RAW-DATA. Also call
  1677. ;; `org-export-filter-final-output-functions'.
  1678. (let* ((body (org-element-normalize-string
  1679. (org-export-data raw-data backend info)))
  1680. (template (intern (format "org-%s-template" backend)))
  1681. (output (org-export-filter-apply-functions
  1682. (plist-get info :filter-final-output)
  1683. (if (or (not (fboundp template)) body-only) body
  1684. (funcall template body info))
  1685. backend info)))
  1686. ;; Maybe add final OUTPUT to kill ring before returning it.
  1687. (when org-export-copy-to-kill-ring (org-kill-new output))
  1688. output)))))
  1689. (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
  1690. body-only ext-plist)
  1691. "Call `org-export-as' with output to a specified buffer.
  1692. BACKEND is the back-end used for transcoding, as a symbol.
  1693. BUFFER is the output buffer. If it already exists, it will be
  1694. erased first, otherwise, it will be created.
  1695. Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
  1696. similar to those used in `org-export-as', which see.
  1697. Return buffer."
  1698. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
  1699. (buffer (get-buffer-create buffer)))
  1700. (with-current-buffer buffer
  1701. (erase-buffer)
  1702. (insert out)
  1703. (goto-char (point-min)))
  1704. buffer))
  1705. (defun org-export-to-file (backend file &optional subtreep visible-only
  1706. body-only ext-plist)
  1707. "Call `org-export-as' with output to a specified file.
  1708. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1709. is the name of the output file, as a string.
  1710. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  1711. EXT-PLIST are similar to those used in `org-export-as', which
  1712. see.
  1713. Return output file's name."
  1714. ;; Checks for FILE permissions. `write-file' would do the same, but
  1715. ;; we'd rather avoid needless transcoding of parse tree.
  1716. (unless (file-writable-p file) (error "Output file not writable"))
  1717. ;; Insert contents to a temporary buffer and write it to FILE.
  1718. (let ((out (org-export-as
  1719. backend subtreep visible-only body-only ext-plist)))
  1720. (with-temp-buffer
  1721. (insert out)
  1722. (let ((coding-system-for-write org-export-coding-system))
  1723. (write-file file))))
  1724. ;; Return full path.
  1725. file)
  1726. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  1727. "Return output file's name according to buffer specifications.
  1728. EXTENSION is a string representing the output file extension,
  1729. with the leading dot.
  1730. With a non-nil optional argument SUBTREEP, try to determine
  1731. output file's name by looking for \"EXPORT_FILE_NAME\" property
  1732. of subtree at point.
  1733. When optional argument PUB-DIR is set, use it as the publishing
  1734. directory.
  1735. Return file name as a string, or nil if it couldn't be
  1736. determined."
  1737. (let ((base-name
  1738. ;; File name may come from EXPORT_FILE_NAME subtree property,
  1739. ;; assuming point is at beginning of said sub-tree.
  1740. (file-name-sans-extension
  1741. (or (and subtreep
  1742. (org-entry-get
  1743. (save-excursion
  1744. (ignore-errors
  1745. (org-back-to-heading (not visible-only)) (point)))
  1746. "EXPORT_FILE_NAME" t))
  1747. ;; File name may be extracted from buffer's associated
  1748. ;; file, if any.
  1749. (buffer-file-name (buffer-base-buffer))
  1750. ;; Can't determine file name on our own: Ask user.
  1751. (let ((read-file-name-function
  1752. (and org-completion-use-ido 'ido-read-file-name)))
  1753. (read-file-name
  1754. "Output file: " pub-dir nil nil nil
  1755. (lambda (name)
  1756. (string= (file-name-extension name t) extension))))))))
  1757. ;; Build file name. Enforce EXTENSION over whatever user may have
  1758. ;; come up with. PUB-DIR, if defined, always has precedence over
  1759. ;; any provided path.
  1760. (cond
  1761. (pub-dir
  1762. (concat (file-name-as-directory pub-dir)
  1763. (file-name-nondirectory base-name)
  1764. extension))
  1765. ((string= (file-name-nondirectory base-name) base-name)
  1766. (concat (file-name-as-directory ".") base-name extension))
  1767. (t (concat base-name extension)))))
  1768. (defmacro org-export-with-current-buffer-copy (&rest body)
  1769. "Apply BODY in a copy of the current buffer.
  1770. The copy preserves local variables and visibility of the original
  1771. buffer.
  1772. Point is at buffer's beginning when BODY is applied."
  1773. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1774. `(let ((,original-buffer ,(current-buffer))
  1775. (,offset ,(1- (point-min)))
  1776. (,buffer-string ,(buffer-string))
  1777. (,overlays (mapcar
  1778. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1779. (with-temp-buffer
  1780. (let ((buffer-invisibility-spec nil))
  1781. (org-clone-local-variables
  1782. ,original-buffer
  1783. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  1784. (insert ,buffer-string)
  1785. (mapc (lambda (ov)
  1786. (move-overlay
  1787. ov
  1788. (- (overlay-start ov) ,offset)
  1789. (- (overlay-end ov) ,offset)
  1790. (current-buffer)))
  1791. ,overlays)
  1792. (goto-char (point-min))
  1793. (progn ,@body))))))
  1794. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1795. (defun org-export-expand-include-keyword (included)
  1796. "Expand every include keyword in buffer.
  1797. INCLUDED is a list of included file names along with their line
  1798. restriction, when appropriate. It is used to avoid infinite
  1799. recursion."
  1800. (let ((case-fold-search nil))
  1801. (goto-char (point-min))
  1802. (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
  1803. (when (eq (car (save-match-data (org-element-at-point))) 'keyword)
  1804. (beginning-of-line)
  1805. ;; Extract arguments from keyword's value.
  1806. (let* ((value (match-string 1))
  1807. (ind (org-get-indentation))
  1808. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1809. (prog1 (expand-file-name (match-string 1 value))
  1810. (setq value (replace-match "" nil nil value)))))
  1811. (lines
  1812. (and (string-match
  1813. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1814. (prog1 (match-string 1 value)
  1815. (setq value (replace-match "" nil nil value)))))
  1816. (env (cond ((string-match "\\<example\\>" value) 'example)
  1817. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1818. (match-string 1 value))))
  1819. ;; Minimal level of included file defaults to the child
  1820. ;; level of the current headline, if any, or one. It
  1821. ;; only applies is the file is meant to be included as
  1822. ;; an Org one.
  1823. (minlevel
  1824. (and (not env)
  1825. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  1826. (prog1 (string-to-number (match-string 1 value))
  1827. (setq value (replace-match "" nil nil value)))
  1828. (let ((cur (org-current-level)))
  1829. (if cur (1+ (org-reduced-level cur)) 1))))))
  1830. ;; Remove keyword.
  1831. (delete-region (point) (progn (forward-line) (point)))
  1832. (cond
  1833. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  1834. ;; Check if files has already been parsed. Look after
  1835. ;; inclusion lines too, as different parts of the same file
  1836. ;; can be included too.
  1837. ((member (list file lines) included)
  1838. (error "Recursive file inclusion: %s" file))
  1839. (t
  1840. (cond
  1841. ((eq env 'example)
  1842. (insert
  1843. (let ((ind-str (make-string ind ? ))
  1844. (contents
  1845. ;; Protect sensitive contents with commas.
  1846. (replace-regexp-in-string
  1847. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  1848. (org-export-prepare-file-contents file lines)
  1849. nil nil 1)))
  1850. (format "%s#+begin_example\n%s%s#+end_example\n"
  1851. ind-str contents ind-str))))
  1852. ((stringp env)
  1853. (insert
  1854. (let ((ind-str (make-string ind ? ))
  1855. (contents
  1856. ;; Protect sensitive contents with commas.
  1857. (replace-regexp-in-string
  1858. (if (string= env "org") "\\(^\\)\\(.\\)"
  1859. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  1860. (org-export-prepare-file-contents file lines)
  1861. nil nil 1)))
  1862. (format "%s#+begin_src %s\n%s%s#+end_src\n"
  1863. ind-str env contents ind-str))))
  1864. (t
  1865. (insert
  1866. (with-temp-buffer
  1867. (org-mode)
  1868. (insert
  1869. (org-export-prepare-file-contents file lines ind minlevel))
  1870. (org-export-expand-include-keyword
  1871. (cons (list file lines) included))
  1872. (buffer-string))))))))))))
  1873. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  1874. "Prepare the contents of FILE for inclusion and return them as a string.
  1875. When optional argument LINES is a string specifying a range of
  1876. lines, include only those lines.
  1877. Optional argument IND, when non-nil, is an integer specifying the
  1878. global indentation of returned contents. Since its purpose is to
  1879. allow an included file to stay in the same environment it was
  1880. created \(i.e. a list item), it doesn't apply past the first
  1881. headline encountered.
  1882. Optional argument MINLEVEL, when non-nil, is an integer
  1883. specifying the level that any top-level headline in the included
  1884. file should have."
  1885. (with-temp-buffer
  1886. (insert-file-contents file)
  1887. (when lines
  1888. (let* ((lines (split-string lines "-"))
  1889. (lbeg (string-to-number (car lines)))
  1890. (lend (string-to-number (cadr lines)))
  1891. (beg (if (zerop lbeg) (point-min)
  1892. (goto-char (point-min))
  1893. (forward-line (1- lbeg))
  1894. (point)))
  1895. (end (if (zerop lend) (point-max)
  1896. (goto-char (point-min))
  1897. (forward-line (1- lend))
  1898. (point))))
  1899. (narrow-to-region beg end)))
  1900. ;; Remove blank lines at beginning and end of contents. The logic
  1901. ;; behind that removal is that blank lines around include keyword
  1902. ;; override blank lines in included file.
  1903. (goto-char (point-min))
  1904. (org-skip-whitespace)
  1905. (beginning-of-line)
  1906. (delete-region (point-min) (point))
  1907. (goto-char (point-max))
  1908. (skip-chars-backward " \r\t\n")
  1909. (forward-line)
  1910. (delete-region (point) (point-max))
  1911. ;; If IND is set, preserve indentation of include keyword until
  1912. ;; the first headline encountered.
  1913. (when ind
  1914. (unless (eq major-mode 'org-mode) (org-mode))
  1915. (goto-char (point-min))
  1916. (let ((ind-str (make-string ind ? )))
  1917. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  1918. ;; Do not move footnote definitions out of column 0.
  1919. (unless (and (looking-at org-footnote-definition-re)
  1920. (eq (car (org-element-at-point)) 'footnote-definition))
  1921. (insert ind-str))
  1922. (forward-line))))
  1923. ;; When MINLEVEL is specified, compute minimal level for headlines
  1924. ;; in the file (CUR-MIN), and remove stars to each headline so
  1925. ;; that headlines with minimal level have a level of MINLEVEL.
  1926. (when minlevel
  1927. (unless (eq major-mode 'org-mode) (org-mode))
  1928. (let ((levels (org-map-entries
  1929. (lambda () (org-reduced-level (org-current-level))))))
  1930. (when levels
  1931. (let ((offset (- minlevel (apply 'min levels))))
  1932. (unless (zerop offset)
  1933. (when org-odd-levels-only (setq offset (* offset 2)))
  1934. ;; Only change stars, don't bother moving whole
  1935. ;; sections.
  1936. (org-map-entries
  1937. (lambda () (if (< offset 0) (delete-char (abs offset))
  1938. (insert (make-string offset ?*))))))))))
  1939. (buffer-string)))
  1940. ;;; Tools For Back-Ends
  1941. ;; A whole set of tools is available to help build new exporters. Any
  1942. ;; function general enough to have its use across many back-ends
  1943. ;; should be added here.
  1944. ;; As of now, functions operating on footnotes, headlines, links,
  1945. ;; macros, references, src-blocks, tables and tables of contents are
  1946. ;; implemented.
  1947. ;;;; For Footnotes
  1948. ;; `org-export-collect-footnote-definitions' is a tool to list
  1949. ;; actually used footnotes definitions in the whole parse tree, or in
  1950. ;; an headline, in order to add footnote listings throughout the
  1951. ;; transcoded data.
  1952. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  1953. ;; back-ends, when they need to attach the footnote definition only to
  1954. ;; the first occurrence of the corresponding label.
  1955. ;; `org-export-get-footnote-definition' and
  1956. ;; `org-export-get-footnote-number' provide easier access to
  1957. ;; additional information relative to a footnote reference.
  1958. (defun org-export-collect-footnote-definitions (data info)
  1959. "Return an alist between footnote numbers, labels and definitions.
  1960. DATA is the parse tree from which definitions are collected.
  1961. INFO is the plist used as a communication channel.
  1962. Definitions are sorted by order of references. They either
  1963. appear as Org data \(transcoded with `org-export-data'\) or as
  1964. a secondary string for inlined footnotes \(transcoded with
  1965. `org-export-secondary-string'\). Unreferenced definitions are
  1966. ignored."
  1967. (let (refs)
  1968. ;; Collect seen references in REFS.
  1969. (org-element-map
  1970. data 'footnote-reference
  1971. (lambda (footnote local)
  1972. (when (org-export-footnote-first-reference-p footnote local)
  1973. (list (org-export-get-footnote-number footnote local)
  1974. (org-element-get-property :label footnote)
  1975. (org-export-get-footnote-definition footnote local))))
  1976. info)))
  1977. (defun org-export-footnote-first-reference-p (footnote-reference info)
  1978. "Non-nil when a footnote reference is the first one for its label.
  1979. FOOTNOTE-REFERENCE is the footnote reference being considered.
  1980. INFO is the plist used as a communication channel."
  1981. (let ((label (org-element-get-property :label footnote-reference)))
  1982. (or (not label)
  1983. (equal
  1984. footnote-reference
  1985. (org-element-map
  1986. (plist-get info :parse-tree) 'footnote-reference
  1987. (lambda (footnote local)
  1988. (when (string= (org-element-get-property :label footnote) label)
  1989. footnote))
  1990. info 'first-match)))))
  1991. (defun org-export-get-footnote-definition (footnote-reference info)
  1992. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  1993. INFO is the plist used as a communication channel."
  1994. (let ((label (org-element-get-property :label footnote-reference)))
  1995. (or (org-element-get-property :inline-definition footnote-reference)
  1996. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  1997. (defun org-export-get-footnote-number (footnote info)
  1998. "Return number associated to a footnote.
  1999. FOOTNOTE is either a footnote reference or a footnote definition.
  2000. INFO is the plist used as a communication channel."
  2001. (let ((label (org-element-get-property :label footnote)) seen-refs)
  2002. (org-element-map
  2003. (plist-get info :parse-tree) 'footnote-reference
  2004. (lambda (fn local)
  2005. (let ((fn-lbl (org-element-get-property :label fn)))
  2006. (cond
  2007. ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
  2008. ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
  2009. ;; Anonymous footnote: it's always a new one. Also, be sure
  2010. ;; to return nil from the `cond' so `first-match' doesn't
  2011. ;; get us out of the loop.
  2012. ((not fn-lbl) (push 'inline seen-refs) nil)
  2013. ;; Label not seen so far: add it so SEEN-REFS. Again,
  2014. ;; return nil to stay in the loop.
  2015. ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
  2016. info 'first-match)))
  2017. ;;;; For Headlines
  2018. ;; `org-export-get-relative-level' is a shortcut to get headline
  2019. ;; level, relatively to the lower headline level in the parsed tree.
  2020. ;; `org-export-get-headline-number' returns the section number of an
  2021. ;; headline, while `org-export-number-to-roman' allows to convert it
  2022. ;; to roman numbers.
  2023. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2024. ;; `org-export-last-sibling-p' are three useful predicates when it
  2025. ;; comes to fulfill the `:headline-levels' property.
  2026. (defun org-export-get-relative-level (headline info)
  2027. "Return HEADLINE relative level within current parsed tree.
  2028. INFO is a plist holding contextual information."
  2029. (+ (org-element-get-property :level headline)
  2030. (or (plist-get info :headline-offset) 0)))
  2031. (defun org-export-low-level-p (headline info)
  2032. "Non-nil when HEADLINE is considered as low level.
  2033. INFO is a plist used as a communication channel.
  2034. A low level headlines has a relative level greater than
  2035. `:headline-levels' property value.
  2036. Return value is the difference between HEADLINE relative level
  2037. and the last level being considered as high enough, or nil."
  2038. (let ((limit (plist-get info :headline-levels)))
  2039. (when (wholenump limit)
  2040. (let ((level (org-export-get-relative-level headline info)))
  2041. (and (> level limit) (- level limit))))))
  2042. (defun org-export-get-headline-number (headline info)
  2043. "Return HEADLINE numbering as a list of numbers.
  2044. INFO is a plist holding contextual information."
  2045. (cdr (assoc headline (plist-get info :headline-numbering))))
  2046. (defun org-export-number-to-roman (n)
  2047. "Convert integer N into a roman numeral."
  2048. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2049. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2050. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2051. ( 1 . "I")))
  2052. (res ""))
  2053. (if (<= n 0)
  2054. (number-to-string n)
  2055. (while roman
  2056. (if (>= n (caar roman))
  2057. (setq n (- n (caar roman))
  2058. res (concat res (cdar roman)))
  2059. (pop roman)))
  2060. res)))
  2061. (defun org-export-first-sibling-p (headline info)
  2062. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2063. INFO is the plist used as a communication channel."
  2064. (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
  2065. (defun org-export-last-sibling-p (headline info)
  2066. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2067. INFO is the plist used as a communication channel."
  2068. (equal
  2069. (car (last (org-element-get-contents (car (plist-get info :genealogy)))))
  2070. headline))
  2071. ;;;; For Links
  2072. ;; `org-export-solidify-link-text' turns a string into a safer version
  2073. ;; for links, replacing most non-standard characters with hyphens.
  2074. ;; `org-export-get-coderef-format' returns an appropriate format
  2075. ;; string for coderefs.
  2076. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2077. ;; provided should be considered as an inline image.
  2078. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2079. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2080. ;; returns an appropriate unique identifier when found, or nil.
  2081. ;; `org-export-resolve-id-link' returns the first headline with
  2082. ;; specified id or custom-id in parse tree, or nil when none was
  2083. ;; found.
  2084. ;; `org-export-resolve-coderef' associates a reference to a line
  2085. ;; number in the element it belongs, or returns the reference itself
  2086. ;; when the element isn't numbered.
  2087. (defun org-export-solidify-link-text (s)
  2088. "Take link text S and make a safe target out of it."
  2089. (save-match-data
  2090. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
  2091. (defun org-export-get-coderef-format (path desc)
  2092. "Return format string for code reference link.
  2093. PATH is the link path. DESC is its description."
  2094. (save-match-data
  2095. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  2096. (replace-match "%s" t t desc))
  2097. ((string= desc "") "%s")
  2098. (t desc))))
  2099. (defun org-export-inline-image-p (link &optional extensions)
  2100. "Non-nil if LINK object points to an inline image.
  2101. When non-nil, optional argument EXTENSIONS is a list of valid
  2102. extensions for image files, as strings. Otherwise, a default
  2103. list is provided \(cf `org-image-file-name-regexp'\)."
  2104. (and (not (org-element-get-contents link))
  2105. (string= (org-element-get-property :type link) "file")
  2106. (org-file-image-p
  2107. (expand-file-name (org-element-get-property :path link))
  2108. extensions)))
  2109. (defun org-export-resolve-fuzzy-link (link info)
  2110. "Return LINK destination.
  2111. INFO is a plist holding contextual information.
  2112. Return value can be an object, an element, or nil:
  2113. - If LINK path exactly matches any target, return the target
  2114. object.
  2115. - If LINK path exactly matches any headline name, return that
  2116. element. If more than one headline share that name, priority
  2117. will be given to the one with the closest common ancestor, if
  2118. any, or the first one in the parse tree otherwise.
  2119. - Otherwise, return nil.
  2120. Assume LINK type is \"fuzzy\"."
  2121. (let ((path (org-element-get-property :path link)))
  2122. ;; Link points to a target: return it.
  2123. (or (loop for target in (plist-get info :target-list)
  2124. when (string= (org-element-get-property :raw-value target) path)
  2125. return target)
  2126. ;; Link either points to an headline or nothing. Try to find
  2127. ;; the source, with priority given to headlines with the closest
  2128. ;; common ancestor. If such candidate is found, return its
  2129. ;; beginning position as an unique identifier, otherwise return
  2130. ;; nil.
  2131. (let ((find-headline
  2132. (function
  2133. ;; Return first headline whose `:raw-value' property
  2134. ;; is NAME in parse tree DATA, or nil.
  2135. (lambda (name data)
  2136. (org-element-map
  2137. data 'headline
  2138. (lambda (headline local)
  2139. (when (string=
  2140. (org-element-get-property :raw-value headline)
  2141. name)
  2142. headline))
  2143. info 'first-match)))))
  2144. ;; Search among headlines sharing an ancestor with link,
  2145. ;; from closest to farthest.
  2146. (or (catch 'exit
  2147. (mapc
  2148. (lambda (parent)
  2149. (when (eq (car parent) 'headline)
  2150. (let ((foundp (funcall find-headline path parent)))
  2151. (when foundp (throw 'exit foundp)))))
  2152. (plist-get info :genealogy)) nil)
  2153. ;; No match with a common ancestor: try the full parse-tree.
  2154. (funcall find-headline path (plist-get info :parse-tree)))))))
  2155. (defun org-export-resolve-id-link (link info)
  2156. "Return headline referenced as LINK destination.
  2157. INFO is a plist used as a communication channel.
  2158. Return value can be an headline element or nil. Assume LINK type
  2159. is either \"id\" or \"custom-id\"."
  2160. (let ((id (org-element-get-property :path link)))
  2161. (org-element-map
  2162. (plist-get info :parse-tree) 'headline
  2163. (lambda (headline local)
  2164. (when (or (string= (org-element-get-property :id headline) id)
  2165. (string= (org-element-get-property :custom-id headline) id))
  2166. headline))
  2167. info 'first-match)))
  2168. (defun org-export-resolve-coderef (ref info)
  2169. "Resolve a code reference REF.
  2170. INFO is a plist used as a communication channel.
  2171. Return associated line number in source code, or REF itself,
  2172. depending on src-block or example element's switches."
  2173. (org-element-map
  2174. (plist-get info :parse-tree) '(src-block example)
  2175. (lambda (el local)
  2176. (let ((switches (or (org-element-get-property :switches el) "")))
  2177. (with-temp-buffer
  2178. (insert (org-trim (org-element-get-property :value el)))
  2179. ;; Build reference regexp.
  2180. (let* ((label
  2181. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2182. (match-string 1 switches))
  2183. org-coderef-label-format))
  2184. (ref-re
  2185. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2186. (replace-regexp-in-string "%s" ref label nil t))))
  2187. ;; Element containing REF is found. Only associate REF to
  2188. ;; a line number if element has "+n" or "-n" and "-k" or
  2189. ;; "-r" as switches. When it has "+n", count accumulated
  2190. ;; locs before, too.
  2191. (when (re-search-backward ref-re nil t)
  2192. (cond
  2193. ((not (string-match "-[kr]\\>" switches)) ref)
  2194. ((string-match "-n\\>" switches) (line-number-at-pos))
  2195. ((string-match "\\+n\\>" switches)
  2196. (+ (org-export-get-loc el local) (line-number-at-pos)))
  2197. (t ref)))))))
  2198. info 'first-match))
  2199. ;;;; For Macros
  2200. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2201. (defun org-export-expand-macro (macro info)
  2202. "Expand MACRO and return it as a string.
  2203. INFO is a plist holding export options."
  2204. (let* ((key (org-element-get-property :key macro))
  2205. (args (org-element-get-property :args macro))
  2206. ;; User's macros are stored in the communication channel with
  2207. ;; a ":macro-" prefix.
  2208. (value (plist-get info (intern (format ":macro-%s" key)))))
  2209. ;; Replace arguments in VALUE. A nil VALUE removes the macro call
  2210. ;; from export.
  2211. (when (stringp value)
  2212. (let ((s 0) n)
  2213. (while (string-match "\\$\\([0-9]+\\)" value s)
  2214. (setq s (1+ (match-beginning 0))
  2215. n (string-to-number (match-string 1 value)))
  2216. (and (>= (length args) n)
  2217. (setq value (replace-match (nth (1- n) args) t t value)))))
  2218. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2219. (when (string-match "\\`(eval\\>" value)
  2220. (setq value (eval (read value))))
  2221. ;; Return string.
  2222. (format "%s" (or value "")))))
  2223. ;;;; For References
  2224. ;; `org-export-get-ordinal' associates a sequence number to any object
  2225. ;; or element.
  2226. (defun org-export-get-ordinal
  2227. (element info &optional types within-section predicate)
  2228. "Return ordinal number of an element or object.
  2229. ELEMENT is the element or object considered. INFO is the plist
  2230. used as a communication channel.
  2231. Optional argument TYPES, when non-nil, is a list of element or
  2232. object types, as symbols, that should also be counted in.
  2233. Otherwise, only provided element's type is considered.
  2234. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2235. to the section containing ELEMENT.
  2236. Optional argument PREDICATE is a function returning a non-nil
  2237. value if the current element or object should be counted in. It
  2238. accepts one argument: the element or object being considered.
  2239. This argument allows to count only a certain type of objects,
  2240. like inline images, which are a subset of links \(in that case,
  2241. `org-export-inline-image-p' might be an useful predicate\)."
  2242. (let ((counter 0)
  2243. ;; Determine if search should apply to current section, in
  2244. ;; which case it should be retrieved first, or to full parse
  2245. ;; tree. As a special case, an element or object without
  2246. ;; a parent headline will also trigger a full search,
  2247. ;; notwithstanding WITHIN-SECTION value.
  2248. (data
  2249. (if (not within-section) (plist-get info :parse-tree)
  2250. (or (org-export-get-parent-headline element info)
  2251. (plist-get info :parse-tree)))))
  2252. ;; Increment counter until ELEMENT is found again.
  2253. (org-element-map
  2254. data (or types (car element))
  2255. (lambda (el local)
  2256. (cond
  2257. ((equal element el) (1+ counter))
  2258. ((not predicate) (incf counter) nil)
  2259. ((funcall predicate el) (incf counter) nil)))
  2260. info 'first-match)))
  2261. ;;;; For Src-Blocks
  2262. ;; `org-export-get-loc' counts number of code lines accumulated in
  2263. ;; src-block or example-block elements with a "+n" switch until
  2264. ;; a given element, excluded. Note: "-n" switches reset that count.
  2265. ;; `org-export-handle-code' takes care of line numbering and reference
  2266. ;; cleaning in source code, when appropriate.
  2267. (defun org-export-get-loc (element info)
  2268. "Return accumulated lines of code up to ELEMENT.
  2269. INFO is the plist used as a communication channel.
  2270. ELEMENT is excluded from count."
  2271. (let ((loc 0))
  2272. (org-element-map
  2273. (plist-get info :parse-tree) `(src-block example-block ,(car element))
  2274. (lambda (el local)
  2275. (cond
  2276. ;; ELEMENT is reached: Quit the loop.
  2277. ((equal el element) t)
  2278. ;; Only count lines from src-block and example-block elements
  2279. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2280. ((not (memq (car el) '(src-block example-block))) nil)
  2281. ((let ((switches (org-element-get-property :switches el)))
  2282. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2283. ;; Accumulate locs or reset them.
  2284. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2285. (lines (org-count-lines
  2286. (org-trim (org-element-get-property :value el)))))
  2287. (setq loc (if accumulatep lines (+ loc lines))))))
  2288. ;; Return nil to stay in the loop.
  2289. nil)))
  2290. info 'first-match)
  2291. ;; Return value.
  2292. loc))
  2293. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2294. "Handle line numbers and code references in ELEMENT.
  2295. ELEMENT has either a `src-block' an `example-block' type. INFO
  2296. is a plist used as a communication channel.
  2297. If optional argument NUM-FMT is a string, it will be used as
  2298. a format string for numbers at beginning of each line.
  2299. If optional argument REF-FMT is a string, it will be used as
  2300. a format string for each line of code containing a reference.
  2301. When optional argument DELAYED is non-nil, `org-loc' and
  2302. `org-coderef' properties, set to an adequate value, are applied
  2303. to, respectively, numbered lines and lines with a reference. No
  2304. line numbering is done and all references are stripped from the
  2305. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2306. in that situation.
  2307. Return new code as a string."
  2308. (let* ((switches (or (org-element-get-property :switches element) ""))
  2309. (code (org-element-get-property :value element))
  2310. (numberp (string-match "[-+]n\\>" switches))
  2311. (accumulatep (string-match "\\+n\\>" switches))
  2312. ;; Initialize loc counter when any kind of numbering is
  2313. ;; active.
  2314. (total-LOC (cond
  2315. (accumulatep (org-export-get-loc element info))
  2316. (numberp 0)))
  2317. ;; Get code and clean it. Remove blank lines at its
  2318. ;; beginning and end. Also remove protective commas.
  2319. (preserve-indent-p (or org-src-preserve-indentation
  2320. (string-match "-i\\>" switches)))
  2321. (replace-labels (when (string-match "-r\\>" switches)
  2322. (if (string-match "-k\\>" switches) 'keep t)))
  2323. (code (let ((c (replace-regexp-in-string
  2324. "\\`\\([ \t]*\n\\)+" ""
  2325. (replace-regexp-in-string
  2326. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2327. ;; If appropriate, remove global indentation.
  2328. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2329. ;; Free up the protected lines. Note: Org blocks
  2330. ;; have commas at the beginning or every line.
  2331. (if (string=
  2332. (or (org-element-get-property :language element) "")
  2333. "org")
  2334. (replace-regexp-in-string "^," "" c)
  2335. (replace-regexp-in-string
  2336. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2337. ;; Split code to process it line by line.
  2338. (code-lines (org-split-string code "\n"))
  2339. ;; If numbering is active, ensure line numbers will be
  2340. ;; correctly padded before applying the format string.
  2341. (num-fmt
  2342. (when (and (not delayed) numberp)
  2343. (format (if (stringp num-fmt) num-fmt "%s: ")
  2344. (format "%%%ds"
  2345. (length (number-to-string
  2346. (+ (length code-lines) total-LOC)))))))
  2347. ;; Get format used for references.
  2348. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2349. (match-string 1 switches))
  2350. org-coderef-label-format))
  2351. ;; Build a regexp matching a loc with a reference.
  2352. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2353. (replace-regexp-in-string
  2354. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2355. (org-element-normalize-string
  2356. (mapconcat
  2357. (lambda (loc)
  2358. ;; Maybe add line number to current line of code (LOC).
  2359. (when numberp
  2360. (incf total-LOC)
  2361. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2362. (concat (format num-fmt total-LOC) loc))))
  2363. ;; Take action if at a ref line.
  2364. (when (string-match with-ref-re loc)
  2365. (let ((ref (match-string 3 loc)))
  2366. (setq loc
  2367. ;; Option "-r" without "-k" removes labels.
  2368. ;; A non-nil DELAYED removes labels unconditionally.
  2369. (if (or delayed
  2370. (and replace-labels (not (eq replace-labels 'keep))))
  2371. (replace-match "" nil nil loc 1)
  2372. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2373. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2374. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2375. ;; If REF-FMT is defined, apply it to current LOC.
  2376. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2377. ;; Return updated LOC for concatenation.
  2378. loc)
  2379. code-lines "\n"))))
  2380. ;;;; For Tables
  2381. ;; `org-export-table-format-info' extracts formatting information
  2382. ;; (alignment, column groups and presence of a special column) from
  2383. ;; a raw table and returns it as a property list.
  2384. ;;
  2385. ;; `org-export-clean-table' cleans the raw table from any Org
  2386. ;; table-specific syntax.
  2387. (defun org-export-table-format-info (table)
  2388. "Extract info from TABLE.
  2389. Return a plist whose properties and values are:
  2390. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2391. `:column-groups' vector of symbols among `start', `end', `start-end',
  2392. `:row-groups' list of integers representing row groups.
  2393. `:special-column-p' non-nil if table has a special column.
  2394. `:width' vector of integers representing desired width of
  2395. current column, or nil."
  2396. (with-temp-buffer
  2397. (insert table)
  2398. (goto-char 1)
  2399. (org-table-align)
  2400. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2401. org-table-last-alignment)))
  2402. (width (make-vector (length org-table-last-alignment) nil))
  2403. (colgroups (make-vector (length org-table-last-alignment) nil))
  2404. (row-group 0)
  2405. (rowgroups)
  2406. (special-column-p 'empty))
  2407. (mapc (lambda (row)
  2408. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2409. (incf row-group)
  2410. ;; Determine if a special column is present by looking
  2411. ;; for special markers in the first column. More
  2412. ;; accurately, the first column is considered special
  2413. ;; if it only contains special markers and, maybe,
  2414. ;; empty cells.
  2415. (setq special-column-p
  2416. (cond
  2417. ((not special-column-p) nil)
  2418. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2419. 'special)
  2420. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2421. (cond
  2422. ;; Read forced alignment and width information, if any,
  2423. ;; and determine final alignment for the table.
  2424. ((org-table-cookie-line-p row)
  2425. (let ((col 0))
  2426. (mapc (lambda (field)
  2427. (when (string-match
  2428. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2429. (let ((align-data (match-string 1 field)))
  2430. (when align-data (aset align col align-data)))
  2431. (let ((w-data (match-string 2 field)))
  2432. (when w-data
  2433. (aset width col (string-to-number w-data)))))
  2434. (incf col))
  2435. (org-split-string row "[ \t]*|[ \t]*"))))
  2436. ;; Read column groups information.
  2437. ((org-table-colgroup-line-p row)
  2438. (let ((col 0))
  2439. (mapc (lambda (field)
  2440. (aset colgroups col
  2441. (cond ((string= "<" field) 'start)
  2442. ((string= ">" field) 'end)
  2443. ((string= "<>" field) 'start-end)))
  2444. (incf col))
  2445. (org-split-string row "[ \t]*|[ \t]*"))))
  2446. ;; Contents line.
  2447. (t (push row-group rowgroups))))
  2448. (org-split-string table "\n"))
  2449. ;; Return plist.
  2450. (list :alignment align
  2451. :column-groups colgroups
  2452. :row-groups (reverse rowgroups)
  2453. :special-column-p (eq special-column-p 'special)
  2454. :width width))))
  2455. (defun org-export-clean-table (table specialp)
  2456. "Clean string TABLE from its formatting elements.
  2457. Remove any row containing column groups or formatting cookies and
  2458. rows starting with a special marker. If SPECIALP is non-nil,
  2459. assume the table contains a special formatting column and remove
  2460. it also."
  2461. (let ((rows (org-split-string table "\n")))
  2462. (mapconcat 'identity
  2463. (delq nil
  2464. (mapcar
  2465. (lambda (row)
  2466. (cond
  2467. ((org-table-colgroup-line-p row) nil)
  2468. ((org-table-cookie-line-p row) nil)
  2469. ;; Ignore rows starting with a special marker.
  2470. ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
  2471. ;; Remove special column.
  2472. ((and specialp
  2473. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2474. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2475. (replace-match "\\1|" t nil row))
  2476. (t row)))
  2477. rows))
  2478. "\n")))
  2479. ;;;; For Tables Of Contents
  2480. ;; `org-export-collect-headlines' builds a list of all exportable
  2481. ;; headline elements, maybe limited to a certain depth. One can then
  2482. ;; easily parse it and transcode it.
  2483. ;; Building lists of tables, figures or listings is quite similar.
  2484. ;; Once the generic function `org-export-collect-elements' is defined,
  2485. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2486. ;; `org-export-collect-listings' can be derived from it.
  2487. (defun org-export-collect-headlines (info &optional n)
  2488. "Collect headlines in order to build a table of contents.
  2489. INFO is a plist used as a communication channel.
  2490. When non-nil, optional argument N must be an integer. It
  2491. specifies the depth of the table of contents.
  2492. Return a list of all exportable headlines as parsed elements."
  2493. (org-element-map
  2494. (plist-get info :parse-tree)
  2495. 'headline
  2496. (lambda (headline local)
  2497. ;; Strip contents from HEADLINE.
  2498. (let ((relative-level (org-export-get-relative-level headline local)))
  2499. (unless (and n (> relative-level n)) headline)))
  2500. info))
  2501. (defun org-export-collect-elements (type info &optional predicate)
  2502. "Collect referenceable elements of a determined type.
  2503. TYPE can be a symbol or a list of symbols specifying element
  2504. types to search. Only elements with a caption or a name are
  2505. collected.
  2506. INFO is a plist used as a communication channel.
  2507. When non-nil, optional argument PREDICATE is a function accepting
  2508. one argument, an element of type TYPE. It returns a non-nil
  2509. value when that element should be collected.
  2510. Return a list of all elements found, in order of appearance."
  2511. (org-element-map
  2512. (plist-get info :parse-tree) type
  2513. (lambda (element local)
  2514. (and (or (org-element-get-property :caption element)
  2515. (org-element-get-property :name element))
  2516. (or (not predicate) (funcall predicate element))
  2517. element)) info))
  2518. (defun org-export-collect-tables (info)
  2519. "Build a list of tables.
  2520. INFO is a plist used as a communication channel.
  2521. Return a list of table elements with a caption or a name
  2522. affiliated keyword."
  2523. (org-export-collect-elements 'table info))
  2524. (defun org-export-collect-figures (info predicate)
  2525. "Build a list of figures.
  2526. INFO is a plist used as a communication channel. PREDICATE is
  2527. a function which accepts one argument: a paragraph element and
  2528. whose return value is non-nil when that element should be
  2529. collected.
  2530. A figure is a paragraph type element, with a caption or a name,
  2531. verifying PREDICATE. The latter has to be provided since
  2532. a \"figure\" is a vague concept that may depend on back-end.
  2533. Return a list of elements recognized as figures."
  2534. (org-export-collect-elements 'paragraph info predicate))
  2535. (defun org-export-collect-listings (info)
  2536. "Build a list of src blocks.
  2537. INFO is a plist used as a communication channel.
  2538. Return a list of src-block elements with a caption or a name
  2539. affiliated keyword."
  2540. (org-export-collect-elements 'src-block info))
  2541. ;;;; Topology
  2542. (defun org-export-get-parent-headline (blob info)
  2543. "Return BLOB's closest parent headline or nil.
  2544. INFO is a plist used as a communication channel."
  2545. (catch 'exit
  2546. (mapc
  2547. (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
  2548. (plist-get info :genealogy))
  2549. nil))
  2550. (defun org-export-get-previous-element (blob info)
  2551. "Return previous element or object.
  2552. BLOB is an element or object. INFO is a plist used as
  2553. a communication channel.
  2554. Return previous element or object, a string, or nil."
  2555. (let ((parent (car (plist-get info :genealogy))))
  2556. (cadr (member blob (reverse (org-element-get-contents parent))))))
  2557. (defun org-export-get-next-element (blob info)
  2558. "Return next element or object.
  2559. BLOB is an element or object. INFO is a plist used as
  2560. a communication channel.
  2561. Return next element or object, a string, or nil."
  2562. (let ((parent (car (plist-get info :genealogy))))
  2563. (cadr (member blob (org-element-get-contents parent)))))
  2564. ;;; The Dispatcher
  2565. ;; `org-export-dispatch' is the standard interactive way to start an
  2566. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2567. ;; for its interface. Most commons back-ends should have an entry in
  2568. ;; it.
  2569. (defun org-export-dispatch ()
  2570. "Export dispatcher for Org mode.
  2571. It provides an access to common export related tasks in a buffer.
  2572. Its interface comes in two flavours: standard and expert. While
  2573. both share the same set of bindings, only the former displays the
  2574. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2575. to switch to one or the other.
  2576. Return an error if key pressed has no associated command."
  2577. (interactive)
  2578. (let* ((input (org-export-dispatch-ui
  2579. (if (listp org-export-initial-scope) org-export-initial-scope
  2580. (list org-export-initial-scope))
  2581. org-export-dispatch-use-expert-ui))
  2582. (raw-key (car input))
  2583. (scope (cdr input)))
  2584. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2585. ;; depending on user's key pressed.
  2586. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2587. ;; Export with `e-ascii' back-end.
  2588. ((?A ?N ?U)
  2589. (let ((outbuf
  2590. (org-export-to-buffer
  2591. 'e-ascii "*Org E-ASCII Export*"
  2592. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
  2593. `(:ascii-charset
  2594. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  2595. (with-current-buffer outbuf (text-mode))
  2596. (when org-export-show-temporary-export-buffer
  2597. (switch-to-buffer-other-window outbuf))))
  2598. ((?a ?n ?u)
  2599. (org-e-ascii-export-to-ascii
  2600. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
  2601. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  2602. ;; Export with `e-latex' back-end.
  2603. (?L
  2604. (let ((outbuf
  2605. (org-export-to-buffer
  2606. 'e-latex "*Org E-LaTeX Export*"
  2607. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
  2608. (with-current-buffer outbuf (latex-mode))
  2609. (when org-export-show-temporary-export-buffer
  2610. (switch-to-buffer-other-window outbuf))))
  2611. (?l (org-e-latex-export-to-latex
  2612. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
  2613. (?p (org-e-latex-export-to-pdf
  2614. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
  2615. (?d (org-open-file
  2616. (org-e-latex-export-to-pdf
  2617. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
  2618. ;; Undefined command.
  2619. (t (error "No command associated with key %s"
  2620. (char-to-string raw-key))))))
  2621. (defun org-export-dispatch-ui (scope expertp)
  2622. "Handle interface for `org-export-dispatch'.
  2623. SCOPE is a list containing current interactive options set for
  2624. export. It can contain any of the following symbols:
  2625. `body' toggles a body-only export
  2626. `subtree' restricts export to current subtree
  2627. `visible' restricts export to visible part of buffer.
  2628. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2629. buffer is provided, but indications about currently active
  2630. options are given in the prompt. Moreover, \[?] allows to switch
  2631. back to standard interface.
  2632. Return value is a list with key pressed as car and a list of
  2633. final interactive export options as cdr."
  2634. (let ((help (format "------------------- General Options --------------------
  2635. \[1] Body only: %s
  2636. \[2] Export scope: %s
  2637. \[3] Visible only: %s
  2638. -------------- ASCII/Latin-1/UTF-8 Export --------------
  2639. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  2640. --------------------- LaTeX Export ---------------------
  2641. \[l] to TEX file [L] to temporary buffer
  2642. \[p] to PDF file [d] ... and open it"
  2643. (if (memq 'body scope) "On" "Off")
  2644. (if (memq 'subtree scope) "Subtree" "Buffer")
  2645. (if (memq 'visible scope) "On" "Off")))
  2646. (standard-prompt "Export command: ")
  2647. (expert-prompt (format "Export command (%s%s%s): "
  2648. (if (memq 'body scope) "b" "-")
  2649. (if (memq 'subtree scope) "s" "-")
  2650. (if (memq 'visible scope) "v" "-")))
  2651. (handle-keypress
  2652. (function
  2653. ;; Read a character from command input, toggling interactive
  2654. ;; options when applicable. PROMPT is the displayed prompt,
  2655. ;; as a string.
  2656. (lambda (prompt)
  2657. (let ((key (read-char-exclusive prompt)))
  2658. (cond
  2659. ;; Ignore non-standard characters (i.e. "M-a").
  2660. ((not (characterp key)) (org-export-dispatch-ui scope expertp))
  2661. ;; Switch back to standard interface.
  2662. ((and (eq key ??) expertp) (org-export-dispatch-ui scope nil))
  2663. ((eq key ?1)
  2664. (org-export-dispatch-ui
  2665. (if (memq 'body scope) (remq 'body scope) (cons 'body scope))
  2666. expertp))
  2667. ((eq key ?2)
  2668. (org-export-dispatch-ui
  2669. (if (memq 'subtree scope) (remq 'subtree scope)
  2670. (cons 'subtree scope))
  2671. expertp))
  2672. ((eq key ?3)
  2673. (org-export-dispatch-ui
  2674. (if (memq 'visible scope) (remq 'visible scope)
  2675. (cons 'visible scope))
  2676. expertp))
  2677. (t (cons key scope))))))))
  2678. ;; With expert UI, just read key with a fancy prompt. In standard
  2679. ;; UI, display an intrusive help buffer.
  2680. (if expertp (funcall handle-keypress expert-prompt)
  2681. (save-window-excursion
  2682. (delete-other-windows)
  2683. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  2684. (org-fit-window-to-buffer
  2685. (get-buffer-window "*Org Export/Publishing Help*"))
  2686. (funcall handle-keypress standard-prompt)))))
  2687. (provide 'org-export)
  2688. ;;; org-export.el ends here