org-export.el 113 KB

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