org-html.el 74 KB

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