org-html.el 76 KB

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