org-html.el 75 KB

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