org-html.el 72 KB

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