org-html.el 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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. (lang-words nil)
  1047. (head-count 0) cnt
  1048. (start 0)
  1049. (coding-system (and (boundp 'buffer-file-coding-system)
  1050. buffer-file-coding-system))
  1051. (coding-system-for-write (or org-export-html-coding-system
  1052. coding-system))
  1053. (save-buffer-coding-system (or org-export-html-coding-system
  1054. coding-system))
  1055. (charset (and coding-system-for-write
  1056. (fboundp 'coding-system-get)
  1057. (coding-system-get coding-system-for-write
  1058. 'mime-charset)))
  1059. (region
  1060. (buffer-substring
  1061. (if region-p (region-beginning) (point-min))
  1062. (if region-p (region-end) (point-max))))
  1063. (org-export-have-math nil)
  1064. (lines
  1065. (org-split-string
  1066. (org-export-preprocess-string
  1067. region
  1068. :emph-multiline t
  1069. :for-backend 'html
  1070. :skip-before-1st-heading
  1071. (plist-get opt-plist :skip-before-1st-heading)
  1072. :drawers (plist-get opt-plist :drawers)
  1073. :todo-keywords (plist-get opt-plist :todo-keywords)
  1074. :tags (plist-get opt-plist :tags)
  1075. :priority (plist-get opt-plist :priority)
  1076. :footnotes (plist-get opt-plist :footnotes)
  1077. :timestamps (plist-get opt-plist :timestamps)
  1078. :archived-trees
  1079. (plist-get opt-plist :archived-trees)
  1080. :select-tags (plist-get opt-plist :select-tags)
  1081. :exclude-tags (plist-get opt-plist :exclude-tags)
  1082. :add-text
  1083. (plist-get opt-plist :text)
  1084. :LaTeX-fragments
  1085. (plist-get opt-plist :LaTeX-fragments))
  1086. "[\r\n]"))
  1087. (mathjax
  1088. (if (or (eq (plist-get opt-plist :LaTeX-fragments) 'mathjax)
  1089. (and org-export-have-math
  1090. (eq (plist-get opt-plist :LaTeX-fragments) t)))
  1091. (org-export-html-mathjax-config
  1092. org-export-html-mathjax-template
  1093. org-export-html-mathjax-options
  1094. (or (plist-get opt-plist :mathjax) ""))
  1095. ""))
  1096. table-open
  1097. table-buffer table-orig-buffer
  1098. ind
  1099. rpl path attr desc descp desc1 desc2 link
  1100. snumber fnc
  1101. footnotes footref-seen
  1102. href
  1103. )
  1104. (let ((inhibit-read-only t))
  1105. (org-unmodified
  1106. (remove-text-properties (point-min) (point-max)
  1107. '(:org-license-to-kill t))))
  1108. (message "Exporting...")
  1109. (setq org-min-level (org-get-min-level lines level-offset))
  1110. (setq org-last-level org-min-level)
  1111. (org-init-section-numbers)
  1112. (cond
  1113. ((and date (string-match "%" date))
  1114. (setq date (format-time-string date)))
  1115. (date)
  1116. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  1117. ;; Get the language-dependent settings
  1118. (setq lang-words (or (assoc language org-export-language-setup)
  1119. (assoc "en" org-export-language-setup)))
  1120. ;; Switch to the output buffer
  1121. (set-buffer buffer)
  1122. (let ((inhibit-read-only t)) (erase-buffer))
  1123. (fundamental-mode)
  1124. (org-install-letbind)
  1125. (and (fboundp 'set-buffer-file-coding-system)
  1126. (set-buffer-file-coding-system coding-system-for-write))
  1127. (let ((case-fold-search nil)
  1128. (org-odd-levels-only odd))
  1129. ;; create local variables for all options, to make sure all called
  1130. ;; functions get the correct information
  1131. (mapc (lambda (x)
  1132. (set (make-local-variable (nth 2 x))
  1133. (plist-get opt-plist (car x))))
  1134. org-export-plist-vars)
  1135. (setq umax (if arg (prefix-numeric-value arg)
  1136. org-export-headline-levels))
  1137. (setq umax-toc (if (integerp org-export-with-toc)
  1138. (min org-export-with-toc umax)
  1139. umax))
  1140. (unless body-only
  1141. ;; File header
  1142. (insert (format
  1143. "%s
  1144. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  1145. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  1146. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  1147. lang=\"%s\" xml:lang=\"%s\">
  1148. <head>
  1149. <title>%s</title>
  1150. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  1151. <meta name=\"generator\" content=\"Org-mode\"/>
  1152. <meta name=\"generated\" content=\"%s\"/>
  1153. <meta name=\"author\" content=\"%s\"/>
  1154. <meta name=\"description\" content=\"%s\"/>
  1155. <meta name=\"keywords\" content=\"%s\"/>
  1156. %s
  1157. %s
  1158. </head>
  1159. <body>
  1160. <div id=\"content\">
  1161. %s
  1162. "
  1163. (format
  1164. (or (and (stringp org-export-html-xml-declaration)
  1165. org-export-html-xml-declaration)
  1166. (cdr (assoc html-extension org-export-html-xml-declaration))
  1167. (cdr (assoc "html" org-export-html-xml-declaration))
  1168. "")
  1169. (or charset "iso-8859-1"))
  1170. language language
  1171. title
  1172. (or charset "iso-8859-1")
  1173. date author description keywords
  1174. style
  1175. mathjax
  1176. (if (or link-up link-home)
  1177. (concat
  1178. (format org-export-html-home/up-format
  1179. (or link-up link-home)
  1180. (or link-home link-up))
  1181. "\n")
  1182. "")))
  1183. ;; insert html preamble
  1184. (when (plist-get opt-plist :html-preamble)
  1185. (let ((html-pre (plist-get opt-plist :html-preamble)))
  1186. (cond ((stringp html-pre)
  1187. (insert
  1188. (format-spec html-pre `((?t . ,title) (?a . ,author)
  1189. (?d . ,date) (?e . ,email)))))
  1190. ((functionp html-pre)
  1191. (funcall html-pre opt-plist))
  1192. (t
  1193. (insert
  1194. (format-spec
  1195. (or (cadr (assoc (nth 0 lang-words)
  1196. org-export-html-preamble-format))
  1197. (cadr (assoc "en" org-export-html-preamble-format)))
  1198. `((?t . ,title) (?a . ,author)
  1199. (?d . ,date) (?e . ,email)))))))))
  1200. (if (and org-export-with-toc (not body-only))
  1201. (progn
  1202. (push (format "<h%d>%s</h%d>\n"
  1203. org-export-html-toplevel-hlevel
  1204. (nth 3 lang-words)
  1205. org-export-html-toplevel-hlevel)
  1206. thetoc)
  1207. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  1208. (push "<ul>\n<li>" thetoc)
  1209. (setq lines
  1210. (mapcar '(lambda (line)
  1211. (if (and (string-match org-todo-line-regexp line)
  1212. (not (get-text-property 0 'org-protected line)))
  1213. ;; This is a headline
  1214. (progn
  1215. (setq have-headings t)
  1216. (setq level (- (match-end 1) (match-beginning 1)
  1217. level-offset)
  1218. level (org-tr-level level)
  1219. txt (save-match-data
  1220. (org-html-expand
  1221. (org-export-cleanup-toc-line
  1222. (match-string 3 line))))
  1223. todo
  1224. (or (and org-export-mark-todo-in-toc
  1225. (match-beginning 2)
  1226. (not (member (match-string 2 line)
  1227. org-done-keywords)))
  1228. ; TODO, not DONE
  1229. (and org-export-mark-todo-in-toc
  1230. (= level umax-toc)
  1231. (org-search-todo-below
  1232. line lines level))))
  1233. (if (string-match
  1234. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  1235. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  1236. (if (string-match quote-re0 txt)
  1237. (setq txt (replace-match "" t t txt)))
  1238. (setq snumber (org-section-number level))
  1239. (if org-export-with-section-numbers
  1240. (setq txt (concat snumber " " txt)))
  1241. (if (<= level (max umax umax-toc))
  1242. (setq head-count (+ head-count 1)))
  1243. (if (<= level umax-toc)
  1244. (progn
  1245. (if (> level org-last-level)
  1246. (progn
  1247. (setq cnt (- level org-last-level))
  1248. (while (>= (setq cnt (1- cnt)) 0)
  1249. (push "\n<ul>\n<li>" thetoc))
  1250. (push "\n" thetoc)))
  1251. (if (< level org-last-level)
  1252. (progn
  1253. (setq cnt (- org-last-level level))
  1254. (while (>= (setq cnt (1- cnt)) 0)
  1255. (push "</li>\n</ul>" thetoc))
  1256. (push "\n" thetoc)))
  1257. ;; Check for targets
  1258. (while (string-match org-any-target-regexp line)
  1259. (setq line (replace-match
  1260. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  1261. t t line)))
  1262. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  1263. (setq txt (replace-match "" t t txt)))
  1264. (setq href
  1265. (replace-regexp-in-string
  1266. "\\." "_" (format "sec-%s" snumber)))
  1267. (setq href (org-solidify-link-text (or (cdr (assoc href org-export-preferred-target-alist)) href)))
  1268. (push
  1269. (format
  1270. (if todo
  1271. "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
  1272. "</li>\n<li><a href=\"#%s\">%s</a>")
  1273. href txt) thetoc)
  1274. (setq org-last-level level))
  1275. )))
  1276. line)
  1277. lines))
  1278. (while (> org-last-level (1- org-min-level))
  1279. (setq org-last-level (1- org-last-level))
  1280. (push "</li>\n</ul>\n" thetoc))
  1281. (push "</div>\n" thetoc)
  1282. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  1283. (setq head-count 0)
  1284. (org-init-section-numbers)
  1285. (org-open-par)
  1286. (while (setq line (pop lines) origline line)
  1287. (catch 'nextline
  1288. ;; end of quote section?
  1289. (when (and inquote (string-match "^\\*+ " line))
  1290. (insert "</pre>\n")
  1291. (org-open-par)
  1292. (setq inquote nil))
  1293. ;; inside a quote section?
  1294. (when inquote
  1295. (insert (org-html-protect line) "\n")
  1296. (throw 'nextline nil))
  1297. ;; Fixed-width, verbatim lines (examples)
  1298. (when (and org-export-with-fixed-width
  1299. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  1300. (when (not infixed)
  1301. (setq infixed t)
  1302. (org-close-par-maybe)
  1303. (insert "<pre class=\"example\">\n"))
  1304. (insert (org-html-protect (match-string 3 line)) "\n")
  1305. (when (or (not lines)
  1306. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  1307. (car lines))))
  1308. (setq infixed nil)
  1309. (insert "</pre>\n")
  1310. (org-open-par))
  1311. (throw 'nextline nil))
  1312. ;; Protected HTML
  1313. (when (and (get-text-property 0 'org-protected line)
  1314. ;; Make sure it is the entire line that is protected
  1315. (not (< (or (next-single-property-change
  1316. 0 'org-protected line) 10000)
  1317. (length line))))
  1318. (let (par (ind (get-text-property 0 'original-indentation line)))
  1319. (when (re-search-backward
  1320. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  1321. (setq par (match-string 1))
  1322. (replace-match "\\2\n"))
  1323. (insert line "\n")
  1324. (while (and lines
  1325. (or (= (length (car lines)) 0)
  1326. (not ind)
  1327. (equal ind (get-text-property 0 'original-indentation (car lines))))
  1328. (or (= (length (car lines)) 0)
  1329. (get-text-property 0 'org-protected (car lines))))
  1330. (insert (pop lines) "\n"))
  1331. (and par (insert "<p>\n")))
  1332. (throw 'nextline nil))
  1333. ;; Blockquotes, verse, and center
  1334. (when (equal "ORG-BLOCKQUOTE-START" line)
  1335. (org-close-par-maybe)
  1336. (insert "<blockquote>\n")
  1337. (org-open-par)
  1338. (throw 'nextline nil))
  1339. (when (equal "ORG-BLOCKQUOTE-END" line)
  1340. (org-close-par-maybe)
  1341. (insert "\n</blockquote>\n")
  1342. (org-open-par)
  1343. (throw 'nextline nil))
  1344. (when (equal "ORG-VERSE-START" line)
  1345. (org-close-par-maybe)
  1346. (insert "\n<p class=\"verse\">\n")
  1347. (setq org-par-open t)
  1348. (setq inverse t)
  1349. (throw 'nextline nil))
  1350. (when (equal "ORG-VERSE-END" line)
  1351. (insert "</p>\n")
  1352. (setq org-par-open nil)
  1353. (org-open-par)
  1354. (setq inverse nil)
  1355. (throw 'nextline nil))
  1356. (when (equal "ORG-CENTER-START" line)
  1357. (org-close-par-maybe)
  1358. (insert "\n<div style=\"text-align: center\">")
  1359. (org-open-par)
  1360. (throw 'nextline nil))
  1361. (when (equal "ORG-CENTER-END" line)
  1362. (org-close-par-maybe)
  1363. (insert "\n</div>")
  1364. (org-open-par)
  1365. (throw 'nextline nil))
  1366. (run-hooks 'org-export-html-after-blockquotes-hook)
  1367. (when inverse
  1368. (let ((i (org-get-string-indentation line)))
  1369. (if (> i 0)
  1370. (setq line (concat (mapconcat 'identity
  1371. (make-list (* 2 i) "\\nbsp") "")
  1372. " " (org-trim line))))
  1373. (unless (string-match "\\\\\\\\[ \t]*$" line)
  1374. (setq line (concat line "\\\\")))))
  1375. ;; make targets to anchors
  1376. (setq start 0)
  1377. (while (string-match
  1378. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  1379. (cond
  1380. ((get-text-property (match-beginning 1) 'org-protected line)
  1381. (setq start (match-end 1)))
  1382. ((match-end 2)
  1383. (setq line (replace-match
  1384. (format
  1385. "@<a name=\"%s\" id=\"%s\">@</a>"
  1386. (org-solidify-link-text (match-string 1 line))
  1387. (org-solidify-link-text (match-string 1 line)))
  1388. t t line)))
  1389. ((and org-export-with-toc (equal (string-to-char line) ?*))
  1390. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  1391. (setq line (replace-match
  1392. (concat "@<span class=\"target\">"
  1393. (match-string 1 line) "@</span> ")
  1394. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  1395. t t line)))
  1396. (t
  1397. (setq line (replace-match
  1398. (concat "@<a name=\""
  1399. (org-solidify-link-text (match-string 1 line))
  1400. "\" class=\"target\">" (match-string 1 line)
  1401. "@</a> ")
  1402. t t line)))))
  1403. (setq line (org-html-handle-time-stamps line))
  1404. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  1405. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  1406. ;; Also handle sub_superscripts and checkboxes
  1407. (or (string-match org-table-hline-regexp line)
  1408. (setq line (org-html-expand line)))
  1409. ;; Format the links
  1410. (setq line (org-html-handle-links line opt-plist))
  1411. ;; TODO items
  1412. (if (and (string-match org-todo-line-regexp line)
  1413. (match-beginning 2))
  1414. (setq line
  1415. (concat (substring line 0 (match-beginning 2))
  1416. "<span class=\""
  1417. (if (member (match-string 2 line)
  1418. org-done-keywords)
  1419. "done" "todo")
  1420. " " (match-string 2 line)
  1421. "\"> " (org-export-html-get-todo-kwd-class-name
  1422. (match-string 2 line))
  1423. "</span>" (substring line (match-end 2)))))
  1424. ;; Does this contain a reference to a footnote?
  1425. (when org-export-with-footnotes
  1426. (setq start 0)
  1427. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  1428. (if (get-text-property (match-beginning 2) 'org-protected line)
  1429. (setq start (match-end 2))
  1430. (let ((n (match-string 2 line)) extra a)
  1431. (if (setq a (assoc n footref-seen))
  1432. (progn
  1433. (setcdr a (1+ (cdr a)))
  1434. (setq extra (format ".%d" (cdr a))))
  1435. (setq extra "")
  1436. (push (cons n 1) footref-seen))
  1437. (setq line
  1438. (replace-match
  1439. (format
  1440. (concat "%s"
  1441. (format org-export-html-footnote-format
  1442. "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
  1443. (or (match-string 1 line) "") n extra n n)
  1444. t t line))))))
  1445. (cond
  1446. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1447. ;; This is a headline
  1448. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1449. level-offset))
  1450. txt (match-string 2 line))
  1451. (if (string-match quote-re0 txt)
  1452. (setq txt (replace-match "" t t txt)))
  1453. (if (<= level (max umax umax-toc))
  1454. (setq head-count (+ head-count 1)))
  1455. (setq first-heading-pos (or first-heading-pos (point)))
  1456. (org-html-level-start level txt umax
  1457. (and org-export-with-toc (<= level umax))
  1458. head-count)
  1459. ;; QUOTES
  1460. (when (string-match quote-re line)
  1461. (org-close-par-maybe)
  1462. (insert "<pre>")
  1463. (setq inquote t)))
  1464. ((and org-export-with-tables
  1465. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1466. (when (not table-open)
  1467. ;; New table starts
  1468. (setq table-open t table-buffer nil table-orig-buffer nil))
  1469. ;; Accumulate lines
  1470. (setq table-buffer (cons line table-buffer)
  1471. table-orig-buffer (cons origline table-orig-buffer))
  1472. (when (or (not lines)
  1473. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1474. (car lines))))
  1475. (setq table-open nil
  1476. table-buffer (nreverse table-buffer)
  1477. table-orig-buffer (nreverse table-orig-buffer))
  1478. (org-close-par-maybe)
  1479. (insert (org-format-table-html table-buffer table-orig-buffer))))
  1480. ;; Normal lines
  1481. (t
  1482. ;; This line either is list item or end a list.
  1483. (when (get-text-property 0 'list-item line)
  1484. (setq line (org-html-export-list-line
  1485. line
  1486. (get-text-property 0 'list-item line)
  1487. (get-text-property 0 'list-struct line)
  1488. (get-text-property 0 'list-prevs line))))
  1489. ;; Horizontal line
  1490. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1491. (if org-par-open
  1492. (insert "\n</p>\n<hr/>\n<p>\n")
  1493. (insert "\n<hr/>\n"))
  1494. (throw 'nextline nil))
  1495. ;; Empty lines start a new paragraph. If hand-formatted lists
  1496. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1497. ;; also start a new paragraph.
  1498. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  1499. ;; Is this the start of a footnote?
  1500. (when org-export-with-footnotes
  1501. (when (and (boundp 'footnote-section-tag-regexp)
  1502. (string-match (concat "^" footnote-section-tag-regexp)
  1503. line))
  1504. ;; ignore this line
  1505. (throw 'nextline nil))
  1506. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1507. (org-close-par-maybe)
  1508. (let ((n (match-string 1 line)))
  1509. (setq org-par-open t
  1510. line (replace-match
  1511. (format
  1512. (concat "<p class=\"footnote\">"
  1513. (format org-export-html-footnote-format
  1514. "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
  1515. n n n) t t line)))))
  1516. ;; Check if the line break needs to be conserved
  1517. (cond
  1518. ((string-match "\\\\\\\\[ \t]*$" line)
  1519. (setq line (replace-match "<br/>" t t line)))
  1520. (org-export-preserve-breaks
  1521. (setq line (concat line "<br/>"))))
  1522. ;; Check if a paragraph should be started
  1523. (let ((start 0))
  1524. (while (and org-par-open
  1525. (string-match "\\\\par\\>" line start))
  1526. ;; Leave a space in the </p> so that the footnote matcher
  1527. ;; does not see this.
  1528. (if (not (get-text-property (match-beginning 0)
  1529. 'org-protected line))
  1530. (setq line (replace-match "</p ><p >" t t line)))
  1531. (setq start (match-end 0))))
  1532. (insert line "\n")))))
  1533. ;; Properly close all local lists and other lists
  1534. (when inquote
  1535. (insert "</pre>\n")
  1536. (org-open-par))
  1537. (org-html-level-start 1 nil umax
  1538. (and org-export-with-toc (<= level umax))
  1539. head-count)
  1540. ;; the </div> to close the last text-... div.
  1541. (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
  1542. (save-excursion
  1543. (goto-char (point-min))
  1544. (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
  1545. (push (match-string 0) footnotes)
  1546. (replace-match "" t t)))
  1547. (when footnotes
  1548. (insert (format org-export-html-footnotes-section
  1549. (nth 4 lang-words)
  1550. (mapconcat 'identity (nreverse footnotes) "\n"))
  1551. "\n"))
  1552. (let ((bib (org-export-html-get-bibliography)))
  1553. (when bib
  1554. (insert "\n" bib "\n")))
  1555. ;; export html postamble
  1556. (unless body-only
  1557. (let ((html-post (plist-get opt-plist :html-postamble))
  1558. (email
  1559. (mapconcat (lambda(e)
  1560. (format "<a href=\"mailto:%s\">%s</a>" e e))
  1561. (split-string email ",+ *")
  1562. ", "))
  1563. (creator-info
  1564. (concat "Org version " org-version " with Emacs version "
  1565. (number-to-string emacs-major-version))))
  1566. (when (plist-get opt-plist :html-postamble)
  1567. (cond ((stringp html-post)
  1568. (insert "<div id=\"postamble\">\n")
  1569. (insert (format-spec html-post
  1570. `((?a . ,author) (?e . ,email)
  1571. (?d . ,date) (?c . ,creator-info)
  1572. (?v . ,html-validation-link))))
  1573. (insert "</div>"))
  1574. ((functionp html-post)
  1575. (funcall html-post opt-plist))
  1576. ((eq html-post 'auto)
  1577. ;; fall back on default postamble
  1578. (insert "<div id=\"postamble\">\n")
  1579. (when (plist-get opt-plist :time-stamp-file)
  1580. (insert "<p class=\"date\">" (nth 2 lang-words) ": " date "</p>\n"))
  1581. (when (and (plist-get opt-plist :author-info) author)
  1582. (insert "<p class=\"author\">" (nth 1 lang-words) ": " author "</p>\n"))
  1583. (when (and (plist-get opt-plist :email-info) email)
  1584. (insert "<p class=\"email\">" email "</p>\n"))
  1585. (when (plist-get opt-plist :creator-info)
  1586. (insert "<p class=\"creator\">"
  1587. (concat "Org version " org-version " with Emacs version "
  1588. (number-to-string emacs-major-version) "</p>\n")))
  1589. (insert html-validation-link "\n</div>"))
  1590. (t
  1591. (insert "<div id=\"postamble\">\n")
  1592. (insert (format-spec
  1593. (or (cadr (assoc (nth 0 lang-words)
  1594. org-export-html-postamble-format))
  1595. (cadr (assoc "en" org-export-html-postamble-format)))
  1596. `((?a . ,author) (?e . ,email)
  1597. (?d . ,date) (?c . ,creator-info)
  1598. (?v . ,html-validation-link))))
  1599. (insert "</div>"))))))
  1600. (if org-export-html-with-timestamp
  1601. (insert org-export-html-html-helper-timestamp))
  1602. (unless body-only (insert "\n</div>\n</body>\n</html>\n"))
  1603. (unless (plist-get opt-plist :buffer-will-be-killed)
  1604. (normal-mode)
  1605. (if (eq major-mode (default-value 'major-mode))
  1606. (html-mode)))
  1607. ;; insert the table of contents
  1608. (goto-char (point-min))
  1609. (when thetoc
  1610. (if (or (re-search-forward
  1611. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1612. (re-search-forward
  1613. "\\[TABLE-OF-CONTENTS\\]" nil t))
  1614. (progn
  1615. (goto-char (match-beginning 0))
  1616. (replace-match ""))
  1617. (goto-char first-heading-pos)
  1618. (when (looking-at "\\s-*</p>")
  1619. (goto-char (match-end 0))
  1620. (insert "\n")))
  1621. (insert "<div id=\"table-of-contents\">\n")
  1622. (let ((beg (point)))
  1623. (mapc 'insert thetoc)
  1624. (insert "</div>\n")
  1625. (while (re-search-backward "<li>[ \r\n\t]*</li>\n?" beg t)
  1626. (replace-match ""))))
  1627. ;; remove empty paragraphs
  1628. (goto-char (point-min))
  1629. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  1630. (replace-match ""))
  1631. (goto-char (point-min))
  1632. ;; Convert whitespace place holders
  1633. (goto-char (point-min))
  1634. (let (beg end n)
  1635. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  1636. (setq n (get-text-property beg 'org-whitespace)
  1637. end (next-single-property-change beg 'org-whitespace))
  1638. (goto-char beg)
  1639. (delete-region beg end)
  1640. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  1641. (make-string n ?x)))))
  1642. ;; Remove empty lines at the beginning of the file.
  1643. (goto-char (point-min))
  1644. (when (looking-at "\\s-+\n") (replace-match ""))
  1645. ;; Remove display properties
  1646. (remove-text-properties (point-min) (point-max) '(display t))
  1647. ;; Run the hook
  1648. (run-hooks 'org-export-html-final-hook)
  1649. (or to-buffer (save-buffer))
  1650. (goto-char (point-min))
  1651. (or (org-export-push-to-kill-ring "HTML")
  1652. (message "Exporting... done"))
  1653. (if (eq to-buffer 'string)
  1654. (prog1 (buffer-substring (point-min) (point-max))
  1655. (kill-buffer (current-buffer)))
  1656. (current-buffer)))))
  1657. (defun org-export-html-format-href (s)
  1658. "Make sure the S is valid as a href reference in an XHTML document."
  1659. (save-match-data
  1660. (let ((start 0))
  1661. (while (string-match "&" s start)
  1662. (setq start (+ (match-beginning 0) 3)
  1663. s (replace-match "&amp;" t t s)))))
  1664. s)
  1665. (defun org-export-html-format-desc (s)
  1666. "Make sure the S is valid as a description in a link."
  1667. (if (and s (not (get-text-property 1 'org-protected s)))
  1668. (save-match-data
  1669. (org-html-do-expand s))
  1670. s))
  1671. (defun org-export-html-format-image (src par-open)
  1672. "Create image tag with source and attributes."
  1673. (save-match-data
  1674. (if (string-match "^ltxpng/" src)
  1675. (format "<img src=\"%s\" alt=\"%s\"/>"
  1676. src (org-find-text-property-in-string 'org-latex-src src))
  1677. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1678. (attr (org-find-text-property-in-string 'org-attributes src))
  1679. (label (org-find-text-property-in-string 'org-label src)))
  1680. (setq caption (and caption (org-html-do-expand caption)))
  1681. (concat
  1682. (if caption
  1683. (format "%s<div %sclass=\"figure\">
  1684. <p>"
  1685. (if org-par-open "</p>\n" "")
  1686. (if label (format "id=\"%s\" " (org-solidify-link-text label)) "")))
  1687. (format "<img src=\"%s\"%s />"
  1688. src
  1689. (if (string-match "\\<alt=" (or attr ""))
  1690. (concat " " attr )
  1691. (concat " " attr " alt=\"" src "\"")))
  1692. (if caption
  1693. (format "</p>%s
  1694. </div>%s"
  1695. (concat "\n<p>" caption "</p>")
  1696. (if org-par-open "\n<p>" ""))))))))
  1697. (defun org-export-html-get-bibliography ()
  1698. "Find bibliography, cut it out and return it."
  1699. (catch 'exit
  1700. (let (beg end (cnt 1) bib)
  1701. (save-excursion
  1702. (goto-char (point-min))
  1703. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  1704. (setq beg (match-beginning 0))
  1705. (while (re-search-forward "</?div\\>" nil t)
  1706. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  1707. (when (= cnt 0)
  1708. (and (looking-at ">") (forward-char 1))
  1709. (setq bib (buffer-substring beg (point)))
  1710. (delete-region beg (point))
  1711. (throw 'exit bib))))
  1712. nil))))
  1713. (defvar org-table-number-regexp) ; defined in org-table.el
  1714. (defun org-format-table-html (lines olines &optional no-css)
  1715. "Find out which HTML converter to use and return the HTML code.
  1716. NO-CSS is passed to the exporter."
  1717. (if (stringp lines)
  1718. (setq lines (org-split-string lines "\n")))
  1719. (if (string-match "^[ \t]*|" (car lines))
  1720. ;; A normal org table
  1721. (org-format-org-table-html lines nil no-css)
  1722. ;; Table made by table.el - test for spanning
  1723. (let* ((hlines (delq nil (mapcar
  1724. (lambda (x)
  1725. (if (string-match "^[ \t]*\\+-" x) x
  1726. nil))
  1727. lines)))
  1728. (first (car hlines))
  1729. (ll (and (string-match "\\S-+" first)
  1730. (match-string 0 first)))
  1731. (re (concat "^[ \t]*" (regexp-quote ll)))
  1732. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  1733. hlines))))
  1734. (if (and (not spanning)
  1735. (not org-export-prefer-native-exporter-for-tables))
  1736. ;; We can use my own converter with HTML conversions
  1737. (org-format-table-table-html lines)
  1738. ;; Need to use the code generator in table.el, with the original text.
  1739. (org-format-table-table-html-using-table-generate-source olines)))))
  1740. (defvar org-table-number-fraction) ; defined in org-table.el
  1741. (defun org-format-org-table-html (lines &optional splice no-css)
  1742. "Format a table into HTML.
  1743. LINES is a list of lines. Optional argument SPLICE means, do not
  1744. insert header and surrounding <table> tags, just format the lines.
  1745. Optional argument NO-CSS means use XHTML attributes instead of CSS
  1746. for formatting. This is required for the DocBook exporter."
  1747. (require 'org-table)
  1748. ;; Get rid of hlines at beginning and end
  1749. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1750. (setq lines (nreverse lines))
  1751. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1752. (setq lines (nreverse lines))
  1753. (when org-export-table-remove-special-lines
  1754. ;; Check if the table has a marking column. If yes remove the
  1755. ;; column and the special lines
  1756. (setq lines (org-table-clean-before-export lines)))
  1757. (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
  1758. (label (org-find-text-property-in-string 'org-label (car lines)))
  1759. (forced-aligns (org-find-text-property-in-string 'org-forced-aligns
  1760. (car lines)))
  1761. (attributes (org-find-text-property-in-string 'org-attributes
  1762. (car lines)))
  1763. (html-table-tag (org-export-splice-attributes
  1764. html-table-tag attributes))
  1765. (head (and org-export-highlight-first-table-line
  1766. (delq nil (mapcar
  1767. (lambda (x) (string-match "^[ \t]*|-" x))
  1768. (cdr lines)))))
  1769. (nline 0) fnum nfields i (cnt 0)
  1770. tbopen line fields html gr colgropen rowstart rowend
  1771. ali align aligns n)
  1772. (setq caption (and caption (org-html-do-expand caption)))
  1773. (when (and forced-aligns org-table-clean-did-remove-column)
  1774. (setq forced-aligns
  1775. (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
  1776. (if splice (setq head nil))
  1777. (unless splice (push (if head "<thead>" "<tbody>") html))
  1778. (setq tbopen t)
  1779. (while (setq line (pop lines))
  1780. (catch 'next-line
  1781. (if (string-match "^[ \t]*|-" line)
  1782. (progn
  1783. (unless splice
  1784. (push (if head "</thead>" "</tbody>") html)
  1785. (if lines (push "<tbody>" html) (setq tbopen nil)))
  1786. (setq head nil) ;; head ends here, first time around
  1787. ;; ignore this line
  1788. (throw 'next-line t)))
  1789. ;; Break the line into fields
  1790. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1791. (unless fnum (setq fnum (make-vector (length fields) 0)
  1792. nfields (length fnum)))
  1793. (setq nline (1+ nline) i -1
  1794. rowstart (eval (car org-export-table-row-tags))
  1795. rowend (eval (cdr org-export-table-row-tags)))
  1796. (push (concat rowstart
  1797. (mapconcat
  1798. (lambda (x)
  1799. (setq i (1+ i) ali (format "@@class%03d@@" i))
  1800. (if (and (< i nfields) ; make sure no rogue line causes an error here
  1801. (string-match org-table-number-regexp x))
  1802. (incf (aref fnum i)))
  1803. (cond
  1804. (head
  1805. (concat
  1806. (format (car org-export-table-header-tags)
  1807. "col" ali)
  1808. x
  1809. (cdr org-export-table-header-tags)))
  1810. ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
  1811. (concat
  1812. (format (car org-export-table-header-tags)
  1813. "row" ali)
  1814. x
  1815. (cdr org-export-table-header-tags)))
  1816. (t
  1817. (concat (format (car org-export-table-data-tags) ali)
  1818. x
  1819. (cdr org-export-table-data-tags)))))
  1820. fields "")
  1821. rowend)
  1822. html)))
  1823. (unless splice (if tbopen (push "</tbody>" html)))
  1824. (unless splice (push "</table>\n" html))
  1825. (setq html (nreverse html))
  1826. (unless splice
  1827. ;; Put in col tags with the alignment (unfortunately often ignored...)
  1828. (unless (car org-table-colgroup-info)
  1829. (setq org-table-colgroup-info
  1830. (cons :start (cdr org-table-colgroup-info))))
  1831. (setq i 0)
  1832. (push (mapconcat
  1833. (lambda (x)
  1834. (setq gr (pop org-table-colgroup-info)
  1835. i (1+ i)
  1836. align (if (assoc i forced-aligns)
  1837. (cdr (assoc (cdr (assoc i forced-aligns))
  1838. '(("l" . "left") ("r" . "right")
  1839. ("c" . "center"))))
  1840. (if (> (/ (float x) nline)
  1841. org-table-number-fraction)
  1842. "right" "left")))
  1843. (push align aligns)
  1844. (format (if no-css
  1845. "%s<col align=\"%s\" />%s"
  1846. "%s<col class=\"%s\" />%s")
  1847. (if (memq gr '(:start :startend))
  1848. (prog1
  1849. (if colgropen
  1850. "</colgroup>\n<colgroup>"
  1851. "<colgroup>")
  1852. (setq colgropen t))
  1853. "")
  1854. align
  1855. (if (memq gr '(:end :startend))
  1856. (progn (setq colgropen nil) "</colgroup>")
  1857. "")))
  1858. fnum "")
  1859. html)
  1860. (setq aligns (nreverse aligns))
  1861. (if colgropen (setq html (cons (car html)
  1862. (cons "</colgroup>" (cdr html)))))
  1863. ;; Since the output of HTML table formatter can also be used in
  1864. ;; DocBook document, we want to always include the caption to make
  1865. ;; DocBook XML file valid.
  1866. (push (format "<caption>%s</caption>" (or caption "")) html)
  1867. (when label
  1868. (setq html-table-tag (org-export-splice-attributes html-table-tag (format "id=\"%s\"" (org-solidify-link-text label)))))
  1869. (push html-table-tag html))
  1870. (setq html (mapcar
  1871. (lambda (x)
  1872. (replace-regexp-in-string
  1873. "@@class\\([0-9]+\\)@@"
  1874. (lambda (txt)
  1875. (if (not org-export-html-table-align-individual-fields)
  1876. ""
  1877. (setq n (string-to-number (match-string 1 txt)))
  1878. (format (if no-css " align=\"%s\"" " class=\"%s\"")
  1879. (or (nth n aligns) "left"))))
  1880. x))
  1881. html))
  1882. (concat (mapconcat 'identity html "\n") "\n")))
  1883. (defun org-export-splice-attributes (tag attributes)
  1884. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  1885. (if (not attributes)
  1886. tag
  1887. (let (oldatt newatt)
  1888. (setq oldatt (org-extract-attributes-from-string tag)
  1889. tag (pop oldatt)
  1890. newatt (cdr (org-extract-attributes-from-string attributes)))
  1891. (while newatt
  1892. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  1893. (if (string-match ">" tag)
  1894. (setq tag
  1895. (replace-match (concat (org-attributes-to-string oldatt) ">")
  1896. t t tag)))
  1897. tag)))
  1898. (defun org-format-table-table-html (lines)
  1899. "Format a table generated by table.el into HTML.
  1900. This conversion does *not* use `table-generate-source' from table.el.
  1901. This has the advantage that Org-mode's HTML conversions can be used.
  1902. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1903. (let (line field-buffer
  1904. (head org-export-highlight-first-table-line)
  1905. fields html empty i)
  1906. (setq html (concat html-table-tag "\n"))
  1907. (while (setq line (pop lines))
  1908. (setq empty "&nbsp;")
  1909. (catch 'next-line
  1910. (if (string-match "^[ \t]*\\+-" line)
  1911. (progn
  1912. (if field-buffer
  1913. (progn
  1914. (setq
  1915. html
  1916. (concat
  1917. html
  1918. "<tr>"
  1919. (mapconcat
  1920. (lambda (x)
  1921. (if (equal x "") (setq x empty))
  1922. (if head
  1923. (concat
  1924. (format (car org-export-table-header-tags) "col" "")
  1925. x
  1926. (cdr org-export-table-header-tags))
  1927. (concat (format (car org-export-table-data-tags) "") x
  1928. (cdr org-export-table-data-tags))))
  1929. field-buffer "\n")
  1930. "</tr>\n"))
  1931. (setq head nil)
  1932. (setq field-buffer nil)))
  1933. ;; Ignore this line
  1934. (throw 'next-line t)))
  1935. ;; Break the line into fields and store the fields
  1936. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1937. (if field-buffer
  1938. (setq field-buffer (mapcar
  1939. (lambda (x)
  1940. (concat x "<br/>" (pop fields)))
  1941. field-buffer))
  1942. (setq field-buffer fields))))
  1943. (setq html (concat html "</table>\n"))
  1944. html))
  1945. (defun org-format-table-table-html-using-table-generate-source (lines)
  1946. "Format a table into html, using `table-generate-source' from table.el.
  1947. This has the advantage that cell- or row-spanning is allowed.
  1948. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  1949. (require 'table)
  1950. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1951. (erase-buffer)
  1952. (insert (mapconcat 'identity lines "\n"))
  1953. (goto-char (point-min))
  1954. (if (not (re-search-forward "|[^+]" nil t))
  1955. (error "Error processing table"))
  1956. (table-recognize-table)
  1957. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1958. (table-generate-source 'html " org-tmp2 ")
  1959. (set-buffer " org-tmp2 ")
  1960. (buffer-substring (point-min) (point-max))))
  1961. (defun org-export-splice-style (style extra)
  1962. "Splice EXTRA into STYLE, just before \"</style>\"."
  1963. (if (and (stringp extra)
  1964. (string-match "\\S-" extra)
  1965. (string-match "</style>" style))
  1966. (concat (substring style 0 (match-beginning 0))
  1967. "\n" extra "\n"
  1968. (substring style (match-beginning 0)))
  1969. style))
  1970. (defun org-html-handle-time-stamps (s)
  1971. "Format time stamps in string S, or remove them."
  1972. (catch 'exit
  1973. (let (r b)
  1974. (while (string-match org-maybe-keyword-time-regexp s)
  1975. (or b (setq b (substring s 0 (match-beginning 0))))
  1976. (setq r (concat
  1977. r (substring s 0 (match-beginning 0))
  1978. " @<span class=\"timestamp-wrapper\">"
  1979. (if (match-end 1)
  1980. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  1981. (match-string 1 s)))
  1982. (format " @<span class=\"timestamp\">%s@</span>"
  1983. (substring
  1984. (org-translate-time (match-string 3 s)) 1 -1))
  1985. "@</span>")
  1986. s (substring s (match-end 0))))
  1987. ;; Line break if line started and ended with time stamp stuff
  1988. (if (not r)
  1989. s
  1990. (setq r (concat r s))
  1991. (unless (string-match "\\S-" (concat b s))
  1992. (setq r (concat r "<br/>")))
  1993. r))))
  1994. (defvar htmlize-buffer-places) ; from htmlize.el
  1995. (defun org-export-htmlize-region-for-paste (beg end)
  1996. "Convert the region to HTML, using htmlize.el.
  1997. This is much like `htmlize-region-for-paste', only that it uses
  1998. the settings define in the org-... variables."
  1999. (let* ((htmlize-output-type org-export-htmlize-output-type)
  2000. (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
  2001. (htmlbuf (htmlize-region beg end)))
  2002. (unwind-protect
  2003. (with-current-buffer htmlbuf
  2004. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  2005. (plist-get htmlize-buffer-places 'content-end)))
  2006. (kill-buffer htmlbuf))))
  2007. ;;;###autoload
  2008. (defun org-export-htmlize-generate-css ()
  2009. "Create the CSS for all font definitions in the current Emacs session.
  2010. Use this to create face definitions in your CSS style file that can then
  2011. be used by code snippets transformed by htmlize.
  2012. This command just produces a buffer that contains class definitions for all
  2013. faces used in the current Emacs session. You can copy and paste the ones you
  2014. need into your CSS file.
  2015. If you then set `org-export-htmlize-output-type' to `css', calls to
  2016. the function `org-export-htmlize-region-for-paste' will produce code
  2017. that uses these same face definitions."
  2018. (interactive)
  2019. (require 'htmlize)
  2020. (and (get-buffer "*html*") (kill-buffer "*html*"))
  2021. (with-temp-buffer
  2022. (let ((fl (face-list))
  2023. (htmlize-css-name-prefix "org-")
  2024. (htmlize-output-type 'css)
  2025. f i)
  2026. (while (setq f (pop fl)
  2027. i (and f (face-attribute f :inherit)))
  2028. (when (and (symbolp f) (or (not i) (not (listp i))))
  2029. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  2030. (htmlize-region (point-min) (point-max))))
  2031. (switch-to-buffer "*html*")
  2032. (goto-char (point-min))
  2033. (if (re-search-forward "<style" nil t)
  2034. (delete-region (point-min) (match-beginning 0)))
  2035. (if (re-search-forward "</style>" nil t)
  2036. (delete-region (1+ (match-end 0)) (point-max)))
  2037. (beginning-of-line 1)
  2038. (if (looking-at " +") (replace-match ""))
  2039. (goto-char (point-min)))
  2040. (defun org-html-protect (s)
  2041. "Convert characters to HTML equivalent.
  2042. Possible conversions are set in `org-export-html-protect-char-alist'."
  2043. (let ((cl org-export-html-protect-char-alist) c)
  2044. (while (setq c (pop cl))
  2045. (let ((start 0))
  2046. (while (string-match (car c) s start)
  2047. (setq s (replace-match (cdr c) t t s)
  2048. start (1+ (match-beginning 0))))))
  2049. s))
  2050. (defun org-html-expand (string)
  2051. "Prepare STRING for HTML export. Apply all active conversions.
  2052. If there are links in the string, don't modify these."
  2053. (let* ((re (concat org-bracket-link-regexp "\\|"
  2054. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
  2055. m s l res)
  2056. (if (string-match "^[ \t]*\\+-[-+]*\\+[ \t]*$" string)
  2057. string
  2058. (while (setq m (string-match re string))
  2059. (setq s (substring string 0 m)
  2060. l (match-string 0 string)
  2061. string (substring string (match-end 0)))
  2062. (push (org-html-do-expand s) res)
  2063. (push l res))
  2064. (push (org-html-do-expand string) res)
  2065. (apply 'concat (nreverse res)))))
  2066. (defun org-html-do-expand (s)
  2067. "Apply all active conversions to translate special ASCII to HTML."
  2068. (setq s (org-html-protect s))
  2069. (if org-export-html-expand
  2070. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  2071. (setq s (replace-match "<\\1>" t nil s))))
  2072. (if org-export-with-emphasize
  2073. (setq s (org-export-html-convert-emphasize s)))
  2074. (if org-export-with-special-strings
  2075. (setq s (org-export-html-convert-special-strings s)))
  2076. (if org-export-with-sub-superscripts
  2077. (setq s (org-export-html-convert-sub-super s)))
  2078. (if org-export-with-TeX-macros
  2079. (let ((start 0) wd rep)
  2080. (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
  2081. s start))
  2082. (if (get-text-property (match-beginning 0) 'org-protected s)
  2083. (setq start (match-end 0))
  2084. (setq wd (match-string 1 s))
  2085. (if (setq rep (org-entity-get-representation wd 'html))
  2086. (setq s (replace-match rep t t s))
  2087. (setq start (+ start (length wd))))))))
  2088. s)
  2089. (defun org-export-html-convert-special-strings (string)
  2090. "Convert special characters in STRING to HTML."
  2091. (let ((all org-export-html-special-string-regexps)
  2092. e a re rpl start)
  2093. (while (setq a (pop all))
  2094. (setq re (car a) rpl (cdr a) start 0)
  2095. (while (string-match re string start)
  2096. (if (get-text-property (match-beginning 0) 'org-protected string)
  2097. (setq start (match-end 0))
  2098. (setq string (replace-match rpl t nil string)))))
  2099. string))
  2100. (defun org-export-html-convert-sub-super (string)
  2101. "Convert sub- and superscripts in STRING to HTML."
  2102. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  2103. (while (string-match org-match-substring-regexp string s)
  2104. (cond
  2105. ((and requireb (match-end 8)) (setq s (match-end 2)))
  2106. ((get-text-property (match-beginning 2) 'org-protected string)
  2107. (setq s (match-end 2)))
  2108. (t
  2109. (setq s (match-end 1)
  2110. key (if (string= (match-string 2 string) "_") "sub" "sup")
  2111. c (or (match-string 8 string)
  2112. (match-string 6 string)
  2113. (match-string 5 string))
  2114. string (replace-match
  2115. (concat (match-string 1 string)
  2116. "<" key ">" c "</" key ">")
  2117. t t string)))))
  2118. (while (string-match "\\\\\\([_^]\\)" string)
  2119. (setq string (replace-match (match-string 1 string) t t string)))
  2120. string))
  2121. (defun org-export-html-convert-emphasize (string)
  2122. "Apply emphasis."
  2123. (let ((s 0) rpl)
  2124. (while (string-match org-emph-re string s)
  2125. (if (not (equal
  2126. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  2127. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  2128. (setq s (match-beginning 0)
  2129. rpl
  2130. (concat
  2131. (match-string 1 string)
  2132. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  2133. (match-string 4 string)
  2134. (nth 3 (assoc (match-string 3 string)
  2135. org-emphasis-alist))
  2136. (match-string 5 string))
  2137. string (replace-match rpl t t string)
  2138. s (+ s (- (length rpl) 2)))
  2139. (setq s (1+ s))))
  2140. string))
  2141. (defun org-open-par ()
  2142. "Insert <p>, but first close previous paragraph if any."
  2143. (org-close-par-maybe)
  2144. (insert "\n<p>")
  2145. (setq org-par-open t))
  2146. (defun org-close-par-maybe ()
  2147. "Close paragraph if there is one open."
  2148. (when org-par-open
  2149. (insert "</p>")
  2150. (setq org-par-open nil)))
  2151. (defun org-close-li (&optional type)
  2152. "Close <li> if necessary."
  2153. (org-close-par-maybe)
  2154. (insert (if (equal type "d") "</dd>\n" "</li>\n")))
  2155. (defvar body-only) ; dynamically scoped into this.
  2156. (defun org-html-level-start (level title umax with-toc head-count)
  2157. "Insert a new level in HTML export.
  2158. When TITLE is nil, just close all open levels."
  2159. (org-close-par-maybe)
  2160. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  2161. (extra-targets (and target
  2162. (assoc target org-export-target-aliases)))
  2163. (extra-class (and title (org-get-text-property-any 0 'html-container-class title)))
  2164. (preferred (and target
  2165. (cdr (assoc target org-export-preferred-target-alist))))
  2166. (l org-level-max)
  2167. snumber snu href suffix)
  2168. (setq extra-targets (remove (or preferred target) extra-targets))
  2169. (setq extra-targets
  2170. (mapconcat (lambda (x)
  2171. (setq x (org-solidify-link-text
  2172. (if (org-uuidgen-p x) (concat "ID-" x) x)))
  2173. (format "<a name=\"%s\" id=\"%s\"></a>"
  2174. x x))
  2175. extra-targets
  2176. ""))
  2177. (while (>= l level)
  2178. (if (aref org-levels-open (1- l))
  2179. (progn
  2180. (org-html-level-close l umax)
  2181. (aset org-levels-open (1- l) nil)))
  2182. (setq l (1- l)))
  2183. (when title
  2184. ;; If title is nil, this means this function is called to close
  2185. ;; all levels, so the rest is done only if title is given
  2186. (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  2187. (setq title (replace-match
  2188. (if org-export-with-tags
  2189. (save-match-data
  2190. (concat
  2191. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  2192. (mapconcat
  2193. (lambda (x)
  2194. (format "<span class=\"%s\">%s</span>"
  2195. (org-export-html-get-tag-class-name x)
  2196. x))
  2197. (org-split-string (match-string 1 title) ":")
  2198. "&nbsp;")
  2199. "</span>"))
  2200. "")
  2201. t t title)))
  2202. (if (> level umax)
  2203. (progn
  2204. (if (aref org-levels-open (1- level))
  2205. (progn
  2206. (org-close-li)
  2207. (if target
  2208. (insert (format "<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
  2209. extra-targets title "<br/>\n")
  2210. (insert "<li>" title "<br/>\n")))
  2211. (aset org-levels-open (1- level) t)
  2212. (org-close-par-maybe)
  2213. (if target
  2214. (insert (format "<ul>\n<li id=\"%s\">" (org-solidify-link-text (or preferred target)))
  2215. extra-targets title "<br/>\n")
  2216. (insert "<ul>\n<li>" title "<br/>\n"))))
  2217. (aset org-levels-open (1- level) t)
  2218. (setq snumber (org-section-number level)
  2219. snu (replace-regexp-in-string "\\." "_" snumber))
  2220. (setq level (+ level org-export-html-toplevel-hlevel -1))
  2221. (if (and org-export-with-section-numbers (not body-only))
  2222. (setq title (concat
  2223. (format "<span class=\"section-number-%d\">%s</span>"
  2224. level snumber)
  2225. " " title)))
  2226. (unless (= head-count 1) (insert "\n</div>\n"))
  2227. (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist)))
  2228. (setq suffix (org-solidify-link-text (or href snu)))
  2229. (setq href (org-solidify-link-text (or href (concat "sec-" snu))))
  2230. (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"
  2231. suffix level (if extra-class (concat " " extra-class) "")
  2232. level href
  2233. extra-targets
  2234. title level level suffix))
  2235. (org-open-par)))))
  2236. (defun org-export-html-get-tag-class-name (tag)
  2237. "Turn tag into a valid class name.
  2238. Replaces invalid characters with \"_\" and then prepends a prefix."
  2239. (save-match-data
  2240. (while (string-match "[^a-zA-Z0-9_]" tag)
  2241. (setq tag (replace-match "_" t t tag))))
  2242. (concat org-export-html-tag-class-prefix tag))
  2243. (defun org-export-html-get-todo-kwd-class-name (kwd)
  2244. "Turn todo keyword into a valid class name.
  2245. Replaces invalid characters with \"_\" and then prepends a prefix."
  2246. (save-match-data
  2247. (while (string-match "[^a-zA-Z0-9_]" kwd)
  2248. (setq kwd (replace-match "_" t t kwd))))
  2249. (concat org-export-html-todo-kwd-class-prefix kwd))
  2250. (defun org-html-level-close (level max-outline-level)
  2251. "Terminate one level in HTML export."
  2252. (if (<= level max-outline-level)
  2253. (insert "</div>\n")
  2254. (org-close-li)
  2255. (insert "</ul>\n")))
  2256. (defun org-html-export-list-line (line pos struct prevs)
  2257. "Insert list syntax in export buffer. Return LINE, maybe modified.
  2258. POS is the item position or line position the line had before
  2259. modifications to buffer. STRUCT is the list structure. PREVS is
  2260. the alist of previous items."
  2261. (let* ((get-type
  2262. (function
  2263. ;; Translate type of list containing POS to "d", "o" or
  2264. ;; "u".
  2265. (lambda (pos struct prevs)
  2266. (let ((type (org-list-get-list-type pos struct prevs)))
  2267. (cond
  2268. ((eq 'ordered type) "o")
  2269. ((eq 'descriptive type) "d")
  2270. (t "u"))))))
  2271. (get-closings
  2272. (function
  2273. ;; Return list of all items and sublists ending at POS, in
  2274. ;; reverse order.
  2275. (lambda (pos)
  2276. (let (out)
  2277. (catch 'exit
  2278. (mapc (lambda (e)
  2279. (let ((end (nth 6 e))
  2280. (item (car e)))
  2281. (cond
  2282. ((= end pos) (push item out))
  2283. ((>= item pos) (throw 'exit nil)))))
  2284. struct))
  2285. out)))))
  2286. ;; First close any previous item, or list, ending at POS.
  2287. (mapc (lambda (e)
  2288. (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
  2289. (first-item (org-list-get-list-begin e struct prevs))
  2290. (type (funcall get-type first-item struct prevs)))
  2291. (org-close-par-maybe)
  2292. ;; Ending for every item
  2293. (org-close-li type)
  2294. ;; We're ending last item of the list: end list.
  2295. (when lastp
  2296. (insert (format "</%sl>\n" type))
  2297. (org-open-par))))
  2298. (funcall get-closings pos))
  2299. (cond
  2300. ;; At an item: insert appropriate tags in export buffer.
  2301. ((assq pos struct)
  2302. (string-match
  2303. (concat "[ \t]*\\(\\S-+[ \t]*\\)"
  2304. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
  2305. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  2306. "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?"
  2307. "\\(.*\\)") line)
  2308. (let* ((checkbox (match-string 3 line))
  2309. (desc-tag (or (match-string 4 line) "???"))
  2310. (body (or (match-string 5 line) ""))
  2311. (list-beg (org-list-get-list-begin pos struct prevs))
  2312. (firstp (= list-beg pos))
  2313. ;; Always refer to first item to determine list type, in
  2314. ;; case list is ill-formed.
  2315. (type (funcall get-type list-beg struct prevs))
  2316. (counter (let ((count-tmp (org-list-get-counter pos struct)))
  2317. (cond
  2318. ((not count-tmp) nil)
  2319. ((string-match "[A-Za-z]" count-tmp)
  2320. (- (string-to-char (upcase count-tmp)) 64))
  2321. ((string-match "[0-9]+" count-tmp)
  2322. count-tmp)))))
  2323. (when firstp
  2324. (org-close-par-maybe)
  2325. (insert (format "<%sl>\n" type)))
  2326. (insert (cond
  2327. ((equal type "d")
  2328. (format "<dt>%s</dt><dd>" desc-tag))
  2329. ((and (equal type "o") counter)
  2330. (format "<li value=\"%s\">" counter))
  2331. (t "<li>")))
  2332. ;; If line had a checkbox, some additional modification is required.
  2333. (when checkbox
  2334. (setq body
  2335. (concat
  2336. (cond
  2337. ((string-match "X" checkbox) "<code>[X]</code> ")
  2338. ((string-match " " checkbox) "<code>[&nbsp;]</code> ")
  2339. (t "<code>[-]</code> "))
  2340. body)))
  2341. ;; Return modified line
  2342. body))
  2343. ;; At a list ender: go to next line (side-effects only).
  2344. ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
  2345. ;; Not at an item: return line unchanged (side-effects only).
  2346. (t line))))
  2347. (provide 'org-html)
  2348. ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
  2349. ;;; org-html.el ends here