org-export.el 121 KB

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