org-export.el 189 KB

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