org-export.el 157 KB

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