org-html.el 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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.34b
  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-include-default '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 at the end of HTML export, in the new buffer.")
  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 body-only)
  563. (not
  564. (plist-get opt-plist :skip-before-1st-heading))
  565. (org-export-grab-title-from-buffer))
  566. (and buffer-file-name
  567. (file-name-sans-extension
  568. (file-name-nondirectory buffer-file-name)))
  569. "UNTITLED"))
  570. (link-up (and (plist-get opt-plist :link-up)
  571. (string-match "\\S-" (plist-get opt-plist :link-up))
  572. (plist-get opt-plist :link-up)))
  573. (link-home (and (plist-get opt-plist :link-home)
  574. (string-match "\\S-" (plist-get opt-plist :link-home))
  575. (plist-get opt-plist :link-home)))
  576. (dummy (setq opt-plist (plist-put opt-plist :title title)))
  577. (html-table-tag (plist-get opt-plist :html-table-tag))
  578. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  579. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  580. (inquote nil)
  581. (infixed nil)
  582. (inverse nil)
  583. (in-local-list nil)
  584. (local-list-type nil)
  585. (local-list-indent nil)
  586. (llt org-plain-list-ordered-item-terminator)
  587. (email (plist-get opt-plist :email))
  588. (language (plist-get opt-plist :language))
  589. (keywords (plist-get opt-plist :keywords))
  590. (description (plist-get opt-plist :description))
  591. (lang-words nil)
  592. (head-count 0) cnt
  593. (start 0)
  594. (coding-system (and (boundp 'buffer-file-coding-system)
  595. buffer-file-coding-system))
  596. (coding-system-for-write (or org-export-html-coding-system
  597. coding-system))
  598. (save-buffer-coding-system (or org-export-html-coding-system
  599. coding-system))
  600. (charset (and coding-system-for-write
  601. (fboundp 'coding-system-get)
  602. (coding-system-get coding-system-for-write
  603. 'mime-charset)))
  604. (region
  605. (buffer-substring
  606. (if region-p (region-beginning) (point-min))
  607. (if region-p (region-end) (point-max))))
  608. (lines
  609. (org-split-string
  610. (org-export-preprocess-string
  611. region
  612. :emph-multiline t
  613. :for-html t
  614. :skip-before-1st-heading
  615. (plist-get opt-plist :skip-before-1st-heading)
  616. :drawers (plist-get opt-plist :drawers)
  617. :todo-keywords (plist-get opt-plist :todo-keywords)
  618. :tags (plist-get opt-plist :tags)
  619. :priority (plist-get opt-plist :priority)
  620. :footnotes (plist-get opt-plist :footnotes)
  621. :timestamps (plist-get opt-plist :timestamps)
  622. :archived-trees
  623. (plist-get opt-plist :archived-trees)
  624. :select-tags (plist-get opt-plist :select-tags)
  625. :exclude-tags (plist-get opt-plist :exclude-tags)
  626. :add-text
  627. (plist-get opt-plist :text)
  628. :LaTeX-fragments
  629. (plist-get opt-plist :LaTeX-fragments))
  630. "[\r\n]"))
  631. table-open type
  632. table-buffer table-orig-buffer
  633. ind item-type starter didclose
  634. rpl path attr desc descp desc1 desc2 link
  635. snumber fnc item-tag
  636. footnotes footref-seen
  637. id-file href
  638. )
  639. (let ((inhibit-read-only t))
  640. (org-unmodified
  641. (remove-text-properties (point-min) (point-max)
  642. '(:org-license-to-kill t))))
  643. (message "Exporting...")
  644. (setq org-min-level (org-get-min-level lines level-offset))
  645. (setq org-last-level org-min-level)
  646. (org-init-section-numbers)
  647. (cond
  648. ((and date (string-match "%" date))
  649. (setq date (format-time-string date)))
  650. (date)
  651. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  652. ;; Get the language-dependent settings
  653. (setq lang-words (or (assoc language org-export-language-setup)
  654. (assoc "en" org-export-language-setup)))
  655. ;; Switch to the output buffer
  656. (set-buffer buffer)
  657. (let ((inhibit-read-only t)) (erase-buffer))
  658. (fundamental-mode)
  659. (org-install-letbind)
  660. (and (fboundp 'set-buffer-file-coding-system)
  661. (set-buffer-file-coding-system coding-system-for-write))
  662. (let ((case-fold-search nil)
  663. (org-odd-levels-only odd))
  664. ;; create local variables for all options, to make sure all called
  665. ;; functions get the correct information
  666. (mapc (lambda (x)
  667. (set (make-local-variable (nth 2 x))
  668. (plist-get opt-plist (car x))))
  669. org-export-plist-vars)
  670. (setq umax (if arg (prefix-numeric-value arg)
  671. org-export-headline-levels))
  672. (setq umax-toc (if (integerp org-export-with-toc)
  673. (min org-export-with-toc umax)
  674. umax))
  675. (unless body-only
  676. ;; File header
  677. (insert (format
  678. "%s
  679. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  680. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  681. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  682. lang=\"%s\" xml:lang=\"%s\">
  683. <head>
  684. <title>%s</title>
  685. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  686. <meta name=\"generator\" content=\"Org-mode\"/>
  687. <meta name=\"generated\" content=\"%s\"/>
  688. <meta name=\"author\" content=\"%s\"/>
  689. <meta name=\"description\" content=\"%s\"/>
  690. <meta name=\"keywords\" content=\"%s\"/>
  691. %s
  692. </head>
  693. <body>
  694. <div id=\"content\">
  695. %s
  696. "
  697. (format
  698. (or (and (stringp org-export-html-xml-declaration)
  699. org-export-html-xml-declaration)
  700. (cdr (assoc html-extension org-export-html-xml-declaration))
  701. (cdr (assoc "html" org-export-html-xml-declaration))
  702. "")
  703. (or charset "iso-8859-1"))
  704. language language
  705. (org-html-expand title)
  706. (or charset "iso-8859-1")
  707. date author description keywords
  708. style
  709. (if (or link-up link-home)
  710. (concat
  711. (format org-export-html-home/up-format
  712. (or link-up link-home)
  713. (or link-home link-up))
  714. "\n")
  715. "")))
  716. (org-export-html-insert-plist-item opt-plist :preamble opt-plist)
  717. (when (plist-get opt-plist :auto-preamble)
  718. (if title (insert (format org-export-html-title-format
  719. (org-html-expand title))))))
  720. (if (and org-export-with-toc (not body-only))
  721. (progn
  722. (push (format "<h%d>%s</h%d>\n"
  723. org-export-html-toplevel-hlevel
  724. (nth 3 lang-words)
  725. org-export-html-toplevel-hlevel)
  726. thetoc)
  727. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  728. (push "<ul>\n<li>" thetoc)
  729. (setq lines
  730. (mapcar '(lambda (line)
  731. (if (string-match org-todo-line-regexp line)
  732. ;; This is a headline
  733. (progn
  734. (setq have-headings t)
  735. (setq level (- (match-end 1) (match-beginning 1)
  736. level-offset)
  737. level (org-tr-level level)
  738. txt (save-match-data
  739. (org-html-expand
  740. (org-export-cleanup-toc-line
  741. (match-string 3 line))))
  742. todo
  743. (or (and org-export-mark-todo-in-toc
  744. (match-beginning 2)
  745. (not (member (match-string 2 line)
  746. org-done-keywords)))
  747. ; TODO, not DONE
  748. (and org-export-mark-todo-in-toc
  749. (= level umax-toc)
  750. (org-search-todo-below
  751. line lines level))))
  752. (if (string-match
  753. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  754. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  755. (if (string-match quote-re0 txt)
  756. (setq txt (replace-match "" t t txt)))
  757. (setq snumber (org-section-number level))
  758. (if org-export-with-section-numbers
  759. (setq txt (concat snumber " " txt)))
  760. (if (<= level (max umax umax-toc))
  761. (setq head-count (+ head-count 1)))
  762. (if (<= level umax-toc)
  763. (progn
  764. (if (> level org-last-level)
  765. (progn
  766. (setq cnt (- level org-last-level))
  767. (while (>= (setq cnt (1- cnt)) 0)
  768. (push "\n<ul>\n<li>" thetoc))
  769. (push "\n" thetoc)))
  770. (if (< level org-last-level)
  771. (progn
  772. (setq cnt (- org-last-level level))
  773. (while (>= (setq cnt (1- cnt)) 0)
  774. (push "</li>\n</ul>" thetoc))
  775. (push "\n" thetoc)))
  776. ;; Check for targets
  777. (while (string-match org-any-target-regexp line)
  778. (setq line (replace-match
  779. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  780. t t line)))
  781. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  782. (setq txt (replace-match "" t t txt)))
  783. (setq href (format "sec-%s" snumber))
  784. (setq href (or (cdr (assoc href org-export-preferred-target-alist)) href))
  785. (push
  786. (format
  787. (if todo
  788. "</li>\n<li><a href=\"#%s\"><span class=\"todo\">%s</span></a>"
  789. "</li>\n<li><a href=\"#%s\">%s</a>")
  790. href txt) thetoc)
  791. (setq org-last-level level))
  792. )))
  793. line)
  794. lines))
  795. (while (> org-last-level (1- org-min-level))
  796. (setq org-last-level (1- org-last-level))
  797. (push "</li>\n</ul>\n" thetoc))
  798. (push "</div>\n" thetoc)
  799. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  800. (setq head-count 0)
  801. (org-init-section-numbers)
  802. (org-open-par)
  803. (while (setq line (pop lines) origline line)
  804. (catch 'nextline
  805. ;; end of quote section?
  806. (when (and inquote (string-match "^\\*+ " line))
  807. (insert "</pre>\n")
  808. (org-open-par)
  809. (setq inquote nil))
  810. ;; inside a quote section?
  811. (when inquote
  812. (insert (org-html-protect line) "\n")
  813. (throw 'nextline nil))
  814. ;; Fixed-width, verbatim lines (examples)
  815. (when (and org-export-with-fixed-width
  816. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  817. (when (not infixed)
  818. (setq infixed t)
  819. (org-close-par-maybe)
  820. (insert "<pre class=\"example\">\n"))
  821. (insert (org-html-protect (match-string 3 line)) "\n")
  822. (when (or (not lines)
  823. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  824. (car lines))))
  825. (setq infixed nil)
  826. (insert "</pre>\n")
  827. (org-open-par))
  828. (throw 'nextline nil))
  829. (org-export-html-close-lists-maybe line)
  830. ;; Protected HTML
  831. (when (get-text-property 0 'org-protected line)
  832. (let (par (ind (get-text-property 0 'original-indentation line)))
  833. (when (re-search-backward
  834. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  835. (setq par (match-string 1))
  836. (replace-match "\\2\n"))
  837. (insert line "\n")
  838. (while (and lines
  839. (or (= (length (car lines)) 0)
  840. (not ind)
  841. (equal ind (get-text-property 0 'original-indentation (car lines))))
  842. (or (= (length (car lines)) 0)
  843. (get-text-property 0 'org-protected (car lines))))
  844. (insert (pop lines) "\n"))
  845. (and par (insert "<p>\n")))
  846. (throw 'nextline nil))
  847. ;; Blockquotes, verse, and center
  848. (when (equal "ORG-BLOCKQUOTE-START" line)
  849. (org-close-par-maybe)
  850. (insert "<blockquote>\n")
  851. (org-open-par)
  852. (throw 'nextline nil))
  853. (when (equal "ORG-BLOCKQUOTE-END" line)
  854. (org-close-par-maybe)
  855. (insert "\n</blockquote>\n")
  856. (org-open-par)
  857. (throw 'nextline nil))
  858. (when (equal "ORG-VERSE-START" line)
  859. (org-close-par-maybe)
  860. (insert "\n<p class=\"verse\">\n")
  861. (setq inverse t)
  862. (throw 'nextline nil))
  863. (when (equal "ORG-VERSE-END" line)
  864. (insert "</p>\n")
  865. (org-open-par)
  866. (setq inverse nil)
  867. (throw 'nextline nil))
  868. (when (equal "ORG-CENTER-START" line)
  869. (org-close-par-maybe)
  870. (insert "\n<div style=\"text-align: center\">")
  871. (org-open-par)
  872. (throw 'nextline nil))
  873. (when (equal "ORG-CENTER-END" line)
  874. (org-close-par-maybe)
  875. (insert "\n</div>")
  876. (org-open-par)
  877. (throw 'nextline nil))
  878. (run-hooks 'org-export-html-after-blockquotes-hook)
  879. (when inverse
  880. (let ((i (org-get-string-indentation line)))
  881. (if (> i 0)
  882. (setq line (concat (mapconcat 'identity
  883. (make-list (* 2 i) "\\nbsp") "")
  884. " " (org-trim line))))
  885. (unless (string-match "\\\\\\\\[ \t]*$" line)
  886. (setq line (concat line "\\\\")))))
  887. ;; make targets to anchors
  888. (setq start 0)
  889. (while (string-match
  890. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  891. (cond
  892. ((get-text-property (match-beginning 1) 'org-protected line)
  893. (setq start (match-end 1)))
  894. ((match-end 2)
  895. (setq line (replace-match
  896. (format
  897. "@<a name=\"%s\" id=\"%s\">@</a>"
  898. (org-solidify-link-text (match-string 1 line))
  899. (org-solidify-link-text (match-string 1 line)))
  900. t t line)))
  901. ((and org-export-with-toc (equal (string-to-char line) ?*))
  902. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  903. (setq line (replace-match
  904. (concat "@<span class=\"target\">"
  905. (match-string 1 line) "@</span> ")
  906. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  907. t t line)))
  908. (t
  909. (setq line (replace-match
  910. (concat "@<a name=\""
  911. (org-solidify-link-text (match-string 1 line))
  912. "\" class=\"target\">" (match-string 1 line)
  913. "@</a> ")
  914. t t line)))))
  915. (setq line (org-html-handle-time-stamps line))
  916. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  917. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  918. ;; Also handle sub_superscripts and checkboxes
  919. (or (string-match org-table-hline-regexp line)
  920. (setq line (org-html-expand line)))
  921. ;; Format the links
  922. (setq start 0)
  923. (while (string-match org-bracket-link-analytic-regexp++ line start)
  924. (setq start (match-beginning 0))
  925. (setq path (save-match-data (org-link-unescape
  926. (match-string 3 line))))
  927. (setq type (cond
  928. ((match-end 2) (match-string 2 line))
  929. ((save-match-data
  930. (or (file-name-absolute-p path)
  931. (string-match "^\\.\\.?/" path)))
  932. "file")
  933. (t "internal")))
  934. (setq path (org-extract-attributes (org-link-unescape path)))
  935. (setq attr (get-text-property 0 'org-attributes path))
  936. (setq desc1 (if (match-end 5) (match-string 5 line))
  937. desc2 (if (match-end 2) (concat type ":" path) path)
  938. descp (and desc1 (not (equal desc1 desc2)))
  939. desc (or desc1 desc2))
  940. ;; Make an image out of the description if that is so wanted
  941. (when (and descp (org-file-image-p
  942. desc org-export-html-inline-image-extensions))
  943. (save-match-data
  944. (if (string-match "^file:" desc)
  945. (setq desc (substring desc (match-end 0)))))
  946. (setq desc (org-add-props
  947. (concat "<img src=\"" desc "\"/>")
  948. '(org-protected t))))
  949. ;; FIXME: do we need to unescape here somewhere?
  950. (cond
  951. ((equal type "internal")
  952. (setq rpl
  953. (concat
  954. "<a href=\""
  955. (if (= (string-to-char path) ?#) "" "#")
  956. (org-solidify-link-text
  957. (save-match-data (org-link-unescape path)) nil)
  958. "\"" attr ">"
  959. (org-export-html-format-desc desc)
  960. "</a>")))
  961. ((and (equal type "id")
  962. (setq id-file (org-id-find-id-file path)))
  963. ;; This is an id: link to another file (if it was the same file,
  964. ;; it would have become an internal link...)
  965. (save-match-data
  966. (setq id-file (file-relative-name
  967. id-file (file-name-directory org-current-export-file)))
  968. (setq id-file (concat (file-name-sans-extension id-file)
  969. "." html-extension))
  970. (setq rpl (concat "<a href=\"" id-file "#"
  971. (if (org-uuidgen-p path) "ID-")
  972. path "\""
  973. attr ">"
  974. (org-export-html-format-desc desc)
  975. "</a>"))))
  976. ((member type '("http" "https"))
  977. ;; standard URL, just check if we need to inline an image
  978. (if (and (or (eq t org-export-html-inline-images)
  979. (and org-export-html-inline-images (not descp)))
  980. (org-file-image-p
  981. path org-export-html-inline-image-extensions))
  982. (setq rpl (org-export-html-format-image
  983. (concat type ":" path) org-par-open))
  984. (setq link (concat type ":" path))
  985. (setq rpl (concat "<a href=\""
  986. (org-export-html-format-href link)
  987. "\"" attr ">"
  988. (org-export-html-format-desc desc)
  989. "</a>"))))
  990. ((member type '("ftp" "mailto" "news"))
  991. ;; standard URL
  992. (setq link (concat type ":" path))
  993. (setq rpl (concat "<a href=\""
  994. (org-export-html-format-href link)
  995. "\"" attr ">"
  996. (org-export-html-format-desc desc)
  997. "</a>")))
  998. ((string= type "coderef")
  999. (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
  1000. path path path
  1001. (format (org-export-get-coderef-format path (and descp desc))
  1002. (cdr (assoc path org-export-code-refs))))))
  1003. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  1004. ;; The link protocol has a function for format the link
  1005. (setq rpl
  1006. (save-match-data
  1007. (funcall fnc (org-link-unescape path) desc1 'html))))
  1008. ((string= type "file")
  1009. ;; FILE link
  1010. (let* ((filename path)
  1011. (abs-p (file-name-absolute-p filename))
  1012. thefile file-is-image-p search)
  1013. (save-match-data
  1014. (if (string-match "::\\(.*\\)" filename)
  1015. (setq search (match-string 1 filename)
  1016. filename (replace-match "" t nil filename)))
  1017. (setq valid
  1018. (if (functionp link-validate)
  1019. (funcall link-validate filename current-dir)
  1020. t))
  1021. (setq file-is-image-p
  1022. (org-file-image-p
  1023. filename org-export-html-inline-image-extensions))
  1024. (setq thefile (if abs-p (expand-file-name filename) filename))
  1025. (when (and org-export-html-link-org-files-as-html
  1026. (string-match "\\.org$" thefile))
  1027. (setq thefile (concat (substring thefile 0
  1028. (match-beginning 0))
  1029. "." html-extension))
  1030. (if (and search
  1031. ;; make sure this is can be used as target search
  1032. (not (string-match "^[0-9]*$" search))
  1033. (not (string-match "^\\*" search))
  1034. (not (string-match "^/.*/$" search)))
  1035. (setq thefile (concat thefile "#"
  1036. (org-solidify-link-text
  1037. (org-link-unescape search)))))
  1038. (when (string-match "^file:" desc)
  1039. (setq desc (replace-match "" t t desc))
  1040. (if (string-match "\\.org$" desc)
  1041. (setq desc (replace-match "" t t desc))))))
  1042. (setq rpl (if (and file-is-image-p
  1043. (or (eq t org-export-html-inline-images)
  1044. (and org-export-html-inline-images
  1045. (not descp))))
  1046. (progn
  1047. (message "image %s %s" thefile org-par-open)
  1048. (org-export-html-format-image thefile org-par-open))
  1049. (concat "<a href=\"" thefile "\"" attr ">"
  1050. (org-export-html-format-desc desc)
  1051. "</a>")))
  1052. (if (not valid) (setq rpl desc))))
  1053. (t
  1054. ;; just publish the path, as default
  1055. (setq rpl (concat "<i>&lt;" type ":"
  1056. (save-match-data (org-link-unescape path))
  1057. "&gt;</i>"))))
  1058. (setq line (replace-match rpl t t line)
  1059. start (+ start (length rpl))))
  1060. ;; TODO items
  1061. (if (and (string-match org-todo-line-regexp line)
  1062. (match-beginning 2))
  1063. (setq line
  1064. (concat (substring line 0 (match-beginning 2))
  1065. "<span class=\""
  1066. (if (member (match-string 2 line)
  1067. org-done-keywords)
  1068. "done" "todo")
  1069. " " (match-string 2 line)
  1070. "\"> " (org-export-html-get-todo-kwd-class-name
  1071. (match-string 2 line))
  1072. "</span>" (substring line (match-end 2)))))
  1073. ;; Does this contain a reference to a footnote?
  1074. (when org-export-with-footnotes
  1075. (setq start 0)
  1076. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  1077. (if (get-text-property (match-beginning 2) 'org-protected line)
  1078. (setq start (match-end 2))
  1079. (let ((n (match-string 2 line)) extra a)
  1080. (if (setq a (assoc n footref-seen))
  1081. (progn
  1082. (setcdr a (1+ (cdr a)))
  1083. (setq extra (format ".%d" (cdr a))))
  1084. (setq extra "")
  1085. (push (cons n 1) footref-seen))
  1086. (setq line
  1087. (replace-match
  1088. (format
  1089. (concat "%s"
  1090. (format org-export-html-footnote-format
  1091. "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"))
  1092. (or (match-string 1 line) "") n extra n n)
  1093. t t line))))))
  1094. (cond
  1095. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1096. ;; This is a headline
  1097. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1098. level-offset))
  1099. txt (match-string 2 line))
  1100. (if (string-match quote-re0 txt)
  1101. (setq txt (replace-match "" t t txt)))
  1102. (if (<= level (max umax umax-toc))
  1103. (setq head-count (+ head-count 1)))
  1104. (when in-local-list
  1105. ;; Close any local lists before inserting a new header line
  1106. (while local-list-type
  1107. (org-close-li (car local-list-type))
  1108. (insert (format "</%sl>\n" (car local-list-type)))
  1109. (pop local-list-type))
  1110. (setq local-list-indent nil
  1111. in-local-list nil))
  1112. (setq first-heading-pos (or first-heading-pos (point)))
  1113. (org-html-level-start level txt umax
  1114. (and org-export-with-toc (<= level umax))
  1115. head-count)
  1116. ;; QUOTES
  1117. (when (string-match quote-re line)
  1118. (org-close-par-maybe)
  1119. (insert "<pre>")
  1120. (setq inquote t)))
  1121. ((string-match "^[ \t]*- __+[ \t]*$" line)
  1122. ;; Explicit list closure
  1123. (when local-list-type
  1124. (let ((ind (org-get-indentation line)))
  1125. (while (and local-list-indent
  1126. (<= ind (car local-list-indent)))
  1127. (org-close-li (car local-list-type))
  1128. (insert (format "</%sl>\n" (car local-list-type)))
  1129. (pop local-list-type)
  1130. (pop local-list-indent))
  1131. (or local-list-indent (setq in-local-list nil))))
  1132. (throw 'nextline nil))
  1133. ((and org-export-with-tables
  1134. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1135. (when (not table-open)
  1136. ;; New table starts
  1137. (setq table-open t table-buffer nil table-orig-buffer nil))
  1138. ;; Accumulate lines
  1139. (setq table-buffer (cons line table-buffer)
  1140. table-orig-buffer (cons origline table-orig-buffer))
  1141. (when (or (not lines)
  1142. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1143. (car lines))))
  1144. (setq table-open nil
  1145. table-buffer (nreverse table-buffer)
  1146. table-orig-buffer (nreverse table-orig-buffer))
  1147. (org-close-par-maybe)
  1148. (insert (org-format-table-html table-buffer table-orig-buffer))))
  1149. (t
  1150. ;; Normal lines
  1151. (when (string-match
  1152. (cond
  1153. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1154. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1155. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1156. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  1157. line)
  1158. (setq ind (or (get-text-property 0 'original-indentation line)
  1159. (org-get-string-indentation line))
  1160. item-type (if (match-beginning 4) "o" "u")
  1161. starter (if (match-beginning 2)
  1162. (substring (match-string 2 line) 0 -1))
  1163. line (substring line (match-beginning 5))
  1164. item-tag nil)
  1165. (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
  1166. (setq item-type "d"
  1167. item-tag (match-string 1 line)
  1168. line (substring line (match-end 0))))
  1169. (when (and (not (equal item-type "d"))
  1170. (not (string-match "[^ \t]" line)))
  1171. ;; empty line. Pretend indentation is large.
  1172. (setq ind (if org-empty-line-terminates-plain-lists
  1173. 0
  1174. (1+ (or (car local-list-indent) 1)))))
  1175. (setq didclose nil)
  1176. (while (and in-local-list
  1177. (or (and (= ind (car local-list-indent))
  1178. (not starter))
  1179. (< ind (car local-list-indent))))
  1180. (setq didclose t)
  1181. (org-close-li (car local-list-type))
  1182. (insert (format "</%sl>\n" (car local-list-type)))
  1183. (pop local-list-type) (pop local-list-indent)
  1184. (setq in-local-list local-list-indent))
  1185. (cond
  1186. ((and starter
  1187. (or (not in-local-list)
  1188. (> ind (car local-list-indent))))
  1189. ;; Start new (level of) list
  1190. (org-close-par-maybe)
  1191. (insert (cond
  1192. ((equal item-type "u") "<ul>\n<li>\n")
  1193. ((equal item-type "o") "<ol>\n<li>\n")
  1194. ((equal item-type "d")
  1195. (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
  1196. (push item-type local-list-type)
  1197. (push ind local-list-indent)
  1198. (setq in-local-list t))
  1199. (starter
  1200. ;; continue current list
  1201. (org-close-li (car local-list-type))
  1202. (insert (cond
  1203. ((equal (car local-list-type) "d")
  1204. (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
  1205. (t "<li>\n"))))
  1206. (didclose
  1207. ;; we did close a list, normal text follows: need <p>
  1208. (org-open-par)))
  1209. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  1210. (setq line
  1211. (replace-match
  1212. (if (equal (match-string 1 line) "X")
  1213. "<b>[X]</b>"
  1214. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  1215. t t line))))
  1216. ;; Horizontal line
  1217. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1218. (if org-par-open
  1219. (insert "\n</p>\n<hr/>\n<p>\n")
  1220. (insert "\n<hr/>\n"))
  1221. (throw 'nextline nil))
  1222. ;; Empty lines start a new paragraph. If hand-formatted lists
  1223. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1224. ;; also start a new paragraph.
  1225. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  1226. ;; Is this the start of a footnote?
  1227. (when org-export-with-footnotes
  1228. (when (and (boundp 'footnote-section-tag-regexp)
  1229. (string-match (concat "^" footnote-section-tag-regexp)
  1230. line))
  1231. ;; ignore this line
  1232. (throw 'nextline nil))
  1233. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1234. (org-close-par-maybe)
  1235. (let ((n (match-string 1 line)))
  1236. (setq org-par-open t
  1237. line (replace-match
  1238. (format
  1239. (concat "<p class=\"footnote\">"
  1240. (format org-export-html-footnote-format
  1241. "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>"))
  1242. n n n) t t line)))))
  1243. ;; Check if the line break needs to be conserved
  1244. (cond
  1245. ((string-match "\\\\\\\\[ \t]*$" line)
  1246. (setq line (replace-match "<br/>" t t line)))
  1247. (org-export-preserve-breaks
  1248. (setq line (concat line "<br/>"))))
  1249. ;; Check if a paragraph should be started
  1250. (let ((start 0))
  1251. (while (and org-par-open
  1252. (string-match "\\\\par\\>" line start))
  1253. ;; Leave a space in the </p> so that the footnote matcher
  1254. ;; does not see this.
  1255. (if (not (get-text-property (match-beginning 0)
  1256. 'org-protected line))
  1257. (setq line (replace-match "</p ><p >" t t line)))
  1258. (setq start (match-end 0))))
  1259. (insert line "\n")))))
  1260. ;; Properly close all local lists and other lists
  1261. (when inquote
  1262. (insert "</pre>\n")
  1263. (org-open-par))
  1264. (when in-local-list
  1265. ;; Close any local lists before inserting a new header line
  1266. (while local-list-type
  1267. (org-close-li (car local-list-type))
  1268. (insert (format "</%sl>\n" (car local-list-type)))
  1269. (pop local-list-type))
  1270. (setq local-list-indent nil
  1271. in-local-list nil))
  1272. (org-html-level-start 1 nil umax
  1273. (and org-export-with-toc (<= level umax))
  1274. head-count)
  1275. ;; the </div> to close the last text-... div.
  1276. (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
  1277. (save-excursion
  1278. (goto-char (point-min))
  1279. (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
  1280. (push (match-string 0) footnotes)
  1281. (replace-match "" t t)))
  1282. (when footnotes
  1283. (insert (format org-export-html-footnotes-section
  1284. (nth 4 lang-words)
  1285. (mapconcat 'identity (nreverse footnotes) "\n"))
  1286. "\n"))
  1287. (let ((bib (org-export-html-get-bibliography)))
  1288. (when bib
  1289. (insert "\n" bib "\n")))
  1290. (unless body-only
  1291. (when (plist-get opt-plist :auto-postamble)
  1292. (insert "<div id=\"postamble\">\n")
  1293. (when (and org-export-author-info author)
  1294. (insert "<p class=\"author\"> "
  1295. (nth 1 lang-words) ": " author "\n")
  1296. (when email
  1297. (if (listp (split-string email ",+ *"))
  1298. (mapc (lambda(e)
  1299. (insert "<a href=\"mailto:" e "\">&lt;"
  1300. e "&gt;</a>\n"))
  1301. (split-string email ",+ *"))
  1302. (insert "<a href=\"mailto:" email "\">&lt;"
  1303. email "&gt;</a>\n")))
  1304. (insert "</p>\n"))
  1305. (when (and date org-export-time-stamp-file)
  1306. (insert "<p class=\"date\"> "
  1307. (nth 2 lang-words) ": "
  1308. date "</p>\n"))
  1309. (when org-export-creator-info
  1310. (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
  1311. org-version emacs-major-version)))
  1312. (when org-export-html-validation-link
  1313. (insert org-export-html-validation-link "\n"))
  1314. (insert "</div>"))
  1315. (if org-export-html-with-timestamp
  1316. (insert org-export-html-html-helper-timestamp))
  1317. (org-export-html-insert-plist-item opt-plist :postamble opt-plist)
  1318. (insert "\n</div>\n</body>\n</html>\n"))
  1319. (unless (plist-get opt-plist :buffer-will-be-killed)
  1320. (normal-mode)
  1321. (if (eq major-mode (default-value 'major-mode))
  1322. (html-mode)))
  1323. ;; insert the table of contents
  1324. (goto-char (point-min))
  1325. (when thetoc
  1326. (if (or (re-search-forward
  1327. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1328. (re-search-forward
  1329. "\\[TABLE-OF-CONTENTS\\]" nil t))
  1330. (progn
  1331. (goto-char (match-beginning 0))
  1332. (replace-match ""))
  1333. (goto-char first-heading-pos)
  1334. (when (looking-at "\\s-*</p>")
  1335. (goto-char (match-end 0))
  1336. (insert "\n")))
  1337. (insert "<div id=\"table-of-contents\">\n")
  1338. (mapc 'insert thetoc)
  1339. (insert "</div>\n"))
  1340. ;; remove empty paragraphs and lists
  1341. (goto-char (point-min))
  1342. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  1343. (replace-match ""))
  1344. (goto-char (point-min))
  1345. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  1346. (replace-match ""))
  1347. (goto-char (point-min))
  1348. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  1349. (replace-match ""))
  1350. ;; Convert whitespace place holders
  1351. (goto-char (point-min))
  1352. (let (beg end n)
  1353. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  1354. (setq n (get-text-property beg 'org-whitespace)
  1355. end (next-single-property-change beg 'org-whitespace))
  1356. (goto-char beg)
  1357. (delete-region beg end)
  1358. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  1359. (make-string n ?x)))))
  1360. ;; Remove empty lines at the beginning of the file.
  1361. (goto-char (point-min))
  1362. (when (looking-at "\\s-+\n") (replace-match ""))
  1363. ;; Remove display properties
  1364. (remove-text-properties (point-min) (point-max) '(display t))
  1365. ;; Run the hook
  1366. (run-hooks 'org-export-html-final-hook)
  1367. (or to-buffer (save-buffer))
  1368. (goto-char (point-min))
  1369. (or (org-export-push-to-kill-ring "HTML")
  1370. (message "Exporting... done"))
  1371. (if (eq to-buffer 'string)
  1372. (prog1 (buffer-substring (point-min) (point-max))
  1373. (kill-buffer (current-buffer)))
  1374. (current-buffer)))))
  1375. (defun org-export-html-insert-plist-item (plist key &rest args)
  1376. (let ((item (plist-get plist key)))
  1377. (cond ((functionp item)
  1378. (apply item args))
  1379. (item
  1380. (insert item)))))
  1381. (defun org-export-html-format-href (s)
  1382. "Make sure the S is valid as a href reference in an XHTML document."
  1383. (save-match-data
  1384. (let ((start 0))
  1385. (while (string-match "&" s start)
  1386. (setq start (+ (match-beginning 0) 3)
  1387. s (replace-match "&amp;" t t s)))))
  1388. s)
  1389. (defun org-export-html-format-desc (s)
  1390. "Make sure the S is valid as a description in a link."
  1391. (if (and s (not (get-text-property 1 'org-protected s)))
  1392. (save-match-data
  1393. (org-html-do-expand s))
  1394. s))
  1395. (defun org-export-html-format-image (src par-open)
  1396. "Create image tag with source and attributes."
  1397. (save-match-data
  1398. (if (string-match "^ltxpng/" src)
  1399. (format "<img src=\"%s\"/>" src)
  1400. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1401. (attr (org-find-text-property-in-string 'org-attributes src))
  1402. (label (org-find-text-property-in-string 'org-label src)))
  1403. (concat
  1404. (if caption
  1405. (format "%s<div %sclass=\"figure\">
  1406. <p>"
  1407. (if org-par-open "</p>\n" "")
  1408. (if label (format "id=\"%s\" " label) "")))
  1409. (format "<img src=\"%s\"%s />"
  1410. src
  1411. (if (string-match "\\<alt=" (or attr ""))
  1412. (concat " " attr )
  1413. (concat " " attr " alt=\"" src "\"")))
  1414. (if caption
  1415. (format "</p>%s
  1416. </div>%s"
  1417. (concat "\n<p>" caption "</p>")
  1418. (if org-par-open "\n<p>" ""))))))))
  1419. (defun org-export-html-get-bibliography ()
  1420. "Find bibliography, cut it out and return it."
  1421. (catch 'exit
  1422. (let (beg end (cnt 1) bib)
  1423. (save-excursion
  1424. (goto-char (point-min))
  1425. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  1426. (setq beg (match-beginning 0))
  1427. (while (re-search-forward "</?div\\>" nil t)
  1428. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  1429. (when (= cnt 0)
  1430. (and (looking-at ">") (forward-char 1))
  1431. (setq bib (buffer-substring beg (point)))
  1432. (delete-region beg (point))
  1433. (throw 'exit bib))))
  1434. nil))))
  1435. (defvar org-table-number-regexp) ; defined in org-table.el
  1436. (defun org-format-table-html (lines olines)
  1437. "Find out which HTML converter to use and return the HTML code."
  1438. (if (stringp lines)
  1439. (setq lines (org-split-string lines "\n")))
  1440. (if (string-match "^[ \t]*|" (car lines))
  1441. ;; A normal org table
  1442. (org-format-org-table-html lines)
  1443. ;; Table made by table.el - test for spanning
  1444. (let* ((hlines (delq nil (mapcar
  1445. (lambda (x)
  1446. (if (string-match "^[ \t]*\\+-" x) x
  1447. nil))
  1448. lines)))
  1449. (first (car hlines))
  1450. (ll (and (string-match "\\S-+" first)
  1451. (match-string 0 first)))
  1452. (re (concat "^[ \t]*" (regexp-quote ll)))
  1453. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  1454. hlines))))
  1455. (if (and (not spanning)
  1456. (not org-export-prefer-native-exporter-for-tables))
  1457. ;; We can use my own converter with HTML conversions
  1458. (org-format-table-table-html lines)
  1459. ;; Need to use the code generator in table.el, with the original text.
  1460. (org-format-table-table-html-using-table-generate-source olines)))))
  1461. (defvar org-table-number-fraction) ; defined in org-table.el
  1462. (defun org-format-org-table-html (lines &optional splice)
  1463. "Format a table into HTML."
  1464. (require 'org-table)
  1465. ;; Get rid of hlines at beginning and end
  1466. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1467. (setq lines (nreverse lines))
  1468. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1469. (setq lines (nreverse lines))
  1470. (when org-export-table-remove-special-lines
  1471. ;; Check if the table has a marking column. If yes remove the
  1472. ;; column and the special lines
  1473. (setq lines (org-table-clean-before-export lines)))
  1474. (let* ((caption (or (get-text-property 0 'org-caption (car lines))
  1475. (get-text-property (or (next-single-property-change
  1476. 0 'org-caption (car lines))
  1477. 0)
  1478. 'org-caption (car lines))))
  1479. (attributes (or (get-text-property 0 'org-attributes (car lines))
  1480. (get-text-property (or (next-single-property-change
  1481. 0 'org-attributes (car lines))
  1482. 0)
  1483. 'org-attributes (car lines))))
  1484. (html-table-tag (org-export-splice-attributes
  1485. html-table-tag attributes))
  1486. (head (and org-export-highlight-first-table-line
  1487. (delq nil (mapcar
  1488. (lambda (x) (string-match "^[ \t]*|-" x))
  1489. (cdr lines)))))
  1490. (nline 0) fnum i
  1491. tbopen line fields html gr colgropen rowstart rowend)
  1492. (if splice (setq head nil))
  1493. (unless splice (push (if head "<thead>" "<tbody>") html))
  1494. (setq tbopen t)
  1495. (while (setq line (pop lines))
  1496. (catch 'next-line
  1497. (if (string-match "^[ \t]*|-" line)
  1498. (progn
  1499. (unless splice
  1500. (push (if head "</thead>" "</tbody>") html)
  1501. (if lines (push "<tbody>" html) (setq tbopen nil)))
  1502. (setq head nil) ;; head ends here, first time around
  1503. ;; ignore this line
  1504. (throw 'next-line t)))
  1505. ;; Break the line into fields
  1506. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1507. (unless fnum (setq fnum (make-vector (length fields) 0)))
  1508. (setq nline (1+ nline) i -1
  1509. rowstart (eval (car org-export-table-row-tags))
  1510. rowend (eval (cdr org-export-table-row-tags)))
  1511. (push (concat rowstart
  1512. (mapconcat
  1513. (lambda (x)
  1514. (setq i (1+ i))
  1515. (if (and (< i nline)
  1516. (string-match org-table-number-regexp x))
  1517. (incf (aref fnum i)))
  1518. (cond
  1519. (head
  1520. (concat
  1521. (format (car org-export-table-header-tags) "col")
  1522. x
  1523. (cdr org-export-table-header-tags)))
  1524. ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
  1525. (concat
  1526. (format (car org-export-table-header-tags) "row")
  1527. x
  1528. (cdr org-export-table-header-tags)))
  1529. (t
  1530. (concat (car org-export-table-data-tags) x
  1531. (cdr org-export-table-data-tags)))))
  1532. fields "")
  1533. rowend)
  1534. html)))
  1535. (unless splice (if tbopen (push "</tbody>" html)))
  1536. (unless splice (push "</table>\n" html))
  1537. (setq html (nreverse html))
  1538. (unless splice
  1539. ;; Put in col tags with the alignment (unfortunately often ignored...)
  1540. (unless (car org-table-colgroup-info)
  1541. (setq org-table-colgroup-info
  1542. (cons :start (cdr org-table-colgroup-info))))
  1543. (push (mapconcat
  1544. (lambda (x)
  1545. (setq gr (pop org-table-colgroup-info))
  1546. (format "%s<col align=\"%s\" />%s"
  1547. (if (memq gr '(:start :startend))
  1548. (prog1
  1549. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  1550. (setq colgropen t))
  1551. "")
  1552. (if (> (/ (float x) nline) org-table-number-fraction)
  1553. "right" "left")
  1554. (if (memq gr '(:end :startend))
  1555. (progn (setq colgropen nil) "</colgroup>")
  1556. "")))
  1557. fnum "")
  1558. html)
  1559. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  1560. ;; Since the output of HTML table formatter can also be used in
  1561. ;; DocBook document, we want to always include the caption to make
  1562. ;; DocBook XML file valid.
  1563. (push (format "<caption>%s</caption>" (or caption "")) html)
  1564. (push html-table-tag html))
  1565. (concat (mapconcat 'identity html "\n") "\n")))
  1566. (defun org-export-splice-attributes (tag attributes)
  1567. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  1568. (if (not attributes)
  1569. tag
  1570. (let (oldatt newatt)
  1571. (setq oldatt (org-extract-attributes-from-string tag)
  1572. tag (pop oldatt)
  1573. newatt (cdr (org-extract-attributes-from-string attributes)))
  1574. (while newatt
  1575. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  1576. (if (string-match ">" tag)
  1577. (setq tag
  1578. (replace-match (concat (org-attributes-to-string oldatt) ">")
  1579. t t tag)))
  1580. tag)))
  1581. (defun org-format-table-table-html (lines)
  1582. "Format a table generated by table.el into HTML.
  1583. This conversion does *not* use `table-generate-source' from table.el.
  1584. This has the advantage that Org-mode's HTML conversions can be used.
  1585. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1586. (let (line field-buffer
  1587. (head org-export-highlight-first-table-line)
  1588. fields html empty i)
  1589. (setq html (concat html-table-tag "\n"))
  1590. (while (setq line (pop lines))
  1591. (setq empty "&nbsp;")
  1592. (catch 'next-line
  1593. (if (string-match "^[ \t]*\\+-" line)
  1594. (progn
  1595. (if field-buffer
  1596. (progn
  1597. (setq
  1598. html
  1599. (concat
  1600. html
  1601. "<tr>"
  1602. (mapconcat
  1603. (lambda (x)
  1604. (if (equal x "") (setq x empty))
  1605. (if head
  1606. (concat
  1607. (format (car org-export-table-header-tags) "col")
  1608. x
  1609. (cdr org-export-table-header-tags))
  1610. (concat (car org-export-table-data-tags) x
  1611. (cdr org-export-table-data-tags))))
  1612. field-buffer "\n")
  1613. "</tr>\n"))
  1614. (setq head nil)
  1615. (setq field-buffer nil)))
  1616. ;; Ignore this line
  1617. (throw 'next-line t)))
  1618. ;; Break the line into fields and store the fields
  1619. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1620. (if field-buffer
  1621. (setq field-buffer (mapcar
  1622. (lambda (x)
  1623. (concat x "<br/>" (pop fields)))
  1624. field-buffer))
  1625. (setq field-buffer fields))))
  1626. (setq html (concat html "</table>\n"))
  1627. html))
  1628. (defun org-format-table-table-html-using-table-generate-source (lines)
  1629. "Format a table into html, using `table-generate-source' from table.el.
  1630. This has the advantage that cell- or row-spanning is allowed.
  1631. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  1632. (require 'table)
  1633. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1634. (erase-buffer)
  1635. (insert (mapconcat 'identity lines "\n"))
  1636. (goto-char (point-min))
  1637. (if (not (re-search-forward "|[^+]" nil t))
  1638. (error "Error processing table"))
  1639. (table-recognize-table)
  1640. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1641. (table-generate-source 'html " org-tmp2 ")
  1642. (set-buffer " org-tmp2 ")
  1643. (buffer-substring (point-min) (point-max))))
  1644. (defun org-export-splice-style (style extra)
  1645. "Splice EXTRA into STYLE, just before \"</style>\"."
  1646. (if (and (stringp extra)
  1647. (string-match "\\S-" extra)
  1648. (string-match "</style>" style))
  1649. (concat (substring style 0 (match-beginning 0))
  1650. "\n" extra "\n"
  1651. (substring style (match-beginning 0)))
  1652. style))
  1653. (defun org-html-handle-time-stamps (s)
  1654. "Format time stamps in string S, or remove them."
  1655. (catch 'exit
  1656. (let (r b)
  1657. (while (string-match org-maybe-keyword-time-regexp s)
  1658. (or b (setq b (substring s 0 (match-beginning 0))))
  1659. (setq r (concat
  1660. r (substring s 0 (match-beginning 0))
  1661. " @<span class=\"timestamp-wrapper\">"
  1662. (if (match-end 1)
  1663. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  1664. (match-string 1 s)))
  1665. (format " @<span class=\"timestamp\">%s@</span>"
  1666. (substring
  1667. (org-translate-time (match-string 3 s)) 1 -1))
  1668. "@</span>")
  1669. s (substring s (match-end 0))))
  1670. ;; Line break if line started and ended with time stamp stuff
  1671. (if (not r)
  1672. s
  1673. (setq r (concat r s))
  1674. (unless (string-match "\\S-" (concat b s))
  1675. (setq r (concat r "@<br/>")))
  1676. r))))
  1677. (defvar htmlize-buffer-places) ; from htmlize.el
  1678. (defun org-export-htmlize-region-for-paste (beg end)
  1679. "Convert the region to HTML, using htmlize.el.
  1680. This is much like `htmlize-region-for-paste', only that it uses
  1681. the settings define in the org-... variables."
  1682. (let* ((htmlize-output-type org-export-htmlize-output-type)
  1683. (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
  1684. (htmlbuf (htmlize-region beg end)))
  1685. (unwind-protect
  1686. (with-current-buffer htmlbuf
  1687. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  1688. (plist-get htmlize-buffer-places 'content-end)))
  1689. (kill-buffer htmlbuf))))
  1690. ;;;###autoload
  1691. (defun org-export-htmlize-generate-css ()
  1692. "Create the CSS for all font definitions in the current Emacs session.
  1693. Use this to create face definitions in your CSS style file that can then
  1694. be used by code snippets transformed by htmlize.
  1695. This command just produces a buffer that contains class definitions for all
  1696. faces used in the current Emacs session. You can copy and paste the ones you
  1697. need into your CSS file.
  1698. If you then set `org-export-htmlize-output-type' to `css', calls to
  1699. the function `org-export-htmlize-region-for-paste' will produce code
  1700. that uses these same face definitions."
  1701. (interactive)
  1702. (require 'htmlize)
  1703. (and (get-buffer "*html*") (kill-buffer "*html*"))
  1704. (with-temp-buffer
  1705. (let ((fl (face-list))
  1706. (htmlize-css-name-prefix "org-")
  1707. (htmlize-output-type 'css)
  1708. f i)
  1709. (while (setq f (pop fl)
  1710. i (and f (face-attribute f :inherit)))
  1711. (when (and (symbolp f) (or (not i) (not (listp i))))
  1712. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  1713. (htmlize-region (point-min) (point-max))))
  1714. (switch-to-buffer "*html*")
  1715. (goto-char (point-min))
  1716. (if (re-search-forward "<style" nil t)
  1717. (delete-region (point-min) (match-beginning 0)))
  1718. (if (re-search-forward "</style>" nil t)
  1719. (delete-region (1+ (match-end 0)) (point-max)))
  1720. (beginning-of-line 1)
  1721. (if (looking-at " +") (replace-match ""))
  1722. (goto-char (point-min)))
  1723. (defun org-html-protect (s)
  1724. ;; convert & to &amp;, < to &lt; and > to &gt;
  1725. (let ((start 0))
  1726. (while (string-match "&" s start)
  1727. (setq s (replace-match "&amp;" t t s)
  1728. start (1+ (match-beginning 0))))
  1729. (while (string-match "<" s)
  1730. (setq s (replace-match "&lt;" t t s)))
  1731. (while (string-match ">" s)
  1732. (setq s (replace-match "&gt;" t t s)))
  1733. ; (while (string-match "\"" s)
  1734. ; (setq s (replace-match "&quot;" t t s)))
  1735. )
  1736. s)
  1737. (defun org-html-expand (string)
  1738. "Prepare STRING for HTML export. Applies all active conversions.
  1739. If there are links in the string, don't modify these."
  1740. (let* ((re (concat org-bracket-link-regexp "\\|"
  1741. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  1742. m s l res)
  1743. (while (setq m (string-match re string))
  1744. (setq s (substring string 0 m)
  1745. l (match-string 0 string)
  1746. string (substring string (match-end 0)))
  1747. (push (org-html-do-expand s) res)
  1748. (push l res))
  1749. (push (org-html-do-expand string) res)
  1750. (apply 'concat (nreverse res))))
  1751. (defun org-html-do-expand (s)
  1752. "Apply all active conversions to translate special ASCII to HTML."
  1753. (setq s (org-html-protect s))
  1754. (if org-export-html-expand
  1755. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1756. (setq s (replace-match "<\\1>" t nil s))))
  1757. (if org-export-with-emphasize
  1758. (setq s (org-export-html-convert-emphasize s)))
  1759. (if org-export-with-special-strings
  1760. (setq s (org-export-html-convert-special-strings s)))
  1761. (if org-export-with-sub-superscripts
  1762. (setq s (org-export-html-convert-sub-super s)))
  1763. (if org-export-with-TeX-macros
  1764. (let ((start 0) wd ass)
  1765. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1766. s start))
  1767. (if (get-text-property (match-beginning 0) 'org-protected s)
  1768. (setq start (match-end 0))
  1769. (setq wd (match-string 1 s))
  1770. (if (setq ass (assoc wd org-html-entities))
  1771. (setq s (replace-match (or (cdr ass)
  1772. (concat "&" (car ass) ";"))
  1773. t t s))
  1774. (setq start (+ start (length wd))))))))
  1775. s)
  1776. (defun org-export-html-convert-special-strings (string)
  1777. "Convert special characters in STRING to HTML."
  1778. (let ((all org-export-html-special-string-regexps)
  1779. e a re rpl start)
  1780. (while (setq a (pop all))
  1781. (setq re (car a) rpl (cdr a) start 0)
  1782. (while (string-match re string start)
  1783. (if (get-text-property (match-beginning 0) 'org-protected string)
  1784. (setq start (match-end 0))
  1785. (setq string (replace-match rpl t nil string)))))
  1786. string))
  1787. (defun org-export-html-convert-sub-super (string)
  1788. "Convert sub- and superscripts in STRING to HTML."
  1789. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1790. (while (string-match org-match-substring-regexp string s)
  1791. (cond
  1792. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1793. ((get-text-property (match-beginning 2) 'org-protected string)
  1794. (setq s (match-end 2)))
  1795. (t
  1796. (setq s (match-end 1)
  1797. key (if (string= (match-string 2 string) "_") "sub" "sup")
  1798. c (or (match-string 8 string)
  1799. (match-string 6 string)
  1800. (match-string 5 string))
  1801. string (replace-match
  1802. (concat (match-string 1 string)
  1803. "<" key ">" c "</" key ">")
  1804. t t string)))))
  1805. (while (string-match "\\\\\\([_^]\\)" string)
  1806. (setq string (replace-match (match-string 1 string) t t string)))
  1807. string))
  1808. (defun org-export-html-convert-emphasize (string)
  1809. "Apply emphasis."
  1810. (let ((s 0) rpl)
  1811. (while (string-match org-emph-re string s)
  1812. (if (not (equal
  1813. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1814. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1815. (setq s (match-beginning 0)
  1816. rpl
  1817. (concat
  1818. (match-string 1 string)
  1819. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  1820. (match-string 4 string)
  1821. (nth 3 (assoc (match-string 3 string)
  1822. org-emphasis-alist))
  1823. (match-string 5 string))
  1824. string (replace-match rpl t t string)
  1825. s (+ s (- (length rpl) 2)))
  1826. (setq s (1+ s))))
  1827. string))
  1828. (defun org-open-par ()
  1829. "Insert <p>, but first close previous paragraph if any."
  1830. (org-close-par-maybe)
  1831. (insert "\n<p>")
  1832. (setq org-par-open t))
  1833. (defun org-close-par-maybe ()
  1834. "Close paragraph if there is one open."
  1835. (when org-par-open
  1836. (insert "</p>")
  1837. (setq org-par-open nil)))
  1838. (defun org-close-li (&optional type)
  1839. "Close <li> if necessary."
  1840. (org-close-par-maybe)
  1841. (insert (if (equal type "d") "</dd>\n" "</li>\n")))
  1842. (defvar in-local-list)
  1843. (defvar local-list-indent)
  1844. (defvar local-list-type)
  1845. (defun org-export-html-close-lists-maybe (line)
  1846. (let ((ind (or (get-text-property 0 'original-indentation line)))
  1847. ; (and (string-match "\\S-" line)
  1848. ; (org-get-indentation line))))
  1849. didclose)
  1850. (when ind
  1851. (while (and in-local-list
  1852. (<= ind (car local-list-indent)))
  1853. (setq didclose t)
  1854. (org-close-li (car local-list-type))
  1855. (insert (format "</%sl>\n" (car local-list-type)))
  1856. (pop local-list-type) (pop local-list-indent)
  1857. (setq in-local-list local-list-indent))
  1858. (and didclose (org-open-par)))))
  1859. (defvar body-only) ; dynamically scoped into this.
  1860. (defun org-html-level-start (level title umax with-toc head-count)
  1861. "Insert a new level in HTML export.
  1862. When TITLE is nil, just close all open levels."
  1863. (org-close-par-maybe)
  1864. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1865. (extra-targets (assoc target org-export-target-aliases))
  1866. (preferred (cdr (assoc target org-export-preferred-target-alist)))
  1867. (remove (or preferred target))
  1868. (l org-level-max)
  1869. snumber href suffix)
  1870. (setq extra-targets (remove remove extra-targets))
  1871. (setq extra-targets
  1872. (mapconcat (lambda (x)
  1873. (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
  1874. (format "<a name=\"%s\" id=\"%s\"></a>"
  1875. x x))
  1876. extra-targets
  1877. ""))
  1878. (while (>= l level)
  1879. (if (aref org-levels-open (1- l))
  1880. (progn
  1881. (org-html-level-close l umax)
  1882. (aset org-levels-open (1- l) nil)))
  1883. (setq l (1- l)))
  1884. (when title
  1885. ;; If title is nil, this means this function is called to close
  1886. ;; all levels, so the rest is done only if title is given
  1887. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1888. (setq title (replace-match
  1889. (if org-export-with-tags
  1890. (save-match-data
  1891. (concat
  1892. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  1893. (mapconcat
  1894. (lambda (x)
  1895. (format "<span class=\"%s\">%s</span>"
  1896. (org-export-html-get-tag-class-name x)
  1897. x))
  1898. (org-split-string (match-string 1 title) ":")
  1899. "&nbsp;")
  1900. "</span>"))
  1901. "")
  1902. t t title)))
  1903. (if (> level umax)
  1904. (progn
  1905. (if (aref org-levels-open (1- level))
  1906. (progn
  1907. (org-close-li)
  1908. (if target
  1909. (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
  1910. (insert "<li>" title "<br/>\n")))
  1911. (aset org-levels-open (1- level) t)
  1912. (org-close-par-maybe)
  1913. (if target
  1914. (insert (format "<ul>\n<li id=\"%s\">" target)
  1915. extra-targets title "<br/>\n")
  1916. (insert "<ul>\n<li>" title "<br/>\n"))))
  1917. (aset org-levels-open (1- level) t)
  1918. (setq snumber (org-section-number level))
  1919. (setq level (+ level org-export-html-toplevel-hlevel -1))
  1920. (if (and org-export-with-section-numbers (not body-only))
  1921. (setq title (concat
  1922. (format "<span class=\"section-number-%d\">%s</span>"
  1923. level snumber)
  1924. " " title)))
  1925. (unless (= head-count 1) (insert "\n</div>\n"))
  1926. (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
  1927. (setq suffix (or href snumber))
  1928. (setq href (or href (concat "sec-" snumber)))
  1929. (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"
  1930. suffix level level href
  1931. extra-targets
  1932. title level level suffix))
  1933. (org-open-par)))))
  1934. (defun org-export-html-get-tag-class-name (tag)
  1935. "Turn tag into a valid class name.
  1936. Replaces invalid characters with \"_\" and then prepends a prefix."
  1937. (save-match-data
  1938. (while (string-match "[^a-zA-Z0-9_]" tag)
  1939. (setq tag (replace-match "_" t t tag))))
  1940. (concat org-export-html-tag-class-prefix tag))
  1941. (defun org-export-html-get-todo-kwd-class-name (kwd)
  1942. "Turn todo keyword into a valid class name.
  1943. Replaces invalid characters with \"_\" and then prepends a prefix."
  1944. (save-match-data
  1945. (while (string-match "[^a-zA-Z0-9_]" kwd)
  1946. (setq kwd (replace-match "_" t t kwd))))
  1947. (concat org-export-html-todo-kwd-class-prefix kwd))
  1948. (defun org-html-level-close (level max-outline-level)
  1949. "Terminate one level in HTML export."
  1950. (if (<= level max-outline-level)
  1951. (insert "</div>\n")
  1952. (org-close-li)
  1953. (insert "</ul>\n")))
  1954. (provide 'org-html)
  1955. ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
  1956. ;;; org-html.el ends here