org-export.el 193 KB

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