org-html.el 90 KB

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