org-export.el 114 KB

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