org-html.el 88 KB

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