org-html.el 68 KB

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