org-html.el 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  1. ;;; org-html.el --- HTML export for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.5
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;;; Code:
  25. (require 'org-exp)
  26. (require 'format-spec)
  27. (eval-when-compile (require 'cl))
  28. (declare-function org-id-find-id-file "org-id" (id))
  29. (declare-function htmlize-region "ext:htmlize" (beg end))
  30. (defgroup org-export-html nil
  31. "Options specific for HTML export of Org-mode files."
  32. :tag "Org Export HTML"
  33. :group 'org-export)
  34. (defcustom org-export-html-footnotes-section "<div id=\"footnotes\">
  35. <h2 class=\"footnotes\">%s: </h2>
  36. <div id=\"text-footnotes\">
  37. %s
  38. </div>
  39. </div>"
  40. "Format for the footnotes section.
  41. Should contain a two instances of %s. The first will be replaced with the
  42. language-specific word for \"Footnotes\", the second one will be replaced
  43. by the footnotes themselves."
  44. :group 'org-export-html
  45. :type 'string)
  46. (defcustom org-export-html-footnote-format "<sup>%s</sup>"
  47. "The format for the footnote reference.
  48. %s will be replaced by the footnote reference itself."
  49. :group 'org-export-html
  50. :type 'string)
  51. (defcustom org-export-html-coding-system nil
  52. "Coding system for HTML export, defaults to `buffer-file-coding-system'."
  53. :group 'org-export-html
  54. :type 'coding-system)
  55. (defcustom org-export-html-extension "html"
  56. "The extension for exported HTML files."
  57. :group 'org-export-html
  58. :type 'string)
  59. (defcustom org-export-html-xml-declaration
  60. '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
  61. ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>"))
  62. "The extension for exported HTML files.
  63. %s will be replaced with the charset of the exported file.
  64. This may be a string, or an alist with export extensions
  65. and corresponding declarations."
  66. :group 'org-export-html
  67. :type '(choice
  68. (string :tag "Single declaration")
  69. (repeat :tag "Dependent on extension"
  70. (cons (string :tag "Extension")
  71. (string :tag "Declaration")))))
  72. (defcustom org-export-html-style-include-scripts t
  73. "Non-nil means include the JavaScript snippets in exported HTML files.
  74. The actual script is defined in `org-export-html-scripts' and should
  75. not be modified."
  76. :group 'org-export-html
  77. :type 'boolean)
  78. (defconst org-export-html-scripts
  79. "<script type=\"text/javascript\">
  80. <!--/*--><![CDATA[/*><!--*/
  81. function CodeHighlightOn(elem, id)
  82. {
  83. var target = document.getElementById(id);
  84. if(null != target) {
  85. elem.cacheClassElem = elem.className;
  86. elem.cacheClassTarget = target.className;
  87. target.className = \"code-highlighted\";
  88. elem.className = \"code-highlighted\";
  89. }
  90. }
  91. function CodeHighlightOff(elem, id)
  92. {
  93. var target = document.getElementById(id);
  94. if(elem.cacheClassElem)
  95. elem.className = elem.cacheClassElem;
  96. if(elem.cacheClassTarget)
  97. target.className = elem.cacheClassTarget;
  98. }
  99. /*]]>*///-->
  100. </script>"
  101. "Basic JavaScript that is needed by HTML files produced by Org-mode.")
  102. (defconst org-export-html-style-default
  103. "<style type=\"text/css\">
  104. <!--/*--><![CDATA[/*><!--*/
  105. html { font-family: Times, serif; font-size: 12pt; }
  106. .title { text-align: center; }
  107. .todo { color: red; }
  108. .done { color: green; }
  109. .tag { background-color: #add8e6; font-weight:normal }
  110. .target { }
  111. .timestamp { color: #bebebe; }
  112. .timestamp-kwd { color: #5f9ea0; }
  113. .right {margin-left:auto; margin-right:0px; text-align:right;}
  114. .left {margin-left:0px; margin-right:auto; text-align:left;}
  115. .center {margin-left:auto; margin-right:auto; text-align:center;}
  116. p.verse { margin-left: 3% }
  117. pre {
  118. border: 1pt solid #AEBDCC;
  119. background-color: #F3F5F7;
  120. padding: 5pt;
  121. font-family: courier, monospace;
  122. font-size: 90%;
  123. overflow:auto;
  124. }
  125. table { border-collapse: collapse; }
  126. td, th { vertical-align: top; }
  127. th.right { text-align:center; }
  128. th.left { text-align:center; }
  129. th.center { text-align:center; }
  130. td.right { text-align:right; }
  131. td.left { text-align:left; }
  132. td.center { text-align:center; }
  133. dt { font-weight: bold; }
  134. div.figure { padding: 0.5em; }
  135. div.figure p { text-align: center; }
  136. textarea { overflow-x: auto; }
  137. .linenr { font-size:smaller }
  138. .code-highlighted {background-color:#ffff00;}
  139. .org-info-js_info-navigation { border-style:none; }
  140. #org-info-js_console-label { font-size:10px; font-weight:bold;
  141. white-space:nowrap; }
  142. .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
  143. font-weight:bold; }
  144. /*]]>*/-->
  145. </style>"
  146. "The default style specification for exported HTML files.
  147. Please use the variables `org-export-html-style' and
  148. `org-export-html-style-extra' to add to this style. If you wish to not
  149. have the default style included, customize the variable
  150. `org-export-html-style-include-default'.")
  151. (defcustom org-export-html-style-include-default t
  152. "Non-nil means include the default style in exported HTML files.
  153. The actual style is defined in `org-export-html-style-default' and should
  154. not be modified. Use the variables `org-export-html-style' to add
  155. your own style information."
  156. :group 'org-export-html
  157. :type 'boolean)
  158. ;;;###autoload
  159. (put 'org-export-html-style-include-default 'safe-local-variable 'booleanp)
  160. (defcustom org-export-html-style ""
  161. "Org-wide style definitions for exported HTML files.
  162. This variable needs to contain the full HTML structure to provide a style,
  163. including the surrounding HTML tags. If you set the value of this variable,
  164. you should consider to include definitions for the following classes:
  165. title, todo, done, timestamp, timestamp-kwd, tag, target.
  166. For example, a valid value would be:
  167. <style type=\"text/css\">
  168. <![CDATA[
  169. p { font-weight: normal; color: gray; }
  170. h1 { color: black; }
  171. .title { text-align: center; }
  172. .todo, .timestamp-kwd { color: red; }
  173. .done { color: green; }
  174. ]]>
  175. </style>
  176. If you'd like to refer to en external style file, use something like
  177. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  178. As the value of this option simply gets inserted into the HTML <head> header,
  179. you can \"misuse\" it to add arbitrary text to the header.
  180. See also the variable `org-export-html-style-extra'."
  181. :group 'org-export-html
  182. :type 'string)
  183. ;;;###autoload
  184. (put 'org-export-html-style 'safe-local-variable 'stringp)
  185. (defcustom org-export-html-style-extra ""
  186. "Additional style information for HTML export.
  187. The value of this variable is inserted into the HTML buffer right after
  188. the value of `org-export-html-style'. Use this variable for per-file
  189. settings of style information, and do not forget to surround the style
  190. settings with <style>...</style> tags."
  191. :group 'org-export-html
  192. :type 'string)
  193. ;;;###autoload
  194. (put 'org-export-html-style-extra 'safe-local-variable 'stringp)
  195. (defcustom org-export-html-mathjax-options
  196. '((path "http://orgmode.org/mathjax/MathJax.js")
  197. (scale "100")
  198. (align "center")
  199. (indent "2em")
  200. (mathml nil))
  201. "Options for MathJax setup.
  202. path The path where to find MathJax
  203. scale Scaling for the HTML-CSS backend, usually between 100 and 133
  204. align How to align display math: left, center, or right
  205. indent If align is not center, how far from the left/right side?
  206. mathml Should a MathML player be used if available?
  207. This is faster and reduces bandwidth use, but currently
  208. sometimes has lower spacing quality. Therefore, the default is
  209. nil. When browsers get better, this switch can be flipped.
  210. You can also customize this for each buffer, using something like
  211. #+MATHJAX: scale:\"133\" align:\"right\" mathml:t path:\"/MathJax/\""
  212. :group 'org-export-html
  213. :type '(list :greedy t
  214. (list :tag "path (the path from where to load MathJax.js)"
  215. (const :format " " path) (string))
  216. (list :tag "scale (scaling for the displayed math)"
  217. (const :format " " scale) (string))
  218. (list :tag "align (alignment of displayed equations)"
  219. (const :format " " align) (string))
  220. (list :tag "indent (indentation with left or right alignment)"
  221. (const :format " " indent) (string))
  222. (list :tag "mathml (should MathML display be used is possible)"
  223. (const :format " " mathml) (boolean))))
  224. (defun org-export-html-mathjax-config (template options in-buffer)
  225. "Insert the user setup into the matchjax template."
  226. (let (name val (yes " ") (no "// ") x)
  227. (mapc
  228. (lambda (e)
  229. (setq name (car e) val (nth 1 e))
  230. (if (string-match (concat "\\<" (symbol-name name) ":") in-buffer)
  231. (setq val (car (read-from-string
  232. (substring in-buffer (match-end 0))))))
  233. (if (not (stringp val)) (setq val (format "%s" val)))
  234. (if (string-match (concat "%" (upcase (symbol-name name))) template)
  235. (setq template (replace-match val t t template))))
  236. options)
  237. (setq val (nth 1 (assq 'mathml options)))
  238. (if (string-match (concat "\\<mathml:") in-buffer)
  239. (setq val (car (read-from-string
  240. (substring in-buffer (match-end 0))))))
  241. ;; Exchange prefixes depending on mathml setting
  242. (if (not val) (setq x yes yes no no x))
  243. ;; Replace cookies to turn on or off the config/jax lines
  244. (if (string-match ":MMLYES:" template)
  245. (setq template (replace-match yes t t template)))
  246. (if (string-match ":MMLNO:" template)
  247. (setq template (replace-match no t t template)))
  248. ;; Return the modified template
  249. template))
  250. (defcustom org-export-html-mathjax-template
  251. "<script type=\"text/javascript\" src=\"%PATH\">
  252. <!--/*--><![CDATA[/*><!--*/
  253. MathJax.Hub.Config({
  254. // Only one of the two following lines, depending on user settings
  255. // First allows browser-native MathML display, second forces HTML/CSS
  256. :MMLYES: config: [\"MMLorHTML.js\"], jax: [\"input/TeX\"],
  257. :MMLNO: jax: [\"input/TeX\", \"output/HTML-CSS\"],
  258. extensions: [\"tex2jax.js\",\"TeX/AMSmath.js\",\"TeX/AMSsymbols.js\",
  259. \"TeX/noUndefined.js\"],
  260. tex2jax: {
  261. inlineMath: [ [\"\\\\(\",\"\\\\)\"] ],
  262. displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"], [\"\\\\begin{displaymath}\",\"\\\\end{displaymath}\"] ],
  263. skipTags: [\"script\",\"noscript\",\"style\",\"textarea\",\"pre\",\"code\"],
  264. ignoreClass: \"tex2jax_ignore\",
  265. processEscapes: false,
  266. processEnvironments: true,
  267. preview: \"TeX\"
  268. },
  269. showProcessingMessages: true,
  270. displayAlign: \"%ALIGN\",
  271. displayIndent: \"%INDENT\",
  272. \"HTML-CSS\": {
  273. scale: %SCALE,
  274. availableFonts: [\"STIX\",\"TeX\"],
  275. preferredFont: \"TeX\",
  276. webFont: \"TeX\",
  277. imageFont: \"TeX\",
  278. showMathMenu: true,
  279. },
  280. MMLorHTML: {
  281. prefer: {
  282. MSIE: \"MML\",
  283. Firefox: \"MML\",
  284. Opera: \"HTML\",
  285. other: \"HTML\"
  286. }
  287. }
  288. });
  289. /*]]>*///-->
  290. </script>"
  291. "The MathJax setup for XHTML files."
  292. :group 'org-export-html
  293. :type 'string)
  294. (defcustom org-export-html-tag-class-prefix ""
  295. "Prefix to class names for TODO keywords.
  296. Each tag gets a class given by the tag itself, with this prefix.
  297. The default prefix is empty because it is nice to just use the keyword
  298. as a class name. But if you get into conflicts with other, existing
  299. CSS classes, then this prefix can be very useful."
  300. :group 'org-export-html
  301. :type 'string)
  302. (defcustom org-export-html-todo-kwd-class-prefix ""
  303. "Prefix to class names for TODO keywords.
  304. Each TODO keyword gets a class given by the keyword itself, with this prefix.
  305. The default prefix is empty because it is nice to just use the keyword
  306. as a class name. But if you get into conflicts with other, existing
  307. CSS classes, then this prefix can be very useful."
  308. :group 'org-export-html
  309. :type 'string)
  310. (defcustom org-export-html-preamble t
  311. "Non-nil means insert a preamble in HTML export.
  312. When `t', insert a string as defined by one of the formatting
  313. strings in `org-export-html-preamble-format'. When set to a
  314. string, this string overrides `org-export-html-preamble-format'.
  315. When set to a function, apply this function and insert the
  316. returned string. The function takes the property list of export
  317. options as its only argument.
  318. Setting :html-preamble in publishing projects will take
  319. precedence over this variable."
  320. :group 'org-export-html
  321. :type '(choice (const :tag "No preamble" nil)
  322. (const :tag "Default preamble" t)
  323. (string :tag "Custom formatting string")
  324. (function :tag "Function (must return a string)")))
  325. (defcustom org-export-html-preamble-format
  326. '(("en" "<h1 class=\"title\">%t</h1>"))
  327. "The format for the HTML preamble.
  328. %t stands for the title.
  329. If you need to use a \"%\" character, you need to escape it
  330. like that: \"%%\"."
  331. :group 'org-export-html
  332. :type 'string)
  333. (defcustom org-export-html-postamble 'auto
  334. "Non-nil means insert a postamble in HTML export.
  335. When `t', insert a string as defined by the formatting string in
  336. `org-export-html-postamble-format'. When set to a string, this
  337. string overrides `org-export-html-postamble-format'. When set to
  338. 'auto, discard `org-export-html-postamble-format' and honor
  339. `org-export-author/email/creator-info' variables. When set to a
  340. function, apply this function and insert the returned string.
  341. The function takes the property list of export options as its
  342. only argument.
  343. Setting :html-postamble in publishing projects will take
  344. precedence over this variable."
  345. :group 'org-export-html
  346. :type '(choice (const :tag "No postamble" nil)
  347. (const :tag "Auto preamble" 'auto)
  348. (const :tag "Default formatting string" t)
  349. (string :tag "Custom formatting string")
  350. (function :tag "Function (must return a string)")))
  351. (defcustom org-export-html-postamble-format
  352. '(("en" "<p class=\"author\">Author: %a (%e)</p>
  353. <p class=\"date\">Date: %d</p>
  354. <p class=\"creator\">Generated by %c</p>
  355. <p class=\"xhtml-validation\">%v</p>
  356. "))
  357. "The format for the HTML postamble.
  358. %a stands for the author.
  359. %e stands for the email(s).
  360. %d stands for the date.
  361. %c will be replaced by information about Org/Emacs.
  362. %v will be replaced by `org-export-html-validation-link'.
  363. If you need to use a \"%\" character, you need to escape it
  364. like that: \"%%\"."
  365. :group 'org-export-html
  366. :type 'string)
  367. (defcustom org-export-html-home/up-format
  368. "<div id=\"org-div-home-and-up\" style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
  369. <a accesskey=\"h\" href=\"%s\"> UP </a>
  370. |
  371. <a accesskey=\"H\" href=\"%s\"> HOME </a>
  372. </div>"
  373. "Snippet used to insert the HOME and UP links.
  374. This is a format string, the first %s will receive the UP link,
  375. the second the HOME link. If both `org-export-html-link-up' and
  376. `org-export-html-link-home' are empty, the entire snippet will be
  377. ignored."
  378. :group 'org-export-html
  379. :type 'string)
  380. (defcustom org-export-html-toplevel-hlevel 2
  381. "The <H> level for level 1 headings in HTML export.
  382. This is also important for the classes that will be wrapped around headlines
  383. and outline structure. If this variable is 1, the top-level headlines will
  384. be <h1>, and the corresponding classes will be outline-1, section-number-1,
  385. and outline-text-1. If this is 2, all of these will get a 2 instead.
  386. The default for this variable is 2, because we use <h1> for formatting the
  387. document title."
  388. :group 'org-export-html
  389. :type 'string)
  390. (defcustom org-export-html-link-org-files-as-html t
  391. "Non-nil means make file links to `file.org' point to `file.html'.
  392. When org-mode is exporting an org-mode file to HTML, links to
  393. non-html files are directly put into a href tag in HTML.
  394. However, links to other Org-mode files (recognized by the
  395. extension `.org.) should become links to the corresponding html
  396. file, assuming that the linked org-mode file will also be
  397. converted to HTML.
  398. When nil, the links still point to the plain `.org' file."
  399. :group 'org-export-html
  400. :type 'boolean)
  401. (defcustom org-export-html-inline-images 'maybe
  402. "Non-nil means inline images into exported HTML pages.
  403. This is done using an <img> tag. When nil, an anchor with href is used to
  404. link to the image. If this option is `maybe', then images in links with
  405. an empty description will be inlined, while images with a description will
  406. be linked only."
  407. :group 'org-export-html
  408. :type '(choice (const :tag "Never" nil)
  409. (const :tag "Always" t)
  410. (const :tag "When there is no description" maybe)))
  411. (defcustom org-export-html-inline-image-extensions
  412. '("png" "jpeg" "jpg" "gif" "svg")
  413. "Extensions of image files that can be inlined into HTML."
  414. :group 'org-export-html
  415. :type '(repeat (string :tag "Extension")))
  416. (defcustom org-export-html-table-tag
  417. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  418. "The HTML tag that is used to start a table.
  419. This must be a <table> tag, but you may change the options like
  420. borders and spacing."
  421. :group 'org-export-html
  422. :type 'string)
  423. (defcustom org-export-table-header-tags '("<th scope=\"%s\"%s>" . "</th>")
  424. "The opening tag for table header fields.
  425. This is customizable so that alignment options can be specified.
  426. The first %s will be filled with the scope of the field, either row or col.
  427. The second %s will be replaced by a style entry to align the field.
  428. See also the variable `org-export-html-table-use-header-tags-for-first-column'.
  429. See also the variable `org-export-html-table-align-individual-fields'."
  430. :group 'org-export-tables
  431. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  432. (defcustom org-export-table-data-tags '("<td%s>" . "</td>")
  433. "The opening tag for table data fields.
  434. This is customizable so that alignment options can be specified.
  435. The first %s will be filled with the scope of the field, either row or col.
  436. The second %s will be replaced by a style entry to align the field.
  437. See also the variable `org-export-html-table-align-individual-fields'."
  438. :group 'org-export-tables
  439. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  440. (defcustom org-export-table-row-tags '("<tr>" . "</tr>")
  441. "The opening tag for table data fields.
  442. This is customizable so that alignment options can be specified.
  443. Instead of strings, these can be Lisp forms that will be evaluated
  444. for each row in order to construct the table row tags. During evaluation,
  445. the variable `head' will be true when this is a header line, nil when this
  446. is a body line. And the variable `nline' will contain the line number,
  447. starting from 1 in the first header line. For example
  448. (setq org-export-table-row-tags
  449. (cons '(if head
  450. \"<tr>\"
  451. (if (= (mod nline 2) 1)
  452. \"<tr class=\\\"tr-odd\\\">\"
  453. \"<tr class=\\\"tr-even\\\">\"))
  454. \"</tr>\"))
  455. will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
  456. :group 'org-export-tables
  457. :type '(cons
  458. (choice :tag "Opening tag"
  459. (string :tag "Specify")
  460. (sexp))
  461. (choice :tag "Closing tag"
  462. (string :tag "Specify")
  463. (sexp))))
  464. (defcustom org-export-html-table-align-individual-fields t
  465. "Non-nil means attach style attributes for alignment to each table field.
  466. When nil, alignment will only be specified in the column tags, but this
  467. is ignored by some browsers (like Firefox, Safari). Opera does it right
  468. though."
  469. :group 'org-export-tables
  470. :type 'boolean)
  471. (defcustom org-export-html-table-use-header-tags-for-first-column nil
  472. "Non-nil means format column one in tables with header tags.
  473. When nil, also column one will use data tags."
  474. :group 'org-export-tables
  475. :type 'boolean)
  476. (defcustom org-export-html-validation-link
  477. "<a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a>"
  478. "Link to HTML validation service."
  479. :group 'org-export-html
  480. :type 'string)
  481. (defcustom org-export-html-with-timestamp nil
  482. "If non-nil, write timestamp into the exported HTML text.
  483. If non-nil, write `org-export-html-html-helper-timestamp' into the
  484. exported HTML text. Otherwise, the buffer will just be saved to
  485. a file."
  486. :group 'org-export-html
  487. :type 'boolean)
  488. (defcustom org-export-html-html-helper-timestamp
  489. "<br/><br/><hr/><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  490. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  491. :group 'org-export-html
  492. :type 'string)
  493. (defcustom org-export-html-protect-char-alist
  494. '(("&" . "&amp;")
  495. ("<" . "&lt;")
  496. (">" . "&gt;"))
  497. "Alist of characters to be converted by `org-html-protect'."
  498. :type '(repeat (cons (string :tag "Character")
  499. (string :tag "HTML equivalent"))))
  500. (defgroup org-export-htmlize nil
  501. "Options for processing examples with htmlize.el."
  502. :tag "Org Export Htmlize"
  503. :group 'org-export-html)
  504. (defcustom org-export-htmlize-output-type 'inline-css
  505. "Output type to be used by htmlize when formatting code snippets.
  506. Choices are `css', to export the CSS selectors only, or `inline-css', to
  507. export the CSS attribute values inline in the HTML. We use as default
  508. `inline-css', in order to make the resulting HTML self-containing.
  509. However, this will fail when using Emacs in batch mode for export, because
  510. then no rich font definitions are in place. It will also not be good if
  511. people with different Emacs setup contribute HTML files to a website,
  512. because the fonts will represent the individual setups. In these cases,
  513. it is much better to let Org/Htmlize assign classes only, and to use
  514. a style file to define the look of these classes.
  515. To get a start for your css file, start Emacs session and make sure that
  516. all the faces you are interested in are defined, for example by loading files
  517. in all modes you want. Then, use the command
  518. \\[org-export-htmlize-generate-css] to extract class definitions."
  519. :group 'org-export-htmlize
  520. :type '(choice (const css) (const inline-css)))
  521. (defcustom org-export-htmlize-css-font-prefix "org-"
  522. "The prefix for CSS class names for htmlize font specifications."
  523. :group 'org-export-htmlize
  524. :type 'string)
  525. (defcustom org-export-htmlized-org-css-url nil
  526. "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
  527. Normally when creating an htmlized version of an Org buffer, htmlize will
  528. create CSS to define the font colors. However, this does not work when
  529. converting in batch mode, and it also can look bad if different people
  530. with different fontification setup work on the same website.
  531. When this variable is non-nil, creating an htmlized version of an Org buffer
  532. using `org-export-as-org' will remove the internal CSS section and replace it
  533. with a link to this URL."
  534. :group 'org-export-htmlize
  535. :type '(choice
  536. (const :tag "Keep internal css" nil)
  537. (string :tag "URL or local href")))
  538. ;;; Hooks
  539. (defvar org-export-html-after-blockquotes-hook nil
  540. "Hook run during HTML export, after blockquote, verse, center are done.")
  541. (defvar org-export-html-final-hook nil
  542. "Hook run at the end of HTML export, in the new buffer.")
  543. ;;; HTML export
  544. (defun org-export-html-preprocess (parameters)
  545. "Convert LaTeX fragments to images."
  546. (when (and org-current-export-file
  547. (plist-get parameters :LaTeX-fragments))
  548. (org-format-latex
  549. (concat "ltxpng/" (file-name-sans-extension
  550. (file-name-nondirectory
  551. org-current-export-file)))
  552. org-current-export-dir nil "Creating LaTeX image %s"
  553. nil nil
  554. (cond
  555. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  556. ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
  557. ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax)
  558. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  559. (t nil))))
  560. (goto-char (point-min))
  561. (let (label l1)
  562. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  563. (org-if-unprotected-at (match-beginning 1)
  564. (setq label (match-string 1))
  565. (save-match-data
  566. (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
  567. (setq l1 (substring label (match-beginning 1)))
  568. (setq l1 label)))
  569. (replace-match (format "[[#%s][%s]]" label l1) t t)))))
  570. ;;;###autoload
  571. (defun org-export-as-html-and-open (arg)
  572. "Export the outline as HTML and immediately open it with a browser.
  573. If there is an active region, export only the region.
  574. The prefix ARG specifies how many levels of the outline should become
  575. headlines. The default is 3. Lower levels will become bulleted lists."
  576. (interactive "P")
  577. (org-export-as-html arg 'hidden)
  578. (org-open-file buffer-file-name)
  579. (when org-export-kill-product-buffer-when-displayed
  580. (kill-buffer (current-buffer))))
  581. ;;;###autoload
  582. (defun org-export-as-html-batch ()
  583. "Call the function `org-export-as-html'.
  584. This function can be used in batch processing as:
  585. emacs --batch
  586. --load=$HOME/lib/emacs/org.el
  587. --eval \"(setq org-export-headline-levels 2)\"
  588. --visit=MyFile --funcall org-export-as-html-batch"
  589. (org-export-as-html org-export-headline-levels 'hidden))
  590. ;;;###autoload
  591. (defun org-export-as-html-to-buffer (arg)
  592. "Call `org-export-as-html` with output to a temporary buffer.
  593. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  594. (interactive "P")
  595. (org-export-as-html arg nil nil "*Org HTML Export*")
  596. (when org-export-show-temporary-export-buffer
  597. (switch-to-buffer-other-window "*Org HTML Export*")))
  598. ;;;###autoload
  599. (defun org-replace-region-by-html (beg end)
  600. "Assume the current region has org-mode syntax, and convert it to HTML.
  601. This can be used in any buffer. For example, you could write an
  602. itemized list in org-mode syntax in an HTML buffer and then use this
  603. command to convert it."
  604. (interactive "r")
  605. (let (reg html buf pop-up-frames)
  606. (save-window-excursion
  607. (if (org-mode-p)
  608. (setq html (org-export-region-as-html
  609. beg end t 'string))
  610. (setq reg (buffer-substring beg end)
  611. buf (get-buffer-create "*Org tmp*"))
  612. (with-current-buffer buf
  613. (erase-buffer)
  614. (insert reg)
  615. (org-mode)
  616. (setq html (org-export-region-as-html
  617. (point-min) (point-max) t 'string)))
  618. (kill-buffer buf)))
  619. (delete-region beg end)
  620. (insert html)))
  621. ;;;###autoload
  622. (defun org-export-region-as-html (beg end &optional body-only buffer)
  623. "Convert region from BEG to END in org-mode buffer to HTML.
  624. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  625. contents, and only produce the region of converted text, useful for
  626. cut-and-paste operations.
  627. If BUFFER is a buffer or a string, use/create that buffer as a target
  628. of the converted HTML. If BUFFER is the symbol `string', return the
  629. produced HTML as a string and leave not buffer behind. For example,
  630. a Lisp program could call this function in the following way:
  631. (setq html (org-export-region-as-html beg end t 'string))
  632. When called interactively, the output buffer is selected, and shown
  633. in a window. A non-interactive call will only return the buffer."
  634. (interactive "r\nP")
  635. (when (interactive-p)
  636. (setq buffer "*Org HTML Export*"))
  637. (let ((transient-mark-mode t) (zmacs-regions t)
  638. ext-plist rtn)
  639. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  640. (goto-char end)
  641. (set-mark (point)) ;; to activate the region
  642. (goto-char beg)
  643. (setq rtn (org-export-as-html
  644. nil nil ext-plist
  645. buffer body-only))
  646. (if (fboundp 'deactivate-mark) (deactivate-mark))
  647. (if (and (interactive-p) (bufferp rtn))
  648. (switch-to-buffer-other-window rtn)
  649. rtn)))
  650. (defvar html-table-tag nil) ; dynamically scoped into this.
  651. (defvar org-par-open nil)
  652. ;;; org-html-cvt-link-fn
  653. (defconst org-html-cvt-link-fn
  654. nil
  655. "Function to convert link URLs to exportable URLs.
  656. Takes two arguments, TYPE and PATH.
  657. Returns exportable url as (TYPE PATH), or nil to signal that it
  658. didn't handle this case.
  659. Intended to be locally bound around a call to `org-export-as-html'." )
  660. (defun org-html-cvt-org-as-html (opt-plist type path)
  661. "Convert an org filename to an equivalent html filename.
  662. If TYPE is not file, just return `nil'.
  663. See variable `org-export-html-link-org-files-as-html'"
  664. (save-match-data
  665. (and
  666. org-export-html-link-org-files-as-html
  667. (string= type "file")
  668. (string-match "\\.org$" path)
  669. (progn
  670. (list
  671. "file"
  672. (concat
  673. (substring path 0 (match-beginning 0))
  674. "."
  675. (plist-get opt-plist :html-extension)))))))
  676. ;;; org-html-should-inline-p
  677. (defun org-html-should-inline-p (filename descp)
  678. "Return non-nil if link FILENAME should be inlined.
  679. The decision to inline the FILENAME link is based on the current
  680. settings. DESCP is the boolean of whether there was a link
  681. description. See variables `org-export-html-inline-images' and
  682. `org-export-html-inline-image-extensions'."
  683. (declare (special
  684. org-export-html-inline-images
  685. org-export-html-inline-image-extensions))
  686. (and (or (eq t org-export-html-inline-images)
  687. (and org-export-html-inline-images (not descp)))
  688. (org-file-image-p
  689. filename org-export-html-inline-image-extensions)))
  690. ;;; org-html-make-link
  691. (defun org-html-make-link (opt-plist type path fragment desc attr
  692. may-inline-p)
  693. "Make an HTML link.
  694. OPT-PLIST is an options list.
  695. TYPE is the device-type of the link (THIS://foo.html)
  696. PATH is the path of the link (http://THIS#locationx)
  697. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  698. DESC is the link description, if any.
  699. ATTR is a string of other attributes of the a element.
  700. MAY-INLINE-P allows inlining it as an image."
  701. (declare (special org-par-open))
  702. (save-match-data
  703. (let* ((filename path)
  704. ;;First pass. Just sanity stuff.
  705. (components-1
  706. (cond
  707. ((string= type "file")
  708. (list
  709. type
  710. ;;Substitute just if original path was absolute.
  711. ;;(Otherwise path must remain relative)
  712. (if (file-name-absolute-p path)
  713. (concat "file://" (expand-file-name path))
  714. path)))
  715. ((string= type "")
  716. (list nil path))
  717. (t (list type path))))
  718. ;;Second pass. Components converted so they can refer
  719. ;;to a remote site.
  720. (components-2
  721. (or
  722. (and org-html-cvt-link-fn
  723. (apply org-html-cvt-link-fn
  724. opt-plist components-1))
  725. (apply #'org-html-cvt-org-as-html
  726. opt-plist components-1)
  727. components-1))
  728. (type (first components-2))
  729. (thefile (second components-2)))
  730. ;;Third pass. Build final link except for leading type
  731. ;;spec.
  732. (cond
  733. ((or
  734. (not type)
  735. (string= type "http")
  736. (string= type "https")
  737. (string= type "file"))
  738. (if fragment
  739. (setq thefile (concat thefile "#" fragment))))
  740. (t))
  741. ;;Final URL-build, for all types.
  742. (setq thefile
  743. (let
  744. ((str (org-export-html-format-href thefile)))
  745. (if (and type (not (string= "file" type)))
  746. (concat type ":" str)
  747. str)))
  748. (if (and
  749. may-inline-p
  750. ;;Can't inline a URL with a fragment.
  751. (not fragment))
  752. (progn
  753. (message "image %s %s" thefile org-par-open)
  754. (org-export-html-format-image thefile org-par-open))
  755. (concat
  756. "<a href=\"" thefile "\"" (if attr (concat " " attr)) ">"
  757. (org-export-html-format-desc desc)
  758. "</a>")))))
  759. (defun org-html-handle-links (line opt-plist)
  760. "Return LINE with markup of Org mode links.
  761. OPT-PLIST is the export options list."
  762. (let ((start 0)
  763. (current-dir (if buffer-file-name
  764. (file-name-directory buffer-file-name)
  765. default-directory))
  766. (link-validate (plist-get opt-plist :link-validation-function))
  767. type id-file fnc
  768. rpl path attr desc descp desc1 desc2 link)
  769. (while (string-match org-bracket-link-analytic-regexp++ line start)
  770. (setq start (match-beginning 0))
  771. (setq path (save-match-data (org-link-unescape
  772. (match-string 3 line))))
  773. (setq type (cond
  774. ((match-end 2) (match-string 2 line))
  775. ((save-match-data
  776. (or (file-name-absolute-p path)
  777. (string-match "^\\.\\.?/" path)))
  778. "file")
  779. (t "internal")))
  780. (setq path (org-extract-attributes (org-link-unescape path)))
  781. (setq attr (get-text-property 0 'org-attributes path))
  782. (setq desc1 (if (match-end 5) (match-string 5 line))
  783. desc2 (if (match-end 2) (concat type ":" path) path)
  784. descp (and desc1 (not (equal desc1 desc2)))
  785. desc (or desc1 desc2))
  786. ;; Make an image out of the description if that is so wanted
  787. (when (and descp (org-file-image-p
  788. desc org-export-html-inline-image-extensions))
  789. (save-match-data
  790. (if (string-match "^file:" desc)
  791. (setq desc (substring desc (match-end 0)))))
  792. (setq desc (org-add-props
  793. (concat "<img src=\"" desc "\"/>")
  794. '(org-protected t))))
  795. (cond
  796. ((equal type "internal")
  797. (let
  798. ((frag-0
  799. (if (= (string-to-char path) ?#)
  800. (substring path 1)
  801. path)))
  802. (setq rpl
  803. (org-html-make-link
  804. opt-plist
  805. ""
  806. ""
  807. (org-solidify-link-text
  808. (save-match-data (org-link-unescape frag-0))
  809. nil)
  810. desc attr nil))))
  811. ((and (equal type "id")
  812. (setq id-file (org-id-find-id-file path)))
  813. ;; This is an id: link to another file (if it was the same file,
  814. ;; it would have become an internal link...)
  815. (save-match-data
  816. (setq id-file (file-relative-name
  817. id-file
  818. (file-name-directory org-current-export-file)))
  819. (setq rpl
  820. (org-html-make-link opt-plist
  821. "file" id-file
  822. (concat (if (org-uuidgen-p path) "ID-") path)
  823. desc
  824. attr
  825. nil))))
  826. ((member type '("http" "https"))
  827. ;; standard URL, can inline as image
  828. (setq rpl
  829. (org-html-make-link opt-plist
  830. type path nil
  831. desc
  832. attr
  833. (org-html-should-inline-p path descp))))
  834. ((member type '("ftp" "mailto" "news"))
  835. ;; standard URL, can't inline as image
  836. (setq rpl
  837. (org-html-make-link opt-plist
  838. type path nil
  839. desc
  840. attr
  841. nil)))
  842. ((string= type "coderef")
  843. (let*
  844. ((coderef-str (format "coderef-%s" path))
  845. (attr-1
  846. (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
  847. coderef-str coderef-str)))
  848. (setq rpl
  849. (org-html-make-link opt-plist
  850. type "" coderef-str
  851. (format
  852. (org-export-get-coderef-format
  853. path
  854. (and descp desc))
  855. (cdr (assoc path org-export-code-refs)))
  856. attr-1
  857. nil))))
  858. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  859. ;; The link protocol has a function for format the link
  860. (setq rpl
  861. (save-match-data
  862. (funcall fnc (org-link-unescape path) desc1 'html))))
  863. ((string= type "file")
  864. ;; FILE link
  865. (save-match-data
  866. (let*
  867. ((components
  868. (if
  869. (string-match "::\\(.*\\)" path)
  870. (list
  871. (replace-match "" t nil path)
  872. (match-string 1 path))
  873. (list path nil)))
  874. ;;The proper path, without a fragment
  875. (path-1
  876. (first components))
  877. ;;The raw fragment
  878. (fragment-0
  879. (second components))
  880. ;;Check the fragment. If it can't be used as
  881. ;;target fragment we'll pass nil instead.
  882. (fragment-1
  883. (if
  884. (and fragment-0
  885. (not (string-match "^[0-9]*$" fragment-0))
  886. (not (string-match "^\\*" fragment-0))
  887. (not (string-match "^/.*/$" fragment-0)))
  888. (org-solidify-link-text
  889. (org-link-unescape fragment-0))
  890. nil))
  891. (desc-2
  892. ;;Description minus "file:" and ".org"
  893. (if (string-match "^file:" desc)
  894. (let
  895. ((desc-1 (replace-match "" t t desc)))
  896. (if (string-match "\\.org$" desc-1)
  897. (replace-match "" t t desc-1)
  898. desc-1))
  899. desc)))
  900. (setq rpl
  901. (if
  902. (and
  903. (functionp link-validate)
  904. (not (funcall link-validate path-1 current-dir)))
  905. desc
  906. (org-html-make-link opt-plist
  907. "file" path-1 fragment-1 desc-2 attr
  908. (org-html-should-inline-p path-1 descp)))))))
  909. (t
  910. ;; just publish the path, as default
  911. (setq rpl (concat "@<i>&lt;" type ":"
  912. (save-match-data (org-link-unescape path))
  913. "&gt;@</i>"))))
  914. (setq line (replace-match rpl t t line)
  915. start (+ start (length rpl))))
  916. line))
  917. ;;; org-export-as-html
  918. ;;;###autoload
  919. (defun org-export-as-html (arg &optional hidden ext-plist
  920. to-buffer body-only pub-dir)
  921. "Export the outline as a pretty HTML file.
  922. If there is an active region, export only the region. The prefix
  923. ARG specifies how many levels of the outline should become
  924. headlines. The default is 3. Lower levels will become bulleted
  925. lists. HIDDEN is obsolete and does nothing.
  926. EXT-PLIST is a property list with external parameters overriding
  927. org-mode's default settings, but still inferior to file-local
  928. settings. When TO-BUFFER is non-nil, create a buffer with that
  929. name and export to that buffer. If TO-BUFFER is the symbol
  930. `string', don't leave any buffer behind but just return the
  931. resulting HTML as a string. When BODY-ONLY is set, don't produce
  932. the file header and footer, simply return the content of
  933. <body>...</body>, without even the body tags themselves. When
  934. PUB-DIR is set, use this as the publishing directory."
  935. (interactive "P")
  936. (run-hooks 'org-export-first-hook)
  937. ;; Make sure we have a file name when we need it.
  938. (when (and (not (or to-buffer body-only))
  939. (not buffer-file-name))
  940. (if (buffer-base-buffer)
  941. (org-set-local 'buffer-file-name
  942. (with-current-buffer (buffer-base-buffer)
  943. buffer-file-name))
  944. (error "Need a file name to be able to export")))
  945. (message "Exporting...")
  946. (setq-default org-todo-line-regexp org-todo-line-regexp)
  947. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  948. (setq-default org-done-keywords org-done-keywords)
  949. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  950. (let* ((opt-plist
  951. (org-export-process-option-filters
  952. (org-combine-plists (org-default-export-plist)
  953. ext-plist
  954. (org-infile-export-plist))))
  955. (body-only (or body-only (plist-get opt-plist :body-only)))
  956. (style (concat (if (plist-get opt-plist :style-include-default)
  957. org-export-html-style-default)
  958. (plist-get opt-plist :style)
  959. (plist-get opt-plist :style-extra)
  960. "\n"
  961. (if (plist-get opt-plist :style-include-scripts)
  962. org-export-html-scripts)))
  963. (html-extension (plist-get opt-plist :html-extension))
  964. valid thetoc have-headings first-heading-pos
  965. (odd org-odd-levels-only)
  966. (region-p (org-region-active-p))
  967. (rbeg (and region-p (region-beginning)))
  968. (rend (and region-p (region-end)))
  969. (subtree-p
  970. (if (plist-get opt-plist :ignore-subtree-p)
  971. nil
  972. (when region-p
  973. (save-excursion
  974. (goto-char rbeg)
  975. (and (org-at-heading-p)
  976. (>= (org-end-of-subtree t t) rend))))))
  977. (level-offset (if subtree-p
  978. (save-excursion
  979. (goto-char rbeg)
  980. (+ (funcall outline-level)
  981. (if org-odd-levels-only 1 0)))
  982. 0))
  983. (opt-plist (setq org-export-opt-plist
  984. (if subtree-p
  985. (org-export-add-subtree-options opt-plist rbeg)
  986. opt-plist)))
  987. ;; The following two are dynamically scoped into other
  988. ;; routines below.
  989. (org-current-export-dir
  990. (or pub-dir (org-export-directory :html opt-plist)))
  991. (org-current-export-file buffer-file-name)
  992. (level 0) (line "") (origline "") txt todo
  993. (umax nil)
  994. (umax-toc nil)
  995. (filename (if to-buffer nil
  996. (expand-file-name
  997. (concat
  998. (file-name-sans-extension
  999. (or (and subtree-p
  1000. (org-entry-get (region-beginning)
  1001. "EXPORT_FILE_NAME" t))
  1002. (file-name-nondirectory buffer-file-name)))
  1003. "." html-extension)
  1004. (file-name-as-directory
  1005. (or pub-dir (org-export-directory :html opt-plist))))))
  1006. (current-dir (if buffer-file-name
  1007. (file-name-directory buffer-file-name)
  1008. default-directory))
  1009. (buffer (if to-buffer
  1010. (cond
  1011. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  1012. (t (get-buffer-create to-buffer)))
  1013. (find-file-noselect filename)))
  1014. (org-levels-open (make-vector org-level-max nil))
  1015. (date (plist-get opt-plist :date))
  1016. (author (plist-get opt-plist :author))
  1017. (html-validation-link (or org-export-html-validation-link ""))
  1018. (title (org-html-expand
  1019. (or (and subtree-p (org-export-get-title-from-subtree))
  1020. (plist-get opt-plist :title)
  1021. (and (not body-only)
  1022. (not
  1023. (plist-get opt-plist :skip-before-1st-heading))
  1024. (org-export-grab-title-from-buffer))
  1025. (and buffer-file-name
  1026. (file-name-sans-extension
  1027. (file-name-nondirectory buffer-file-name)))
  1028. "UNTITLED")))
  1029. (link-up (and (plist-get opt-plist :link-up)
  1030. (string-match "\\S-" (plist-get opt-plist :link-up))
  1031. (plist-get opt-plist :link-up)))
  1032. (link-home (and (plist-get opt-plist :link-home)
  1033. (string-match "\\S-" (plist-get opt-plist :link-home))
  1034. (plist-get opt-plist :link-home)))
  1035. (dummy (setq opt-plist (plist-put opt-plist :title title)))
  1036. (html-table-tag (plist-get opt-plist :html-table-tag))
  1037. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  1038. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  1039. (inquote nil)
  1040. (infixed nil)
  1041. (inverse nil)
  1042. (email (plist-get opt-plist :email))
  1043. (language (plist-get opt-plist :language))
  1044. (keywords (plist-get opt-plist :keywords))
  1045. (description (plist-get opt-plist :description))
  1046. (num (plist-get opt-plist :section-numbers))
  1047. (lang-words nil)
  1048. (head-count 0) cnt
  1049. (start 0)
  1050. (coding-system (and (boundp 'buffer-file-coding-system)
  1051. buffer-file-coding-system))
  1052. (coding-system-for-write (or org-export-html-coding-system
  1053. coding-system))
  1054. (save-buffer-coding-system (or org-export-html-coding-system
  1055. coding-system))
  1056. (charset (and coding-system-for-write
  1057. (fboundp 'coding-system-get)
  1058. (coding-system-get coding-system-for-write
  1059. 'mime-charset)))
  1060. (region
  1061. (buffer-substring
  1062. (if region-p (region-beginning) (point-min))
  1063. (if region-p (region-end) (point-max))))
  1064. (org-export-have-math nil)
  1065. (lines
  1066. (org-split-string
  1067. (org-export-preprocess-string
  1068. region
  1069. :emph-multiline t
  1070. :for-backend 'html
  1071. :skip-before-1st-heading
  1072. (plist-get opt-plist :skip-before-1st-heading)
  1073. :drawers (plist-get opt-plist :drawers)
  1074. :todo-keywords (plist-get opt-plist :todo-keywords)
  1075. :tasks (plist-get opt-plist :tasks)
  1076. :done-tasks (plist-get opt-plist :done-tasks)
  1077. :tags (plist-get opt-plist :tags)
  1078. :priority (plist-get opt-plist :priority)
  1079. :footnotes (plist-get opt-plist :footnotes)
  1080. :timestamps (plist-get opt-plist :timestamps)
  1081. :archived-trees
  1082. (plist-get opt-plist :archived-trees)
  1083. :select-tags (plist-get opt-plist :select-tags)
  1084. :exclude-tags (plist-get opt-plist :exclude-tags)
  1085. :add-text
  1086. (plist-get opt-plist :text)
  1087. :LaTeX-fragments
  1088. (plist-get opt-plist :LaTeX-fragments))
  1089. "[\r\n]"))
  1090. (mathjax
  1091. (if (or (eq (plist-get opt-plist :LaTeX-fragments) 'mathjax)
  1092. (and org-export-have-math
  1093. (eq (plist-get opt-plist :LaTeX-fragments) t)))
  1094. (org-export-html-mathjax-config
  1095. org-export-html-mathjax-template
  1096. org-export-html-mathjax-options
  1097. (or (plist-get opt-plist :mathjax) ""))
  1098. ""))
  1099. table-open
  1100. table-buffer table-orig-buffer
  1101. ind
  1102. rpl path attr desc descp desc1 desc2 link
  1103. snumber fnc
  1104. footnotes footref-seen
  1105. href
  1106. )
  1107. (let ((inhibit-read-only t))
  1108. (org-unmodified
  1109. (remove-text-properties (point-min) (point-max)
  1110. '(:org-license-to-kill t))))
  1111. (message "Exporting...")
  1112. (setq org-min-level (org-get-min-level lines level-offset))
  1113. (setq org-last-level org-min-level)
  1114. (org-init-section-numbers)
  1115. (cond
  1116. ((and date (string-match "%" date))
  1117. (setq date (format-time-string date)))
  1118. (date)
  1119. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  1120. ;; Get the language-dependent settings
  1121. (setq lang-words (or (assoc language org-export-language-setup)
  1122. (assoc "en" org-export-language-setup)))
  1123. ;; Switch to the output buffer
  1124. (set-buffer buffer)
  1125. (let ((inhibit-read-only t)) (erase-buffer))
  1126. (fundamental-mode)
  1127. (org-install-letbind)
  1128. (and (fboundp 'set-buffer-file-coding-system)
  1129. (set-buffer-file-coding-system coding-system-for-write))
  1130. (let ((case-fold-search nil)
  1131. (org-odd-levels-only odd))
  1132. ;; create local variables for all options, to make sure all called
  1133. ;; functions get the correct information
  1134. (mapc (lambda (x)
  1135. (set (make-local-variable (nth 2 x))
  1136. (plist-get opt-plist (car x))))
  1137. org-export-plist-vars)
  1138. (setq umax (if arg (prefix-numeric-value arg)
  1139. org-export-headline-levels))
  1140. (setq umax-toc (if (integerp org-export-with-toc)
  1141. (min org-export-with-toc umax)
  1142. umax))
  1143. (unless body-only
  1144. ;; File header
  1145. (insert (format
  1146. "%s
  1147. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  1148. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  1149. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  1150. lang=\"%s\" xml:lang=\"%s\">
  1151. <head>
  1152. <title>%s</title>
  1153. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  1154. <meta name=\"generator\" content=\"Org-mode\"/>
  1155. <meta name=\"generated\" content=\"%s\"/>
  1156. <meta name=\"author\" content=\"%s\"/>
  1157. <meta name=\"description\" content=\"%s\"/>
  1158. <meta name=\"keywords\" content=\"%s\"/>
  1159. %s
  1160. %s
  1161. </head>
  1162. <body>
  1163. <div id=\"content\">
  1164. %s
  1165. "
  1166. (format
  1167. (or (and (stringp org-export-html-xml-declaration)
  1168. org-export-html-xml-declaration)
  1169. (cdr (assoc html-extension org-export-html-xml-declaration))
  1170. (cdr (assoc "html" org-export-html-xml-declaration))
  1171. "")
  1172. (or charset "iso-8859-1"))
  1173. language language
  1174. title
  1175. (or charset "iso-8859-1")
  1176. date author description keywords
  1177. style
  1178. mathjax
  1179. (if (or link-up link-home)
  1180. (concat
  1181. (format org-export-html-home/up-format
  1182. (or link-up link-home)
  1183. (or link-home link-up))
  1184. "\n")
  1185. "")))
  1186. ;; insert html preamble
  1187. (when (plist-get opt-plist :html-preamble)
  1188. (let ((html-pre (plist-get opt-plist :html-preamble)))
  1189. (cond ((stringp html-pre)
  1190. (insert
  1191. (format-spec html-pre `((?t . ,title) (?a . ,author)
  1192. (?d . ,date) (?e . ,email)))))
  1193. ((functionp html-pre)
  1194. (funcall html-pre opt-plist))
  1195. (t
  1196. (insert
  1197. (format-spec
  1198. (or (cadr (assoc (nth 0 lang-words)
  1199. org-export-html-preamble-format))
  1200. (cadr (assoc "en" org-export-html-preamble-format)))
  1201. `((?t . ,title) (?a . ,author)
  1202. (?d . ,date) (?e . ,email)))))))))
  1203. (if (and org-export-with-toc (not body-only))
  1204. (progn
  1205. (push (format "<h%d>%s</h%d>\n"
  1206. org-export-html-toplevel-hlevel
  1207. (nth 3 lang-words)
  1208. org-export-html-toplevel-hlevel)
  1209. thetoc)
  1210. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  1211. (push "<ul>\n<li>" thetoc)
  1212. (setq lines
  1213. (mapcar '(lambda (line)
  1214. (if (and (string-match org-todo-line-regexp line)
  1215. (not (get-text-property 0 'org-protected line)))
  1216. ;; This is a headline
  1217. (progn
  1218. (setq have-headings t)
  1219. (setq level (- (match-end 1) (match-beginning 1)
  1220. level-offset)
  1221. level (org-tr-level level)
  1222. txt (save-match-data
  1223. (org-html-expand
  1224. (org-export-cleanup-toc-line
  1225. (match-string 3 line))))
  1226. todo
  1227. (or (and org-export-mark-todo-in-toc
  1228. (match-beginning 2)
  1229. (not (member (match-string 2 line)
  1230. org-done-keywords)))
  1231. ; TODO, not DONE
  1232. (and org-export-mark-todo-in-toc
  1233. (= level umax-toc)
  1234. (org-search-todo-below
  1235. line lines level))))
  1236. (if (string-match
  1237. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  1238. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  1239. (if (string-match quote-re0 txt)
  1240. (setq txt (replace-match "" t t txt)))
  1241. (setq snumber (org-section-number level))
  1242. (if (and num (integerp num) (>= num level))
  1243. (setq txt (concat snumber " " txt)))
  1244. (if (<= level (max umax umax-toc))
  1245. (setq head-count (+ head-count 1)))
  1246. (if (<= level umax-toc)
  1247. (progn
  1248. (if (> level org-last-level)
  1249. (progn
  1250. (setq cnt (- level org-last-level))
  1251. (while (>= (setq cnt (1- cnt)) 0)
  1252. (push "\n<ul>\n<li>" thetoc))
  1253. (push "\n" thetoc)))
  1254. (if (< level org-last-level)
  1255. (progn
  1256. (setq cnt (- org-last-level level))
  1257. (while (>= (setq cnt (1- cnt)) 0)
  1258. (push "</li>\n</ul>" thetoc))
  1259. (push "\n" thetoc)))
  1260. ;; Check for targets
  1261. (while (string-match org-any-target-regexp line)
  1262. (setq line (replace-match
  1263. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  1264. t t line)))
  1265. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  1266. (setq txt (replace-match "" t t txt)))
  1267. (setq href
  1268. (replace-regexp-in-string
  1269. "\\." "_" (format "sec-%s" snumber)))
  1270. (setq href (org-solidify-link-text (or (cdr (assoc href org-export-preferred-target-alist)) href)))
  1271. (push
  1272. (format
  1273. (if todo
  1274. "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
  1275. "</li>\n<li><a href=\"#%s\">%s</a>")
  1276. href txt) thetoc)
  1277. (setq org-last-level level))
  1278. )))
  1279. line)
  1280. lines))
  1281. (while (> org-last-level (1- org-min-level))
  1282. (setq org-last-level (1- org-last-level))
  1283. (push "</li>\n</ul>\n" thetoc))
  1284. (push "</div>\n" thetoc)
  1285. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  1286. (setq head-count 0)
  1287. (org-init-section-numbers)
  1288. (org-open-par)
  1289. (while (setq line (pop lines) origline line)
  1290. (catch 'nextline
  1291. ;; end of quote section?
  1292. (when (and inquote (string-match "^\\*+ " line))
  1293. (insert "</pre>\n")
  1294. (org-open-par)
  1295. (setq inquote nil))
  1296. ;; inside a quote section?
  1297. (when inquote
  1298. (insert (org-html-protect line) "\n")
  1299. (throw 'nextline nil))
  1300. ;; Fixed-width, verbatim lines (examples)
  1301. (when (and org-export-with-fixed-width
  1302. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  1303. (when (not infixed)
  1304. (setq infixed t)
  1305. (org-close-par-maybe)
  1306. (insert "<pre class=\"example\">\n"))
  1307. (insert (org-html-protect (match-string 3 line)) "\n")
  1308. (when (or (not lines)
  1309. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  1310. (car lines))))
  1311. (setq infixed nil)
  1312. (insert "</pre>\n")
  1313. (org-open-par))
  1314. (throw 'nextline nil))
  1315. ;; Protected HTML
  1316. (when (and (get-text-property 0 'org-protected line)
  1317. ;; Make sure it is the entire line that is protected
  1318. (not (< (or (next-single-property-change
  1319. 0 'org-protected line) 10000)
  1320. (length line))))
  1321. (let (par (ind (get-text-property 0 'original-indentation line)))
  1322. (when (re-search-backward
  1323. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  1324. (setq par (match-string 1))
  1325. (replace-match "\\2\n"))
  1326. (insert line "\n")
  1327. (while (and lines
  1328. (or (= (length (car lines)) 0)
  1329. (not ind)
  1330. (equal ind (get-text-property 0 'original-indentation (car lines))))
  1331. (or (= (length (car lines)) 0)
  1332. (get-text-property 0 'org-protected (car lines))))
  1333. (insert (pop lines) "\n"))
  1334. (and par (insert "<p>\n")))
  1335. (throw 'nextline nil))
  1336. ;; Blockquotes, verse, and center
  1337. (when (equal "ORG-BLOCKQUOTE-START" line)
  1338. (org-close-par-maybe)
  1339. (insert "<blockquote>\n")
  1340. (org-open-par)
  1341. (throw 'nextline nil))
  1342. (when (equal "ORG-BLOCKQUOTE-END" line)
  1343. (org-close-par-maybe)
  1344. (insert "\n</blockquote>\n")
  1345. (org-open-par)
  1346. (throw 'nextline nil))
  1347. (when (equal "ORG-VERSE-START" line)
  1348. (org-close-par-maybe)
  1349. (insert "\n<p class=\"verse\">\n")
  1350. (setq org-par-open t)
  1351. (setq inverse t)
  1352. (throw 'nextline nil))
  1353. (when (equal "ORG-VERSE-END" line)
  1354. (insert "</p>\n")
  1355. (setq org-par-open nil)
  1356. (org-open-par)
  1357. (setq inverse nil)
  1358. (throw 'nextline nil))
  1359. (when (equal "ORG-CENTER-START" line)
  1360. (org-close-par-maybe)
  1361. (insert "\n<div style=\"text-align: center\">")
  1362. (org-open-par)
  1363. (throw 'nextline nil))
  1364. (when (equal "ORG-CENTER-END" line)
  1365. (org-close-par-maybe)
  1366. (insert "\n</div>")
  1367. (org-open-par)
  1368. (throw 'nextline nil))
  1369. (run-hooks 'org-export-html-after-blockquotes-hook)
  1370. (when inverse
  1371. (let ((i (org-get-string-indentation line)))
  1372. (if (> i 0)
  1373. (setq line (concat (mapconcat 'identity
  1374. (make-list (* 2 i) "\\nbsp") "")
  1375. " " (org-trim line))))
  1376. (unless (string-match "\\\\\\\\[ \t]*$" line)
  1377. (setq line (concat line "\\\\")))))
  1378. ;; make targets to anchors
  1379. (setq start 0)
  1380. (while (string-match
  1381. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  1382. (cond
  1383. ((get-text-property (match-beginning 1) 'org-protected line)
  1384. (setq start (match-end 1)))
  1385. ((match-end 2)
  1386. (setq line (replace-match
  1387. (format
  1388. "@<a name=\"%s\" id=\"%s\">@</a>"
  1389. (org-solidify-link-text (match-string 1 line))
  1390. (org-solidify-link-text (match-string 1 line)))
  1391. t t line)))
  1392. ((and org-export-with-toc (equal (string-to-char line) ?*))
  1393. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  1394. (setq line (replace-match
  1395. (concat "@<span class=\"target\">"
  1396. (match-string 1 line) "@</span> ")
  1397. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  1398. t t line)))
  1399. (t
  1400. (setq line (replace-match
  1401. (concat "@<a name=\""
  1402. (org-solidify-link-text (match-string 1 line))
  1403. "\" class=\"target\">" (match-string 1 line)
  1404. "@</a> ")
  1405. t t line)))))
  1406. (setq line (org-html-handle-time-stamps line))
  1407. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  1408. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  1409. ;; Also handle sub_superscripts and checkboxes
  1410. (or (string-match org-table-hline-regexp line)
  1411. (setq line (org-html-expand line)))
  1412. ;; Format the links
  1413. (setq line (org-html-handle-links line opt-plist))
  1414. ;; TODO items
  1415. (if (and (string-match org-todo-line-regexp line)
  1416. (match-beginning 2))
  1417. (setq line
  1418. (concat (substring line 0 (match-beginning 2))
  1419. "<span class=\""
  1420. (if (member (match-string 2 line)
  1421. org-done-keywords)
  1422. "done" "todo")
  1423. " " (match-string 2 line)
  1424. "\"> " (org-export-html-get-todo-kwd-class-name
  1425. (match-string 2 line))
  1426. "</span>" (substring line (match-end 2)))))
  1427. ;; Does this contain a reference to a footnote?
  1428. (when org-export-with-footnotes
  1429. (setq start 0)
  1430. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  1431. (if (get-text-property (match-beginning 2) 'org-protected line)
  1432. (setq start (match-end 2))
  1433. (let ((n (match-string 2 line)) extra a)
  1434. (if (setq a (assoc n footref-seen))
  1435. (progn
  1436. (setcdr a (1+ (cdr a)))
  1437. (setq extra (format ".%d" (cdr a))))
  1438. (setq extra "")
  1439. (push (cons n 1) footref-seen))
  1440. (setq line
  1441. (replace-match
  1442. (format
  1443. (concat "%s"
  1444. (format org-export-html-footnote-format
  1445. "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
  1446. (or (match-string 1 line) "") n extra n n)
  1447. t t line))))))
  1448. (cond
  1449. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1450. ;; This is a headline
  1451. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1452. level-offset))
  1453. txt (match-string 2 line))
  1454. (if (string-match quote-re0 txt)
  1455. (setq txt (replace-match "" t t txt)))
  1456. (if (<= level (max umax umax-toc))
  1457. (setq head-count (+ head-count 1)))
  1458. (setq first-heading-pos (or first-heading-pos (point)))
  1459. (org-html-level-start level txt umax
  1460. (and org-export-with-toc (<= level umax))
  1461. head-count opt-plist)
  1462. ;; QUOTES
  1463. (when (string-match quote-re line)
  1464. (org-close-par-maybe)
  1465. (insert "<pre>")
  1466. (setq inquote t)))
  1467. ((and org-export-with-tables
  1468. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1469. (when (not table-open)
  1470. ;; New table starts
  1471. (setq table-open t table-buffer nil table-orig-buffer nil))
  1472. ;; Accumulate lines
  1473. (setq table-buffer (cons line table-buffer)
  1474. table-orig-buffer (cons origline table-orig-buffer))
  1475. (when (or (not lines)
  1476. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1477. (car lines))))
  1478. (setq table-open nil
  1479. table-buffer (nreverse table-buffer)
  1480. table-orig-buffer (nreverse table-orig-buffer))
  1481. (org-close-par-maybe)
  1482. (insert (org-format-table-html table-buffer table-orig-buffer))))
  1483. ;; Normal lines
  1484. (t
  1485. ;; This line either is list item or end a list.
  1486. (when (get-text-property 0 'list-item line)
  1487. (setq line (org-html-export-list-line
  1488. line
  1489. (get-text-property 0 'list-item line)
  1490. (get-text-property 0 'list-struct line)
  1491. (get-text-property 0 'list-prevs line))))
  1492. ;; Horizontal line
  1493. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1494. (if org-par-open
  1495. (insert "\n</p>\n<hr/>\n<p>\n")
  1496. (insert "\n<hr/>\n"))
  1497. (throw 'nextline nil))
  1498. ;; Empty lines start a new paragraph. If hand-formatted lists
  1499. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1500. ;; also start a new paragraph.
  1501. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  1502. ;; Is this the start of a footnote?
  1503. (when org-export-with-footnotes
  1504. (when (and (boundp 'footnote-section-tag-regexp)
  1505. (string-match (concat "^" footnote-section-tag-regexp)
  1506. line))
  1507. ;; ignore this line
  1508. (throw 'nextline nil))
  1509. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1510. (org-close-par-maybe)
  1511. (let ((n (match-string 1 line)))
  1512. (setq org-par-open t
  1513. line (replace-match
  1514. (format
  1515. (concat "<p class=\"footnote\">"
  1516. (format org-export-html-footnote-format
  1517. "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
  1518. n n n) t t line)))))
  1519. ;; Check if the line break needs to be conserved
  1520. (cond
  1521. ((string-match "\\\\\\\\[ \t]*$" line)
  1522. (setq line (replace-match "<br/>" t t line)))
  1523. (org-export-preserve-breaks
  1524. (setq line (concat line "<br/>"))))
  1525. ;; Check if a paragraph should be started
  1526. (let ((start 0))
  1527. (while (and org-par-open
  1528. (string-match "\\\\par\\>" line start))
  1529. ;; Leave a space in the </p> so that the footnote matcher
  1530. ;; does not see this.
  1531. (if (not (get-text-property (match-beginning 0)
  1532. 'org-protected line))
  1533. (setq line (replace-match "</p ><p >" t t line)))
  1534. (setq start (match-end 0))))
  1535. (insert line "\n")))))
  1536. ;; Properly close all local lists and other lists
  1537. (when inquote
  1538. (insert "</pre>\n")
  1539. (org-open-par))
  1540. (org-html-level-start 1 nil umax
  1541. (and org-export-with-toc (<= level umax))
  1542. head-count opt-plist)
  1543. ;; the </div> to close the last text-... div.
  1544. (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
  1545. (save-excursion
  1546. (goto-char (point-min))
  1547. (while (re-search-forward
  1548. "\\(\\(<p class=\"footnote\">\\)[^\000]*?\\)\\(\\(\\2\\)\\|\\'\\)"
  1549. nil t)
  1550. (push (match-string 1) footnotes)
  1551. (replace-match "\\4" t nil)
  1552. (goto-char (match-beginning 0))))
  1553. (when footnotes
  1554. (insert (format org-export-html-footnotes-section
  1555. (nth 4 lang-words)
  1556. (mapconcat 'identity (nreverse footnotes) "\n"))
  1557. "\n"))
  1558. (let ((bib (org-export-html-get-bibliography)))
  1559. (when bib
  1560. (insert "\n" bib "\n")))
  1561. ;; export html postamble
  1562. (unless body-only
  1563. (let ((html-post (plist-get opt-plist :html-postamble))
  1564. (email
  1565. (mapconcat (lambda(e)
  1566. (format "<a href=\"mailto:%s\">%s</a>" e e))
  1567. (split-string email ",+ *")
  1568. ", "))
  1569. (creator-info
  1570. (concat "Org version " org-version " with Emacs version "
  1571. (number-to-string emacs-major-version))))
  1572. (when (plist-get opt-plist :html-postamble)
  1573. (cond ((stringp html-post)
  1574. (insert "<div id=\"postamble\">\n")
  1575. (insert (format-spec html-post
  1576. `((?a . ,author) (?e . ,email)
  1577. (?d . ,date) (?c . ,creator-info)
  1578. (?v . ,html-validation-link))))
  1579. (insert "</div>"))
  1580. ((functionp html-post)
  1581. (funcall html-post opt-plist))
  1582. ((eq html-post 'auto)
  1583. ;; fall back on default postamble
  1584. (insert "<div id=\"postamble\">\n")
  1585. (when (plist-get opt-plist :time-stamp-file)
  1586. (insert "<p class=\"date\">" (nth 2 lang-words) ": " date "</p>\n"))
  1587. (when (and (plist-get opt-plist :author-info) author)
  1588. (insert "<p class=\"author\">" (nth 1 lang-words) ": " author "</p>\n"))
  1589. (when (and (plist-get opt-plist :email-info) email)
  1590. (insert "<p class=\"email\">" email "</p>\n"))
  1591. (when (plist-get opt-plist :creator-info)
  1592. (insert "<p class=\"creator\">"
  1593. (concat "Org version " org-version " with Emacs version "
  1594. (number-to-string emacs-major-version) "</p>\n")))
  1595. (insert html-validation-link "\n</div>"))
  1596. (t
  1597. (insert "<div id=\"postamble\">\n")
  1598. (insert (format-spec
  1599. (or (cadr (assoc (nth 0 lang-words)
  1600. org-export-html-postamble-format))
  1601. (cadr (assoc "en" org-export-html-postamble-format)))
  1602. `((?a . ,author) (?e . ,email)
  1603. (?d . ,date) (?c . ,creator-info)
  1604. (?v . ,html-validation-link))))
  1605. (insert "</div>"))))))
  1606. (if org-export-html-with-timestamp
  1607. (insert org-export-html-html-helper-timestamp))
  1608. (unless body-only (insert "\n</div>\n</body>\n</html>\n"))
  1609. (unless (plist-get opt-plist :buffer-will-be-killed)
  1610. (normal-mode)
  1611. (if (eq major-mode (default-value 'major-mode))
  1612. (html-mode)))
  1613. ;; insert the table of contents
  1614. (goto-char (point-min))
  1615. (when thetoc
  1616. (if (or (re-search-forward
  1617. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1618. (re-search-forward
  1619. "\\[TABLE-OF-CONTENTS\\]" nil t))
  1620. (progn
  1621. (goto-char (match-beginning 0))
  1622. (replace-match ""))
  1623. (goto-char first-heading-pos)
  1624. (when (looking-at "\\s-*</p>")
  1625. (goto-char (match-end 0))
  1626. (insert "\n")))
  1627. (insert "<div id=\"table-of-contents\">\n")
  1628. (let ((beg (point)))
  1629. (mapc 'insert thetoc)
  1630. (insert "</div>\n")
  1631. (while (re-search-backward "<li>[ \r\n\t]*</li>\n?" beg t)
  1632. (replace-match ""))))
  1633. ;; remove empty paragraphs
  1634. (goto-char (point-min))
  1635. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  1636. (replace-match ""))
  1637. (goto-char (point-min))
  1638. ;; Convert whitespace place holders
  1639. (goto-char (point-min))
  1640. (let (beg end n)
  1641. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  1642. (setq n (get-text-property beg 'org-whitespace)
  1643. end (next-single-property-change beg 'org-whitespace))
  1644. (goto-char beg)
  1645. (delete-region beg end)
  1646. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  1647. (make-string n ?x)))))
  1648. ;; Remove empty lines at the beginning of the file.
  1649. (goto-char (point-min))
  1650. (when (looking-at "\\s-+\n") (replace-match ""))
  1651. ;; Remove display properties
  1652. (remove-text-properties (point-min) (point-max) '(display t))
  1653. ;; Run the hook
  1654. (run-hooks 'org-export-html-final-hook)
  1655. (or to-buffer (save-buffer))
  1656. (goto-char (point-min))
  1657. (or (org-export-push-to-kill-ring "HTML")
  1658. (message "Exporting... done"))
  1659. (if (eq to-buffer 'string)
  1660. (prog1 (buffer-substring (point-min) (point-max))
  1661. (kill-buffer (current-buffer)))
  1662. (current-buffer)))))
  1663. (defun org-export-html-format-href (s)
  1664. "Make sure the S is valid as a href reference in an XHTML document."
  1665. (save-match-data
  1666. (let ((start 0))
  1667. (while (string-match "&" s start)
  1668. (setq start (+ (match-beginning 0) 3)
  1669. s (replace-match "&amp;" t t s)))))
  1670. s)
  1671. (defun org-export-html-format-desc (s)
  1672. "Make sure the S is valid as a description in a link."
  1673. (if (and s (not (get-text-property 1 'org-protected s)))
  1674. (save-match-data
  1675. (org-html-do-expand s))
  1676. s))
  1677. (defun org-export-html-format-image (src par-open)
  1678. "Create image tag with source and attributes."
  1679. (save-match-data
  1680. (if (string-match "^ltxpng/" src)
  1681. (format "<img src=\"%s\" alt=\"%s\"/>"
  1682. src (org-find-text-property-in-string 'org-latex-src src))
  1683. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1684. (attr (org-find-text-property-in-string 'org-attributes src))
  1685. (label (org-find-text-property-in-string 'org-label src)))
  1686. (setq caption (and caption (org-html-do-expand caption)))
  1687. (concat
  1688. (if caption
  1689. (format "%s<div %sclass=\"figure\">
  1690. <p>"
  1691. (if org-par-open "</p>\n" "")
  1692. (if label (format "id=\"%s\" " (org-solidify-link-text label)) "")))
  1693. (format "<img src=\"%s\"%s />"
  1694. src
  1695. (if (string-match "\\<alt=" (or attr ""))
  1696. (concat " " attr )
  1697. (concat " " attr " alt=\"" src "\"")))
  1698. (if caption
  1699. (format "</p>%s
  1700. </div>%s"
  1701. (concat "\n<p>" caption "</p>")
  1702. (if org-par-open "\n<p>" ""))))))))
  1703. (defun org-export-html-get-bibliography ()
  1704. "Find bibliography, cut it out and return it."
  1705. (catch 'exit
  1706. (let (beg end (cnt 1) bib)
  1707. (save-excursion
  1708. (goto-char (point-min))
  1709. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  1710. (setq beg (match-beginning 0))
  1711. (while (re-search-forward "</?div\\>" nil t)
  1712. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  1713. (when (= cnt 0)
  1714. (and (looking-at ">") (forward-char 1))
  1715. (setq bib (buffer-substring beg (point)))
  1716. (delete-region beg (point))
  1717. (throw 'exit bib))))
  1718. nil))))
  1719. (defvar org-table-number-regexp) ; defined in org-table.el
  1720. (defun org-format-table-html (lines olines &optional no-css)
  1721. "Find out which HTML converter to use and return the HTML code.
  1722. NO-CSS is passed to the exporter."
  1723. (if (stringp lines)
  1724. (setq lines (org-split-string lines "\n")))
  1725. (if (string-match "^[ \t]*|" (car lines))
  1726. ;; A normal org table
  1727. (org-format-org-table-html lines nil no-css)
  1728. ;; Table made by table.el - test for spanning
  1729. (let* ((hlines (delq nil (mapcar
  1730. (lambda (x)
  1731. (if (string-match "^[ \t]*\\+-" x) x
  1732. nil))
  1733. lines)))
  1734. (first (car hlines))
  1735. (ll (and (string-match "\\S-+" first)
  1736. (match-string 0 first)))
  1737. (re (concat "^[ \t]*" (regexp-quote ll)))
  1738. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  1739. hlines))))
  1740. (if (and (not spanning)
  1741. (not org-export-prefer-native-exporter-for-tables))
  1742. ;; We can use my own converter with HTML conversions
  1743. (org-format-table-table-html lines)
  1744. ;; Need to use the code generator in table.el, with the original text.
  1745. (org-format-table-table-html-using-table-generate-source olines)))))
  1746. (defvar org-table-number-fraction) ; defined in org-table.el
  1747. (defun org-format-org-table-html (lines &optional splice no-css)
  1748. "Format a table into HTML.
  1749. LINES is a list of lines. Optional argument SPLICE means, do not
  1750. insert header and surrounding <table> tags, just format the lines.
  1751. Optional argument NO-CSS means use XHTML attributes instead of CSS
  1752. for formatting. This is required for the DocBook exporter."
  1753. (require 'org-table)
  1754. ;; Get rid of hlines at beginning and end
  1755. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1756. (setq lines (nreverse lines))
  1757. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1758. (setq lines (nreverse lines))
  1759. (when org-export-table-remove-special-lines
  1760. ;; Check if the table has a marking column. If yes remove the
  1761. ;; column and the special lines
  1762. (setq lines (org-table-clean-before-export lines)))
  1763. (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
  1764. (label (org-find-text-property-in-string 'org-label (car lines)))
  1765. (forced-aligns (org-find-text-property-in-string 'org-forced-aligns
  1766. (car lines)))
  1767. (attributes (org-find-text-property-in-string 'org-attributes
  1768. (car lines)))
  1769. (html-table-tag (org-export-splice-attributes
  1770. html-table-tag attributes))
  1771. (head (and org-export-highlight-first-table-line
  1772. (delq nil (mapcar
  1773. (lambda (x) (string-match "^[ \t]*|-" x))
  1774. (cdr lines)))))
  1775. (nline 0) fnum nfields i (cnt 0)
  1776. tbopen line fields html gr colgropen rowstart rowend
  1777. ali align aligns n)
  1778. (setq caption (and caption (org-html-do-expand caption)))
  1779. (when (and forced-aligns org-table-clean-did-remove-column)
  1780. (setq forced-aligns
  1781. (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
  1782. (if splice (setq head nil))
  1783. (unless splice (push (if head "<thead>" "<tbody>") html))
  1784. (setq tbopen t)
  1785. (while (setq line (pop lines))
  1786. (catch 'next-line
  1787. (if (string-match "^[ \t]*|-" line)
  1788. (progn
  1789. (unless splice
  1790. (push (if head "</thead>" "</tbody>") html)
  1791. (if lines (push "<tbody>" html) (setq tbopen nil)))
  1792. (setq head nil) ;; head ends here, first time around
  1793. ;; ignore this line
  1794. (throw 'next-line t)))
  1795. ;; Break the line into fields
  1796. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1797. (unless fnum (setq fnum (make-vector (length fields) 0)
  1798. nfields (length fnum)))
  1799. (setq nline (1+ nline) i -1
  1800. rowstart (eval (car org-export-table-row-tags))
  1801. rowend (eval (cdr org-export-table-row-tags)))
  1802. (push (concat rowstart
  1803. (mapconcat
  1804. (lambda (x)
  1805. (setq i (1+ i) ali (format "@@class%03d@@" i))
  1806. (if (and (< i nfields) ; make sure no rogue line causes an error here
  1807. (string-match org-table-number-regexp x))
  1808. (incf (aref fnum i)))
  1809. (cond
  1810. (head
  1811. (concat
  1812. (format (car org-export-table-header-tags)
  1813. "col" ali)
  1814. x
  1815. (cdr org-export-table-header-tags)))
  1816. ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
  1817. (concat
  1818. (format (car org-export-table-header-tags)
  1819. "row" ali)
  1820. x
  1821. (cdr org-export-table-header-tags)))
  1822. (t
  1823. (concat (format (car org-export-table-data-tags) ali)
  1824. x
  1825. (cdr org-export-table-data-tags)))))
  1826. fields "")
  1827. rowend)
  1828. html)))
  1829. (unless splice (if tbopen (push "</tbody>" html)))
  1830. (unless splice (push "</table>\n" html))
  1831. (setq html (nreverse html))
  1832. (unless splice
  1833. ;; Put in col tags with the alignment (unfortunately often ignored...)
  1834. (unless (car org-table-colgroup-info)
  1835. (setq org-table-colgroup-info
  1836. (cons :start (cdr org-table-colgroup-info))))
  1837. (setq i 0)
  1838. (push (mapconcat
  1839. (lambda (x)
  1840. (setq gr (pop org-table-colgroup-info)
  1841. i (1+ i)
  1842. align (if (assoc i forced-aligns)
  1843. (cdr (assoc (cdr (assoc i forced-aligns))
  1844. '(("l" . "left") ("r" . "right")
  1845. ("c" . "center"))))
  1846. (if (> (/ (float x) nline)
  1847. org-table-number-fraction)
  1848. "right" "left")))
  1849. (push align aligns)
  1850. (format (if no-css
  1851. "%s<col align=\"%s\" />%s"
  1852. "%s<col class=\"%s\" />%s")
  1853. (if (memq gr '(:start :startend))
  1854. (prog1
  1855. (if colgropen
  1856. "</colgroup>\n<colgroup>"
  1857. "<colgroup>")
  1858. (setq colgropen t))
  1859. "")
  1860. align
  1861. (if (memq gr '(:end :startend))
  1862. (progn (setq colgropen nil) "</colgroup>")
  1863. "")))
  1864. fnum "")
  1865. html)
  1866. (setq aligns (nreverse aligns))
  1867. (if colgropen (setq html (cons (car html)
  1868. (cons "</colgroup>" (cdr html)))))
  1869. ;; Since the output of HTML table formatter can also be used in
  1870. ;; DocBook document, we want to always include the caption to make
  1871. ;; DocBook XML file valid.
  1872. (push (format "<caption>%s</caption>" (or caption "")) html)
  1873. (when label
  1874. (setq html-table-tag (org-export-splice-attributes html-table-tag (format "id=\"%s\"" (org-solidify-link-text label)))))
  1875. (push html-table-tag html))
  1876. (setq html (mapcar
  1877. (lambda (x)
  1878. (replace-regexp-in-string
  1879. "@@class\\([0-9]+\\)@@"
  1880. (lambda (txt)
  1881. (if (not org-export-html-table-align-individual-fields)
  1882. ""
  1883. (setq n (string-to-number (match-string 1 txt)))
  1884. (format (if no-css " align=\"%s\"" " class=\"%s\"")
  1885. (or (nth n aligns) "left"))))
  1886. x))
  1887. html))
  1888. (concat (mapconcat 'identity html "\n") "\n")))
  1889. (defun org-export-splice-attributes (tag attributes)
  1890. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  1891. (if (not attributes)
  1892. tag
  1893. (let (oldatt newatt)
  1894. (setq oldatt (org-extract-attributes-from-string tag)
  1895. tag (pop oldatt)
  1896. newatt (cdr (org-extract-attributes-from-string attributes)))
  1897. (while newatt
  1898. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  1899. (if (string-match ">" tag)
  1900. (setq tag
  1901. (replace-match (concat (org-attributes-to-string oldatt) ">")
  1902. t t tag)))
  1903. tag)))
  1904. (defun org-format-table-table-html (lines)
  1905. "Format a table generated by table.el into HTML.
  1906. This conversion does *not* use `table-generate-source' from table.el.
  1907. This has the advantage that Org-mode's HTML conversions can be used.
  1908. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1909. (let (line field-buffer
  1910. (head org-export-highlight-first-table-line)
  1911. fields html empty i)
  1912. (setq html (concat html-table-tag "\n"))
  1913. (while (setq line (pop lines))
  1914. (setq empty "&nbsp;")
  1915. (catch 'next-line
  1916. (if (string-match "^[ \t]*\\+-" line)
  1917. (progn
  1918. (if field-buffer
  1919. (progn
  1920. (setq
  1921. html
  1922. (concat
  1923. html
  1924. "<tr>"
  1925. (mapconcat
  1926. (lambda (x)
  1927. (if (equal x "") (setq x empty))
  1928. (if head
  1929. (concat
  1930. (format (car org-export-table-header-tags) "col" "")
  1931. x
  1932. (cdr org-export-table-header-tags))
  1933. (concat (format (car org-export-table-data-tags) "") x
  1934. (cdr org-export-table-data-tags))))
  1935. field-buffer "\n")
  1936. "</tr>\n"))
  1937. (setq head nil)
  1938. (setq field-buffer nil)))
  1939. ;; Ignore this line
  1940. (throw 'next-line t)))
  1941. ;; Break the line into fields and store the fields
  1942. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1943. (if field-buffer
  1944. (setq field-buffer (mapcar
  1945. (lambda (x)
  1946. (concat x "<br/>" (pop fields)))
  1947. field-buffer))
  1948. (setq field-buffer fields))))
  1949. (setq html (concat html "</table>\n"))
  1950. html))
  1951. (defun org-format-table-table-html-using-table-generate-source (lines)
  1952. "Format a table into html, using `table-generate-source' from table.el.
  1953. This has the advantage that cell- or row-spanning is allowed.
  1954. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  1955. (require 'table)
  1956. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1957. (erase-buffer)
  1958. (insert (mapconcat 'identity lines "\n"))
  1959. (goto-char (point-min))
  1960. (if (not (re-search-forward "|[^+]" nil t))
  1961. (error "Error processing table"))
  1962. (table-recognize-table)
  1963. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1964. (table-generate-source 'html " org-tmp2 ")
  1965. (set-buffer " org-tmp2 ")
  1966. (buffer-substring (point-min) (point-max))))
  1967. (defun org-export-splice-style (style extra)
  1968. "Splice EXTRA into STYLE, just before \"</style>\"."
  1969. (if (and (stringp extra)
  1970. (string-match "\\S-" extra)
  1971. (string-match "</style>" style))
  1972. (concat (substring style 0 (match-beginning 0))
  1973. "\n" extra "\n"
  1974. (substring style (match-beginning 0)))
  1975. style))
  1976. (defun org-html-handle-time-stamps (s)
  1977. "Format time stamps in string S, or remove them."
  1978. (catch 'exit
  1979. (let (r b)
  1980. (while (string-match org-maybe-keyword-time-regexp s)
  1981. (or b (setq b (substring s 0 (match-beginning 0))))
  1982. (setq r (concat
  1983. r (substring s 0 (match-beginning 0))
  1984. " @<span class=\"timestamp-wrapper\">"
  1985. (if (match-end 1)
  1986. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  1987. (match-string 1 s)))
  1988. (format " @<span class=\"timestamp\">%s@</span>"
  1989. (substring
  1990. (org-translate-time (match-string 3 s)) 1 -1))
  1991. "@</span>")
  1992. s (substring s (match-end 0))))
  1993. ;; Line break if line started and ended with time stamp stuff
  1994. (if (not r)
  1995. s
  1996. (setq r (concat r s))
  1997. (unless (string-match "\\S-" (concat b s))
  1998. (setq r (concat r "@<br/>")))
  1999. r))))
  2000. (defvar htmlize-buffer-places) ; from htmlize.el
  2001. (defun org-export-htmlize-region-for-paste (beg end)
  2002. "Convert the region to HTML, using htmlize.el.
  2003. This is much like `htmlize-region-for-paste', only that it uses
  2004. the settings define in the org-... variables."
  2005. (let* ((htmlize-output-type org-export-htmlize-output-type)
  2006. (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
  2007. (htmlbuf (htmlize-region beg end)))
  2008. (unwind-protect
  2009. (with-current-buffer htmlbuf
  2010. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  2011. (plist-get htmlize-buffer-places 'content-end)))
  2012. (kill-buffer htmlbuf))))
  2013. ;;;###autoload
  2014. (defun org-export-htmlize-generate-css ()
  2015. "Create the CSS for all font definitions in the current Emacs session.
  2016. Use this to create face definitions in your CSS style file that can then
  2017. be used by code snippets transformed by htmlize.
  2018. This command just produces a buffer that contains class definitions for all
  2019. faces used in the current Emacs session. You can copy and paste the ones you
  2020. need into your CSS file.
  2021. If you then set `org-export-htmlize-output-type' to `css', calls to
  2022. the function `org-export-htmlize-region-for-paste' will produce code
  2023. that uses these same face definitions."
  2024. (interactive)
  2025. (require 'htmlize)
  2026. (and (get-buffer "*html*") (kill-buffer "*html*"))
  2027. (with-temp-buffer
  2028. (let ((fl (face-list))
  2029. (htmlize-css-name-prefix "org-")
  2030. (htmlize-output-type 'css)
  2031. f i)
  2032. (while (setq f (pop fl)
  2033. i (and f (face-attribute f :inherit)))
  2034. (when (and (symbolp f) (or (not i) (not (listp i))))
  2035. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  2036. (htmlize-region (point-min) (point-max))))
  2037. (switch-to-buffer "*html*")
  2038. (goto-char (point-min))
  2039. (if (re-search-forward "<style" nil t)
  2040. (delete-region (point-min) (match-beginning 0)))
  2041. (if (re-search-forward "</style>" nil t)
  2042. (delete-region (1+ (match-end 0)) (point-max)))
  2043. (beginning-of-line 1)
  2044. (if (looking-at " +") (replace-match ""))
  2045. (goto-char (point-min)))
  2046. (defun org-html-protect (s)
  2047. "Convert characters to HTML equivalent.
  2048. Possible conversions are set in `org-export-html-protect-char-alist'."
  2049. (let ((cl org-export-html-protect-char-alist) c)
  2050. (while (setq c (pop cl))
  2051. (let ((start 0))
  2052. (while (string-match (car c) s start)
  2053. (setq s (replace-match (cdr c) t t s)
  2054. start (1+ (match-beginning 0))))))
  2055. s))
  2056. (defun org-html-expand (string)
  2057. "Prepare STRING for HTML export. Apply all active conversions.
  2058. If there are links in the string, don't modify these."
  2059. (let* ((re (concat org-bracket-link-regexp "\\|"
  2060. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
  2061. m s l res)
  2062. (if (string-match "^[ \t]*\\+-[-+]*\\+[ \t]*$" string)
  2063. string
  2064. (while (setq m (string-match re string))
  2065. (setq s (substring string 0 m)
  2066. l (match-string 0 string)
  2067. string (substring string (match-end 0)))
  2068. (push (org-html-do-expand s) res)
  2069. (push l res))
  2070. (push (org-html-do-expand string) res)
  2071. (apply 'concat (nreverse res)))))
  2072. (defun org-html-do-expand (s)
  2073. "Apply all active conversions to translate special ASCII to HTML."
  2074. (setq s (org-html-protect s))
  2075. (if org-export-html-expand
  2076. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  2077. (setq s (replace-match "<\\1>" t nil s))))
  2078. (if org-export-with-emphasize
  2079. (setq s (org-export-html-convert-emphasize s)))
  2080. (if org-export-with-special-strings
  2081. (setq s (org-export-html-convert-special-strings s)))
  2082. (if org-export-with-sub-superscripts
  2083. (setq s (org-export-html-convert-sub-super s)))
  2084. (if org-export-with-TeX-macros
  2085. (let ((start 0) wd rep)
  2086. (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
  2087. s start))
  2088. (if (get-text-property (match-beginning 0) 'org-protected s)
  2089. (setq start (match-end 0))
  2090. (setq wd (match-string 1 s))
  2091. (if (setq rep (org-entity-get-representation wd 'html))
  2092. (setq s (replace-match rep t t s))
  2093. (setq start (+ start (length wd))))))))
  2094. s)
  2095. (defun org-export-html-convert-special-strings (string)
  2096. "Convert special characters in STRING to HTML."
  2097. (let ((all org-export-html-special-string-regexps)
  2098. e a re rpl start)
  2099. (while (setq a (pop all))
  2100. (setq re (car a) rpl (cdr a) start 0)
  2101. (while (string-match re string start)
  2102. (if (get-text-property (match-beginning 0) 'org-protected string)
  2103. (setq start (match-end 0))
  2104. (setq string (replace-match rpl t nil string)))))
  2105. string))
  2106. (defun org-export-html-convert-sub-super (string)
  2107. "Convert sub- and superscripts in STRING to HTML."
  2108. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  2109. (while (string-match org-match-substring-regexp string s)
  2110. (cond
  2111. ((and requireb (match-end 8)) (setq s (match-end 2)))
  2112. ((get-text-property (match-beginning 2) 'org-protected string)
  2113. (setq s (match-end 2)))
  2114. (t
  2115. (setq s (match-end 1)
  2116. key (if (string= (match-string 2 string) "_") "sub" "sup")
  2117. c (or (match-string 8 string)
  2118. (match-string 6 string)
  2119. (match-string 5 string))
  2120. string (replace-match
  2121. (concat (match-string 1 string)
  2122. "<" key ">" c "</" key ">")
  2123. t t string)))))
  2124. (while (string-match "\\\\\\([_^]\\)" string)
  2125. (setq string (replace-match (match-string 1 string) t t string)))
  2126. string))
  2127. (defun org-export-html-convert-emphasize (string)
  2128. "Apply emphasis."
  2129. (let ((s 0) rpl)
  2130. (while (string-match org-emph-re string s)
  2131. (if (not (equal
  2132. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  2133. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  2134. (setq s (match-beginning 0)
  2135. rpl
  2136. (concat
  2137. (match-string 1 string)
  2138. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  2139. (match-string 4 string)
  2140. (nth 3 (assoc (match-string 3 string)
  2141. org-emphasis-alist))
  2142. (match-string 5 string))
  2143. string (replace-match rpl t t string)
  2144. s (+ s (- (length rpl) 2)))
  2145. (setq s (1+ s))))
  2146. string))
  2147. (defun org-open-par ()
  2148. "Insert <p>, but first close previous paragraph if any."
  2149. (org-close-par-maybe)
  2150. (insert "\n<p>")
  2151. (setq org-par-open t))
  2152. (defun org-close-par-maybe ()
  2153. "Close paragraph if there is one open."
  2154. (when org-par-open
  2155. (insert "</p>")
  2156. (setq org-par-open nil)))
  2157. (defun org-close-li (&optional type)
  2158. "Close <li> if necessary."
  2159. (org-close-par-maybe)
  2160. (insert (if (equal type "d") "</dd>\n" "</li>\n")))
  2161. (defvar body-only) ; dynamically scoped into this.
  2162. (defun org-html-level-start (level title umax with-toc head-count &optional opt-plist)
  2163. "Insert a new level in HTML export.
  2164. When TITLE is nil, just close all open levels."
  2165. (org-close-par-maybe)
  2166. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  2167. (extra-targets (and target
  2168. (assoc target org-export-target-aliases)))
  2169. (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
  2170. (preferred (and target
  2171. (cdr (assoc target org-export-preferred-target-alist))))
  2172. (l org-level-max)
  2173. (num (plist-get opt-plist :section-numbers))
  2174. snumber snu href suffix)
  2175. (setq extra-targets (remove (or preferred target) extra-targets))
  2176. (setq extra-targets
  2177. (mapconcat (lambda (x)
  2178. (setq x (org-solidify-link-text
  2179. (if (org-uuidgen-p x) (concat "ID-" x) x)))
  2180. (format "<a name=\"%s\" id=\"%s\"></a>"
  2181. x x))
  2182. extra-targets
  2183. ""))
  2184. (while (>= l level)
  2185. (if (aref org-levels-open (1- l))
  2186. (progn
  2187. (org-html-level-close l umax)
  2188. (aset org-levels-open (1- l) nil)))
  2189. (setq l (1- l)))
  2190. (when title
  2191. ;; If title is nil, this means this function is called to close
  2192. ;; all levels, so the rest is done only if title is given
  2193. (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  2194. (setq title (replace-match
  2195. (if org-export-with-tags
  2196. (save-match-data
  2197. (concat
  2198. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  2199. (mapconcat
  2200. (lambda (x)
  2201. (format "<span class=\"%s\">%s</span>"
  2202. (org-export-html-get-tag-class-name x)
  2203. x))
  2204. (org-split-string (match-string 1 title) ":")
  2205. "&nbsp;")
  2206. "</span>"))
  2207. "")
  2208. t t title)))
  2209. (if (> level umax)
  2210. (progn
  2211. (if (aref org-levels-open (1- level))
  2212. (progn
  2213. (org-close-li)
  2214. (if target
  2215. (insert (format "<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
  2216. extra-targets title "<br/>\n")
  2217. (insert "<li>" title "<br/>\n")))
  2218. (aset org-levels-open (1- level) t)
  2219. (org-close-par-maybe)
  2220. (if target
  2221. (insert (format "<ul>\n<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
  2222. extra-targets title "<br/>\n")
  2223. (insert "<ul>\n<li>" title "<br/>\n"))))
  2224. (aset org-levels-open (1- level) t)
  2225. (setq snumber (org-section-number level)
  2226. snu (replace-regexp-in-string "\\." "_" snumber))
  2227. (setq level (+ level org-export-html-toplevel-hlevel -1))
  2228. (if (and num (not body-only))
  2229. (setq title (concat
  2230. (format "<span class=\"section-number-%d\">%s</span>"
  2231. level
  2232. (if (and (integerp num)
  2233. ;; fix up num to take into
  2234. ;; account the top-level
  2235. ;; heading value
  2236. (>= (+ num
  2237. org-export-html-toplevel-hlevel
  2238. -1)
  2239. level))
  2240. snumber
  2241. ""))
  2242. " " title)))
  2243. (unless (= head-count 1) (insert "\n</div>\n"))
  2244. (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist)))
  2245. (setq suffix (org-solidify-link-text (or href snu)))
  2246. (setq href (org-solidify-link-text (or href (concat "sec-" snu))))
  2247. (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d%s\">\n<h%d id=\"%s\">%s%s</h%d>\n<div class=\"outline-text-%d\" id=\"text-%s\">\n"
  2248. suffix level (if extra-class (concat " " extra-class) "")
  2249. level href
  2250. extra-targets
  2251. title level level suffix))
  2252. (org-open-par)))))
  2253. (defun org-export-html-get-tag-class-name (tag)
  2254. "Turn tag into a valid class name.
  2255. Replaces invalid characters with \"_\" and then prepends a prefix."
  2256. (save-match-data
  2257. (while (string-match "[^a-zA-Z0-9_]" tag)
  2258. (setq tag (replace-match "_" t t tag))))
  2259. (concat org-export-html-tag-class-prefix tag))
  2260. (defun org-export-html-get-todo-kwd-class-name (kwd)
  2261. "Turn todo keyword into a valid class name.
  2262. Replaces invalid characters with \"_\" and then prepends a prefix."
  2263. (save-match-data
  2264. (while (string-match "[^a-zA-Z0-9_]" kwd)
  2265. (setq kwd (replace-match "_" t t kwd))))
  2266. (concat org-export-html-todo-kwd-class-prefix kwd))
  2267. (defun org-html-level-close (level max-outline-level)
  2268. "Terminate one level in HTML export."
  2269. (if (<= level max-outline-level)
  2270. (insert "</div>\n")
  2271. (org-close-li)
  2272. (insert "</ul>\n")))
  2273. (defun org-html-export-list-line (line pos struct prevs)
  2274. "Insert list syntax in export buffer. Return LINE, maybe modified.
  2275. POS is the item position or line position the line had before
  2276. modifications to buffer. STRUCT is the list structure. PREVS is
  2277. the alist of previous items."
  2278. (let* ((get-type
  2279. (function
  2280. ;; Translate type of list containing POS to "d", "o" or
  2281. ;; "u".
  2282. (lambda (pos struct prevs)
  2283. (let ((type (org-list-get-list-type pos struct prevs)))
  2284. (cond
  2285. ((eq 'ordered type) "o")
  2286. ((eq 'descriptive type) "d")
  2287. (t "u"))))))
  2288. (get-closings
  2289. (function
  2290. ;; Return list of all items and sublists ending at POS, in
  2291. ;; reverse order.
  2292. (lambda (pos)
  2293. (let (out)
  2294. (catch 'exit
  2295. (mapc (lambda (e)
  2296. (let ((end (nth 6 e))
  2297. (item (car e)))
  2298. (cond
  2299. ((= end pos) (push item out))
  2300. ((>= item pos) (throw 'exit nil)))))
  2301. struct))
  2302. out)))))
  2303. ;; First close any previous item, or list, ending at POS.
  2304. (mapc (lambda (e)
  2305. (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
  2306. (first-item (org-list-get-list-begin e struct prevs))
  2307. (type (funcall get-type first-item struct prevs)))
  2308. (org-close-par-maybe)
  2309. ;; Ending for every item
  2310. (org-close-li type)
  2311. ;; We're ending last item of the list: end list.
  2312. (when lastp
  2313. (insert (format "</%sl>\n" type))
  2314. (org-open-par))))
  2315. (funcall get-closings pos))
  2316. (cond
  2317. ;; At an item: insert appropriate tags in export buffer.
  2318. ((assq pos struct)
  2319. (string-match
  2320. (concat "[ \t]*\\(\\S-+[ \t]*\\)"
  2321. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
  2322. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  2323. "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?"
  2324. "\\(.*\\)") line)
  2325. (let* ((checkbox (match-string 3 line))
  2326. (desc-tag (or (match-string 4 line) "???"))
  2327. (body (or (match-string 5 line) ""))
  2328. (list-beg (org-list-get-list-begin pos struct prevs))
  2329. (firstp (= list-beg pos))
  2330. ;; Always refer to first item to determine list type, in
  2331. ;; case list is ill-formed.
  2332. (type (funcall get-type list-beg struct prevs))
  2333. (counter (let ((count-tmp (org-list-get-counter pos struct)))
  2334. (cond
  2335. ((not count-tmp) nil)
  2336. ((string-match "[A-Za-z]" count-tmp)
  2337. (- (string-to-char (upcase count-tmp)) 64))
  2338. ((string-match "[0-9]+" count-tmp)
  2339. count-tmp)))))
  2340. (when firstp
  2341. (org-close-par-maybe)
  2342. (insert (format "<%sl>\n" type)))
  2343. (insert (cond
  2344. ((equal type "d")
  2345. (format "<dt>%s</dt><dd>" desc-tag))
  2346. ((and (equal type "o") counter)
  2347. (format "<li value=\"%s\">" counter))
  2348. (t "<li>")))
  2349. ;; If line had a checkbox, some additional modification is required.
  2350. (when checkbox
  2351. (setq body
  2352. (concat
  2353. (cond
  2354. ((string-match "X" checkbox) "<code>[X]</code> ")
  2355. ((string-match " " checkbox) "<code>[&nbsp;]</code> ")
  2356. (t "<code>[-]</code> "))
  2357. body)))
  2358. ;; Return modified line
  2359. body))
  2360. ;; At a list ender: go to next line (side-effects only).
  2361. ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
  2362. ;; Not at an item: return line unchanged (side-effects only).
  2363. (t line))))
  2364. (provide 'org-html)
  2365. ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
  2366. ;;; org-html.el ends here