org-html.el 87 KB

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