org-html.el 75 KB

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