org-html.el 69 KB

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