org-export.el 135 KB

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