org-export.el 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016
  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. ;;
  17. ;; This library implements a generic export engine for Org, built on
  18. ;; its syntactical parser: Org Elements.
  19. ;;
  20. ;; Besides that parser, the generic exporter is made of three distinct
  21. ;; parts:
  22. ;;
  23. ;; - The communication channel consists in a property list, which is
  24. ;; created and updated during the process. Its use is to offer
  25. ;; every piece of information, would it be about initial environment
  26. ;; or contextual data, all in a single place. The exhaustive list
  27. ;; of properties is given in "The Communication Channel" section of
  28. ;; this file.
  29. ;;
  30. ;; - The transcoder walks the parse tree, ignores or treat as plain
  31. ;; text elements and objects according to export options, and
  32. ;; eventually calls back-end specific functions to do the real
  33. ;; transcoding, concatenating their return value along the way.
  34. ;;
  35. ;; - The filter system is activated at the very beginning and the very
  36. ;; end of the export process, and each time an element or an object
  37. ;; has been converted. It is the entry point to fine-tune standard
  38. ;; output from back-end transcoders.
  39. ;;
  40. ;; The core function is `org-export-as'. It returns the transcoded
  41. ;; buffer as a string.
  42. ;;
  43. ;; In order to derive an exporter out of this generic implementation,
  44. ;; one can define a transcode function for each element or object.
  45. ;; Such function should return a string for the corresponding element,
  46. ;; without any trailing space, or nil. It must accept three
  47. ;; arguments:
  48. ;; 1. the element or object itself,
  49. ;; 2. its contents, or nil when it isn't recursive,
  50. ;; 3. the property list used as a communication channel.
  51. ;;
  52. ;; If no such function is found, that element or object type will
  53. ;; simply be ignored, along with any separating blank line. The same
  54. ;; will happen if the function returns the nil value. If that
  55. ;; function returns the empty string, the type will be ignored, but
  56. ;; the blank lines will be kept.
  57. ;;
  58. ;; Contents, when not nil, are stripped from any global indentation
  59. ;; (although the relative one is preserved). They also always end
  60. ;; with a single newline character.
  61. ;;
  62. ;; These functions must follow a strict naming convention:
  63. ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
  64. ;; export back-end and TYPE the type of the element or object handled.
  65. ;;
  66. ;; Moreover, two additional functions can be defined. On the one
  67. ;; hand, `org-BACKEND-template' returns the final transcoded string,
  68. ;; and can be used to add a preamble and a postamble to document's
  69. ;; body. It must accept two arguments: the transcoded string and the
  70. ;; property list containing export options. On the other hand,
  71. ;; `org-BACKEND-plain-text', when defined, is to be called on every
  72. ;; text not recognized as an element or an object. It must accept two
  73. ;; arguments: the text string and the information channel.
  74. ;;
  75. ;; Any back-end can define its own variables. Among them, those
  76. ;; customizables should belong to the `org-export-BACKEND' group.
  77. ;; Also, a special variable, `org-BACKEND-option-alist', allows to
  78. ;; define buffer keywords and "#+options:" items specific to that
  79. ;; back-end. See `org-export-option-alist' for supported defaults and
  80. ;; syntax.
  81. ;;
  82. ;; Tools for common tasks across back-ends are implemented in the
  83. ;; penultimate part of this file. A dispatcher for standard back-ends
  84. ;; is provided in the last one.
  85. ;;; Code:
  86. (eval-when-compile (require 'cl))
  87. (require 'org-element)
  88. ;; Require major back-ends and publishing tools
  89. (require 'org-e-ascii "./org-e-ascii.el")
  90. (require 'org-e-html "./org-e-html.el")
  91. (require 'org-e-latex "./org-e-latex.el")
  92. (require 'org-e-odt "./org-e-odt.el")
  93. (require 'org-e-publish "./org-e-publish.el")
  94. ;;; Internal Variables
  95. ;;
  96. ;; Among internal variables, the most important is
  97. ;; `org-export-option-alist'. This variable define the global export
  98. ;; options, shared between every exporter, and how they are acquired.
  99. (defconst org-export-max-depth 19
  100. "Maximum nesting depth for headlines, counting from 0.")
  101. (defconst org-export-option-alist
  102. '((:author "AUTHOR" nil user-full-name t)
  103. (:creator "CREATOR" nil org-export-creator-string)
  104. (:date "DATE" nil nil t)
  105. (:description "DESCRIPTION" nil nil newline)
  106. (:email "EMAIL" nil user-mail-address t)
  107. (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
  108. (:headline-levels nil "H" org-export-headline-levels)
  109. (:keywords "KEYWORDS" nil nil space)
  110. (:language "LANGUAGE" nil org-export-default-language t)
  111. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  112. (:section-numbers nil "num" org-export-with-section-numbers)
  113. (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
  114. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  115. (:title "TITLE" nil nil space)
  116. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  117. (:with-author nil "author" org-export-with-author)
  118. (:with-clocks nil "c" org-export-with-clocks)
  119. (:with-creator nil "creator" org-export-with-creator)
  120. (:with-drawers nil "d" org-export-with-drawers)
  121. (:with-email nil "email" org-export-with-email)
  122. (:with-emphasize nil "*" org-export-with-emphasize)
  123. (:with-entities nil "e" org-export-with-entities)
  124. (:with-fixed-width nil ":" org-export-with-fixed-width)
  125. (:with-footnotes nil "f" org-export-with-footnotes)
  126. (:with-plannings nil "p" org-export-with-planning)
  127. (:with-priority nil "pri" org-export-with-priority)
  128. (:with-special-strings nil "-" org-export-with-special-strings)
  129. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  130. (:with-toc nil "toc" org-export-with-toc)
  131. (:with-tables nil "|" org-export-with-tables)
  132. (:with-tags nil "tags" org-export-with-tags)
  133. (:with-tasks nil "tasks" org-export-with-tasks)
  134. (:with-timestamps nil "<" org-export-with-timestamps)
  135. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  136. "Alist between export properties and ways to set them.
  137. The CAR of the alist is the property name, and the CDR is a list
  138. like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
  139. KEYWORD is a string representing a buffer keyword, or nil.
  140. OPTION is a string that could be found in an #+OPTIONS: line.
  141. DEFAULT is the default value for the property.
  142. BEHAVIOUR determine how Org should handle multiple keywords for
  143. the same property. It is a symbol among:
  144. nil Keep old value and discard the new one.
  145. t Replace old value with the new one.
  146. `space' Concatenate the values, separating them with a space.
  147. `newline' Concatenate the values, separating them with
  148. a newline.
  149. `split' Split values at white spaces, and cons them to the
  150. previous list.
  151. KEYWORD and OPTION have precedence over DEFAULT.
  152. All these properties should be back-end agnostic. For back-end
  153. specific properties, define a similar variable named
  154. `org-BACKEND-option-alist', replacing BACKEND with the name of
  155. the appropriate back-end. You can also redefine properties
  156. there, as they have precedence over these.")
  157. (defconst org-export-special-keywords
  158. '("SETUP_FILE" "OPTIONS" "MACRO")
  159. "List of in-buffer keywords that require special treatment.
  160. These keywords are not directly associated to a property. The
  161. way they are handled must be hard-coded into
  162. `org-export-get-inbuffer-options' function.")
  163. (defconst org-export-filters-alist
  164. '((:filter-bold . org-export-filter-bold-functions)
  165. (:filter-babel-call . org-export-filter-babel-call-functions)
  166. (:filter-center-block . org-export-filter-center-block-functions)
  167. (:filter-clock . org-export-filter-clock-functions)
  168. (:filter-code . org-export-filter-code-functions)
  169. (:filter-comment . org-export-filter-comment-functions)
  170. (:filter-comment-block . org-export-filter-comment-block-functions)
  171. (:filter-drawer . org-export-filter-drawer-functions)
  172. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  173. (:filter-entity . org-export-filter-entity-functions)
  174. (:filter-example-block . org-export-filter-example-block-functions)
  175. (:filter-export-block . org-export-filter-export-block-functions)
  176. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  177. (:filter-final-output . org-export-filter-final-output-functions)
  178. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  179. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  180. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  181. (:filter-headline . org-export-filter-headline-functions)
  182. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  183. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  184. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  185. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  186. (:filter-italic . org-export-filter-italic-functions)
  187. (:filter-item . org-export-filter-item-functions)
  188. (:filter-keyword . org-export-filter-keyword-functions)
  189. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  190. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  191. (:filter-line-break . org-export-filter-line-break-functions)
  192. (:filter-link . org-export-filter-link-functions)
  193. (:filter-macro . org-export-filter-macro-functions)
  194. (:filter-paragraph . org-export-filter-paragraph-functions)
  195. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  196. (:filter-plain-list . org-export-filter-plain-list-functions)
  197. (:filter-plain-text . org-export-filter-plain-text-functions)
  198. (:filter-planning . org-export-filter-planning-functions)
  199. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  200. (:filter-quote-block . org-export-filter-quote-block-functions)
  201. (:filter-quote-section . org-export-filter-quote-section-functions)
  202. (:filter-radio-target . org-export-filter-radio-target-functions)
  203. (:filter-section . org-export-filter-section-functions)
  204. (:filter-special-block . org-export-filter-special-block-functions)
  205. (:filter-src-block . org-export-filter-src-block-functions)
  206. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  207. (:filter-strike-through . org-export-filter-strike-through-functions)
  208. (:filter-subscript . org-export-filter-subscript-functions)
  209. (:filter-superscript . org-export-filter-superscript-functions)
  210. (:filter-table . org-export-filter-table-functions)
  211. (:filter-table-cell . org-export-filter-table-cell-functions)
  212. (:filter-table-row . org-export-filter-table-row-functions)
  213. (:filter-target . org-export-filter-target-functions)
  214. (:filter-timestamp . org-export-filter-timestamp-functions)
  215. (:filter-underline . org-export-filter-underline-functions)
  216. (:filter-verbatim . org-export-filter-verbatim-functions)
  217. (:filter-verse-block . org-export-filter-verse-block-functions))
  218. "Alist between filters properties and initial values.
  219. The key of each association is a property name accessible through
  220. the communication channel its value is a configurable global
  221. variable defining initial filters.
  222. This list is meant to install user specified filters. Back-end
  223. developers may install their own filters using
  224. `org-BACKEND-filters-alist', where BACKEND is the name of the
  225. considered back-end. Filters defined there will always be
  226. prepended to the current list, so they always get applied
  227. first.")
  228. (defconst org-export-default-inline-image-rule
  229. `(("file" .
  230. ,(format "\\.%s\\'"
  231. (regexp-opt
  232. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  233. "xpm" "pbm" "pgm" "ppm") t))))
  234. "Default rule for link matching an inline image.
  235. This rule applies to links with no description. By default, it
  236. will be considered as an inline image if it targets a local file
  237. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  238. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  239. See `org-export-inline-image-p' for more information about
  240. rules.")
  241. ;;; User-configurable Variables
  242. ;;
  243. ;; Configuration for the masses.
  244. ;;
  245. ;; They should never be accessed directly, as their value is to be
  246. ;; stored in a property list (cf. `org-export-option-alist').
  247. ;; Back-ends will read their value from there instead.
  248. (defgroup org-export nil
  249. "Options for exporting Org mode files."
  250. :tag "Org Export"
  251. :group 'org)
  252. (defgroup org-export-general nil
  253. "General options for export engine."
  254. :tag "Org Export General"
  255. :group 'org-export)
  256. (defcustom org-export-with-archived-trees 'headline
  257. "Whether sub-trees with the ARCHIVE tag should be exported.
  258. This can have three different values:
  259. nil Do not export, pretend this tree is not present.
  260. t Do export the entire tree.
  261. `headline' Only export the headline, but skip the tree below it.
  262. This option can also be set with the #+OPTIONS line,
  263. e.g. \"arch:nil\"."
  264. :group 'org-export-general
  265. :type '(choice
  266. (const :tag "Not at all" nil)
  267. (const :tag "Headline only" 'headline)
  268. (const :tag "Entirely" t)))
  269. (defcustom org-export-with-author t
  270. "Non-nil means insert author name into the exported file.
  271. This option can also be set with the #+OPTIONS line,
  272. e.g. \"author:nil\"."
  273. :group 'org-export-general
  274. :type 'boolean)
  275. (defcustom org-export-with-clocks nil
  276. "Non-nil means export CLOCK keywords.
  277. This option can also be set with the #+OPTIONS line,
  278. e.g. \"c:t\"."
  279. :group 'org-export-general
  280. :type 'boolean)
  281. (defcustom org-export-with-creator 'comment
  282. "Non-nil means the postamble should contain a creator sentence.
  283. The sentence can be set in `org-export-creator-string' and
  284. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  285. If the value is `comment' insert it as a comment."
  286. :group 'org-export-general
  287. :type '(choice
  288. (const :tag "No creator sentence" nil)
  289. (const :tag "Sentence as a comment" 'comment)
  290. (const :tag "Insert the sentence" t)))
  291. (defcustom org-export-creator-string
  292. (format "Generated by Org mode %s in Emacs %s."
  293. (if (fboundp 'org-version) (org-version) "(Unknown)")
  294. emacs-version)
  295. "String to insert at the end of the generated document."
  296. :group 'org-export-general
  297. :type '(string :tag "Creator string"))
  298. (defcustom org-export-with-drawers t
  299. "Non-nil means export contents of standard drawers.
  300. When t, all drawers are exported. This may also be a list of
  301. drawer names to export. This variable doesn't apply to
  302. properties drawers.
  303. This option can also be set with the #+OPTIONS line,
  304. e.g. \"d:nil\"."
  305. :group 'org-export-general
  306. :type '(choice
  307. (const :tag "All drawers" t)
  308. (const :tag "None" nil)
  309. (repeat :tag "Selected drawers"
  310. (string :tag "Drawer name"))))
  311. (defcustom org-export-with-email nil
  312. "Non-nil means insert author email into the exported file.
  313. This option can also be set with the #+OPTIONS line,
  314. e.g. \"email:t\"."
  315. :group 'org-export-general
  316. :type 'boolean)
  317. (defcustom org-export-with-emphasize t
  318. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  319. If the export target supports emphasizing text, the word will be
  320. typeset in bold, italic, or underlined, respectively. Not all
  321. export backends support this.
  322. This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
  323. :group 'org-export-general
  324. :type 'boolean)
  325. (defcustom org-export-exclude-tags '("noexport")
  326. "Tags that exclude a tree from export.
  327. All trees carrying any of these tags will be excluded from
  328. export. This is without condition, so even subtrees inside that
  329. carry one of the `org-export-select-tags' will be removed.
  330. This option can also be set with the #+EXPORT_EXCLUDE_TAGS:
  331. keyword."
  332. :group 'org-export-general
  333. :type '(repeat (string :tag "Tag")))
  334. (defcustom org-export-with-fixed-width t
  335. "Non-nil means lines starting with \":\" will be in fixed width font.
  336. This can be used to have pre-formatted text, fragments of code
  337. etc. For example:
  338. : ;; Some Lisp examples
  339. : (while (defc cnt)
  340. : (ding))
  341. will be looking just like this in also HTML. See also the QUOTE
  342. keyword. Not all export backends support this.
  343. This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
  344. :group 'org-export-translation
  345. :type 'boolean)
  346. (defcustom org-export-with-footnotes t
  347. "Non-nil means Org footnotes should be exported.
  348. This option can also be set with the #+OPTIONS line,
  349. e.g. \"f:nil\"."
  350. :group 'org-export-general
  351. :type 'boolean)
  352. (defcustom org-export-headline-levels 3
  353. "The last level which is still exported as a headline.
  354. Inferior levels will produce itemize lists when exported. Note
  355. that a numeric prefix argument to an exporter function overrides
  356. this setting.
  357. This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
  358. :group 'org-export-general
  359. :type 'integer)
  360. (defcustom org-export-default-language "en"
  361. "The default language for export and clocktable translations, as a string.
  362. This may have an association in
  363. `org-clock-clocktable-language-setup'."
  364. :group 'org-export-general
  365. :type '(string :tag "Language"))
  366. (defcustom org-export-preserve-breaks nil
  367. "Non-nil means preserve all line breaks when exporting.
  368. Normally, in HTML output paragraphs will be reformatted.
  369. This option can also be set with the #+OPTIONS line,
  370. e.g. \"\\n:t\"."
  371. :group 'org-export-general
  372. :type 'boolean)
  373. (defcustom org-export-with-entities t
  374. "Non-nil means interpret entities when exporting.
  375. For example, HTML export converts \\alpha to &alpha; and \\AA to
  376. &Aring;.
  377. For a list of supported names, see the constant `org-entities'
  378. and the user option `org-entities-user'.
  379. This option can also be set with the #+OPTIONS line,
  380. e.g. \"e:nil\"."
  381. :group 'org-export-general
  382. :type 'boolean)
  383. (defcustom org-export-with-planning nil
  384. "Non-nil means include planning info in export.
  385. This option can also be set with the #+OPTIONS: line,
  386. e.g. \"p:t\"."
  387. :group 'org-export-general
  388. :type 'boolean)
  389. (defcustom org-export-with-priority nil
  390. "Non-nil means include priority cookies in export.
  391. When nil, remove priority cookies for export.
  392. This option can also be set with the #+OPTIONS line,
  393. e.g. \"pri:t\"."
  394. :group 'org-export-general
  395. :type 'boolean)
  396. (defcustom org-export-with-section-numbers t
  397. "Non-nil means add section numbers to headlines when exporting.
  398. When set to an integer n, numbering will only happen for
  399. headlines whose relative level is higher or equal to n.
  400. This option can also be set with the #+OPTIONS line,
  401. e.g. \"num:t\"."
  402. :group 'org-export-general
  403. :type 'boolean)
  404. (defcustom org-export-select-tags '("export")
  405. "Tags that select a tree for export.
  406. If any such tag is found in a buffer, all trees that do not carry
  407. one of these tags will be ignored during export. Inside trees
  408. that are selected like this, you can still deselect a subtree by
  409. tagging it with one of the `org-export-exclude-tags'.
  410. This option can also be set with the #+EXPORT_SELECT_TAGS:
  411. keyword."
  412. :group 'org-export-general
  413. :type '(repeat (string :tag "Tag")))
  414. (defcustom org-export-with-special-strings t
  415. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  416. When this option is turned on, these strings will be exported as:
  417. Org HTML LaTeX
  418. -----+----------+--------
  419. \\- &shy; \\-
  420. -- &ndash; --
  421. --- &mdash; ---
  422. ... &hellip; \ldots
  423. This option can also be set with the #+OPTIONS line,
  424. e.g. \"-:nil\"."
  425. :group 'org-export-general
  426. :type 'boolean)
  427. (defcustom org-export-with-sub-superscripts t
  428. "Non-nil means interpret \"_\" and \"^\" for export.
  429. When this option is turned on, you can use TeX-like syntax for
  430. sub- and superscripts. Several characters after \"_\" or \"^\"
  431. will be considered as a single item - so grouping with {} is
  432. normally not needed. For example, the following things will be
  433. parsed as single sub- or superscripts.
  434. 10^24 or 10^tau several digits will be considered 1 item.
  435. 10^-12 or 10^-tau a leading sign with digits or a word
  436. x^2-y^3 will be read as x^2 - y^3, because items are
  437. terminated by almost any nonword/nondigit char.
  438. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  439. Still, ambiguity is possible - so when in doubt use {} to enclose
  440. the sub/superscript. If you set this variable to the symbol
  441. `{}', the braces are *required* in order to trigger
  442. interpretations as sub/superscript. This can be helpful in
  443. documents that need \"_\" frequently in plain text.
  444. This option can also be set with the #+OPTIONS line,
  445. e.g. \"^:nil\"."
  446. :group 'org-export-general
  447. :type '(choice
  448. (const :tag "Interpret them" t)
  449. (const :tag "Curly brackets only" {})
  450. (const :tag "Do not interpret them" nil)))
  451. (defcustom org-export-with-toc t
  452. "Non-nil means create a table of contents in exported files.
  453. The TOC contains headlines with levels up
  454. to`org-export-headline-levels'. When an integer, include levels
  455. up to N in the toc, this may then be different from
  456. `org-export-headline-levels', but it will not be allowed to be
  457. larger than the number of headline levels. When nil, no table of
  458. contents is made.
  459. This option can also be set with the #+OPTIONS line,
  460. e.g. \"toc:nil\" or \"toc:3\"."
  461. :group 'org-export-general
  462. :type '(choice
  463. (const :tag "No Table of Contents" nil)
  464. (const :tag "Full Table of Contents" t)
  465. (integer :tag "TOC to level")))
  466. (defcustom org-export-with-tables t
  467. "If non-nil, lines starting with \"|\" define a table.
  468. For example:
  469. | Name | Address | Birthday |
  470. |-------------+----------+-----------|
  471. | Arthur Dent | England | 29.2.2100 |
  472. This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
  473. :group 'org-export-general
  474. :type 'boolean)
  475. (defcustom org-export-with-tags t
  476. "If nil, do not export tags, just remove them from headlines.
  477. If this is the symbol `not-in-toc', tags will be removed from
  478. table of contents entries, but still be shown in the headlines of
  479. the document.
  480. This option can also be set with the #+OPTIONS line,
  481. e.g. \"tags:nil\"."
  482. :group 'org-export-general
  483. :type '(choice
  484. (const :tag "Off" nil)
  485. (const :tag "Not in TOC" not-in-toc)
  486. (const :tag "On" t)))
  487. (defcustom org-export-with-tasks t
  488. "Non-nil means include TODO items for export.
  489. This may have the following values:
  490. t include tasks independent of state.
  491. todo include only tasks that are not yet done.
  492. done include only tasks that are already done.
  493. nil remove all tasks before export
  494. list of keywords keep only tasks with these keywords"
  495. :group 'org-export-general
  496. :type '(choice
  497. (const :tag "All tasks" t)
  498. (const :tag "No tasks" nil)
  499. (const :tag "Not-done tasks" todo)
  500. (const :tag "Only done tasks" done)
  501. (repeat :tag "Specific TODO keywords"
  502. (string :tag "Keyword"))))
  503. (defcustom org-export-time-stamp-file t
  504. "Non-nil means insert a time stamp into the exported file.
  505. The time stamp shows when the file was created.
  506. This option can also be set with the #+OPTIONS line,
  507. e.g. \"timestamp:nil\"."
  508. :group 'org-export-general
  509. :type 'boolean)
  510. (defcustom org-export-with-timestamps t
  511. "Non nil means allow timestamps in export.
  512. It can be set to `active', `inactive', t or nil, in order to
  513. export, respectively, only active timestamps, only inactive ones,
  514. all of them or none.
  515. This option can also be set with the #+OPTIONS line, e.g.
  516. \"<:nil\"."
  517. :group 'org-export-general
  518. :type '(choice
  519. (const :tag "All timestamps" t)
  520. (const :tag "Only active timestamps" active)
  521. (const :tag "Only inactive timestamps" inactive)
  522. (const :tag "No timestamp" nil)))
  523. (defcustom org-export-with-todo-keywords t
  524. "Non-nil means include TODO keywords in export.
  525. When nil, remove all these keywords from the export."
  526. :group 'org-export-general
  527. :type 'boolean)
  528. (defcustom org-export-allow-BIND 'confirm
  529. "Non-nil means allow #+BIND to define local variable values for export.
  530. This is a potential security risk, which is why the user must
  531. confirm the use of these lines."
  532. :group 'org-export-general
  533. :type '(choice
  534. (const :tag "Never" nil)
  535. (const :tag "Always" t)
  536. (const :tag "Ask a confirmation for each file" confirm)))
  537. (defcustom org-export-snippet-translation-alist nil
  538. "Alist between export snippets back-ends and exporter back-ends.
  539. This variable allows to provide shortcuts for export snippets.
  540. For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
  541. back-end will recognize the contents of \"@h{<b>}\" as HTML code
  542. while every other back-end will ignore it."
  543. :group 'org-export-general
  544. :type '(repeat
  545. (cons
  546. (string :tag "Shortcut")
  547. (string :tag "Back-end"))))
  548. (defcustom org-export-coding-system nil
  549. "Coding system for the exported file."
  550. :group 'org-export-general
  551. :type 'coding-system)
  552. (defcustom org-export-copy-to-kill-ring t
  553. "Non-nil means exported stuff will also be pushed onto the kill ring."
  554. :group 'org-export-general
  555. :type 'boolean)
  556. (defcustom org-export-initial-scope 'buffer
  557. "The initial scope when exporting with `org-export-dispatch'.
  558. This variable can be either set to `buffer' or `subtree'."
  559. :group 'org-export-general
  560. :type '(choice
  561. (const :tag "Export current buffer" 'buffer)
  562. (const :tag "Export current subtree" 'subtree)))
  563. (defcustom org-export-show-temporary-export-buffer t
  564. "Non-nil means show buffer after exporting to temp buffer.
  565. When Org exports to a file, the buffer visiting that file is ever
  566. shown, but remains buried. However, when exporting to
  567. a temporary buffer, that buffer is popped up in a second window.
  568. When this variable is nil, the buffer remains buried also in
  569. these cases."
  570. :group 'org-export-general
  571. :type 'boolean)
  572. (defcustom org-export-dispatch-use-expert-ui nil
  573. "Non-nil means using a non-intrusive `org-export-dispatch'.
  574. In that case, no help buffer is displayed. Though, an indicator
  575. for current export scope is added to the prompt \(i.e. \"b\" when
  576. output is restricted to body only, \"s\" when it is restricted to
  577. the current subtree and \"v\" when only visible elements are
  578. considered for export\). Also, \[?] allows to switch back to
  579. standard mode."
  580. :group 'org-export-general
  581. :type 'boolean)
  582. ;;; The Communication Channel
  583. ;;
  584. ;; During export process, every function has access to a number of
  585. ;; properties. They are of three types:
  586. ;;
  587. ;; 1. Environment options are collected once at the very beginning of
  588. ;; the process, out of the original buffer and configuration.
  589. ;; Collecting them is handled by `org-export-get-environment'
  590. ;; function.
  591. ;;
  592. ;; Most environment options are defined through the
  593. ;; `org-export-option-alist' variable.
  594. ;;
  595. ;; 2. Tree properties are extracted directly from the parsed tree,
  596. ;; just before export, by `org-export-collect-tree-properties'.
  597. ;;
  598. ;; 3. Local options are updated during parsing, and their value
  599. ;; depends on the level of recursion. For now, only `:ignore-list'
  600. ;; belongs to that category.
  601. ;;
  602. ;; Here is the full list of properties available during transcode
  603. ;; process, with their category (option, tree or local) and their
  604. ;; value type.
  605. ;;
  606. ;; + `:author' :: Author's name.
  607. ;; - category :: option
  608. ;; - type :: string
  609. ;;
  610. ;; + `:back-end' :: Current back-end used for transcoding.
  611. ;; - category :: tree
  612. ;; - type :: symbol
  613. ;;
  614. ;; + `:creator' :: String to write as creation information.
  615. ;; - category :: option
  616. ;; - type :: string
  617. ;;
  618. ;; + `:date' :: String to use as date.
  619. ;; - category :: option
  620. ;; - type :: string
  621. ;;
  622. ;; + `:description' :: Description text for the current data.
  623. ;; - category :: option
  624. ;; - type :: string
  625. ;;
  626. ;; + `:email' :: Author's email.
  627. ;; - category :: option
  628. ;; - type :: string
  629. ;;
  630. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  631. ;; process.
  632. ;; - category :: option
  633. ;; - type :: list of strings
  634. ;;
  635. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  636. ;; their definition, as parsed data. Only non-inlined footnotes
  637. ;; are represented in this alist. Also, every definition isn't
  638. ;; guaranteed to be referenced in the parse tree. The purpose of
  639. ;; this property is to preserve definitions from oblivion
  640. ;; (i.e. when the parse tree comes from a part of the original
  641. ;; buffer), it isn't meant for direct use in a back-end. To
  642. ;; retrieve a definition relative to a reference, use
  643. ;; `org-export-get-footnote-definition' instead.
  644. ;; - category :: option
  645. ;; - type :: alist (STRING . LIST)
  646. ;;
  647. ;; + `:headline-levels' :: Maximum level being exported as an
  648. ;; headline. Comparison is done with the relative level of
  649. ;; headlines in the parse tree, not necessarily with their
  650. ;; actual level.
  651. ;; - category :: option
  652. ;; - type :: integer
  653. ;;
  654. ;; + `:headline-offset' :: Difference between relative and real level
  655. ;; of headlines in the parse tree. For example, a value of -1
  656. ;; means a level 2 headline should be considered as level
  657. ;; 1 (cf. `org-export-get-relative-level').
  658. ;; - category :: tree
  659. ;; - type :: integer
  660. ;;
  661. ;; + `:headline-numbering' :: Alist between headlines and their
  662. ;; numbering, as a list of numbers
  663. ;; (cf. `org-export-get-headline-number').
  664. ;; - category :: tree
  665. ;; - type :: alist (INTEGER . LIST)
  666. ;;
  667. ;; + `:ignore-list' :: List of elements and objects that should be
  668. ;; ignored during export.
  669. ;; - category :: local
  670. ;; - type :: list of elements and objects
  671. ;;
  672. ;; + `:input-file' :: Full path to input file, if any.
  673. ;; - category :: option
  674. ;; - type :: string or nil
  675. ;;
  676. ;; + `:keywords' :: List of keywords attached to data.
  677. ;; - category :: option
  678. ;; - type :: string
  679. ;;
  680. ;; + `:language' :: Default language used for translations.
  681. ;; - category :: option
  682. ;; - type :: string
  683. ;;
  684. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  685. ;; transcoding.
  686. ;; - category :: global
  687. ;; - type :: list (as returned by `org-element-parse-buffer')
  688. ;;
  689. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  690. ;; all line breaks.
  691. ;; - category :: option
  692. ;; - type :: symbol (nil, t)
  693. ;;
  694. ;; + `:section-numbers' :: Non-nil means transcoding should add
  695. ;; section numbers to headlines.
  696. ;; - category :: option
  697. ;; - type :: symbol (nil, t)
  698. ;;
  699. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  700. ;; in transcoding. When such a tag is present, subtrees without
  701. ;; it are de facto excluded from the process. See
  702. ;; `use-select-tags'.
  703. ;; - category :: option
  704. ;; - type :: list of strings
  705. ;;
  706. ;; + `:target-list' :: List of targets encountered in the parse tree.
  707. ;; This is used to partly resolve "fuzzy" links
  708. ;; (cf. `org-export-resolve-fuzzy-link').
  709. ;; - category :: tree
  710. ;; - type :: list of strings
  711. ;;
  712. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  713. ;; a time stamp in the output.
  714. ;; - category :: option
  715. ;; - type :: symbol (nil, t)
  716. ;;
  717. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  718. ;; also be transcoded. If it is set to the `headline' symbol,
  719. ;; only the archived headline's name is retained.
  720. ;; - category :: option
  721. ;; - type :: symbol (nil, t, `headline')
  722. ;;
  723. ;; + `:with-author' :: Non-nil means author's name should be included
  724. ;; in the output.
  725. ;; - category :: option
  726. ;; - type :: symbol (nil, t)
  727. ;;
  728. ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
  729. ;; - category :: option
  730. ;; - type :: symbol (nil, t)
  731. ;;
  732. ;; + `:with-creator' :: Non-nild means a creation sentence should be
  733. ;; inserted at the end of the transcoded string. If the value
  734. ;; is `comment', it should be commented.
  735. ;; - category :: option
  736. ;; - type :: symbol (`comment', nil, t)
  737. ;;
  738. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  739. ;; its value is a list of names, only drawers with such names
  740. ;; will be transcoded.
  741. ;; - category :: option
  742. ;; - type :: symbol (nil, t) or list of strings
  743. ;;
  744. ;; + `:with-email' :: Non-nil means output should contain author's
  745. ;; email.
  746. ;; - category :: option
  747. ;; - type :: symbol (nil, t)
  748. ;;
  749. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  750. ;; interpreted.
  751. ;; - category :: option
  752. ;; - type :: symbol (nil, t)
  753. ;;
  754. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  755. ;; strings starting with a colon as a fixed-with (verbatim) area.
  756. ;; - category :: option
  757. ;; - type :: symbol (nil, t)
  758. ;;
  759. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  760. ;; footnotes.
  761. ;; - category :: option
  762. ;; - type :: symbol (nil, t)
  763. ;;
  764. ;; + `:with-plannings' :: Non-nil means transcoding should include
  765. ;; planning info.
  766. ;; - category :: option
  767. ;; - type :: symbol (nil, t)
  768. ;;
  769. ;; + `:with-priority' :: Non-nil means transcoding should include
  770. ;; priority cookies.
  771. ;; - category :: option
  772. ;; - type :: symbol (nil, t)
  773. ;;
  774. ;; + `:with-special-strings' :: Non-nil means transcoding should
  775. ;; interpret special strings in plain text.
  776. ;; - category :: option
  777. ;; - type :: symbol (nil, t)
  778. ;;
  779. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  780. ;; interpret subscript and superscript. With a value of "{}",
  781. ;; only interpret those using curly brackets.
  782. ;; - category :: option
  783. ;; - type :: symbol (nil, {}, t)
  784. ;;
  785. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  786. ;; tables.
  787. ;; - category :: option
  788. ;; - type :: symbol (nil, t)
  789. ;;
  790. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  791. ;; headlines. A `not-in-toc' value will remove them from the
  792. ;; table of contents, if any, nonetheless.
  793. ;; - category :: option
  794. ;; - type :: symbol (nil, t, `not-in-toc')
  795. ;;
  796. ;; + `:with-tasks' :: Non-nil means transcoding should include
  797. ;; headlines with a TODO keyword. A `todo' value will only
  798. ;; include headlines with a todo type keyword while a `done'
  799. ;; value will do the contrary. If a list of strings is provided,
  800. ;; only tasks with keywords belonging to that list will be kept.
  801. ;; - category :: option
  802. ;; - type :: symbol (t, todo, done, nil) or list of strings
  803. ;;
  804. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  805. ;; time stamps. Special value `active' (resp. `inactive') ask to
  806. ;; export only active (resp. inactive) timestamps. Otherwise,
  807. ;; completely remove them.
  808. ;; - category :: option
  809. ;; - type :: symbol: (`active', `inactive', t, nil)
  810. ;;
  811. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  812. ;; added to the output. An integer value limits its depth.
  813. ;; - category :: option
  814. ;; - type :: symbol (nil, t or integer)
  815. ;;
  816. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  817. ;; include TODO keywords.
  818. ;; - category :: option
  819. ;; - type :: symbol (nil, t)
  820. ;;;; Environment Options
  821. ;;
  822. ;; Environment options encompass all parameters defined outside the
  823. ;; scope of the parsed data. They come from five sources, in
  824. ;; increasing precedence order:
  825. ;;
  826. ;; - Global variables,
  827. ;; - Buffer's attributes,
  828. ;; - Options keyword symbols,
  829. ;; - Buffer keywords,
  830. ;; - Subtree properties.
  831. ;;
  832. ;; The central internal function with regards to environment options
  833. ;; is `org-export-get-environment'. It updates global variables with
  834. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  835. ;; the different sources.
  836. ;;
  837. ;; The internal functions doing the retrieval are:
  838. ;; `org-export-get-global-options',
  839. ;; `org-export-get-buffer-attributes',
  840. ;; `org-export-parse-option-keyword',
  841. ;; `org-export-get-subtree-options' and
  842. ;; `org-export-get-inbuffer-options'
  843. ;;
  844. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  845. ;; take care of the part relative to "#+BIND:" keywords.
  846. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  847. "Collect export options from the current buffer.
  848. Optional argument BACKEND is a symbol specifying which back-end
  849. specific options to read, if any.
  850. When optional argument SUBTREEP is non-nil, assume the export is
  851. done against the current sub-tree.
  852. Third optional argument EXT-PLIST is a property list with
  853. external parameters overriding Org default settings, but still
  854. inferior to file-local settings."
  855. ;; First install #+BIND variables.
  856. (org-export-install-letbind-maybe)
  857. ;; Get and prioritize export options...
  858. (let ((options (org-combine-plists
  859. ;; ... from global variables...
  860. (org-export-get-global-options backend)
  861. ;; ... from buffer's attributes...
  862. (org-export-get-buffer-attributes)
  863. ;; ... from an external property list...
  864. ext-plist
  865. ;; ... from in-buffer settings...
  866. (org-export-get-inbuffer-options
  867. backend
  868. (and buffer-file-name
  869. (org-remove-double-quotes buffer-file-name)))
  870. ;; ... and from subtree, when appropriate.
  871. (and subtreep (org-export-get-subtree-options))
  872. ;; Also install back-end symbol.
  873. `(:back-end ,backend))))
  874. ;; Return plist.
  875. options))
  876. (defun org-export-parse-option-keyword (options &optional backend)
  877. "Parse an OPTIONS line and return values as a plist.
  878. Optional argument BACKEND is a symbol specifying which back-end
  879. specific items to read, if any."
  880. (let* ((all
  881. (append org-export-option-alist
  882. (and backend
  883. (let ((var (intern
  884. (format "org-%s-option-alist" backend))))
  885. (and (boundp var) (eval var))))))
  886. ;; Build an alist between #+OPTION: item and property-name.
  887. (alist (delq nil
  888. (mapcar (lambda (e)
  889. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  890. (car e))))
  891. all)))
  892. plist)
  893. (mapc (lambda (e)
  894. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  895. (car e)
  896. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  897. options)
  898. (setq plist (plist-put plist
  899. (cdr e)
  900. (car (read-from-string
  901. (match-string 2 options)))))))
  902. alist)
  903. plist))
  904. (defun org-export-get-subtree-options ()
  905. "Get export options in subtree at point.
  906. Assume point is at subtree's beginning.
  907. Return options as a plist."
  908. (let (prop plist)
  909. (when (setq prop (progn (looking-at org-todo-line-regexp)
  910. (or (save-match-data
  911. (org-entry-get (point) "EXPORT_TITLE"))
  912. (org-match-string-no-properties 3))))
  913. (setq plist
  914. (plist-put
  915. plist :title
  916. (org-element-parse-secondary-string
  917. prop
  918. (cdr (assq 'keyword org-element-string-restrictions))))))
  919. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  920. (setq plist (plist-put plist :text prop)))
  921. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  922. (setq plist (plist-put plist :author prop)))
  923. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  924. (setq plist (plist-put plist :date prop)))
  925. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  926. (setq plist (org-export-add-options-to-plist plist prop)))
  927. plist))
  928. (defun org-export-get-inbuffer-options (&optional backend files)
  929. "Return current buffer export options, as a plist.
  930. Optional argument BACKEND, when non-nil, is a symbol specifying
  931. which back-end specific options should also be read in the
  932. process.
  933. Optional argument FILES is a list of setup files names read so
  934. far, used to avoid circular dependencies.
  935. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  936. (org-with-wide-buffer
  937. (goto-char (point-min))
  938. (let ((case-fold-search t) plist)
  939. ;; 1. Special keywords, as in `org-export-special-keywords'.
  940. (let ((special-re (org-make-options-regexp org-export-special-keywords)))
  941. (while (re-search-forward special-re nil t)
  942. (let ((element (org-element-at-point)))
  943. (when (eq (org-element-type element) 'keyword)
  944. (let* ((key (org-element-property :key element))
  945. (val (org-element-property :value element))
  946. (prop
  947. (cond
  948. ((string= key "SETUP_FILE")
  949. (let ((file
  950. (expand-file-name
  951. (org-remove-double-quotes (org-trim val)))))
  952. ;; Avoid circular dependencies.
  953. (unless (member file files)
  954. (with-temp-buffer
  955. (insert (org-file-contents file 'noerror))
  956. (org-mode)
  957. (org-export-get-inbuffer-options
  958. backend (cons file files))))))
  959. ((string= key "OPTIONS")
  960. (org-export-parse-option-keyword val backend))
  961. ((string= key "MACRO")
  962. (when (string-match
  963. "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
  964. val)
  965. (let ((key
  966. (intern
  967. (concat ":macro-"
  968. (downcase (match-string 1 val)))))
  969. (value (org-match-string-no-properties 2 val)))
  970. (cond
  971. ((not value) nil)
  972. ;; Value will be evaled. Leave it as-is.
  973. ((string-match "\\`(eval\\>" value)
  974. (list key value))
  975. ;; Value has to be parsed for nested
  976. ;; macros.
  977. (t
  978. (list
  979. key
  980. (let ((restr (org-element-restriction 'macro)))
  981. (org-element-parse-secondary-string
  982. ;; If user explicitly asks for
  983. ;; a newline, be sure to preserve it
  984. ;; from further filling with
  985. ;; `hard-newline'. Also replace
  986. ;; "\\n" with "\n", "\\\n" with "\\n"
  987. ;; and so on...
  988. (replace-regexp-in-string
  989. "\\(\\\\\\\\\\)n" "\\\\"
  990. (replace-regexp-in-string
  991. "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
  992. hard-newline value nil nil 1)
  993. nil nil 1)
  994. restr)))))))))))
  995. (setq plist (org-combine-plists plist prop)))))))
  996. ;; 2. Standard options, as in `org-export-option-alist'.
  997. (let* ((all (append org-export-option-alist
  998. ;; Also look for back-end specific options
  999. ;; if BACKEND is defined.
  1000. (and backend
  1001. (let ((var
  1002. (intern
  1003. (format "org-%s-option-alist" backend))))
  1004. (and (boundp var) (eval var))))))
  1005. ;; Build alist between keyword name and property name.
  1006. (alist
  1007. (delq nil (mapcar
  1008. (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
  1009. all)))
  1010. ;; Build regexp matching all keywords associated to export
  1011. ;; options. Note: the search is case insensitive.
  1012. (opt-re (org-make-options-regexp
  1013. (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
  1014. (goto-char (point-min))
  1015. (while (re-search-forward opt-re nil t)
  1016. (let ((element (org-element-at-point)))
  1017. (when (eq (org-element-type element) 'keyword)
  1018. (let* ((key (org-element-property :key element))
  1019. (val (org-element-property :value element))
  1020. (prop (cdr (assoc key alist)))
  1021. (behaviour (nth 4 (assq prop all))))
  1022. (setq plist
  1023. (plist-put
  1024. plist prop
  1025. ;; Handle value depending on specified BEHAVIOUR.
  1026. (case behaviour
  1027. (space
  1028. (if (not (plist-get plist prop)) (org-trim val)
  1029. (concat (plist-get plist prop) " " (org-trim val))))
  1030. (newline
  1031. (org-trim
  1032. (concat (plist-get plist prop) "\n" (org-trim val))))
  1033. (split
  1034. `(,@(plist-get plist prop) ,@(org-split-string val)))
  1035. ('t val)
  1036. (otherwise (if (not (plist-member plist prop)) val
  1037. (plist-get plist prop))))))))))
  1038. ;; Parse keywords specified in `org-element-parsed-keywords'.
  1039. (mapc
  1040. (lambda (key)
  1041. (let* ((prop (cdr (assoc key alist)))
  1042. (value (and prop (plist-get plist prop))))
  1043. (when (stringp value)
  1044. (setq plist
  1045. (plist-put
  1046. plist prop
  1047. (org-element-parse-secondary-string
  1048. value (org-element-restriction 'keyword)))))))
  1049. org-element-parsed-keywords))
  1050. ;; 3. Return final value.
  1051. plist)))
  1052. (defun org-export-get-buffer-attributes ()
  1053. "Return properties related to buffer attributes, as a plist."
  1054. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1055. (list
  1056. ;; Store full path of input file name, or nil. For internal use.
  1057. :input-file visited-file
  1058. :title (or (and visited-file
  1059. (file-name-sans-extension
  1060. (file-name-nondirectory visited-file)))
  1061. (buffer-name (buffer-base-buffer)))
  1062. :macro-modification-time
  1063. (and visited-file
  1064. (file-exists-p visited-file)
  1065. (concat "(eval (format-time-string \"$1\" '"
  1066. (prin1-to-string (nth 5 (file-attributes visited-file)))
  1067. "))"))
  1068. ;; Store input file name as a macro.
  1069. :macro-input-file (and visited-file (file-name-nondirectory visited-file))
  1070. ;; `:macro-date', `:macro-time' and `:macro-property' could as
  1071. ;; well be initialized as tree properties, since they don't
  1072. ;; depend on buffer properties. Though, it may be more logical
  1073. ;; to keep them close to other ":macro-" properties.
  1074. :macro-date "(eval (format-time-string \"$1\"))"
  1075. :macro-time "(eval (format-time-string \"$1\"))"
  1076. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")))
  1077. (defun org-export-get-global-options (&optional backend)
  1078. "Return global export options as a plist.
  1079. Optional argument BACKEND, if non-nil, is a symbol specifying
  1080. which back-end specific export options should also be read in the
  1081. process."
  1082. (let ((all (append org-export-option-alist
  1083. (and backend
  1084. (let ((var (intern
  1085. (format "org-%s-option-alist" backend))))
  1086. (and (boundp var) (eval var))))))
  1087. ;; Output value.
  1088. plist)
  1089. (mapc (lambda (cell)
  1090. (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
  1091. all)
  1092. ;; Return value.
  1093. plist))
  1094. (defun org-export-store-footnote-definitions (info)
  1095. "Collect and store footnote definitions from current buffer in INFO.
  1096. INFO is a plist containing export options.
  1097. Footnotes definitions are stored as a alist whose CAR is
  1098. footnote's label, as a string, and CDR the contents, as a parse
  1099. tree. This alist will be consed to the value of
  1100. `:footnote-definition-alist' in INFO, if any.
  1101. The new plist is returned; use
  1102. \(setq info (org-export-store-footnote-definitions info))
  1103. to be sure to use the new value. INFO is modified by side
  1104. effects."
  1105. ;; Footnotes definitions must be collected in the original buffer,
  1106. ;; as there's no insurance that they will still be in the parse
  1107. ;; tree, due to some narrowing.
  1108. (plist-put
  1109. info :footnote-definition-alist
  1110. (let ((alist (plist-get info :footnote-definition-alist)))
  1111. (org-with-wide-buffer
  1112. (goto-char (point-min))
  1113. (while (re-search-forward org-footnote-definition-re nil t)
  1114. (let ((def (org-footnote-at-definition-p)))
  1115. (when def
  1116. (org-skip-whitespace)
  1117. (push (cons (car def)
  1118. (save-restriction
  1119. (narrow-to-region (point) (nth 2 def))
  1120. ;; Like `org-element-parse-buffer', but
  1121. ;; makes sure the definition doesn't start
  1122. ;; with a section element.
  1123. (nconc
  1124. (list 'org-data nil)
  1125. (org-element-parse-elements
  1126. (point-min) (point-max) nil nil nil nil nil))))
  1127. alist))))
  1128. alist))))
  1129. (defvar org-export-allow-BIND-local nil)
  1130. (defun org-export-confirm-letbind ()
  1131. "Can we use #+BIND values during export?
  1132. By default this will ask for confirmation by the user, to divert
  1133. possible security risks."
  1134. (cond
  1135. ((not org-export-allow-BIND) nil)
  1136. ((eq org-export-allow-BIND t) t)
  1137. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  1138. (t (org-set-local 'org-export-allow-BIND-local
  1139. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  1140. (defun org-export-install-letbind-maybe ()
  1141. "Install the values from #+BIND lines as local variables.
  1142. Variables must be installed before in-buffer options are
  1143. retrieved."
  1144. (let (letbind pair)
  1145. (org-with-wide-buffer
  1146. (goto-char (point-min))
  1147. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  1148. (when (org-export-confirm-letbind)
  1149. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  1150. letbind))))
  1151. (while (setq pair (pop letbind))
  1152. (org-set-local (car pair) (nth 1 pair)))))
  1153. ;;;; Tree Properties
  1154. ;;
  1155. ;; Tree properties are infromation extracted from parse tree. They
  1156. ;; are initialized at the beginning of the transcoding process by
  1157. ;; `org-export-collect-tree-properties'.
  1158. ;;
  1159. ;; Dedicated functions focus on computing the value of specific tree
  1160. ;; properties during initialization. Thus,
  1161. ;; `org-export-populate-ignore-list' lists elements and objects that
  1162. ;; should be skipped during export, `org-export-get-min-level' gets
  1163. ;; the minimal exportable level, used as a basis to compute relative
  1164. ;; level for headlines. Eventually
  1165. ;; `org-export-collect-headline-numbering' builds an alist between
  1166. ;; headlines and their numbering.
  1167. (defun org-export-collect-tree-properties (data info)
  1168. "Extract tree properties from parse tree.
  1169. DATA is the parse tree from which information is retrieved. INFO
  1170. is a list holding export options.
  1171. Following tree properties are set or updated:
  1172. `:footnote-definition-alist' List of footnotes definitions in
  1173. original buffer and current parse tree.
  1174. `:headline-offset' Offset between true level of headlines and
  1175. local level. An offset of -1 means an headline
  1176. of level 2 should be considered as a level
  1177. 1 headline in the context.
  1178. `:headline-numbering' Alist of all headlines as key an the
  1179. associated numbering as value.
  1180. `:ignore-list' List of elements that should be ignored during
  1181. export.
  1182. `:target-list' List of all targets in the parse tree."
  1183. ;; Install the parse tree in the communication channel, in order to
  1184. ;; use `org-export-get-genealogy' and al.
  1185. (setq info (plist-put info :parse-tree data))
  1186. ;; Get the list of elements and objects to ignore, and put it into
  1187. ;; `:ignore-list'. Do not overwrite any user ignore that might have
  1188. ;; been done during parse tree filtering.
  1189. (setq info
  1190. (plist-put info
  1191. :ignore-list
  1192. (append (org-export-populate-ignore-list data info)
  1193. (plist-get info :ignore-list))))
  1194. ;; Compute `:headline-offset' in order to be able to use
  1195. ;; `org-export-get-relative-level'.
  1196. (setq info
  1197. (plist-put info
  1198. :headline-offset (- 1 (org-export-get-min-level data info))))
  1199. ;; Update footnotes definitions list with definitions in parse tree.
  1200. ;; This is required since buffer expansion might have modified
  1201. ;; boundaries of footnote definitions contained in the parse tree.
  1202. ;; This way, definitions in `footnote-definition-alist' are bound to
  1203. ;; match those in the parse tree.
  1204. (let ((defs (plist-get info :footnote-definition-alist)))
  1205. (org-element-map
  1206. data 'footnote-definition
  1207. (lambda (fn)
  1208. (push (cons (org-element-property :label fn)
  1209. `(org-data nil ,@(org-element-contents fn)))
  1210. defs)))
  1211. (setq info (plist-put info :footnote-definition-alist defs)))
  1212. ;; Properties order doesn't matter: get the rest of the tree
  1213. ;; properties.
  1214. (nconc
  1215. `(:target-list
  1216. ,(org-element-map
  1217. data '(keyword target)
  1218. (lambda (blob)
  1219. (when (or (eq (org-element-type blob) 'target)
  1220. (string= (org-element-property :key blob) "TARGET"))
  1221. blob)) info)
  1222. :headline-numbering ,(org-export-collect-headline-numbering data info))
  1223. info))
  1224. (defun org-export-get-min-level (data options)
  1225. "Return minimum exportable headline's level in DATA.
  1226. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1227. OPTIONS is a plist holding export options."
  1228. (catch 'exit
  1229. (let ((min-level 10000))
  1230. (mapc
  1231. (lambda (blob)
  1232. (when (and (eq (org-element-type blob) 'headline)
  1233. (not (member blob (plist-get options :ignore-list))))
  1234. (setq min-level
  1235. (min (org-element-property :level blob) min-level)))
  1236. (when (= min-level 1) (throw 'exit 1)))
  1237. (org-element-contents data))
  1238. ;; If no headline was found, for the sake of consistency, set
  1239. ;; minimum level to 1 nonetheless.
  1240. (if (= min-level 10000) 1 min-level))))
  1241. (defun org-export-collect-headline-numbering (data options)
  1242. "Return numbering of all exportable headlines in a parse tree.
  1243. DATA is the parse tree. OPTIONS is the plist holding export
  1244. options.
  1245. Return an alist whose key is an headline and value is its
  1246. associated numbering \(in the shape of a list of numbers\)."
  1247. (let ((numbering (make-vector org-export-max-depth 0)))
  1248. (org-element-map
  1249. data
  1250. 'headline
  1251. (lambda (headline)
  1252. (let ((relative-level
  1253. (1- (org-export-get-relative-level headline options))))
  1254. (cons
  1255. headline
  1256. (loop for n across numbering
  1257. for idx from 0 to org-export-max-depth
  1258. when (< idx relative-level) collect n
  1259. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1260. when (> idx relative-level) do (aset numbering idx 0)))))
  1261. options)))
  1262. (defun org-export-populate-ignore-list (data options)
  1263. "Return list of elements and objects to ignore during export.
  1264. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1265. export options.
  1266. Return elements or objects to ignore as a list."
  1267. (let (ignore
  1268. (walk-data
  1269. (function
  1270. (lambda (data options selected)
  1271. ;; Collect ignored elements or objects into IGNORE-LIST.
  1272. (mapc
  1273. (lambda (el)
  1274. (if (org-export--skip-p el options selected) (push el ignore)
  1275. (let ((type (org-element-type el)))
  1276. (if (and (eq (plist-get info :with-archived-trees) 'headline)
  1277. (eq (org-element-type el) 'headline)
  1278. (org-element-property :archivedp el))
  1279. ;; If headline is archived but tree below has
  1280. ;; to be skipped, add it to ignore list.
  1281. (mapc (lambda (e) (push e ignore))
  1282. (org-element-contents el))
  1283. ;; Move into recursive objects/elements.
  1284. (when (org-element-contents el)
  1285. (funcall walk-data el options selected))))))
  1286. (org-element-contents data))))))
  1287. ;; Main call. First find trees containing a select tag, if any.
  1288. (funcall walk-data data options (org-export--selected-trees data options))
  1289. ;; Return value.
  1290. ignore))
  1291. (defun org-export--selected-trees (data info)
  1292. "Return list of headlines containing a select tag in their tree.
  1293. DATA is parsed data as returned by `org-element-parse-buffer'.
  1294. INFO is a plist holding export options."
  1295. (let (selected-trees
  1296. (walk-data
  1297. (function
  1298. (lambda (data genealogy)
  1299. (case (org-element-type data)
  1300. (org-data
  1301. (funcall walk-data (org-element-contents data) genealogy))
  1302. (headline
  1303. (let ((tags (org-element-property :tags headline)))
  1304. (if (and tags
  1305. (loop for tag in (plist-get info :select-tags)
  1306. thereis (string-match
  1307. (format ":%s:" tag) tags)))
  1308. ;; When a select tag is found, mark as acceptable
  1309. ;; full genealogy and every headline within the
  1310. ;; tree.
  1311. (setq selected-trees
  1312. (append
  1313. (cons data genealogy)
  1314. (org-element-map data 'headline 'identity)
  1315. selected-trees))
  1316. ;; Else, continue searching in tree, recursively.
  1317. (funcall walk-data data (cons data genealogy))))))))))
  1318. (funcall walk-data data nil) selected-trees))
  1319. (defun org-export--skip-p (blob options select-tags)
  1320. "Non-nil when element or object BLOB should be skipped during export.
  1321. OPTIONS is the plist holding export options. SELECT-TAGS, when
  1322. non-nil, is a list of tags marking a subtree as exportable."
  1323. (case (org-element-type blob)
  1324. ;; Check headline.
  1325. (headline
  1326. (let ((with-tasks (plist-get options :with-tasks))
  1327. (todo (org-element-property :todo-keyword blob))
  1328. (todo-type (org-element-property :todo-type blob))
  1329. (archived (plist-get options :with-archived-trees))
  1330. (tag-list (let ((tags (org-element-property :tags blob)))
  1331. (and tags (org-split-string tags ":")))))
  1332. (or
  1333. ;; Ignore subtrees with an exclude tag.
  1334. (loop for k in (plist-get options :exclude-tags)
  1335. thereis (member k tag-list))
  1336. ;; Ignore subtrees without a select tag, when such tag is
  1337. ;; found in the buffer.
  1338. (member blob select-tags)
  1339. ;; Ignore commented sub-trees.
  1340. (org-element-property :commentedp blob)
  1341. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1342. (and (not archived) (org-element-property :archivedp blob))
  1343. ;; Ignore tasks, if specified by `:with-tasks' property.
  1344. (and todo
  1345. (or (not with-tasks)
  1346. (and (memq with-tasks '(todo done))
  1347. (not (eq todo-type with-tasks)))
  1348. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1349. ;; Check timestamp.
  1350. (timestamp
  1351. (case (plist-get options :with-timestamps)
  1352. ;; No timestamp allowed.
  1353. ('nil t)
  1354. ;; Only active timestamps allowed and the current one isn't
  1355. ;; active.
  1356. (active
  1357. (not (memq (org-element-property :type blob)
  1358. '(active active-range))))
  1359. ;; Only inactive timestamps allowed and the current one isn't
  1360. ;; inactive.
  1361. (inactive
  1362. (not (memq (org-element-property :type blob)
  1363. '(inactive inactive-range))))))
  1364. ;; Check drawer.
  1365. (drawer
  1366. (or (not (plist-get options :with-drawers))
  1367. (and (consp (plist-get options :with-drawers))
  1368. (not (member (org-element-property :drawer-name blob)
  1369. (plist-get options :with-drawers))))))
  1370. ;; Check table-row.
  1371. (table-row (org-export-table-row-is-special-p blob options))
  1372. ;; Check table-cell.
  1373. (table-cell
  1374. (and (org-export-table-has-special-column-p
  1375. (nth 1 (org-export-get-genealogy blob options)))
  1376. (not (org-export-get-previous-element blob options))))
  1377. ;; Check clock.
  1378. (clock (not (plist-get options :with-clocks)))
  1379. ;; Check planning.
  1380. (planning (not (plist-get options :with-plannings)))))
  1381. ;;; The Transcoder
  1382. ;;
  1383. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1384. ;; `org-element-parse-buffer') and transcodes it into a specified
  1385. ;; back-end output. It takes care of updating local properties,
  1386. ;; filtering out elements or objects according to export options and
  1387. ;; organizing the output blank lines and white space are preserved.
  1388. ;;
  1389. ;; Though, this function is inapropriate for secondary strings, which
  1390. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1391. ;; `org-export-secondary-string' is provided for that specific task.
  1392. ;;
  1393. ;; Internally, three functions handle the filtering of objects and
  1394. ;; elements during the export. In particular,
  1395. ;; `org-export-ignore-element' marks an element or object so future
  1396. ;; parse tree traversals skip it, `org-export-interpret-p' tells which
  1397. ;; elements or objects should be seen as real Org syntax and
  1398. ;; `org-export-expand' transforms the others back into their original
  1399. ;; shape.
  1400. (defun org-export-data (data info)
  1401. "Convert DATA into current back-end format.
  1402. DATA is a nested list as returned by `org-element-parse-buffer'.
  1403. INFO is a plist holding export options and also used as
  1404. a communication channel between elements when walking the nested
  1405. list.
  1406. Return transcoded string."
  1407. (mapconcat
  1408. ;; BLOB can be an element, an object, a string, or nil.
  1409. (lambda (blob)
  1410. (cond
  1411. ((not blob) nil)
  1412. ;; BLOB is a string. Check if the optional transcoder for plain
  1413. ;; text exists, and call it in that case. Otherwise, simply
  1414. ;; return string. Also update INFO and call
  1415. ;; `org-export-filter-plain-text-functions'.
  1416. ((stringp blob)
  1417. (let ((transcoder (intern (format "org-%s-plain-text"
  1418. (plist-get info :back-end)))))
  1419. (org-export-filter-apply-functions
  1420. (plist-get info :filter-plain-text)
  1421. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1422. info)))
  1423. ;; BLOB is an element or an object.
  1424. (t
  1425. (let* ((type (org-element-type blob))
  1426. ;; 1. Determine the appropriate TRANSCODER.
  1427. (transcoder
  1428. (cond
  1429. ;; 1.0 A full Org document is inserted.
  1430. ((eq type 'org-data) 'identity)
  1431. ;; 1.1. BLOB should be ignored.
  1432. ((member blob (plist-get info :ignore-list)) nil)
  1433. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1434. ;; back into Org syntax.
  1435. ((not (org-export-interpret-p blob info)) 'org-export-expand)
  1436. ;; 1.3. Else apply naming convention.
  1437. (t (let ((trans (intern (format "org-%s-%s"
  1438. (plist-get info :back-end)
  1439. type))))
  1440. (and (fboundp trans) trans)))))
  1441. ;; 2. Compute CONTENTS of BLOB.
  1442. (contents
  1443. (cond
  1444. ;; Case 0. No transcoder or no contents: ignore BLOB.
  1445. ((or (not transcoder) (not (org-element-contents blob))) nil)
  1446. ;; Case 1. Transparently export an Org document.
  1447. ((eq type 'org-data) (org-export-data blob info))
  1448. ;; Case 2. For a greater element.
  1449. ((memq type org-element-greater-elements)
  1450. ;; Ignore contents of an archived tree
  1451. ;; when `:with-archived-trees' is `headline'.
  1452. (unless (and
  1453. (eq type 'headline)
  1454. (eq (plist-get info :with-archived-trees) 'headline)
  1455. (org-element-property :archivedp blob))
  1456. (org-element-normalize-string (org-export-data blob info))))
  1457. ;; Case 3. For an element containing objects.
  1458. (t
  1459. (org-export-data
  1460. (org-element-normalize-contents
  1461. blob
  1462. ;; When normalizing contents of the first paragraph
  1463. ;; in an item or a footnote definition, ignore
  1464. ;; first line's indentation: there is none and it
  1465. ;; might be misleading.
  1466. (and (eq type 'paragraph)
  1467. (not (org-export-get-previous-element blob info))
  1468. (let ((parent (org-export-get-parent blob info)))
  1469. (memq (org-element-type parent)
  1470. '(footnote-definition item)))))
  1471. info))))
  1472. ;; 3. Transcode BLOB into RESULTS string.
  1473. (results (cond
  1474. ((not transcoder) nil)
  1475. ((eq transcoder 'org-export-expand)
  1476. (org-export-data
  1477. `(org-data nil ,(funcall transcoder blob contents))
  1478. info))
  1479. (t (funcall transcoder blob contents info)))))
  1480. ;; 4. Return results.
  1481. (cond
  1482. ((not results) nil)
  1483. ;; No filter for a full document.
  1484. ((eq type 'org-data) results)
  1485. ;; Otherwise, update INFO, append the same white space
  1486. ;; between elements or objects as in the original buffer,
  1487. ;; and call appropriate filters.
  1488. (t
  1489. (let ((results
  1490. (org-export-filter-apply-functions
  1491. (plist-get info (intern (format ":filter-%s" type)))
  1492. (let ((post-blank (org-element-property :post-blank blob)))
  1493. (if (memq type org-element-all-elements)
  1494. (concat (org-element-normalize-string results)
  1495. (make-string post-blank ?\n))
  1496. (concat results (make-string post-blank ? ))))
  1497. info)))
  1498. ;; Eventually return string.
  1499. results)))))))
  1500. (org-element-contents data) ""))
  1501. (defun org-export-secondary-string (secondary info)
  1502. "Convert SECONDARY string into current back-end target format.
  1503. SECONDARY is a nested list as returned by
  1504. `org-element-parse-secondary-string'. INFO is a plist used as
  1505. a communication channel.
  1506. Return transcoded string."
  1507. ;; Make SECONDARY acceptable for `org-export-data'.
  1508. (let ((s (if (listp secondary) secondary (list secondary))))
  1509. (org-export-data `(org-data nil ,@s) (copy-sequence info))))
  1510. (defun org-export-interpret-p (blob info)
  1511. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1512. Check is done according to export options INFO, stored as
  1513. a plist."
  1514. (case (org-element-type blob)
  1515. ;; ... entities...
  1516. (entity (plist-get info :with-entities))
  1517. ;; ... emphasis...
  1518. (emphasis (plist-get info :with-emphasize))
  1519. ;; ... fixed-width areas.
  1520. (fixed-width (plist-get info :with-fixed-width))
  1521. ;; ... footnotes...
  1522. ((footnote-definition footnote-reference)
  1523. (plist-get info :with-footnotes))
  1524. ;; ... sub/superscripts...
  1525. ((subscript superscript)
  1526. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1527. (if (eq sub/super-p '{})
  1528. (org-element-property :use-brackets-p blob)
  1529. sub/super-p)))
  1530. ;; ... tables...
  1531. (table (plist-get info :with-tables))
  1532. (otherwise t)))
  1533. (defsubst org-export-expand (blob contents)
  1534. "Expand a parsed element or object to its original state.
  1535. BLOB is either an element or an object. CONTENTS is its
  1536. contents, as a string or nil."
  1537. (funcall (intern (format "org-element-%s-interpreter" (org-element-type blob)))
  1538. blob contents))
  1539. (defun org-export-ignore-element (element info)
  1540. "Add ELEMENT to `:ignore-list' in INFO.
  1541. Any element in `:ignore-list' will be skipped when using
  1542. `org-element-map'. INFO is modified by side effects."
  1543. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  1544. ;;; The Filter System
  1545. ;;
  1546. ;; Filters allow end-users to tweak easily the transcoded output.
  1547. ;; They are the functional counterpart of hooks, as every filter in
  1548. ;; a set is applied to the return value of the previous one.
  1549. ;;
  1550. ;; Every set is back-end agnostic. Although, a filter is always
  1551. ;; called, in addition to the string it applies to, with the back-end
  1552. ;; used as argument, so it's easy enough for the end-user to add
  1553. ;; back-end specific filters in the set. The communication channel,
  1554. ;; as a plist, is required as the third argument.
  1555. ;;
  1556. ;; Filters sets are defined below. There are of four types:
  1557. ;;
  1558. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1559. ;; complete parsed tree. It's the only filters set that doesn't
  1560. ;; apply to a string.
  1561. ;; - `org-export-filter-final-output-functions' applies to the final
  1562. ;; transcoded string.
  1563. ;; - `org-export-filter-plain-text-functions' applies to any string
  1564. ;; not recognized as Org syntax.
  1565. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1566. ;; after an element or object of type TYPE has been transcoded.
  1567. ;;
  1568. ;; All filters sets are applied through
  1569. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1570. ;; applied in a LIFO fashion. It allows developers to be sure that
  1571. ;; their filters will be applied first.
  1572. ;;
  1573. ;; Filters properties are installed in communication channel with
  1574. ;; `org-export-install-filters' function.
  1575. ;;
  1576. ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
  1577. ;; before parsing to allow for heavy structure modifications.
  1578. ;;;; Before Parsing Hook
  1579. (defvar org-export-before-parsing-hook nil
  1580. "Hook run before parsing an export buffer.
  1581. This is run after include keywords have been expanded and Babel
  1582. code executed, on a copy of original buffer's area being
  1583. exported. Visibility is the same as in the original one. Point
  1584. is left at the beginning of the new one.")
  1585. ;;;; Special Filters
  1586. (defvar org-export-filter-parse-tree-functions nil
  1587. "List of functions applied to the parsed tree.
  1588. Each filter is called with three arguments: the parse tree, as
  1589. returned by `org-element-parse-buffer', the back-end, as
  1590. a symbol, and the communication channel, as a plist. It must
  1591. return the modified parse tree to transcode.")
  1592. (defvar org-export-filter-final-output-functions nil
  1593. "List of functions applied to the transcoded string.
  1594. Each filter is called with three arguments: the full transcoded
  1595. string, the back-end, as a symbol, and the communication channel,
  1596. as a plist. It must return a string that will be used as the
  1597. final export output.")
  1598. (defvar org-export-filter-plain-text-functions nil
  1599. "List of functions applied to plain text.
  1600. Each filter is called with three arguments: a string which
  1601. contains no Org syntax, the back-end, as a symbol, and the
  1602. communication channel, as a plist. It must return a string or
  1603. nil.")
  1604. ;;;; Elements Filters
  1605. (defvar org-export-filter-center-block-functions nil
  1606. "List of functions applied to a transcoded center block.
  1607. Each filter is called with three arguments: the transcoded data,
  1608. as a string, the back-end, as a symbol, and the communication
  1609. channel, as a plist. It must return a string or nil.")
  1610. (defvar org-export-filter-clock-functions nil
  1611. "List of functions applied to a transcoded clock.
  1612. Each filter is called with three arguments: the transcoded data,
  1613. as a string, the back-end, as a symbol, and the communication
  1614. channel, as a plist. It must return a string or nil.")
  1615. (defvar org-export-filter-drawer-functions nil
  1616. "List of functions applied to a transcoded drawer.
  1617. Each filter is called with three arguments: the transcoded data,
  1618. as a string, the back-end, as a symbol, and the communication
  1619. channel, as a plist. It must return a string or nil.")
  1620. (defvar org-export-filter-dynamic-block-functions nil
  1621. "List of functions applied to a transcoded dynamic-block.
  1622. Each filter is called with three arguments: the transcoded data,
  1623. as a string, the back-end, as a symbol, and the communication
  1624. channel, as a plist. It must return a string or nil.")
  1625. (defvar org-export-filter-headline-functions nil
  1626. "List of functions applied to a transcoded headline.
  1627. Each filter is called with three arguments: the transcoded data,
  1628. as a string, the back-end, as a symbol, and the communication
  1629. channel, as a plist. It must return a string or nil.")
  1630. (defvar org-export-filter-inlinetask-functions nil
  1631. "List of functions applied to a transcoded inlinetask.
  1632. Each filter is called with three arguments: the transcoded data,
  1633. as a string, the back-end, as a symbol, and the communication
  1634. channel, as a plist. It must return a string or nil.")
  1635. (defvar org-export-filter-plain-list-functions nil
  1636. "List of functions applied to a transcoded plain-list.
  1637. Each filter is called with three arguments: the transcoded data,
  1638. as a string, the back-end, as a symbol, and the communication
  1639. channel, as a plist. It must return a string or nil.")
  1640. (defvar org-export-filter-item-functions nil
  1641. "List of functions applied to a transcoded item.
  1642. Each filter is called with three arguments: the transcoded data,
  1643. as a string, the back-end, as a symbol, and the communication
  1644. channel, as a plist. It must return a string or nil.")
  1645. (defvar org-export-filter-comment-functions nil
  1646. "List of functions applied to a transcoded comment.
  1647. Each filter is called with three arguments: the transcoded data,
  1648. as a string, the back-end, as a symbol, and the communication
  1649. channel, as a plist. It must return a string or nil.")
  1650. (defvar org-export-filter-comment-block-functions nil
  1651. "List of functions applied to a transcoded comment-comment.
  1652. Each filter is called with three arguments: the transcoded data,
  1653. as a string, the back-end, as a symbol, and the communication
  1654. channel, as a plist. It must return a string or nil.")
  1655. (defvar org-export-filter-example-block-functions nil
  1656. "List of functions applied to a transcoded example-block.
  1657. Each filter is called with three arguments: the transcoded data,
  1658. as a string, the back-end, as a symbol, and the communication
  1659. channel, as a plist. It must return a string or nil.")
  1660. (defvar org-export-filter-export-block-functions nil
  1661. "List of functions applied to a transcoded export-block.
  1662. Each filter is called with three arguments: the transcoded data,
  1663. as a string, the back-end, as a symbol, and the communication
  1664. channel, as a plist. It must return a string or nil.")
  1665. (defvar org-export-filter-fixed-width-functions nil
  1666. "List of functions applied to a transcoded fixed-width.
  1667. Each filter is called with three arguments: the transcoded data,
  1668. as a string, the back-end, as a symbol, and the communication
  1669. channel, as a plist. It must return a string or nil.")
  1670. (defvar org-export-filter-footnote-definition-functions nil
  1671. "List of functions applied to a transcoded footnote-definition.
  1672. Each filter is called with three arguments: the transcoded data,
  1673. as a string, the back-end, as a symbol, and the communication
  1674. channel, as a plist. It must return a string or nil.")
  1675. (defvar org-export-filter-horizontal-rule-functions nil
  1676. "List of functions applied to a transcoded horizontal-rule.
  1677. Each filter is called with three arguments: the transcoded data,
  1678. as a string, the back-end, as a symbol, and the communication
  1679. channel, as a plist. It must return a string or nil.")
  1680. (defvar org-export-filter-keyword-functions nil
  1681. "List of functions applied to a transcoded keyword.
  1682. Each filter is called with three arguments: the transcoded data,
  1683. as a string, the back-end, as a symbol, and the communication
  1684. channel, as a plist. It must return a string or nil.")
  1685. (defvar org-export-filter-latex-environment-functions nil
  1686. "List of functions applied to a transcoded latex-environment.
  1687. Each filter is called with three arguments: the transcoded data,
  1688. as a string, the back-end, as a symbol, and the communication
  1689. channel, as a plist. It must return a string or nil.")
  1690. (defvar org-export-filter-babel-call-functions nil
  1691. "List of functions applied to a transcoded babel-call.
  1692. Each filter is called with three arguments: the transcoded data,
  1693. as a string, the back-end, as a symbol, and the communication
  1694. channel, as a plist. It must return a string or nil.")
  1695. (defvar org-export-filter-paragraph-functions nil
  1696. "List of functions applied to a transcoded paragraph.
  1697. Each filter is called with three arguments: the transcoded data,
  1698. as a string, the back-end, as a symbol, and the communication
  1699. channel, as a plist. It must return a string or nil.")
  1700. (defvar org-export-filter-planning-functions nil
  1701. "List of functions applied to a transcoded planning.
  1702. Each filter is called with three arguments: the transcoded data,
  1703. as a string, the back-end, as a symbol, and the communication
  1704. channel, as a plist. It must return a string or nil.")
  1705. (defvar org-export-filter-property-drawer-functions nil
  1706. "List of functions applied to a transcoded property-drawer.
  1707. Each filter is called with three arguments: the transcoded data,
  1708. as a string, the back-end, as a symbol, and the communication
  1709. channel, as a plist. It must return a string or nil.")
  1710. (defvar org-export-filter-quote-block-functions nil
  1711. "List of functions applied to a transcoded quote block.
  1712. Each filter is called with three arguments: the transcoded quote
  1713. data, as a string, the back-end, as a symbol, and the
  1714. communication channel, as a plist. It must return a string or
  1715. nil.")
  1716. (defvar org-export-filter-quote-section-functions nil
  1717. "List of functions applied to a transcoded quote-section.
  1718. Each filter is called with three arguments: the transcoded data,
  1719. as a string, the back-end, as a symbol, and the communication
  1720. channel, as a plist. It must return a string or nil.")
  1721. (defvar org-export-filter-section-functions nil
  1722. "List of functions applied to a transcoded section.
  1723. Each filter is called with three arguments: the transcoded data,
  1724. as a string, the back-end, as a symbol, and the communication
  1725. channel, as a plist. It must return a string or nil.")
  1726. (defvar org-export-filter-special-block-functions nil
  1727. "List of functions applied to a transcoded special block.
  1728. Each filter is called with three arguments: the transcoded data,
  1729. as a string, the back-end, as a symbol, and the communication
  1730. channel, as a plist. It must return a string or nil.")
  1731. (defvar org-export-filter-src-block-functions nil
  1732. "List of functions applied to a transcoded src-block.
  1733. Each filter is called with three arguments: the transcoded data,
  1734. as a string, the back-end, as a symbol, and the communication
  1735. channel, as a plist. It must return a string or nil.")
  1736. (defvar org-export-filter-table-functions nil
  1737. "List of functions applied to a transcoded table.
  1738. Each filter is called with three arguments: the transcoded data,
  1739. as a string, the back-end, as a symbol, and the communication
  1740. channel, as a plist. It must return a string or nil.")
  1741. (defvar org-export-filter-table-cell-functions nil
  1742. "List of functions applied to a transcoded table-cell.
  1743. Each filter is called with three arguments: the transcoded data,
  1744. as a string, the back-end, as a symbol, and the communication
  1745. channel, as a plist. It must return a string or nil.")
  1746. (defvar org-export-filter-table-row-functions nil
  1747. "List of functions applied to a transcoded table-row.
  1748. Each filter is called with three arguments: the transcoded data,
  1749. as a string, the back-end, as a symbol, and the communication
  1750. channel, as a plist. It must return a string or nil.")
  1751. (defvar org-export-filter-verse-block-functions nil
  1752. "List of functions applied to a transcoded verse block.
  1753. Each filter is called with three arguments: the transcoded data,
  1754. as a string, the back-end, as a symbol, and the communication
  1755. channel, as a plist. It must return a string or nil.")
  1756. ;;;; Objects Filters
  1757. (defvar org-export-filter-bold-functions nil
  1758. "List of functions applied to transcoded bold text.
  1759. Each filter is called with three arguments: the transcoded data,
  1760. as a string, the back-end, as a symbol, and the communication
  1761. channel, as a plist. It must return a string or nil.")
  1762. (defvar org-export-filter-code-functions nil
  1763. "List of functions applied to transcoded code text.
  1764. Each filter is called with three arguments: the transcoded data,
  1765. as a string, the back-end, as a symbol, and the communication
  1766. channel, as a plist. It must return a string or nil.")
  1767. (defvar org-export-filter-entity-functions nil
  1768. "List of functions applied to a transcoded entity.
  1769. Each filter is called with three arguments: the transcoded data,
  1770. as a string, the back-end, as a symbol, and the communication
  1771. channel, as a plist. It must return a string or nil.")
  1772. (defvar org-export-filter-export-snippet-functions nil
  1773. "List of functions applied to a transcoded export-snippet.
  1774. Each filter is called with three arguments: the transcoded data,
  1775. as a string, the back-end, as a symbol, and the communication
  1776. channel, as a plist. It must return a string or nil.")
  1777. (defvar org-export-filter-footnote-reference-functions nil
  1778. "List of functions applied to a transcoded footnote-reference.
  1779. Each filter is called with three arguments: the transcoded data,
  1780. as a string, the back-end, as a symbol, and the communication
  1781. channel, as a plist. It must return a string or nil.")
  1782. (defvar org-export-filter-inline-babel-call-functions nil
  1783. "List of functions applied to a transcoded inline-babel-call.
  1784. Each filter is called with three arguments: the transcoded data,
  1785. as a string, the back-end, as a symbol, and the communication
  1786. channel, as a plist. It must return a string or nil.")
  1787. (defvar org-export-filter-inline-src-block-functions nil
  1788. "List of functions applied to a transcoded inline-src-block.
  1789. Each filter is called with three arguments: the transcoded data,
  1790. as a string, the back-end, as a symbol, and the communication
  1791. channel, as a plist. It must return a string or nil.")
  1792. (defvar org-export-filter-italic-functions nil
  1793. "List of functions applied to transcoded italic text.
  1794. Each filter is called with three arguments: the transcoded data,
  1795. as a string, the back-end, as a symbol, and the communication
  1796. channel, as a plist. It must return a string or nil.")
  1797. (defvar org-export-filter-latex-fragment-functions nil
  1798. "List of functions applied to a transcoded latex-fragment.
  1799. Each filter is called with three arguments: the transcoded data,
  1800. as a string, the back-end, as a symbol, and the communication
  1801. channel, as a plist. It must return a string or nil.")
  1802. (defvar org-export-filter-line-break-functions nil
  1803. "List of functions applied to a transcoded line-break.
  1804. Each filter is called with three arguments: the transcoded data,
  1805. as a string, the back-end, as a symbol, and the communication
  1806. channel, as a plist. It must return a string or nil.")
  1807. (defvar org-export-filter-link-functions nil
  1808. "List of functions applied to a transcoded link.
  1809. Each filter is called with three arguments: the transcoded data,
  1810. as a string, the back-end, as a symbol, and the communication
  1811. channel, as a plist. It must return a string or nil.")
  1812. (defvar org-export-filter-macro-functions nil
  1813. "List of functions applied to a transcoded macro.
  1814. Each filter is called with three arguments: the transcoded data,
  1815. as a string, the back-end, as a symbol, and the communication
  1816. channel, as a plist. It must return a string or nil.")
  1817. (defvar org-export-filter-radio-target-functions nil
  1818. "List of functions applied to a transcoded radio-target.
  1819. Each filter is called with three arguments: the transcoded data,
  1820. as a string, the back-end, as a symbol, and the communication
  1821. channel, as a plist. It must return a string or nil.")
  1822. (defvar org-export-filter-statistics-cookie-functions nil
  1823. "List of functions applied to a transcoded statistics-cookie.
  1824. Each filter is called with three arguments: the transcoded data,
  1825. as a string, the back-end, as a symbol, and the communication
  1826. channel, as a plist. It must return a string or nil.")
  1827. (defvar org-export-filter-strike-through-functions nil
  1828. "List of functions applied to transcoded strike-through text.
  1829. Each filter is called with three arguments: the transcoded data,
  1830. as a string, the back-end, as a symbol, and the communication
  1831. channel, as a plist. It must return a string or nil.")
  1832. (defvar org-export-filter-subscript-functions nil
  1833. "List of functions applied to a transcoded subscript.
  1834. Each filter is called with three arguments: the transcoded data,
  1835. as a string, the back-end, as a symbol, and the communication
  1836. channel, as a plist. It must return a string or nil.")
  1837. (defvar org-export-filter-superscript-functions nil
  1838. "List of functions applied to a transcoded superscript.
  1839. Each filter is called with three arguments: the transcoded data,
  1840. as a string, the back-end, as a symbol, and the communication
  1841. channel, as a plist. It must return a string or nil.")
  1842. (defvar org-export-filter-target-functions nil
  1843. "List of functions applied to a transcoded target.
  1844. Each filter is called with three arguments: the transcoded data,
  1845. as a string, the back-end, as a symbol, and the communication
  1846. channel, as a plist. It must return a string or nil.")
  1847. (defvar org-export-filter-timestamp-functions nil
  1848. "List of functions applied to a transcoded timestamp.
  1849. Each filter is called with three arguments: the transcoded data,
  1850. as a string, the back-end, as a symbol, and the communication
  1851. channel, as a plist. It must return a string or nil.")
  1852. (defvar org-export-filter-underline-functions nil
  1853. "List of functions applied to transcoded underline text.
  1854. Each filter is called with three arguments: the transcoded data,
  1855. as a string, the back-end, as a symbol, and the communication
  1856. channel, as a plist. It must return a string or nil.")
  1857. (defvar org-export-filter-verbatim-functions nil
  1858. "List of functions applied to transcoded verbatim text.
  1859. Each filter is called with three arguments: the transcoded data,
  1860. as a string, the back-end, as a symbol, and the communication
  1861. channel, as a plist. It must return a string or nil.")
  1862. (defun org-export-filter-apply-functions (filters value info)
  1863. "Call every function in FILTERS.
  1864. Functions are called with arguments VALUE, current export
  1865. back-end and INFO. Call is done in a LIFO fashion, to be sure
  1866. that developer specified filters, if any, are called first."
  1867. (loop for filter in filters
  1868. if (not value) return nil else
  1869. do (setq value (funcall filter value (plist-get info :back-end) info)))
  1870. value)
  1871. (defun org-export-install-filters (info)
  1872. "Install filters properties in communication channel.
  1873. INFO is a plist containing the current communication channel.
  1874. Return the updated communication channel."
  1875. (let (plist)
  1876. ;; Install user defined filters with `org-export-filters-alist'.
  1877. (mapc (lambda (p)
  1878. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  1879. org-export-filters-alist)
  1880. ;; Prepend back-end specific filters to that list.
  1881. (let ((back-end-filters (intern (format "org-%s-filters-alist"
  1882. (plist-get info :back-end)))))
  1883. (when (boundp back-end-filters)
  1884. (mapc (lambda (p)
  1885. ;; Single values get consed, lists are prepended.
  1886. (let ((key (car p)) (value (cdr p)))
  1887. (when value
  1888. (setq plist
  1889. (plist-put
  1890. plist key
  1891. (if (atom value) (cons value (plist-get plist key))
  1892. (append value (plist-get plist key))))))))
  1893. (eval back-end-filters))))
  1894. ;; Return new communication channel.
  1895. (org-combine-plists info plist)))
  1896. ;;; Core functions
  1897. ;;
  1898. ;; This is the room for the main function, `org-export-as', along with
  1899. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1900. ;; They differ only by the way they output the resulting code.
  1901. ;;
  1902. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1903. ;; used with `org-export-to-file'. With a given extension, it tries
  1904. ;; to provide a canonical file name to write export output to.
  1905. ;;
  1906. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1907. ;; but a copy of it (with the same buffer-local variables and
  1908. ;; visibility), where include keywords are expanded and Babel blocks
  1909. ;; are executed, if appropriate.
  1910. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1911. ;;
  1912. ;; File inclusion is taken care of by
  1913. ;; `org-export-expand-include-keyword' and
  1914. ;; `org-export-prepare-file-contents'. Structure wise, including
  1915. ;; a whole Org file in a buffer often makes little sense. For
  1916. ;; example, if the file contains an headline and the include keyword
  1917. ;; was within an item, the item should contain the headline. That's
  1918. ;; why file inclusion should be done before any structure can be
  1919. ;; associated to the file, that is before parsing.
  1920. (defun org-export-as
  1921. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  1922. "Transcode current Org buffer into BACKEND code.
  1923. If narrowing is active in the current buffer, only transcode its
  1924. narrowed part.
  1925. If a region is active, transcode that region.
  1926. When optional argument SUBTREEP is non-nil, transcode the
  1927. sub-tree at point, extracting information from the headline
  1928. properties first.
  1929. When optional argument VISIBLE-ONLY is non-nil, don't export
  1930. contents of hidden elements.
  1931. When optional argument BODY-ONLY is non-nil, only return body
  1932. code, without preamble nor postamble.
  1933. Optional argument EXT-PLIST, when provided, is a property list
  1934. with external parameters overriding Org default settings, but
  1935. still inferior to file-local settings.
  1936. Optional argument NOEXPAND, when non-nil, prevents included files
  1937. to be expanded and Babel code to be executed.
  1938. Return code as a string."
  1939. (save-excursion
  1940. (save-restriction
  1941. ;; Narrow buffer to an appropriate region or subtree for
  1942. ;; parsing. If parsing subtree, be sure to remove main headline
  1943. ;; too.
  1944. (cond ((org-region-active-p)
  1945. (narrow-to-region (region-beginning) (region-end)))
  1946. (subtreep
  1947. (org-narrow-to-subtree)
  1948. (goto-char (point-min))
  1949. (forward-line)
  1950. (narrow-to-region (point) (point-max))))
  1951. ;; 1. Get export environment from original buffer. Store
  1952. ;; original footnotes definitions in communication channel as
  1953. ;; they might not be accessible anymore in a narrowed parse
  1954. ;; tree. Also install user's and developer's filters.
  1955. (let ((info (org-export-install-filters
  1956. (org-export-store-footnote-definitions
  1957. (org-export-get-environment backend subtreep ext-plist))))
  1958. ;; 2. Get parse tree. Buffer isn't parsed directly.
  1959. ;; Instead, a temporary copy is created, where include
  1960. ;; keywords are expanded and code blocks are evaluated.
  1961. (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
  1962. (current-buffer))))
  1963. (org-export-with-current-buffer-copy
  1964. (unless noexpand
  1965. (org-export-expand-include-keyword)
  1966. ;; Setting `org-current-export-file' is
  1967. ;; required by Org Babel to properly resolve
  1968. ;; noweb references.
  1969. (let ((org-current-export-file buf))
  1970. (org-export-blocks-preprocess)))
  1971. (goto-char (point-min))
  1972. (run-hooks 'org-export-before-parsing-hook)
  1973. (org-element-parse-buffer nil visible-only)))))
  1974. ;; 3. Call parse-tree filters to get the final tree.
  1975. (setq tree
  1976. (org-export-filter-apply-functions
  1977. (plist-get info :filter-parse-tree) tree info))
  1978. ;; 4. Now tree is complete, compute its properties and add
  1979. ;; them to communication channel.
  1980. (setq info
  1981. (org-combine-plists
  1982. info (org-export-collect-tree-properties tree info)))
  1983. ;; 5. Eventually transcode TREE. Wrap the resulting string
  1984. ;; into a template, if required. Eventually call
  1985. ;; final-output filter.
  1986. (let* ((body (org-element-normalize-string (org-export-data tree info)))
  1987. (template (intern (format "org-%s-template" backend)))
  1988. (output (org-export-filter-apply-functions
  1989. (plist-get info :filter-final-output)
  1990. (if (or (not (fboundp template)) body-only) body
  1991. (funcall template body info))
  1992. info)))
  1993. ;; Maybe add final OUTPUT to kill ring, then return it.
  1994. (when org-export-copy-to-kill-ring (org-kill-new output))
  1995. output)))))
  1996. (defun org-export-to-buffer
  1997. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  1998. "Call `org-export-as' with output to a specified buffer.
  1999. BACKEND is the back-end used for transcoding, as a symbol.
  2000. BUFFER is the output buffer. If it already exists, it will be
  2001. erased first, otherwise, it will be created.
  2002. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  2003. and NOEXPAND are similar to those used in `org-export-as', which
  2004. see.
  2005. Return buffer."
  2006. (let ((out (org-export-as
  2007. backend subtreep visible-only body-only ext-plist noexpand))
  2008. (buffer (get-buffer-create buffer)))
  2009. (with-current-buffer buffer
  2010. (erase-buffer)
  2011. (insert out)
  2012. (goto-char (point-min)))
  2013. buffer))
  2014. (defun org-export-to-file
  2015. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  2016. "Call `org-export-as' with output to a specified file.
  2017. BACKEND is the back-end used for transcoding, as a symbol. FILE
  2018. is the name of the output file, as a string.
  2019. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  2020. and NOEXPAND are similar to those used in `org-export-as', which
  2021. see.
  2022. Return output file's name."
  2023. ;; Checks for FILE permissions. `write-file' would do the same, but
  2024. ;; we'd rather avoid needless transcoding of parse tree.
  2025. (unless (file-writable-p file) (error "Output file not writable"))
  2026. ;; Insert contents to a temporary buffer and write it to FILE.
  2027. (let ((out (org-export-as
  2028. backend subtreep visible-only body-only ext-plist noexpand)))
  2029. (with-temp-buffer
  2030. (insert out)
  2031. (let ((coding-system-for-write org-export-coding-system))
  2032. (write-file file))))
  2033. ;; Return full path.
  2034. file)
  2035. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  2036. "Return output file's name according to buffer specifications.
  2037. EXTENSION is a string representing the output file extension,
  2038. with the leading dot.
  2039. With a non-nil optional argument SUBTREEP, try to determine
  2040. output file's name by looking for \"EXPORT_FILE_NAME\" property
  2041. of subtree at point.
  2042. When optional argument PUB-DIR is set, use it as the publishing
  2043. directory.
  2044. Return file name as a string, or nil if it couldn't be
  2045. determined."
  2046. (let ((base-name
  2047. ;; File name may come from EXPORT_FILE_NAME subtree property,
  2048. ;; assuming point is at beginning of said sub-tree.
  2049. (file-name-sans-extension
  2050. (or (and subtreep
  2051. (org-entry-get
  2052. (save-excursion
  2053. (ignore-errors
  2054. (org-back-to-heading (not visible-only)) (point)))
  2055. "EXPORT_FILE_NAME" t))
  2056. ;; File name may be extracted from buffer's associated
  2057. ;; file, if any.
  2058. (buffer-file-name (buffer-base-buffer))
  2059. ;; Can't determine file name on our own: Ask user.
  2060. (let ((read-file-name-function
  2061. (and org-completion-use-ido 'ido-read-file-name)))
  2062. (read-file-name
  2063. "Output file: " pub-dir nil nil nil
  2064. (lambda (name)
  2065. (string= (file-name-extension name t) extension))))))))
  2066. ;; Build file name. Enforce EXTENSION over whatever user may have
  2067. ;; come up with. PUB-DIR, if defined, always has precedence over
  2068. ;; any provided path.
  2069. (cond
  2070. (pub-dir
  2071. (concat (file-name-as-directory pub-dir)
  2072. (file-name-nondirectory base-name)
  2073. extension))
  2074. ((string= (file-name-nondirectory base-name) base-name)
  2075. (concat (file-name-as-directory ".") base-name extension))
  2076. (t (concat base-name extension)))))
  2077. (defmacro org-export-with-current-buffer-copy (&rest body)
  2078. "Apply BODY in a copy of the current buffer.
  2079. The copy preserves local variables and visibility of the original
  2080. buffer.
  2081. Point is at buffer's beginning when BODY is applied."
  2082. (org-with-gensyms (original-buffer offset buffer-string overlays)
  2083. `(let ((,original-buffer ,(current-buffer))
  2084. (,offset ,(1- (point-min)))
  2085. (,buffer-string ,(buffer-string))
  2086. (,overlays (mapcar
  2087. 'copy-overlay (overlays-in (point-min) (point-max)))))
  2088. (with-temp-buffer
  2089. (let ((buffer-invisibility-spec nil))
  2090. (org-clone-local-variables
  2091. ,original-buffer
  2092. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  2093. (insert ,buffer-string)
  2094. (mapc (lambda (ov)
  2095. (move-overlay
  2096. ov
  2097. (- (overlay-start ov) ,offset)
  2098. (- (overlay-end ov) ,offset)
  2099. (current-buffer)))
  2100. ,overlays)
  2101. (goto-char (point-min))
  2102. (progn ,@body))))))
  2103. (def-edebug-spec org-export-with-current-buffer-copy (body))
  2104. (defun org-export-expand-include-keyword (&optional included dir)
  2105. "Expand every include keyword in buffer.
  2106. Optional argument INCLUDED is a list of included file names along
  2107. with their line restriction, when appropriate. It is used to
  2108. avoid infinite recursion. Optional argument DIR is the current
  2109. working directory. It is used to properly resolve relative
  2110. paths."
  2111. (let ((case-fold-search t))
  2112. (goto-char (point-min))
  2113. (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
  2114. (when (eq (org-element-type (save-match-data (org-element-at-point)))
  2115. 'keyword)
  2116. (beginning-of-line)
  2117. ;; Extract arguments from keyword's value.
  2118. (let* ((value (match-string 1))
  2119. (ind (org-get-indentation))
  2120. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  2121. (prog1 (expand-file-name (match-string 1 value) dir)
  2122. (setq value (replace-match "" nil nil value)))))
  2123. (lines
  2124. (and (string-match
  2125. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  2126. (prog1 (match-string 1 value)
  2127. (setq value (replace-match "" nil nil value)))))
  2128. (env (cond ((string-match "\\<example\\>" value) 'example)
  2129. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2130. (match-string 1 value))))
  2131. ;; Minimal level of included file defaults to the child
  2132. ;; level of the current headline, if any, or one. It
  2133. ;; only applies is the file is meant to be included as
  2134. ;; an Org one.
  2135. (minlevel
  2136. (and (not env)
  2137. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2138. (prog1 (string-to-number (match-string 1 value))
  2139. (setq value (replace-match "" nil nil value)))
  2140. (let ((cur (org-current-level)))
  2141. (if cur (1+ (org-reduced-level cur)) 1))))))
  2142. ;; Remove keyword.
  2143. (delete-region (point) (progn (forward-line) (point)))
  2144. (cond
  2145. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  2146. ;; Check if files has already been parsed. Look after
  2147. ;; inclusion lines too, as different parts of the same file
  2148. ;; can be included too.
  2149. ((member (list file lines) included)
  2150. (error "Recursive file inclusion: %s" file))
  2151. (t
  2152. (cond
  2153. ((eq env 'example)
  2154. (insert
  2155. (let ((ind-str (make-string ind ? ))
  2156. (contents
  2157. ;; Protect sensitive contents with commas.
  2158. (replace-regexp-in-string
  2159. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  2160. (org-export-prepare-file-contents file lines)
  2161. nil nil 1)))
  2162. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2163. ind-str contents ind-str))))
  2164. ((stringp env)
  2165. (insert
  2166. (let ((ind-str (make-string ind ? ))
  2167. (contents
  2168. ;; Protect sensitive contents with commas.
  2169. (replace-regexp-in-string
  2170. (if (string= env "org") "\\(^\\)\\(.\\)"
  2171. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  2172. (org-export-prepare-file-contents file lines)
  2173. nil nil 1)))
  2174. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2175. ind-str env contents ind-str))))
  2176. (t
  2177. (insert
  2178. (with-temp-buffer
  2179. (org-mode)
  2180. (insert
  2181. (org-export-prepare-file-contents file lines ind minlevel))
  2182. (org-export-expand-include-keyword
  2183. (cons (list file lines) included)
  2184. (file-name-directory file))
  2185. (buffer-string))))))))))))
  2186. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  2187. "Prepare the contents of FILE for inclusion and return them as a string.
  2188. When optional argument LINES is a string specifying a range of
  2189. lines, include only those lines.
  2190. Optional argument IND, when non-nil, is an integer specifying the
  2191. global indentation of returned contents. Since its purpose is to
  2192. allow an included file to stay in the same environment it was
  2193. created \(i.e. a list item), it doesn't apply past the first
  2194. headline encountered.
  2195. Optional argument MINLEVEL, when non-nil, is an integer
  2196. specifying the level that any top-level headline in the included
  2197. file should have."
  2198. (with-temp-buffer
  2199. (insert-file-contents file)
  2200. (when lines
  2201. (let* ((lines (split-string lines "-"))
  2202. (lbeg (string-to-number (car lines)))
  2203. (lend (string-to-number (cadr lines)))
  2204. (beg (if (zerop lbeg) (point-min)
  2205. (goto-char (point-min))
  2206. (forward-line (1- lbeg))
  2207. (point)))
  2208. (end (if (zerop lend) (point-max)
  2209. (goto-char (point-min))
  2210. (forward-line (1- lend))
  2211. (point))))
  2212. (narrow-to-region beg end)))
  2213. ;; Remove blank lines at beginning and end of contents. The logic
  2214. ;; behind that removal is that blank lines around include keyword
  2215. ;; override blank lines in included file.
  2216. (goto-char (point-min))
  2217. (org-skip-whitespace)
  2218. (beginning-of-line)
  2219. (delete-region (point-min) (point))
  2220. (goto-char (point-max))
  2221. (skip-chars-backward " \r\t\n")
  2222. (forward-line)
  2223. (delete-region (point) (point-max))
  2224. ;; If IND is set, preserve indentation of include keyword until
  2225. ;; the first headline encountered.
  2226. (when ind
  2227. (unless (eq major-mode 'org-mode) (org-mode))
  2228. (goto-char (point-min))
  2229. (let ((ind-str (make-string ind ? )))
  2230. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2231. ;; Do not move footnote definitions out of column 0.
  2232. (unless (and (looking-at org-footnote-definition-re)
  2233. (eq (org-element-type (org-element-at-point))
  2234. 'footnote-definition))
  2235. (insert ind-str))
  2236. (forward-line))))
  2237. ;; When MINLEVEL is specified, compute minimal level for headlines
  2238. ;; in the file (CUR-MIN), and remove stars to each headline so
  2239. ;; that headlines with minimal level have a level of MINLEVEL.
  2240. (when minlevel
  2241. (unless (eq major-mode 'org-mode) (org-mode))
  2242. (let ((levels (org-map-entries
  2243. (lambda () (org-reduced-level (org-current-level))))))
  2244. (when levels
  2245. (let ((offset (- minlevel (apply 'min levels))))
  2246. (unless (zerop offset)
  2247. (when org-odd-levels-only (setq offset (* offset 2)))
  2248. ;; Only change stars, don't bother moving whole
  2249. ;; sections.
  2250. (org-map-entries
  2251. (lambda () (if (< offset 0) (delete-char (abs offset))
  2252. (insert (make-string offset ?*))))))))))
  2253. (buffer-string)))
  2254. ;;; Tools For Back-Ends
  2255. ;;
  2256. ;; A whole set of tools is available to help build new exporters. Any
  2257. ;; function general enough to have its use across many back-ends
  2258. ;; should be added here.
  2259. ;;
  2260. ;; As of now, functions operating on footnotes, headlines, links,
  2261. ;; macros, references, src-blocks, tables and tables of contents are
  2262. ;; implemented.
  2263. ;;;; For Export Snippets
  2264. ;;
  2265. ;; Every export snippet is transmitted to the back-end. Though, the
  2266. ;; latter will only retain one type of export-snippet, ignoring
  2267. ;; others, based on the former's target back-end. The function
  2268. ;; `org-export-snippet-backend' returns that back-end for a given
  2269. ;; export-snippet.
  2270. (defun org-export-snippet-backend (export-snippet)
  2271. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  2272. Translation, with `org-export-snippet-translation-alist', is
  2273. applied."
  2274. (let ((back-end (org-element-property :back-end export-snippet)))
  2275. (intern
  2276. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  2277. back-end))))
  2278. ;;;; For Footnotes
  2279. ;;
  2280. ;; `org-export-collect-footnote-definitions' is a tool to list
  2281. ;; actually used footnotes definitions in the whole parse tree, or in
  2282. ;; an headline, in order to add footnote listings throughout the
  2283. ;; transcoded data.
  2284. ;;
  2285. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  2286. ;; back-ends, when they need to attach the footnote definition only to
  2287. ;; the first occurrence of the corresponding label.
  2288. ;;
  2289. ;; `org-export-get-footnote-definition' and
  2290. ;; `org-export-get-footnote-number' provide easier access to
  2291. ;; additional information relative to a footnote reference.
  2292. (defun org-export-collect-footnote-definitions (data info)
  2293. "Return an alist between footnote numbers, labels and definitions.
  2294. DATA is the parse tree from which definitions are collected.
  2295. INFO is the plist used as a communication channel.
  2296. Definitions are sorted by order of references. They either
  2297. appear as Org data (transcoded with `org-export-data') or as
  2298. a secondary string for inlined footnotes (transcoded with
  2299. `org-export-secondary-string'). Unreferenced definitions are
  2300. ignored."
  2301. (let (num-alist
  2302. (collect-fn
  2303. (function
  2304. (lambda (data)
  2305. ;; Collect footnote number, label and definition in DATA.
  2306. (org-element-map
  2307. data 'footnote-reference
  2308. (lambda (fn)
  2309. (when (org-export-footnote-first-reference-p fn info)
  2310. (let ((def (org-export-get-footnote-definition fn info)))
  2311. (push
  2312. (list (org-export-get-footnote-number fn info)
  2313. (org-element-property :label fn)
  2314. def)
  2315. num-alist)
  2316. ;; Also search in definition for nested footnotes.
  2317. (when (eq (org-element-property :type fn) 'standard)
  2318. (funcall collect-fn def)))))
  2319. ;; Don't enter footnote definitions since it will happen
  2320. ;; when their first reference is found.
  2321. info nil 'footnote-definition)))))
  2322. (funcall collect-fn (plist-get info :parse-tree))
  2323. (reverse num-alist)))
  2324. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2325. "Non-nil when a footnote reference is the first one for its label.
  2326. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2327. INFO is the plist used as a communication channel."
  2328. (let ((label (org-element-property :label footnote-reference)))
  2329. ;; Anonymous footnotes are always a first reference.
  2330. (if (not label) t
  2331. ;; Otherwise, return the first footnote with the same LABEL and
  2332. ;; test if it is equal to FOOTNOTE-REFERENCE.
  2333. (let ((search-refs
  2334. (function
  2335. (lambda (data)
  2336. (org-element-map
  2337. data 'footnote-reference
  2338. (lambda (fn)
  2339. (cond
  2340. ((string= (org-element-property :label fn) label)
  2341. (throw 'exit fn))
  2342. ;; If FN isn't inlined, be sure to traverse its
  2343. ;; definition before resuming search. See
  2344. ;; comments in `org-export-get-footnote-number'
  2345. ;; for more information.
  2346. ((eq (org-element-property :type fn) 'standard)
  2347. (funcall search-refs
  2348. (org-export-get-footnote-definition fn info)))))
  2349. ;; Don't enter footnote definitions since it will
  2350. ;; happen when their first reference is found.
  2351. info 'first-match 'footnote-definition)))))
  2352. (equal (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  2353. footnote-reference)))))
  2354. (defun org-export-get-footnote-definition (footnote-reference info)
  2355. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2356. INFO is the plist used as a communication channel."
  2357. (let ((label (org-element-property :label footnote-reference)))
  2358. (or (org-element-property :inline-definition footnote-reference)
  2359. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2360. (defun org-export-get-footnote-number (footnote info)
  2361. "Return number associated to a footnote.
  2362. FOOTNOTE is either a footnote reference or a footnote definition.
  2363. INFO is the plist used as a communication channel."
  2364. (let ((label (org-element-property :label footnote))
  2365. seen-refs
  2366. (search-ref
  2367. (function
  2368. (lambda (data)
  2369. ;; Search footnote references through DATA, filling
  2370. ;; SEEN-REFS along the way.
  2371. (org-element-map
  2372. data 'footnote-reference
  2373. (lambda (fn)
  2374. (let ((fn-lbl (org-element-property :label fn)))
  2375. (cond
  2376. ;; Anonymous footnote match: return number.
  2377. ((and (not fn-lbl) (equal fn footnote))
  2378. (throw 'exit (1+ (length seen-refs))))
  2379. ;; Labels match: return number.
  2380. ((and label (string= label fn-lbl))
  2381. (throw 'exit (1+ (length seen-refs))))
  2382. ;; Anonymous footnote: it's always a new one. Also,
  2383. ;; be sure to return nil from the `cond' so
  2384. ;; `first-match' doesn't get us out of the loop.
  2385. ((not fn-lbl) (push 'inline seen-refs) nil)
  2386. ;; Label not seen so far: add it so SEEN-REFS.
  2387. ;;
  2388. ;; Also search for subsequent references in footnote
  2389. ;; definition so numbering following reading logic.
  2390. ;; Note that we don't have to care about inline
  2391. ;; definitions, since `org-element-map' already
  2392. ;; traverse them at the right time.
  2393. ;;
  2394. ;; Once again, return nil to stay in the loop.
  2395. ((not (member fn-lbl seen-refs))
  2396. (push fn-lbl seen-refs)
  2397. (funcall search-ref
  2398. (org-export-get-footnote-definition fn info))
  2399. nil))))
  2400. ;; Don't enter footnote definitions since it will happen
  2401. ;; when their first reference is found.
  2402. info 'first-match 'footnote-definition)))))
  2403. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  2404. ;;;; For Headlines
  2405. ;;
  2406. ;; `org-export-get-relative-level' is a shortcut to get headline
  2407. ;; level, relatively to the lower headline level in the parsed tree.
  2408. ;;
  2409. ;; `org-export-get-headline-number' returns the section number of an
  2410. ;; headline, while `org-export-number-to-roman' allows to convert it
  2411. ;; to roman numbers.
  2412. ;;
  2413. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2414. ;; `org-export-last-sibling-p' are three useful predicates when it
  2415. ;; comes to fulfill the `:headline-levels' property.
  2416. (defun org-export-get-relative-level (headline info)
  2417. "Return HEADLINE relative level within current parsed tree.
  2418. INFO is a plist holding contextual information."
  2419. (+ (org-element-property :level headline)
  2420. (or (plist-get info :headline-offset) 0)))
  2421. (defun org-export-low-level-p (headline info)
  2422. "Non-nil when HEADLINE is considered as low level.
  2423. INFO is a plist used as a communication channel.
  2424. A low level headlines has a relative level greater than
  2425. `:headline-levels' property value.
  2426. Return value is the difference between HEADLINE relative level
  2427. and the last level being considered as high enough, or nil."
  2428. (let ((limit (plist-get info :headline-levels)))
  2429. (when (wholenump limit)
  2430. (let ((level (org-export-get-relative-level headline info)))
  2431. (and (> level limit) (- level limit))))))
  2432. (defun org-export-get-headline-number (headline info)
  2433. "Return HEADLINE numbering as a list of numbers.
  2434. INFO is a plist holding contextual information."
  2435. (cdr (assoc headline (plist-get info :headline-numbering))))
  2436. (defun org-export-numbered-headline-p (headline info)
  2437. "Return a non-nil value if HEADLINE element should be numbered.
  2438. INFO is a plist used as a communication channel."
  2439. (let ((sec-num (plist-get info :section-numbers))
  2440. (level (org-export-get-relative-level headline info)))
  2441. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  2442. (defun org-export-number-to-roman (n)
  2443. "Convert integer N into a roman numeral."
  2444. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2445. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2446. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2447. ( 1 . "I")))
  2448. (res ""))
  2449. (if (<= n 0)
  2450. (number-to-string n)
  2451. (while roman
  2452. (if (>= n (caar roman))
  2453. (setq n (- n (caar roman))
  2454. res (concat res (cdar roman)))
  2455. (pop roman)))
  2456. res)))
  2457. (defun org-export-first-sibling-p (headline info)
  2458. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2459. INFO is the plist used as a communication channel."
  2460. (not (eq (org-element-type (org-export-get-previous-element headline info))
  2461. 'headline)))
  2462. (defun org-export-last-sibling-p (headline info)
  2463. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2464. INFO is the plist used as a communication channel."
  2465. (not (org-export-get-next-element headline info)))
  2466. ;;;; For Links
  2467. ;;
  2468. ;; `org-export-solidify-link-text' turns a string into a safer version
  2469. ;; for links, replacing most non-standard characters with hyphens.
  2470. ;;
  2471. ;; `org-export-get-coderef-format' returns an appropriate format
  2472. ;; string for coderefs.
  2473. ;;
  2474. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2475. ;; provided should be considered as an inline image.
  2476. ;;
  2477. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2478. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2479. ;; returns an appropriate unique identifier when found, or nil.
  2480. ;;
  2481. ;; `org-export-resolve-id-link' returns the first headline with
  2482. ;; specified id or custom-id in parse tree, or nil when none was
  2483. ;; found.
  2484. ;;
  2485. ;; `org-export-resolve-coderef' associates a reference to a line
  2486. ;; number in the element it belongs, or returns the reference itself
  2487. ;; when the element isn't numbered.
  2488. (defun org-export-solidify-link-text (s)
  2489. "Take link text S and make a safe target out of it."
  2490. (save-match-data
  2491. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2492. (defun org-export-get-coderef-format (path desc)
  2493. "Return format string for code reference link.
  2494. PATH is the link path. DESC is its description."
  2495. (save-match-data
  2496. (cond ((not desc) "%s")
  2497. ((string-match (regexp-quote (concat "(" path ")")) desc)
  2498. (replace-match "%s" t t desc))
  2499. (t desc))))
  2500. (defun org-export-inline-image-p (link &optional rules)
  2501. "Non-nil if LINK object points to an inline image.
  2502. Optional argument is a set of RULES defining inline images. It
  2503. is an alist where associations have the following shape:
  2504. \(TYPE . REGEXP)
  2505. Applying a rule means apply REGEXP against LINK's path when its
  2506. type is TYPE. The function will return a non-nil value if any of
  2507. the provided rules is non-nil. The default rule is
  2508. `org-export-default-inline-image-rule'.
  2509. This only applies to links without a description."
  2510. (and (not (org-element-contents link))
  2511. (let ((case-fold-search t)
  2512. (rules (or rules org-export-default-inline-image-rule)))
  2513. (some
  2514. (lambda (rule)
  2515. (and (string= (org-element-property :type link) (car rule))
  2516. (string-match (cdr rule)
  2517. (org-element-property :path link))))
  2518. rules))))
  2519. (defun org-export-resolve-fuzzy-link (link info)
  2520. "Return LINK destination.
  2521. INFO is a plist holding contextual information.
  2522. Return value can be an object, an element, or nil:
  2523. - If LINK path matches a target object (i.e. <<path>>) or
  2524. element (i.e. \"#+TARGET: path\"), return it.
  2525. - If LINK path exactly matches the name affiliated keyword
  2526. \(i.e. #+NAME: path) of an element, return that element.
  2527. - If LINK path exactly matches any headline name, return that
  2528. element. If more than one headline share that name, priority
  2529. will be given to the one with the closest common ancestor, if
  2530. any, or the first one in the parse tree otherwise.
  2531. - Otherwise, return nil.
  2532. Assume LINK type is \"fuzzy\"."
  2533. (let ((path (org-element-property :path link)))
  2534. (cond
  2535. ;; First try to find a matching "<<path>>" unless user specified
  2536. ;; he was looking for an headline (path starts with a *
  2537. ;; character).
  2538. ((and (not (eq (substring path 0 1) ?*))
  2539. (loop for target in (plist-get info :target-list)
  2540. when (string= (org-element-property :value target) path)
  2541. return target)))
  2542. ;; Then try to find an element with a matching "#+NAME: path"
  2543. ;; affiliated keyword.
  2544. ((and (not (eq (substring path 0 1) ?*))
  2545. (org-element-map
  2546. (plist-get info :parse-tree) org-element-all-elements
  2547. (lambda (el)
  2548. (when (string= (org-element-property :name el) path) el))
  2549. info 'first-match)))
  2550. ;; Last case: link either points to an headline or to
  2551. ;; nothingness. Try to find the source, with priority given to
  2552. ;; headlines with the closest common ancestor. If such candidate
  2553. ;; is found, return its beginning position as an unique
  2554. ;; identifier, otherwise return nil.
  2555. (t
  2556. (let ((find-headline
  2557. (function
  2558. ;; Return first headline whose `:raw-value' property
  2559. ;; is NAME in parse tree DATA, or nil.
  2560. (lambda (name data)
  2561. (org-element-map
  2562. data 'headline
  2563. (lambda (headline)
  2564. (when (string=
  2565. (org-element-property :raw-value headline)
  2566. name)
  2567. headline))
  2568. info 'first-match)))))
  2569. ;; Search among headlines sharing an ancestor with link,
  2570. ;; from closest to farthest.
  2571. (or (catch 'exit
  2572. (mapc
  2573. (lambda (parent)
  2574. (when (eq (org-element-type parent) 'headline)
  2575. (let ((foundp (funcall find-headline path parent)))
  2576. (when foundp (throw 'exit foundp)))))
  2577. (org-export-get-genealogy link info)) nil)
  2578. ;; No match with a common ancestor: try the full parse-tree.
  2579. (funcall find-headline path (plist-get info :parse-tree))))))))
  2580. (defun org-export-resolve-id-link (link info)
  2581. "Return headline referenced as LINK destination.
  2582. INFO is a plist used as a communication channel.
  2583. Return value can be an headline element or nil. Assume LINK type
  2584. is either \"id\" or \"custom-id\"."
  2585. (let ((id (org-element-property :path link)))
  2586. (org-element-map
  2587. (plist-get info :parse-tree) 'headline
  2588. (lambda (headline)
  2589. (when (or (string= (org-element-property :id headline) id)
  2590. (string= (org-element-property :custom-id headline) id))
  2591. headline))
  2592. info 'first-match)))
  2593. (defun org-export-resolve-coderef (ref info)
  2594. "Resolve a code reference REF.
  2595. INFO is a plist used as a communication channel.
  2596. Return associated line number in source code, or REF itself,
  2597. depending on src-block or example element's switches."
  2598. (org-element-map
  2599. (plist-get info :parse-tree) '(example-block src-block)
  2600. (lambda (el)
  2601. (with-temp-buffer
  2602. (insert (org-trim (org-element-property :value el)))
  2603. (let* ((label-fmt (regexp-quote
  2604. (or (org-element-property :label-fmt el)
  2605. org-coderef-label-format)))
  2606. (ref-re
  2607. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2608. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  2609. ;; Element containing REF is found. Resolve it to either
  2610. ;; a label or a line number, as needed.
  2611. (when (re-search-backward ref-re nil t)
  2612. (cond
  2613. ((org-element-property :use-labels el) ref)
  2614. ((eq (org-element-property :number-lines el) 'continued)
  2615. (+ (org-export-get-loc el info) (line-number-at-pos)))
  2616. (t (line-number-at-pos)))))))
  2617. info 'first-match))
  2618. ;;;; For Macros
  2619. ;;
  2620. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2621. (defun org-export-expand-macro (macro info)
  2622. "Expand MACRO and return it as a string.
  2623. INFO is a plist holding export options."
  2624. (let* ((key (org-element-property :key macro))
  2625. (args (org-element-property :args macro))
  2626. ;; User's macros are stored in the communication channel with
  2627. ;; a ":macro-" prefix. If it's a string leave it as-is.
  2628. ;; Otherwise, it's a secondary string that needs to be
  2629. ;; expanded recursively.
  2630. (value
  2631. (let ((val (plist-get info (intern (format ":macro-%s" key)))))
  2632. (if (stringp val) val (org-export-secondary-string val info)))))
  2633. ;; Replace arguments in VALUE.
  2634. (let ((s 0) n)
  2635. (while (string-match "\\$\\([0-9]+\\)" value s)
  2636. (setq s (1+ (match-beginning 0))
  2637. n (string-to-number (match-string 1 value)))
  2638. (and (>= (length args) n)
  2639. (setq value (replace-match (nth (1- n) args) t t value)))))
  2640. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2641. (when (string-match "\\`(eval\\>" value)
  2642. (setq value (eval (read value))))
  2643. ;; Return string.
  2644. (format "%s" (or value ""))))
  2645. ;;;; For References
  2646. ;;
  2647. ;; `org-export-get-ordinal' associates a sequence number to any object
  2648. ;; or element.
  2649. (defun org-export-get-ordinal (element info &optional types predicate)
  2650. "Return ordinal number of an element or object.
  2651. ELEMENT is the element or object considered. INFO is the plist
  2652. used as a communication channel.
  2653. Optional argument TYPES, when non-nil, is a list of element or
  2654. object types, as symbols, that should also be counted in.
  2655. Otherwise, only provided element's type is considered.
  2656. Optional argument PREDICATE is a function returning a non-nil
  2657. value if the current element or object should be counted in. It
  2658. accepts two arguments: the element or object being considered and
  2659. the plist used as a communication channel. This allows to count
  2660. only a certain type of objects (i.e. inline images).
  2661. Return value is a list of numbers if ELEMENT is an headline or an
  2662. item. It is nil for keywords. It represents the footnote number
  2663. for footnote definitions and footnote references. If ELEMENT is
  2664. a target, return the same value as if ELEMENT was the closest
  2665. table, item or headline containing the target. In any other
  2666. case, return the sequence number of ELEMENT among elements or
  2667. objects of the same type."
  2668. ;; A target keyword, representing an invisible target, never has
  2669. ;; a sequence number.
  2670. (unless (eq (org-element-type element) 'keyword)
  2671. ;; Ordinal of a target object refer to the ordinal of the closest
  2672. ;; table, item, or headline containing the object.
  2673. (when (eq (org-element-type element) 'target)
  2674. (setq element
  2675. (loop for parent in (org-export-get-genealogy element info)
  2676. when
  2677. (memq
  2678. (org-element-type parent)
  2679. '(footnote-definition footnote-reference headline item
  2680. table))
  2681. return parent)))
  2682. (case (org-element-type element)
  2683. ;; Special case 1: An headline returns its number as a list.
  2684. (headline (org-export-get-headline-number element info))
  2685. ;; Special case 2: An item returns its number as a list.
  2686. (item (let ((struct (org-element-property :structure element)))
  2687. (org-list-get-item-number
  2688. (org-element-property :begin element)
  2689. struct
  2690. (org-list-prevs-alist struct)
  2691. (org-list-parents-alist struct))))
  2692. ((footnote definition footnote-reference)
  2693. (org-export-get-footnote-number element info))
  2694. (otherwise
  2695. (let ((counter 0))
  2696. ;; Increment counter until ELEMENT is found again.
  2697. (org-element-map
  2698. (plist-get info :parse-tree) (or types (org-element-type element))
  2699. (lambda (el)
  2700. (cond
  2701. ((equal element el) (1+ counter))
  2702. ((not predicate) (incf counter) nil)
  2703. ((funcall predicate el info) (incf counter) nil)))
  2704. info 'first-match))))))
  2705. ;;;; For Src-Blocks
  2706. ;;
  2707. ;; `org-export-get-loc' counts number of code lines accumulated in
  2708. ;; src-block or example-block elements with a "+n" switch until
  2709. ;; a given element, excluded. Note: "-n" switches reset that count.
  2710. ;;
  2711. ;; `org-export-unravel-code' extracts source code (along with a code
  2712. ;; references alist) from an `element-block' or `src-block' type
  2713. ;; element.
  2714. ;;
  2715. ;; `org-export-format-code' applies a formatting function to each line
  2716. ;; of code, providing relative line number and code reference when
  2717. ;; appropriate. Since it doesn't access the original element from
  2718. ;; which the source code is coming, it expects from the code calling
  2719. ;; it to know if lines should be numbered and if code references
  2720. ;; should appear.
  2721. ;;
  2722. ;; Eventually, `org-export-format-code-default' is a higher-level
  2723. ;; function (it makes use of the two previous functions) which handles
  2724. ;; line numbering and code references inclusion, and returns source
  2725. ;; code in a format suitable for plain text or verbatim output.
  2726. (defun org-export-get-loc (element info)
  2727. "Return accumulated lines of code up to ELEMENT.
  2728. INFO is the plist used as a communication channel.
  2729. ELEMENT is excluded from count."
  2730. (let ((loc 0))
  2731. (org-element-map
  2732. (plist-get info :parse-tree)
  2733. `(src-block example-block ,(org-element-type element))
  2734. (lambda (el)
  2735. (cond
  2736. ;; ELEMENT is reached: Quit the loop.
  2737. ((equal el element) t)
  2738. ;; Only count lines from src-block and example-block elements
  2739. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2740. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  2741. ((let ((linums (org-element-property :number-lines el)))
  2742. (when linums
  2743. ;; Accumulate locs or reset them.
  2744. (let ((lines (org-count-lines
  2745. (org-trim (org-element-property :value el)))))
  2746. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  2747. ;; Return nil to stay in the loop.
  2748. nil)))
  2749. info 'first-match)
  2750. ;; Return value.
  2751. loc))
  2752. (defun org-export-unravel-code (element)
  2753. "Clean source code and extract references out of it.
  2754. ELEMENT has either a `src-block' an `example-block' type.
  2755. Return a cons cell whose CAR is the source code, cleaned from any
  2756. reference and protective comma and CDR is an alist between
  2757. relative line number (integer) and name of code reference on that
  2758. line (string)."
  2759. (let* ((line 0) refs
  2760. ;; Get code and clean it. Remove blank lines at its
  2761. ;; beginning and end. Also remove protective commas.
  2762. (code (let ((c (replace-regexp-in-string
  2763. "\\`\\([ \t]*\n\\)+" ""
  2764. (replace-regexp-in-string
  2765. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
  2766. (org-element-property :value element)))))
  2767. ;; If appropriate, remove global indentation.
  2768. (unless (or org-src-preserve-indentation
  2769. (org-element-property :preserve-indent element))
  2770. (setq c (org-remove-indentation c)))
  2771. ;; Free up the protected lines. Note: Org blocks
  2772. ;; have commas at the beginning or every line.
  2773. (if (string= (org-element-property :language element) "org")
  2774. (replace-regexp-in-string "^," "" c)
  2775. (replace-regexp-in-string
  2776. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2777. ;; Get format used for references.
  2778. (label-fmt (regexp-quote
  2779. (or (org-element-property :label-fmt element)
  2780. org-coderef-label-format)))
  2781. ;; Build a regexp matching a loc with a reference.
  2782. (with-ref-re
  2783. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  2784. (replace-regexp-in-string
  2785. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2786. ;; Return value.
  2787. (cons
  2788. ;; Code with references removed.
  2789. (org-element-normalize-string
  2790. (mapconcat
  2791. (lambda (loc)
  2792. (incf line)
  2793. (if (not (string-match with-ref-re loc)) loc
  2794. ;; Ref line: remove ref, and signal its position in REFS.
  2795. (push (cons line (match-string 3 loc)) refs)
  2796. (replace-match "" nil nil loc 1)))
  2797. (org-split-string code "\n") "\n"))
  2798. ;; Reference alist.
  2799. refs)))
  2800. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  2801. "Format CODE by applying FUN line-wise and return it.
  2802. CODE is a string representing the code to format. FUN is
  2803. a function. It must accept three arguments: a line of
  2804. code (string), the current line number (integer) or nil and the
  2805. reference associated to the current line (string) or nil.
  2806. Optional argument NUM-LINES can be an integer representing the
  2807. number of code lines accumulated until the current code. Line
  2808. numbers passed to FUN will take it into account. If it is nil,
  2809. FUN's second argument will always be nil. This number can be
  2810. obtained with `org-export-get-loc' function.
  2811. Optional argument REF-ALIST can be an alist between relative line
  2812. number (i.e. ignoring NUM-LINES) and the name of the code
  2813. reference on it. If it is nil, FUN's third argument will always
  2814. be nil. It can be obtained through the use of
  2815. `org-export-unravel-code' function."
  2816. (let ((--locs (org-split-string code "\n"))
  2817. (--line 0))
  2818. (org-element-normalize-string
  2819. (mapconcat
  2820. (lambda (--loc)
  2821. (incf --line)
  2822. (let ((--ref (cdr (assq --line ref-alist))))
  2823. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  2824. --locs "\n"))))
  2825. (defun org-export-format-code-default (element info)
  2826. "Return source code from ELEMENT, formatted in a standard way.
  2827. ELEMENT is either a `src-block' or `example-block' element. INFO
  2828. is a plist used as a communication channel.
  2829. This function takes care of line numbering and code references
  2830. inclusion. Line numbers, when applicable, appear at the
  2831. beginning of the line, separated from the code by two white
  2832. spaces. Code references, on the other hand, appear flushed to
  2833. the right, separated by six white spaces from the widest line of
  2834. code."
  2835. ;; Extract code and references.
  2836. (let* ((code-info (org-export-unravel-code element))
  2837. (code (car code-info))
  2838. (code-lines (org-split-string code "\n"))
  2839. (refs (and (org-element-property :retain-labels element)
  2840. (cdr code-info)))
  2841. ;; Handle line numbering.
  2842. (num-start (case (org-element-property :number-lines element)
  2843. (continued (org-export-get-loc element info))
  2844. (new 0)))
  2845. (num-fmt
  2846. (and num-start
  2847. (format "%%%ds "
  2848. (length (number-to-string
  2849. (+ (length code-lines) num-start))))))
  2850. ;; Prepare references display, if required. Any reference
  2851. ;; should start six columns after the widest line of code,
  2852. ;; wrapped with parenthesis.
  2853. (max-width
  2854. (+ (apply 'max (mapcar 'length code-lines))
  2855. (if (not num-start) 0 (length (format num-fmt num-start))))))
  2856. (org-export-format-code
  2857. code
  2858. (lambda (loc line-num ref)
  2859. (let ((number-str (and num-fmt (format num-fmt line-num))))
  2860. (concat
  2861. number-str
  2862. loc
  2863. (and ref
  2864. (concat (make-string
  2865. (- (+ 6 max-width)
  2866. (+ (length loc) (length number-str))) ? )
  2867. (format "(%s)" ref))))))
  2868. num-start refs)))
  2869. ;;;; For Tables
  2870. ;;
  2871. ;; `org-export-table-has-special-column-p' and
  2872. ;; `org-export-table-row-is-special-p' are predicates used to look for
  2873. ;; meta-information about the table structure.
  2874. ;;
  2875. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  2876. ;; and `org-export-table-cell-borders' extract information from
  2877. ;; a table-cell element.
  2878. ;;
  2879. ;; `org-export-table-dimensions' gives the number on rows and columns
  2880. ;; in the table, ignoring horizontal rules and special columns.
  2881. ;; `org-export-table-cell-address', given a table-cell object, returns
  2882. ;; the absolute address of a cell. On the other hand,
  2883. ;; `org-export-get-table-cell-at' does the contrary.
  2884. (defun org-export-table-has-special-column-p (table)
  2885. "Non-nil when TABLE has a special column.
  2886. All special columns will be ignored during export."
  2887. ;; The table has a special column when every first cell of every row
  2888. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  2889. ;; "*" "_" and "^". Though, do not consider a first row containing
  2890. ;; only empty cells as special.
  2891. (let ((special-column-p 'empty))
  2892. (catch 'exit
  2893. (mapc
  2894. (lambda (row)
  2895. (when (eq (org-element-property :type row) 'standard)
  2896. (let ((value (org-element-contents
  2897. (car (org-element-contents row)))))
  2898. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  2899. (setq special-column-p 'special))
  2900. ((not value))
  2901. (t (throw 'exit nil))))))
  2902. (org-element-contents table))
  2903. (eq special-column-p 'special))))
  2904. (defun org-export-table-has-header-p (table info)
  2905. "Non-nil when TABLE has an header.
  2906. INFO is a plist used as a communication channel.
  2907. A table has an header when it contains at least two row groups."
  2908. (let ((rowgroup 1) row-flag)
  2909. (org-element-map
  2910. table 'table-row
  2911. (lambda (row)
  2912. (cond
  2913. ((> rowgroup 1) t)
  2914. ((and row-flag (eq (org-element-property :type row) 'rule))
  2915. (incf rowgroup) (setq row-flag nil))
  2916. ((and (not row-flag) (eq (org-element-property :type row) 'standard))
  2917. (setq row-flag t) nil)))
  2918. info)))
  2919. (defun org-export-table-row-is-special-p (table-row info)
  2920. "Non-nil if TABLE-ROW is considered special.
  2921. INFO is a plist used as the communication channel.
  2922. All special rows will be ignored during export."
  2923. (when (eq (org-element-property :type table-row) 'standard)
  2924. (let ((first-cell (org-element-contents
  2925. (car (org-element-contents table-row)))))
  2926. ;; A row is special either when...
  2927. (or
  2928. ;; ... it starts with a field only containing "/",
  2929. (equal first-cell '("/"))
  2930. ;; ... the table contains a special column and the row start
  2931. ;; with a marking character among, "^", "_", "$" or "!",
  2932. (and (org-export-table-has-special-column-p
  2933. (org-export-get-parent table-row info))
  2934. (member first-cell '(("^") ("_") ("$") ("!"))))
  2935. ;; ... it contains only alignment cookies and empty cells.
  2936. (let ((special-row-p 'empty))
  2937. (catch 'exit
  2938. (mapc
  2939. (lambda (cell)
  2940. (let ((value (org-element-contents cell)))
  2941. (cond ((not value))
  2942. ((and (not (cdr value))
  2943. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  2944. (car value)))
  2945. (setq special-row-p 'cookie))
  2946. (t (throw 'exit nil)))))
  2947. (org-element-contents table-row))
  2948. (eq special-row-p 'cookie)))))))
  2949. (defun org-export-table-row-group (table-row info)
  2950. "Return TABLE-ROW's group.
  2951. INFO is a plist used as the communication channel.
  2952. Return value is the group number, as an integer, or nil special
  2953. rows and table rules. Group 1 is also table's header."
  2954. (unless (or (eq (org-element-property :type table-row) 'rule)
  2955. (org-export-table-row-is-special-p table-row info))
  2956. (let ((group 0) row-flag)
  2957. (catch 'found
  2958. (mapc
  2959. (lambda (row)
  2960. (cond
  2961. ((and (eq (org-element-property :type row) 'standard)
  2962. (not (org-export-table-row-is-special-p row info)))
  2963. (unless row-flag (incf group) (setq row-flag t)))
  2964. ((eq (org-element-property :type row) 'rule)
  2965. (setq row-flag nil)))
  2966. (when (equal table-row row) (throw 'found group)))
  2967. (org-element-contents (org-export-get-parent table-row info)))))))
  2968. (defun org-export-table-cell-width (table-cell info)
  2969. "Return TABLE-CELL contents width.
  2970. INFO is a plist used as the communication channel.
  2971. Return value is the width given by the last width cookie in the
  2972. same column as TABLE-CELL, or nil."
  2973. (let* ((genealogy (org-export-get-genealogy table-cell info))
  2974. (row (car genealogy))
  2975. (column (let ((cells (org-element-contents row)))
  2976. (- (length cells) (length (member table-cell cells)))))
  2977. (table (nth 1 genealogy))
  2978. cookie-width)
  2979. (mapc
  2980. (lambda (row)
  2981. (cond
  2982. ;; In a special row, try to find a width cookie at COLUMN.
  2983. ((org-export-table-row-is-special-p row info)
  2984. (let ((value (org-element-contents
  2985. (elt (org-element-contents row) column))))
  2986. (cond
  2987. ((not value))
  2988. ((and (not (cdr value))
  2989. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
  2990. (match-string 1 (car value)))
  2991. (setq cookie-width
  2992. (string-to-number (match-string 1 (car value))))))))
  2993. ;; Ignore table rules.
  2994. ((eq (org-element-property :type row) 'rule))))
  2995. (org-element-contents table))
  2996. ;; Return value.
  2997. cookie-width))
  2998. (defun org-export-table-cell-alignment (table-cell info)
  2999. "Return TABLE-CELL contents alignment.
  3000. INFO is a plist used as the communication channel.
  3001. Return alignment as specified by the last alignment cookie in the
  3002. same column as TABLE-CELL. If no such cookie is found, a default
  3003. alignment value will be deduced from fraction of numbers in the
  3004. column (see `org-table-number-fraction' for more information).
  3005. Possible values are `left', `right' and `center'."
  3006. (let* ((genealogy (org-export-get-genealogy table-cell info))
  3007. (row (car genealogy))
  3008. (column (let ((cells (org-element-contents row)))
  3009. (- (length cells) (length (member table-cell cells)))))
  3010. (table (nth 1 genealogy))
  3011. (number-cells 0)
  3012. (total-cells 0)
  3013. cookie-align)
  3014. (mapc
  3015. (lambda (row)
  3016. (cond
  3017. ;; In a special row, try to find an alignment cookie at
  3018. ;; COLUMN.
  3019. ((org-export-table-row-is-special-p row info)
  3020. (let ((value (org-element-contents
  3021. (elt (org-element-contents row) column))))
  3022. (cond
  3023. ((not value))
  3024. ((and (not (cdr value))
  3025. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  3026. (car value))
  3027. (match-string 1 (car value)))
  3028. (setq cookie-align (match-string 1 (car value)))))))
  3029. ;; Ignore table rules.
  3030. ((eq (org-element-property :type row) 'rule))
  3031. ;; In a standard row, check if cell's contents are expressing
  3032. ;; some kind of number. Increase NUMBER-CELLS accordingly.
  3033. ;; Though, don't bother if an alignment cookie has already
  3034. ;; defined cell's alignment.
  3035. ((not cookie-align)
  3036. (let ((value (org-element-interpret-secondary
  3037. (org-element-contents
  3038. (elt (org-element-contents row) column)))))
  3039. (incf total-cells)
  3040. (when (string-match org-table-number-regexp value)
  3041. (incf number-cells))))))
  3042. (org-element-contents table))
  3043. ;; Return value. Alignment specified by cookies has precedence
  3044. ;; over alignment deduced from cells contents.
  3045. (cond ((equal cookie-align "l") 'left)
  3046. ((equal cookie-align "r") 'right)
  3047. ((equal cookie-align "c") 'center)
  3048. ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
  3049. 'right)
  3050. (t 'left))))
  3051. (defun org-export-table-cell-borders (table-cell info)
  3052. "Return TABLE-CELL borders.
  3053. INFO is a plist used as a communication channel.
  3054. Return value is a list of symbols, or nil. Possible values are:
  3055. `top', `bottom', `above', `below', `left' and `right'. Note:
  3056. `top' (resp. `bottom') only happen for a cell in the first
  3057. row (resp. last row) of the table, ignoring table rules, if any.
  3058. Returned borders ignore special rows."
  3059. (let* ((genealogy (org-export-get-genealogy table-cell info))
  3060. (row (car genealogy))
  3061. (table (nth 1 genealogy))
  3062. borders)
  3063. ;; Top/above border? TABLE-CELL has a border above when a rule
  3064. ;; used to demarcate row groups can be found above. Hence,
  3065. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  3066. ;; another regular row has to be found above that rule.
  3067. (let (rule-flag)
  3068. (catch 'exit
  3069. (mapc (lambda (row)
  3070. (cond ((eq (org-element-property :type row) 'rule)
  3071. (setq rule-flag t))
  3072. ((not (org-export-table-row-is-special-p row info))
  3073. (if rule-flag (throw 'exit (push 'above borders))
  3074. (throw 'exit nil)))))
  3075. ;; Look at every row before the current one.
  3076. (cdr (member row (reverse (org-element-contents table)))))
  3077. ;; No rule above, or rule found starts the table (ignoring any
  3078. ;; special row): TABLE-CELL is at the top of the table.
  3079. (when rule-flag (push 'above borders))
  3080. (push 'top borders)))
  3081. ;; Bottom/below border? TABLE-CELL has a border below when next
  3082. ;; non-regular row below is a rule.
  3083. (let (rule-flag)
  3084. (catch 'exit
  3085. (mapc (lambda (row)
  3086. (cond ((eq (org-element-property :type row) 'rule)
  3087. (setq rule-flag t))
  3088. ((not (org-export-table-row-is-special-p row info))
  3089. (if rule-flag (throw 'exit (push 'below borders))
  3090. (throw 'exit nil)))))
  3091. ;; Look at every row after the current one.
  3092. (cdr (member row (org-element-contents table))))
  3093. ;; No rule below, or rule found ends the table (modulo some
  3094. ;; special row): TABLE-CELL is at the bottom of the table.
  3095. (when rule-flag (push 'below borders))
  3096. (push 'bottom borders)))
  3097. ;; Right/left borders? They can only be specified by column
  3098. ;; groups. Column groups are defined in a row starting with "/".
  3099. ;; Also a column groups row only contains "<", "<>", ">" or blank
  3100. ;; cells.
  3101. (catch 'exit
  3102. (let ((column (let ((cells (org-element-contents row)))
  3103. (- (length cells) (length (member table-cell cells))))))
  3104. (mapc
  3105. (lambda (row)
  3106. (unless (eq (org-element-property :type row) 'rule)
  3107. (when (equal (org-element-contents
  3108. (car (org-element-contents row)))
  3109. '("/"))
  3110. (let ((column-groups
  3111. (mapcar
  3112. (lambda (cell)
  3113. (let ((value (org-element-contents cell)))
  3114. (when (member value '(("<") ("<>") (">") nil))
  3115. (car value))))
  3116. (org-element-contents row))))
  3117. ;; There's a left border when previous cell, if
  3118. ;; any, ends a group, or current one starts one.
  3119. (when (or (and (not (zerop column))
  3120. (member (elt column-groups (1- column))
  3121. '(">" "<>")))
  3122. (member (elt column-groups column) '("<" "<>")))
  3123. (push 'left borders))
  3124. ;; There's a right border when next cell, if any,
  3125. ;; starts a group, or current one ends one.
  3126. (when (or (and (/= (1+ column) (length column-groups))
  3127. (member (elt column-groups (1+ column))
  3128. '("<" "<>")))
  3129. (member (elt column-groups column) '(">" "<>")))
  3130. (push 'right borders))
  3131. (throw 'exit nil)))))
  3132. ;; Table rows are read in reverse order so last column groups
  3133. ;; row has precedence over any previous one.
  3134. (reverse (org-element-contents table)))))
  3135. ;; Return value.
  3136. borders))
  3137. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  3138. "Non-nil when TABLE-CELL is at the beginning of a row group.
  3139. INFO is a plist used as a communication channel."
  3140. ;; A cell starts a column group either when it is at the beginning
  3141. ;; of a row (or after the special column, if any) or when it has
  3142. ;; a left border.
  3143. (or (equal (org-element-map
  3144. (org-export-get-parent table-cell info)
  3145. 'table-cell 'identity info 'first-match)
  3146. table-cell)
  3147. (memq 'left (org-export-table-cell-borders table-cell info))))
  3148. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  3149. "Non-nil when TABLE-CELL is at the end of a row group.
  3150. INFO is a plist used as a communication channel."
  3151. ;; A cell ends a column group either when it is at the end of a row
  3152. ;; or when it has a right border.
  3153. (or (equal (car (last (org-element-contents
  3154. (org-export-get-parent table-cell info))))
  3155. table-cell)
  3156. (memq 'right (org-export-table-cell-borders table-cell info))))
  3157. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  3158. "Non-nil when TABLE-ROW is at the beginning of a column group.
  3159. INFO is a plist used as a communication channel."
  3160. (unless (or (eq (org-element-property :type table-row) 'rule)
  3161. (org-export-table-row-is-special-p table-row info))
  3162. (let ((borders (org-export-table-cell-borders
  3163. (car (org-element-contents table-row)) info)))
  3164. (or (memq 'top borders) (memq 'above borders)))))
  3165. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  3166. "Non-nil when TABLE-ROW is at the end of a column group.
  3167. INFO is a plist used as a communication channel."
  3168. (unless (or (eq (org-element-property :type table-row) 'rule)
  3169. (org-export-table-row-is-special-p table-row info))
  3170. (let ((borders (org-export-table-cell-borders
  3171. (car (org-element-contents table-row)) info)))
  3172. (or (memq 'bottom borders) (memq 'below borders)))))
  3173. (defun org-export-table-row-starts-header-p (table-row info)
  3174. "Non-nil when TABLE-ROW is the first table header's row.
  3175. INFO is a plist used as a communication channel."
  3176. (and (org-export-table-has-header-p
  3177. (org-export-get-parent-table table-row info) info)
  3178. (org-export-table-row-starts-rowgroup-p table-row info)
  3179. (= (org-export-table-row-group table-row info) 1)))
  3180. (defun org-export-table-row-ends-header-p (table-row info)
  3181. "Non-nil when TABLE-ROW is the last table header's row.
  3182. INFO is a plist used as a communication channel."
  3183. (and (org-export-table-has-header-p
  3184. (org-export-get-parent-table table-row info) info)
  3185. (org-export-table-row-ends-rowgroup-p table-row info)
  3186. (= (org-export-table-row-group table-row info) 1)))
  3187. (defun org-export-table-dimensions (table info)
  3188. "Return TABLE dimensions.
  3189. INFO is a plist used as a communication channel.
  3190. Return value is a CONS like (ROWS . COLUMNS) where
  3191. ROWS (resp. COLUMNS) is the number of exportable
  3192. rows (resp. columns)."
  3193. (let (first-row (columns 0) (rows 0))
  3194. ;; Set number of rows, and extract first one.
  3195. (org-element-map
  3196. table 'table-row
  3197. (lambda (row)
  3198. (when (eq (org-element-property :type row) 'standard)
  3199. (incf rows)
  3200. (unless first-row (setq first-row row)))) info)
  3201. ;; Set number of columns.
  3202. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  3203. ;; Return value.
  3204. (cons rows columns)))
  3205. (defun org-export-table-cell-address (table-cell info)
  3206. "Return address of a regular TABLE-CELL object.
  3207. TABLE-CELL is the cell considered. INFO is a plist used as
  3208. a communication channel.
  3209. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3210. zero-based index. Only exportable cells are considered. The
  3211. function returns nil for other cells."
  3212. (let* ((table-row (org-export-get-parent table-cell info))
  3213. (table (org-export-get-parent-table table-cell info)))
  3214. ;; Ignore cells in special rows or in special column.
  3215. (unless (or (org-export-table-row-is-special-p table-row info)
  3216. (and (org-export-table-has-special-column-p table)
  3217. (equal (car (org-element-contents table-row)) table-cell)))
  3218. (cons
  3219. ;; Row number.
  3220. (let ((row-count 0))
  3221. (org-element-map
  3222. table 'table-row
  3223. (lambda (row)
  3224. (cond ((eq (org-element-property :type row) 'rule) nil)
  3225. ((equal row table-row) row-count)
  3226. (t (incf row-count) nil)))
  3227. info 'first-match))
  3228. ;; Column number.
  3229. (let ((col-count 0))
  3230. (org-element-map
  3231. table-row 'table-cell
  3232. (lambda (cell)
  3233. (if (equal cell table-cell) col-count
  3234. (incf col-count) nil))
  3235. info 'first-match))))))
  3236. (defun org-export-get-table-cell-at (address table info)
  3237. "Return regular table-cell object at ADDRESS in TABLE.
  3238. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3239. zero-based index. TABLE is a table type element. INFO is
  3240. a plist used as a communication channel.
  3241. If no table-cell, among exportable cells, is found at ADDRESS,
  3242. return nil."
  3243. (let ((column-pos (cdr address)) (column-count 0))
  3244. (org-element-map
  3245. ;; Row at (car address) or nil.
  3246. (let ((row-pos (car address)) (row-count 0))
  3247. (org-element-map
  3248. table 'table-row
  3249. (lambda (row)
  3250. (cond ((eq (org-element-property :type row) 'rule) nil)
  3251. ((= row-count row-pos) row)
  3252. (t (incf row-count) nil)))
  3253. info 'first-match))
  3254. 'table-cell
  3255. (lambda (cell)
  3256. (if (= column-count column-pos) cell
  3257. (incf column-count) nil))
  3258. info 'first-match)))
  3259. ;;;; For Tables Of Contents
  3260. ;;
  3261. ;; `org-export-collect-headlines' builds a list of all exportable
  3262. ;; headline elements, maybe limited to a certain depth. One can then
  3263. ;; easily parse it and transcode it.
  3264. ;;
  3265. ;; Building lists of tables, figures or listings is quite similar.
  3266. ;; Once the generic function `org-export-collect-elements' is defined,
  3267. ;; `org-export-collect-tables', `org-export-collect-figures' and
  3268. ;; `org-export-collect-listings' can be derived from it.
  3269. (defun org-export-collect-headlines (info &optional n)
  3270. "Collect headlines in order to build a table of contents.
  3271. INFO is a plist used as a communication channel.
  3272. When non-nil, optional argument N must be an integer. It
  3273. specifies the depth of the table of contents.
  3274. Return a list of all exportable headlines as parsed elements."
  3275. (org-element-map
  3276. (plist-get info :parse-tree)
  3277. 'headline
  3278. (lambda (headline)
  3279. ;; Strip contents from HEADLINE.
  3280. (let ((relative-level (org-export-get-relative-level headline info)))
  3281. (unless (and n (> relative-level n)) headline)))
  3282. info))
  3283. (defun org-export-collect-elements (type info &optional predicate)
  3284. "Collect referenceable elements of a determined type.
  3285. TYPE can be a symbol or a list of symbols specifying element
  3286. types to search. Only elements with a caption or a name are
  3287. collected.
  3288. INFO is a plist used as a communication channel.
  3289. When non-nil, optional argument PREDICATE is a function accepting
  3290. one argument, an element of type TYPE. It returns a non-nil
  3291. value when that element should be collected.
  3292. Return a list of all elements found, in order of appearance."
  3293. (org-element-map
  3294. (plist-get info :parse-tree) type
  3295. (lambda (element)
  3296. (and (or (org-element-property :caption element)
  3297. (org-element-property :name element))
  3298. (or (not predicate) (funcall predicate element))
  3299. element)) info))
  3300. (defun org-export-collect-tables (info)
  3301. "Build a list of tables.
  3302. INFO is a plist used as a communication channel.
  3303. Return a list of table elements with a caption or a name
  3304. affiliated keyword."
  3305. (org-export-collect-elements 'table info))
  3306. (defun org-export-collect-figures (info predicate)
  3307. "Build a list of figures.
  3308. INFO is a plist used as a communication channel. PREDICATE is
  3309. a function which accepts one argument: a paragraph element and
  3310. whose return value is non-nil when that element should be
  3311. collected.
  3312. A figure is a paragraph type element, with a caption or a name,
  3313. verifying PREDICATE. The latter has to be provided since
  3314. a \"figure\" is a vague concept that may depend on back-end.
  3315. Return a list of elements recognized as figures."
  3316. (org-export-collect-elements 'paragraph info predicate))
  3317. (defun org-export-collect-listings (info)
  3318. "Build a list of src blocks.
  3319. INFO is a plist used as a communication channel.
  3320. Return a list of src-block elements with a caption or a name
  3321. affiliated keyword."
  3322. (org-export-collect-elements 'src-block info))
  3323. ;;;; Topology
  3324. ;;
  3325. ;; Here are various functions to retrieve information about the
  3326. ;; neighbourhood of a given element or object. Neighbours of interest
  3327. ;; are direct parent (`org-export-get-parent'), parent headline
  3328. ;; (`org-export-get-parent-headline'), parent paragraph
  3329. ;; (`org-export-get-parent-paragraph'), previous element or object
  3330. ;; (`org-export-get-previous-element') and next element or object
  3331. ;; (`org-export-get-next-element').
  3332. ;;
  3333. ;; All of these functions are just a specific use of the more generic
  3334. ;; `org-export-get-genealogy', which returns the genealogy relative to
  3335. ;; the element or object.
  3336. (defun org-export-get-genealogy (blob info)
  3337. "Return genealogy relative to a given element or object.
  3338. BLOB is the element or object being considered. INFO is a plist
  3339. used as a communication channel."
  3340. (let* ((type (org-element-type blob))
  3341. (end (org-element-property :end blob))
  3342. (walk-data
  3343. (lambda (data genealogy)
  3344. ;; Walk DATA, looking for BLOB. GENEALOGY is the list of
  3345. ;; parents of all elements in DATA.
  3346. (mapc
  3347. (lambda (el)
  3348. (cond
  3349. ((stringp el) nil)
  3350. ((equal el blob) (throw 'exit genealogy))
  3351. ((>= (org-element-property :end el) end)
  3352. ;; If BLOB is an object and EL contains a secondary
  3353. ;; string, be sure to check it.
  3354. (when (memq type org-element-all-objects)
  3355. (let ((sec-prop
  3356. (cdr (assq (org-element-type el)
  3357. org-element-secondary-value-alist))))
  3358. (when sec-prop
  3359. (funcall
  3360. walk-data
  3361. (cons 'org-data
  3362. (cons nil (org-element-property sec-prop el)))
  3363. (cons el genealogy)))))
  3364. (funcall walk-data el (cons el genealogy)))))
  3365. (org-element-contents data)))))
  3366. (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
  3367. (defun org-export-get-parent (blob info)
  3368. "Return BLOB parent or nil.
  3369. BLOB is the element or object considered. INFO is a plist used
  3370. as a communication channel."
  3371. (car (org-export-get-genealogy blob info)))
  3372. (defun org-export-get-parent-headline (blob info)
  3373. "Return BLOB parent headline or nil.
  3374. BLOB is the element or object being considered. INFO is a plist
  3375. used as a communication channel."
  3376. (catch 'exit
  3377. (mapc
  3378. (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
  3379. (org-export-get-genealogy blob info))
  3380. nil))
  3381. (defun org-export-get-parent-paragraph (object info)
  3382. "Return OBJECT parent paragraph or nil.
  3383. OBJECT is the object to consider. INFO is a plist used as
  3384. a communication channel."
  3385. (catch 'exit
  3386. (mapc
  3387. (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
  3388. (org-export-get-genealogy object info))
  3389. nil))
  3390. (defun org-export-get-parent-table (object info)
  3391. "Return OBJECT parent table or nil.
  3392. OBJECT is either a `table-cell' or `table-element' type object.
  3393. INFO is a plist used as a communication channel."
  3394. (catch 'exit
  3395. (mapc
  3396. (lambda (el) (when (eq (org-element-type el) 'table) (throw 'exit el)))
  3397. (org-export-get-genealogy object info))
  3398. nil))
  3399. (defun org-export-get-previous-element (blob info)
  3400. "Return previous element or object.
  3401. BLOB is an element or object. INFO is a plist used as
  3402. a communication channel.
  3403. Return previous element or object, a string, or nil."
  3404. (let ((parent (org-export-get-parent blob info)))
  3405. (cadr (member blob (reverse (org-element-contents parent))))))
  3406. (defun org-export-get-next-element (blob info)
  3407. "Return next element or object.
  3408. BLOB is an element or object. INFO is a plist used as
  3409. a communication channel.
  3410. Return next element or object, a string, or nil."
  3411. (let ((parent (org-export-get-parent blob info)))
  3412. (cadr (member blob (org-element-contents parent)))))
  3413. ;;; The Dispatcher
  3414. ;;
  3415. ;; `org-export-dispatch' is the standard interactive way to start an
  3416. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  3417. ;; for its interface. Most commons back-ends should have an entry in
  3418. ;; it.
  3419. (defun org-export-dispatch ()
  3420. "Export dispatcher for Org mode.
  3421. It provides an access to common export related tasks in a buffer.
  3422. Its interface comes in two flavours: standard and expert. While
  3423. both share the same set of bindings, only the former displays the
  3424. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  3425. to switch to one or the other.
  3426. Return an error if key pressed has no associated command."
  3427. (interactive)
  3428. (let* ((input (org-export-dispatch-ui
  3429. (if (listp org-export-initial-scope) org-export-initial-scope
  3430. (list org-export-initial-scope))
  3431. org-export-dispatch-use-expert-ui))
  3432. (raw-key (car input))
  3433. (optns (cdr input)))
  3434. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  3435. ;; depending on user's key pressed.
  3436. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  3437. ;; Allow to quit with "q" key.
  3438. (?q nil)
  3439. ;; Export with `e-ascii' back-end.
  3440. ((?A ?N ?U)
  3441. (let ((outbuf
  3442. (org-export-to-buffer
  3443. 'e-ascii "*Org E-ASCII Export*"
  3444. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3445. `(:ascii-charset
  3446. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  3447. (with-current-buffer outbuf (text-mode))
  3448. (when org-export-show-temporary-export-buffer
  3449. (switch-to-buffer-other-window outbuf))))
  3450. ((?a ?n ?u)
  3451. (org-e-ascii-export-to-ascii
  3452. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3453. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  3454. ;; Export with `e-latex' back-end.
  3455. (?L
  3456. (let ((outbuf
  3457. (org-export-to-buffer
  3458. 'e-latex "*Org E-LaTeX Export*"
  3459. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3460. (with-current-buffer outbuf (latex-mode))
  3461. (when org-export-show-temporary-export-buffer
  3462. (switch-to-buffer-other-window outbuf))))
  3463. (?l (org-e-latex-export-to-latex
  3464. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3465. (?p (org-e-latex-export-to-pdf
  3466. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3467. (?d (org-open-file
  3468. (org-e-latex-export-to-pdf
  3469. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3470. ;; Export with `e-html' back-end.
  3471. (?H
  3472. (let ((outbuf
  3473. (org-export-to-buffer
  3474. 'e-html "*Org E-HTML Export*"
  3475. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3476. ;; set major mode
  3477. (with-current-buffer outbuf
  3478. (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
  3479. (when org-export-show-temporary-export-buffer
  3480. (switch-to-buffer-other-window outbuf))))
  3481. (?h (org-e-html-export-to-html
  3482. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3483. (?b (org-open-file
  3484. (org-e-html-export-to-html
  3485. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3486. ;; Export with `e-odt' back-end.
  3487. (?o (org-e-odt-export-to-odt
  3488. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3489. (?O (org-open-file
  3490. (org-e-odt-export-to-odt
  3491. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3492. ;; Publishing facilities
  3493. (?F (org-e-publish-current-file (memq 'force optns)))
  3494. (?P (org-e-publish-current-project (memq 'force optns)))
  3495. (?X (let ((project
  3496. (assoc (org-icompleting-read
  3497. "Publish project: " org-e-publish-project-alist nil t)
  3498. org-e-publish-project-alist)))
  3499. (org-e-publish project (memq 'force optns))))
  3500. (?E (org-e-publish-all (memq 'force optns)))
  3501. ;; Undefined command.
  3502. (t (error "No command associated with key %s"
  3503. (char-to-string raw-key))))))
  3504. (defun org-export-dispatch-ui (options expertp)
  3505. "Handle interface for `org-export-dispatch'.
  3506. OPTIONS is a list containing current interactive options set for
  3507. export. It can contain any of the following symbols:
  3508. `body' toggles a body-only export
  3509. `subtree' restricts export to current subtree
  3510. `visible' restricts export to visible part of buffer.
  3511. `force' force publishing files.
  3512. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  3513. buffer is provided, but indications about currently active
  3514. options are given in the prompt. Moreover, \[?] allows to switch
  3515. back to standard interface.
  3516. Return value is a list with key pressed as CAR and a list of
  3517. final interactive export options as CDR."
  3518. (let ((help
  3519. (format "---- (Options) -------------------------------------------
  3520. \[1] Body only: %s [2] Export scope: %s
  3521. \[3] Visible only: %s [4] Force publishing: %s
  3522. --- (ASCII/Latin-1/UTF-8 Export) -------------------------
  3523. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  3524. --- (HTML Export) ----------------------------------------
  3525. \[h] to HTML file [b] ... and open it
  3526. \[H] to temporary buffer
  3527. --- (LaTeX Export) ---------------------------------------
  3528. \[l] to TEX file [L] to temporary buffer
  3529. \[p] to PDF file [d] ... and open it
  3530. --- (ODF Export) -----------------------------------------
  3531. \[o] to ODT file [O] ... and open it
  3532. --- (Publish) --------------------------------------------
  3533. \[F] current file [P] current project
  3534. \[X] a project [E] every project"
  3535. (if (memq 'body options) "On " "Off")
  3536. (if (memq 'subtree options) "Subtree" "Buffer ")
  3537. (if (memq 'visible options) "On " "Off")
  3538. (if (memq 'force options) "On " "Off")))
  3539. (standard-prompt "Export command: ")
  3540. (expert-prompt (format "Export command (%s%s%s%s): "
  3541. (if (memq 'body options) "b" "-")
  3542. (if (memq 'subtree options) "s" "-")
  3543. (if (memq 'visible options) "v" "-")
  3544. (if (memq 'force options) "f" "-")))
  3545. (handle-keypress
  3546. (function
  3547. ;; Read a character from command input, toggling interactive
  3548. ;; options when applicable. PROMPT is the displayed prompt,
  3549. ;; as a string.
  3550. (lambda (prompt)
  3551. (let ((key (read-char-exclusive prompt)))
  3552. (cond
  3553. ;; Ignore non-standard characters (i.e. "M-a").
  3554. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  3555. ;; Help key: Switch back to standard interface if
  3556. ;; expert UI was active.
  3557. ((eq key ??) (org-export-dispatch-ui options nil))
  3558. ;; Toggle export options.
  3559. ((memq key '(?1 ?2 ?3 ?4))
  3560. (org-export-dispatch-ui
  3561. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  3562. (?4 'force))))
  3563. (if (memq option options) (remq option options)
  3564. (cons option options)))
  3565. expertp))
  3566. ;; Action selected: Send key and options back to
  3567. ;; `org-export-dispatch'.
  3568. (t (cons key options))))))))
  3569. ;; With expert UI, just read key with a fancy prompt. In standard
  3570. ;; UI, display an intrusive help buffer.
  3571. (if expertp (funcall handle-keypress expert-prompt)
  3572. (save-window-excursion
  3573. (delete-other-windows)
  3574. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  3575. (org-fit-window-to-buffer
  3576. (get-buffer-window "*Org Export/Publishing Help*"))
  3577. (funcall handle-keypress standard-prompt)))))
  3578. (provide 'org-export)
  3579. ;;; org-export.el ends here