org-html.el 68 KB

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