org-html.el 91 KB

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