org-html.el 74 KB

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