org-export.el 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989
  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 (loop for tag in (plist-get info :select-tags)
  1305. thereis (member tag tags))
  1306. ;; When a select tag is found, mark as acceptable
  1307. ;; full genealogy and every headline within the
  1308. ;; tree.
  1309. (setq selected-trees
  1310. (append
  1311. (cons data genealogy)
  1312. (org-element-map data 'headline 'identity)
  1313. selected-trees))
  1314. ;; Else, continue searching in tree, recursively.
  1315. (funcall walk-data data (cons data genealogy))))))))))
  1316. (funcall walk-data data nil) selected-trees))
  1317. (defun org-export--skip-p (blob options select-tags)
  1318. "Non-nil when element or object BLOB should be skipped during export.
  1319. OPTIONS is the plist holding export options. SELECT-TAGS, when
  1320. non-nil, is a list of tags marking a subtree as exportable."
  1321. (case (org-element-type blob)
  1322. ;; Check headline.
  1323. (headline
  1324. (let ((with-tasks (plist-get options :with-tasks))
  1325. (todo (org-element-property :todo-keyword blob))
  1326. (todo-type (org-element-property :todo-type blob))
  1327. (archived (plist-get options :with-archived-trees))
  1328. (tags (org-element-property :tags blob)))
  1329. (or
  1330. ;; Ignore subtrees with an exclude tag.
  1331. (loop for k in (plist-get options :exclude-tags)
  1332. thereis (member k tags))
  1333. ;; Ignore subtrees without a select tag, when such tag is
  1334. ;; found in the buffer.
  1335. (member blob select-tags)
  1336. ;; Ignore commented sub-trees.
  1337. (org-element-property :commentedp blob)
  1338. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1339. (and (not archived) (org-element-property :archivedp blob))
  1340. ;; Ignore tasks, if specified by `:with-tasks' property.
  1341. (and todo
  1342. (or (not with-tasks)
  1343. (and (memq with-tasks '(todo done))
  1344. (not (eq todo-type with-tasks)))
  1345. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1346. ;; Check timestamp.
  1347. (timestamp
  1348. (case (plist-get options :with-timestamps)
  1349. ;; No timestamp allowed.
  1350. ('nil t)
  1351. ;; Only active timestamps allowed and the current one isn't
  1352. ;; active.
  1353. (active
  1354. (not (memq (org-element-property :type blob)
  1355. '(active active-range))))
  1356. ;; Only inactive timestamps allowed and the current one isn't
  1357. ;; inactive.
  1358. (inactive
  1359. (not (memq (org-element-property :type blob)
  1360. '(inactive inactive-range))))))
  1361. ;; Check drawer.
  1362. (drawer
  1363. (or (not (plist-get options :with-drawers))
  1364. (and (consp (plist-get options :with-drawers))
  1365. (not (member (org-element-property :drawer-name blob)
  1366. (plist-get options :with-drawers))))))
  1367. ;; Check table-row.
  1368. (table-row (org-export-table-row-is-special-p blob options))
  1369. ;; Check table-cell.
  1370. (table-cell
  1371. (and (org-export-table-has-special-column-p
  1372. (nth 1 (org-export-get-genealogy blob options)))
  1373. (not (org-export-get-previous-element blob options))))
  1374. ;; Check clock.
  1375. (clock (not (plist-get options :with-clocks)))
  1376. ;; Check planning.
  1377. (planning (not (plist-get options :with-plannings)))))
  1378. ;;; The Transcoder
  1379. ;;
  1380. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1381. ;; `org-element-parse-buffer') and transcodes it into a specified
  1382. ;; back-end output. It takes care of filtering out elements or
  1383. ;; objects according to export options and organizing the output blank
  1384. ;; lines and white space are preserved.
  1385. ;;
  1386. ;; Internally, three functions handle the filtering of objects and
  1387. ;; elements during the export. In particular,
  1388. ;; `org-export-ignore-element' marks an element or object so future
  1389. ;; parse tree traversals skip it, `org-export-interpret-p' tells which
  1390. ;; elements or objects should be seen as real Org syntax and
  1391. ;; `org-export-expand' transforms the others back into their original
  1392. ;; shape.
  1393. (defun org-export-transcoder (blob info)
  1394. "Return appropriate transcoder for BLOB.
  1395. INFO is a plist containing export directives."
  1396. (let ((type (org-element-type blob)))
  1397. ;; Return contents only for complete parse trees.
  1398. (if (eq type 'org-data) (lambda (blob contents info) contents)
  1399. (let ((transcoder
  1400. (intern (format "org-%s-%s" (plist-get info :back-end) type))))
  1401. (and (fboundp transcoder) transcoder)))))
  1402. (defun org-export-data (data info)
  1403. "Convert DATA into current back-end format.
  1404. DATA is a parse tree, an element or an object or a secondary
  1405. string. INFO is a plist holding export options.
  1406. Return transcoded string."
  1407. (let* ((type (org-element-type data))
  1408. (results
  1409. (cond
  1410. ;; Ignored element/object.
  1411. ((member data (plist-get info :ignore-list)) nil)
  1412. ;; Plain text.
  1413. ((eq type 'plain-text)
  1414. (org-export-filter-apply-functions
  1415. (plist-get info :filter-plain-text)
  1416. (let ((transcoder (org-export-transcoder data info)))
  1417. (if transcoder (funcall transcoder data info) data))
  1418. info))
  1419. ;; Uninterpreted element/object: change it back to Org
  1420. ;; syntax.
  1421. ((not (org-export-interpret-p data info))
  1422. (org-export-expand
  1423. data
  1424. (org-element-normalize-string
  1425. (mapconcat (lambda (blob) (org-export-data blob info))
  1426. (org-element-contents data)
  1427. ""))))
  1428. ;; Secondary string.
  1429. ((not type)
  1430. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1431. ;; Element/Object without contents or, as a special case,
  1432. ;; headline with archive tag and archived trees restricted
  1433. ;; to title only.
  1434. ((or (not (org-element-contents data))
  1435. (and (eq type 'headline)
  1436. (eq (plist-get info :with-archived-trees) 'headline)
  1437. (org-element-property :archivedp data)))
  1438. (let ((transcoder (org-export-transcoder data info)))
  1439. (and (fboundp transcoder) (funcall transcoder data nil info))))
  1440. ;; Element/Object with contents.
  1441. (t
  1442. (let ((transcoder (org-export-transcoder data info)))
  1443. (when transcoder
  1444. (let* ((greaterp (memq type org-element-greater-elements))
  1445. (objectp (and (not greaterp)
  1446. (memq type org-element-recursive-objects)))
  1447. (contents
  1448. (mapconcat
  1449. (lambda (element) (org-export-data element info))
  1450. (org-element-contents
  1451. (if (or greaterp objectp) data
  1452. ;; Elements directly containing objects
  1453. ;; must have their indentation normalized
  1454. ;; first.
  1455. (org-element-normalize-contents
  1456. data
  1457. ;; When normalizing contents of the first
  1458. ;; paragraph in an item or a footnote
  1459. ;; definition, ignore first line's
  1460. ;; indentation: there is none and it
  1461. ;; might be misleading.
  1462. (when (eq type 'paragraph)
  1463. (let ((parent (org-export-get-parent data info)))
  1464. (and (equal (car (org-element-contents parent))
  1465. data)
  1466. (memq (org-element-type parent)
  1467. '(footnote-definition item))))))))
  1468. "")))
  1469. (funcall transcoder data
  1470. (if greaterp (org-element-normalize-string contents)
  1471. contents)
  1472. info))))))))
  1473. (cond
  1474. ((not results) nil)
  1475. ((memq type '(org-data plain-text nil)) results)
  1476. ;; Append the same white space between elements or objects as in
  1477. ;; the original buffer, and call appropriate filters.
  1478. (t
  1479. (let ((results
  1480. (org-export-filter-apply-functions
  1481. (plist-get info (intern (format ":filter-%s" type)))
  1482. (let ((post-blank (org-element-property :post-blank data)))
  1483. (if (memq type org-element-all-elements)
  1484. (concat (org-element-normalize-string results)
  1485. (make-string post-blank ?\n))
  1486. (concat results (make-string post-blank ? ))))
  1487. info)))
  1488. ;; Eventually return string.
  1489. results)))))
  1490. (defun org-export-interpret-p (blob info)
  1491. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1492. Check is done according to export options INFO, stored as
  1493. a plist."
  1494. (case (org-element-type blob)
  1495. ;; ... entities...
  1496. (entity (plist-get info :with-entities))
  1497. ;; ... emphasis...
  1498. (emphasis (plist-get info :with-emphasize))
  1499. ;; ... fixed-width areas.
  1500. (fixed-width (plist-get info :with-fixed-width))
  1501. ;; ... footnotes...
  1502. ((footnote-definition footnote-reference)
  1503. (plist-get info :with-footnotes))
  1504. ;; ... sub/superscripts...
  1505. ((subscript superscript)
  1506. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1507. (if (eq sub/super-p '{})
  1508. (org-element-property :use-brackets-p blob)
  1509. sub/super-p)))
  1510. ;; ... tables...
  1511. (table (plist-get info :with-tables))
  1512. (otherwise t)))
  1513. (defsubst org-export-expand (blob contents)
  1514. "Expand a parsed element or object to its original state.
  1515. BLOB is either an element or an object. CONTENTS is its
  1516. contents, as a string or nil."
  1517. (funcall
  1518. (intern (format "org-element-%s-interpreter" (org-element-type blob)))
  1519. blob contents))
  1520. (defun org-export-ignore-element (element info)
  1521. "Add ELEMENT to `:ignore-list' in INFO.
  1522. Any element in `:ignore-list' will be skipped when using
  1523. `org-element-map'. INFO is modified by side effects."
  1524. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  1525. ;;; The Filter System
  1526. ;;
  1527. ;; Filters allow end-users to tweak easily the transcoded output.
  1528. ;; They are the functional counterpart of hooks, as every filter in
  1529. ;; a set is applied to the return value of the previous one.
  1530. ;;
  1531. ;; Every set is back-end agnostic. Although, a filter is always
  1532. ;; called, in addition to the string it applies to, with the back-end
  1533. ;; used as argument, so it's easy enough for the end-user to add
  1534. ;; back-end specific filters in the set. The communication channel,
  1535. ;; as a plist, is required as the third argument.
  1536. ;;
  1537. ;; Filters sets are defined below. There are of four types:
  1538. ;;
  1539. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1540. ;; complete parsed tree. It's the only filters set that doesn't
  1541. ;; apply to a string.
  1542. ;; - `org-export-filter-final-output-functions' applies to the final
  1543. ;; transcoded string.
  1544. ;; - `org-export-filter-plain-text-functions' applies to any string
  1545. ;; not recognized as Org syntax.
  1546. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1547. ;; after an element or object of type TYPE has been transcoded.
  1548. ;;
  1549. ;; All filters sets are applied through
  1550. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1551. ;; applied in a LIFO fashion. It allows developers to be sure that
  1552. ;; their filters will be applied first.
  1553. ;;
  1554. ;; Filters properties are installed in communication channel with
  1555. ;; `org-export-install-filters' function.
  1556. ;;
  1557. ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
  1558. ;; before parsing to allow for heavy structure modifications.
  1559. ;;;; Before Parsing Hook
  1560. (defvar org-export-before-parsing-hook nil
  1561. "Hook run before parsing an export buffer.
  1562. This is run after include keywords have been expanded and Babel
  1563. code executed, on a copy of original buffer's area being
  1564. exported. Visibility is the same as in the original one. Point
  1565. is left at the beginning of the new one.")
  1566. ;;;; Special Filters
  1567. (defvar org-export-filter-parse-tree-functions nil
  1568. "List of functions applied to the parsed tree.
  1569. Each filter is called with three arguments: the parse tree, as
  1570. returned by `org-element-parse-buffer', the back-end, as
  1571. a symbol, and the communication channel, as a plist. It must
  1572. return the modified parse tree to transcode.")
  1573. (defvar org-export-filter-final-output-functions nil
  1574. "List of functions applied to the transcoded string.
  1575. Each filter is called with three arguments: the full transcoded
  1576. string, the back-end, as a symbol, and the communication channel,
  1577. as a plist. It must return a string that will be used as the
  1578. final export output.")
  1579. (defvar org-export-filter-plain-text-functions nil
  1580. "List of functions applied to plain text.
  1581. Each filter is called with three arguments: a string which
  1582. contains no Org syntax, the back-end, as a symbol, and the
  1583. communication channel, as a plist. It must return a string or
  1584. nil.")
  1585. ;;;; Elements Filters
  1586. (defvar org-export-filter-center-block-functions nil
  1587. "List of functions applied to a transcoded center block.
  1588. Each filter is called with three arguments: the transcoded data,
  1589. as a string, the back-end, as a symbol, and the communication
  1590. channel, as a plist. It must return a string or nil.")
  1591. (defvar org-export-filter-clock-functions nil
  1592. "List of functions applied to a transcoded clock.
  1593. Each filter is called with three arguments: the transcoded data,
  1594. as a string, the back-end, as a symbol, and the communication
  1595. channel, as a plist. It must return a string or nil.")
  1596. (defvar org-export-filter-drawer-functions nil
  1597. "List of functions applied to a transcoded drawer.
  1598. Each filter is called with three arguments: the transcoded data,
  1599. as a string, the back-end, as a symbol, and the communication
  1600. channel, as a plist. It must return a string or nil.")
  1601. (defvar org-export-filter-dynamic-block-functions nil
  1602. "List of functions applied to a transcoded dynamic-block.
  1603. Each filter is called with three arguments: the transcoded data,
  1604. as a string, the back-end, as a symbol, and the communication
  1605. channel, as a plist. It must return a string or nil.")
  1606. (defvar org-export-filter-headline-functions nil
  1607. "List of functions applied to a transcoded headline.
  1608. Each filter is called with three arguments: the transcoded data,
  1609. as a string, the back-end, as a symbol, and the communication
  1610. channel, as a plist. It must return a string or nil.")
  1611. (defvar org-export-filter-inlinetask-functions nil
  1612. "List of functions applied to a transcoded inlinetask.
  1613. Each filter is called with three arguments: the transcoded data,
  1614. as a string, the back-end, as a symbol, and the communication
  1615. channel, as a plist. It must return a string or nil.")
  1616. (defvar org-export-filter-plain-list-functions nil
  1617. "List of functions applied to a transcoded plain-list.
  1618. Each filter is called with three arguments: the transcoded data,
  1619. as a string, the back-end, as a symbol, and the communication
  1620. channel, as a plist. It must return a string or nil.")
  1621. (defvar org-export-filter-item-functions nil
  1622. "List of functions applied to a transcoded item.
  1623. Each filter is called with three arguments: the transcoded data,
  1624. as a string, the back-end, as a symbol, and the communication
  1625. channel, as a plist. It must return a string or nil.")
  1626. (defvar org-export-filter-comment-functions nil
  1627. "List of functions applied to a transcoded comment.
  1628. Each filter is called with three arguments: the transcoded data,
  1629. as a string, the back-end, as a symbol, and the communication
  1630. channel, as a plist. It must return a string or nil.")
  1631. (defvar org-export-filter-comment-block-functions nil
  1632. "List of functions applied to a transcoded comment-comment.
  1633. Each filter is called with three arguments: the transcoded data,
  1634. as a string, the back-end, as a symbol, and the communication
  1635. channel, as a plist. It must return a string or nil.")
  1636. (defvar org-export-filter-example-block-functions nil
  1637. "List of functions applied to a transcoded example-block.
  1638. Each filter is called with three arguments: the transcoded data,
  1639. as a string, the back-end, as a symbol, and the communication
  1640. channel, as a plist. It must return a string or nil.")
  1641. (defvar org-export-filter-export-block-functions nil
  1642. "List of functions applied to a transcoded export-block.
  1643. Each filter is called with three arguments: the transcoded data,
  1644. as a string, the back-end, as a symbol, and the communication
  1645. channel, as a plist. It must return a string or nil.")
  1646. (defvar org-export-filter-fixed-width-functions nil
  1647. "List of functions applied to a transcoded fixed-width.
  1648. Each filter is called with three arguments: the transcoded data,
  1649. as a string, the back-end, as a symbol, and the communication
  1650. channel, as a plist. It must return a string or nil.")
  1651. (defvar org-export-filter-footnote-definition-functions nil
  1652. "List of functions applied to a transcoded footnote-definition.
  1653. Each filter is called with three arguments: the transcoded data,
  1654. as a string, the back-end, as a symbol, and the communication
  1655. channel, as a plist. It must return a string or nil.")
  1656. (defvar org-export-filter-horizontal-rule-functions nil
  1657. "List of functions applied to a transcoded horizontal-rule.
  1658. Each filter is called with three arguments: the transcoded data,
  1659. as a string, the back-end, as a symbol, and the communication
  1660. channel, as a plist. It must return a string or nil.")
  1661. (defvar org-export-filter-keyword-functions nil
  1662. "List of functions applied to a transcoded keyword.
  1663. Each filter is called with three arguments: the transcoded data,
  1664. as a string, the back-end, as a symbol, and the communication
  1665. channel, as a plist. It must return a string or nil.")
  1666. (defvar org-export-filter-latex-environment-functions nil
  1667. "List of functions applied to a transcoded latex-environment.
  1668. Each filter is called with three arguments: the transcoded data,
  1669. as a string, the back-end, as a symbol, and the communication
  1670. channel, as a plist. It must return a string or nil.")
  1671. (defvar org-export-filter-babel-call-functions nil
  1672. "List of functions applied to a transcoded babel-call.
  1673. Each filter is called with three arguments: the transcoded data,
  1674. as a string, the back-end, as a symbol, and the communication
  1675. channel, as a plist. It must return a string or nil.")
  1676. (defvar org-export-filter-paragraph-functions nil
  1677. "List of functions applied to a transcoded paragraph.
  1678. Each filter is called with three arguments: the transcoded data,
  1679. as a string, the back-end, as a symbol, and the communication
  1680. channel, as a plist. It must return a string or nil.")
  1681. (defvar org-export-filter-planning-functions nil
  1682. "List of functions applied to a transcoded planning.
  1683. Each filter is called with three arguments: the transcoded data,
  1684. as a string, the back-end, as a symbol, and the communication
  1685. channel, as a plist. It must return a string or nil.")
  1686. (defvar org-export-filter-property-drawer-functions nil
  1687. "List of functions applied to a transcoded property-drawer.
  1688. Each filter is called with three arguments: the transcoded data,
  1689. as a string, the back-end, as a symbol, and the communication
  1690. channel, as a plist. It must return a string or nil.")
  1691. (defvar org-export-filter-quote-block-functions nil
  1692. "List of functions applied to a transcoded quote block.
  1693. Each filter is called with three arguments: the transcoded quote
  1694. data, as a string, the back-end, as a symbol, and the
  1695. communication channel, as a plist. It must return a string or
  1696. nil.")
  1697. (defvar org-export-filter-quote-section-functions nil
  1698. "List of functions applied to a transcoded quote-section.
  1699. Each filter is called with three arguments: the transcoded data,
  1700. as a string, the back-end, as a symbol, and the communication
  1701. channel, as a plist. It must return a string or nil.")
  1702. (defvar org-export-filter-section-functions nil
  1703. "List of functions applied to a transcoded section.
  1704. Each filter is called with three arguments: the transcoded data,
  1705. as a string, the back-end, as a symbol, and the communication
  1706. channel, as a plist. It must return a string or nil.")
  1707. (defvar org-export-filter-special-block-functions nil
  1708. "List of functions applied to a transcoded special block.
  1709. Each filter is called with three arguments: the transcoded data,
  1710. as a string, the back-end, as a symbol, and the communication
  1711. channel, as a plist. It must return a string or nil.")
  1712. (defvar org-export-filter-src-block-functions nil
  1713. "List of functions applied to a transcoded src-block.
  1714. Each filter is called with three arguments: the transcoded data,
  1715. as a string, the back-end, as a symbol, and the communication
  1716. channel, as a plist. It must return a string or nil.")
  1717. (defvar org-export-filter-table-functions nil
  1718. "List of functions applied to a transcoded table.
  1719. Each filter is called with three arguments: the transcoded data,
  1720. as a string, the back-end, as a symbol, and the communication
  1721. channel, as a plist. It must return a string or nil.")
  1722. (defvar org-export-filter-table-cell-functions nil
  1723. "List of functions applied to a transcoded table-cell.
  1724. Each filter is called with three arguments: the transcoded data,
  1725. as a string, the back-end, as a symbol, and the communication
  1726. channel, as a plist. It must return a string or nil.")
  1727. (defvar org-export-filter-table-row-functions nil
  1728. "List of functions applied to a transcoded table-row.
  1729. Each filter is called with three arguments: the transcoded data,
  1730. as a string, the back-end, as a symbol, and the communication
  1731. channel, as a plist. It must return a string or nil.")
  1732. (defvar org-export-filter-verse-block-functions nil
  1733. "List of functions applied to a transcoded verse block.
  1734. Each filter is called with three arguments: the transcoded data,
  1735. as a string, the back-end, as a symbol, and the communication
  1736. channel, as a plist. It must return a string or nil.")
  1737. ;;;; Objects Filters
  1738. (defvar org-export-filter-bold-functions nil
  1739. "List of functions applied to transcoded bold text.
  1740. Each filter is called with three arguments: the transcoded data,
  1741. as a string, the back-end, as a symbol, and the communication
  1742. channel, as a plist. It must return a string or nil.")
  1743. (defvar org-export-filter-code-functions nil
  1744. "List of functions applied to transcoded code text.
  1745. Each filter is called with three arguments: the transcoded data,
  1746. as a string, the back-end, as a symbol, and the communication
  1747. channel, as a plist. It must return a string or nil.")
  1748. (defvar org-export-filter-entity-functions nil
  1749. "List of functions applied to a transcoded entity.
  1750. Each filter is called with three arguments: the transcoded data,
  1751. as a string, the back-end, as a symbol, and the communication
  1752. channel, as a plist. It must return a string or nil.")
  1753. (defvar org-export-filter-export-snippet-functions nil
  1754. "List of functions applied to a transcoded export-snippet.
  1755. Each filter is called with three arguments: the transcoded data,
  1756. as a string, the back-end, as a symbol, and the communication
  1757. channel, as a plist. It must return a string or nil.")
  1758. (defvar org-export-filter-footnote-reference-functions nil
  1759. "List of functions applied to a transcoded footnote-reference.
  1760. Each filter is called with three arguments: the transcoded data,
  1761. as a string, the back-end, as a symbol, and the communication
  1762. channel, as a plist. It must return a string or nil.")
  1763. (defvar org-export-filter-inline-babel-call-functions nil
  1764. "List of functions applied to a transcoded inline-babel-call.
  1765. Each filter is called with three arguments: the transcoded data,
  1766. as a string, the back-end, as a symbol, and the communication
  1767. channel, as a plist. It must return a string or nil.")
  1768. (defvar org-export-filter-inline-src-block-functions nil
  1769. "List of functions applied to a transcoded inline-src-block.
  1770. Each filter is called with three arguments: the transcoded data,
  1771. as a string, the back-end, as a symbol, and the communication
  1772. channel, as a plist. It must return a string or nil.")
  1773. (defvar org-export-filter-italic-functions nil
  1774. "List of functions applied to transcoded italic text.
  1775. Each filter is called with three arguments: the transcoded data,
  1776. as a string, the back-end, as a symbol, and the communication
  1777. channel, as a plist. It must return a string or nil.")
  1778. (defvar org-export-filter-latex-fragment-functions nil
  1779. "List of functions applied to a transcoded latex-fragment.
  1780. Each filter is called with three arguments: the transcoded data,
  1781. as a string, the back-end, as a symbol, and the communication
  1782. channel, as a plist. It must return a string or nil.")
  1783. (defvar org-export-filter-line-break-functions nil
  1784. "List of functions applied to a transcoded line-break.
  1785. Each filter is called with three arguments: the transcoded data,
  1786. as a string, the back-end, as a symbol, and the communication
  1787. channel, as a plist. It must return a string or nil.")
  1788. (defvar org-export-filter-link-functions nil
  1789. "List of functions applied to a transcoded link.
  1790. Each filter is called with three arguments: the transcoded data,
  1791. as a string, the back-end, as a symbol, and the communication
  1792. channel, as a plist. It must return a string or nil.")
  1793. (defvar org-export-filter-macro-functions nil
  1794. "List of functions applied to a transcoded macro.
  1795. Each filter is called with three arguments: the transcoded data,
  1796. as a string, the back-end, as a symbol, and the communication
  1797. channel, as a plist. It must return a string or nil.")
  1798. (defvar org-export-filter-radio-target-functions nil
  1799. "List of functions applied to a transcoded radio-target.
  1800. Each filter is called with three arguments: the transcoded data,
  1801. as a string, the back-end, as a symbol, and the communication
  1802. channel, as a plist. It must return a string or nil.")
  1803. (defvar org-export-filter-statistics-cookie-functions nil
  1804. "List of functions applied to a transcoded statistics-cookie.
  1805. Each filter is called with three arguments: the transcoded data,
  1806. as a string, the back-end, as a symbol, and the communication
  1807. channel, as a plist. It must return a string or nil.")
  1808. (defvar org-export-filter-strike-through-functions nil
  1809. "List of functions applied to transcoded strike-through text.
  1810. Each filter is called with three arguments: the transcoded data,
  1811. as a string, the back-end, as a symbol, and the communication
  1812. channel, as a plist. It must return a string or nil.")
  1813. (defvar org-export-filter-subscript-functions nil
  1814. "List of functions applied to a transcoded subscript.
  1815. Each filter is called with three arguments: the transcoded data,
  1816. as a string, the back-end, as a symbol, and the communication
  1817. channel, as a plist. It must return a string or nil.")
  1818. (defvar org-export-filter-superscript-functions nil
  1819. "List of functions applied to a transcoded superscript.
  1820. Each filter is called with three arguments: the transcoded data,
  1821. as a string, the back-end, as a symbol, and the communication
  1822. channel, as a plist. It must return a string or nil.")
  1823. (defvar org-export-filter-target-functions nil
  1824. "List of functions applied to a transcoded target.
  1825. Each filter is called with three arguments: the transcoded data,
  1826. as a string, the back-end, as a symbol, and the communication
  1827. channel, as a plist. It must return a string or nil.")
  1828. (defvar org-export-filter-timestamp-functions nil
  1829. "List of functions applied to a transcoded timestamp.
  1830. Each filter is called with three arguments: the transcoded data,
  1831. as a string, the back-end, as a symbol, and the communication
  1832. channel, as a plist. It must return a string or nil.")
  1833. (defvar org-export-filter-underline-functions nil
  1834. "List of functions applied to transcoded underline text.
  1835. Each filter is called with three arguments: the transcoded data,
  1836. as a string, the back-end, as a symbol, and the communication
  1837. channel, as a plist. It must return a string or nil.")
  1838. (defvar org-export-filter-verbatim-functions nil
  1839. "List of functions applied to transcoded verbatim text.
  1840. Each filter is called with three arguments: the transcoded data,
  1841. as a string, the back-end, as a symbol, and the communication
  1842. channel, as a plist. It must return a string or nil.")
  1843. (defun org-export-filter-apply-functions (filters value info)
  1844. "Call every function in FILTERS.
  1845. Functions are called with arguments VALUE, current export
  1846. back-end and INFO. Call is done in a LIFO fashion, to be sure
  1847. that developer specified filters, if any, are called first."
  1848. (loop for filter in filters
  1849. if (not value) return nil else
  1850. do (setq value (funcall filter value (plist-get info :back-end) info)))
  1851. value)
  1852. (defun org-export-install-filters (info)
  1853. "Install filters properties in communication channel.
  1854. INFO is a plist containing the current communication channel.
  1855. Return the updated communication channel."
  1856. (let (plist)
  1857. ;; Install user defined filters with `org-export-filters-alist'.
  1858. (mapc (lambda (p)
  1859. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  1860. org-export-filters-alist)
  1861. ;; Prepend back-end specific filters to that list.
  1862. (let ((back-end-filters (intern (format "org-%s-filters-alist"
  1863. (plist-get info :back-end)))))
  1864. (when (boundp back-end-filters)
  1865. (mapc (lambda (p)
  1866. ;; Single values get consed, lists are prepended.
  1867. (let ((key (car p)) (value (cdr p)))
  1868. (when value
  1869. (setq plist
  1870. (plist-put
  1871. plist key
  1872. (if (atom value) (cons value (plist-get plist key))
  1873. (append value (plist-get plist key))))))))
  1874. (eval back-end-filters))))
  1875. ;; Return new communication channel.
  1876. (org-combine-plists info plist)))
  1877. ;;; Core functions
  1878. ;;
  1879. ;; This is the room for the main function, `org-export-as', along with
  1880. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1881. ;; They differ only by the way they output the resulting code.
  1882. ;;
  1883. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1884. ;; used with `org-export-to-file'. With a given extension, it tries
  1885. ;; to provide a canonical file name to write export output to.
  1886. ;;
  1887. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1888. ;; but a copy of it (with the same buffer-local variables and
  1889. ;; visibility), where include keywords are expanded and Babel blocks
  1890. ;; are executed, if appropriate.
  1891. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1892. ;;
  1893. ;; File inclusion is taken care of by
  1894. ;; `org-export-expand-include-keyword' and
  1895. ;; `org-export-prepare-file-contents'. Structure wise, including
  1896. ;; a whole Org file in a buffer often makes little sense. For
  1897. ;; example, if the file contains an headline and the include keyword
  1898. ;; was within an item, the item should contain the headline. That's
  1899. ;; why file inclusion should be done before any structure can be
  1900. ;; associated to the file, that is before parsing.
  1901. (defun org-export-as
  1902. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  1903. "Transcode current Org buffer into BACKEND code.
  1904. If narrowing is active in the current buffer, only transcode its
  1905. narrowed part.
  1906. If a region is active, transcode that region.
  1907. When optional argument SUBTREEP is non-nil, transcode the
  1908. sub-tree at point, extracting information from the headline
  1909. properties first.
  1910. When optional argument VISIBLE-ONLY is non-nil, don't export
  1911. contents of hidden elements.
  1912. When optional argument BODY-ONLY is non-nil, only return body
  1913. code, without preamble nor postamble.
  1914. Optional argument EXT-PLIST, when provided, is a property list
  1915. with external parameters overriding Org default settings, but
  1916. still inferior to file-local settings.
  1917. Optional argument NOEXPAND, when non-nil, prevents included files
  1918. to be expanded and Babel code to be executed.
  1919. Return code as a string."
  1920. (save-excursion
  1921. (save-restriction
  1922. ;; Narrow buffer to an appropriate region or subtree for
  1923. ;; parsing. If parsing subtree, be sure to remove main headline
  1924. ;; too.
  1925. (cond ((org-region-active-p)
  1926. (narrow-to-region (region-beginning) (region-end)))
  1927. (subtreep
  1928. (org-narrow-to-subtree)
  1929. (goto-char (point-min))
  1930. (forward-line)
  1931. (narrow-to-region (point) (point-max))))
  1932. ;; 1. Get export environment from original buffer. Store
  1933. ;; original footnotes definitions in communication channel as
  1934. ;; they might not be accessible anymore in a narrowed parse
  1935. ;; tree. Also install user's and developer's filters.
  1936. (let ((info (org-export-install-filters
  1937. (org-export-store-footnote-definitions
  1938. (org-export-get-environment backend subtreep ext-plist))))
  1939. ;; 2. Get parse tree. Buffer isn't parsed directly.
  1940. ;; Instead, a temporary copy is created, where include
  1941. ;; keywords are expanded and code blocks are evaluated.
  1942. (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
  1943. (current-buffer))))
  1944. (org-export-with-current-buffer-copy
  1945. (unless noexpand
  1946. (org-export-expand-include-keyword)
  1947. ;; Setting `org-current-export-file' is
  1948. ;; required by Org Babel to properly resolve
  1949. ;; noweb references.
  1950. (let ((org-current-export-file buf))
  1951. (org-export-blocks-preprocess)))
  1952. (goto-char (point-min))
  1953. (run-hooks 'org-export-before-parsing-hook)
  1954. (org-element-parse-buffer nil visible-only)))))
  1955. ;; 3. Call parse-tree filters to get the final tree.
  1956. (setq tree
  1957. (org-export-filter-apply-functions
  1958. (plist-get info :filter-parse-tree) tree info))
  1959. ;; 4. Now tree is complete, compute its properties and add
  1960. ;; them to communication channel.
  1961. (setq info
  1962. (org-combine-plists
  1963. info (org-export-collect-tree-properties tree info)))
  1964. ;; 5. Eventually transcode TREE. Wrap the resulting string
  1965. ;; into a template, if required. Eventually call
  1966. ;; final-output filter.
  1967. (let* ((body (org-element-normalize-string (org-export-data tree info)))
  1968. (template (intern (format "org-%s-template" backend)))
  1969. (output (org-export-filter-apply-functions
  1970. (plist-get info :filter-final-output)
  1971. (if (or (not (fboundp template)) body-only) body
  1972. (funcall template body info))
  1973. info)))
  1974. ;; Maybe add final OUTPUT to kill ring, then return it.
  1975. (when org-export-copy-to-kill-ring (org-kill-new output))
  1976. output)))))
  1977. (defun org-export-to-buffer
  1978. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  1979. "Call `org-export-as' with output to a specified buffer.
  1980. BACKEND is the back-end used for transcoding, as a symbol.
  1981. BUFFER is the output buffer. If it already exists, it will be
  1982. erased first, otherwise, it will be created.
  1983. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1984. and NOEXPAND are similar to those used in `org-export-as', which
  1985. see.
  1986. Return buffer."
  1987. (let ((out (org-export-as
  1988. backend subtreep visible-only body-only ext-plist noexpand))
  1989. (buffer (get-buffer-create buffer)))
  1990. (with-current-buffer buffer
  1991. (erase-buffer)
  1992. (insert out)
  1993. (goto-char (point-min)))
  1994. buffer))
  1995. (defun org-export-to-file
  1996. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  1997. "Call `org-export-as' with output to a specified file.
  1998. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1999. is the name of the output file, as a string.
  2000. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  2001. and NOEXPAND are similar to those used in `org-export-as', which
  2002. see.
  2003. Return output file's name."
  2004. ;; Checks for FILE permissions. `write-file' would do the same, but
  2005. ;; we'd rather avoid needless transcoding of parse tree.
  2006. (unless (file-writable-p file) (error "Output file not writable"))
  2007. ;; Insert contents to a temporary buffer and write it to FILE.
  2008. (let ((out (org-export-as
  2009. backend subtreep visible-only body-only ext-plist noexpand)))
  2010. (with-temp-buffer
  2011. (insert out)
  2012. (let ((coding-system-for-write org-export-coding-system))
  2013. (write-file file))))
  2014. ;; Return full path.
  2015. file)
  2016. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  2017. "Return output file's name according to buffer specifications.
  2018. EXTENSION is a string representing the output file extension,
  2019. with the leading dot.
  2020. With a non-nil optional argument SUBTREEP, try to determine
  2021. output file's name by looking for \"EXPORT_FILE_NAME\" property
  2022. of subtree at point.
  2023. When optional argument PUB-DIR is set, use it as the publishing
  2024. directory.
  2025. Return file name as a string, or nil if it couldn't be
  2026. determined."
  2027. (let ((base-name
  2028. ;; File name may come from EXPORT_FILE_NAME subtree property,
  2029. ;; assuming point is at beginning of said sub-tree.
  2030. (file-name-sans-extension
  2031. (or (and subtreep
  2032. (org-entry-get
  2033. (save-excursion
  2034. (ignore-errors
  2035. (org-back-to-heading (not visible-only)) (point)))
  2036. "EXPORT_FILE_NAME" t))
  2037. ;; File name may be extracted from buffer's associated
  2038. ;; file, if any.
  2039. (buffer-file-name (buffer-base-buffer))
  2040. ;; Can't determine file name on our own: Ask user.
  2041. (let ((read-file-name-function
  2042. (and org-completion-use-ido 'ido-read-file-name)))
  2043. (read-file-name
  2044. "Output file: " pub-dir nil nil nil
  2045. (lambda (name)
  2046. (string= (file-name-extension name t) extension))))))))
  2047. ;; Build file name. Enforce EXTENSION over whatever user may have
  2048. ;; come up with. PUB-DIR, if defined, always has precedence over
  2049. ;; any provided path.
  2050. (cond
  2051. (pub-dir
  2052. (concat (file-name-as-directory pub-dir)
  2053. (file-name-nondirectory base-name)
  2054. extension))
  2055. ((string= (file-name-nondirectory base-name) base-name)
  2056. (concat (file-name-as-directory ".") base-name extension))
  2057. (t (concat base-name extension)))))
  2058. (defmacro org-export-with-current-buffer-copy (&rest body)
  2059. "Apply BODY in a copy of the current buffer.
  2060. The copy preserves local variables and visibility of the original
  2061. buffer.
  2062. Point is at buffer's beginning when BODY is applied."
  2063. (org-with-gensyms (original-buffer offset buffer-string overlays)
  2064. `(let ((,original-buffer ,(current-buffer))
  2065. (,offset ,(1- (point-min)))
  2066. (,buffer-string ,(buffer-string))
  2067. (,overlays (mapcar
  2068. 'copy-overlay (overlays-in (point-min) (point-max)))))
  2069. (with-temp-buffer
  2070. (let ((buffer-invisibility-spec nil))
  2071. (org-clone-local-variables
  2072. ,original-buffer
  2073. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  2074. (insert ,buffer-string)
  2075. (mapc (lambda (ov)
  2076. (move-overlay
  2077. ov
  2078. (- (overlay-start ov) ,offset)
  2079. (- (overlay-end ov) ,offset)
  2080. (current-buffer)))
  2081. ,overlays)
  2082. (goto-char (point-min))
  2083. (progn ,@body))))))
  2084. (def-edebug-spec org-export-with-current-buffer-copy (body))
  2085. (defun org-export-expand-include-keyword (&optional included dir)
  2086. "Expand every include keyword in buffer.
  2087. Optional argument INCLUDED is a list of included file names along
  2088. with their line restriction, when appropriate. It is used to
  2089. avoid infinite recursion. Optional argument DIR is the current
  2090. working directory. It is used to properly resolve relative
  2091. paths."
  2092. (let ((case-fold-search t))
  2093. (goto-char (point-min))
  2094. (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
  2095. (when (eq (org-element-type (save-match-data (org-element-at-point)))
  2096. 'keyword)
  2097. (beginning-of-line)
  2098. ;; Extract arguments from keyword's value.
  2099. (let* ((value (match-string 1))
  2100. (ind (org-get-indentation))
  2101. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  2102. (prog1 (expand-file-name (match-string 1 value) dir)
  2103. (setq value (replace-match "" nil nil value)))))
  2104. (lines
  2105. (and (string-match
  2106. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  2107. (prog1 (match-string 1 value)
  2108. (setq value (replace-match "" nil nil value)))))
  2109. (env (cond ((string-match "\\<example\\>" value) 'example)
  2110. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2111. (match-string 1 value))))
  2112. ;; Minimal level of included file defaults to the child
  2113. ;; level of the current headline, if any, or one. It
  2114. ;; only applies is the file is meant to be included as
  2115. ;; an Org one.
  2116. (minlevel
  2117. (and (not env)
  2118. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2119. (prog1 (string-to-number (match-string 1 value))
  2120. (setq value (replace-match "" nil nil value)))
  2121. (let ((cur (org-current-level)))
  2122. (if cur (1+ (org-reduced-level cur)) 1))))))
  2123. ;; Remove keyword.
  2124. (delete-region (point) (progn (forward-line) (point)))
  2125. (cond
  2126. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  2127. ;; Check if files has already been parsed. Look after
  2128. ;; inclusion lines too, as different parts of the same file
  2129. ;; can be included too.
  2130. ((member (list file lines) included)
  2131. (error "Recursive file inclusion: %s" file))
  2132. (t
  2133. (cond
  2134. ((eq env 'example)
  2135. (insert
  2136. (let ((ind-str (make-string ind ? ))
  2137. (contents
  2138. ;; Protect sensitive contents with commas.
  2139. (replace-regexp-in-string
  2140. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  2141. (org-export-prepare-file-contents file lines)
  2142. nil nil 1)))
  2143. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2144. ind-str contents ind-str))))
  2145. ((stringp env)
  2146. (insert
  2147. (let ((ind-str (make-string ind ? ))
  2148. (contents
  2149. ;; Protect sensitive contents with commas.
  2150. (replace-regexp-in-string
  2151. (if (string= env "org") "\\(^\\)\\(.\\)"
  2152. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  2153. (org-export-prepare-file-contents file lines)
  2154. nil nil 1)))
  2155. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2156. ind-str env contents ind-str))))
  2157. (t
  2158. (insert
  2159. (with-temp-buffer
  2160. (org-mode)
  2161. (insert
  2162. (org-export-prepare-file-contents file lines ind minlevel))
  2163. (org-export-expand-include-keyword
  2164. (cons (list file lines) included)
  2165. (file-name-directory file))
  2166. (buffer-string))))))))))))
  2167. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  2168. "Prepare the contents of FILE for inclusion and return them as a string.
  2169. When optional argument LINES is a string specifying a range of
  2170. lines, include only those lines.
  2171. Optional argument IND, when non-nil, is an integer specifying the
  2172. global indentation of returned contents. Since its purpose is to
  2173. allow an included file to stay in the same environment it was
  2174. created \(i.e. a list item), it doesn't apply past the first
  2175. headline encountered.
  2176. Optional argument MINLEVEL, when non-nil, is an integer
  2177. specifying the level that any top-level headline in the included
  2178. file should have."
  2179. (with-temp-buffer
  2180. (insert-file-contents file)
  2181. (when lines
  2182. (let* ((lines (split-string lines "-"))
  2183. (lbeg (string-to-number (car lines)))
  2184. (lend (string-to-number (cadr lines)))
  2185. (beg (if (zerop lbeg) (point-min)
  2186. (goto-char (point-min))
  2187. (forward-line (1- lbeg))
  2188. (point)))
  2189. (end (if (zerop lend) (point-max)
  2190. (goto-char (point-min))
  2191. (forward-line (1- lend))
  2192. (point))))
  2193. (narrow-to-region beg end)))
  2194. ;; Remove blank lines at beginning and end of contents. The logic
  2195. ;; behind that removal is that blank lines around include keyword
  2196. ;; override blank lines in included file.
  2197. (goto-char (point-min))
  2198. (org-skip-whitespace)
  2199. (beginning-of-line)
  2200. (delete-region (point-min) (point))
  2201. (goto-char (point-max))
  2202. (skip-chars-backward " \r\t\n")
  2203. (forward-line)
  2204. (delete-region (point) (point-max))
  2205. ;; If IND is set, preserve indentation of include keyword until
  2206. ;; the first headline encountered.
  2207. (when ind
  2208. (unless (eq major-mode 'org-mode) (org-mode))
  2209. (goto-char (point-min))
  2210. (let ((ind-str (make-string ind ? )))
  2211. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2212. ;; Do not move footnote definitions out of column 0.
  2213. (unless (and (looking-at org-footnote-definition-re)
  2214. (eq (org-element-type (org-element-at-point))
  2215. 'footnote-definition))
  2216. (insert ind-str))
  2217. (forward-line))))
  2218. ;; When MINLEVEL is specified, compute minimal level for headlines
  2219. ;; in the file (CUR-MIN), and remove stars to each headline so
  2220. ;; that headlines with minimal level have a level of MINLEVEL.
  2221. (when minlevel
  2222. (unless (eq major-mode 'org-mode) (org-mode))
  2223. (let ((levels (org-map-entries
  2224. (lambda () (org-reduced-level (org-current-level))))))
  2225. (when levels
  2226. (let ((offset (- minlevel (apply 'min levels))))
  2227. (unless (zerop offset)
  2228. (when org-odd-levels-only (setq offset (* offset 2)))
  2229. ;; Only change stars, don't bother moving whole
  2230. ;; sections.
  2231. (org-map-entries
  2232. (lambda () (if (< offset 0) (delete-char (abs offset))
  2233. (insert (make-string offset ?*))))))))))
  2234. (buffer-string)))
  2235. ;;; Tools For Back-Ends
  2236. ;;
  2237. ;; A whole set of tools is available to help build new exporters. Any
  2238. ;; function general enough to have its use across many back-ends
  2239. ;; should be added here.
  2240. ;;
  2241. ;; As of now, functions operating on footnotes, headlines, links,
  2242. ;; macros, references, src-blocks, tables and tables of contents are
  2243. ;; implemented.
  2244. ;;;; For Export Snippets
  2245. ;;
  2246. ;; Every export snippet is transmitted to the back-end. Though, the
  2247. ;; latter will only retain one type of export-snippet, ignoring
  2248. ;; others, based on the former's target back-end. The function
  2249. ;; `org-export-snippet-backend' returns that back-end for a given
  2250. ;; export-snippet.
  2251. (defun org-export-snippet-backend (export-snippet)
  2252. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  2253. Translation, with `org-export-snippet-translation-alist', is
  2254. applied."
  2255. (let ((back-end (org-element-property :back-end export-snippet)))
  2256. (intern
  2257. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  2258. back-end))))
  2259. ;;;; For Footnotes
  2260. ;;
  2261. ;; `org-export-collect-footnote-definitions' is a tool to list
  2262. ;; actually used footnotes definitions in the whole parse tree, or in
  2263. ;; an headline, in order to add footnote listings throughout the
  2264. ;; transcoded data.
  2265. ;;
  2266. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  2267. ;; back-ends, when they need to attach the footnote definition only to
  2268. ;; the first occurrence of the corresponding label.
  2269. ;;
  2270. ;; `org-export-get-footnote-definition' and
  2271. ;; `org-export-get-footnote-number' provide easier access to
  2272. ;; additional information relative to a footnote reference.
  2273. (defun org-export-collect-footnote-definitions (data info)
  2274. "Return an alist between footnote numbers, labels and definitions.
  2275. DATA is the parse tree from which definitions are collected.
  2276. INFO is the plist used as a communication channel.
  2277. Definitions are sorted by order of references. They either
  2278. appear as Org data or as a secondary string for inlined
  2279. footnotes. Unreferenced definitions are ignored."
  2280. (let (num-alist
  2281. (collect-fn
  2282. (function
  2283. (lambda (data)
  2284. ;; Collect footnote number, label and definition in DATA.
  2285. (org-element-map
  2286. data 'footnote-reference
  2287. (lambda (fn)
  2288. (when (org-export-footnote-first-reference-p fn info)
  2289. (let ((def (org-export-get-footnote-definition fn info)))
  2290. (push
  2291. (list (org-export-get-footnote-number fn info)
  2292. (org-element-property :label fn)
  2293. def)
  2294. num-alist)
  2295. ;; Also search in definition for nested footnotes.
  2296. (when (eq (org-element-property :type fn) 'standard)
  2297. (funcall collect-fn def)))))
  2298. ;; Don't enter footnote definitions since it will happen
  2299. ;; when their first reference is found.
  2300. info nil 'footnote-definition)))))
  2301. (funcall collect-fn (plist-get info :parse-tree))
  2302. (reverse num-alist)))
  2303. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2304. "Non-nil when a footnote reference is the first one for its label.
  2305. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2306. INFO is the plist used as a communication channel."
  2307. (let ((label (org-element-property :label footnote-reference)))
  2308. ;; Anonymous footnotes are always a first reference.
  2309. (if (not label) t
  2310. ;; Otherwise, return the first footnote with the same LABEL and
  2311. ;; test if it is equal to FOOTNOTE-REFERENCE.
  2312. (let ((search-refs
  2313. (function
  2314. (lambda (data)
  2315. (org-element-map
  2316. data 'footnote-reference
  2317. (lambda (fn)
  2318. (cond
  2319. ((string= (org-element-property :label fn) label)
  2320. (throw 'exit fn))
  2321. ;; If FN isn't inlined, be sure to traverse its
  2322. ;; definition before resuming search. See
  2323. ;; comments in `org-export-get-footnote-number'
  2324. ;; for more information.
  2325. ((eq (org-element-property :type fn) 'standard)
  2326. (funcall search-refs
  2327. (org-export-get-footnote-definition fn info)))))
  2328. ;; Don't enter footnote definitions since it will
  2329. ;; happen when their first reference is found.
  2330. info 'first-match 'footnote-definition)))))
  2331. (equal (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  2332. footnote-reference)))))
  2333. (defun org-export-get-footnote-definition (footnote-reference info)
  2334. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2335. INFO is the plist used as a communication channel."
  2336. (let ((label (org-element-property :label footnote-reference)))
  2337. (or (org-element-property :inline-definition footnote-reference)
  2338. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2339. (defun org-export-get-footnote-number (footnote info)
  2340. "Return number associated to a footnote.
  2341. FOOTNOTE is either a footnote reference or a footnote definition.
  2342. INFO is the plist used as a communication channel."
  2343. (let ((label (org-element-property :label footnote))
  2344. seen-refs
  2345. (search-ref
  2346. (function
  2347. (lambda (data)
  2348. ;; Search footnote references through DATA, filling
  2349. ;; SEEN-REFS along the way.
  2350. (org-element-map
  2351. data 'footnote-reference
  2352. (lambda (fn)
  2353. (let ((fn-lbl (org-element-property :label fn)))
  2354. (cond
  2355. ;; Anonymous footnote match: return number.
  2356. ((and (not fn-lbl) (equal fn footnote))
  2357. (throw 'exit (1+ (length seen-refs))))
  2358. ;; Labels match: return number.
  2359. ((and label (string= label fn-lbl))
  2360. (throw 'exit (1+ (length seen-refs))))
  2361. ;; Anonymous footnote: it's always a new one. Also,
  2362. ;; be sure to return nil from the `cond' so
  2363. ;; `first-match' doesn't get us out of the loop.
  2364. ((not fn-lbl) (push 'inline seen-refs) nil)
  2365. ;; Label not seen so far: add it so SEEN-REFS.
  2366. ;;
  2367. ;; Also search for subsequent references in footnote
  2368. ;; definition so numbering following reading logic.
  2369. ;; Note that we don't have to care about inline
  2370. ;; definitions, since `org-element-map' already
  2371. ;; traverse them at the right time.
  2372. ;;
  2373. ;; Once again, return nil to stay in the loop.
  2374. ((not (member fn-lbl seen-refs))
  2375. (push fn-lbl seen-refs)
  2376. (funcall search-ref
  2377. (org-export-get-footnote-definition fn info))
  2378. nil))))
  2379. ;; Don't enter footnote definitions since it will happen
  2380. ;; when their first reference is found.
  2381. info 'first-match 'footnote-definition)))))
  2382. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  2383. ;;;; For Headlines
  2384. ;;
  2385. ;; `org-export-get-relative-level' is a shortcut to get headline
  2386. ;; level, relatively to the lower headline level in the parsed tree.
  2387. ;;
  2388. ;; `org-export-get-headline-number' returns the section number of an
  2389. ;; headline, while `org-export-number-to-roman' allows to convert it
  2390. ;; to roman numbers.
  2391. ;;
  2392. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2393. ;; `org-export-last-sibling-p' are three useful predicates when it
  2394. ;; comes to fulfill the `:headline-levels' property.
  2395. (defun org-export-get-relative-level (headline info)
  2396. "Return HEADLINE relative level within current parsed tree.
  2397. INFO is a plist holding contextual information."
  2398. (+ (org-element-property :level headline)
  2399. (or (plist-get info :headline-offset) 0)))
  2400. (defun org-export-low-level-p (headline info)
  2401. "Non-nil when HEADLINE is considered as low level.
  2402. INFO is a plist used as a communication channel.
  2403. A low level headlines has a relative level greater than
  2404. `:headline-levels' property value.
  2405. Return value is the difference between HEADLINE relative level
  2406. and the last level being considered as high enough, or nil."
  2407. (let ((limit (plist-get info :headline-levels)))
  2408. (when (wholenump limit)
  2409. (let ((level (org-export-get-relative-level headline info)))
  2410. (and (> level limit) (- level limit))))))
  2411. (defun org-export-get-headline-number (headline info)
  2412. "Return HEADLINE numbering as a list of numbers.
  2413. INFO is a plist holding contextual information."
  2414. (cdr (assoc headline (plist-get info :headline-numbering))))
  2415. (defun org-export-numbered-headline-p (headline info)
  2416. "Return a non-nil value if HEADLINE element should be numbered.
  2417. INFO is a plist used as a communication channel."
  2418. (let ((sec-num (plist-get info :section-numbers))
  2419. (level (org-export-get-relative-level headline info)))
  2420. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  2421. (defun org-export-number-to-roman (n)
  2422. "Convert integer N into a roman numeral."
  2423. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2424. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2425. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2426. ( 1 . "I")))
  2427. (res ""))
  2428. (if (<= n 0)
  2429. (number-to-string n)
  2430. (while roman
  2431. (if (>= n (caar roman))
  2432. (setq n (- n (caar roman))
  2433. res (concat res (cdar roman)))
  2434. (pop roman)))
  2435. res)))
  2436. (defun org-export-first-sibling-p (headline info)
  2437. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2438. INFO is the plist used as a communication channel."
  2439. (not (eq (org-element-type (org-export-get-previous-element headline info))
  2440. 'headline)))
  2441. (defun org-export-last-sibling-p (headline info)
  2442. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2443. INFO is the plist used as a communication channel."
  2444. (not (org-export-get-next-element headline info)))
  2445. ;;;; For Links
  2446. ;;
  2447. ;; `org-export-solidify-link-text' turns a string into a safer version
  2448. ;; for links, replacing most non-standard characters with hyphens.
  2449. ;;
  2450. ;; `org-export-get-coderef-format' returns an appropriate format
  2451. ;; string for coderefs.
  2452. ;;
  2453. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2454. ;; provided should be considered as an inline image.
  2455. ;;
  2456. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2457. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2458. ;; returns an appropriate unique identifier when found, or nil.
  2459. ;;
  2460. ;; `org-export-resolve-id-link' returns the first headline with
  2461. ;; specified id or custom-id in parse tree, or nil when none was
  2462. ;; found.
  2463. ;;
  2464. ;; `org-export-resolve-coderef' associates a reference to a line
  2465. ;; number in the element it belongs, or returns the reference itself
  2466. ;; when the element isn't numbered.
  2467. (defun org-export-solidify-link-text (s)
  2468. "Take link text S and make a safe target out of it."
  2469. (save-match-data
  2470. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2471. (defun org-export-get-coderef-format (path desc)
  2472. "Return format string for code reference link.
  2473. PATH is the link path. DESC is its description."
  2474. (save-match-data
  2475. (cond ((not desc) "%s")
  2476. ((string-match (regexp-quote (concat "(" path ")")) desc)
  2477. (replace-match "%s" t t desc))
  2478. (t desc))))
  2479. (defun org-export-inline-image-p (link &optional rules)
  2480. "Non-nil if LINK object points to an inline image.
  2481. Optional argument is a set of RULES defining inline images. It
  2482. is an alist where associations have the following shape:
  2483. \(TYPE . REGEXP)
  2484. Applying a rule means apply REGEXP against LINK's path when its
  2485. type is TYPE. The function will return a non-nil value if any of
  2486. the provided rules is non-nil. The default rule is
  2487. `org-export-default-inline-image-rule'.
  2488. This only applies to links without a description."
  2489. (and (not (org-element-contents link))
  2490. (let ((case-fold-search t)
  2491. (rules (or rules org-export-default-inline-image-rule)))
  2492. (some
  2493. (lambda (rule)
  2494. (and (string= (org-element-property :type link) (car rule))
  2495. (string-match (cdr rule)
  2496. (org-element-property :path link))))
  2497. rules))))
  2498. (defun org-export-resolve-fuzzy-link (link info)
  2499. "Return LINK destination.
  2500. INFO is a plist holding contextual information.
  2501. Return value can be an object, an element, or nil:
  2502. - If LINK path matches a target object (i.e. <<path>>) or
  2503. element (i.e. \"#+TARGET: path\"), return it.
  2504. - If LINK path exactly matches the name affiliated keyword
  2505. \(i.e. #+NAME: path) of an element, return that element.
  2506. - If LINK path exactly matches any headline name, return that
  2507. element. If more than one headline share that name, priority
  2508. will be given to the one with the closest common ancestor, if
  2509. any, or the first one in the parse tree otherwise.
  2510. - Otherwise, return nil.
  2511. Assume LINK type is \"fuzzy\"."
  2512. (let ((path (org-element-property :path link)))
  2513. (cond
  2514. ;; First try to find a matching "<<path>>" unless user specified
  2515. ;; he was looking for an headline (path starts with a *
  2516. ;; character).
  2517. ((and (not (eq (substring path 0 1) ?*))
  2518. (loop for target in (plist-get info :target-list)
  2519. when (string= (org-element-property :value target) path)
  2520. return target)))
  2521. ;; Then try to find an element with a matching "#+NAME: path"
  2522. ;; affiliated keyword.
  2523. ((and (not (eq (substring path 0 1) ?*))
  2524. (org-element-map
  2525. (plist-get info :parse-tree) org-element-all-elements
  2526. (lambda (el)
  2527. (when (string= (org-element-property :name el) path) el))
  2528. info 'first-match)))
  2529. ;; Last case: link either points to an headline or to
  2530. ;; nothingness. Try to find the source, with priority given to
  2531. ;; headlines with the closest common ancestor. If such candidate
  2532. ;; is found, return its beginning position as an unique
  2533. ;; identifier, otherwise return nil.
  2534. (t
  2535. (let ((find-headline
  2536. (function
  2537. ;; Return first headline whose `:raw-value' property
  2538. ;; is NAME in parse tree DATA, or nil.
  2539. (lambda (name data)
  2540. (org-element-map
  2541. data 'headline
  2542. (lambda (headline)
  2543. (when (string=
  2544. (org-element-property :raw-value headline)
  2545. name)
  2546. headline))
  2547. info 'first-match)))))
  2548. ;; Search among headlines sharing an ancestor with link,
  2549. ;; from closest to farthest.
  2550. (or (catch 'exit
  2551. (mapc
  2552. (lambda (parent)
  2553. (when (eq (org-element-type parent) 'headline)
  2554. (let ((foundp (funcall find-headline path parent)))
  2555. (when foundp (throw 'exit foundp)))))
  2556. (org-export-get-genealogy link info)) nil)
  2557. ;; No match with a common ancestor: try the full parse-tree.
  2558. (funcall find-headline path (plist-get info :parse-tree))))))))
  2559. (defun org-export-resolve-id-link (link info)
  2560. "Return headline referenced as LINK destination.
  2561. INFO is a plist used as a communication channel.
  2562. Return value can be an headline element or nil. Assume LINK type
  2563. is either \"id\" or \"custom-id\"."
  2564. (let ((id (org-element-property :path link)))
  2565. (org-element-map
  2566. (plist-get info :parse-tree) 'headline
  2567. (lambda (headline)
  2568. (when (or (string= (org-element-property :id headline) id)
  2569. (string= (org-element-property :custom-id headline) id))
  2570. headline))
  2571. info 'first-match)))
  2572. (defun org-export-resolve-coderef (ref info)
  2573. "Resolve a code reference REF.
  2574. INFO is a plist used as a communication channel.
  2575. Return associated line number in source code, or REF itself,
  2576. depending on src-block or example element's switches."
  2577. (org-element-map
  2578. (plist-get info :parse-tree) '(example-block src-block)
  2579. (lambda (el)
  2580. (with-temp-buffer
  2581. (insert (org-trim (org-element-property :value el)))
  2582. (let* ((label-fmt (regexp-quote
  2583. (or (org-element-property :label-fmt el)
  2584. org-coderef-label-format)))
  2585. (ref-re
  2586. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2587. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  2588. ;; Element containing REF is found. Resolve it to either
  2589. ;; a label or a line number, as needed.
  2590. (when (re-search-backward ref-re nil t)
  2591. (cond
  2592. ((org-element-property :use-labels el) ref)
  2593. ((eq (org-element-property :number-lines el) 'continued)
  2594. (+ (org-export-get-loc el info) (line-number-at-pos)))
  2595. (t (line-number-at-pos)))))))
  2596. info 'first-match))
  2597. ;;;; For Macros
  2598. ;;
  2599. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2600. (defun org-export-expand-macro (macro info)
  2601. "Expand MACRO and return it as a string.
  2602. INFO is a plist holding export options."
  2603. (let* ((key (org-element-property :key macro))
  2604. (args (org-element-property :args macro))
  2605. ;; User's macros are stored in the communication channel with
  2606. ;; a ":macro-" prefix.
  2607. (value (org-export-data
  2608. (plist-get info (intern (format ":macro-%s" key))) val info)))
  2609. ;; Replace arguments in VALUE.
  2610. (let ((s 0) n)
  2611. (while (string-match "\\$\\([0-9]+\\)" value s)
  2612. (setq s (1+ (match-beginning 0))
  2613. n (string-to-number (match-string 1 value)))
  2614. (and (>= (length args) n)
  2615. (setq value (replace-match (nth (1- n) args) t t value)))))
  2616. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2617. (when (string-match "\\`(eval\\>" value)
  2618. (setq value (eval (read value))))
  2619. ;; Return string.
  2620. (format "%s" (or value ""))))
  2621. ;;;; For References
  2622. ;;
  2623. ;; `org-export-get-ordinal' associates a sequence number to any object
  2624. ;; or element.
  2625. (defun org-export-get-ordinal (element info &optional types predicate)
  2626. "Return ordinal number of an element or object.
  2627. ELEMENT is the element or object considered. INFO is the plist
  2628. used as a communication channel.
  2629. Optional argument TYPES, when non-nil, is a list of element or
  2630. object types, as symbols, that should also be counted in.
  2631. Otherwise, only provided element's type is considered.
  2632. Optional argument PREDICATE is a function returning a non-nil
  2633. value if the current element or object should be counted in. It
  2634. accepts two arguments: the element or object being considered and
  2635. the plist used as a communication channel. This allows to count
  2636. only a certain type of objects (i.e. inline images).
  2637. Return value is a list of numbers if ELEMENT is an headline or an
  2638. item. It is nil for keywords. It represents the footnote number
  2639. for footnote definitions and footnote references. If ELEMENT is
  2640. a target, return the same value as if ELEMENT was the closest
  2641. table, item or headline containing the target. In any other
  2642. case, return the sequence number of ELEMENT among elements or
  2643. objects of the same type."
  2644. ;; A target keyword, representing an invisible target, never has
  2645. ;; a sequence number.
  2646. (unless (eq (org-element-type element) 'keyword)
  2647. ;; Ordinal of a target object refer to the ordinal of the closest
  2648. ;; table, item, or headline containing the object.
  2649. (when (eq (org-element-type element) 'target)
  2650. (setq element
  2651. (loop for parent in (org-export-get-genealogy element info)
  2652. when
  2653. (memq
  2654. (org-element-type parent)
  2655. '(footnote-definition footnote-reference headline item
  2656. table))
  2657. return parent)))
  2658. (case (org-element-type element)
  2659. ;; Special case 1: An headline returns its number as a list.
  2660. (headline (org-export-get-headline-number element info))
  2661. ;; Special case 2: An item returns its number as a list.
  2662. (item (let ((struct (org-element-property :structure element)))
  2663. (org-list-get-item-number
  2664. (org-element-property :begin element)
  2665. struct
  2666. (org-list-prevs-alist struct)
  2667. (org-list-parents-alist struct))))
  2668. ((footnote definition footnote-reference)
  2669. (org-export-get-footnote-number element info))
  2670. (otherwise
  2671. (let ((counter 0))
  2672. ;; Increment counter until ELEMENT is found again.
  2673. (org-element-map
  2674. (plist-get info :parse-tree) (or types (org-element-type element))
  2675. (lambda (el)
  2676. (cond
  2677. ((equal element el) (1+ counter))
  2678. ((not predicate) (incf counter) nil)
  2679. ((funcall predicate el info) (incf counter) nil)))
  2680. info 'first-match))))))
  2681. ;;;; For Src-Blocks
  2682. ;;
  2683. ;; `org-export-get-loc' counts number of code lines accumulated in
  2684. ;; src-block or example-block elements with a "+n" switch until
  2685. ;; a given element, excluded. Note: "-n" switches reset that count.
  2686. ;;
  2687. ;; `org-export-unravel-code' extracts source code (along with a code
  2688. ;; references alist) from an `element-block' or `src-block' type
  2689. ;; element.
  2690. ;;
  2691. ;; `org-export-format-code' applies a formatting function to each line
  2692. ;; of code, providing relative line number and code reference when
  2693. ;; appropriate. Since it doesn't access the original element from
  2694. ;; which the source code is coming, it expects from the code calling
  2695. ;; it to know if lines should be numbered and if code references
  2696. ;; should appear.
  2697. ;;
  2698. ;; Eventually, `org-export-format-code-default' is a higher-level
  2699. ;; function (it makes use of the two previous functions) which handles
  2700. ;; line numbering and code references inclusion, and returns source
  2701. ;; code in a format suitable for plain text or verbatim output.
  2702. (defun org-export-get-loc (element info)
  2703. "Return accumulated lines of code up to ELEMENT.
  2704. INFO is the plist used as a communication channel.
  2705. ELEMENT is excluded from count."
  2706. (let ((loc 0))
  2707. (org-element-map
  2708. (plist-get info :parse-tree)
  2709. `(src-block example-block ,(org-element-type element))
  2710. (lambda (el)
  2711. (cond
  2712. ;; ELEMENT is reached: Quit the loop.
  2713. ((equal el element) t)
  2714. ;; Only count lines from src-block and example-block elements
  2715. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2716. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  2717. ((let ((linums (org-element-property :number-lines el)))
  2718. (when linums
  2719. ;; Accumulate locs or reset them.
  2720. (let ((lines (org-count-lines
  2721. (org-trim (org-element-property :value el)))))
  2722. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  2723. ;; Return nil to stay in the loop.
  2724. nil)))
  2725. info 'first-match)
  2726. ;; Return value.
  2727. loc))
  2728. (defun org-export-unravel-code (element)
  2729. "Clean source code and extract references out of it.
  2730. ELEMENT has either a `src-block' an `example-block' type.
  2731. Return a cons cell whose CAR is the source code, cleaned from any
  2732. reference and protective comma and CDR is an alist between
  2733. relative line number (integer) and name of code reference on that
  2734. line (string)."
  2735. (let* ((line 0) refs
  2736. ;; Get code and clean it. Remove blank lines at its
  2737. ;; beginning and end. Also remove protective commas.
  2738. (code (let ((c (replace-regexp-in-string
  2739. "\\`\\([ \t]*\n\\)+" ""
  2740. (replace-regexp-in-string
  2741. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
  2742. (org-element-property :value element)))))
  2743. ;; If appropriate, remove global indentation.
  2744. (unless (or org-src-preserve-indentation
  2745. (org-element-property :preserve-indent element))
  2746. (setq c (org-remove-indentation c)))
  2747. ;; Free up the protected lines. Note: Org blocks
  2748. ;; have commas at the beginning or every line.
  2749. (if (string= (org-element-property :language element) "org")
  2750. (replace-regexp-in-string "^," "" c)
  2751. (replace-regexp-in-string
  2752. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2753. ;; Get format used for references.
  2754. (label-fmt (regexp-quote
  2755. (or (org-element-property :label-fmt element)
  2756. org-coderef-label-format)))
  2757. ;; Build a regexp matching a loc with a reference.
  2758. (with-ref-re
  2759. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  2760. (replace-regexp-in-string
  2761. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2762. ;; Return value.
  2763. (cons
  2764. ;; Code with references removed.
  2765. (org-element-normalize-string
  2766. (mapconcat
  2767. (lambda (loc)
  2768. (incf line)
  2769. (if (not (string-match with-ref-re loc)) loc
  2770. ;; Ref line: remove ref, and signal its position in REFS.
  2771. (push (cons line (match-string 3 loc)) refs)
  2772. (replace-match "" nil nil loc 1)))
  2773. (org-split-string code "\n") "\n"))
  2774. ;; Reference alist.
  2775. refs)))
  2776. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  2777. "Format CODE by applying FUN line-wise and return it.
  2778. CODE is a string representing the code to format. FUN is
  2779. a function. It must accept three arguments: a line of
  2780. code (string), the current line number (integer) or nil and the
  2781. reference associated to the current line (string) or nil.
  2782. Optional argument NUM-LINES can be an integer representing the
  2783. number of code lines accumulated until the current code. Line
  2784. numbers passed to FUN will take it into account. If it is nil,
  2785. FUN's second argument will always be nil. This number can be
  2786. obtained with `org-export-get-loc' function.
  2787. Optional argument REF-ALIST can be an alist between relative line
  2788. number (i.e. ignoring NUM-LINES) and the name of the code
  2789. reference on it. If it is nil, FUN's third argument will always
  2790. be nil. It can be obtained through the use of
  2791. `org-export-unravel-code' function."
  2792. (let ((--locs (org-split-string code "\n"))
  2793. (--line 0))
  2794. (org-element-normalize-string
  2795. (mapconcat
  2796. (lambda (--loc)
  2797. (incf --line)
  2798. (let ((--ref (cdr (assq --line ref-alist))))
  2799. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  2800. --locs "\n"))))
  2801. (defun org-export-format-code-default (element info)
  2802. "Return source code from ELEMENT, formatted in a standard way.
  2803. ELEMENT is either a `src-block' or `example-block' element. INFO
  2804. is a plist used as a communication channel.
  2805. This function takes care of line numbering and code references
  2806. inclusion. Line numbers, when applicable, appear at the
  2807. beginning of the line, separated from the code by two white
  2808. spaces. Code references, on the other hand, appear flushed to
  2809. the right, separated by six white spaces from the widest line of
  2810. code."
  2811. ;; Extract code and references.
  2812. (let* ((code-info (org-export-unravel-code element))
  2813. (code (car code-info))
  2814. (code-lines (org-split-string code "\n"))
  2815. (refs (and (org-element-property :retain-labels element)
  2816. (cdr code-info)))
  2817. ;; Handle line numbering.
  2818. (num-start (case (org-element-property :number-lines element)
  2819. (continued (org-export-get-loc element info))
  2820. (new 0)))
  2821. (num-fmt
  2822. (and num-start
  2823. (format "%%%ds "
  2824. (length (number-to-string
  2825. (+ (length code-lines) num-start))))))
  2826. ;; Prepare references display, if required. Any reference
  2827. ;; should start six columns after the widest line of code,
  2828. ;; wrapped with parenthesis.
  2829. (max-width
  2830. (+ (apply 'max (mapcar 'length code-lines))
  2831. (if (not num-start) 0 (length (format num-fmt num-start))))))
  2832. (org-export-format-code
  2833. code
  2834. (lambda (loc line-num ref)
  2835. (let ((number-str (and num-fmt (format num-fmt line-num))))
  2836. (concat
  2837. number-str
  2838. loc
  2839. (and ref
  2840. (concat (make-string
  2841. (- (+ 6 max-width)
  2842. (+ (length loc) (length number-str))) ? )
  2843. (format "(%s)" ref))))))
  2844. num-start refs)))
  2845. ;;;; For Tables
  2846. ;;
  2847. ;; `org-export-table-has-special-column-p' and
  2848. ;; `org-export-table-row-is-special-p' are predicates used to look for
  2849. ;; meta-information about the table structure.
  2850. ;;
  2851. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  2852. ;; and `org-export-table-cell-borders' extract information from
  2853. ;; a table-cell element.
  2854. ;;
  2855. ;; `org-export-table-dimensions' gives the number on rows and columns
  2856. ;; in the table, ignoring horizontal rules and special columns.
  2857. ;; `org-export-table-cell-address', given a table-cell object, returns
  2858. ;; the absolute address of a cell. On the other hand,
  2859. ;; `org-export-get-table-cell-at' does the contrary.
  2860. (defun org-export-table-has-special-column-p (table)
  2861. "Non-nil when TABLE has a special column.
  2862. All special columns will be ignored during export."
  2863. ;; The table has a special column when every first cell of every row
  2864. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  2865. ;; "*" "_" and "^". Though, do not consider a first row containing
  2866. ;; only empty cells as special.
  2867. (let ((special-column-p 'empty))
  2868. (catch 'exit
  2869. (mapc
  2870. (lambda (row)
  2871. (when (eq (org-element-property :type row) 'standard)
  2872. (let ((value (org-element-contents
  2873. (car (org-element-contents row)))))
  2874. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  2875. (setq special-column-p 'special))
  2876. ((not value))
  2877. (t (throw 'exit nil))))))
  2878. (org-element-contents table))
  2879. (eq special-column-p 'special))))
  2880. (defun org-export-table-has-header-p (table info)
  2881. "Non-nil when TABLE has an header.
  2882. INFO is a plist used as a communication channel.
  2883. A table has an header when it contains at least two row groups."
  2884. (let ((rowgroup 1) row-flag)
  2885. (org-element-map
  2886. table 'table-row
  2887. (lambda (row)
  2888. (cond
  2889. ((> rowgroup 1) t)
  2890. ((and row-flag (eq (org-element-property :type row) 'rule))
  2891. (incf rowgroup) (setq row-flag nil))
  2892. ((and (not row-flag) (eq (org-element-property :type row) 'standard))
  2893. (setq row-flag t) nil)))
  2894. info)))
  2895. (defun org-export-table-row-is-special-p (table-row info)
  2896. "Non-nil if TABLE-ROW is considered special.
  2897. INFO is a plist used as the communication channel.
  2898. All special rows will be ignored during export."
  2899. (when (eq (org-element-property :type table-row) 'standard)
  2900. (let ((first-cell (org-element-contents
  2901. (car (org-element-contents table-row)))))
  2902. ;; A row is special either when...
  2903. (or
  2904. ;; ... it starts with a field only containing "/",
  2905. (equal first-cell '("/"))
  2906. ;; ... the table contains a special column and the row start
  2907. ;; with a marking character among, "^", "_", "$" or "!",
  2908. (and (org-export-table-has-special-column-p
  2909. (org-export-get-parent table-row info))
  2910. (member first-cell '(("^") ("_") ("$") ("!"))))
  2911. ;; ... it contains only alignment cookies and empty cells.
  2912. (let ((special-row-p 'empty))
  2913. (catch 'exit
  2914. (mapc
  2915. (lambda (cell)
  2916. (let ((value (org-element-contents cell)))
  2917. (cond ((not value))
  2918. ((and (not (cdr value))
  2919. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  2920. (car value)))
  2921. (setq special-row-p 'cookie))
  2922. (t (throw 'exit nil)))))
  2923. (org-element-contents table-row))
  2924. (eq special-row-p 'cookie)))))))
  2925. (defun org-export-table-row-group (table-row info)
  2926. "Return TABLE-ROW's group.
  2927. INFO is a plist used as the communication channel.
  2928. Return value is the group number, as an integer, or nil special
  2929. rows and table rules. Group 1 is also table's header."
  2930. (unless (or (eq (org-element-property :type table-row) 'rule)
  2931. (org-export-table-row-is-special-p table-row info))
  2932. (let ((group 0) row-flag)
  2933. (catch 'found
  2934. (mapc
  2935. (lambda (row)
  2936. (cond
  2937. ((and (eq (org-element-property :type row) 'standard)
  2938. (not (org-export-table-row-is-special-p row info)))
  2939. (unless row-flag (incf group) (setq row-flag t)))
  2940. ((eq (org-element-property :type row) 'rule)
  2941. (setq row-flag nil)))
  2942. (when (equal table-row row) (throw 'found group)))
  2943. (org-element-contents (org-export-get-parent table-row info)))))))
  2944. (defun org-export-table-cell-width (table-cell info)
  2945. "Return TABLE-CELL contents width.
  2946. INFO is a plist used as the communication channel.
  2947. Return value is the width given by the last width cookie in the
  2948. same column as TABLE-CELL, or nil."
  2949. (let* ((genealogy (org-export-get-genealogy table-cell info))
  2950. (row (car genealogy))
  2951. (column (let ((cells (org-element-contents row)))
  2952. (- (length cells) (length (member table-cell cells)))))
  2953. (table (nth 1 genealogy))
  2954. cookie-width)
  2955. (mapc
  2956. (lambda (row)
  2957. (cond
  2958. ;; In a special row, try to find a width cookie at COLUMN.
  2959. ((org-export-table-row-is-special-p row info)
  2960. (let ((value (org-element-contents
  2961. (elt (org-element-contents row) column))))
  2962. (cond
  2963. ((not value))
  2964. ((and (not (cdr value))
  2965. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
  2966. (match-string 1 (car value)))
  2967. (setq cookie-width
  2968. (string-to-number (match-string 1 (car value))))))))
  2969. ;; Ignore table rules.
  2970. ((eq (org-element-property :type row) 'rule))))
  2971. (org-element-contents table))
  2972. ;; Return value.
  2973. cookie-width))
  2974. (defun org-export-table-cell-alignment (table-cell info)
  2975. "Return TABLE-CELL contents alignment.
  2976. INFO is a plist used as the communication channel.
  2977. Return alignment as specified by the last alignment cookie in the
  2978. same column as TABLE-CELL. If no such cookie is found, a default
  2979. alignment value will be deduced from fraction of numbers in the
  2980. column (see `org-table-number-fraction' for more information).
  2981. Possible values are `left', `right' and `center'."
  2982. (let* ((genealogy (org-export-get-genealogy table-cell info))
  2983. (row (car genealogy))
  2984. (column (let ((cells (org-element-contents row)))
  2985. (- (length cells) (length (member table-cell cells)))))
  2986. (table (nth 1 genealogy))
  2987. (number-cells 0)
  2988. (total-cells 0)
  2989. cookie-align)
  2990. (mapc
  2991. (lambda (row)
  2992. (cond
  2993. ;; In a special row, try to find an alignment cookie at
  2994. ;; COLUMN.
  2995. ((org-export-table-row-is-special-p row info)
  2996. (let ((value (org-element-contents
  2997. (elt (org-element-contents row) column))))
  2998. (cond
  2999. ((not value))
  3000. ((and (not (cdr value))
  3001. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  3002. (car value))
  3003. (match-string 1 (car value)))
  3004. (setq cookie-align (match-string 1 (car value)))))))
  3005. ;; Ignore table rules.
  3006. ((eq (org-element-property :type row) 'rule))
  3007. ;; In a standard row, check if cell's contents are expressing
  3008. ;; some kind of number. Increase NUMBER-CELLS accordingly.
  3009. ;; Though, don't bother if an alignment cookie has already
  3010. ;; defined cell's alignment.
  3011. ((not cookie-align)
  3012. (let ((value (org-element-interpret-data
  3013. (org-element-contents
  3014. (elt (org-element-contents row) column)))))
  3015. (incf total-cells)
  3016. (when (string-match org-table-number-regexp value)
  3017. (incf number-cells))))))
  3018. (org-element-contents table))
  3019. ;; Return value. Alignment specified by cookies has precedence
  3020. ;; over alignment deduced from cells contents.
  3021. (cond ((equal cookie-align "l") 'left)
  3022. ((equal cookie-align "r") 'right)
  3023. ((equal cookie-align "c") 'center)
  3024. ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
  3025. 'right)
  3026. (t 'left))))
  3027. (defun org-export-table-cell-borders (table-cell info)
  3028. "Return TABLE-CELL borders.
  3029. INFO is a plist used as a communication channel.
  3030. Return value is a list of symbols, or nil. Possible values are:
  3031. `top', `bottom', `above', `below', `left' and `right'. Note:
  3032. `top' (resp. `bottom') only happen for a cell in the first
  3033. row (resp. last row) of the table, ignoring table rules, if any.
  3034. Returned borders ignore special rows."
  3035. (let* ((genealogy (org-export-get-genealogy table-cell info))
  3036. (row (car genealogy))
  3037. (table (nth 1 genealogy))
  3038. borders)
  3039. ;; Top/above border? TABLE-CELL has a border above when a rule
  3040. ;; used to demarcate row groups can be found above. Hence,
  3041. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  3042. ;; another regular row has to be found above that rule.
  3043. (let (rule-flag)
  3044. (catch 'exit
  3045. (mapc (lambda (row)
  3046. (cond ((eq (org-element-property :type row) 'rule)
  3047. (setq rule-flag t))
  3048. ((not (org-export-table-row-is-special-p row info))
  3049. (if rule-flag (throw 'exit (push 'above borders))
  3050. (throw 'exit nil)))))
  3051. ;; Look at every row before the current one.
  3052. (cdr (member row (reverse (org-element-contents table)))))
  3053. ;; No rule above, or rule found starts the table (ignoring any
  3054. ;; special row): TABLE-CELL is at the top of the table.
  3055. (when rule-flag (push 'above borders))
  3056. (push 'top borders)))
  3057. ;; Bottom/below border? TABLE-CELL has a border below when next
  3058. ;; non-regular row below is a rule.
  3059. (let (rule-flag)
  3060. (catch 'exit
  3061. (mapc (lambda (row)
  3062. (cond ((eq (org-element-property :type row) 'rule)
  3063. (setq rule-flag t))
  3064. ((not (org-export-table-row-is-special-p row info))
  3065. (if rule-flag (throw 'exit (push 'below borders))
  3066. (throw 'exit nil)))))
  3067. ;; Look at every row after the current one.
  3068. (cdr (member row (org-element-contents table))))
  3069. ;; No rule below, or rule found ends the table (modulo some
  3070. ;; special row): TABLE-CELL is at the bottom of the table.
  3071. (when rule-flag (push 'below borders))
  3072. (push 'bottom borders)))
  3073. ;; Right/left borders? They can only be specified by column
  3074. ;; groups. Column groups are defined in a row starting with "/".
  3075. ;; Also a column groups row only contains "<", "<>", ">" or blank
  3076. ;; cells.
  3077. (catch 'exit
  3078. (let ((column (let ((cells (org-element-contents row)))
  3079. (- (length cells) (length (member table-cell cells))))))
  3080. (mapc
  3081. (lambda (row)
  3082. (unless (eq (org-element-property :type row) 'rule)
  3083. (when (equal (org-element-contents
  3084. (car (org-element-contents row)))
  3085. '("/"))
  3086. (let ((column-groups
  3087. (mapcar
  3088. (lambda (cell)
  3089. (let ((value (org-element-contents cell)))
  3090. (when (member value '(("<") ("<>") (">") nil))
  3091. (car value))))
  3092. (org-element-contents row))))
  3093. ;; There's a left border when previous cell, if
  3094. ;; any, ends a group, or current one starts one.
  3095. (when (or (and (not (zerop column))
  3096. (member (elt column-groups (1- column))
  3097. '(">" "<>")))
  3098. (member (elt column-groups column) '("<" "<>")))
  3099. (push 'left borders))
  3100. ;; There's a right border when next cell, if any,
  3101. ;; starts a group, or current one ends one.
  3102. (when (or (and (/= (1+ column) (length column-groups))
  3103. (member (elt column-groups (1+ column))
  3104. '("<" "<>")))
  3105. (member (elt column-groups column) '(">" "<>")))
  3106. (push 'right borders))
  3107. (throw 'exit nil)))))
  3108. ;; Table rows are read in reverse order so last column groups
  3109. ;; row has precedence over any previous one.
  3110. (reverse (org-element-contents table)))))
  3111. ;; Return value.
  3112. borders))
  3113. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  3114. "Non-nil when TABLE-CELL is at the beginning of a row group.
  3115. INFO is a plist used as a communication channel."
  3116. ;; A cell starts a column group either when it is at the beginning
  3117. ;; of a row (or after the special column, if any) or when it has
  3118. ;; a left border.
  3119. (or (equal (org-element-map
  3120. (org-export-get-parent table-cell info)
  3121. 'table-cell 'identity info 'first-match)
  3122. table-cell)
  3123. (memq 'left (org-export-table-cell-borders table-cell info))))
  3124. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  3125. "Non-nil when TABLE-CELL is at the end of a row group.
  3126. INFO is a plist used as a communication channel."
  3127. ;; A cell ends a column group either when it is at the end of a row
  3128. ;; or when it has a right border.
  3129. (or (equal (car (last (org-element-contents
  3130. (org-export-get-parent table-cell info))))
  3131. table-cell)
  3132. (memq 'right (org-export-table-cell-borders table-cell info))))
  3133. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  3134. "Non-nil when TABLE-ROW is at the beginning of a column group.
  3135. INFO is a plist used as a communication channel."
  3136. (unless (or (eq (org-element-property :type table-row) 'rule)
  3137. (org-export-table-row-is-special-p table-row info))
  3138. (let ((borders (org-export-table-cell-borders
  3139. (car (org-element-contents table-row)) info)))
  3140. (or (memq 'top borders) (memq 'above borders)))))
  3141. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  3142. "Non-nil when TABLE-ROW is at the end of a column group.
  3143. INFO is a plist used as a communication channel."
  3144. (unless (or (eq (org-element-property :type table-row) 'rule)
  3145. (org-export-table-row-is-special-p table-row info))
  3146. (let ((borders (org-export-table-cell-borders
  3147. (car (org-element-contents table-row)) info)))
  3148. (or (memq 'bottom borders) (memq 'below borders)))))
  3149. (defun org-export-table-row-starts-header-p (table-row info)
  3150. "Non-nil when TABLE-ROW is the first table header's row.
  3151. INFO is a plist used as a communication channel."
  3152. (and (org-export-table-has-header-p
  3153. (org-export-get-parent-table table-row info) info)
  3154. (org-export-table-row-starts-rowgroup-p table-row info)
  3155. (= (org-export-table-row-group table-row info) 1)))
  3156. (defun org-export-table-row-ends-header-p (table-row info)
  3157. "Non-nil when TABLE-ROW is the last table header's row.
  3158. INFO is a plist used as a communication channel."
  3159. (and (org-export-table-has-header-p
  3160. (org-export-get-parent-table table-row info) info)
  3161. (org-export-table-row-ends-rowgroup-p table-row info)
  3162. (= (org-export-table-row-group table-row info) 1)))
  3163. (defun org-export-table-dimensions (table info)
  3164. "Return TABLE dimensions.
  3165. INFO is a plist used as a communication channel.
  3166. Return value is a CONS like (ROWS . COLUMNS) where
  3167. ROWS (resp. COLUMNS) is the number of exportable
  3168. rows (resp. columns)."
  3169. (let (first-row (columns 0) (rows 0))
  3170. ;; Set number of rows, and extract first one.
  3171. (org-element-map
  3172. table 'table-row
  3173. (lambda (row)
  3174. (when (eq (org-element-property :type row) 'standard)
  3175. (incf rows)
  3176. (unless first-row (setq first-row row)))) info)
  3177. ;; Set number of columns.
  3178. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  3179. ;; Return value.
  3180. (cons rows columns)))
  3181. (defun org-export-table-cell-address (table-cell info)
  3182. "Return address of a regular TABLE-CELL object.
  3183. TABLE-CELL is the cell considered. INFO is a plist used as
  3184. a communication channel.
  3185. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3186. zero-based index. Only exportable cells are considered. The
  3187. function returns nil for other cells."
  3188. (let* ((table-row (org-export-get-parent table-cell info))
  3189. (table (org-export-get-parent-table table-cell info)))
  3190. ;; Ignore cells in special rows or in special column.
  3191. (unless (or (org-export-table-row-is-special-p table-row info)
  3192. (and (org-export-table-has-special-column-p table)
  3193. (equal (car (org-element-contents table-row)) table-cell)))
  3194. (cons
  3195. ;; Row number.
  3196. (let ((row-count 0))
  3197. (org-element-map
  3198. table 'table-row
  3199. (lambda (row)
  3200. (cond ((eq (org-element-property :type row) 'rule) nil)
  3201. ((equal row table-row) row-count)
  3202. (t (incf row-count) nil)))
  3203. info 'first-match))
  3204. ;; Column number.
  3205. (let ((col-count 0))
  3206. (org-element-map
  3207. table-row 'table-cell
  3208. (lambda (cell)
  3209. (if (equal cell table-cell) col-count
  3210. (incf col-count) nil))
  3211. info 'first-match))))))
  3212. (defun org-export-get-table-cell-at (address table info)
  3213. "Return regular table-cell object at ADDRESS in TABLE.
  3214. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3215. zero-based index. TABLE is a table type element. INFO is
  3216. a plist used as a communication channel.
  3217. If no table-cell, among exportable cells, is found at ADDRESS,
  3218. return nil."
  3219. (let ((column-pos (cdr address)) (column-count 0))
  3220. (org-element-map
  3221. ;; Row at (car address) or nil.
  3222. (let ((row-pos (car address)) (row-count 0))
  3223. (org-element-map
  3224. table 'table-row
  3225. (lambda (row)
  3226. (cond ((eq (org-element-property :type row) 'rule) nil)
  3227. ((= row-count row-pos) row)
  3228. (t (incf row-count) nil)))
  3229. info 'first-match))
  3230. 'table-cell
  3231. (lambda (cell)
  3232. (if (= column-count column-pos) cell
  3233. (incf column-count) nil))
  3234. info 'first-match)))
  3235. ;;;; For Tables Of Contents
  3236. ;;
  3237. ;; `org-export-collect-headlines' builds a list of all exportable
  3238. ;; headline elements, maybe limited to a certain depth. One can then
  3239. ;; easily parse it and transcode it.
  3240. ;;
  3241. ;; Building lists of tables, figures or listings is quite similar.
  3242. ;; Once the generic function `org-export-collect-elements' is defined,
  3243. ;; `org-export-collect-tables', `org-export-collect-figures' and
  3244. ;; `org-export-collect-listings' can be derived from it.
  3245. (defun org-export-collect-headlines (info &optional n)
  3246. "Collect headlines in order to build a table of contents.
  3247. INFO is a plist used as a communication channel.
  3248. When non-nil, optional argument N must be an integer. It
  3249. specifies the depth of the table of contents.
  3250. Return a list of all exportable headlines as parsed elements."
  3251. (org-element-map
  3252. (plist-get info :parse-tree)
  3253. 'headline
  3254. (lambda (headline)
  3255. ;; Strip contents from HEADLINE.
  3256. (let ((relative-level (org-export-get-relative-level headline info)))
  3257. (unless (and n (> relative-level n)) headline)))
  3258. info))
  3259. (defun org-export-collect-elements (type info &optional predicate)
  3260. "Collect referenceable elements of a determined type.
  3261. TYPE can be a symbol or a list of symbols specifying element
  3262. types to search. Only elements with a caption or a name are
  3263. collected.
  3264. INFO is a plist used as a communication channel.
  3265. When non-nil, optional argument PREDICATE is a function accepting
  3266. one argument, an element of type TYPE. It returns a non-nil
  3267. value when that element should be collected.
  3268. Return a list of all elements found, in order of appearance."
  3269. (org-element-map
  3270. (plist-get info :parse-tree) type
  3271. (lambda (element)
  3272. (and (or (org-element-property :caption element)
  3273. (org-element-property :name element))
  3274. (or (not predicate) (funcall predicate element))
  3275. element)) info))
  3276. (defun org-export-collect-tables (info)
  3277. "Build a list of tables.
  3278. INFO is a plist used as a communication channel.
  3279. Return a list of table elements with a caption or a name
  3280. affiliated keyword."
  3281. (org-export-collect-elements 'table info))
  3282. (defun org-export-collect-figures (info predicate)
  3283. "Build a list of figures.
  3284. INFO is a plist used as a communication channel. PREDICATE is
  3285. a function which accepts one argument: a paragraph element and
  3286. whose return value is non-nil when that element should be
  3287. collected.
  3288. A figure is a paragraph type element, with a caption or a name,
  3289. verifying PREDICATE. The latter has to be provided since
  3290. a \"figure\" is a vague concept that may depend on back-end.
  3291. Return a list of elements recognized as figures."
  3292. (org-export-collect-elements 'paragraph info predicate))
  3293. (defun org-export-collect-listings (info)
  3294. "Build a list of src blocks.
  3295. INFO is a plist used as a communication channel.
  3296. Return a list of src-block elements with a caption or a name
  3297. affiliated keyword."
  3298. (org-export-collect-elements 'src-block info))
  3299. ;;;; Topology
  3300. ;;
  3301. ;; Here are various functions to retrieve information about the
  3302. ;; neighbourhood of a given element or object. Neighbours of interest
  3303. ;; are direct parent (`org-export-get-parent'), parent headline
  3304. ;; (`org-export-get-parent-headline'), parent paragraph
  3305. ;; (`org-export-get-parent-paragraph'), previous element or object
  3306. ;; (`org-export-get-previous-element') and next element or object
  3307. ;; (`org-export-get-next-element').
  3308. ;;
  3309. ;; All of these functions are just a specific use of the more generic
  3310. ;; `org-export-get-genealogy', which returns the genealogy relative to
  3311. ;; the element or object.
  3312. (defun org-export-get-genealogy (blob info)
  3313. "Return genealogy relative to a given element or object.
  3314. BLOB is the element or object being considered. INFO is a plist
  3315. used as a communication channel."
  3316. (let* ((type (org-element-type blob))
  3317. (end (org-element-property :end blob))
  3318. (walk-data
  3319. (lambda (data genealogy)
  3320. ;; Walk DATA, looking for BLOB. GENEALOGY is the list of
  3321. ;; parents of all elements in DATA.
  3322. (mapc
  3323. (lambda (el)
  3324. (cond
  3325. ((stringp el) nil)
  3326. ((equal el blob) (throw 'exit genealogy))
  3327. ((>= (org-element-property :end el) end)
  3328. ;; If BLOB is an object and EL contains a secondary
  3329. ;; string, be sure to check it.
  3330. (when (memq type org-element-all-objects)
  3331. (let ((sec-prop
  3332. (cdr (assq (org-element-type el)
  3333. org-element-secondary-value-alist))))
  3334. (when sec-prop
  3335. (funcall
  3336. walk-data
  3337. (cons 'org-data
  3338. (cons nil (org-element-property sec-prop el)))
  3339. (cons el genealogy)))))
  3340. (funcall walk-data el (cons el genealogy)))))
  3341. (org-element-contents data)))))
  3342. (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
  3343. (defun org-export-get-parent (blob info)
  3344. "Return BLOB parent or nil.
  3345. BLOB is the element or object considered. INFO is a plist used
  3346. as a communication channel."
  3347. (car (org-export-get-genealogy blob info)))
  3348. (defun org-export-get-parent-headline (blob info)
  3349. "Return BLOB parent headline or nil.
  3350. BLOB is the element or object being considered. INFO is a plist
  3351. used as a communication channel."
  3352. (catch 'exit
  3353. (mapc
  3354. (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
  3355. (org-export-get-genealogy blob info))
  3356. nil))
  3357. (defun org-export-get-parent-paragraph (object info)
  3358. "Return OBJECT parent paragraph or nil.
  3359. OBJECT is the object to consider. INFO is a plist used as
  3360. a communication channel."
  3361. (catch 'exit
  3362. (mapc
  3363. (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
  3364. (org-export-get-genealogy object info))
  3365. nil))
  3366. (defun org-export-get-parent-table (object info)
  3367. "Return OBJECT parent table or nil.
  3368. OBJECT is either a `table-cell' or `table-element' type object.
  3369. INFO is a plist used as a communication channel."
  3370. (catch 'exit
  3371. (mapc
  3372. (lambda (el) (when (eq (org-element-type el) 'table) (throw 'exit el)))
  3373. (org-export-get-genealogy object info))
  3374. nil))
  3375. (defun org-export-get-previous-element (blob info)
  3376. "Return previous element or object.
  3377. BLOB is an element or object. INFO is a plist used as
  3378. a communication channel.
  3379. Return previous element or object, a string, or nil."
  3380. (let ((parent (org-export-get-parent blob info)))
  3381. (cadr (member blob (reverse (org-element-contents parent))))))
  3382. (defun org-export-get-next-element (blob info)
  3383. "Return next element or object.
  3384. BLOB is an element or object. INFO is a plist used as
  3385. a communication channel.
  3386. Return next element or object, a string, or nil."
  3387. (let ((parent (org-export-get-parent blob info)))
  3388. (cadr (member blob (org-element-contents parent)))))
  3389. ;;; The Dispatcher
  3390. ;;
  3391. ;; `org-export-dispatch' is the standard interactive way to start an
  3392. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  3393. ;; for its interface. Most commons back-ends should have an entry in
  3394. ;; it.
  3395. (defun org-export-dispatch ()
  3396. "Export dispatcher for Org mode.
  3397. It provides an access to common export related tasks in a buffer.
  3398. Its interface comes in two flavours: standard and expert. While
  3399. both share the same set of bindings, only the former displays the
  3400. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  3401. to switch to one or the other.
  3402. Return an error if key pressed has no associated command."
  3403. (interactive)
  3404. (let* ((input (org-export-dispatch-ui
  3405. (if (listp org-export-initial-scope) org-export-initial-scope
  3406. (list org-export-initial-scope))
  3407. org-export-dispatch-use-expert-ui))
  3408. (raw-key (car input))
  3409. (optns (cdr input)))
  3410. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  3411. ;; depending on user's key pressed.
  3412. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  3413. ;; Allow to quit with "q" key.
  3414. (?q nil)
  3415. ;; Export with `e-ascii' back-end.
  3416. ((?A ?N ?U)
  3417. (let ((outbuf
  3418. (org-export-to-buffer
  3419. 'e-ascii "*Org E-ASCII Export*"
  3420. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3421. `(:ascii-charset
  3422. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  3423. (with-current-buffer outbuf (text-mode))
  3424. (when org-export-show-temporary-export-buffer
  3425. (switch-to-buffer-other-window outbuf))))
  3426. ((?a ?n ?u)
  3427. (org-e-ascii-export-to-ascii
  3428. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3429. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  3430. ;; Export with `e-latex' back-end.
  3431. (?L
  3432. (let ((outbuf
  3433. (org-export-to-buffer
  3434. 'e-latex "*Org E-LaTeX Export*"
  3435. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3436. (with-current-buffer outbuf (latex-mode))
  3437. (when org-export-show-temporary-export-buffer
  3438. (switch-to-buffer-other-window outbuf))))
  3439. (?l (org-e-latex-export-to-latex
  3440. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3441. (?p (org-e-latex-export-to-pdf
  3442. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3443. (?d (org-open-file
  3444. (org-e-latex-export-to-pdf
  3445. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3446. ;; Export with `e-html' back-end.
  3447. (?H
  3448. (let ((outbuf
  3449. (org-export-to-buffer
  3450. 'e-html "*Org E-HTML Export*"
  3451. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3452. ;; set major mode
  3453. (with-current-buffer outbuf
  3454. (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
  3455. (when org-export-show-temporary-export-buffer
  3456. (switch-to-buffer-other-window outbuf))))
  3457. (?h (org-e-html-export-to-html
  3458. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3459. (?b (org-open-file
  3460. (org-e-html-export-to-html
  3461. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3462. ;; Export with `e-odt' back-end.
  3463. (?o (org-e-odt-export-to-odt
  3464. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3465. (?O (org-open-file
  3466. (org-e-odt-export-to-odt
  3467. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3468. ;; Publishing facilities
  3469. (?F (org-e-publish-current-file (memq 'force optns)))
  3470. (?P (org-e-publish-current-project (memq 'force optns)))
  3471. (?X (let ((project
  3472. (assoc (org-icompleting-read
  3473. "Publish project: " org-e-publish-project-alist nil t)
  3474. org-e-publish-project-alist)))
  3475. (org-e-publish project (memq 'force optns))))
  3476. (?E (org-e-publish-all (memq 'force optns)))
  3477. ;; Undefined command.
  3478. (t (error "No command associated with key %s"
  3479. (char-to-string raw-key))))))
  3480. (defun org-export-dispatch-ui (options expertp)
  3481. "Handle interface for `org-export-dispatch'.
  3482. OPTIONS is a list containing current interactive options set for
  3483. export. It can contain any of the following symbols:
  3484. `body' toggles a body-only export
  3485. `subtree' restricts export to current subtree
  3486. `visible' restricts export to visible part of buffer.
  3487. `force' force publishing files.
  3488. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  3489. buffer is provided, but indications about currently active
  3490. options are given in the prompt. Moreover, \[?] allows to switch
  3491. back to standard interface.
  3492. Return value is a list with key pressed as CAR and a list of
  3493. final interactive export options as CDR."
  3494. (let ((help
  3495. (format "---- (Options) -------------------------------------------
  3496. \[1] Body only: %s [2] Export scope: %s
  3497. \[3] Visible only: %s [4] Force publishing: %s
  3498. --- (ASCII/Latin-1/UTF-8 Export) -------------------------
  3499. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  3500. --- (HTML Export) ----------------------------------------
  3501. \[h] to HTML file [b] ... and open it
  3502. \[H] to temporary buffer
  3503. --- (LaTeX Export) ---------------------------------------
  3504. \[l] to TEX file [L] to temporary buffer
  3505. \[p] to PDF file [d] ... and open it
  3506. --- (ODF Export) -----------------------------------------
  3507. \[o] to ODT file [O] ... and open it
  3508. --- (Publish) --------------------------------------------
  3509. \[F] current file [P] current project
  3510. \[X] a project [E] every project"
  3511. (if (memq 'body options) "On " "Off")
  3512. (if (memq 'subtree options) "Subtree" "Buffer ")
  3513. (if (memq 'visible options) "On " "Off")
  3514. (if (memq 'force options) "On " "Off")))
  3515. (standard-prompt "Export command: ")
  3516. (expert-prompt (format "Export command (%s%s%s%s): "
  3517. (if (memq 'body options) "b" "-")
  3518. (if (memq 'subtree options) "s" "-")
  3519. (if (memq 'visible options) "v" "-")
  3520. (if (memq 'force options) "f" "-")))
  3521. (handle-keypress
  3522. (function
  3523. ;; Read a character from command input, toggling interactive
  3524. ;; options when applicable. PROMPT is the displayed prompt,
  3525. ;; as a string.
  3526. (lambda (prompt)
  3527. (let ((key (read-char-exclusive prompt)))
  3528. (cond
  3529. ;; Ignore non-standard characters (i.e. "M-a").
  3530. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  3531. ;; Help key: Switch back to standard interface if
  3532. ;; expert UI was active.
  3533. ((eq key ??) (org-export-dispatch-ui options nil))
  3534. ;; Toggle export options.
  3535. ((memq key '(?1 ?2 ?3 ?4))
  3536. (org-export-dispatch-ui
  3537. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  3538. (?4 'force))))
  3539. (if (memq option options) (remq option options)
  3540. (cons option options)))
  3541. expertp))
  3542. ;; Action selected: Send key and options back to
  3543. ;; `org-export-dispatch'.
  3544. (t (cons key options))))))))
  3545. ;; With expert UI, just read key with a fancy prompt. In standard
  3546. ;; UI, display an intrusive help buffer.
  3547. (if expertp (funcall handle-keypress expert-prompt)
  3548. (save-window-excursion
  3549. (delete-other-windows)
  3550. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  3551. (org-fit-window-to-buffer
  3552. (get-buffer-window "*Org Export/Publishing Help*"))
  3553. (funcall handle-keypress standard-prompt)))))
  3554. (provide 'org-export)
  3555. ;;; org-export.el ends here