org-export.el 111 KB

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