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