org-export.el 170 KB

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