org-html.el 89 KB

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