org-export.el 160 KB

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