org-html.el 70 KB

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