org-html.el 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  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.33trans
  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 '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 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 during HTML export, after blockquote, verse, center are done.")
  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. ;;;###autoload
  391. (defun org-export-as-html-batch ()
  392. "Call `org-export-as-html', may be used in batch processing as
  393. emacs --batch
  394. --load=$HOME/lib/emacs/org.el
  395. --eval \"(setq org-export-headline-levels 2)\"
  396. --visit=MyFile --funcall org-export-as-html-batch"
  397. (org-export-as-html org-export-headline-levels 'hidden))
  398. ;;;###autoload
  399. (defun org-export-as-html-to-buffer (arg)
  400. "Call `org-export-as-html` with output to a temporary buffer.
  401. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  402. (interactive "P")
  403. (org-export-as-html arg nil nil "*Org HTML Export*")
  404. (when org-export-show-temporary-export-buffer
  405. (switch-to-buffer-other-window "*Org HTML Export*")))
  406. ;;;###autoload
  407. (defun org-replace-region-by-html (beg end)
  408. "Assume the current region has org-mode syntax, and convert it to HTML.
  409. This can be used in any buffer. For example, you could write an
  410. itemized list in org-mode syntax in an HTML buffer and then use this
  411. command to convert it."
  412. (interactive "r")
  413. (let (reg html buf pop-up-frames)
  414. (save-window-excursion
  415. (if (org-mode-p)
  416. (setq html (org-export-region-as-html
  417. beg end t 'string))
  418. (setq reg (buffer-substring beg end)
  419. buf (get-buffer-create "*Org tmp*"))
  420. (with-current-buffer buf
  421. (erase-buffer)
  422. (insert reg)
  423. (org-mode)
  424. (setq html (org-export-region-as-html
  425. (point-min) (point-max) t 'string)))
  426. (kill-buffer buf)))
  427. (delete-region beg end)
  428. (insert html)))
  429. ;;;###autoload
  430. (defun org-export-region-as-html (beg end &optional body-only buffer)
  431. "Convert region from BEG to END in org-mode buffer to HTML.
  432. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  433. contents, and only produce the region of converted text, useful for
  434. cut-and-paste operations.
  435. If BUFFER is a buffer or a string, use/create that buffer as a target
  436. of the converted HTML. If BUFFER is the symbol `string', return the
  437. produced HTML as a string and leave not buffer behind. For example,
  438. a Lisp program could call this function in the following way:
  439. (setq html (org-export-region-as-html beg end t 'string))
  440. When called interactively, the output buffer is selected, and shown
  441. in a window. A non-interactive call will only return the buffer."
  442. (interactive "r\nP")
  443. (when (interactive-p)
  444. (setq buffer "*Org HTML Export*"))
  445. (let ((transient-mark-mode t) (zmacs-regions t)
  446. ext-plist rtn)
  447. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  448. (goto-char end)
  449. (set-mark (point)) ;; to activate the region
  450. (goto-char beg)
  451. (setq rtn (org-export-as-html
  452. nil nil ext-plist
  453. buffer body-only))
  454. (if (fboundp 'deactivate-mark) (deactivate-mark))
  455. (if (and (interactive-p) (bufferp rtn))
  456. (switch-to-buffer-other-window rtn)
  457. rtn)))
  458. (defvar html-table-tag nil) ; dynamically scoped into this.
  459. (defvar org-par-open nil)
  460. ;;;###autoload
  461. (defun org-export-as-html (arg &optional hidden ext-plist
  462. to-buffer body-only pub-dir)
  463. "Export the outline as a pretty HTML file.
  464. If there is an active region, export only the region. The prefix
  465. ARG specifies how many levels of the outline should become
  466. headlines. The default is 3. Lower levels will become bulleted
  467. lists. HIDDEN is obsolete and does nothing.
  468. EXT-PLIST is a property list with external parameters overriding
  469. org-mode's default settings, but still inferior to file-local
  470. settings. When TO-BUFFER is non-nil, create a buffer with that
  471. name and export to that buffer. If TO-BUFFER is the symbol
  472. `string', don't leave any buffer behind but just return the
  473. resulting HTML as a string. When BODY-ONLY is set, don't produce
  474. the file header and footer, simply return the content of
  475. <body>...</body>, without even the body tags themselves. When
  476. PUB-DIR is set, use this as the publishing directory."
  477. (interactive "P")
  478. ;; Make sure we have a file name when we need it.
  479. (when (and (not (or to-buffer body-only))
  480. (not buffer-file-name))
  481. (if (buffer-base-buffer)
  482. (org-set-local 'buffer-file-name
  483. (with-current-buffer (buffer-base-buffer)
  484. buffer-file-name))
  485. (error "Need a file name to be able to export")))
  486. (message "Exporting...")
  487. (setq-default org-todo-line-regexp org-todo-line-regexp)
  488. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  489. (setq-default org-done-keywords org-done-keywords)
  490. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  491. (let* ((opt-plist
  492. (org-export-process-option-filters
  493. (org-combine-plists (org-default-export-plist)
  494. ext-plist
  495. (org-infile-export-plist))))
  496. (body-only (or body-only (plist-get opt-plist :body-only)))
  497. (style (concat (if (plist-get opt-plist :style-include-default)
  498. org-export-html-style-default)
  499. (plist-get opt-plist :style)
  500. (plist-get opt-plist :style-extra)
  501. "\n"
  502. (if (plist-get opt-plist :style-include-scripts)
  503. org-export-html-scripts)))
  504. (html-extension (plist-get opt-plist :html-extension))
  505. (link-validate (plist-get opt-plist :link-validation-function))
  506. valid thetoc have-headings first-heading-pos
  507. (odd org-odd-levels-only)
  508. (region-p (org-region-active-p))
  509. (rbeg (and region-p (region-beginning)))
  510. (rend (and region-p (region-end)))
  511. (subtree-p
  512. (if (plist-get opt-plist :ignore-subtree-p)
  513. nil
  514. (when region-p
  515. (save-excursion
  516. (goto-char rbeg)
  517. (and (org-at-heading-p)
  518. (>= (org-end-of-subtree t t) rend))))))
  519. (level-offset (if subtree-p
  520. (save-excursion
  521. (goto-char rbeg)
  522. (+ (funcall outline-level)
  523. (if org-odd-levels-only 1 0)))
  524. 0))
  525. (opt-plist (setq org-export-opt-plist
  526. (if subtree-p
  527. (org-export-add-subtree-options opt-plist rbeg)
  528. opt-plist)))
  529. ;; The following two are dynamically scoped into other
  530. ;; routines below.
  531. (org-current-export-dir
  532. (or pub-dir (org-export-directory :html opt-plist)))
  533. (org-current-export-file buffer-file-name)
  534. (level 0) (line "") (origline "") txt todo
  535. (umax nil)
  536. (umax-toc nil)
  537. (filename (if to-buffer nil
  538. (expand-file-name
  539. (concat
  540. (file-name-sans-extension
  541. (or (and subtree-p
  542. (org-entry-get (region-beginning)
  543. "EXPORT_FILE_NAME" t))
  544. (file-name-nondirectory buffer-file-name)))
  545. "." html-extension)
  546. (file-name-as-directory
  547. (or pub-dir (org-export-directory :html opt-plist))))))
  548. (current-dir (if buffer-file-name
  549. (file-name-directory buffer-file-name)
  550. default-directory))
  551. (buffer (if to-buffer
  552. (cond
  553. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  554. (t (get-buffer-create to-buffer)))
  555. (find-file-noselect filename)))
  556. (org-levels-open (make-vector org-level-max nil))
  557. (date (plist-get opt-plist :date))
  558. (author (plist-get opt-plist :author))
  559. (title (or (and subtree-p (org-export-get-title-from-subtree))
  560. (plist-get opt-plist :title)
  561. (and (not
  562. (plist-get opt-plist :skip-before-1st-heading))
  563. (org-export-grab-title-from-buffer))
  564. (and buffer-file-name
  565. (file-name-sans-extension
  566. (file-name-nondirectory buffer-file-name)))
  567. "UNTITLED"))
  568. (link-up (and (plist-get opt-plist :link-up)
  569. (string-match "\\S-" (plist-get opt-plist :link-up))
  570. (plist-get opt-plist :link-up)))
  571. (link-home (and (plist-get opt-plist :link-home)
  572. (string-match "\\S-" (plist-get opt-plist :link-home))
  573. (plist-get opt-plist :link-home)))
  574. (dummy (setq opt-plist (plist-put opt-plist :title title)))
  575. (html-table-tag (plist-get opt-plist :html-table-tag))
  576. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  577. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  578. (inquote nil)
  579. (infixed nil)
  580. (inverse nil)
  581. (in-local-list nil)
  582. (local-list-type nil)
  583. (local-list-indent nil)
  584. (llt org-plain-list-ordered-item-terminator)
  585. (email (plist-get opt-plist :email))
  586. (language (plist-get opt-plist :language))
  587. (keywords (plist-get opt-plist :keywords))
  588. (description (plist-get opt-plist :description))
  589. (lang-words nil)
  590. (head-count 0) cnt
  591. (start 0)
  592. (coding-system (and (boundp 'buffer-file-coding-system)
  593. buffer-file-coding-system))
  594. (coding-system-for-write (or org-export-html-coding-system
  595. coding-system))
  596. (save-buffer-coding-system (or org-export-html-coding-system
  597. coding-system))
  598. (charset (and coding-system-for-write
  599. (fboundp 'coding-system-get)
  600. (coding-system-get coding-system-for-write
  601. 'mime-charset)))
  602. (region
  603. (buffer-substring
  604. (if region-p (region-beginning) (point-min))
  605. (if region-p (region-end) (point-max))))
  606. (lines
  607. (org-split-string
  608. (org-export-preprocess-string
  609. region
  610. :emph-multiline t
  611. :for-html t
  612. :skip-before-1st-heading
  613. (plist-get opt-plist :skip-before-1st-heading)
  614. :drawers (plist-get opt-plist :drawers)
  615. :todo-keywords (plist-get opt-plist :todo-keywords)
  616. :tags (plist-get opt-plist :tags)
  617. :priority (plist-get opt-plist :priority)
  618. :footnotes (plist-get opt-plist :footnotes)
  619. :timestamps (plist-get opt-plist :timestamps)
  620. :archived-trees
  621. (plist-get opt-plist :archived-trees)
  622. :select-tags (plist-get opt-plist :select-tags)
  623. :exclude-tags (plist-get opt-plist :exclude-tags)
  624. :add-text
  625. (plist-get opt-plist :text)
  626. :LaTeX-fragments
  627. (plist-get opt-plist :LaTeX-fragments))
  628. "[\r\n]"))
  629. table-open type
  630. table-buffer table-orig-buffer
  631. ind item-type starter didclose
  632. rpl path attr desc descp desc1 desc2 link
  633. snumber fnc item-tag
  634. footnotes footref-seen
  635. id-file href
  636. )
  637. (let ((inhibit-read-only t))
  638. (org-unmodified
  639. (remove-text-properties (point-min) (point-max)
  640. '(:org-license-to-kill t))))
  641. (message "Exporting...")
  642. (setq org-min-level (org-get-min-level lines level-offset))
  643. (setq org-last-level org-min-level)
  644. (org-init-section-numbers)
  645. (cond
  646. ((and date (string-match "%" date))
  647. (setq date (format-time-string date)))
  648. (date)
  649. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  650. ;; Get the language-dependent settings
  651. (setq lang-words (or (assoc language org-export-language-setup)
  652. (assoc "en" org-export-language-setup)))
  653. ;; Switch to the output buffer
  654. (set-buffer buffer)
  655. (let ((inhibit-read-only t)) (erase-buffer))
  656. (fundamental-mode)
  657. (org-install-letbind)
  658. (and (fboundp 'set-buffer-file-coding-system)
  659. (set-buffer-file-coding-system coding-system-for-write))
  660. (let ((case-fold-search nil)
  661. (org-odd-levels-only odd))
  662. ;; create local variables for all options, to make sure all called
  663. ;; functions get the correct information
  664. (mapc (lambda (x)
  665. (set (make-local-variable (nth 2 x))
  666. (plist-get opt-plist (car x))))
  667. org-export-plist-vars)
  668. (setq umax (if arg (prefix-numeric-value arg)
  669. org-export-headline-levels))
  670. (setq umax-toc (if (integerp org-export-with-toc)
  671. (min org-export-with-toc umax)
  672. umax))
  673. (unless body-only
  674. ;; File header
  675. (insert (format
  676. "%s
  677. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  678. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  679. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  680. lang=\"%s\" xml:lang=\"%s\">
  681. <head>
  682. %s
  683. <title>%s</title>
  684. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  685. <meta name=\"generator\" content=\"Org-mode\"/>
  686. <meta name=\"generated\" content=\"%s\"/>
  687. <meta name=\"author\" content=\"%s\"/>
  688. <meta name=\"description\" content=\"%s\"/>
  689. <meta name=\"keywords\" content=\"%s\"/>
  690. %s
  691. </head>
  692. <body>
  693. <div id=\"content\">
  694. "
  695. (format
  696. (or (and (stringp org-export-html-xml-declaration)
  697. org-export-html-xml-declaration)
  698. (cdr (assoc html-extension org-export-html-xml-declaration))
  699. (cdr (assoc "html" org-export-html-xml-declaration))
  700. "")
  701. (or charset "iso-8859-1"))
  702. language language
  703. (if (or link-up link-home)
  704. (concat
  705. (format org-export-html-home/up-format
  706. (or link-up link-home)
  707. (or link-home link-up))
  708. "\n")
  709. "")
  710. (org-html-expand title)
  711. (or charset "iso-8859-1")
  712. date author description keywords
  713. style))
  714. (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
  715. (when (plist-get opt-plist :auto-preamble)
  716. (if title (insert (format org-export-html-title-format
  717. (org-html-expand title))))))
  718. (if (and org-export-with-toc (not body-only))
  719. (progn
  720. (push (format "<h%d>%s</h%d>\n"
  721. org-export-html-toplevel-hlevel
  722. (nth 3 lang-words)
  723. org-export-html-toplevel-hlevel)
  724. thetoc)
  725. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  726. (push "<ul>\n<li>" thetoc)
  727. (setq lines
  728. (mapcar '(lambda (line)
  729. (if (string-match org-todo-line-regexp line)
  730. ;; This is a headline
  731. (progn
  732. (setq have-headings t)
  733. (setq level (- (match-end 1) (match-beginning 1)
  734. level-offset)
  735. level (org-tr-level level)
  736. txt (save-match-data
  737. (org-html-expand
  738. (org-export-cleanup-toc-line
  739. (match-string 3 line))))
  740. todo
  741. (or (and org-export-mark-todo-in-toc
  742. (match-beginning 2)
  743. (not (member (match-string 2 line)
  744. org-done-keywords)))
  745. ; TODO, not DONE
  746. (and org-export-mark-todo-in-toc
  747. (= level umax-toc)
  748. (org-search-todo-below
  749. line lines level))))
  750. (if (string-match
  751. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  752. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  753. (if (string-match quote-re0 txt)
  754. (setq txt (replace-match "" t t txt)))
  755. (setq snumber (org-section-number level))
  756. (if org-export-with-section-numbers
  757. (setq txt (concat snumber " " txt)))
  758. (if (<= level (max umax umax-toc))
  759. (setq head-count (+ head-count 1)))
  760. (if (<= level umax-toc)
  761. (progn
  762. (if (> level org-last-level)
  763. (progn
  764. (setq cnt (- level org-last-level))
  765. (while (>= (setq cnt (1- cnt)) 0)
  766. (push "\n<ul>\n<li>" thetoc))
  767. (push "\n" thetoc)))
  768. (if (< level org-last-level)
  769. (progn
  770. (setq cnt (- org-last-level level))
  771. (while (>= (setq cnt (1- cnt)) 0)
  772. (push "</li>\n</ul>" thetoc))
  773. (push "\n" thetoc)))
  774. ;; Check for targets
  775. (while (string-match org-any-target-regexp line)
  776. (setq line (replace-match
  777. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  778. t t line)))
  779. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  780. (setq txt (replace-match "" t t txt)))
  781. (setq href (format "sec-%s" snumber))
  782. (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
  783. (push
  784. (format
  785. (if todo
  786. "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
  787. "</li>\n<li><a href=\"#%s\">%s</a>")
  788. href txt) thetoc)
  789. (setq org-last-level level))
  790. )))
  791. line)
  792. lines))
  793. (while (> org-last-level (1- org-min-level))
  794. (setq org-last-level (1- org-last-level))
  795. (push "</li>\n</ul>\n" thetoc))
  796. (push "</div>\n" thetoc)
  797. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  798. (setq head-count 0)
  799. (org-init-section-numbers)
  800. (org-open-par)
  801. (while (setq line (pop lines) origline line)
  802. (catch 'nextline
  803. ;; end of quote section?
  804. (when (and inquote (string-match "^\\*+ " line))
  805. (insert "</pre>\n")
  806. (org-open-par)
  807. (setq inquote nil))
  808. ;; inside a quote section?
  809. (when inquote
  810. (insert (org-html-protect line) "\n")
  811. (throw 'nextline nil))
  812. ;; Fixed-width, verbatim lines (examples)
  813. (when (and org-export-with-fixed-width
  814. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  815. (when (not infixed)
  816. (setq infixed t)
  817. (org-close-par-maybe)
  818. (insert "<pre class=\"example\">\n"))
  819. (insert (org-html-protect (match-string 3 line)) "\n")
  820. (when (or (not lines)
  821. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  822. (car lines))))
  823. (setq infixed nil)
  824. (insert "</pre>\n")
  825. (org-open-par))
  826. (throw 'nextline nil))
  827. (org-export-html-close-lists-maybe line)
  828. ;; Protected HTML
  829. (when (get-text-property 0 'org-protected line)
  830. (let (par (ind (get-text-property 0 'original-indentation line)))
  831. (when (re-search-backward
  832. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  833. (setq par (match-string 1))
  834. (replace-match "\\2\n"))
  835. (insert line "\n")
  836. (while (and lines
  837. (or (= (length (car lines)) 0)
  838. (not ind)
  839. (equal ind (get-text-property 0 'original-indentation (car lines))))
  840. (or (= (length (car lines)) 0)
  841. (get-text-property 0 'org-protected (car lines))))
  842. (insert (pop lines) "\n"))
  843. (and par (insert "<p>\n")))
  844. (throw 'nextline nil))
  845. ;; Blockquotes, verse, and center
  846. (when (equal "ORG-BLOCKQUOTE-START" line)
  847. (org-close-par-maybe)
  848. (insert "<blockquote>\n")
  849. (org-open-par)
  850. (throw 'nextline nil))
  851. (when (equal "ORG-BLOCKQUOTE-END" line)
  852. (org-close-par-maybe)
  853. (insert "\n</blockquote>\n")
  854. (org-open-par)
  855. (throw 'nextline nil))
  856. (when (equal "ORG-VERSE-START" line)
  857. (org-close-par-maybe)
  858. (insert "\n<p class=\"verse\">\n")
  859. (setq inverse t)
  860. (throw 'nextline nil))
  861. (when (equal "ORG-VERSE-END" line)
  862. (insert "</p>\n")
  863. (org-open-par)
  864. (setq inverse nil)
  865. (throw 'nextline nil))
  866. (when (equal "ORG-CENTER-START" line)
  867. (org-close-par-maybe)
  868. (insert "\n<div style=\"text-align: center\">")
  869. (org-open-par)
  870. (throw 'nextline nil))
  871. (when (equal "ORG-CENTER-END" line)
  872. (org-close-par-maybe)
  873. (insert "\n</div>")
  874. (org-open-par)
  875. (throw 'nextline nil))
  876. (run-hooks 'org-export-html-after-blockquotes-hook)
  877. (when inverse
  878. (let ((i (org-get-string-indentation line)))
  879. (if (> i 0)
  880. (setq line (concat (mapconcat 'identity
  881. (make-list (* 2 i) "\\nbsp") "")
  882. " " (org-trim line))))
  883. (unless (string-match "\\\\\\\\[ \t]*$" line)
  884. (setq line (concat line "\\\\")))))
  885. ;; make targets to anchors
  886. (setq start 0)
  887. (while (string-match
  888. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  889. (cond
  890. ((get-text-property (match-beginning 1) 'org-protected line)
  891. (setq start (match-end 1)))
  892. ((match-end 2)
  893. (setq line (replace-match
  894. (format
  895. "@<a name=\"%s\" id=\"%s\">@</a>"
  896. (org-solidify-link-text (match-string 1 line))
  897. (org-solidify-link-text (match-string 1 line)))
  898. t t line)))
  899. ((and org-export-with-toc (equal (string-to-char line) ?*))
  900. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  901. (setq line (replace-match
  902. (concat "@<span class=\"target\">"
  903. (match-string 1 line) "@</span> ")
  904. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  905. t t line)))
  906. (t
  907. (setq line (replace-match
  908. (concat "@<a name=\""
  909. (org-solidify-link-text (match-string 1 line))
  910. "\" class=\"target\">" (match-string 1 line)
  911. "@</a> ")
  912. t t line)))))
  913. (setq line (org-html-handle-time-stamps line))
  914. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  915. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  916. ;; Also handle sub_superscripts and checkboxes
  917. (or (string-match org-table-hline-regexp line)
  918. (setq line (org-html-expand line)))
  919. ;; Format the links
  920. (setq start 0)
  921. (while (string-match org-bracket-link-analytic-regexp++ line start)
  922. (setq start (match-beginning 0))
  923. (setq path (save-match-data (org-link-unescape
  924. (match-string 3 line))))
  925. (setq type (cond
  926. ((match-end 2) (match-string 2 line))
  927. ((save-match-data
  928. (or (file-name-absolute-p path)
  929. (string-match "^\\.\\.?/" path)))
  930. "file")
  931. (t "internal")))
  932. (setq path (org-extract-attributes (org-link-unescape path)))
  933. (setq attr (get-text-property 0 'org-attributes path))
  934. (setq desc1 (if (match-end 5) (match-string 5 line))
  935. desc2 (if (match-end 2) (concat type ":" path) path)
  936. descp (and desc1 (not (equal desc1 desc2)))
  937. desc (or desc1 desc2))
  938. ;; Make an image out of the description if that is so wanted
  939. (when (and descp (org-file-image-p
  940. desc org-export-html-inline-image-extensions))
  941. (save-match-data
  942. (if (string-match "^file:" desc)
  943. (setq desc (substring desc (match-end 0)))))
  944. (setq desc (org-add-props
  945. (concat "<img src=\"" desc "\"/>")
  946. '(org-protected t))))
  947. ;; FIXME: do we need to unescape here somewhere?
  948. (cond
  949. ((equal type "internal")
  950. (setq rpl
  951. (concat
  952. "<a href=\""
  953. (if (= (string-to-char path) ?#) "" "#")
  954. (org-solidify-link-text
  955. (save-match-data (org-link-unescape path)) nil)
  956. "\"" attr ">"
  957. (org-export-html-format-desc desc)
  958. "</a>")))
  959. ((and (equal type "id")
  960. (setq id-file (org-id-find-id-file path)))
  961. ;; This is an id: link to another file (if it was the same file,
  962. ;; it would have become an internal link...)
  963. (save-match-data
  964. (setq id-file (file-relative-name
  965. id-file (file-name-directory org-current-export-file)))
  966. (setq id-file (concat (file-name-sans-extension id-file)
  967. "." html-extension))
  968. (setq rpl (concat "<a href=\"" id-file "#"
  969. (if (org-uuidgen-p path) "ID-")
  970. path "\""
  971. attr ">"
  972. (org-export-html-format-desc desc)
  973. "</a>"))))
  974. ((member type '("http" "https"))
  975. ;; standard URL, just check if we need to inline an image
  976. (if (and (or (eq t org-export-html-inline-images)
  977. (and org-export-html-inline-images (not descp)))
  978. (org-file-image-p
  979. path org-export-html-inline-image-extensions))
  980. (setq rpl (org-export-html-format-image
  981. (concat type ":" path) org-par-open))
  982. (setq link (concat type ":" path))
  983. (setq rpl (concat "<a href=\""
  984. (org-export-html-format-href link)
  985. "\"" attr ">"
  986. (org-export-html-format-desc desc)
  987. "</a>"))))
  988. ((member type '("ftp" "mailto" "news"))
  989. ;; standard URL
  990. (setq link (concat type ":" path))
  991. (setq rpl (concat "<a href=\""
  992. (org-export-html-format-href link)
  993. "\"" attr ">"
  994. (org-export-html-format-desc desc)
  995. "</a>")))
  996. ((string= type "coderef")
  997. (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
  998. path path path
  999. (format (org-export-get-coderef-format path (and descp desc))
  1000. (cdr (assoc path org-export-code-refs))))))
  1001. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  1002. ;; The link protocol has a function for format the link
  1003. (setq rpl
  1004. (save-match-data
  1005. (funcall fnc (org-link-unescape path) desc1 'html))))
  1006. ((string= type "file")
  1007. ;; FILE link
  1008. (let* ((filename path)
  1009. (abs-p (file-name-absolute-p filename))
  1010. thefile file-is-image-p search)
  1011. (save-match-data
  1012. (if (string-match "::\\(.*\\)" filename)
  1013. (setq search (match-string 1 filename)
  1014. filename (replace-match "" t nil filename)))
  1015. (setq valid
  1016. (if (functionp link-validate)
  1017. (funcall link-validate filename current-dir)
  1018. t))
  1019. (setq file-is-image-p
  1020. (org-file-image-p
  1021. filename org-export-html-inline-image-extensions))
  1022. (setq thefile (if abs-p (expand-file-name filename) filename))
  1023. (when (and org-export-html-link-org-files-as-html
  1024. (string-match "\\.org$" thefile))
  1025. (setq thefile (concat (substring thefile 0
  1026. (match-beginning 0))
  1027. "." html-extension))
  1028. (if (and search
  1029. ;; make sure this is can be used as target search
  1030. (not (string-match "^[0-9]*$" search))
  1031. (not (string-match "^\\*" search))
  1032. (not (string-match "^/.*/$" search)))
  1033. (setq thefile (concat thefile "#"
  1034. (org-solidify-link-text
  1035. (org-link-unescape search)))))
  1036. (when (string-match "^file:" desc)
  1037. (setq desc (replace-match "" t t desc))
  1038. (if (string-match "\\.org$" desc)
  1039. (setq desc (replace-match "" t t desc))))))
  1040. (setq rpl (if (and file-is-image-p
  1041. (or (eq t org-export-html-inline-images)
  1042. (and org-export-html-inline-images
  1043. (not descp))))
  1044. (progn
  1045. (message "image %s %s" thefile org-par-open)
  1046. (org-export-html-format-image thefile org-par-open))
  1047. (concat "<a href=\"" thefile "\"" attr ">"
  1048. (org-export-html-format-desc desc)
  1049. "</a>")))
  1050. (if (not valid) (setq rpl desc))))
  1051. (t
  1052. ;; just publish the path, as default
  1053. (setq rpl (concat "<i>&lt;" type ":"
  1054. (save-match-data (org-link-unescape path))
  1055. "&gt;</i>"))))
  1056. (setq line (replace-match rpl t t line)
  1057. start (+ start (length rpl))))
  1058. ;; TODO items
  1059. (if (and (string-match org-todo-line-regexp line)
  1060. (match-beginning 2))
  1061. (setq line
  1062. (concat (substring line 0 (match-beginning 2))
  1063. "<span class=\""
  1064. (if (member (match-string 2 line)
  1065. org-done-keywords)
  1066. "done" "todo")
  1067. " " (match-string 2 line)
  1068. "\"> " (org-export-html-get-todo-kwd-class-name
  1069. (match-string 2 line))
  1070. "</span>" (substring line (match-end 2)))))
  1071. ;; Does this contain a reference to a footnote?
  1072. (when org-export-with-footnotes
  1073. (setq start 0)
  1074. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  1075. (if (get-text-property (match-beginning 2) 'org-protected line)
  1076. (setq start (match-end 2))
  1077. (let ((n (match-string 2 line)) extra a)
  1078. (if (setq a (assoc n footref-seen))
  1079. (progn
  1080. (setcdr a (1+ (cdr a)))
  1081. (setq extra (format ".%d" (cdr a))))
  1082. (setq extra "")
  1083. (push (cons n 1) footref-seen))
  1084. (setq line
  1085. (replace-match
  1086. (format
  1087. (concat "%s"
  1088. (format org-export-html-footnote-format
  1089. "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
  1090. (or (match-string 1 line) "") n extra n n)
  1091. t t line))))))
  1092. (cond
  1093. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1094. ;; This is a headline
  1095. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1096. level-offset))
  1097. txt (match-string 2 line))
  1098. (if (string-match quote-re0 txt)
  1099. (setq txt (replace-match "" t t txt)))
  1100. (if (<= level (max umax umax-toc))
  1101. (setq head-count (+ head-count 1)))
  1102. (when in-local-list
  1103. ;; Close any local lists before inserting a new header line
  1104. (while local-list-type
  1105. (org-close-li (car local-list-type))
  1106. (insert (format "</%sl>\n" (car local-list-type)))
  1107. (pop local-list-type))
  1108. (setq local-list-indent nil
  1109. in-local-list nil))
  1110. (setq first-heading-pos (or first-heading-pos (point)))
  1111. (org-html-level-start level txt umax
  1112. (and org-export-with-toc (<= level umax))
  1113. head-count)
  1114. ;; QUOTES
  1115. (when (string-match quote-re line)
  1116. (org-close-par-maybe)
  1117. (insert "<pre>")
  1118. (setq inquote t)))
  1119. ((string-match "^[ \t]*- __+[ \t]*$" line)
  1120. ;; Explicit list closure
  1121. (when local-list-type
  1122. (let ((ind (org-get-indentation line)))
  1123. (while (and local-list-indent
  1124. (<= ind (car local-list-indent)))
  1125. (org-close-li (car local-list-type))
  1126. (insert (format "</%sl>\n" (car local-list-type)))
  1127. (pop local-list-type)
  1128. (pop local-list-indent))
  1129. (or local-list-indent (setq in-local-list nil))))
  1130. (throw 'nextline nil))
  1131. ((and org-export-with-tables
  1132. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1133. (when (not table-open)
  1134. ;; New table starts
  1135. (setq table-open t table-buffer nil table-orig-buffer nil))
  1136. ;; Accumulate lines
  1137. (setq table-buffer (cons line table-buffer)
  1138. table-orig-buffer (cons origline table-orig-buffer))
  1139. (when (or (not lines)
  1140. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1141. (car lines))))
  1142. (setq table-open nil
  1143. table-buffer (nreverse table-buffer)
  1144. table-orig-buffer (nreverse table-orig-buffer))
  1145. (org-close-par-maybe)
  1146. (insert (org-format-table-html table-buffer table-orig-buffer))))
  1147. (t
  1148. ;; Normal lines
  1149. (when (string-match
  1150. (cond
  1151. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1152. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1153. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1154. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  1155. line)
  1156. (setq ind (or (get-text-property 0 'original-indentation line)
  1157. (org-get-string-indentation line))
  1158. item-type (if (match-beginning 4) "o" "u")
  1159. starter (if (match-beginning 2)
  1160. (substring (match-string 2 line) 0 -1))
  1161. line (substring line (match-beginning 5))
  1162. item-tag nil)
  1163. (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
  1164. (setq item-type "d"
  1165. item-tag (match-string 1 line)
  1166. line (substring line (match-end 0))))
  1167. (when (and (not (equal item-type "d"))
  1168. (not (string-match "[^ \t]" line)))
  1169. ;; empty line. Pretend indentation is large.
  1170. (setq ind (if org-empty-line-terminates-plain-lists
  1171. 0
  1172. (1+ (or (car local-list-indent) 1)))))
  1173. (setq didclose nil)
  1174. (while (and in-local-list
  1175. (or (and (= ind (car local-list-indent))
  1176. (not starter))
  1177. (< ind (car local-list-indent))))
  1178. (setq didclose t)
  1179. (org-close-li (car local-list-type))
  1180. (insert (format "</%sl>\n" (car local-list-type)))
  1181. (pop local-list-type) (pop local-list-indent)
  1182. (setq in-local-list local-list-indent))
  1183. (cond
  1184. ((and starter
  1185. (or (not in-local-list)
  1186. (> ind (car local-list-indent))))
  1187. ;; Start new (level of) list
  1188. (org-close-par-maybe)
  1189. (insert (cond
  1190. ((equal item-type "u") "<ul>\n<li>\n")
  1191. ((equal item-type "o") "<ol>\n<li>\n")
  1192. ((equal item-type "d")
  1193. (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
  1194. (push item-type local-list-type)
  1195. (push ind local-list-indent)
  1196. (setq in-local-list t))
  1197. (starter
  1198. ;; continue current list
  1199. (org-close-li (car local-list-type))
  1200. (insert (cond
  1201. ((equal (car local-list-type) "d")
  1202. (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
  1203. (t "<li>\n"))))
  1204. (didclose
  1205. ;; we did close a list, normal text follows: need <p>
  1206. (org-open-par)))
  1207. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  1208. (setq line
  1209. (replace-match
  1210. (if (equal (match-string 1 line) "X")
  1211. "<b>[X]</b>"
  1212. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  1213. t t line))))
  1214. ;; Horizontal line
  1215. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1216. (if org-par-open
  1217. (insert "\n</p>\n<hr/>\n<p>\n")
  1218. (insert "\n<hr/>\n"))
  1219. (throw 'nextline nil))
  1220. ;; Empty lines start a new paragraph. If hand-formatted lists
  1221. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1222. ;; also start a new paragraph.
  1223. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  1224. ;; Is this the start of a footnote?
  1225. (when org-export-with-footnotes
  1226. (when (and (boundp 'footnote-section-tag-regexp)
  1227. (string-match (concat "^" footnote-section-tag-regexp)
  1228. line))
  1229. ;; ignore this line
  1230. (throw 'nextline nil))
  1231. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1232. (org-close-par-maybe)
  1233. (let ((n (match-string 1 line)))
  1234. (setq org-par-open t
  1235. line (replace-match
  1236. (format
  1237. (concat "<p class=\"footnote\">"
  1238. (format org-export-html-footnote-format
  1239. "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
  1240. n n n) t t line)))))
  1241. ;; Check if the line break needs to be conserved
  1242. (cond
  1243. ((string-match "\\\\\\\\[ \t]*$" line)
  1244. (setq line (replace-match "<br/>" t t line)))
  1245. (org-export-preserve-breaks
  1246. (setq line (concat line "<br/>"))))
  1247. ;; Check if a paragraph should be started
  1248. (let ((start 0))
  1249. (while (and org-par-open
  1250. (string-match "\\\\par\\>" line start))
  1251. ;; Leave a space in the </p> so that the footnote matcher
  1252. ;; does not see this.
  1253. (if (not (get-text-property (match-beginning 0)
  1254. 'org-protected line))
  1255. (setq line (replace-match "</p ><p >" t t line)))
  1256. (setq start (match-end 0))))
  1257. (insert line "\n")))))
  1258. ;; Properly close all local lists and other lists
  1259. (when inquote
  1260. (insert "</pre>\n")
  1261. (org-open-par))
  1262. (when in-local-list
  1263. ;; Close any local lists before inserting a new header line
  1264. (while local-list-type
  1265. (org-close-li (car local-list-type))
  1266. (insert (format "</%sl>\n" (car local-list-type)))
  1267. (pop local-list-type))
  1268. (setq local-list-indent nil
  1269. in-local-list nil))
  1270. (org-html-level-start 1 nil umax
  1271. (and org-export-with-toc (<= level umax))
  1272. head-count)
  1273. ;; the </div> to close the last text-... div.
  1274. (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
  1275. (save-excursion
  1276. (goto-char (point-min))
  1277. (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
  1278. (push (match-string 0) footnotes)
  1279. (replace-match "" t t)))
  1280. (when footnotes
  1281. (insert (format org-export-html-footnotes-section
  1282. (nth 4 lang-words)
  1283. (mapconcat 'identity (nreverse footnotes) "\n"))
  1284. "\n"))
  1285. (let ((bib (org-export-html-get-bibliography)))
  1286. (when bib
  1287. (insert "\n" bib "\n")))
  1288. (unless body-only
  1289. (when (plist-get opt-plist :auto-postamble)
  1290. (insert "<div id=\"postamble\">\n")
  1291. (when (and org-export-author-info author)
  1292. (insert "<p class=\"author\"> "
  1293. (nth 1 lang-words) ": " author "\n")
  1294. (when email
  1295. (if (listp (split-string email ",+ *"))
  1296. (mapc (lambda(e)
  1297. (insert "<a href=\"mailto:" e "\">&lt;"
  1298. e "&gt;</a>\n"))
  1299. (split-string email ",+ *"))
  1300. (insert "<a href=\"mailto:" email "\">&lt;"
  1301. email "&gt;</a>\n")))
  1302. (insert "</p>\n"))
  1303. (when (and date org-export-time-stamp-file)
  1304. (insert "<p class=\"date\"> "
  1305. (nth 2 lang-words) ": "
  1306. date "</p>\n"))
  1307. (when org-export-creator-info
  1308. (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
  1309. org-version emacs-major-version)))
  1310. (when org-export-html-validation-link
  1311. (insert org-export-html-validation-link "\n"))
  1312. (insert "</div>"))
  1313. (if org-export-html-with-timestamp
  1314. (insert org-export-html-html-helper-timestamp))
  1315. (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
  1316. (insert "\n</div>\n</body>\n</html>\n"))
  1317. (unless (plist-get opt-plist :buffer-will-be-killed)
  1318. (normal-mode)
  1319. (if (eq major-mode (default-value 'major-mode))
  1320. (html-mode)))
  1321. ;; insert the table of contents
  1322. (goto-char (point-min))
  1323. (when thetoc
  1324. (if (or (re-search-forward
  1325. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1326. (re-search-forward
  1327. "\\[TABLE-OF-CONTENTS\\]" nil t))
  1328. (progn
  1329. (goto-char (match-beginning 0))
  1330. (replace-match ""))
  1331. (goto-char first-heading-pos)
  1332. (when (looking-at "\\s-*</p>")
  1333. (goto-char (match-end 0))
  1334. (insert "\n")))
  1335. (insert "<div id=\"table-of-contents\">\n")
  1336. (mapc 'insert thetoc)
  1337. (insert "</div>\n"))
  1338. ;; remove empty paragraphs and lists
  1339. (goto-char (point-min))
  1340. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  1341. (replace-match ""))
  1342. (goto-char (point-min))
  1343. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  1344. (replace-match ""))
  1345. (goto-char (point-min))
  1346. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  1347. (replace-match ""))
  1348. ;; Convert whitespace place holders
  1349. (goto-char (point-min))
  1350. (let (beg end n)
  1351. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  1352. (setq n (get-text-property beg 'org-whitespace)
  1353. end (next-single-property-change beg 'org-whitespace))
  1354. (goto-char beg)
  1355. (delete-region beg end)
  1356. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  1357. (make-string n ?x)))))
  1358. (run-hooks 'org-export-html-final-hook)
  1359. (or to-buffer (save-buffer))
  1360. (goto-char (point-min))
  1361. (or (org-export-push-to-kill-ring "HTML")
  1362. (message "Exporting... done"))
  1363. (if (eq to-buffer 'string)
  1364. (prog1 (buffer-substring (point-min) (point-max))
  1365. (kill-buffer (current-buffer)))
  1366. (current-buffer)))))
  1367. (defun org-export-html-insert-plist-item (plist key &rest args)
  1368. (let ((item (plist-get plist key)))
  1369. (cond ((functionp item)
  1370. (apply item args))
  1371. (item
  1372. (insert item)))))
  1373. (defun org-export-html-format-href (s)
  1374. "Make sure the S is valid as a href reference in an XHTML document."
  1375. (save-match-data
  1376. (let ((start 0))
  1377. (while (string-match "&" s start)
  1378. (setq start (+ (match-beginning 0) 3)
  1379. s (replace-match "&amp;" t t s)))))
  1380. s)
  1381. (defun org-export-html-format-desc (s)
  1382. "Make sure the S is valid as a description in a link."
  1383. (if (and s (not (get-text-property 1 'org-protected s)))
  1384. (save-match-data
  1385. (org-html-do-expand s))
  1386. s))
  1387. (defun org-export-html-format-image (src par-open)
  1388. "Create image tag with source and attributes."
  1389. (save-match-data
  1390. (if (string-match "^ltxpng/" src)
  1391. (format "<img src=\"%s\"/>" src)
  1392. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1393. (attr (org-find-text-property-in-string 'org-attributes src))
  1394. (label (org-find-text-property-in-string 'org-label src)))
  1395. (concat
  1396. (if caption
  1397. (format "%s<div %sclass=\"figure\">
  1398. <p>"
  1399. (if org-par-open "</p>\n" "")
  1400. (if label (format "id=\"%s\" " label) "")))
  1401. (format "<img src=\"%s\"%s />"
  1402. src
  1403. (if (string-match "\\<alt=" (or attr ""))
  1404. (concat " " attr )
  1405. (concat " " attr " alt=\"" src "\"")))
  1406. (if caption
  1407. (format "</p>%s
  1408. </div>%s"
  1409. (concat "\n<p>" caption "</p>")
  1410. (if org-par-open "\n<p>" ""))))))))
  1411. (defun org-export-html-get-bibliography ()
  1412. "Find bibliography, cut it out and return it."
  1413. (catch 'exit
  1414. (let (beg end (cnt 1) bib)
  1415. (save-excursion
  1416. (goto-char (point-min))
  1417. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  1418. (setq beg (match-beginning 0))
  1419. (while (re-search-forward "</?div\\>" nil t)
  1420. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  1421. (when (= cnt 0)
  1422. (and (looking-at ">") (forward-char 1))
  1423. (setq bib (buffer-substring beg (point)))
  1424. (delete-region beg (point))
  1425. (throw 'exit bib))))
  1426. nil))))
  1427. (defvar org-table-number-regexp) ; defined in org-table.el
  1428. (defun org-format-table-html (lines olines)
  1429. "Find out which HTML converter to use and return the HTML code."
  1430. (if (stringp lines)
  1431. (setq lines (org-split-string lines "\n")))
  1432. (if (string-match "^[ \t]*|" (car lines))
  1433. ;; A normal org table
  1434. (org-format-org-table-html lines)
  1435. ;; Table made by table.el - test for spanning
  1436. (let* ((hlines (delq nil (mapcar
  1437. (lambda (x)
  1438. (if (string-match "^[ \t]*\\+-" x) x
  1439. nil))
  1440. lines)))
  1441. (first (car hlines))
  1442. (ll (and (string-match "\\S-+" first)
  1443. (match-string 0 first)))
  1444. (re (concat "^[ \t]*" (regexp-quote ll)))
  1445. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  1446. hlines))))
  1447. (if (and (not spanning)
  1448. (not org-export-prefer-native-exporter-for-tables))
  1449. ;; We can use my own converter with HTML conversions
  1450. (org-format-table-table-html lines)
  1451. ;; Need to use the code generator in table.el, with the original text.
  1452. (org-format-table-table-html-using-table-generate-source olines)))))
  1453. (defvar org-table-number-fraction) ; defined in org-table.el
  1454. (defun org-format-org-table-html (lines &optional splice)
  1455. "Format a table into HTML."
  1456. (require 'org-table)
  1457. ;; Get rid of hlines at beginning and end
  1458. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1459. (setq lines (nreverse lines))
  1460. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1461. (setq lines (nreverse lines))
  1462. (when org-export-table-remove-special-lines
  1463. ;; Check if the table has a marking column. If yes remove the
  1464. ;; column and the special lines
  1465. (setq lines (org-table-clean-before-export lines)))
  1466. (let* ((caption (or (get-text-property 0 'org-caption (car lines))
  1467. (get-text-property (or (next-single-property-change
  1468. 0 'org-caption (car lines))
  1469. 0)
  1470. 'org-caption (car lines))))
  1471. (attributes (or (get-text-property 0 'org-attributes (car lines))
  1472. (get-text-property (or (next-single-property-change
  1473. 0 'org-attributes (car lines))
  1474. 0)
  1475. 'org-attributes (car lines))))
  1476. (html-table-tag (org-export-splice-attributes
  1477. html-table-tag attributes))
  1478. (head (and org-export-highlight-first-table-line
  1479. (delq nil (mapcar
  1480. (lambda (x) (string-match "^[ \t]*|-" x))
  1481. (cdr lines)))))
  1482. (nline 0) fnum i
  1483. tbopen line fields html gr colgropen rowstart rowend)
  1484. (if splice (setq head nil))
  1485. (unless splice (push (if head "<thead>" "<tbody>") html))
  1486. (setq tbopen t)
  1487. (while (setq line (pop lines))
  1488. (catch 'next-line
  1489. (if (string-match "^[ \t]*|-" line)
  1490. (progn
  1491. (unless splice
  1492. (push (if head "</thead>" "</tbody>") html)
  1493. (if lines (push "<tbody>" html) (setq tbopen nil)))
  1494. (setq head nil) ;; head ends here, first time around
  1495. ;; ignore this line
  1496. (throw 'next-line t)))
  1497. ;; Break the line into fields
  1498. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1499. (unless fnum (setq fnum (make-vector (length fields) 0)))
  1500. (setq nline (1+ nline) i -1
  1501. rowstart (eval (car org-export-table-row-tags))
  1502. rowend (eval (cdr org-export-table-row-tags)))
  1503. (push (concat rowstart
  1504. (mapconcat
  1505. (lambda (x)
  1506. (setq i (1+ i))
  1507. (if (and (< i nline)
  1508. (string-match org-table-number-regexp x))
  1509. (incf (aref fnum i)))
  1510. (cond
  1511. (head
  1512. (concat
  1513. (format (car org-export-table-header-tags) "col")
  1514. x
  1515. (cdr org-export-table-header-tags)))
  1516. ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
  1517. (concat
  1518. (format (car org-export-table-header-tags) "row")
  1519. x
  1520. (cdr org-export-table-header-tags)))
  1521. (t
  1522. (concat (car org-export-table-data-tags) x
  1523. (cdr org-export-table-data-tags)))))
  1524. fields "")
  1525. rowend)
  1526. html)))
  1527. (unless splice (if tbopen (push "</tbody>" html)))
  1528. (unless splice (push "</table>\n" html))
  1529. (setq html (nreverse html))
  1530. (unless splice
  1531. ;; Put in col tags with the alignment (unfortunately often ignored...)
  1532. (unless (car org-table-colgroup-info)
  1533. (setq org-table-colgroup-info
  1534. (cons :start (cdr org-table-colgroup-info))))
  1535. (push (mapconcat
  1536. (lambda (x)
  1537. (setq gr (pop org-table-colgroup-info))
  1538. (format "%s<col align=\"%s\" />%s"
  1539. (if (memq gr '(:start :startend))
  1540. (prog1
  1541. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  1542. (setq colgropen t))
  1543. "")
  1544. (if (> (/ (float x) nline) org-table-number-fraction)
  1545. "right" "left")
  1546. (if (memq gr '(:end :startend))
  1547. (progn (setq colgropen nil) "</colgroup>")
  1548. "")))
  1549. fnum "")
  1550. html)
  1551. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  1552. ;; Since the output of HTML table formatter can also be used in
  1553. ;; DocBook document, we want to always include the caption to make
  1554. ;; DocBook XML file valid.
  1555. (push (format "<caption>%s</caption>" (or caption "")) html)
  1556. (push html-table-tag html))
  1557. (concat (mapconcat 'identity html "\n") "\n")))
  1558. (defun org-export-splice-attributes (tag attributes)
  1559. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  1560. (if (not attributes)
  1561. tag
  1562. (let (oldatt newatt)
  1563. (setq oldatt (org-extract-attributes-from-string tag)
  1564. tag (pop oldatt)
  1565. newatt (cdr (org-extract-attributes-from-string attributes)))
  1566. (while newatt
  1567. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  1568. (if (string-match ">" tag)
  1569. (setq tag
  1570. (replace-match (concat (org-attributes-to-string oldatt) ">")
  1571. t t tag)))
  1572. tag)))
  1573. (defun org-format-table-table-html (lines)
  1574. "Format a table generated by table.el into HTML.
  1575. This conversion does *not* use `table-generate-source' from table.el.
  1576. This has the advantage that Org-mode's HTML conversions can be used.
  1577. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1578. (let (line field-buffer
  1579. (head org-export-highlight-first-table-line)
  1580. fields html empty i)
  1581. (setq html (concat html-table-tag "\n"))
  1582. (while (setq line (pop lines))
  1583. (setq empty "&nbsp;")
  1584. (catch 'next-line
  1585. (if (string-match "^[ \t]*\\+-" line)
  1586. (progn
  1587. (if field-buffer
  1588. (progn
  1589. (setq
  1590. html
  1591. (concat
  1592. html
  1593. "<tr>"
  1594. (mapconcat
  1595. (lambda (x)
  1596. (if (equal x "") (setq x empty))
  1597. (if head
  1598. (concat
  1599. (format (car org-export-table-header-tags) "col")
  1600. x
  1601. (cdr org-export-table-header-tags))
  1602. (concat (car org-export-table-data-tags) x
  1603. (cdr org-export-table-data-tags))))
  1604. field-buffer "\n")
  1605. "</tr>\n"))
  1606. (setq head nil)
  1607. (setq field-buffer nil)))
  1608. ;; Ignore this line
  1609. (throw 'next-line t)))
  1610. ;; Break the line into fields and store the fields
  1611. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1612. (if field-buffer
  1613. (setq field-buffer (mapcar
  1614. (lambda (x)
  1615. (concat x "<br/>" (pop fields)))
  1616. field-buffer))
  1617. (setq field-buffer fields))))
  1618. (setq html (concat html "</table>\n"))
  1619. html))
  1620. (defun org-format-table-table-html-using-table-generate-source (lines)
  1621. "Format a table into html, using `table-generate-source' from table.el.
  1622. This has the advantage that cell- or row-spanning is allowed.
  1623. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  1624. (require 'table)
  1625. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1626. (erase-buffer)
  1627. (insert (mapconcat 'identity lines "\n"))
  1628. (goto-char (point-min))
  1629. (if (not (re-search-forward "|[^+]" nil t))
  1630. (error "Error processing table"))
  1631. (table-recognize-table)
  1632. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1633. (table-generate-source 'html " org-tmp2 ")
  1634. (set-buffer " org-tmp2 ")
  1635. (buffer-substring (point-min) (point-max))))
  1636. (defun org-export-splice-style (style extra)
  1637. "Splice EXTRA into STYLE, just before \"</style>\"."
  1638. (if (and (stringp extra)
  1639. (string-match "\\S-" extra)
  1640. (string-match "</style>" style))
  1641. (concat (substring style 0 (match-beginning 0))
  1642. "\n" extra "\n"
  1643. (substring style (match-beginning 0)))
  1644. style))
  1645. (defun org-html-handle-time-stamps (s)
  1646. "Format time stamps in string S, or remove them."
  1647. (catch 'exit
  1648. (let (r b)
  1649. (while (string-match org-maybe-keyword-time-regexp s)
  1650. (or b (setq b (substring s 0 (match-beginning 0))))
  1651. (setq r (concat
  1652. r (substring s 0 (match-beginning 0))
  1653. " @<span class=\"timestamp-wrapper\">"
  1654. (if (match-end 1)
  1655. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  1656. (match-string 1 s)))
  1657. (format " @<span class=\"timestamp\">%s@</span>"
  1658. (substring
  1659. (org-translate-time (match-string 3 s)) 1 -1))
  1660. "@</span>")
  1661. s (substring s (match-end 0))))
  1662. ;; Line break if line started and ended with time stamp stuff
  1663. (if (not r)
  1664. s
  1665. (setq r (concat r s))
  1666. (unless (string-match "\\S-" (concat b s))
  1667. (setq r (concat r "@<br/>")))
  1668. r))))
  1669. (defvar htmlize-buffer-places) ; from htmlize.el
  1670. (defun org-export-htmlize-region-for-paste (beg end)
  1671. "Convert the region to HTML, using htmlize.el.
  1672. This is much like `htmlize-region-for-paste', only that it uses
  1673. the settings define in the org-... variables."
  1674. (let* ((htmlize-output-type org-export-htmlize-output-type)
  1675. (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
  1676. (htmlbuf (htmlize-region beg end)))
  1677. (unwind-protect
  1678. (with-current-buffer htmlbuf
  1679. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  1680. (plist-get htmlize-buffer-places 'content-end)))
  1681. (kill-buffer htmlbuf))))
  1682. ;;;###autoload
  1683. (defun org-export-htmlize-generate-css ()
  1684. "Create the CSS for all font definitions in the current Emacs session.
  1685. Use this to create face definitions in your CSS style file that can then
  1686. be used by code snippets transformed by htmlize.
  1687. This command just produces a buffer that contains class definitions for all
  1688. faces used in the current Emacs session. You can copy and paste the ones you
  1689. need into your CSS file.
  1690. If you then set `org-export-htmlize-output-type' to `css', calls to
  1691. the function `org-export-htmlize-region-for-paste' will produce code
  1692. that uses these same face definitions."
  1693. (interactive)
  1694. (require 'htmlize)
  1695. (and (get-buffer "*html*") (kill-buffer "*html*"))
  1696. (with-temp-buffer
  1697. (let ((fl (face-list))
  1698. (htmlize-css-name-prefix "org-")
  1699. (htmlize-output-type 'css)
  1700. f i)
  1701. (while (setq f (pop fl)
  1702. i (and f (face-attribute f :inherit)))
  1703. (when (and (symbolp f) (or (not i) (not (listp i))))
  1704. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  1705. (htmlize-region (point-min) (point-max))))
  1706. (switch-to-buffer "*html*")
  1707. (goto-char (point-min))
  1708. (if (re-search-forward "<style" nil t)
  1709. (delete-region (point-min) (match-beginning 0)))
  1710. (if (re-search-forward "</style>" nil t)
  1711. (delete-region (1+ (match-end 0)) (point-max)))
  1712. (beginning-of-line 1)
  1713. (if (looking-at " +") (replace-match ""))
  1714. (goto-char (point-min)))
  1715. (defun org-html-protect (s)
  1716. ;; convert & to &amp;, < to &lt; and > to &gt;
  1717. (let ((start 0))
  1718. (while (string-match "&" s start)
  1719. (setq s (replace-match "&amp;" t t s)
  1720. start (1+ (match-beginning 0))))
  1721. (while (string-match "<" s)
  1722. (setq s (replace-match "&lt;" t t s)))
  1723. (while (string-match ">" s)
  1724. (setq s (replace-match "&gt;" t t s)))
  1725. ; (while (string-match "\"" s)
  1726. ; (setq s (replace-match "&quot;" t t s)))
  1727. )
  1728. s)
  1729. (defun org-html-expand (string)
  1730. "Prepare STRING for HTML export. Applies all active conversions.
  1731. If there are links in the string, don't modify these."
  1732. (let* ((re (concat org-bracket-link-regexp "\\|"
  1733. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  1734. m s l res)
  1735. (while (setq m (string-match re string))
  1736. (setq s (substring string 0 m)
  1737. l (match-string 0 string)
  1738. string (substring string (match-end 0)))
  1739. (push (org-html-do-expand s) res)
  1740. (push l res))
  1741. (push (org-html-do-expand string) res)
  1742. (apply 'concat (nreverse res))))
  1743. (defun org-html-do-expand (s)
  1744. "Apply all active conversions to translate special ASCII to HTML."
  1745. (setq s (org-html-protect s))
  1746. (if org-export-html-expand
  1747. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1748. (setq s (replace-match "<\\1>" t nil s))))
  1749. (if org-export-with-emphasize
  1750. (setq s (org-export-html-convert-emphasize s)))
  1751. (if org-export-with-special-strings
  1752. (setq s (org-export-html-convert-special-strings s)))
  1753. (if org-export-with-sub-superscripts
  1754. (setq s (org-export-html-convert-sub-super s)))
  1755. (if org-export-with-TeX-macros
  1756. (let ((start 0) wd ass)
  1757. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1758. s start))
  1759. (if (get-text-property (match-beginning 0) 'org-protected s)
  1760. (setq start (match-end 0))
  1761. (setq wd (match-string 1 s))
  1762. (if (setq ass (assoc wd org-html-entities))
  1763. (setq s (replace-match (or (cdr ass)
  1764. (concat "&" (car ass) ";"))
  1765. t t s))
  1766. (setq start (+ start (length wd))))))))
  1767. s)
  1768. (defun org-export-html-convert-special-strings (string)
  1769. "Convert special characters in STRING to HTML."
  1770. (let ((all org-export-html-special-string-regexps)
  1771. e a re rpl start)
  1772. (while (setq a (pop all))
  1773. (setq re (car a) rpl (cdr a) start 0)
  1774. (while (string-match re string start)
  1775. (if (get-text-property (match-beginning 0) 'org-protected string)
  1776. (setq start (match-end 0))
  1777. (setq string (replace-match rpl t nil string)))))
  1778. string))
  1779. (defun org-export-html-convert-sub-super (string)
  1780. "Convert sub- and superscripts in STRING to HTML."
  1781. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1782. (while (string-match org-match-substring-regexp string s)
  1783. (cond
  1784. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1785. ((get-text-property (match-beginning 2) 'org-protected string)
  1786. (setq s (match-end 2)))
  1787. (t
  1788. (setq s (match-end 1)
  1789. key (if (string= (match-string 2 string) "_") "sub" "sup")
  1790. c (or (match-string 8 string)
  1791. (match-string 6 string)
  1792. (match-string 5 string))
  1793. string (replace-match
  1794. (concat (match-string 1 string)
  1795. "<" key ">" c "</" key ">")
  1796. t t string)))))
  1797. (while (string-match "\\\\\\([_^]\\)" string)
  1798. (setq string (replace-match (match-string 1 string) t t string)))
  1799. string))
  1800. (defun org-export-html-convert-emphasize (string)
  1801. "Apply emphasis."
  1802. (let ((s 0) rpl)
  1803. (while (string-match org-emph-re string s)
  1804. (if (not (equal
  1805. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1806. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1807. (setq s (match-beginning 0)
  1808. rpl
  1809. (concat
  1810. (match-string 1 string)
  1811. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  1812. (match-string 4 string)
  1813. (nth 3 (assoc (match-string 3 string)
  1814. org-emphasis-alist))
  1815. (match-string 5 string))
  1816. string (replace-match rpl t t string)
  1817. s (+ s (- (length rpl) 2)))
  1818. (setq s (1+ s))))
  1819. string))
  1820. (defun org-open-par ()
  1821. "Insert <p>, but first close previous paragraph if any."
  1822. (org-close-par-maybe)
  1823. (insert "\n<p>")
  1824. (setq org-par-open t))
  1825. (defun org-close-par-maybe ()
  1826. "Close paragraph if there is one open."
  1827. (when org-par-open
  1828. (insert "</p>")
  1829. (setq org-par-open nil)))
  1830. (defun org-close-li (&optional type)
  1831. "Close <li> if necessary."
  1832. (org-close-par-maybe)
  1833. (insert (if (equal type "d") "</dd>\n" "</li>\n")))
  1834. (defvar in-local-list)
  1835. (defvar local-list-indent)
  1836. (defvar local-list-type)
  1837. (defun org-export-html-close-lists-maybe (line)
  1838. (let ((ind (or (get-text-property 0 'original-indentation line)))
  1839. ; (and (string-match "\\S-" line)
  1840. ; (org-get-indentation line))))
  1841. didclose)
  1842. (when ind
  1843. (while (and in-local-list
  1844. (<= ind (car local-list-indent)))
  1845. (setq didclose t)
  1846. (org-close-li (car local-list-type))
  1847. (insert (format "</%sl>\n" (car local-list-type)))
  1848. (pop local-list-type) (pop local-list-indent)
  1849. (setq in-local-list local-list-indent))
  1850. (and didclose (org-open-par)))))
  1851. (defvar body-only) ; dynamically scoped into this.
  1852. (defun org-html-level-start (level title umax with-toc head-count)
  1853. "Insert a new level in HTML export.
  1854. When TITLE is nil, just close all open levels."
  1855. (org-close-par-maybe)
  1856. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1857. (extra-targets (assoc target org-export-target-aliases))
  1858. (preferred (cdr (assoc target org-export-preferred-target-alist)))
  1859. (remove (or preferred target))
  1860. (l org-level-max)
  1861. snumber href suffix)
  1862. (setq extra-targets (remove remove extra-targets))
  1863. (setq extra-targets
  1864. (mapconcat (lambda (x)
  1865. (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
  1866. (format "<a name=\"%s\" id=\"%s\"></a>"
  1867. x x))
  1868. extra-targets
  1869. ""))
  1870. (while (>= l level)
  1871. (if (aref org-levels-open (1- l))
  1872. (progn
  1873. (org-html-level-close l umax)
  1874. (aset org-levels-open (1- l) nil)))
  1875. (setq l (1- l)))
  1876. (when title
  1877. ;; If title is nil, this means this function is called to close
  1878. ;; all levels, so the rest is done only if title is given
  1879. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1880. (setq title (replace-match
  1881. (if org-export-with-tags
  1882. (save-match-data
  1883. (concat
  1884. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  1885. (mapconcat
  1886. (lambda (x)
  1887. (format "<span class=\"%s\">%s</span>"
  1888. (org-export-html-get-tag-class-name x)
  1889. x))
  1890. (org-split-string (match-string 1 title) ":")
  1891. "&nbsp;")
  1892. "</span>"))
  1893. "")
  1894. t t title)))
  1895. (if (> level umax)
  1896. (progn
  1897. (if (aref org-levels-open (1- level))
  1898. (progn
  1899. (org-close-li)
  1900. (if target
  1901. (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
  1902. (insert "<li>" title "<br/>\n")))
  1903. (aset org-levels-open (1- level) t)
  1904. (org-close-par-maybe)
  1905. (if target
  1906. (insert (format "<ul>\n<li id=\"%s\">" target)
  1907. extra-targets title "<br/>\n")
  1908. (insert "<ul>\n<li>" title "<br/>\n"))))
  1909. (aset org-levels-open (1- level) t)
  1910. (setq snumber (org-section-number level))
  1911. (setq level (+ level org-export-html-toplevel-hlevel -1))
  1912. (if (and org-export-with-section-numbers (not body-only))
  1913. (setq title (concat
  1914. (format "<span class=\"section-number-%d\">%s</span>"
  1915. level snumber)
  1916. " " title)))
  1917. (unless (= head-count 1) (insert "\n</div>\n"))
  1918. (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
  1919. (setq suffix (or href snumber))
  1920. (setq href (or href (concat "sec-" snumber)))
  1921. (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"%s\">%s%s</h%d>\n<div class=\"outline-text-%d\" id=\"text-%s\">\n"
  1922. suffix level level href
  1923. extra-targets
  1924. title level level suffix))
  1925. (org-open-par)))))
  1926. (defun org-export-html-get-tag-class-name (tag)
  1927. "Turn tag into a valid class name.
  1928. Replaces invalid characters with \"_\" and then prepends a prefix."
  1929. (save-match-data
  1930. (while (string-match "[^a-zA-Z0-9_]" tag)
  1931. (setq tag (replace-match "_" t t tag))))
  1932. (concat org-export-html-tag-class-prefix tag))
  1933. (defun org-export-html-get-todo-kwd-class-name (kwd)
  1934. "Turn todo keyword into a valid class name.
  1935. Replaces invalid characters with \"_\" and then prepends a prefix."
  1936. (save-match-data
  1937. (while (string-match "[^a-zA-Z0-9_]" kwd)
  1938. (setq kwd (replace-match "_" t t kwd))))
  1939. (concat org-export-html-todo-kwd-class-prefix kwd))
  1940. (defun org-html-level-close (level max-outline-level)
  1941. "Terminate one level in HTML export."
  1942. (if (<= level max-outline-level)
  1943. (insert "</div>\n")
  1944. (org-close-li)
  1945. (insert "</ul>\n")))
  1946. (provide 'org-html)
  1947. ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
  1948. ;;; org-html.el ends here