org-html.el 89 KB

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