org-export.el 194 KB

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