org-html.el 87 KB

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