org-exp.el 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330
  1. ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
  2. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;; Code:
  23. (require 'org)
  24. (require 'org-macs)
  25. (require 'org-agenda)
  26. (require 'org-exp-blocks)
  27. (require 'ob-exp)
  28. (require 'org-src)
  29. (eval-when-compile
  30. (require 'cl))
  31. (declare-function org-export-latex-preprocess "org-latex" (parameters))
  32. (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
  33. (declare-function org-export-html-preprocess "org-html" (parameters))
  34. (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
  35. (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
  36. (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
  37. (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
  38. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  39. (declare-function org-table-cookie-line-p "org-table" (line))
  40. (declare-function org-table-colgroup-line-p "org-table" (line))
  41. (declare-function org-pop-to-buffer-same-window "org-compat"
  42. (&optional buffer-or-name norecord label))
  43. (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
  44. (autoload 'org-export-as-odt "org-odt"
  45. "Export the outline to a OpenDocument Text file." t)
  46. (autoload 'org-export-as-odt-and-open "org-odt"
  47. "Export the outline to a OpenDocument Text file and open it." t)
  48. (defgroup org-export nil
  49. "Options for exporting org-listings."
  50. :tag "Org Export"
  51. :group 'org)
  52. (defgroup org-export-general nil
  53. "General options for exporting Org-mode files."
  54. :tag "Org Export General"
  55. :group 'org-export)
  56. (defcustom org-export-allow-BIND 'confirm
  57. "Non-nil means allow #+BIND to define local variable values for export.
  58. This is a potential security risk, which is why the user must confirm the
  59. use of these lines."
  60. :group 'org-export-general
  61. :type '(choice
  62. (const :tag "Never" nil)
  63. (const :tag "Always" t)
  64. (const :tag "Make the user confirm for each file" confirm)))
  65. ;; FIXME
  66. (defvar org-export-publishing-directory nil)
  67. (defcustom org-export-show-temporary-export-buffer t
  68. "Non-nil means show buffer after exporting to temp buffer.
  69. When Org exports to a file, the buffer visiting that file is ever
  70. shown, but remains buried. However, when exporting to a temporary
  71. buffer, that buffer is popped up in a second window. When this variable
  72. is nil, the buffer remains buried also in these cases."
  73. :group 'org-export-general
  74. :type 'boolean)
  75. (defcustom org-export-copy-to-kill-ring t
  76. "Non-nil means exported stuff will also be pushed onto the kill ring."
  77. :group 'org-export-general
  78. :type 'boolean)
  79. (defcustom org-export-kill-product-buffer-when-displayed nil
  80. "Non-nil means kill the product buffer if it is displayed immediately.
  81. This applied to the commands `org-export-as-html-and-open' and
  82. `org-export-as-pdf-and-open'."
  83. :group 'org-export-general
  84. :type 'boolean)
  85. (defcustom org-export-run-in-background nil
  86. "Non-nil means export and publishing commands will run in background.
  87. This works by starting up a separate Emacs process visiting the same file
  88. and doing the export from there.
  89. Not all export commands are affected by this - only the ones which
  90. actually write to a file, and that do not depend on the buffer state.
  91. \\<org-mode-map>
  92. If this option is nil, you can still get background export by calling
  93. `org-export' with a double prefix arg: \
  94. \\[universal-argument] \\[universal-argument] \\[org-export].
  95. If this option is t, the double prefix can be used to exceptionally
  96. force an export command into the current process."
  97. :group 'org-export-general
  98. :type 'boolean)
  99. (defcustom org-export-initial-scope 'buffer
  100. "The initial scope when exporting with `org-export'.
  101. This variable can be either set to 'buffer or 'subtree."
  102. :group 'org-export-general
  103. :type '(choice
  104. (const :tag "Export current buffer" 'buffer)
  105. (const :tag "Export current subtree" 'subtree)))
  106. (defcustom org-export-select-tags '("export")
  107. "Tags that select a tree for export.
  108. If any such tag is found in a buffer, all trees that do not carry one
  109. of these tags will be deleted before export.
  110. Inside trees that are selected like this, you can still deselect a
  111. subtree by tagging it with one of the `org-export-exclude-tags'."
  112. :group 'org-export-general
  113. :type '(repeat (string :tag "Tag")))
  114. (defcustom org-export-exclude-tags '("noexport")
  115. "Tags that exclude a tree from export.
  116. All trees carrying any of these tags will be excluded from export.
  117. This is without condition, so even subtrees inside that carry one of the
  118. `org-export-select-tags' will be removed."
  119. :group 'org-export-general
  120. :type '(repeat (string :tag "Tag")))
  121. ;; FIXME: rename, this is a general variable
  122. (defcustom org-export-html-expand t
  123. "Non-nil means for HTML export, treat @<...> as HTML tag.
  124. When nil, these tags will be exported as plain text and therefore
  125. not be interpreted by a browser.
  126. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  127. :group 'org-export-html
  128. :group 'org-export-general
  129. :type 'boolean)
  130. (defcustom org-export-with-special-strings t
  131. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  132. When this option is turned on, these strings will be exported as:
  133. Org HTML LaTeX
  134. -----+----------+--------
  135. \\- &shy; \\-
  136. -- &ndash; --
  137. --- &mdash; ---
  138. ... &hellip; \ldots
  139. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  140. :group 'org-export-translation
  141. :type 'boolean)
  142. (defcustom org-export-html-link-up ""
  143. "Where should the \"UP\" link of exported HTML pages lead?"
  144. :group 'org-export-html
  145. :group 'org-export-general
  146. :type '(string :tag "File or URL"))
  147. (defcustom org-export-html-link-home ""
  148. "Where should the \"HOME\" link of exported HTML pages lead?"
  149. :group 'org-export-html
  150. :group 'org-export-general
  151. :type '(string :tag "File or URL"))
  152. (defcustom org-export-language-setup
  153. '(("en" "Author" "Date" "Table of Contents" "Footnotes")
  154. ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
  155. ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
  156. ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
  157. ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
  158. ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
  159. ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
  160. ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
  161. ("fr" "Auteur" "Date" "Table des mati&egrave;res" "Notes de bas de page")
  162. ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
  163. ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
  164. ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
  165. ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
  166. ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
  167. ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
  168. ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
  169. ("pl" "Autor" "Data" "Spis tre&#x015b;ci" "Przypis")
  170. ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter"))
  171. "Terms used in export text, translated to different languages.
  172. Use the variable `org-export-default-language' to set the language,
  173. or use the +OPTION lines for a per-file setting."
  174. :group 'org-export-general
  175. :type '(repeat
  176. (list
  177. (string :tag "HTML language tag")
  178. (string :tag "Author")
  179. (string :tag "Date")
  180. (string :tag "Table of Contents")
  181. (string :tag "Footnotes"))))
  182. (defcustom org-export-default-language "en"
  183. "The default language for export and clocktable translations, as a string.
  184. This should have an association in `org-export-language-setup'
  185. and in `org-clock-clocktable-language-setup'."
  186. :group 'org-export-general
  187. :type 'string)
  188. (defcustom org-export-date-timestamp-format "%Y-%m-%d"
  189. "Time string format for Org timestamps in the #+DATE option."
  190. :group 'org-export-general
  191. :type 'string)
  192. (defvar org-export-page-description ""
  193. "The page description, for the XHTML meta tag.
  194. This is best set with the #+DESCRIPTION line in a file, it does not make
  195. sense to set this globally.")
  196. (defvar org-export-page-keywords ""
  197. "The page description, for the XHTML meta tag.
  198. This is best set with the #+KEYWORDS line in a file, it does not make
  199. sense to set this globally.")
  200. (defcustom org-export-skip-text-before-1st-heading nil
  201. "Non-nil means skip all text before the first headline when exporting.
  202. When nil, that text is exported as well."
  203. :group 'org-export-general
  204. :type 'boolean)
  205. (defcustom org-export-headline-levels 3
  206. "The last level which is still exported as a headline.
  207. Inferior levels will produce itemize lists when exported.
  208. Note that a numeric prefix argument to an exporter function overrides
  209. this setting.
  210. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  211. :group 'org-export-general
  212. :type 'integer)
  213. (defcustom org-export-with-section-numbers t
  214. "Non-nil means add section numbers to headlines when exporting.
  215. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  216. :group 'org-export-general
  217. :type 'boolean)
  218. (defcustom org-export-section-number-format '((("1" ".")) . "")
  219. "Format of section numbers for export.
  220. The variable has two components.
  221. 1. A list of lists, each indicating a counter type and a separator.
  222. The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\".
  223. It causes causes numeric, alphabetic, or roman counters, respectively.
  224. The separator is only used if another counter for a subsection is being
  225. added.
  226. If there are more numbered section levels than entries in this lists,
  227. then the last entry will be reused.
  228. 2. A terminator string that will be added after the entire
  229. section number."
  230. :group 'org-export-general
  231. :type '(cons
  232. (repeat
  233. (list
  234. (string :tag "Counter Type")
  235. (string :tag "Separator ")))
  236. (string :tag "Terminator")))
  237. (defcustom org-export-with-toc t
  238. "Non-nil means create a table of contents in exported files.
  239. The TOC contains headlines with levels up to`org-export-headline-levels'.
  240. When an integer, include levels up to N in the toc, this may then be
  241. different from `org-export-headline-levels', but it will not be allowed
  242. to be larger than the number of headline levels.
  243. When nil, no table of contents is made.
  244. Headlines which contain any TODO items will be marked with \"(*)\" in
  245. ASCII export, and with red color in HTML output, if the option
  246. `org-export-mark-todo-in-toc' is set.
  247. In HTML output, the TOC will be clickable.
  248. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  249. or \"toc:3\"."
  250. :group 'org-export-general
  251. :type '(choice
  252. (const :tag "No Table of Contents" nil)
  253. (const :tag "Full Table of Contents" t)
  254. (integer :tag "TOC to level")))
  255. (defcustom org-export-mark-todo-in-toc nil
  256. "Non-nil means mark TOC lines that contain any open TODO items."
  257. :group 'org-export-general
  258. :type 'boolean)
  259. (defcustom org-export-with-todo-keywords t
  260. "Non-nil means include TODO keywords in export.
  261. When nil, remove all these keywords from the export."
  262. :group 'org-export-general
  263. :type 'boolean)
  264. (defcustom org-export-with-tasks t
  265. "Non-nil means include TODO items for export.
  266. This may have the following values:
  267. t include tasks independent of state.
  268. todo include only tasks that are not yet done.
  269. done include only tasks that are already done.
  270. nil remove all tasks before export
  271. list of TODO kwds keep only tasks with these keywords"
  272. :group 'org-export-general
  273. :type '(choice
  274. (const :tag "All tasks" t)
  275. (const :tag "No tasks" nil)
  276. (const :tag "Not-done tasks" todo)
  277. (const :tag "Only done tasks" done)
  278. (repeat :tag "Specific TODO keywords"
  279. (string :tag "Keyword"))))
  280. (defcustom org-export-with-priority nil
  281. "Non-nil means include priority cookies in export.
  282. When nil, remove priority cookies for export."
  283. :group 'org-export-general
  284. :type 'boolean)
  285. (defcustom org-export-preserve-breaks nil
  286. "Non-nil means preserve all line breaks when exporting.
  287. Normally, in HTML output paragraphs will be reformatted. In ASCII
  288. export, line breaks will always be preserved, regardless of this variable.
  289. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  290. :group 'org-export-general
  291. :type 'boolean)
  292. (defcustom org-export-with-archived-trees 'headline
  293. "Whether subtrees with the ARCHIVE tag should be exported.
  294. This can have three different values
  295. nil Do not export, pretend this tree is not present
  296. t Do export the entire tree
  297. headline Only export the headline, but skip the tree below it."
  298. :group 'org-export-general
  299. :group 'org-archive
  300. :type '(choice
  301. (const :tag "not at all" nil)
  302. (const :tag "headline only" 'headline)
  303. (const :tag "entirely" t)))
  304. (defcustom org-export-author-info t
  305. "Non-nil means insert author name and email into the exported file.
  306. This option can also be set with the +OPTIONS line,
  307. e.g. \"author:nil\"."
  308. :group 'org-export-general
  309. :type 'boolean)
  310. (defcustom org-export-email-info nil
  311. "Non-nil means insert author name and email into the exported file.
  312. This option can also be set with the +OPTIONS line,
  313. e.g. \"email:t\"."
  314. :group 'org-export-general
  315. :type 'boolean)
  316. (defcustom org-export-creator-info t
  317. "Non-nil means the postamble should contain a creator sentence.
  318. This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
  319. :group 'org-export-general
  320. :type 'boolean)
  321. (defcustom org-export-time-stamp-file t
  322. "Non-nil means insert a time stamp into the exported file.
  323. The time stamp shows when the file was created.
  324. This option can also be set with the +OPTIONS line,
  325. e.g. \"timestamp:nil\"."
  326. :group 'org-export-general
  327. :type 'boolean)
  328. (defcustom org-export-with-timestamps t
  329. "If nil, do not export time stamps and associated keywords."
  330. :group 'org-export-general
  331. :type 'boolean)
  332. (defcustom org-export-remove-timestamps-from-toc t
  333. "If t, remove timestamps from the table of contents entries."
  334. :group 'org-export-general
  335. :type 'boolean)
  336. (defcustom org-export-with-tags 'not-in-toc
  337. "If nil, do not export tags, just remove them from headlines.
  338. If this is the symbol `not-in-toc', tags will be removed from table of
  339. contents entries, but still be shown in the headlines of the document.
  340. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  341. :group 'org-export-general
  342. :type '(choice
  343. (const :tag "Off" nil)
  344. (const :tag "Not in TOC" not-in-toc)
  345. (const :tag "On" t)))
  346. (defcustom org-export-with-drawers nil
  347. "Non-nil means export with drawers like the property drawer.
  348. When t, all drawers are exported. This may also be a list of
  349. drawer names to export."
  350. :group 'org-export-general
  351. :type '(choice
  352. (const :tag "All drawers" t)
  353. (const :tag "None" nil)
  354. (repeat :tag "Selected drawers"
  355. (string :tag "Drawer name"))))
  356. (defvar org-export-first-hook nil
  357. "Hook called as the first thing in each exporter.
  358. Point will be still in the original buffer.
  359. Good for general initialization")
  360. (defvar org-export-preprocess-hook nil
  361. "Hook for preprocessing an export buffer.
  362. Pretty much the first thing when exporting is running this hook.
  363. Point will be in a temporary buffer that contains a copy of
  364. the original buffer, or of the section that is being exported.
  365. All the other hooks in the org-export-preprocess... category
  366. also work in that temporary buffer, already modified by various
  367. stages of the processing.")
  368. (defvar org-export-preprocess-after-include-files-hook nil
  369. "Hook for preprocessing an export buffer.
  370. This is run after the contents of included files have been inserted.")
  371. (defvar org-export-preprocess-after-tree-selection-hook nil
  372. "Hook for preprocessing an export buffer.
  373. This is run after selection of trees to be exported has happened.
  374. This selection includes tags-based selection, as well as removal
  375. of commented and archived trees.")
  376. (defvar org-export-preprocess-after-headline-targets-hook nil
  377. "Hook for preprocessing export buffer.
  378. This is run just after the headline targets have been defined and
  379. the target-alist has been set up.")
  380. (defvar org-export-preprocess-before-selecting-backend-code-hook nil
  381. "Hook for preprocessing an export buffer.
  382. This is run just before backend-specific blocks get selected.")
  383. (defvar org-export-preprocess-after-blockquote-hook nil
  384. "Hook for preprocessing an export buffer.
  385. This is run after blockquote/quote/verse/center have been marked
  386. with cookies.")
  387. (defvar org-export-preprocess-after-radio-targets-hook nil
  388. "Hook for preprocessing an export buffer.
  389. This is run after radio target processing.")
  390. (defvar org-export-preprocess-before-normalizing-links-hook nil
  391. "Hook for preprocessing an export buffer.
  392. This hook is run before links are normalized.")
  393. (defvar org-export-preprocess-before-backend-specifics-hook nil
  394. "Hook run before backend-specific functions are called during preprocessing.")
  395. (defvar org-export-preprocess-final-hook nil
  396. "Hook for preprocessing an export buffer.
  397. This is run as the last thing in the preprocessing buffer, just before
  398. returning the buffer string to the backend.")
  399. (defgroup org-export-translation nil
  400. "Options for translating special ascii sequences for the export backends."
  401. :tag "Org Export Translation"
  402. :group 'org-export)
  403. (defcustom org-export-with-emphasize t
  404. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  405. If the export target supports emphasizing text, the word will be
  406. typeset in bold, italic, or underlined, respectively. Works only for
  407. single words, but you can say: I *really* *mean* *this*.
  408. Not all export backends support this.
  409. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  410. :group 'org-export-translation
  411. :type 'boolean)
  412. (defcustom org-export-with-footnotes t
  413. "If nil, export [1] as a footnote marker.
  414. Lines starting with [1] will be formatted as footnotes.
  415. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  416. :group 'org-export-translation
  417. :type 'boolean)
  418. (defcustom org-export-with-TeX-macros t
  419. "Non-nil means interpret simple TeX-like macros when exporting.
  420. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  421. Not only real TeX macros will work here, but the standard HTML entities
  422. for math can be used as macro names as well. For a list of supported
  423. names in HTML export, see the constant `org-entities' and the user option
  424. `org-entities-user'.
  425. Not all export backends support this.
  426. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  427. :group 'org-export-translation
  428. :group 'org-export-latex
  429. :type 'boolean)
  430. (defcustom org-export-with-LaTeX-fragments t
  431. "Non-nil means process LaTeX math fragments for HTML display.
  432. When set, the exporter will find and process LaTeX environments if the
  433. \\begin line is the first non-white thing on a line. It will also find
  434. and process the math delimiters like $a=b$ and \\( a=b \\) for inline math,
  435. $$a=b$$ and \\=\\[ a=b \\] for display math.
  436. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
  437. Allowed values are:
  438. nil Don't do anything.
  439. verbatim Keep everything in verbatim
  440. dvipng Process the LaTeX fragments to images.
  441. This will also include processing of non-math environments.
  442. t Do MathJax preprocessing if there is at least on math snippet,
  443. and arrange for MathJax.js to be loaded.
  444. The default is nil, because this option needs the `dvipng' program which
  445. is not available on all systems."
  446. :group 'org-export-translation
  447. :group 'org-export-latex
  448. :type '(choice
  449. (const :tag "Do not process math in any way" nil)
  450. (const :tag "Obsolete, use dvipng setting" t)
  451. (const :tag "Use dvipng to make images" dvipng)
  452. (const :tag "Use MathJax to display math" mathjax)
  453. (const :tag "Leave math verbatim" verbatim)))
  454. (defcustom org-export-with-fixed-width t
  455. "Non-nil means lines starting with \":\" will be in fixed width font.
  456. This can be used to have pre-formatted text, fragments of code etc. For
  457. example:
  458. : ;; Some Lisp examples
  459. : (while (defc cnt)
  460. : (ding))
  461. will be looking just like this in also HTML. See also the QUOTE keyword.
  462. Not all export backends support this.
  463. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  464. :group 'org-export-translation
  465. :type 'boolean)
  466. (defgroup org-export-tables nil
  467. "Options for exporting tables in Org-mode."
  468. :tag "Org Export Tables"
  469. :group 'org-export)
  470. (defcustom org-export-with-tables t
  471. "If non-nil, lines starting with \"|\" define a table.
  472. For example:
  473. | Name | Address | Birthday |
  474. |-------------+----------+-----------|
  475. | Arthur Dent | England | 29.2.2100 |
  476. Not all export backends support this.
  477. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  478. :group 'org-export-tables
  479. :type 'boolean)
  480. (defcustom org-export-highlight-first-table-line t
  481. "Non-nil means highlight the first table line.
  482. In HTML export, this means use <th> instead of <td>.
  483. In tables created with table.el, this applies to the first table line.
  484. In Org-mode tables, all lines before the first horizontal separator
  485. line will be formatted with <th> tags."
  486. :group 'org-export-tables
  487. :type 'boolean)
  488. (defcustom org-export-table-remove-special-lines t
  489. "Remove special lines and marking characters in calculating tables.
  490. This removes the special marking character column from tables that are set
  491. up for spreadsheet calculations. It also removes the entire lines
  492. marked with `!', `_', or `^'. The lines with `$' are kept, because
  493. the values of constants may be useful to have."
  494. :group 'org-export-tables
  495. :type 'boolean)
  496. (defcustom org-export-table-remove-empty-lines t
  497. "Remove empty lines when exporting tables.
  498. This is the global equivalent of the :remove-nil-lines option
  499. when locally sending a table with #+ORGTBL."
  500. :group 'org-export-tables
  501. :type 'boolean)
  502. (defcustom org-export-prefer-native-exporter-for-tables nil
  503. "Non-nil means always export tables created with table.el natively.
  504. Natively means use the HTML code generator in table.el.
  505. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  506. the table does not use row- or column-spanning). This has the
  507. advantage, that the automatic HTML conversions for math symbols and
  508. sub/superscripts can be applied. Org-mode's HTML generator is also
  509. much faster. The LaTeX exporter always use the native exporter for
  510. table.el tables."
  511. :group 'org-export-tables
  512. :type 'boolean)
  513. ;;;; Exporting
  514. ;;; Variables, constants, and parameter plists
  515. (defconst org-level-max 20)
  516. (defvar org-export-current-backend nil
  517. "During export, this will be bound to a symbol such as 'html,
  518. 'latex, 'docbook, 'ascii, etc, indicating which of the export
  519. backends is in use. Otherwise it has the value nil. Users
  520. should not attempt to change the value of this variable
  521. directly, but it can be used in code to test whether export is
  522. in progress, and if so, what the backend is.")
  523. (defvar org-current-export-file nil) ; dynamically scoped parameter
  524. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  525. (defvar org-export-opt-plist nil
  526. "Contains the current option plist.")
  527. (defvar org-last-level nil) ; dynamically scoped variable
  528. (defvar org-min-level nil) ; dynamically scoped variable
  529. (defvar org-levels-open nil) ; dynamically scoped parameter
  530. (defvar org-export-footnotes-data nil
  531. "Alist of labels used in buffers, along with their definition.")
  532. (defvar org-export-footnotes-seen nil
  533. "Alist of labels encountered so far by the exporter, along with their definition.")
  534. (defconst org-export-plist-vars
  535. '((:link-up nil org-export-html-link-up)
  536. (:link-home nil org-export-html-link-home)
  537. (:language nil org-export-default-language)
  538. (:keywords nil org-export-page-keywords)
  539. (:description nil org-export-page-description)
  540. (:customtime nil org-display-custom-times)
  541. (:headline-levels "H" org-export-headline-levels)
  542. (:section-numbers "num" org-export-with-section-numbers)
  543. (:section-number-format nil org-export-section-number-format)
  544. (:table-of-contents "toc" org-export-with-toc)
  545. (:preserve-breaks "\\n" org-export-preserve-breaks)
  546. (:archived-trees nil org-export-with-archived-trees)
  547. (:emphasize "*" org-export-with-emphasize)
  548. (:sub-superscript "^" org-export-with-sub-superscripts)
  549. (:special-strings "-" org-export-with-special-strings)
  550. (:footnotes "f" org-export-with-footnotes)
  551. (:drawers "d" org-export-with-drawers)
  552. (:tags "tags" org-export-with-tags)
  553. (:todo-keywords "todo" org-export-with-todo-keywords)
  554. (:tasks "tasks" org-export-with-tasks)
  555. (:priority "pri" org-export-with-priority)
  556. (:TeX-macros "TeX" org-export-with-TeX-macros)
  557. (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
  558. (:latex-listings nil org-export-latex-listings)
  559. (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
  560. (:fixed-width ":" org-export-with-fixed-width)
  561. (:timestamps "<" org-export-with-timestamps)
  562. (:author nil user-full-name)
  563. (:email nil user-mail-address)
  564. (:author-info "author" org-export-author-info)
  565. (:email-info "email" org-export-email-info)
  566. (:creator-info "creator" org-export-creator-info)
  567. (:time-stamp-file "timestamp" org-export-time-stamp-file)
  568. (:tables "|" org-export-with-tables)
  569. (:table-auto-headline nil org-export-highlight-first-table-line)
  570. (:style-include-default nil org-export-html-style-include-default)
  571. (:style-include-scripts nil org-export-html-style-include-scripts)
  572. (:style nil org-export-html-style)
  573. (:style-extra nil org-export-html-style-extra)
  574. (:agenda-style nil org-agenda-export-html-style)
  575. (:convert-org-links nil org-export-html-link-org-files-as-html)
  576. (:inline-images nil org-export-html-inline-images)
  577. (:html-extension nil org-export-html-extension)
  578. (:html-preamble nil org-export-html-preamble)
  579. (:html-postamble nil org-export-html-postamble)
  580. (:xml-declaration nil org-export-html-xml-declaration)
  581. (:html-table-tag nil org-export-html-table-tag)
  582. (:expand-quoted-html "@" org-export-html-expand)
  583. (:timestamp nil org-export-html-with-timestamp)
  584. (:publishing-directory nil org-export-publishing-directory)
  585. (:select-tags nil org-export-select-tags)
  586. (:exclude-tags nil org-export-exclude-tags)
  587. (:latex-image-options nil org-export-latex-image-default-option))
  588. "List of properties that represent export/publishing variables.
  589. Each element is a list of 3 items:
  590. 1. The property that is used internally, and also for org-publish-project-alist
  591. 2. The string that can be used in the OPTION lines to set this option,
  592. or nil if this option cannot be changed in this way
  593. 3. The customization variable that sets the default for this option."
  594. )
  595. (defun org-default-export-plist ()
  596. "Return the property list with default settings for the export variables."
  597. (let* ((infile (org-infile-export-plist))
  598. (letbind (plist-get infile :let-bind))
  599. (l org-export-plist-vars) rtn e s v)
  600. (while (setq e (pop l))
  601. (setq s (nth 2 e)
  602. v (cond
  603. ((assq s letbind) (nth 1 (assq s letbind)))
  604. ((boundp s) (symbol-value s))
  605. (t nil))
  606. rtn (cons (car e) (cons v rtn))))
  607. rtn))
  608. (defvar org-export-inbuffer-options-extra nil
  609. "List of additional in-buffer options that should be detected.
  610. Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
  611. etc. Extensions can add to this list to get their options detected, and they
  612. can then add a function to `org-export-options-filters' to process these
  613. options.
  614. Each element in this list must be a list, with the in-buffer keyword as car,
  615. and a property (a symbol) as the next element. All occurrences of the
  616. keyword will be found, the values concatenated with a space character
  617. in between, and the result stored in the export options property list.")
  618. (defvar org-export-options-filters nil
  619. "Functions to be called to finalize the export/publishing options.
  620. All these options are stored in a property list, and each of the functions
  621. in this hook gets a chance to modify this property list. Each function
  622. must accept the property list as an argument, and must return the (possibly
  623. modified) list.")
  624. ;; FIXME: should we fold case here?
  625. (defun org-infile-export-plist ()
  626. "Return the property list with file-local settings for export."
  627. (save-excursion
  628. (save-restriction
  629. (widen)
  630. (goto-char (point-min))
  631. (let ((re (org-make-options-regexp
  632. (append
  633. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
  634. "MATHJAX"
  635. "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
  636. "LATEX_HEADER" "LATEX_CLASS" "LATEX_CLASS_OPTIONS"
  637. "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
  638. "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
  639. (mapcar 'car org-export-inbuffer-options-extra))))
  640. (case-fold-search t)
  641. p key val text options mathjax a pr style
  642. latex-header latex-class latex-class-options macros letbind
  643. ext-setup-or-nil setup-file setup-dir setup-contents (start 0))
  644. (while (or (and ext-setup-or-nil
  645. (string-match re ext-setup-or-nil start)
  646. (setq start (match-end 0)))
  647. (and (setq ext-setup-or-nil nil start 0)
  648. (re-search-forward re nil t)))
  649. (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
  650. val (org-match-string-no-properties 2 ext-setup-or-nil))
  651. (cond
  652. ((setq a (assoc key org-export-inbuffer-options-extra))
  653. (setq pr (nth 1 a))
  654. (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
  655. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  656. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  657. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  658. ((string-equal key "DATE")
  659. ;; If date is an Org timestamp, convert it to a time
  660. ;; string using `org-export-date-timestamp-format'
  661. (when (string-match org-ts-regexp3 val)
  662. (setq val (format-time-string
  663. org-export-date-timestamp-format
  664. (apply 'encode-time (org-parse-time-string
  665. (match-string 0 val))))))
  666. (setq p (plist-put p :date val)))
  667. ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
  668. ((string-equal key "DESCRIPTION")
  669. (setq p (plist-put p :description val)))
  670. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  671. ((string-equal key "STYLE")
  672. (setq style (concat style "\n" val)))
  673. ((string-equal key "LATEX_HEADER")
  674. (setq latex-header (concat latex-header "\n" val)))
  675. ((string-equal key "LATEX_CLASS")
  676. (setq latex-class val))
  677. ((string-equal key "LATEX_CLASS_OPTIONS")
  678. (setq latex-class-options val))
  679. ((string-equal key "TEXT")
  680. (setq text (if text (concat text "\n" val) val)))
  681. ((string-equal key "OPTIONS")
  682. (setq options (concat val " " options)))
  683. ((string-equal key "MATHJAX")
  684. (setq mathjax (concat val " " mathjax)))
  685. ((string-equal key "BIND")
  686. (push (read (concat "(" val ")")) letbind))
  687. ((string-equal key "XSLT")
  688. (setq p (plist-put p :xslt val)))
  689. ((string-equal key "LINK_UP")
  690. (setq p (plist-put p :link-up val)))
  691. ((string-equal key "LINK_HOME")
  692. (setq p (plist-put p :link-home val)))
  693. ((string-equal key "EXPORT_SELECT_TAGS")
  694. (setq p (plist-put p :select-tags (org-split-string val))))
  695. ((string-equal key "EXPORT_EXCLUDE_TAGS")
  696. (setq p (plist-put p :exclude-tags (org-split-string val))))
  697. ((string-equal key "MACRO")
  698. (push val macros))
  699. ((equal key "SETUPFILE")
  700. (setq setup-file (org-remove-double-quotes (org-trim val))
  701. ;; take care of recursive inclusion of setupfiles
  702. setup-file (if (or (file-name-absolute-p val) (not setup-dir))
  703. (expand-file-name setup-file)
  704. (let ((default-directory setup-dir))
  705. (expand-file-name setup-file))))
  706. (setq setup-dir (file-name-directory setup-file))
  707. (setq setup-contents (org-file-contents setup-file 'noerror))
  708. (if (not ext-setup-or-nil)
  709. (setq ext-setup-or-nil setup-contents start 0)
  710. (setq ext-setup-or-nil
  711. (concat (substring ext-setup-or-nil 0 start)
  712. "\n" setup-contents "\n"
  713. (substring ext-setup-or-nil start)))))))
  714. (setq p (plist-put p :text text))
  715. (when (and letbind (org-export-confirm-letbind))
  716. (setq p (plist-put p :let-bind letbind)))
  717. (when style (setq p (plist-put p :style-extra style)))
  718. (when latex-header
  719. (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
  720. (when latex-class
  721. (setq p (plist-put p :latex-class latex-class)))
  722. (when latex-class-options
  723. (setq p (plist-put p :latex-class-options latex-class-options)))
  724. (when options
  725. (setq p (org-export-add-options-to-plist p options)))
  726. (when mathjax
  727. (setq p (plist-put p :mathjax mathjax)))
  728. ;; Add macro definitions
  729. (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
  730. (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
  731. (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
  732. (setq p (plist-put
  733. p :macro-modification-time
  734. (and (buffer-file-name)
  735. (file-exists-p (buffer-file-name))
  736. (concat
  737. "(eval (format-time-string \"$1\" '"
  738. (prin1-to-string (nth 5 (file-attributes
  739. (buffer-file-name))))
  740. "))"))))
  741. (setq p (plist-put p :macro-input-file (and (buffer-file-name)
  742. (file-name-nondirectory
  743. (buffer-file-name)))))
  744. (while (setq val (pop macros))
  745. (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
  746. (setq p (plist-put
  747. p (intern
  748. (concat ":macro-" (downcase (match-string 1 val))))
  749. (org-export-interpolate-newlines (match-string 2 val))))))
  750. p))))
  751. (defun org-export-interpolate-newlines (s)
  752. (while (string-match "\\\\n" s)
  753. (setq s (replace-match "\n" t t s)))
  754. s)
  755. (defvar org-export-allow-BIND-local nil)
  756. (defun org-export-confirm-letbind ()
  757. "Can we use #+BIND values during export?
  758. By default this will ask for confirmation by the user, to divert possible
  759. security risks."
  760. (cond
  761. ((not org-export-allow-BIND) nil)
  762. ((eq org-export-allow-BIND t) t)
  763. ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
  764. org-export-allow-BIND-local)
  765. (t (org-set-local 'org-export-allow-BIND-local
  766. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  767. (defun org-install-letbind ()
  768. "Install the values from #+BIND lines as local variables."
  769. (let ((letbind (plist-get org-export-opt-plist :let-bind))
  770. pair)
  771. (while (setq pair (pop letbind))
  772. (org-set-local (car pair) (nth 1 pair)))))
  773. (defun org-export-add-options-to-plist (p options)
  774. "Parse an OPTIONS line and set values in the property list P."
  775. (let (o)
  776. (when options
  777. (let ((op org-export-plist-vars))
  778. (while (setq o (pop op))
  779. (if (and (nth 1 o)
  780. (string-match (concat "\\(\\`\\|[ \t]\\)"
  781. (regexp-quote (nth 1 o))
  782. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  783. options))
  784. (setq p (plist-put p (car o)
  785. (car (read-from-string
  786. (match-string 2 options))))))))))
  787. p)
  788. (defun org-export-add-subtree-options (p pos)
  789. "Add options in subtree at position POS to property list P."
  790. (save-excursion
  791. (goto-char pos)
  792. (when (org-at-heading-p)
  793. (let (a)
  794. ;; This is actually read in `org-export-get-title-from-subtree'
  795. ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
  796. ;; (setq p (plist-put p :title a)))
  797. (when (setq a (org-entry-get pos "EXPORT_TEXT"))
  798. (setq p (plist-put p :text a)))
  799. (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
  800. (setq p (plist-put p :author a)))
  801. (when (setq a (org-entry-get pos "EXPORT_DATE"))
  802. (setq p (plist-put p :date a)))
  803. (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
  804. (setq p (org-export-add-options-to-plist p a)))))
  805. p))
  806. (defun org-export-directory (type plist)
  807. (let* ((val (plist-get plist :publishing-directory))
  808. (dir (if (listp val)
  809. (or (cdr (assoc type val)) ".")
  810. val)))
  811. dir))
  812. (defun org-export-process-option-filters (plist)
  813. (let ((functions org-export-options-filters) f)
  814. (while (setq f (pop functions))
  815. (setq plist (funcall f plist))))
  816. plist)
  817. ;;;###autoload
  818. (defun org-export (&optional arg)
  819. "Export dispatcher for Org-mode.
  820. When `org-export-run-in-background' is non-nil, try to run the command
  821. in the background. This will be done only for commands that write
  822. to a file. For details see the docstring of `org-export-run-in-background'.
  823. The prefix argument ARG will be passed to the exporter. However, if
  824. ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
  825. that means to inverse the
  826. value of `org-export-run-in-background'.
  827. If `org-export-initial-scope' is set to 'subtree, try to export
  828. the current subtree, otherwise try to export the whole buffer.
  829. Pressing `1' will switch between these two options."
  830. (interactive "P")
  831. (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
  832. (subtree-p (or (org-region-active-p)
  833. (eq org-export-initial-scope 'subtree)))
  834. (help "[t] insert the export option template
  835. \[v] limit export to visible part of outline tree
  836. \[1] switch buffer/subtree export
  837. \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
  838. \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
  839. \[h] export as HTML [H] to temporary buffer [R] export region
  840. \[b] export as HTML and open in browser
  841. \[l] export as LaTeX [L] to temporary buffer
  842. \[p] export as LaTeX and process to PDF [d] ... and open PDF file
  843. \[D] export as DocBook [V] export as DocBook, process to PDF, and open
  844. \[o] export as OpenDocument Text [O] ... and open
  845. \[j] export as TaskJuggler [J] ... and open
  846. \[m] export as Freemind mind map
  847. \[x] export as XOXO
  848. \[g] export using Wes Hardaker's generic exporter
  849. \[i] export current file as iCalendar file
  850. \[I] export all agenda files as iCalendar files [c] ...as one combined file
  851. \[F] publish current file [P] publish current project
  852. \[X] publish a project... [E] publish every projects")
  853. (cmds
  854. '((?t org-insert-export-options-template nil)
  855. (?v org-export-visible nil)
  856. (?a org-export-as-ascii t)
  857. (?A org-export-as-ascii-to-buffer t)
  858. (?n org-export-as-latin1 t)
  859. (?N org-export-as-latin1-to-buffer t)
  860. (?u org-export-as-utf8 t)
  861. (?U org-export-as-utf8-to-buffer t)
  862. (?h org-export-as-html t)
  863. (?b org-export-as-html-and-open t)
  864. (?H org-export-as-html-to-buffer nil)
  865. (?R org-export-region-as-html nil)
  866. (?x org-export-as-xoxo t)
  867. (?g org-export-generic t)
  868. (?D org-export-as-docbook t)
  869. (?V org-export-as-docbook-pdf-and-open t)
  870. (?o org-export-as-odt t)
  871. (?O org-export-as-odt-and-open t)
  872. (?j org-export-as-taskjuggler t)
  873. (?J org-export-as-taskjuggler-and-open t)
  874. (?m org-export-as-freemind t)
  875. (?l org-export-as-latex t)
  876. (?p org-export-as-pdf t)
  877. (?d org-export-as-pdf-and-open t)
  878. (?L org-export-as-latex-to-buffer nil)
  879. (?i org-export-icalendar-this-file t)
  880. (?I org-export-icalendar-all-agenda-files t)
  881. (?c org-export-icalendar-combine-agenda-files t)
  882. (?F org-publish-current-file t)
  883. (?P org-publish-current-project t)
  884. (?X org-publish t)
  885. (?E org-publish-all t)))
  886. r1 r2 ass
  887. (cpos (point)) (cbuf (current-buffer)) bpos)
  888. (save-excursion
  889. (save-window-excursion
  890. (if subtree-p
  891. (message "Export subtree: ")
  892. (message "Export buffer: "))
  893. (delete-other-windows)
  894. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  895. (princ help))
  896. (org-fit-window-to-buffer (get-buffer-window
  897. "*Org Export/Publishing Help*"))
  898. (while (eq (setq r1 (read-char-exclusive)) ?1)
  899. (cond (subtree-p
  900. (setq subtree-p nil)
  901. (message "Export buffer: "))
  902. ((not subtree-p)
  903. (setq subtree-p t)
  904. (setq bpos (point))
  905. (message "Export subtree: "))))
  906. (when (eq r1 ?\ )
  907. (let ((case-fold-search t)
  908. (end (save-excursion (while (org-up-heading-safe)) (point))))
  909. (outline-next-heading)
  910. (if (re-search-backward
  911. "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
  912. end t)
  913. (progn
  914. (org-back-to-heading t)
  915. (setq subtree-p t)
  916. (setq bpos (point))
  917. (message "Select command (for subtree): ")
  918. (setq r1 (read-char-exclusive)))
  919. (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
  920. )))))
  921. (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay)
  922. (and bpos (goto-char bpos))
  923. (setq r2 (if (< r1 27) (+ r1 96) r1))
  924. (unless (setq ass (assq r2 cmds))
  925. (error "No command associated with key %c" r1))
  926. (if (and bg (nth 2 ass)
  927. (not (buffer-base-buffer))
  928. (not (org-region-active-p)))
  929. ;; execute in background
  930. (let ((p (start-process
  931. (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
  932. "*Org Processes*"
  933. (expand-file-name invocation-name invocation-directory)
  934. "-batch"
  935. "-l" user-init-file
  936. "--eval" "(require 'org-exp)"
  937. "--eval" "(setq org-wait .2)"
  938. (buffer-file-name)
  939. "-f" (symbol-name (nth 1 ass)))))
  940. (set-process-sentinel p 'org-export-process-sentinel)
  941. (message "Background process \"%s\": started" p))
  942. ;; background processing not requested, or not possible
  943. (if subtree-p (progn (org-mark-subtree) (org-activate-mark)))
  944. (call-interactively (nth 1 ass))
  945. (when (and bpos (get-buffer-window cbuf))
  946. (let ((cw (selected-window)))
  947. (select-window (get-buffer-window cbuf))
  948. (goto-char cpos)
  949. (deactivate-mark)
  950. (select-window cw))))))
  951. (defun org-export-process-sentinel (process status)
  952. (if (string-match "\n+\\'" status)
  953. (setq status (substring status 0 -1)))
  954. (message "Background process \"%s\": %s" process status))
  955. ;;; General functions for all backends
  956. (defvar org-export-target-aliases nil
  957. "Alist of targets with invisible aliases.")
  958. (defvar org-export-preferred-target-alist nil
  959. "Alist of section id's with preferred aliases.")
  960. (defvar org-export-id-target-alist nil
  961. "Alist of section id's with preferred aliases.")
  962. (defvar org-export-code-refs nil
  963. "Alist of code references and line numbers.")
  964. (defun org-export-preprocess-string (string &rest parameters)
  965. "Cleanup STRING so that the true exported has a more consistent source.
  966. This function takes STRING, which should be a buffer-string of an org-file
  967. to export. It then creates a temporary buffer where it does its job.
  968. The result is then again returned as a string, and the exporter works
  969. on this string to produce the exported version."
  970. (interactive)
  971. (let* ((org-export-current-backend (or (plist-get parameters :for-backend)
  972. org-export-current-backend))
  973. (archived-trees (plist-get parameters :archived-trees))
  974. (inhibit-read-only t)
  975. (drawers org-drawers)
  976. (source-buffer (current-buffer))
  977. target-alist rtn)
  978. (setq org-export-target-aliases nil
  979. org-export-preferred-target-alist nil
  980. org-export-id-target-alist nil
  981. org-export-code-refs nil)
  982. (with-temp-buffer
  983. (erase-buffer)
  984. (insert string)
  985. (setq case-fold-search t)
  986. (let ((inhibit-read-only t))
  987. (remove-text-properties (point-min) (point-max)
  988. '(read-only t)))
  989. ;; Remove license-to-kill stuff
  990. ;; The caller marks some stuff for killing, stuff that has been
  991. ;; used to create the page title, for example.
  992. (org-export-kill-licensed-text)
  993. (let ((org-inhibit-startup t)) (org-mode))
  994. (setq case-fold-search t)
  995. (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
  996. (org-install-letbind)
  997. ;; Call the hook
  998. (run-hooks 'org-export-preprocess-hook)
  999. (untabify (point-min) (point-max))
  1000. ;; Handle include files, and call a hook
  1001. (org-export-handle-include-files-recurse)
  1002. (run-hooks 'org-export-preprocess-after-include-files-hook)
  1003. ;; Get rid of archived trees
  1004. (org-export-remove-archived-trees archived-trees)
  1005. ;; Remove comment environment and comment subtrees
  1006. (org-export-remove-comment-blocks-and-subtrees)
  1007. ;; Get rid of excluded trees, and call a hook
  1008. (org-export-handle-export-tags (plist-get parameters :select-tags)
  1009. (plist-get parameters :exclude-tags))
  1010. (run-hooks 'org-export-preprocess-after-tree-selection-hook)
  1011. ;; Get rid of tasks, depending on configuration
  1012. (org-export-remove-tasks (plist-get parameters :tasks))
  1013. ;; Prepare footnotes for export. During that process, footnotes
  1014. ;; actually included in the exported part of the buffer go
  1015. ;; though some transformations:
  1016. ;; 1. They have their label normalized (like "[N]");
  1017. ;; 2. They get moved at the same place in the buffer (usually at
  1018. ;; its end, but backends may define another place via
  1019. ;; `org-footnote-insert-pos-for-preprocessor');
  1020. ;; 3. The are stored in `org-export-footnotes-seen', while
  1021. ;; `org-export-preprocess-string' is applied to their
  1022. ;; definition.
  1023. ;; Line-wise exporters ignore `org-export-footnotes-seen', as
  1024. ;; they interpret footnotes at the moment they see them in the
  1025. ;; buffer. Context-wise exporters grab all the info needed in
  1026. ;; that variable and delete moved definitions (as described in
  1027. ;; 2nd step).
  1028. (when (plist-get parameters :footnotes)
  1029. (org-footnote-normalize nil parameters))
  1030. ;; Change lists ending. Other parts of export may insert blank
  1031. ;; lines and lists' structure could be altered.
  1032. (org-export-mark-list-end)
  1033. ;; Process the macros
  1034. (org-export-preprocess-apply-macros)
  1035. (run-hooks 'org-export-preprocess-after-macros-hook)
  1036. ;; Export code blocks
  1037. (org-export-blocks-preprocess)
  1038. ;; Mark lists with properties
  1039. (org-export-mark-list-properties)
  1040. ;; Handle source code snippets
  1041. (org-export-replace-src-segments-and-examples)
  1042. ;; Protect short examples marked by a leading colon
  1043. (org-export-protect-colon-examples)
  1044. ;; Protected spaces
  1045. (org-export-convert-protected-spaces)
  1046. ;; Find all headings and compute the targets for them
  1047. (setq target-alist (org-export-define-heading-targets target-alist))
  1048. (run-hooks 'org-export-preprocess-after-headline-targets-hook)
  1049. ;; Find HTML special classes for headlines
  1050. (org-export-remember-html-container-classes)
  1051. ;; Get rid of drawers
  1052. (org-export-remove-or-extract-drawers
  1053. drawers (plist-get parameters :drawers))
  1054. ;; Get the correct stuff before the first headline
  1055. (when (plist-get parameters :skip-before-1st-heading)
  1056. (goto-char (point-min))
  1057. (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
  1058. (delete-region (point-min) (match-beginning 0))
  1059. (goto-char (point-min))
  1060. (insert "\n")))
  1061. (when (plist-get parameters :add-text)
  1062. (goto-char (point-min))
  1063. (insert (plist-get parameters :add-text) "\n"))
  1064. ;; Remove todo-keywords before exporting, if the user has requested so
  1065. (org-export-remove-headline-metadata parameters)
  1066. ;; Find targets in comments and move them out of comments,
  1067. ;; but mark them as targets that should be invisible
  1068. (setq target-alist (org-export-handle-invisible-targets target-alist))
  1069. ;; Select and protect backend specific stuff, throw away stuff
  1070. ;; that is specific for other backends
  1071. (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
  1072. (org-export-select-backend-specific-text)
  1073. ;; Protect quoted subtrees
  1074. (org-export-protect-quoted-subtrees)
  1075. ;; Remove clock lines
  1076. (org-export-remove-clock-lines)
  1077. ;; Protect verbatim elements
  1078. (org-export-protect-verbatim)
  1079. ;; Blockquotes, verse, and center
  1080. (org-export-mark-blockquote-verse-center)
  1081. (run-hooks 'org-export-preprocess-after-blockquote-hook)
  1082. ;; Remove timestamps, if the user has requested so
  1083. (unless (plist-get parameters :timestamps)
  1084. (org-export-remove-timestamps))
  1085. ;; Attach captions to the correct object
  1086. (setq target-alist (org-export-attach-captions-and-attributes target-alist))
  1087. ;; Find matches for radio targets and turn them into internal links
  1088. (org-export-mark-radio-links)
  1089. (run-hooks 'org-export-preprocess-after-radio-targets-hook)
  1090. ;; Find all links that contain a newline and put them into a single line
  1091. (org-export-concatenate-multiline-links)
  1092. ;; Normalize links: Convert angle and plain links into bracket links
  1093. ;; and expand link abbreviations
  1094. (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
  1095. (org-export-normalize-links)
  1096. ;; Find all internal links. If they have a fuzzy match (i.e. not
  1097. ;; a *dedicated* target match, let the link point to the
  1098. ;; corresponding section.
  1099. (org-export-target-internal-links target-alist)
  1100. ;; Find multiline emphasis and put them into single line
  1101. (when (plist-get parameters :emph-multiline)
  1102. (org-export-concatenate-multiline-emphasis))
  1103. ;; Remove special table lines, and store alignment information
  1104. (org-store-forced-table-alignment)
  1105. (when org-export-table-remove-special-lines
  1106. (org-export-remove-special-table-lines))
  1107. ;; Another hook
  1108. (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
  1109. ;; Backend-specific preprocessing
  1110. (let* ((backend-name (symbol-name org-export-current-backend))
  1111. (f (intern (format "org-export-%s-preprocess" backend-name))))
  1112. (require (intern (concat "org-" backend-name)) nil)
  1113. (funcall f parameters))
  1114. ;; Remove or replace comments
  1115. (org-export-handle-comments (plist-get parameters :comments))
  1116. ;; Remove #+TBLFM and #+TBLNAME lines
  1117. (org-export-handle-table-metalines)
  1118. ;; Remove #+results and #+name lines
  1119. (org-export-res/src-name-cleanup)
  1120. ;; Run the final hook
  1121. (run-hooks 'org-export-preprocess-final-hook)
  1122. (setq rtn (buffer-string)))
  1123. rtn))
  1124. (defun org-export-kill-licensed-text ()
  1125. "Remove all text that is marked with a :org-license-to-kill property."
  1126. (let (p)
  1127. (while (setq p (text-property-any (point-min) (point-max)
  1128. :org-license-to-kill t))
  1129. (delete-region
  1130. p (or (next-single-property-change p :org-license-to-kill)
  1131. (point-max))))))
  1132. (defvar org-export-define-heading-targets-headline-hook nil
  1133. "Hook that is run when a headline was matched during target search.
  1134. This is part of the preprocessing for export.")
  1135. (defun org-export-define-heading-targets (target-alist)
  1136. "Find all headings and define the targets for them.
  1137. The new targets are added to TARGET-ALIST, which is also returned.
  1138. Also find all ID and CUSTOM_ID properties and store them."
  1139. (goto-char (point-min))
  1140. (org-init-section-numbers)
  1141. (let ((re (concat "^" org-outline-regexp
  1142. "\\|"
  1143. "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
  1144. level target last-section-target a id)
  1145. (while (re-search-forward re nil t)
  1146. (org-if-unprotected-at (match-beginning 0)
  1147. (if (match-end 2)
  1148. (progn
  1149. (setq id (org-match-string-no-properties 2))
  1150. (push (cons id target) target-alist)
  1151. (setq a (or (assoc last-section-target org-export-target-aliases)
  1152. (progn
  1153. (push (list last-section-target)
  1154. org-export-target-aliases)
  1155. (car org-export-target-aliases))))
  1156. (push (caar target-alist) (cdr a))
  1157. (when (equal (match-string 1) "CUSTOM_ID")
  1158. (if (not (assoc last-section-target
  1159. org-export-preferred-target-alist))
  1160. (push (cons last-section-target id)
  1161. org-export-preferred-target-alist)))
  1162. (when (equal (match-string 1) "ID")
  1163. (if (not (assoc last-section-target
  1164. org-export-id-target-alist))
  1165. (push (cons last-section-target (concat "ID-" id))
  1166. org-export-id-target-alist))))
  1167. (setq level (org-reduced-level
  1168. (save-excursion (goto-char (point-at-bol))
  1169. (org-outline-level))))
  1170. (setq target (org-solidify-link-text
  1171. (format "sec-%s" (replace-regexp-in-string
  1172. "\\." "-"
  1173. (org-section-number level)))))
  1174. (setq last-section-target target)
  1175. (push (cons target target) target-alist)
  1176. (add-text-properties
  1177. (point-at-bol) (point-at-eol)
  1178. (list 'target target))
  1179. (run-hooks 'org-export-define-heading-targets-headline-hook)))))
  1180. target-alist)
  1181. (defun org-export-handle-invisible-targets (target-alist)
  1182. "Find targets in comments and move them out of comments.
  1183. Mark them as invisible targets."
  1184. (let (target tmp a)
  1185. (goto-char (point-min))
  1186. (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
  1187. ;; Check if the line before or after is a headline with a target
  1188. (if (setq target (or (get-text-property (point-at-bol 0) 'target)
  1189. (get-text-property (point-at-bol 2) 'target)))
  1190. (progn
  1191. ;; use the existing target in a neighboring line
  1192. (setq tmp (match-string 2))
  1193. (replace-match "")
  1194. (and (looking-at "\n") (delete-char 1))
  1195. (push (cons (setq tmp (org-solidify-link-text tmp)) target)
  1196. target-alist)
  1197. (setq a (or (assoc target org-export-target-aliases)
  1198. (progn
  1199. (push (list target) org-export-target-aliases)
  1200. (car org-export-target-aliases))))
  1201. (push tmp (cdr a)))
  1202. ;; Make an invisible target
  1203. (replace-match "\\1(INVISIBLE)"))))
  1204. target-alist)
  1205. (defun org-export-target-internal-links (target-alist)
  1206. "Find all internal links and assign targets to them.
  1207. If a link has a fuzzy match (i.e. not a *dedicated* target match),
  1208. let the link point to the corresponding section.
  1209. This function also handles the id links, if they have a match in
  1210. the current file."
  1211. (goto-char (point-min))
  1212. (while (re-search-forward org-bracket-link-regexp nil t)
  1213. (org-if-unprotected-at (1+ (match-beginning 0))
  1214. (let* ((org-link-search-must-match-exact-headline t)
  1215. (md (match-data))
  1216. (desc (match-end 2))
  1217. (link (org-link-unescape (match-string 1)))
  1218. (slink (org-solidify-link-text link))
  1219. found props pos cref
  1220. (target
  1221. (cond
  1222. ((= (string-to-char link) ?#)
  1223. ;; user wants exactly this link
  1224. link)
  1225. ((cdr (assoc slink target-alist))
  1226. (or (cdr (assoc (assoc slink target-alist)
  1227. org-export-preferred-target-alist))
  1228. (cdr (assoc slink target-alist))))
  1229. ((and (string-match "^id:" link)
  1230. (cdr (assoc (substring link 3) target-alist))))
  1231. ((string-match "^(\\(.*\\))$" link)
  1232. (setq cref (match-string 1 link))
  1233. (concat "coderef:" cref))
  1234. ((string-match org-link-types-re link) nil)
  1235. ((or (file-name-absolute-p link)
  1236. (string-match "^\\." link))
  1237. nil)
  1238. (t
  1239. (let ((org-link-search-inhibit-query t))
  1240. (save-excursion
  1241. (setq found (condition-case nil (org-link-search link)
  1242. (error nil)))
  1243. (when (and found
  1244. (or (org-on-heading-p)
  1245. (not (eq found 'dedicated))))
  1246. (or (get-text-property (point) 'target)
  1247. (get-text-property
  1248. (max (point-min)
  1249. (1- (or (previous-single-property-change
  1250. (point) 'target) 0)))
  1251. 'target)))))))))
  1252. (when target
  1253. (set-match-data md)
  1254. (goto-char (match-beginning 1))
  1255. (setq props (text-properties-at (point)))
  1256. (delete-region (match-beginning 1) (match-end 1))
  1257. (setq pos (point))
  1258. (insert target)
  1259. (unless desc (insert "][" link))
  1260. (add-text-properties pos (point) props))))))
  1261. (defun org-export-remember-html-container-classes ()
  1262. "Store the HTML_CONTAINER_CLASS properties in a text property."
  1263. (goto-char (point-min))
  1264. (let (class)
  1265. (while (re-search-forward
  1266. "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
  1267. (setq class (match-string 1))
  1268. (save-excursion
  1269. (org-back-to-heading t)
  1270. (put-text-property (point-at-bol) (point-at-eol) 'html-container-class class)))))
  1271. (defvar org-export-format-drawer-function nil
  1272. "Function to be called to format the contents of a drawer.
  1273. The function must accept two parameters:
  1274. NAME the drawer name, like \"PROPERTIES\"
  1275. CONTENT the content of the drawer.
  1276. You can check the export backend through `org-export-current-backend'.
  1277. The function should return the text to be inserted into the buffer.
  1278. If this is nil, `org-export-format-drawer' is used as a default.")
  1279. (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
  1280. "Remove drawers, or extract and format the content.
  1281. ALL-DRAWERS is a list of all drawer names valid in the current buffer.
  1282. EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
  1283. whose content to keep. Any drawers that are in ALL-DRAWERS but not in
  1284. EXP-DRAWERS will be removed."
  1285. (goto-char (point-min))
  1286. (let ((re (concat "^[ \t]*:\\("
  1287. (mapconcat 'identity all-drawers "\\|")
  1288. "\\):[ \t]*$"))
  1289. name beg beg-content eol content)
  1290. (while (re-search-forward re nil t)
  1291. (org-if-unprotected
  1292. (setq name (match-string 1))
  1293. (setq beg (match-beginning 0)
  1294. beg-content (1+ (point-at-eol))
  1295. eol (point-at-eol))
  1296. (if (not (and (re-search-forward
  1297. "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
  1298. (match-end 1)))
  1299. (goto-char eol)
  1300. (goto-char (match-beginning 0))
  1301. (and (looking-at ".*\n?") (replace-match ""))
  1302. (setq content (buffer-substring beg-content (point)))
  1303. (delete-region beg (point))
  1304. (when (or (eq exp-drawers t)
  1305. (member name exp-drawers))
  1306. (setq content (funcall (or org-export-format-drawer-function
  1307. 'org-export-format-drawer)
  1308. name content))
  1309. (insert content)))))))
  1310. (defun org-export-format-drawer (name content)
  1311. "Format the content of a drawer as a colon example."
  1312. (if (string-match "[ \t]+\\'" content)
  1313. (setq content (substring content (match-beginning 0))))
  1314. (while (string-match "\\`[ \t]*\n" content)
  1315. (setq content (substring content (match-end 0))))
  1316. (setq content (org-remove-indentation content))
  1317. (setq content (concat ": " (mapconcat 'identity
  1318. (org-split-string content "\n")
  1319. "\n: ")
  1320. "\n"))
  1321. (setq content (concat " : " (upcase name) "\n" content))
  1322. (org-add-props content nil 'org-protected t))
  1323. (defun org-export-handle-export-tags (select-tags exclude-tags)
  1324. "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
  1325. Both arguments are lists of tags.
  1326. If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
  1327. will be removed.
  1328. After that, all subtrees that are marked by EXCLUDE-TAGS will be
  1329. removed as well."
  1330. (remove-text-properties (point-min) (point-max) '(:org-delete t))
  1331. (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
  1332. select-tags "\\|")
  1333. "\\):"))
  1334. (re-excl (concat ":\\(" (mapconcat 'regexp-quote
  1335. exclude-tags "\\|")
  1336. "\\):"))
  1337. beg end cont)
  1338. (goto-char (point-min))
  1339. (when (and select-tags
  1340. (re-search-forward
  1341. (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
  1342. ;; At least one tree is marked for export, this means
  1343. ;; all the unmarked stuff needs to go.
  1344. ;; Dig out the trees that should be exported
  1345. (goto-char (point-min))
  1346. (outline-next-heading)
  1347. (setq beg (point))
  1348. (put-text-property beg (point-max) :org-delete t)
  1349. (while (re-search-forward re-sel nil t)
  1350. (when (org-on-heading-p)
  1351. (org-back-to-heading)
  1352. (remove-text-properties
  1353. (max (1- (point)) (point-min))
  1354. (setq cont (save-excursion (org-end-of-subtree t t)))
  1355. '(:org-delete t))
  1356. (while (and (org-up-heading-safe)
  1357. (get-text-property (point) :org-delete))
  1358. (remove-text-properties (max (1- (point)) (point-min))
  1359. (point-at-eol) '(:org-delete t)))
  1360. (goto-char cont))))
  1361. ;; Remove the trees explicitly marked for noexport
  1362. (when exclude-tags
  1363. (goto-char (point-min))
  1364. (while (re-search-forward re-excl nil t)
  1365. (when (org-at-heading-p)
  1366. (org-back-to-heading t)
  1367. (setq beg (point))
  1368. (org-end-of-subtree t t)
  1369. (delete-region beg (point))
  1370. (when (featurep 'org-inlinetask)
  1371. (org-inlinetask-remove-END-maybe)))))
  1372. ;; Remove everything that is now still marked for deletion
  1373. (goto-char (point-min))
  1374. (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
  1375. (setq end (or (next-single-property-change beg :org-delete)
  1376. (point-max)))
  1377. (delete-region beg end))))
  1378. (defun org-export-remove-tasks (keep)
  1379. "Remove tasks depending on configuration.
  1380. When KEEP is nil, remove all tasks.
  1381. When KEEP is `todo', remove the tasks that are DONE.
  1382. When KEEP is `done', remove the tasks that are not yet done.
  1383. When it is a list of strings, keep only tasks with these TODO keywords."
  1384. (when (or (listp keep) (memq keep '(todo done nil)))
  1385. (let ((re (concat "^\\*+[ \t]+\\("
  1386. (mapconcat
  1387. 'regexp-quote
  1388. (cond ((not keep) org-todo-keywords-1)
  1389. ((eq keep 'todo) org-done-keywords)
  1390. ((eq keep 'done) org-not-done-keywords)
  1391. ((listp keep)
  1392. (org-delete-all keep (copy-sequence
  1393. org-todo-keywords-1))))
  1394. "\\|")
  1395. "\\)\\($\\|[ \t]\\)"))
  1396. (case-fold-search nil)
  1397. beg)
  1398. (goto-char (point-min))
  1399. (while (re-search-forward re nil t)
  1400. (org-if-unprotected
  1401. (setq beg (match-beginning 0))
  1402. (org-end-of-subtree t t)
  1403. (if (looking-at "^\\*+[ \t]+END[ \t]*$")
  1404. ;; Kill the END line of the inline task
  1405. (goto-char (min (point-max) (1+ (match-end 0)))))
  1406. (delete-region beg (point)))))))
  1407. (defun org-export-remove-archived-trees (export-archived-trees)
  1408. "Remove archived trees.
  1409. When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
  1410. When it is t, the entire archived tree will be exported.
  1411. When it is nil the entire tree including the headline will be removed
  1412. from the buffer."
  1413. (let ((re-archive (concat ":" org-archive-tag ":"))
  1414. a b)
  1415. (when (not (eq export-archived-trees t))
  1416. (goto-char (point-min))
  1417. (while (re-search-forward re-archive nil t)
  1418. (if (not (org-on-heading-p t))
  1419. (goto-char (point-at-eol))
  1420. (beginning-of-line 1)
  1421. (setq a (if export-archived-trees
  1422. (1+ (point-at-eol)) (point))
  1423. b (org-end-of-subtree t))
  1424. (if (> b a) (delete-region a b)))))))
  1425. (defun org-export-remove-headline-metadata (opts)
  1426. "Remove meta data from the headline, according to user options."
  1427. (let ((re org-complex-heading-regexp)
  1428. (todo (plist-get opts :todo-keywords))
  1429. (tags (plist-get opts :tags))
  1430. (pri (plist-get opts :priority))
  1431. (elts '(1 2 3 4 5))
  1432. (case-fold-search nil)
  1433. rpl)
  1434. (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
  1435. (when (or (not todo) (not tags) (not pri))
  1436. (goto-char (point-min))
  1437. (while (re-search-forward re nil t)
  1438. (org-if-unprotected
  1439. (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
  1440. elts " "))
  1441. (replace-match rpl t t))))))
  1442. (defun org-export-remove-timestamps ()
  1443. "Remove timestamps and keywords for export."
  1444. (goto-char (point-min))
  1445. (while (re-search-forward org-maybe-keyword-time-regexp nil t)
  1446. (backward-char 1)
  1447. (org-if-unprotected
  1448. (unless (save-match-data (org-at-table-p))
  1449. (replace-match "")
  1450. (beginning-of-line 1)
  1451. (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
  1452. (replace-match ""))))))
  1453. (defun org-export-remove-clock-lines ()
  1454. "Remove clock lines for export."
  1455. (goto-char (point-min))
  1456. (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
  1457. (while (re-search-forward re nil t)
  1458. (org-if-unprotected
  1459. (replace-match "")))))
  1460. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  1461. (defun org-export-protect-quoted-subtrees ()
  1462. "Mark quoted subtrees with the protection property."
  1463. (let ((org-re-quote (format org-heading-keyword-regexp-format
  1464. org-quote-string)))
  1465. (goto-char (point-min))
  1466. (while (re-search-forward org-re-quote nil t)
  1467. (goto-char (match-beginning 0))
  1468. (end-of-line 1)
  1469. (add-text-properties (point) (org-end-of-subtree t)
  1470. '(org-protected t)))))
  1471. (defun org-export-convert-protected-spaces ()
  1472. "Convert strings like \\____ to protected spaces in all backends."
  1473. (goto-char (point-min))
  1474. (while (re-search-forward "\\\\__+" nil t)
  1475. (org-if-unprotected-1
  1476. (replace-match
  1477. (org-add-props
  1478. (cond
  1479. ((eq org-export-current-backend 'latex)
  1480. (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
  1481. ((eq org-export-current-backend 'html)
  1482. (org-add-props (match-string 0) nil
  1483. 'org-whitespace (- (match-end 0) (match-beginning 0))))
  1484. ;; ((eq org-export-current-backend 'docbook))
  1485. ((eq org-export-current-backend 'ascii)
  1486. (org-add-props (match-string 0) '(org-whitespace t)))
  1487. (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  1488. '(org-protected t))
  1489. t t))))
  1490. (defun org-export-protect-verbatim ()
  1491. "Mark verbatim snippets with the protection property."
  1492. (goto-char (point-min))
  1493. (while (re-search-forward org-verbatim-re nil t)
  1494. (org-if-unprotected
  1495. (add-text-properties (match-beginning 4) (match-end 4)
  1496. '(org-protected t org-verbatim-emph t))
  1497. (goto-char (1+ (match-end 4))))))
  1498. (defun org-export-protect-colon-examples ()
  1499. "Protect lines starting with a colon."
  1500. (goto-char (point-min))
  1501. (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
  1502. (while (re-search-forward re nil t)
  1503. (beginning-of-line 1)
  1504. (setq beg (point))
  1505. (while (looking-at re)
  1506. (end-of-line 1)
  1507. (or (eobp) (forward-char 1)))
  1508. (add-text-properties beg (if (bolp) (1- (point)) (point))
  1509. '(org-protected t)))))
  1510. (defvar org-export-backends
  1511. '(docbook html beamer ascii latex)
  1512. "List of Org supported export backends.")
  1513. (defun org-export-select-backend-specific-text ()
  1514. (let ((formatters org-export-backends)
  1515. (case-fold-search t)
  1516. backend backend-name beg beg-content end end-content ind)
  1517. (while formatters
  1518. (setq backend (pop formatters)
  1519. backend-name (symbol-name backend))
  1520. ;; Handle #+BACKEND: stuff
  1521. (goto-char (point-min))
  1522. (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
  1523. ":[ \t]*\\(.*\\)") nil t)
  1524. (if (not (eq backend org-export-current-backend))
  1525. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  1526. (replace-match "\\1\\2" t)
  1527. (add-text-properties
  1528. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  1529. `(org-protected t original-indentation ,ind org-native-text t))))
  1530. ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
  1531. ;; matching the current backend will be taken care of by
  1532. ;; `org-export-attach-captions-and-attributes'
  1533. (goto-char (point-min))
  1534. (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
  1535. ":[ \t]*\\(.*\\)") nil t)
  1536. (setq ind (org-get-indentation))
  1537. (when (not (eq backend org-export-current-backend))
  1538. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1539. ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
  1540. (goto-char (point-min))
  1541. (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
  1542. nil t)
  1543. (setq beg (match-beginning 0) beg-content (match-end 0))
  1544. (setq ind (save-excursion (goto-char beg) (org-get-indentation)))
  1545. (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
  1546. nil t)
  1547. (setq end (match-end 0) end-content (match-beginning 0))
  1548. (if (eq backend org-export-current-backend)
  1549. ;; yes, keep this
  1550. (progn
  1551. (add-text-properties
  1552. beg-content end-content
  1553. `(org-protected t original-indentation ,ind org-native-text t))
  1554. ;; strip protective commas
  1555. (save-excursion
  1556. (save-match-data
  1557. (goto-char beg-content)
  1558. (let ((front-line (save-excursion
  1559. (re-search-forward
  1560. "[^[:space:]]" end-content t)
  1561. (goto-char (match-beginning 0))
  1562. (current-column))))
  1563. (while (re-search-forward "^[ \t]*\\(,\\)" end-content t)
  1564. (when (= (current-column) front-line)
  1565. (replace-match "" nil nil nil 1))))))
  1566. (delete-region (match-beginning 0) (match-end 0))
  1567. (save-excursion
  1568. (goto-char beg)
  1569. (delete-region (point) (1+ (point-at-eol)))))
  1570. ;; No, this is for a different backend, kill it
  1571. (delete-region beg end)))))))
  1572. (defun org-export-mark-blockquote-verse-center ()
  1573. "Mark block quote and verse environments with special cookies.
  1574. These special cookies will later be interpreted by the backend."
  1575. ;; Blockquotes
  1576. (let (type t1 ind beg end beg1 end1 content)
  1577. (goto-char (point-min))
  1578. (while (re-search-forward
  1579. "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
  1580. nil t)
  1581. (setq ind (length (match-string 1))
  1582. type (downcase (match-string 3))
  1583. t1 (if (equal type "quote") "blockquote" type))
  1584. (setq beg (match-beginning 0)
  1585. beg1 (1+ (match-end 0)))
  1586. (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
  1587. (setq end1 (1- (match-beginning 0))
  1588. end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
  1589. (setq content (org-remove-indentation (buffer-substring beg1 end1)))
  1590. (setq content (concat "ORG-" (upcase t1) "-START\n"
  1591. content "\n"
  1592. "ORG-" (upcase t1) "-END\n"))
  1593. (delete-region beg end)
  1594. (insert (org-add-props content nil 'original-indentation ind))))))
  1595. (defun org-export-mark-list-end ()
  1596. "Mark all list endings with a special string."
  1597. (unless (eq org-export-current-backend 'ascii)
  1598. (mapc
  1599. (lambda (e)
  1600. ;; For each type allowing list export, find every list, remove
  1601. ;; ending regexp if needed, and insert org-list-end.
  1602. (goto-char (point-min))
  1603. (while (re-search-forward (org-item-beginning-re) nil t)
  1604. (when (eq (nth 2 (org-list-context)) e)
  1605. (let* ((struct (org-list-struct))
  1606. (bottom (org-list-get-bottom-point struct))
  1607. (top (point-at-bol))
  1608. (top-ind (org-list-get-ind top struct)))
  1609. (goto-char bottom)
  1610. (when (and (not (eq org-list-ending-method 'indent))
  1611. (not (looking-at "[ \t]*$"))
  1612. (looking-at org-list-end-re))
  1613. (replace-match ""))
  1614. (unless (bolp) (insert "\n"))
  1615. ;; As org-list-end is inserted at column 0, it would end
  1616. ;; by indentation any list. It can be problematic when
  1617. ;; there are lists within lists: the inner list end would
  1618. ;; also become the outer list end. To avoid this, text
  1619. ;; property `original-indentation' is added, as
  1620. ;; `org-list-struct' pays attention to it when reading a
  1621. ;; list.
  1622. (insert (org-add-props
  1623. "ORG-LIST-END-MARKER\n"
  1624. (list 'original-indentation top-ind)))))))
  1625. (cons nil org-list-export-context))))
  1626. (defun org-export-mark-list-properties ()
  1627. "Mark list with special properties.
  1628. These special properties will later be interpreted by the backend."
  1629. (let ((mark-list
  1630. (function
  1631. ;; Mark a list with 3 properties: `list-item' which is
  1632. ;; position at beginning of line, `list-struct' which is
  1633. ;; list structure, and `list-prevs' which is the alist of
  1634. ;; item and its predecessor. Leave point at list ending.
  1635. (lambda (ctxt)
  1636. (let* ((struct (org-list-struct))
  1637. (top (org-list-get-top-point struct))
  1638. (bottom (org-list-get-bottom-point struct))
  1639. (prevs (org-list-prevs-alist struct))
  1640. poi)
  1641. ;; Get every item and ending position, without dups and
  1642. ;; without bottom point of list.
  1643. (mapc (lambda (e)
  1644. (let ((pos (car e))
  1645. (end (nth 6 e)))
  1646. (unless (memq pos poi)
  1647. (push pos poi))
  1648. (unless (or (= end bottom) (memq end poi))
  1649. (push end poi))))
  1650. struct)
  1651. (setq poi (sort poi '<))
  1652. ;; For every point of interest, mark the whole line with
  1653. ;; its position in list.
  1654. (mapc
  1655. (lambda (e)
  1656. (goto-char e)
  1657. (add-text-properties (point-at-bol) (point-at-eol)
  1658. (list 'list-item (point-at-bol)
  1659. 'list-struct struct
  1660. 'list-prevs prevs)))
  1661. poi)
  1662. ;; Take care of bottom point. As babel may have inserted
  1663. ;; a new list in buffer, list ending isn't always
  1664. ;; marked. Now mark every list ending and add properties
  1665. ;; useful to line processing exporters.
  1666. (goto-char bottom)
  1667. (when (or (looking-at "^ORG-LIST-END-MARKER\n")
  1668. (and (not (eq org-list-ending-method 'indent))
  1669. (not (looking-at "[ \t]*$"))
  1670. (looking-at org-list-end-re)))
  1671. (replace-match ""))
  1672. (unless (bolp) (insert "\n"))
  1673. (insert
  1674. (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
  1675. 'list-struct struct
  1676. 'list-prevs prevs)))
  1677. ;; Following property is used by LaTeX exporter.
  1678. (add-text-properties top (point) (list 'list-context ctxt)))))))
  1679. ;; Mark lists except for backends not interpreting them.
  1680. (unless (eq org-export-current-backend 'ascii)
  1681. (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
  1682. (mapc
  1683. (lambda (e)
  1684. (goto-char (point-min))
  1685. (while (re-search-forward (org-item-beginning-re) nil t)
  1686. (let ((context (nth 2 (org-list-context))))
  1687. (if (eq context e)
  1688. (funcall mark-list e)
  1689. (put-text-property (point-at-bol) (point-at-eol)
  1690. 'list-context context)))))
  1691. (cons nil org-list-export-context))))))
  1692. (defun org-export-attach-captions-and-attributes (target-alist)
  1693. "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
  1694. If the next thing following is a table, add the text properties to the first
  1695. table line. If it is a link, add it to the line containing the link."
  1696. (goto-char (point-min))
  1697. (remove-text-properties (point-min) (point-max)
  1698. '(org-caption nil org-attributes nil))
  1699. (let ((case-fold-search t)
  1700. (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
  1701. "\\|"
  1702. "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
  1703. "\\|"
  1704. "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
  1705. "\\|"
  1706. "^[ \t]*\\(|[^-]\\)"
  1707. "\\|"
  1708. "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
  1709. cap shortn attr label end)
  1710. (while (re-search-forward re nil t)
  1711. (cond
  1712. ;; there is a caption
  1713. ((match-end 1)
  1714. (progn
  1715. (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
  1716. (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
  1717. (setq shortn (match-string 1 cap)
  1718. cap (match-string 2 cap)))
  1719. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1720. ;; there is an attribute
  1721. ((match-end 2)
  1722. (progn
  1723. (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
  1724. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1725. ;; there is a label
  1726. ((match-end 3)
  1727. (progn
  1728. (setq label (org-trim (match-string 3)))
  1729. (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
  1730. (t
  1731. (setq end (if (match-end 4)
  1732. (let ((ee (org-table-end)))
  1733. (prog1 (1- (marker-position ee)) (move-marker ee nil)))
  1734. (point-at-eol)))
  1735. (add-text-properties (point-at-bol) end
  1736. (list 'org-caption cap
  1737. 'org-caption-shortn shortn
  1738. 'org-attributes attr
  1739. 'org-label label))
  1740. (if label (push (cons label label) target-alist))
  1741. (goto-char end)
  1742. (setq cap nil shortn nil attr nil label nil)))))
  1743. target-alist)
  1744. (defun org-export-remove-comment-blocks-and-subtrees ()
  1745. "Remove the comment environment, and also commented subtrees."
  1746. (let ((re-commented (format org-heading-keyword-regexp-format
  1747. org-comment-string))
  1748. case-fold-search)
  1749. ;; Remove comment environment
  1750. (goto-char (point-min))
  1751. (setq case-fold-search t)
  1752. (while (re-search-forward
  1753. "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
  1754. (replace-match "" t t))
  1755. ;; Remove subtrees that are commented
  1756. (goto-char (point-min))
  1757. (setq case-fold-search nil)
  1758. (while (re-search-forward re-commented nil t)
  1759. (goto-char (match-beginning 0))
  1760. (delete-region (point) (org-end-of-subtree t)))))
  1761. (defun org-export-handle-comments (org-commentsp)
  1762. "Remove comments, or convert to backend-specific format.
  1763. ORG-COMMENTSP can be a format string for publishing comments.
  1764. When it is nil, all comments will be removed."
  1765. (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
  1766. pos)
  1767. (goto-char (point-min))
  1768. (while (or (looking-at re)
  1769. (re-search-forward re nil t))
  1770. (setq pos (match-beginning 0))
  1771. (if (get-text-property pos 'org-protected)
  1772. (goto-char (1+ pos))
  1773. (if (and org-commentsp
  1774. (not (equal (char-before (match-end 1)) ?+)))
  1775. (progn (add-text-properties
  1776. (match-beginning 0) (match-end 0) '(org-protected t))
  1777. (replace-match (org-add-props
  1778. (format org-commentsp (match-string 2))
  1779. nil 'org-protected t)
  1780. t t))
  1781. (goto-char (1+ pos))
  1782. (replace-match "")
  1783. (goto-char (max (point-min) (1- pos))))))))
  1784. (defun org-export-handle-table-metalines ()
  1785. "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
  1786. (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
  1787. pos)
  1788. (goto-char (point-min))
  1789. (while (or (looking-at re)
  1790. (re-search-forward re nil t))
  1791. (setq pos (match-beginning 0))
  1792. (if (get-text-property (match-beginning 1) 'org-protected)
  1793. (goto-char (1+ pos))
  1794. (goto-char (1+ pos))
  1795. (replace-match "")
  1796. (goto-char (max (point-min) (1- pos)))))))
  1797. (defun org-export-res/src-name-cleanup ()
  1798. "Clean up #+results and #+name lines for export.
  1799. This function should only be called after all block processing
  1800. has taken place."
  1801. (interactive)
  1802. (save-excursion
  1803. (goto-char (point-min))
  1804. (let ((case-fold-search t))
  1805. (while (org-re-search-forward-unprotected
  1806. "#\\+\\(name\\|results\\(\\[[a-z0-9]+\\]\\)?\\):" nil t)
  1807. (delete-region (match-beginning 0) (progn (forward-line) (point)))))))
  1808. (defun org-export-mark-radio-links ()
  1809. "Find all matches for radio targets and turn them into internal links."
  1810. (let ((re-radio (and org-target-link-regexp
  1811. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
  1812. (goto-char (point-min))
  1813. (when re-radio
  1814. (while (re-search-forward re-radio nil t)
  1815. (unless
  1816. (save-match-data
  1817. (or (org-in-regexp org-bracket-link-regexp)
  1818. (org-in-regexp org-plain-link-re)
  1819. (org-in-regexp "<<[^<>]+>>")))
  1820. (org-if-unprotected
  1821. (replace-match "\\1[[\\2]]")))))))
  1822. (defun org-store-forced-table-alignment ()
  1823. "Find table lines which force alignment, store the results in properties."
  1824. (let (line cnt cookies)
  1825. (goto-char (point-min))
  1826. (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
  1827. nil t)
  1828. ;; OK, this looks like a table line with an alignment cookie
  1829. (org-if-unprotected
  1830. (setq line (buffer-substring (point-at-bol) (point-at-eol)))
  1831. (when (and (org-at-table-p)
  1832. (org-table-cookie-line-p line))
  1833. (setq cnt 0 cookies nil)
  1834. (mapc
  1835. (lambda (x)
  1836. (setq cnt (1+ cnt))
  1837. (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
  1838. (let ((align (and (match-end 1)
  1839. (downcase (match-string 1 x))))
  1840. (width (and (match-end 2)
  1841. (string-to-number (match-string 2 x)))))
  1842. (push (cons cnt (list align width)) cookies))))
  1843. (org-split-string line "[ \t]*|[ \t]*"))
  1844. (add-text-properties (org-table-begin) (org-table-end)
  1845. (list 'org-col-cookies cookies))))
  1846. (goto-char (point-at-eol)))))
  1847. (defun org-export-remove-special-table-lines ()
  1848. "Remove tables lines that are used for internal purposes.
  1849. Also, store forced alignment information found in such lines."
  1850. (goto-char (point-min))
  1851. (while (re-search-forward "^[ \t]*|" nil t)
  1852. (org-if-unprotected-at (1- (point))
  1853. (beginning-of-line 1)
  1854. (if (or (looking-at "[ \t]*| *[!_^] *|")
  1855. (not
  1856. (memq
  1857. nil
  1858. (mapcar
  1859. (lambda (f)
  1860. (or (and org-export-table-remove-empty-lines (= (length f) 0))
  1861. (string-match
  1862. "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
  1863. (org-split-string ;; FIXME, can't we do without splitting???
  1864. (buffer-substring (point-at-bol) (point-at-eol))
  1865. "[ \t]*|[ \t]*")))))
  1866. (delete-region (max (point-min) (1- (point-at-bol)))
  1867. (point-at-eol))
  1868. (end-of-line 1)))))
  1869. (defun org-export-protect-sub-super (s)
  1870. (save-match-data
  1871. (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
  1872. (setq s (replace-match "\\1\\\\\\2" nil nil s)))
  1873. s))
  1874. (defun org-export-normalize-links ()
  1875. "Convert all links to bracket links, and expand link abbreviations."
  1876. (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  1877. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  1878. nodesc)
  1879. (goto-char (point-min))
  1880. (while (re-search-forward org-bracket-link-regexp nil t)
  1881. (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t))
  1882. (goto-char (point-min))
  1883. (while (re-search-forward re-plain-link nil t)
  1884. (unless (get-text-property (match-beginning 0) 'org-normalized-link)
  1885. (goto-char (1- (match-end 0)))
  1886. (org-if-unprotected-at (1+ (match-beginning 0))
  1887. (let* ((s (concat (match-string 1)
  1888. "[[" (match-string 2) ":" (match-string 3)
  1889. "][" (match-string 2) ":" (org-export-protect-sub-super
  1890. (match-string 3))
  1891. "]]")))
  1892. ;; added 'org-link face to links
  1893. (put-text-property 0 (length s) 'face 'org-link s)
  1894. (replace-match s t t)))))
  1895. (goto-char (point-min))
  1896. (while (re-search-forward re-angle-link nil t)
  1897. (goto-char (1- (match-end 0)))
  1898. (org-if-unprotected
  1899. (let* ((s (concat (match-string 1)
  1900. "[[" (match-string 2) ":" (match-string 3)
  1901. "][" (match-string 2) ":" (org-export-protect-sub-super
  1902. (match-string 3))
  1903. "]]")))
  1904. (put-text-property 0 (length s) 'face 'org-link s)
  1905. (replace-match s t t))))
  1906. (goto-char (point-min))
  1907. (while (re-search-forward org-bracket-link-regexp nil t)
  1908. (goto-char (1- (match-end 0)))
  1909. (setq nodesc (not (match-end 3)))
  1910. (org-if-unprotected
  1911. (let* ((xx (save-match-data
  1912. (org-translate-link
  1913. (org-link-expand-abbrev (match-string 1)))))
  1914. (s (concat
  1915. "[[" (org-add-props (copy-sequence xx)
  1916. nil 'org-protected t 'org-no-description nodesc)
  1917. "]"
  1918. (if (match-end 3)
  1919. (match-string 2)
  1920. (concat "[" (copy-sequence xx)
  1921. "]"))
  1922. "]")))
  1923. (put-text-property 0 (length s) 'face 'org-link s)
  1924. (replace-match s t t))))))
  1925. (defun org-export-concatenate-multiline-links ()
  1926. "Find multi-line links and put it all into a single line.
  1927. This is to make sure that the line-processing export backends
  1928. can work correctly."
  1929. (goto-char (point-min))
  1930. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  1931. (org-if-unprotected-at (match-beginning 1)
  1932. (replace-match "\\1 \\3")
  1933. (goto-char (match-beginning 0)))))
  1934. (defun org-export-concatenate-multiline-emphasis ()
  1935. "Find multi-line emphasis and put it all into a single line.
  1936. This is to make sure that the line-processing export backends
  1937. can work correctly."
  1938. (goto-char (point-min))
  1939. (while (re-search-forward org-emph-re nil t)
  1940. (if (and (not (= (char-after (match-beginning 3))
  1941. (char-after (match-beginning 4))))
  1942. (save-excursion (goto-char (match-beginning 0))
  1943. (save-match-data
  1944. (and (not (org-at-table-p))
  1945. (not (org-at-heading-p))))))
  1946. (org-if-unprotected
  1947. (subst-char-in-region (match-beginning 0) (match-end 0)
  1948. ?\n ?\ t)
  1949. (goto-char (1- (match-end 0))))
  1950. (goto-char (1+ (match-beginning 0))))))
  1951. (defun org-export-grab-title-from-buffer ()
  1952. "Get a title for the current document, from looking at the buffer."
  1953. (let ((inhibit-read-only t))
  1954. (save-excursion
  1955. (goto-char (point-min))
  1956. (let ((end (if (looking-at org-outline-regexp)
  1957. (point)
  1958. (save-excursion (outline-next-heading) (point)))))
  1959. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  1960. ;; Mark the line so that it will not be exported as normal text.
  1961. (unless (org-in-block-p org-list-forbidden-blocks)
  1962. (org-unmodified
  1963. (add-text-properties (match-beginning 0) (match-end 0)
  1964. (list :org-license-to-kill t))))
  1965. ;; Return the title string
  1966. (org-trim (match-string 0)))))))
  1967. (defun org-export-get-title-from-subtree ()
  1968. "Return subtree title and exclude it from export."
  1969. (let ((rbeg (region-beginning)) (rend (region-end))
  1970. (inhibit-read-only t)
  1971. (tags (plist-get (org-infile-export-plist) :tags))
  1972. title)
  1973. (save-excursion
  1974. (goto-char rbeg)
  1975. (when (and (org-at-heading-p)
  1976. (>= (org-end-of-subtree t t) rend))
  1977. (when (plist-member org-export-opt-plist :tags)
  1978. (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
  1979. ;; This is a subtree, we take the title from the first heading
  1980. (goto-char rbeg)
  1981. (looking-at org-todo-line-tags-regexp)
  1982. (setq title (if (eq tags t)
  1983. (format "%s\t%s" (match-string 3) (match-string 4))
  1984. (match-string 3)))
  1985. (org-unmodified
  1986. (add-text-properties (point) (1+ (point-at-eol))
  1987. (list :org-license-to-kill t)))
  1988. (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
  1989. title))
  1990. (defun org-solidify-link-text (s &optional alist)
  1991. "Take link text and make a safe target out of it."
  1992. (save-match-data
  1993. (let* ((rtn
  1994. (mapconcat
  1995. 'identity
  1996. (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
  1997. (a (assoc rtn alist)))
  1998. (or (cdr a) rtn))))
  1999. (defun org-get-min-level (lines &optional offset)
  2000. "Get the minimum level in LINES."
  2001. (let ((re "^\\(\\*+\\) ") l)
  2002. (catch 'exit
  2003. (while (setq l (pop lines))
  2004. (if (string-match re l)
  2005. (throw 'exit (org-tr-level (- (length (match-string 1 l))
  2006. (or offset 0))))))
  2007. 1)))
  2008. ;; Variable holding the vector with section numbers
  2009. (defvar org-section-numbers (make-vector org-level-max 0))
  2010. (defun org-init-section-numbers ()
  2011. "Initialize the vector for the section numbers."
  2012. (let* ((level -1)
  2013. (numbers (nreverse (org-split-string "" "\\.")))
  2014. (depth (1- (length org-section-numbers)))
  2015. (i depth) number-string)
  2016. (while (>= i 0)
  2017. (if (> i level)
  2018. (aset org-section-numbers i 0)
  2019. (setq number-string (or (car numbers) "0"))
  2020. (if (string-match "\\`[A-Z]\\'" number-string)
  2021. (aset org-section-numbers i
  2022. (- (string-to-char number-string) ?A -1))
  2023. (aset org-section-numbers i (string-to-number number-string)))
  2024. (pop numbers))
  2025. (setq i (1- i)))))
  2026. (defun org-section-number (&optional level)
  2027. "Return a string with the current section number.
  2028. When LEVEL is non-nil, increase section numbers on that level."
  2029. (let* ((depth (1- (length org-section-numbers)))
  2030. (string "")
  2031. (fmts (car org-export-section-number-format))
  2032. (term (cdr org-export-section-number-format))
  2033. (sep "")
  2034. ctype fmt idx n)
  2035. (when level
  2036. (when (> level -1)
  2037. (aset org-section-numbers
  2038. level (1+ (aref org-section-numbers level))))
  2039. (setq idx (1+ level))
  2040. (while (<= idx depth)
  2041. (if (not (= idx 1))
  2042. (aset org-section-numbers idx 0))
  2043. (setq idx (1+ idx))))
  2044. (setq idx 0)
  2045. (while (<= idx depth)
  2046. (when (> (aref org-section-numbers idx) 0)
  2047. (setq fmt (or (pop fmts) fmt)
  2048. ctype (car fmt)
  2049. n (aref org-section-numbers idx)
  2050. string (if (> n 0)
  2051. (concat string sep (org-number-to-counter n ctype))
  2052. (concat string ".0"))
  2053. sep (nth 1 fmt)))
  2054. (setq idx (1+ idx)))
  2055. (save-match-data
  2056. (if (string-match "\\`\\([@0]\\.\\)+" string)
  2057. (setq string (replace-match "" t nil string)))
  2058. (if (string-match "\\(\\.0\\)+\\'" string)
  2059. (setq string (replace-match "" t nil string))))
  2060. (concat string term)))
  2061. (defun org-number-to-counter (n type)
  2062. "Concert number N to a string counter, according to TYPE.
  2063. TYPE must be a string, any of:
  2064. 1 number
  2065. A A,B,....
  2066. a a,b,....
  2067. I upper case roman numeral
  2068. i lower case roman numeral"
  2069. (cond
  2070. ((equal type "1") (number-to-string n))
  2071. ((equal type "A") (char-to-string (+ ?A n -1)))
  2072. ((equal type "a") (char-to-string (+ ?a n -1)))
  2073. ((equal type "I") (org-number-to-roman n))
  2074. ((equal type "i") (downcase (org-number-to-roman n)))
  2075. (t (error "Invalid counter type `%s'" type))))
  2076. (defun org-number-to-roman (n)
  2077. "Convert integer N into a roman numeral."
  2078. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2079. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2080. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2081. ( 1 . "I")))
  2082. (res ""))
  2083. (if (<= n 0)
  2084. (number-to-string n)
  2085. (while roman
  2086. (if (>= n (caar roman))
  2087. (setq n (- n (caar roman))
  2088. res (concat res (cdar roman)))
  2089. (pop roman)))
  2090. res)))
  2091. ;;; Macros
  2092. (defun org-export-preprocess-apply-macros ()
  2093. "Replace macro references."
  2094. (goto-char (point-min))
  2095. (let (sy val key args args2 ind-str s n)
  2096. (while (re-search-forward
  2097. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2098. nil t)
  2099. (unless (save-match-data (save-excursion
  2100. (goto-char (point-at-bol))
  2101. (looking-at "[ \t]*#\\+macro")))
  2102. ;; Get macro name (KEY), arguments (ARGS), and indentation of
  2103. ;; current line (IND-STR) as strings.
  2104. (setq key (downcase (match-string 1))
  2105. args (match-string 3)
  2106. ind-str (save-match-data (save-excursion
  2107. (beginning-of-line)
  2108. (looking-at "^\\([ \t]*\\).*")
  2109. (match-string 1))))
  2110. ;; When macro is defined, retrieve replacement text in VAL,
  2111. ;; and proceed with expansion.
  2112. (when (setq val (or (plist-get org-export-opt-plist
  2113. (intern (concat ":macro-" key)))
  2114. (plist-get org-export-opt-plist
  2115. (intern (concat ":" key)))))
  2116. (save-match-data
  2117. ;; If arguments are provided, first retrieve them properly
  2118. ;; (in ARGS, as a list), then replace them in VAL.
  2119. (when args
  2120. (setq args (org-split-string args ",") args2 nil)
  2121. (while args
  2122. (while (string-match "\\\\\\'" (car args))
  2123. ;; Repair bad splits.
  2124. (setcar (cdr args) (concat (substring (car args) 0 -1)
  2125. "," (nth 1 args)))
  2126. (pop args))
  2127. (push (pop args) args2))
  2128. (setq args (mapcar 'org-trim (nreverse args2)))
  2129. (setq s 0)
  2130. (while (string-match "\\$\\([0-9]+\\)" val s)
  2131. (setq s (1+ (match-beginning 0))
  2132. n (string-to-number (match-string 1 val)))
  2133. (and (>= (length args) n)
  2134. (setq val (replace-match (nth (1- n) args) t t val)))))
  2135. ;; VAL starts with "(eval": it is a sexp, `eval' it.
  2136. (when (string-match "\\`(eval\\>" val)
  2137. (setq val (eval (read val))))
  2138. ;; Ensure VAL is a string (or nil) and that each new line
  2139. ;; is indented as the first one.
  2140. (setq val (and val
  2141. (mapconcat 'identity
  2142. (org-split-string
  2143. (if (stringp val) val (format "%s" val))
  2144. "\n")
  2145. (concat "\n" ind-str)))))
  2146. ;; Eventually do the replacement, if VAL isn't nil. Move
  2147. ;; point at beginning of macro for recursive expansions.
  2148. (when val
  2149. (replace-match val t t)
  2150. (goto-char (match-beginning 0))))))))
  2151. (defun org-export-apply-macros-in-string (s)
  2152. "Apply the macros in string S."
  2153. (when s
  2154. (with-temp-buffer
  2155. (insert s)
  2156. (org-export-preprocess-apply-macros)
  2157. (buffer-string))))
  2158. ;;; Include files
  2159. (defun org-export-handle-include-files ()
  2160. "Include the contents of include files, with proper formatting."
  2161. (let ((case-fold-search t)
  2162. params file markup lang start end prefix prefix1 switches all minlevel lines)
  2163. (goto-char (point-min))
  2164. (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
  2165. (setq params (read (concat "(" (match-string 1) ")"))
  2166. prefix (org-get-and-remove-property 'params :prefix)
  2167. prefix1 (org-get-and-remove-property 'params :prefix1)
  2168. minlevel (org-get-and-remove-property 'params :minlevel)
  2169. lines (org-get-and-remove-property 'params :lines)
  2170. file (org-symname-or-string (pop params))
  2171. markup (org-symname-or-string (pop params))
  2172. lang (and (member markup '("src" "SRC"))
  2173. (org-symname-or-string (pop params)))
  2174. switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
  2175. start nil end nil)
  2176. (delete-region (match-beginning 0) (match-end 0))
  2177. (if (or (not file)
  2178. (not (file-exists-p file))
  2179. (not (file-readable-p file)))
  2180. (insert (format "CANNOT INCLUDE FILE %s" file))
  2181. (setq all (cons file all))
  2182. (when markup
  2183. (if (equal (downcase markup) "src")
  2184. (setq start (format "#+begin_src %s %s\n"
  2185. (or lang "fundamental")
  2186. (or switches ""))
  2187. end "#+end_src")
  2188. (setq start (format "#+begin_%s %s\n" markup switches)
  2189. end (format "#+end_%s" markup))))
  2190. (insert (or start ""))
  2191. (insert (org-get-file-contents (expand-file-name file)
  2192. prefix prefix1 markup minlevel lines))
  2193. (or (bolp) (newline))
  2194. (insert (or end ""))))
  2195. all))
  2196. (defun org-export-handle-include-files-recurse ()
  2197. "Recursively include files aborting on circular inclusion."
  2198. (let ((now (list org-current-export-file)) all)
  2199. (while now
  2200. (setq all (append now all))
  2201. (setq now (org-export-handle-include-files))
  2202. (let ((intersection
  2203. (delq nil
  2204. (mapcar (lambda (el) (when (member el all) el)) now))))
  2205. (when intersection
  2206. (error "Recursive #+INCLUDE: %S" intersection))))))
  2207. (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
  2208. "Get the contents of FILE and return them as a string.
  2209. If PREFIX is a string, prepend it to each line. If PREFIX1
  2210. is a string, prepend it to the first line instead of PREFIX.
  2211. If MARKUP, don't protect org-like lines, the exporter will
  2212. take care of the block they are in. If LINES is a string
  2213. specifying a range of lines, include only those lines ."
  2214. (if (stringp markup) (setq markup (downcase markup)))
  2215. (with-temp-buffer
  2216. (insert-file-contents file)
  2217. (when lines
  2218. (let* ((lines (split-string lines "-"))
  2219. (lbeg (string-to-number (car lines)))
  2220. (lend (string-to-number (cadr lines)))
  2221. (beg (if (zerop lbeg) (point-min)
  2222. (goto-char (point-min))
  2223. (forward-line (1- lbeg))
  2224. (point)))
  2225. (end (if (zerop lend) (point-max)
  2226. (goto-char (point-min))
  2227. (forward-line (1- lend))
  2228. (point))))
  2229. (narrow-to-region beg end)))
  2230. (when (or prefix prefix1)
  2231. (goto-char (point-min))
  2232. (while (not (eobp))
  2233. (insert (or prefix1 prefix))
  2234. (setq prefix1 "")
  2235. (beginning-of-line 2)))
  2236. (buffer-string)
  2237. (when (member markup '("src" "example"))
  2238. (goto-char (point-min))
  2239. (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
  2240. (goto-char (match-beginning 0))
  2241. (insert ",")
  2242. (end-of-line 1)))
  2243. (when minlevel
  2244. (dotimes (lvl minlevel)
  2245. (org-map-region 'org-demote (point-min) (point-max))))
  2246. (buffer-string)))
  2247. (defun org-get-and-remove-property (listvar prop)
  2248. "Check if the value of LISTVAR contains PROP as a property.
  2249. If yes, return the value of that property (i.e. the element following
  2250. in the list) and remove property and value from the list in LISTVAR."
  2251. (let ((list (symbol-value listvar)) m v)
  2252. (when (setq m (member prop list))
  2253. (setq v (nth 1 m))
  2254. (if (equal (car list) prop)
  2255. (set listvar (cddr list))
  2256. (setcdr (nthcdr (- (length list) (length m) 1) list)
  2257. (cddr m))
  2258. (set listvar list)))
  2259. v))
  2260. (defun org-symname-or-string (s)
  2261. (if (symbolp s)
  2262. (if s (symbol-name s) s)
  2263. s))
  2264. ;;; Fontification and line numbers for code examples
  2265. (defvar org-export-last-code-line-counter-value 0)
  2266. (defun org-export-replace-src-segments-and-examples ()
  2267. "Replace source code segments with special code for export."
  2268. (setq org-export-last-code-line-counter-value 0)
  2269. (let ((case-fold-search t)
  2270. lang code trans opts indent caption)
  2271. (goto-char (point-min))
  2272. (while (re-search-forward
  2273. "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
  2274. nil t)
  2275. (if (match-end 1)
  2276. (if (not (match-string 4))
  2277. (error "Source block missing language specification: %s"
  2278. (let* ((body (match-string 6))
  2279. (nothing (message "body:%s" body))
  2280. (preview (or (and (string-match
  2281. "^[ \t]*\\([^\n\r]*\\)" body)
  2282. (match-string 1 body)) body)))
  2283. (if (> (length preview) 35)
  2284. (concat (substring preview 0 32) "...")
  2285. preview)))
  2286. ;; src segments
  2287. (setq lang (match-string 4)
  2288. opts (match-string 5)
  2289. code (match-string 6)
  2290. indent (length (match-string 2))
  2291. caption (get-text-property 0 'org-caption (match-string 0))))
  2292. (setq lang nil
  2293. opts (match-string 9)
  2294. code (match-string 10)
  2295. indent (length (match-string 8))
  2296. caption (get-text-property 0 'org-caption (match-string 0))))
  2297. (setq trans (org-export-format-source-code-or-example
  2298. lang code opts indent caption))
  2299. (replace-match trans t t))))
  2300. (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
  2301. (defvar org-export-latex-listings) ;; defined in org-latex.el
  2302. (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
  2303. (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
  2304. (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
  2305. (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
  2306. (defvar org-export-latex-listings-options) ;; defined in org-latex.el
  2307. (defvar org-export-latex-minted-options) ;; defined in org-latex.el
  2308. (defun org-remove-formatting-on-newlines-in-region (beg end)
  2309. "Remove formatting on newline characters"
  2310. (interactive "r")
  2311. (save-excursion
  2312. (goto-char beg)
  2313. (while (progn (end-of-line) (< (point) end))
  2314. (put-text-property (point) (1+ (point)) 'face nil)
  2315. (forward-char 1))))
  2316. (defun org-export-format-source-code-or-example
  2317. (lang code &optional opts indent caption)
  2318. "Format CODE from language LANG and return it formatted for export.
  2319. The CODE is marked up in `org-export-current-backend' format.
  2320. Check if a function by name
  2321. \"org-<backend>-format-source-code-or-example\" is bound. If yes,
  2322. use it as the custom formatter. Otherwise, use the default
  2323. formatter. Default formatters are provided for docbook, html,
  2324. latex and ascii backends. For example, use
  2325. `org-html-format-source-code-or-example' to provide a custom
  2326. formatter for export to \"html\".
  2327. If LANG is nil, do not add any fontification.
  2328. OPTS contains formatting options, like `-n' for triggering numbering lines,
  2329. and `+n' for continuing previous numbering.
  2330. Code formatting according to language currently only works for HTML.
  2331. Numbering lines works for all three major backends (html, latex, and ascii).
  2332. INDENT was the original indentation of the block."
  2333. (save-match-data
  2334. (let* ((backend-name (symbol-name org-export-current-backend))
  2335. (backend-formatter
  2336. (intern (format "org-%s-format-source-code-or-example"
  2337. backend-name)))
  2338. (backend-feature (intern (concat "org-" backend-name)))
  2339. (backend-formatter
  2340. (and (require (intern (concat "org-" backend-name)) nil)
  2341. (fboundp backend-formatter) backend-formatter))
  2342. num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
  2343. (setq opts (or opts "")
  2344. num (string-match "[-+]n\\>" opts)
  2345. cont (string-match "\\+n\\>" opts)
  2346. rpllbl (string-match "-r\\>" opts)
  2347. keepp (string-match "-k\\>" opts)
  2348. textareap (string-match "-t\\>" opts)
  2349. preserve-indentp (or org-src-preserve-indentation
  2350. (string-match "-i\\>" opts))
  2351. cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
  2352. (string-to-number (match-string 1 opts))
  2353. 80)
  2354. rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
  2355. (string-to-number (match-string 1 opts))
  2356. (org-count-lines code))
  2357. fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
  2358. (match-string 1 opts)))
  2359. (when (and textareap (eq org-export-current-backend 'html))
  2360. ;; we cannot use numbering or highlighting.
  2361. (setq num nil cont nil lang nil))
  2362. (if keepp (setq rpllbl 'keep))
  2363. (setq rtn (if preserve-indentp code (org-remove-indentation code)))
  2364. (when (string-match "^," rtn)
  2365. (setq rtn (with-temp-buffer
  2366. (insert rtn)
  2367. ;; Free up the protected lines
  2368. (goto-char (point-min))
  2369. (while (re-search-forward "^," nil t)
  2370. (if (or (equal lang "org")
  2371. (save-match-data
  2372. (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
  2373. (replace-match ""))
  2374. (end-of-line 1))
  2375. (buffer-string))))
  2376. ;; Now backend-specific coding
  2377. (setq rtn
  2378. (cond
  2379. (backend-formatter
  2380. (funcall backend-formatter rtn lang caption textareap cols rows num
  2381. cont rpllbl fmt))
  2382. ((eq org-export-current-backend 'docbook)
  2383. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2384. (concat "<programlisting><![CDATA["
  2385. rtn
  2386. "]]></programlisting>\n"))
  2387. ((eq org-export-current-backend 'html)
  2388. ;; We are exporting to HTML
  2389. (when lang
  2390. (if (featurep 'xemacs)
  2391. (require 'htmlize)
  2392. (require 'htmlize nil t))
  2393. (when (not (fboundp 'htmlize-region-for-paste))
  2394. ;; we do not have htmlize.el, or an old version of it
  2395. (setq lang nil)
  2396. (message
  2397. "htmlize.el 1.34 or later is needed for source code formatting")))
  2398. (if lang
  2399. (let* ((lang-m (when lang
  2400. (or (cdr (assoc lang org-src-lang-modes))
  2401. lang)))
  2402. (mode (and lang-m (intern
  2403. (concat
  2404. (if (symbolp lang-m)
  2405. (symbol-name lang-m)
  2406. lang-m)
  2407. "-mode"))))
  2408. (org-inhibit-startup t)
  2409. (org-startup-folded nil))
  2410. (setq rtn
  2411. (with-temp-buffer
  2412. (insert rtn)
  2413. (if (functionp mode)
  2414. (funcall mode)
  2415. (fundamental-mode))
  2416. (font-lock-fontify-buffer)
  2417. ;; markup each line separately
  2418. (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
  2419. (org-src-mode)
  2420. (set-buffer-modified-p nil)
  2421. (org-export-htmlize-region-for-paste
  2422. (point-min) (point-max))))
  2423. (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
  2424. (setq rtn
  2425. (concat
  2426. (if caption
  2427. (concat
  2428. "<div class=\"org-src-container\">"
  2429. (format
  2430. "<label class=\"org-src-name\">%s</label>"
  2431. caption))
  2432. "")
  2433. (replace-match
  2434. (format "<pre class=\"src src-%s\">\n" lang)
  2435. t t rtn)
  2436. (if caption "</div>" "")))))
  2437. (if textareap
  2438. (setq rtn (concat
  2439. (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
  2440. cols rows)
  2441. rtn "</textarea>\n</p>\n"))
  2442. (with-temp-buffer
  2443. (insert rtn)
  2444. (goto-char (point-min))
  2445. (while (re-search-forward "[<>&]" nil t)
  2446. (replace-match (cdr (assq (char-before)
  2447. '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
  2448. t t))
  2449. (setq rtn (buffer-string)))
  2450. (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
  2451. (unless textareap
  2452. (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
  2453. (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
  2454. (setq rtn (replace-match "\\1" t nil rtn)))
  2455. rtn)
  2456. ((eq org-export-current-backend 'latex)
  2457. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2458. (cond
  2459. ((and lang org-export-latex-listings)
  2460. (flet ((make-option-string
  2461. (pair)
  2462. (concat (first pair)
  2463. (if (> (length (second pair)) 0)
  2464. (concat "=" (second pair))))))
  2465. (let* ((lang-sym (intern lang))
  2466. (minted-p (eq org-export-latex-listings 'minted))
  2467. (listings-p (not minted-p))
  2468. (backend-lang
  2469. (or (cadr
  2470. (assq
  2471. lang-sym
  2472. (cond
  2473. (minted-p org-export-latex-minted-langs)
  2474. (listings-p org-export-latex-listings-langs))))
  2475. lang))
  2476. (custom-environment
  2477. (cadr
  2478. (assq
  2479. lang-sym
  2480. org-export-latex-custom-lang-environments))))
  2481. (concat
  2482. (when (and listings-p (not custom-environment))
  2483. (format
  2484. "\\lstset{%s}\n"
  2485. (mapconcat
  2486. #'make-option-string
  2487. (append org-export-latex-listings-options
  2488. `(("language" ,backend-lang))) ",")))
  2489. (when (and caption org-export-latex-listings-w-names)
  2490. (format
  2491. "\n%s $\\equiv$ \n"
  2492. (replace-regexp-in-string "_" "\\\\_" caption)))
  2493. (cond
  2494. (custom-environment
  2495. (format "\\begin{%s}\n%s\\end{%s}\n"
  2496. custom-environment rtn custom-environment))
  2497. (listings-p
  2498. (format "\\begin{%s}\n%s\\end{%s}"
  2499. "lstlisting" rtn "lstlisting"))
  2500. (minted-p
  2501. (format
  2502. "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
  2503. (mapconcat #'make-option-string
  2504. org-export-latex-minted-options ",")
  2505. backend-lang rtn)))))))
  2506. (t (concat (car org-export-latex-verbatim-wrap)
  2507. rtn (cdr org-export-latex-verbatim-wrap)))))
  2508. ((eq org-export-current-backend 'ascii)
  2509. ;; This is not HTML or LaTeX, so just make it an example.
  2510. (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
  2511. (concat caption "\n"
  2512. (concat
  2513. (mapconcat
  2514. (lambda (l) (concat " " l))
  2515. (org-split-string rtn "\n")
  2516. "\n")
  2517. "\n")
  2518. ))
  2519. (t
  2520. (error "Don't know how to markup source or example block in %s"
  2521. (upcase backend-name)))))
  2522. (setq rtn
  2523. (concat
  2524. "\n#+BEGIN_" backend-name "\n"
  2525. (org-add-props rtn
  2526. '(org-protected t org-example t org-native-text t))
  2527. "\n#+END_" backend-name "\n"))
  2528. (org-add-props rtn nil 'original-indentation indent))))
  2529. (defun org-export-number-lines (text &optional skip1 skip2 number cont
  2530. replace-labels label-format preprocess)
  2531. "Apply line numbers to literal examples and handle code references.
  2532. Handle user-specified options under info node `(org)Literal
  2533. examples' and return the modified source block.
  2534. TEXT contains the source or example block.
  2535. SKIP1 and SKIP2 are the number of lines that are to be skipped at
  2536. the beginning and end of TEXT. Use these to skip over
  2537. backend-specific lines pre-pended or appended to the original
  2538. source block.
  2539. NUMBER is non-nil if the literal example specifies \"+n\" or
  2540. \"-n\" switch. If NUMBER is non-nil add line numbers.
  2541. CONT is non-nil if the literal example specifies \"+n\" switch.
  2542. If CONT is nil, start numbering this block from 1. Otherwise
  2543. continue numbering from the last numbered block.
  2544. REPLACE-LABELS is dual-purpose.
  2545. 1. It controls the retention of labels in the exported block.
  2546. 2. It specifies in what manner the links (or references) to a
  2547. labelled line be formatted.
  2548. REPLACE-LABELS is the symbol `keep' if the literal example
  2549. specifies \"-k\" option, is numeric if the literal example
  2550. specifies \"-r\" option and is nil otherwise.
  2551. Handle REPLACE-LABELS as below:
  2552. - If nil, retain labels in the exported block and use
  2553. user-provided labels for referencing the labelled lines.
  2554. - If it is a number, remove labels in the exported block and use
  2555. one of line numbers or labels for referencing labelled lines based
  2556. on NUMBER option.
  2557. - If it is a keep, retain labels in the exported block and use
  2558. one of line numbers or labels for referencing labelled lines
  2559. based on NUMBER option.
  2560. LABEL-FORMAT is the value of \"-l\" switch associated with
  2561. literal example. See `org-coderef-label-format'.
  2562. PREPROCESS is intended for backend-agnostic handling of source
  2563. block numbering. When non-nil do the following:
  2564. - do not number the lines
  2565. - always strip the labels from exported block
  2566. - do not make the labelled line a target of an incoming link.
  2567. Instead mark the labelled line with `org-coderef' property and
  2568. store the label in it."
  2569. (setq skip1 (or skip1 0) skip2 (or skip2 0))
  2570. (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
  2571. (with-temp-buffer
  2572. (insert text)
  2573. (goto-char (point-max))
  2574. (skip-chars-backward " \t\n\r")
  2575. (delete-region (point) (point-max))
  2576. (beginning-of-line (- 1 skip2))
  2577. (let* ((last (org-current-line))
  2578. (n org-export-last-code-line-counter-value)
  2579. (nmax (+ n (- last skip1)))
  2580. (fmt (format "%%%dd: " (length (number-to-string nmax))))
  2581. (fm
  2582. (cond
  2583. ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
  2584. fmt))
  2585. ((eq org-export-current-backend 'ascii) fmt)
  2586. ((eq org-export-current-backend 'latex) fmt)
  2587. ((eq org-export-current-backend 'docbook) fmt)
  2588. (t "")))
  2589. (label-format (or label-format org-coderef-label-format))
  2590. (label-pre (if (string-match "%s" label-format)
  2591. (substring label-format 0 (match-beginning 0))
  2592. label-format))
  2593. (label-post (if (string-match "%s" label-format)
  2594. (substring label-format (match-end 0))
  2595. ""))
  2596. (lbl-re
  2597. (concat
  2598. ".*?\\S-.*?\\([ \t]*\\("
  2599. (regexp-quote label-pre)
  2600. "\\([-a-zA-Z0-9_ ]+\\)"
  2601. (regexp-quote label-post)
  2602. "\\)\\)"))
  2603. ref)
  2604. (org-goto-line (1+ skip1))
  2605. (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
  2606. (when number (incf n))
  2607. (if (or preprocess (not number))
  2608. (forward-char 1)
  2609. (insert (format fm n)))
  2610. (when (looking-at lbl-re)
  2611. (setq ref (match-string 3))
  2612. (cond ((numberp replace-labels)
  2613. ;; remove labels; use numbers for references when lines
  2614. ;; are numbered, use labels otherwise
  2615. (delete-region (match-beginning 1) (match-end 1))
  2616. (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
  2617. ((eq replace-labels 'keep)
  2618. ;; don't remove labels; use numbers for references when
  2619. ;; lines are numbered, use labels otherwise
  2620. (goto-char (match-beginning 2))
  2621. (delete-region (match-beginning 2) (match-end 2))
  2622. (unless preprocess
  2623. (insert "(" ref ")"))
  2624. (push (cons ref (if (> n 0) n (concat "(" ref ")")))
  2625. org-export-code-refs))
  2626. (t
  2627. ;; don't remove labels and don't use numbers for
  2628. ;; references
  2629. (goto-char (match-beginning 2))
  2630. (delete-region (match-beginning 2) (match-end 2))
  2631. (unless preprocess
  2632. (insert "(" ref ")"))
  2633. (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
  2634. (when (and (eq org-export-current-backend 'html) (not preprocess))
  2635. (save-excursion
  2636. (beginning-of-line 1)
  2637. (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
  2638. ref))
  2639. (end-of-line 1)
  2640. (insert "</span>")))
  2641. (when preprocess
  2642. (add-text-properties
  2643. (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
  2644. (setq org-export-last-code-line-counter-value n)
  2645. (goto-char (point-max))
  2646. (newline)
  2647. (buffer-string))))
  2648. (defun org-search-todo-below (line lines level)
  2649. "Search the subtree below LINE for any TODO entries."
  2650. (let ((rest (cdr (memq line lines)))
  2651. (re org-todo-line-regexp)
  2652. line lv todo)
  2653. (catch 'exit
  2654. (while (setq line (pop rest))
  2655. (if (string-match re line)
  2656. (progn
  2657. (setq lv (- (match-end 1) (match-beginning 1))
  2658. todo (and (match-beginning 2)
  2659. (not (member (match-string 2 line)
  2660. org-done-keywords))))
  2661. ; TODO, not DONE
  2662. (if (<= lv level) (throw 'exit nil))
  2663. (if todo (throw 'exit t))))))))
  2664. ;;;###autoload
  2665. (defun org-export-visible (type arg)
  2666. "Create a copy of the visible part of the current buffer, and export it.
  2667. The copy is created in a temporary buffer and removed after use.
  2668. TYPE is the final key (as a string) that also selects the export command in
  2669. the \\<org-mode-map>\\[org-export] export dispatcher.
  2670. As a special case, if the you type SPC at the prompt, the temporary
  2671. org-mode file will not be removed but presented to you so that you can
  2672. continue to use it. The prefix arg ARG is passed through to the exporting
  2673. command."
  2674. (interactive
  2675. (list (progn
  2676. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]buffer with HTML [D]ocBook [l]atex [p]df [d]view pdf [L]atex buffer [x]OXO [ ]keep buffer")
  2677. (read-char-exclusive))
  2678. current-prefix-arg))
  2679. (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
  2680. (error "Invalid export key"))
  2681. (let* ((binding (cdr (assoc type
  2682. '(
  2683. (?a . org-export-as-ascii)
  2684. (?A . org-export-as-ascii-to-buffer)
  2685. (?n . org-export-as-latin1)
  2686. (?N . org-export-as-latin1-to-buffer)
  2687. (?u . org-export-as-utf8)
  2688. (?U . org-export-as-utf8-to-buffer)
  2689. (?\C-a . org-export-as-ascii)
  2690. (?b . org-export-as-html-and-open)
  2691. (?\C-b . org-export-as-html-and-open)
  2692. (?h . org-export-as-html)
  2693. (?H . org-export-as-html-to-buffer)
  2694. (?R . org-export-region-as-html)
  2695. (?D . org-export-as-docbook)
  2696. (?l . org-export-as-latex)
  2697. (?p . org-export-as-pdf)
  2698. (?d . org-export-as-pdf-and-open)
  2699. (?L . org-export-as-latex-to-buffer)
  2700. (?x . org-export-as-xoxo)))))
  2701. (keepp (equal type ?\ ))
  2702. (file buffer-file-name)
  2703. (buffer (get-buffer-create "*Org Export Visible*"))
  2704. s e)
  2705. ;; Need to hack the drawers here.
  2706. (save-excursion
  2707. (goto-char (point-min))
  2708. (while (re-search-forward org-drawer-regexp nil t)
  2709. (goto-char (match-beginning 1))
  2710. (or (outline-invisible-p) (org-flag-drawer nil))))
  2711. (with-current-buffer buffer (erase-buffer))
  2712. (save-excursion
  2713. (setq s (goto-char (point-min)))
  2714. (while (not (= (point) (point-max)))
  2715. (goto-char (org-find-invisible))
  2716. (append-to-buffer buffer s (point))
  2717. (setq s (goto-char (org-find-visible))))
  2718. (org-cycle-hide-drawers 'all)
  2719. (goto-char (point-min))
  2720. (unless keepp
  2721. ;; Copy all comment lines to the end, to make sure #+ settings are
  2722. ;; still available for the second export step. Kind of a hack, but
  2723. ;; does do the trick.
  2724. (if (looking-at "#[^\r\n]*")
  2725. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  2726. (when (re-search-forward "^\\*+[ \t]+" nil t)
  2727. (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
  2728. (append-to-buffer buffer (1+ (match-beginning 0))
  2729. (min (point-max) (1+ (match-end 0)))))))
  2730. (set-buffer buffer)
  2731. (let ((buffer-file-name file)
  2732. (org-inhibit-startup t))
  2733. (org-mode)
  2734. (show-all)
  2735. (unless keepp (funcall binding arg))))
  2736. (if (not keepp)
  2737. (kill-buffer buffer)
  2738. (switch-to-buffer-other-window buffer)
  2739. (goto-char (point-min)))))
  2740. (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
  2741. (defun org-export-string (string fmt &optional dir)
  2742. "Export STRING to FMT using existing export facilities.
  2743. During export STRING is saved to a temporary file whose location
  2744. could vary. Optional argument DIR can be used to force the
  2745. directory in which the temporary file is created during export
  2746. which can be useful for resolving relative paths. Dir defaults
  2747. to the value of `temporary-file-directory'."
  2748. (let ((temporary-file-directory (or dir temporary-file-directory))
  2749. (tmp-file (make-temp-file "org-")))
  2750. (unwind-protect
  2751. (with-temp-buffer
  2752. (insert string)
  2753. (write-file tmp-file)
  2754. (org-load-modules-maybe)
  2755. (unless org-local-vars
  2756. (setq org-local-vars (org-get-local-variables)))
  2757. (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
  2758. (list 'let org-local-vars
  2759. (list (intern (format "org-export-as-%s" fmt))
  2760. nil nil nil ''string t))))
  2761. (delete-file tmp-file))))
  2762. ;;;###autoload
  2763. (defun org-export-as-org (arg &optional hidden ext-plist
  2764. to-buffer body-only pub-dir)
  2765. "Make a copy with not-exporting stuff removed.
  2766. The purpose of this function is to provide a way to export the source
  2767. Org file of a webpage in Org format, but with sensitive and/or irrelevant
  2768. stuff removed. This command will remove the following:
  2769. - archived trees (if the variable `org-export-with-archived-trees' is nil)
  2770. - comment blocks and trees starting with the COMMENT keyword
  2771. - only trees that are consistent with `org-export-select-tags'
  2772. and `org-export-exclude-tags'.
  2773. The only arguments that will be used are EXT-PLIST and PUB-DIR,
  2774. all the others will be ignored (but are present so that the general
  2775. mechanism to call publishing functions will work).
  2776. EXT-PLIST is a property list with external parameters overriding
  2777. org-mode's default settings, but still inferior to file-local
  2778. settings. When PUB-DIR is set, use this as the publishing
  2779. directory."
  2780. (interactive "P")
  2781. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  2782. ext-plist
  2783. (org-infile-export-plist)))
  2784. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  2785. (filename (concat (file-name-as-directory
  2786. (or pub-dir
  2787. (org-export-directory :org opt-plist)))
  2788. (file-name-sans-extension
  2789. (file-name-nondirectory bfname))
  2790. ".org"))
  2791. (filename (and filename
  2792. (if (equal (file-truename filename)
  2793. (file-truename bfname))
  2794. (concat (file-name-sans-extension filename)
  2795. "-source."
  2796. (file-name-extension filename))
  2797. filename)))
  2798. (backup-inhibited t)
  2799. (buffer (find-file-noselect filename))
  2800. (region (buffer-string))
  2801. str-ret)
  2802. (save-excursion
  2803. (org-pop-to-buffer-same-window buffer)
  2804. (erase-buffer)
  2805. (insert region)
  2806. (let ((org-inhibit-startup t)) (org-mode))
  2807. (org-install-letbind)
  2808. ;; Get rid of archived trees
  2809. (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
  2810. ;; Remove comment environment and comment subtrees
  2811. (org-export-remove-comment-blocks-and-subtrees)
  2812. ;; Get rid of excluded trees
  2813. (org-export-handle-export-tags (plist-get opt-plist :select-tags)
  2814. (plist-get opt-plist :exclude-tags))
  2815. (when (or (plist-get opt-plist :plain-source)
  2816. (not (or (plist-get opt-plist :plain-source)
  2817. (plist-get opt-plist :htmlized-source))))
  2818. ;; Either nothing special is requested (default call)
  2819. ;; or the plain source is explicitly requested
  2820. ;; so: save it
  2821. (save-buffer))
  2822. (when (plist-get opt-plist :htmlized-source)
  2823. ;; Make the htmlized version
  2824. (require 'htmlize)
  2825. (require 'org-html)
  2826. (font-lock-fontify-buffer)
  2827. (let* ((htmlize-output-type 'css)
  2828. (newbuf (htmlize-buffer)))
  2829. (with-current-buffer newbuf
  2830. (when org-export-htmlized-org-css-url
  2831. (goto-char (point-min))
  2832. (and (re-search-forward
  2833. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
  2834. nil t)
  2835. (replace-match
  2836. (format
  2837. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  2838. org-export-htmlized-org-css-url)
  2839. t t)))
  2840. (write-file (concat filename ".html")))
  2841. (kill-buffer newbuf)))
  2842. (set-buffer-modified-p nil)
  2843. (if (equal to-buffer 'string)
  2844. (progn (setq str-ret (buffer-string))
  2845. (kill-buffer (current-buffer))
  2846. str-ret)
  2847. (kill-buffer (current-buffer))))))
  2848. (defvar org-archive-location) ;; gets loaded with the org-archive require.
  2849. (defun org-get-current-options ()
  2850. "Return a string with current options as keyword options.
  2851. Does include HTML export options as well as TODO and CATEGORY stuff."
  2852. (require 'org-archive)
  2853. (format
  2854. "#+TITLE: %s
  2855. #+AUTHOR: %s
  2856. #+EMAIL: %s
  2857. #+DATE: %s
  2858. #+DESCRIPTION:
  2859. #+KEYWORDS:
  2860. #+LANGUAGE: %s
  2861. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
  2862. #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
  2863. %s
  2864. #+EXPORT_SELECT_TAGS: %s
  2865. #+EXPORT_EXCLUDE_TAGS: %s
  2866. #+LINK_UP: %s
  2867. #+LINK_HOME: %s
  2868. #+XSLT:
  2869. #+CATEGORY: %s
  2870. #+SEQ_TODO: %s
  2871. #+TYP_TODO: %s
  2872. #+PRIORITIES: %c %c %c
  2873. #+DRAWERS: %s
  2874. #+STARTUP: %s %s %s %s %s
  2875. #+TAGS: %s
  2876. #+FILETAGS: %s
  2877. #+ARCHIVE: %s
  2878. #+LINK: %s
  2879. "
  2880. (buffer-name) (user-full-name) user-mail-address
  2881. (format-time-string (substring (car org-time-stamp-formats) 1 -1))
  2882. org-export-default-language
  2883. org-export-headline-levels
  2884. org-export-with-section-numbers
  2885. org-export-with-toc
  2886. org-export-preserve-breaks
  2887. org-export-html-expand
  2888. org-export-with-fixed-width
  2889. org-export-with-tables
  2890. org-export-with-sub-superscripts
  2891. org-export-with-special-strings
  2892. org-export-with-footnotes
  2893. org-export-with-emphasize
  2894. org-export-with-timestamps
  2895. org-export-with-TeX-macros
  2896. org-export-with-LaTeX-fragments
  2897. org-export-skip-text-before-1st-heading
  2898. org-export-with-drawers
  2899. org-export-with-todo-keywords
  2900. org-export-with-priority
  2901. org-export-with-tags
  2902. (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
  2903. (mapconcat 'identity org-export-select-tags " ")
  2904. (mapconcat 'identity org-export-exclude-tags " ")
  2905. org-export-html-link-up
  2906. org-export-html-link-home
  2907. (or (ignore-errors
  2908. (file-name-sans-extension
  2909. (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
  2910. "NOFILENAME")
  2911. "TODO FEEDBACK VERIFY DONE"
  2912. "Me Jason Marie DONE"
  2913. org-highest-priority org-lowest-priority org-default-priority
  2914. (mapconcat 'identity org-drawers " ")
  2915. (cdr (assoc org-startup-folded
  2916. '((nil . "showall") (t . "overview") (content . "content"))))
  2917. (if org-odd-levels-only "odd" "oddeven")
  2918. (if org-hide-leading-stars "hidestars" "showstars")
  2919. (if org-startup-align-all-tables "align" "noalign")
  2920. (cond ((eq org-log-done t) "logdone")
  2921. ((equal org-log-done 'note) "lognotedone")
  2922. ((not org-log-done) "nologdone"))
  2923. (or (mapconcat (lambda (x)
  2924. (cond
  2925. ((equal :startgroup (car x)) "{")
  2926. ((equal :endgroup (car x)) "}")
  2927. ((equal :newline (car x)) "")
  2928. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  2929. (t (car x))))
  2930. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  2931. (mapconcat 'identity org-file-tags " ")
  2932. org-archive-location
  2933. "org file:~/org/%s.org"
  2934. ))
  2935. ;;;###autoload
  2936. (defun org-insert-export-options-template ()
  2937. "Insert into the buffer a template with information for exporting."
  2938. (interactive)
  2939. (if (not (bolp)) (newline))
  2940. (let ((s (org-get-current-options)))
  2941. (and (string-match "#\\+CATEGORY" s)
  2942. (setq s (substring s 0 (match-beginning 0))))
  2943. (insert s)))
  2944. (defvar org-table-colgroup-info nil)
  2945. (defun org-table-clean-before-export (lines &optional maybe-quoted)
  2946. "Check if the table has a marking column.
  2947. If yes remove the column and the special lines."
  2948. (setq org-table-colgroup-info nil)
  2949. (if (memq nil
  2950. (mapcar
  2951. (lambda (x) (or (string-match "^[ \t]*|-" x)
  2952. (string-match
  2953. (if maybe-quoted
  2954. "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
  2955. "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
  2956. x)))
  2957. lines))
  2958. ;; No special marking column
  2959. (progn
  2960. (setq org-table-clean-did-remove-column nil)
  2961. (delq nil
  2962. (mapcar
  2963. (lambda (x)
  2964. (cond
  2965. ((org-table-colgroup-line-p x)
  2966. ;; This line contains colgroup info, extract it
  2967. ;; and then discard the line
  2968. (setq org-table-colgroup-info
  2969. (mapcar (lambda (x)
  2970. (cond ((member x '("<" "&lt;")) :start)
  2971. ((member x '(">" "&gt;")) :end)
  2972. ((member x '("<>" "&lt;&gt;")) :startend)
  2973. (t nil)))
  2974. (org-split-string x "[ \t]*|[ \t]*")))
  2975. nil)
  2976. ((org-table-cookie-line-p x)
  2977. ;; This line contains formatting cookies, discard it
  2978. nil)
  2979. (t x)))
  2980. lines)))
  2981. ;; there is a special marking column
  2982. (setq org-table-clean-did-remove-column t)
  2983. (delq nil
  2984. (mapcar
  2985. (lambda (x)
  2986. (cond
  2987. ((org-table-colgroup-line-p x)
  2988. ;; This line contains colgroup info, extract it
  2989. ;; and then discard the line
  2990. (setq org-table-colgroup-info
  2991. (mapcar (lambda (x)
  2992. (cond ((member x '("<" "&lt;")) :start)
  2993. ((member x '(">" "&gt;")) :end)
  2994. ((member x '("<>" "&lt;&gt;")) :startend)
  2995. (t nil)))
  2996. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  2997. nil)
  2998. ((org-table-cookie-line-p x)
  2999. ;; This line contains formatting cookies, discard it
  3000. nil)
  3001. ((string-match "^[ \t]*| *[!_^/] *|" x)
  3002. ;; ignore this line
  3003. nil)
  3004. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  3005. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  3006. ;; remove the first column
  3007. (replace-match "\\1|" t nil x))))
  3008. lines))))
  3009. (defun org-export-cleanup-toc-line (s)
  3010. "Remove tags and timestamps from lines going into the toc."
  3011. (when (memq org-export-with-tags '(not-in-toc nil))
  3012. (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
  3013. (setq s (replace-match "" t t s))))
  3014. (when org-export-remove-timestamps-from-toc
  3015. (while (string-match org-maybe-keyword-time-regexp s)
  3016. (setq s (replace-match "" t t s))))
  3017. (while (string-match org-bracket-link-regexp s)
  3018. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  3019. t t s)))
  3020. (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
  3021. (setq s (replace-match "" t t s)))
  3022. s)
  3023. (defun org-get-text-property-any (pos prop &optional object)
  3024. (or (get-text-property pos prop object)
  3025. (and (setq pos (next-single-property-change pos prop object))
  3026. (get-text-property pos prop object))))
  3027. (defun org-export-get-coderef-format (path desc)
  3028. (save-match-data
  3029. (if (and desc (string-match
  3030. (regexp-quote (concat "(" path ")"))
  3031. desc))
  3032. (replace-match "%s" t t desc)
  3033. (or desc "%s"))))
  3034. (defun org-export-push-to-kill-ring (format)
  3035. "Push buffer content to kill ring.
  3036. The depends on the variable `org-export-copy-to-kill-ring'."
  3037. (when org-export-copy-to-kill-ring
  3038. (org-kill-new (buffer-string))
  3039. (when (fboundp 'x-set-selection)
  3040. (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
  3041. (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
  3042. (message "%s export done, pushed to kill ring and clipboard" format)))
  3043. (provide 'org-exp)
  3044. ;;; org-exp.el ends here