org-exp.el 124 KB

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