org-html.el 69 KB

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