org-export.el 171 KB

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