org-export.el 158 KB

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