org-export.el 152 KB

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