org-html.el 84 KB

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