org-xhtml.el 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. ;;; org-xhtml.el --- XHTML export for Org-mode (uses org-lparse)
  2. ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.8
  7. ;; This file is not (yet) part of GNU Emacs.
  8. ;; However, it is distributed under the same license.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;; Code:
  23. (require 'org-exp)
  24. (require 'format-spec)
  25. (require 'org-lparse)
  26. (eval-when-compile (require 'cl) (require 'table) (require 'browse-url))
  27. (declare-function org-id-find-id-file "org-id" (id))
  28. (declare-function htmlize-region "ext:htmlize" (beg end))
  29. (defgroup org-export-xhtml nil
  30. "Options specific for HTML export of Org-mode files."
  31. :tag "Org Export HTML"
  32. :group 'org-export)
  33. (defconst org-export-xhtml-special-string-regexps
  34. '(("\\\\-" . "&shy;")
  35. ("---\\([^-]\\)" . "&mdash;\\1")
  36. ("--\\([^-]\\)" . "&ndash;\\1")
  37. ("\\.\\.\\." . "&hellip;"))
  38. "Regular expressions for special string conversion.")
  39. (defcustom org-export-xhtml-footnotes-section "<div id=\"footnotes\">
  40. <h2 class=\"footnotes\">%s: </h2>
  41. <div id=\"text-footnotes\">
  42. %s
  43. </div>
  44. </div>"
  45. "Format for the footnotes section.
  46. Should contain a two instances of %s. The first will be replaced with the
  47. language-specific word for \"Footnotes\", the second one will be replaced
  48. by the footnotes themselves."
  49. :group 'org-export-xhtml
  50. :type 'string)
  51. (defcustom org-export-xhtml-footnote-format "<sup>%s</sup>"
  52. "The format for the footnote reference.
  53. %s will be replaced by the footnote reference itself."
  54. :group 'org-export-xhtml
  55. :type 'string)
  56. (defcustom org-export-xhtml-footnote-separator "<sup>, </sup>"
  57. "Text used to separate footnotes."
  58. :group 'org-export-xhtml
  59. :type 'string)
  60. (defcustom org-export-xhtml-coding-system nil
  61. "Coding system for HTML export, defaults to `buffer-file-coding-system'."
  62. :group 'org-export-xhtml
  63. :type 'coding-system)
  64. (defcustom org-export-xhtml-extension "html"
  65. "The extension for exported HTML files."
  66. :group 'org-export-xhtml
  67. :type 'string)
  68. (defcustom org-export-xhtml-xml-declaration
  69. '(("html" . "<?xml version=\"1.0\" encoding=\"%s\"?>")
  70. ("php" . "<?php echo \"<?xml version=\\\"1.0\\\" encoding=\\\"%s\\\" ?>\"; ?>"))
  71. "The extension for exported HTML files.
  72. %s will be replaced with the charset of the exported file.
  73. This may be a string, or an alist with export extensions
  74. and corresponding declarations."
  75. :group 'org-export-xhtml
  76. :type '(choice
  77. (string :tag "Single declaration")
  78. (repeat :tag "Dependent on extension"
  79. (cons (string :tag "Extension")
  80. (string :tag "Declaration")))))
  81. (defcustom org-export-xhtml-style-include-scripts t
  82. "Non-nil means include the JavaScript snippets in exported HTML files.
  83. The actual script is defined in `org-export-xhtml-scripts' and should
  84. not be modified."
  85. :group 'org-export-xhtml
  86. :type 'boolean)
  87. (defconst org-export-xhtml-scripts
  88. "<script type=\"text/javascript\">
  89. <!--/*--><![CDATA[/*><!--*/
  90. function CodeHighlightOn(elem, id)
  91. {
  92. var target = document.getElementById(id);
  93. if(null != target) {
  94. elem.cacheClassElem = elem.className;
  95. elem.cacheClassTarget = target.className;
  96. target.className = \"code-highlighted\";
  97. elem.className = \"code-highlighted\";
  98. }
  99. }
  100. function CodeHighlightOff(elem, id)
  101. {
  102. var target = document.getElementById(id);
  103. if(elem.cacheClassElem)
  104. elem.className = elem.cacheClassElem;
  105. if(elem.cacheClassTarget)
  106. target.className = elem.cacheClassTarget;
  107. }
  108. /*]]>*///-->
  109. </script>"
  110. "Basic JavaScript that is needed by HTML files produced by Org-mode.")
  111. (defconst org-export-xhtml-style-default
  112. "<style type=\"text/css\">
  113. <!--/*--><![CDATA[/*><!--*/
  114. html { font-family: Times, serif; font-size: 12pt; }
  115. .title { text-align: center; }
  116. .todo { color: red; }
  117. .done { color: green; }
  118. .tag { background-color: #add8e6; font-weight:normal }
  119. .target { }
  120. .timestamp { color: #bebebe; }
  121. .timestamp-kwd { color: #5f9ea0; }
  122. .right {margin-left:auto; margin-right:0px; text-align:right;}
  123. .left {margin-left:0px; margin-right:auto; text-align:left;}
  124. .center {margin-left:auto; margin-right:auto; text-align:center;}
  125. p.verse { margin-left: 3% }
  126. pre {
  127. border: 1pt solid #AEBDCC;
  128. background-color: #F3F5F7;
  129. padding: 5pt;
  130. font-family: courier, monospace;
  131. font-size: 90%;
  132. overflow:auto;
  133. }
  134. table { border-collapse: collapse; }
  135. td, th { vertical-align: top; }
  136. th.right { text-align:center; }
  137. th.left { text-align:center; }
  138. th.center { text-align:center; }
  139. td.right { text-align:right; }
  140. td.left { text-align:left; }
  141. td.center { text-align:center; }
  142. dt { font-weight: bold; }
  143. div.figure { padding: 0.5em; }
  144. div.figure p { text-align: center; }
  145. div.inlinetask {
  146. padding:10px;
  147. border:2px solid gray;
  148. margin:10px;
  149. background: #ffffcc;
  150. }
  151. textarea { overflow-x: auto; }
  152. .linenr { font-size:smaller }
  153. .code-highlighted {background-color:#ffff00;}
  154. .org-info-js_info-navigation { border-style:none; }
  155. #org-info-js_console-label { font-size:10px; font-weight:bold;
  156. white-space:nowrap; }
  157. .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
  158. font-weight:bold; }
  159. /*]]>*/-->
  160. </style>"
  161. "The default style specification for exported HTML files.
  162. Please use the variables `org-export-xhtml-style' and
  163. `org-export-xhtml-style-extra' to add to this style. If you wish to not
  164. have the default style included, customize the variable
  165. `org-export-xhtml-style-include-default'.")
  166. (defcustom org-export-xhtml-style-include-default t
  167. "Non-nil means include the default style in exported HTML files.
  168. The actual style is defined in `org-export-xhtml-style-default' and should
  169. not be modified. Use the variables `org-export-xhtml-style' to add
  170. your own style information."
  171. :group 'org-export-xhtml
  172. :type 'boolean)
  173. ;;;###autoload
  174. (put 'org-export-xhtml-style-include-default 'safe-local-variable 'booleanp)
  175. (defcustom org-export-xhtml-style ""
  176. "Org-wide style definitions for exported HTML files.
  177. This variable needs to contain the full HTML structure to provide a style,
  178. including the surrounding HTML tags. If you set the value of this variable,
  179. you should consider to include definitions for the following classes:
  180. title, todo, done, timestamp, timestamp-kwd, tag, target.
  181. For example, a valid value would be:
  182. <style type=\"text/css\">
  183. <![CDATA[
  184. p { font-weight: normal; color: gray; }
  185. h1 { color: black; }
  186. .title { text-align: center; }
  187. .todo, .timestamp-kwd { color: red; }
  188. .done { color: green; }
  189. ]]>
  190. </style>
  191. If you'd like to refer to an external style file, use something like
  192. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  193. As the value of this option simply gets inserted into the HTML <head> header,
  194. you can \"misuse\" it to add arbitrary text to the header.
  195. See also the variable `org-export-xhtml-style-extra'."
  196. :group 'org-export-xhtml
  197. :type 'string)
  198. ;;;###autoload
  199. (put 'org-export-xhtml-style 'safe-local-variable 'stringp)
  200. (defcustom org-export-xhtml-style-extra ""
  201. "Additional style information for HTML export.
  202. The value of this variable is inserted into the HTML buffer right after
  203. the value of `org-export-xhtml-style'. Use this variable for per-file
  204. settings of style information, and do not forget to surround the style
  205. settings with <style>...</style> tags."
  206. :group 'org-export-xhtml
  207. :type 'string)
  208. ;;;###autoload
  209. (put 'org-export-xhtml-style-extra 'safe-local-variable 'stringp)
  210. (defcustom org-export-xhtml-mathjax-options
  211. '((path "http://orgmode.org/mathjax/MathJax.js")
  212. (scale "100")
  213. (align "center")
  214. (indent "2em")
  215. (mathml nil))
  216. "Options for MathJax setup.
  217. path The path where to find MathJax
  218. scale Scaling for the HTML-CSS backend, usually between 100 and 133
  219. align How to align display math: left, center, or right
  220. indent If align is not center, how far from the left/right side?
  221. mathml Should a MathML player be used if available?
  222. This is faster and reduces bandwidth use, but currently
  223. sometimes has lower spacing quality. Therefore, the default is
  224. nil. When browsers get better, this switch can be flipped.
  225. You can also customize this for each buffer, using something like
  226. #+MATHJAX: scale:\"133\" align:\"right\" mathml:t path:\"/MathJax/\""
  227. :group 'org-export-xhtml
  228. :type '(list :greedy t
  229. (list :tag "path (the path from where to load MathJax.js)"
  230. (const :format " " path) (string))
  231. (list :tag "scale (scaling for the displayed math)"
  232. (const :format " " scale) (string))
  233. (list :tag "align (alignment of displayed equations)"
  234. (const :format " " align) (string))
  235. (list :tag "indent (indentation with left or right alignment)"
  236. (const :format " " indent) (string))
  237. (list :tag "mathml (should MathML display be used is possible)"
  238. (const :format " " mathml) (boolean))))
  239. (defun org-export-xhtml-mathjax-config (template options in-buffer)
  240. "Insert the user setup into the matchjax template."
  241. (let (name val (yes " ") (no "// ") x)
  242. (mapc
  243. (lambda (e)
  244. (setq name (car e) val (nth 1 e))
  245. (if (string-match (concat "\\<" (symbol-name name) ":") in-buffer)
  246. (setq val (car (read-from-string
  247. (substring in-buffer (match-end 0))))))
  248. (if (not (stringp val)) (setq val (format "%s" val)))
  249. (if (string-match (concat "%" (upcase (symbol-name name))) template)
  250. (setq template (replace-match val t t template))))
  251. options)
  252. (setq val (nth 1 (assq 'mathml options)))
  253. (if (string-match (concat "\\<mathml:") in-buffer)
  254. (setq val (car (read-from-string
  255. (substring in-buffer (match-end 0))))))
  256. ;; Exchange prefixes depending on mathml setting
  257. (if (not val) (setq x yes yes no no x))
  258. ;; Replace cookies to turn on or off the config/jax lines
  259. (if (string-match ":MMLYES:" template)
  260. (setq template (replace-match yes t t template)))
  261. (if (string-match ":MMLNO:" template)
  262. (setq template (replace-match no t t template)))
  263. ;; Return the modified template
  264. template))
  265. (defcustom org-export-xhtml-mathjax-template
  266. "<script type=\"text/javascript\" src=\"%PATH\">
  267. <!--/*--><![CDATA[/*><!--*/
  268. MathJax.Hub.Config({
  269. // Only one of the two following lines, depending on user settings
  270. // First allows browser-native MathML display, second forces HTML/CSS
  271. :MMLYES: config: [\"MMLorHTML.js\"], jax: [\"input/TeX\"],
  272. :MMLNO: jax: [\"input/TeX\", \"output/HTML-CSS\"],
  273. extensions: [\"tex2jax.js\",\"TeX/AMSmath.js\",\"TeX/AMSsymbols.js\",
  274. \"TeX/noUndefined.js\"],
  275. tex2jax: {
  276. inlineMath: [ [\"\\\\(\",\"\\\\)\"] ],
  277. displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"], [\"\\\\begin{displaymath}\",\"\\\\end{displaymath}\"] ],
  278. skipTags: [\"script\",\"noscript\",\"style\",\"textarea\",\"pre\",\"code\"],
  279. ignoreClass: \"tex2jax_ignore\",
  280. processEscapes: false,
  281. processEnvironments: true,
  282. preview: \"TeX\"
  283. },
  284. showProcessingMessages: true,
  285. displayAlign: \"%ALIGN\",
  286. displayIndent: \"%INDENT\",
  287. \"HTML-CSS\": {
  288. scale: %SCALE,
  289. availableFonts: [\"STIX\",\"TeX\"],
  290. preferredFont: \"TeX\",
  291. webFont: \"TeX\",
  292. imageFont: \"TeX\",
  293. showMathMenu: true,
  294. },
  295. MMLorHTML: {
  296. prefer: {
  297. MSIE: \"MML\",
  298. Firefox: \"MML\",
  299. Opera: \"HTML\",
  300. other: \"HTML\"
  301. }
  302. }
  303. });
  304. /*]]>*///-->
  305. </script>"
  306. "The MathJax setup for XHTML files."
  307. :group 'org-export-xhtml
  308. :type 'string)
  309. (defcustom org-export-xhtml-tag-class-prefix ""
  310. "Prefix to class names for TODO keywords.
  311. Each tag gets a class given by the tag itself, with this prefix.
  312. The default prefix is empty because it is nice to just use the keyword
  313. as a class name. But if you get into conflicts with other, existing
  314. CSS classes, then this prefix can be very useful."
  315. :group 'org-export-xhtml
  316. :type 'string)
  317. (defcustom org-export-xhtml-todo-kwd-class-prefix ""
  318. "Prefix to class names for TODO keywords.
  319. Each TODO keyword gets a class given by the keyword itself, with this prefix.
  320. The default prefix is empty because it is nice to just use the keyword
  321. as a class name. But if you get into conflicts with other, existing
  322. CSS classes, then this prefix can be very useful."
  323. :group 'org-export-xhtml
  324. :type 'string)
  325. (defcustom org-export-xhtml-preamble t
  326. "Non-nil means insert a preamble in HTML export.
  327. When `t', insert a string as defined by one of the formatting
  328. strings in `org-export-xhtml-preamble-format'. When set to a
  329. string, this string overrides `org-export-xhtml-preamble-format'.
  330. When set to a function, apply this function and insert the
  331. returned string. The function takes the property list of export
  332. options as its only argument.
  333. Setting :html-preamble in publishing projects will take
  334. precedence over this variable."
  335. :group 'org-export-xhtml
  336. :type '(choice (const :tag "No preamble" nil)
  337. (const :tag "Default preamble" t)
  338. (string :tag "Custom formatting string")
  339. (function :tag "Function (must return a string)")))
  340. (defcustom org-export-xhtml-preamble-format
  341. '(("en" "<h1 class=\"title\">%t</h1>"))
  342. "The format for the HTML preamble.
  343. %t stands for the title.
  344. If you need to use a \"%\" character, you need to escape it
  345. like that: \"%%\"."
  346. :group 'org-export-xhtml
  347. :type 'string)
  348. (defcustom org-export-xhtml-postamble 'auto
  349. "Non-nil means insert a postamble in HTML export.
  350. When `t', insert a string as defined by the formatting string in
  351. `org-export-xhtml-postamble-format'. When set to a string, this
  352. string overrides `org-export-xhtml-postamble-format'. When set to
  353. 'auto, discard `org-export-xhtml-postamble-format' and honor
  354. `org-export-author/email/creator-info' variables. When set to a
  355. function, apply this function and insert the returned string.
  356. The function takes the property list of export options as its
  357. only argument.
  358. Setting :html-postamble in publishing projects will take
  359. precedence over this variable."
  360. :group 'org-export-xhtml
  361. :type '(choice (const :tag "No postamble" nil)
  362. (const :tag "Auto preamble" 'auto)
  363. (const :tag "Default formatting string" t)
  364. (string :tag "Custom formatting string")
  365. (function :tag "Function (must return a string)")))
  366. (defcustom org-export-xhtml-postamble-format
  367. '(("en" "<p class=\"author\">Author: %a (%e)</p>
  368. <p class=\"date\">Date: %d</p>
  369. <p class=\"creator\">Generated by %c</p>
  370. <p class=\"xhtml-validation\">%v</p>
  371. "))
  372. "The format for the HTML postamble.
  373. %a stands for the author.
  374. %e stands for the email(s).
  375. %d stands for the date.
  376. %c will be replaced by information about Org/Emacs.
  377. %v will be replaced by `org-export-xhtml-validation-link'.
  378. If you need to use a \"%\" character, you need to escape it
  379. like that: \"%%\"."
  380. :group 'org-export-xhtml
  381. :type 'string)
  382. (defcustom org-export-xhtml-home/up-format
  383. "<div id=\"org-div-home-and-up\" style=\"text-align:right;font-size:70%%;white-space:nowrap;\">
  384. <a accesskey=\"h\" href=\"%s\"> UP </a>
  385. |
  386. <a accesskey=\"H\" href=\"%s\"> HOME </a>
  387. </div>"
  388. "Snippet used to insert the HOME and UP links.
  389. This is a format string, the first %s will receive the UP link,
  390. the second the HOME link. If both `org-export-xhtml-link-up' and
  391. `org-export-xhtml-link-home' are empty, the entire snippet will be
  392. ignored."
  393. :group 'org-export-xhtml
  394. :type 'string)
  395. (defcustom org-export-xhtml-toplevel-hlevel 2
  396. "The <H> level for level 1 headings in HTML export.
  397. This is also important for the classes that will be wrapped around headlines
  398. and outline structure. If this variable is 1, the top-level headlines will
  399. be <h1>, and the corresponding classes will be outline-1, section-number-1,
  400. and outline-text-1. If this is 2, all of these will get a 2 instead.
  401. The default for this variable is 2, because we use <h1> for formatting the
  402. document title."
  403. :group 'org-export-xhtml
  404. :type 'string)
  405. (defcustom org-export-xhtml-link-org-files-as-html t
  406. "Non-nil means make file links to `file.org' point to `file.html'.
  407. When org-mode is exporting an org-mode file to HTML, links to
  408. non-html files are directly put into a href tag in HTML.
  409. However, links to other Org-mode files (recognized by the
  410. extension `.org.) should become links to the corresponding html
  411. file, assuming that the linked org-mode file will also be
  412. converted to HTML.
  413. When nil, the links still point to the plain `.org' file."
  414. :group 'org-export-xhtml
  415. :type 'boolean)
  416. (defcustom org-export-xhtml-inline-images 'maybe
  417. "Non-nil means inline images into exported HTML pages.
  418. This is done using an <img> tag. When nil, an anchor with href is used to
  419. link to the image. If this option is `maybe', then images in links with
  420. an empty description will be inlined, while images with a description will
  421. be linked only."
  422. :group 'org-export-xhtml
  423. :type '(choice (const :tag "Never" nil)
  424. (const :tag "Always" t)
  425. (const :tag "When there is no description" maybe)))
  426. (defcustom org-export-xhtml-inline-image-extensions
  427. '("png" "jpeg" "jpg" "gif" "svg")
  428. "Extensions of image files that can be inlined into HTML."
  429. :group 'org-export-xhtml
  430. :type '(repeat (string :tag "Extension")))
  431. (defcustom org-export-xhtml-table-tag
  432. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  433. "The HTML tag that is used to start a table.
  434. This must be a <table> tag, but you may change the options like
  435. borders and spacing."
  436. :group 'org-export-xhtml
  437. :type 'string)
  438. (defcustom org-export-table-header-tags '("<th scope=\"%s\"%s>" . "</th>")
  439. "The opening tag for table header fields.
  440. This is customizable so that alignment options can be specified.
  441. The first %s will be filled with the scope of the field, either row or col.
  442. The second %s will be replaced by a style entry to align the field.
  443. See also the variable `org-export-xhtml-table-use-header-tags-for-first-column'.
  444. See also the variable `org-export-xhtml-table-align-individual-fields'."
  445. :group 'org-export-tables
  446. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  447. (defcustom org-export-table-data-tags '("<td%s>" . "</td>")
  448. "The opening tag for table data fields.
  449. This is customizable so that alignment options can be specified.
  450. The first %s will be filled with the scope of the field, either row or col.
  451. The second %s will be replaced by a style entry to align the field.
  452. See also the variable `org-export-xhtml-table-align-individual-fields'."
  453. :group 'org-export-tables
  454. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  455. (defcustom org-export-table-row-tags '("<tr>" . "</tr>")
  456. "The opening tag for table data fields.
  457. This is customizable so that alignment options can be specified.
  458. Instead of strings, these can be Lisp forms that will be evaluated
  459. for each row in order to construct the table row tags. During evaluation,
  460. the variable `head' will be true when this is a header line, nil when this
  461. is a body line. And the variable `nline' will contain the line number,
  462. starting from 1 in the first header line. For example
  463. (setq org-export-table-row-tags
  464. (cons '(if head
  465. \"<tr>\"
  466. (if (= (mod nline 2) 1)
  467. \"<tr class=\\\"tr-odd\\\">\"
  468. \"<tr class=\\\"tr-even\\\">\"))
  469. \"</tr>\"))
  470. will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
  471. :group 'org-export-tables
  472. :type '(cons
  473. (choice :tag "Opening tag"
  474. (string :tag "Specify")
  475. (sexp))
  476. (choice :tag "Closing tag"
  477. (string :tag "Specify")
  478. (sexp))))
  479. (defcustom org-export-xhtml-table-align-individual-fields t
  480. "Non-nil means attach style attributes for alignment to each table field.
  481. When nil, alignment will only be specified in the column tags, but this
  482. is ignored by some browsers (like Firefox, Safari). Opera does it right
  483. though."
  484. :group 'org-export-tables
  485. :type 'boolean)
  486. (defcustom org-export-xhtml-table-use-header-tags-for-first-column nil
  487. "Non-nil means format column one in tables with header tags.
  488. When nil, also column one will use data tags."
  489. :group 'org-export-tables
  490. :type 'boolean)
  491. (defcustom org-export-xhtml-validation-link
  492. "<a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a>"
  493. "Link to HTML validation service."
  494. :group 'org-export-xhtml
  495. :type 'string)
  496. (defcustom org-export-xhtml-with-timestamp nil
  497. "If non-nil, write timestamp into the exported HTML text.
  498. If non-nil, write `org-export-xhtml-html-helper-timestamp' into the
  499. exported HTML text. Otherwise, the buffer will just be saved to
  500. a file."
  501. :group 'org-export-xhtml
  502. :type 'boolean)
  503. (defcustom org-export-xhtml-html-helper-timestamp
  504. "<br/><br/><hr/><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  505. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  506. :group 'org-export-xhtml
  507. :type 'string)
  508. (defcustom org-export-xhtml-protect-char-alist
  509. '(("&" . "&amp;")
  510. ("<" . "&lt;")
  511. (">" . "&gt;"))
  512. "Alist of characters to be converted by `org-html-protect'."
  513. :type '(repeat (cons (string :tag "Character")
  514. (string :tag "HTML equivalent"))))
  515. (defgroup org-export-xhtmlize nil
  516. "Options for processing examples with htmlize.el."
  517. :tag "Org Export Htmlize"
  518. :group 'org-export-xhtml)
  519. (defcustom org-export-xhtmlize-output-type 'inline-css
  520. "Output type to be used by htmlize when formatting code snippets.
  521. Choices are `css', to export the CSS selectors only, or `inline-css', to
  522. export the CSS attribute values inline in the HTML. We use as default
  523. `inline-css', in order to make the resulting HTML self-containing.
  524. However, this will fail when using Emacs in batch mode for export, because
  525. then no rich font definitions are in place. It will also not be good if
  526. people with different Emacs setup contribute HTML files to a website,
  527. because the fonts will represent the individual setups. In these cases,
  528. it is much better to let Org/Htmlize assign classes only, and to use
  529. a style file to define the look of these classes.
  530. To get a start for your css file, start Emacs session and make sure that
  531. all the faces you are interested in are defined, for example by loading files
  532. in all modes you want. Then, use the command
  533. \\[org-export-xhtmlize-generate-css] to extract class definitions."
  534. :group 'org-export-xhtmlize
  535. :type '(choice (const css) (const inline-css)))
  536. (defcustom org-export-xhtmlize-css-font-prefix "org-"
  537. "The prefix for CSS class names for htmlize font specifications."
  538. :group 'org-export-xhtmlize
  539. :type 'string)
  540. (defcustom org-export-xhtmlized-org-css-url nil
  541. "URL pointing to a CSS file defining text colors for htmlized Emacs buffers.
  542. Normally when creating an htmlized version of an Org buffer, htmlize will
  543. create CSS to define the font colors. However, this does not work when
  544. converting in batch mode, and it also can look bad if different people
  545. with different fontification setup work on the same website.
  546. When this variable is non-nil, creating an htmlized version of an Org buffer
  547. using `org-export-as-org' will remove the internal CSS section and replace it
  548. with a link to this URL."
  549. :group 'org-export-xhtmlize
  550. :type '(choice
  551. (const :tag "Keep internal css" nil)
  552. (string :tag "URL or local href")))
  553. ;;; Hooks
  554. (defvar org-export-xhtml-after-blockquotes-hook nil
  555. "Hook run during HTML export, after blockquote, verse, center are done.")
  556. (defvar org-export-xhtml-final-hook nil
  557. "Hook run at the end of HTML export, in the new buffer.")
  558. ;;; HTML export
  559. (defun org-export-xhtml-preprocess (parameters)
  560. "Convert LaTeX fragments to images."
  561. (when (and org-current-export-file
  562. (plist-get parameters :LaTeX-fragments))
  563. (org-format-latex
  564. (concat "ltxpng/" (file-name-sans-extension
  565. (file-name-nondirectory
  566. org-current-export-file)))
  567. org-current-export-dir nil "Creating LaTeX image %s"
  568. nil nil
  569. (cond
  570. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  571. ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
  572. ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax)
  573. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  574. (t nil))))
  575. (goto-char (point-min))
  576. (let (label l1)
  577. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  578. (org-if-unprotected-at (match-beginning 1)
  579. (setq label (match-string 1))
  580. (save-match-data
  581. (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
  582. (setq l1 (substring label (match-beginning 1)))
  583. (setq l1 label)))
  584. (replace-match (format "[[#%s][%s]]" label l1) t t)))))
  585. (defvar html-table-tag nil) ; dynamically scoped into this.
  586. ;; FIXME: it already exists in org-html.el
  587. (defconst org-html-cvt-link-fn
  588. nil
  589. "Function to convert link URLs to exportable URLs.
  590. Takes two arguments, TYPE and PATH.
  591. Returns exportable url as (TYPE PATH), or nil to signal that it
  592. didn't handle this case.
  593. Intended to be locally bound around a call to `org-export-as-html'." )
  594. ;; FIXME: it already exists in org-html.el
  595. (defun org-html-cvt-org-as-html (opt-plist type path)
  596. "Convert an org filename to an equivalent html filename.
  597. If TYPE is not file, just return `nil'.
  598. See variable `org-export-xhtml-link-org-files-as-html'"
  599. (save-match-data
  600. (and
  601. org-export-xhtml-link-org-files-as-html
  602. (string= type "file")
  603. (string-match "\\.org$" path)
  604. (progn
  605. (list
  606. "file"
  607. (concat
  608. (substring path 0 (match-beginning 0))
  609. "."
  610. (plist-get opt-plist :html-extension)))))))
  611. ;;; org-xhtml-format-org-link
  612. (defun org-xhtml-format-org-link (opt-plist type-1 path fragment desc attr
  613. descp)
  614. "Make an HTML link.
  615. OPT-PLIST is an options list.
  616. TYPE is the device-type of the link (THIS://foo.html)
  617. PATH is the path of the link (http://THIS#locationx)
  618. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  619. DESC is the link description, if any.
  620. ATTR is a string of other attributes of the a element.
  621. MAY-INLINE-P allows inlining it as an image."
  622. (declare (special org-lparse-par-open))
  623. (when (string= type-1 "coderef")
  624. (setq attr
  625. (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\""
  626. fragment fragment)))
  627. (save-match-data
  628. (let* ((may-inline-p
  629. (and (member type-1 '("http" "https" "file"))
  630. (org-lparse-should-inline-p path descp)
  631. (not fragment)))
  632. (type (if (equal type-1 "id") "file" type-1))
  633. (filename path)
  634. ;;First pass. Just sanity stuff.
  635. (components-1
  636. (cond
  637. ((string= type "file")
  638. (list
  639. type
  640. ;;Substitute just if original path was absolute.
  641. ;;(Otherwise path must remain relative)
  642. (if (file-name-absolute-p path)
  643. (concat "file://" (expand-file-name path))
  644. path)))
  645. ((string= type "")
  646. (list nil path))
  647. (t (list type path))))
  648. ;;Second pass. Components converted so they can refer
  649. ;;to a remote site.
  650. (components-2
  651. (or
  652. (and org-html-cvt-link-fn
  653. (apply org-html-cvt-link-fn
  654. opt-plist components-1))
  655. (apply #'org-html-cvt-org-as-html
  656. opt-plist components-1)
  657. components-1))
  658. (type (first components-2))
  659. (thefile (second components-2)))
  660. ;;Third pass. Build final link except for leading type
  661. ;;spec.
  662. (cond
  663. ((or
  664. (not type)
  665. (string= type "http")
  666. (string= type "https")
  667. (string= type "file")
  668. (string= type "coderef"))
  669. (if fragment
  670. (setq thefile (concat thefile "#" fragment))))
  671. (t))
  672. ;;Final URL-build, for all types.
  673. (setq thefile
  674. (let
  675. ((str (org-xml-format-href thefile)))
  676. (if (and type (not (or (string= "file" type)
  677. (string= "coderef" type))))
  678. (concat type ":" str)
  679. str)))
  680. (if may-inline-p
  681. (org-xhtml-format-image thefile)
  682. (org-lparse-format
  683. 'LINK (org-xml-format-desc desc) thefile attr)))))
  684. (defun org-xhtml-format-inline-image (desc)
  685. ;; FIXME: alt text missing here?
  686. (org-xhtml-format-tags "<img src=\"%s\" alt=\"\"/>" "" desc))
  687. ;; FIXME: the org-lparse defvar belongs to org-lparse.el
  688. (defvar org-lparse-link-description-is-image)
  689. (defun org-xhtml-format-image (src)
  690. "Create image tag with source and attributes."
  691. (save-match-data
  692. (if (string-match "^ltxpng/" src)
  693. (format "<img src=\"%s\" alt=\"%s\"/>"
  694. src (org-find-text-property-in-string 'org-latex-src src))
  695. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  696. (attr (org-find-text-property-in-string 'org-attributes src))
  697. (label (org-find-text-property-in-string 'org-label src))
  698. (caption (and caption (org-xml-encode-org-text caption)))
  699. (img (format "<img src=\"%s\"%s />"
  700. src
  701. (if (string-match "\\<alt=" (or attr ""))
  702. (concat " " attr )
  703. (concat " " attr " alt=\"" src "\""))))
  704. (extra (concat
  705. (and label
  706. (format "id=\"%s\" " (org-solidify-link-text label)))
  707. "class=\"figure\"")))
  708. (if caption
  709. (with-temp-buffer
  710. (with-org-lparse-preserve-paragraph-state
  711. (insert
  712. (org-lparse-format
  713. '("<div %s>" . "\n</div>")
  714. (concat
  715. (org-lparse-format '("\n<p>" . "</p>") img)
  716. (org-lparse-format '("\n<p>" . "</p>") caption))
  717. extra)))
  718. (buffer-string))
  719. img)))))
  720. (defun org-export-xhtml-get-bibliography ()
  721. "Find bibliography, cut it out and return it."
  722. (catch 'exit
  723. (let (beg end (cnt 1) bib)
  724. (save-excursion
  725. (goto-char (point-min))
  726. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  727. (setq beg (match-beginning 0))
  728. (while (re-search-forward "</?div\\>" nil t)
  729. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  730. (when (= cnt 0)
  731. (and (looking-at ">") (forward-char 1))
  732. (setq bib (buffer-substring beg (point)))
  733. (delete-region beg (point))
  734. (throw 'exit bib))))
  735. nil))))
  736. (defun org-xhtml-format-table (lines olines)
  737. (let ((org-xhtml-format-table-no-css nil))
  738. (org-lparse-format-table lines olines)))
  739. ;; Following variable is defined for native tables i.e., when
  740. ;; `org-lparse-table-is-styled' evals to t.
  741. (defvar org-xhtml-format-table-no-css)
  742. (defvar org-table-number-regexp) ; defined in org-table.el
  743. ;; FIXME: This function is called from other modules. Use xhtml suffix
  744. ;; to avoid conflict
  745. (defun org-format-table-xhtml (lines olines &optional no-css)
  746. "Find out which HTML converter to use and return the HTML code.
  747. NO-CSS is passed to the exporter."
  748. (let* ((org-lparse-backend 'xhtml)
  749. (org-lparse-entity-control-callbacks-alist
  750. (org-lparse-get 'ENTITY-CONTROL))
  751. (org-lparse-entity-format-callbacks-alist
  752. (org-lparse-get 'ENTITY-FORMAT))
  753. (org-xhtml-format-table-no-css no-css))
  754. (org-lparse-format-table lines olines)))
  755. ;; FIXME: This function is called from other modules. Use xhtml suffix
  756. ;; to avoid conflict
  757. (defun org-format-org-table-xhtml (lines &optional splice no-css)
  758. ;; This routine might get called outside of org-export-as-html. For
  759. ;; example, this could happen as part of org-table-export or as part
  760. ;; of org-export-as-docbook. Explicitly bind the parser callback to
  761. ;; the html ones for the duration of the call.
  762. (let* ((org-lparse-backend 'xhtml)
  763. (org-lparse-entity-control-callbacks-alist
  764. (org-lparse-get 'ENTITY-CONTROL))
  765. (org-lparse-entity-format-callbacks-alist
  766. (org-lparse-get 'ENTITY-FORMAT))
  767. (org-xhtml-format-table-no-css no-css))
  768. (org-lparse-format-org-table lines splice)))
  769. ;; FIXME: it already exists in org-html.el
  770. (defun org-export-splice-attributes (tag attributes)
  771. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  772. (if (not attributes)
  773. tag
  774. (let (oldatt newatt)
  775. (setq oldatt (org-extract-attributes-from-string tag)
  776. tag (pop oldatt)
  777. newatt (cdr (org-extract-attributes-from-string attributes)))
  778. (while newatt
  779. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  780. (if (string-match ">" tag)
  781. (setq tag
  782. (replace-match (concat (org-attributes-to-string oldatt) ">")
  783. t t tag)))
  784. tag)))
  785. ;; FIXME: This function is called from other modules. Use xhtml suffix
  786. ;; to avoid conflict
  787. (defun org-format-table-table-xhtml (lines)
  788. (let* ((org-lparse-get 'html)
  789. (org-lparse-entity-control-callbacks-alist
  790. (org-lparse-get 'ENTITY-CONTROL))
  791. (org-lparse-entity-format-callbacks-alist
  792. (org-lparse-get 'ENTITY-FORMAT)))
  793. (org-lparse-format-table-table lines)))
  794. ;; FIXME: it already exists in org-html.el
  795. (defun org-export-splice-style (style extra)
  796. "Splice EXTRA into STYLE, just before \"</style>\"."
  797. (if (and (stringp extra)
  798. (string-match "\\S-" extra)
  799. (string-match "</style>" style))
  800. (concat (substring style 0 (match-beginning 0))
  801. "\n" extra "\n"
  802. (substring style (match-beginning 0)))
  803. style))
  804. (defvar htmlize-buffer-places) ; from htmlize.el
  805. (defun org-export-xhtmlize-region-for-paste (beg end)
  806. "Convert the region to HTML, using htmlize.el.
  807. This is much like `htmlize-region-for-paste', only that it uses
  808. the settings define in the org-... variables."
  809. (let* ((htmlize-output-type org-export-xhtmlize-output-type)
  810. (htmlize-css-name-prefix org-export-xhtmlize-css-font-prefix)
  811. (htmlbuf (htmlize-region beg end)))
  812. (unwind-protect
  813. (with-current-buffer htmlbuf
  814. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  815. (plist-get htmlize-buffer-places 'content-end)))
  816. (kill-buffer htmlbuf))))
  817. ;;;###autoload
  818. (defun org-export-xhtmlize-generate-css ()
  819. "Create the CSS for all font definitions in the current Emacs session.
  820. Use this to create face definitions in your CSS style file that can then
  821. be used by code snippets transformed by htmlize.
  822. This command just produces a buffer that contains class definitions for all
  823. faces used in the current Emacs session. You can copy and paste the ones you
  824. need into your CSS file.
  825. If you then set `org-export-xhtmlize-output-type' to `css', calls to
  826. the function `org-export-xhtmlize-region-for-paste' will produce code
  827. that uses these same face definitions."
  828. (interactive)
  829. (require 'htmlize)
  830. (and (get-buffer "*html*") (kill-buffer "*html*"))
  831. (with-temp-buffer
  832. (let ((fl (face-list))
  833. (htmlize-css-name-prefix "org-")
  834. (htmlize-output-type 'css)
  835. f i)
  836. (while (setq f (pop fl)
  837. i (and f (face-attribute f :inherit)))
  838. (when (and (symbolp f) (or (not i) (not (listp i))))
  839. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  840. (htmlize-region (point-min) (point-max))))
  841. (org-pop-to-buffer-same-window "*html*")
  842. (goto-char (point-min))
  843. (if (re-search-forward "<style" nil t)
  844. (delete-region (point-min) (match-beginning 0)))
  845. (if (re-search-forward "</style>" nil t)
  846. (delete-region (1+ (match-end 0)) (point-max)))
  847. (beginning-of-line 1)
  848. (if (looking-at " +") (replace-match ""))
  849. (goto-char (point-min)))
  850. (defvar body-only) ; dynamically scoped into this.
  851. ;; Following variable is let bound when `org-do-lparse' is in
  852. ;; progress. See org-lparse.el.
  853. ;; FIXME: the org-lparse defvar belongs to org-lparse.el
  854. (defvar org-lparse-toc)
  855. (defvar org-lparse-footnote-buffer)
  856. (defvar org-lparse-footnote-definitions)
  857. (defvar org-lparse-dyn-first-heading-pos)
  858. (defun org-xhtml-end-export ()
  859. ;; insert the table of contents
  860. (when (and org-export-with-toc (not body-only) org-lparse-toc)
  861. (org-xhtml-insert-toc org-lparse-toc))
  862. ;; remove empty paragraphs
  863. (goto-char (point-min))
  864. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  865. (replace-match ""))
  866. ;; Convert whitespace place holders
  867. (goto-char (point-min))
  868. (let (beg end n)
  869. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  870. (setq n (get-text-property beg 'org-whitespace)
  871. end (next-single-property-change beg 'org-whitespace))
  872. (goto-char beg)
  873. (delete-region beg end)
  874. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  875. (make-string n ?x)))))
  876. ;; Remove empty lines at the beginning of the file.
  877. (goto-char (point-min))
  878. (when (looking-at "\\s-+\n") (replace-match ""))
  879. ;; Remove display properties
  880. (remove-text-properties (point-min) (point-max) '(display t))
  881. ;; kill temporary buffers
  882. (when org-lparse-footnote-buffer
  883. (kill-buffer org-lparse-footnote-buffer))
  884. ;; Run the hook
  885. (run-hooks 'org-export-xhtml-final-hook))
  886. (defun org-xhtml-format-toc-entry (snumber todo headline tags href)
  887. (setq headline (concat
  888. (and org-export-with-section-numbers
  889. (concat snumber " "))
  890. headline
  891. (and tags
  892. (concat
  893. (org-lparse-format 'SPACES 3)
  894. (org-lparse-format 'FONTIFY tags "tag")))))
  895. (when todo
  896. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  897. (org-lparse-format 'LINK headline (concat "#" href)))
  898. (defun org-xhtml-format-toc-item (toc-entry level org-last-level)
  899. (when (> level org-last-level)
  900. (let ((cnt (- level org-last-level)))
  901. (while (>= (setq cnt (1- cnt)) 0)
  902. (org-lparse-begin-list 'unordered)
  903. (org-lparse-begin 'LIST-ITEM 'unordered))))
  904. (when (< level org-last-level)
  905. (let ((cnt (- org-last-level level)))
  906. (while (>= (setq cnt (1- cnt)) 0)
  907. (org-lparse-end-list-item)
  908. (org-lparse-end-list 'unordered))))
  909. (org-lparse-end-list-item)
  910. (org-lparse-begin 'LIST-ITEM 'unordered)
  911. (insert toc-entry))
  912. (defun org-xhtml-begin-toc (lang-specific-heading)
  913. (org-lparse-insert-tag "<div id=\"table-of-contents\">")
  914. (insert
  915. (org-lparse-format 'HEADING lang-specific-heading
  916. (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1)))
  917. (org-lparse-insert-tag "<div id=\"text-table-of-contents\">")
  918. (org-lparse-begin-list 'unordered)
  919. (org-lparse-begin 'LIST-ITEM 'unordered))
  920. (defun org-xhtml-end-toc ()
  921. (while (> org-last-level (1- org-min-level))
  922. (setq org-last-level (1- org-last-level))
  923. (org-lparse-end-list-item)
  924. (org-lparse-end-list 'unordered))
  925. (org-lparse-insert-tag "</div>")
  926. (org-lparse-insert-tag "</div>")
  927. ;; cleanup empty list items in toc
  928. (while (re-search-backward "<li>[ \r\n\t]*</li>\n?" (point-min) t)
  929. (replace-match "")))
  930. ;;;###autoload
  931. (defun org-export-as-xhtml-and-open (arg)
  932. "Export the outline as HTML and immediately open it with a browser.
  933. If there is an active region, export only the region.
  934. The prefix ARG specifies how many levels of the outline should become
  935. headlines. The default is 3. Lower levels will become bulleted lists."
  936. (interactive "P")
  937. (org-lparse-and-open "xhtml" "xhtml" arg))
  938. ;;;###autoload
  939. (defun org-export-as-xhtml-batch ()
  940. "Call the function `org-lparse-batch'.
  941. This function can be used in batch processing as:
  942. emacs --batch
  943. --load=$HOME/lib/emacs/org.el
  944. --eval \"(setq org-export-headline-levels 2)\"
  945. --visit=MyFile --funcall org-export-as-html-batch"
  946. (org-lparse-batch "xhtml"))
  947. ;;;###autoload
  948. (defun org-export-as-xhtml-to-buffer (arg)
  949. "Call `org-lparse-to-buffer` with output to a temporary buffer.
  950. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  951. (interactive "P")
  952. (org-lparse-to-buffer "xhtml" arg))
  953. ;;;###autoload
  954. (defun org-replace-region-by-xhtml (beg end)
  955. "Assume the current region has org-mode syntax, and convert it to HTML.
  956. This can be used in any buffer. For example, you could write an
  957. itemized list in org-mode syntax in an HTML buffer and then use this
  958. command to convert it."
  959. (interactive "r")
  960. (org-replace-region-by "xhtml" beg end))
  961. ;;;###autoload
  962. (defun org-export-region-as-xhtml (beg end &optional body-only buffer)
  963. "Convert region from BEG to END in `org-mode' buffer to HTML.
  964. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  965. contents, and only produce the region of converted text, useful for
  966. cut-and-paste operations.
  967. If BUFFER is a buffer or a string, use/create that buffer as a target
  968. of the converted HTML. If BUFFER is the symbol `string', return the
  969. produced HTML as a string and leave not buffer behind. For example,
  970. a Lisp program could call this function in the following way:
  971. (setq html (org-export-region-as-html beg end t 'string))
  972. When called interactively, the output buffer is selected, and shown
  973. in a window. A non-interactive call will only return the buffer."
  974. (interactive "r\nP")
  975. (org-lparse-region "xhtml" beg end body-only buffer))
  976. ;;; org-export-as-xhtml
  977. ;;;###autoload
  978. (defun org-export-as-xhtml (arg &optional hidden ext-plist
  979. to-buffer body-only pub-dir)
  980. "Export the outline as a pretty HTML file.
  981. Use `org-lparse' internally to perform the actual export. This
  982. routine merely binds the TARGET-BACKEND and NATIVE-BACKEND args
  983. of `org-lparse' to \"html\"."
  984. (interactive "P")
  985. (org-lparse "xhtml" "xhtml" arg hidden ext-plist to-buffer body-only pub-dir))
  986. (defvar org-xhtml-entity-control-callbacks-alist
  987. `((EXPORT
  988. . (org-xhtml-begin-export org-xhtml-end-export))
  989. (DOCUMENT-CONTENT
  990. . (org-xhtml-begin-document-content org-xhtml-end-document-content))
  991. (DOCUMENT-BODY
  992. . (org-xhtml-begin-document-body org-xhtml-end-document-body))
  993. (TOC
  994. . (org-xhtml-begin-toc org-xhtml-end-toc))
  995. (ENVIRONMENT
  996. . (org-xhtml-begin-environment org-xhtml-end-environment))
  997. (FOOTNOTE-DEFINITION
  998. . (org-xhtml-begin-footnote-definition org-xhtml-end-footnote-definition))
  999. (TABLE
  1000. . (org-xhtml-begin-table org-xhtml-end-table))
  1001. (TABLE-ROWGROUP
  1002. . (org-xhtml-begin-table-rowgroup org-xhtml-end-table-rowgroup))
  1003. (LIST
  1004. . (org-xhtml-begin-list org-xhtml-end-list))
  1005. (LIST-ITEM
  1006. . (org-xhtml-begin-list-item org-xhtml-end-list-item))
  1007. (OUTLINE
  1008. . (org-xhtml-begin-outline org-xhtml-end-outline))
  1009. (OUTLINE-TEXT
  1010. . (org-xhtml-begin-outline-text org-xhtml-end-outline-text))
  1011. (PARAGRAPH
  1012. . (org-xhtml-begin-paragraph org-xhtml-end-paragraph)))
  1013. "Alist of control callbacks registered with the exporter.
  1014. Each element is of the form (ENTITY . (BEGIN-ENTITY-FUNCTION
  1015. END-ENTITY-FUNCTION)). ENTITY is one of PARAGRAPH, LIST etc as
  1016. seen above. BEGIN-ENTITY-FUNCTION and END-ENTITY-FUNCTION are
  1017. functions that get called when the exporter needs to begin or end
  1018. an entity in the currently exported file. The signatures of
  1019. these callbacks are specific to the ENTITY being emitted. These
  1020. callbacks always get called with exported file as the current
  1021. buffer and need to insert the appropriate tags into the current
  1022. buffer. For example, `org-xhtml-begin-paragraph' inserts <p> and
  1023. `org-xhtml-end-paragraph' inserts </p> in to the current buffer.
  1024. These callbacks are invoked via `org-lparse-begin' and
  1025. `org-lparse-end'.")
  1026. (defvar org-xhtml-entity-format-callbacks-alist
  1027. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  1028. (ORG-TAGS . org-lparse-format-org-tags)
  1029. (SECTION-NUMBER . org-lparse-format-section-number)
  1030. (HEADLINE . org-xhtml-format-headline)
  1031. (TOC-ENTRY . org-xhtml-format-toc-entry)
  1032. (TOC-ITEM . org-xhtml-format-toc-item)
  1033. (TAGS . org-xhtml-format-tags)
  1034. (SPACES . org-xhtml-format-spaces)
  1035. (TABS . org-xhtml-format-tabs)
  1036. (LINE-BREAK . org-xhtml-format-line-break)
  1037. (FONTIFY . org-xhtml-format-fontify)
  1038. (TODO . org-lparse-format-todo)
  1039. (ORG-LINK . org-xhtml-format-org-link)
  1040. (LINK . org-xhtml-format-link)
  1041. (INLINE-IMAGE . org-xhtml-format-inline-image)
  1042. (HEADING . org-xhtml-format-heading)
  1043. (ANCHOR . org-xhtml-format-anchor)
  1044. (TABLE . org-xhtml-format-table)
  1045. (TABLE-ROW . org-xhtml-format-table-row)
  1046. (TABLE-CELL . org-xhtml-format-table-cell)
  1047. (FOOTNOTES-SECTION . org-xhtml-format-footnotes-section)
  1048. (FOOTNOTE-REFERENCE . org-xhtml-format-footnote-reference)
  1049. (HORIZONTAL-LINE . org-xhtml-format-horizontal-line)
  1050. (LINE . org-xhtml-format-line)
  1051. (COMMENT . org-xhtml-format-comment)
  1052. (ORG-ENTITY . org-xhtml-format-org-entity))
  1053. "Alist of format callbacks registered with the exporter.
  1054. Each element is of the form (ENTITY . ENTITY-FORMAT-FUNCTION).
  1055. ENTITY is one of LINE, HEADING, COMMENT, LINK, TABLE-ROW etc as
  1056. seen above. ENTITY-FORMAT-FUNCTION is a functions that gets
  1057. called when the exporter needs to format a string in `org-mode'
  1058. buffer in a backend specific way. The signatures of the
  1059. formatting callback is specific to the ENTITY being passed in.
  1060. These callbacks always need to encode the incoming entity in
  1061. backend specific way and return the same. These callbacks do not
  1062. make any modifications to the exporter file. For example,
  1063. `org-xhtml-format-table-row' encloses incoming entity in <tr>
  1064. </tr> tags and returns it. See also `org-lparse-format'.")
  1065. ;; register the xhtml exporter with org-lparse library
  1066. (org-lparse-register-backend 'xhtml)
  1067. (defun org-xhtml-unload-function ()
  1068. ;; notify org-lparse library on unload
  1069. (org-lparse-unregister-backend 'xhtml)
  1070. nil)
  1071. (defun org-xhtml-begin-document-body (opt-plist)
  1072. (let ((link-up (and (plist-get opt-plist :link-up)
  1073. (string-match "\\S-" (plist-get opt-plist :link-up))
  1074. (plist-get opt-plist :link-up)))
  1075. (link-home (and (plist-get opt-plist :link-home)
  1076. (string-match "\\S-" (plist-get opt-plist :link-home))
  1077. (plist-get opt-plist :link-home))))
  1078. (insert "\n<body>")
  1079. (org-lparse-insert-tag "<div id=\"content\">")
  1080. (insert "\n"
  1081. (or (and (or link-up link-home)
  1082. (format org-export-xhtml-home/up-format
  1083. (or link-up link-home)
  1084. (or link-home link-up))) "")
  1085. "\n"))
  1086. (org-xhtml-insert-preamble opt-plist))
  1087. (defun org-xhtml-end-document-body (opt-plist)
  1088. (org-xhtml-insert-postamble opt-plist)
  1089. (unless body-only
  1090. (org-lparse-insert-tag "</div>")
  1091. (insert "\n</body>")))
  1092. (defun org-xhtml-begin-document-content (opt-plist)
  1093. (let* ((language (plist-get opt-plist :language))
  1094. (charset (or (and coding-system-for-write
  1095. (fboundp 'coding-system-get)
  1096. (coding-system-get coding-system-for-write
  1097. 'mime-charset))
  1098. "iso-8859-1"))
  1099. (style (concat (if (plist-get opt-plist :style-include-default)
  1100. org-export-xhtml-style-default)
  1101. (plist-get opt-plist :style)
  1102. (plist-get opt-plist :style-extra)
  1103. "\n"
  1104. (if (plist-get opt-plist :style-include-scripts)
  1105. org-export-xhtml-scripts)))
  1106. (mathjax
  1107. (if (or (eq (plist-get opt-plist :LaTeX-fragments) 'mathjax)
  1108. (and org-export-have-math
  1109. (eq (plist-get opt-plist :LaTeX-fragments) t)))
  1110. (org-export-xhtml-mathjax-config
  1111. org-export-xhtml-mathjax-template
  1112. org-export-xhtml-mathjax-options
  1113. (or (plist-get opt-plist :mathjax) "")) "")))
  1114. (insert (format
  1115. "%s
  1116. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  1117. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  1118. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  1119. lang=\"%s\" xml:lang=\"%s\">
  1120. <head>
  1121. <title>%s</title>
  1122. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  1123. <meta name=\"generator\" content=\"Org-mode\"/>
  1124. <meta name=\"generated\" content=\"%s\"/>
  1125. <meta name=\"author\" content=\"%s\"/>
  1126. <meta name=\"description\" content=\"%s\"/>
  1127. <meta name=\"keywords\" content=\"%s\"/>
  1128. %s
  1129. %s
  1130. </head>
  1131. "
  1132. (format
  1133. (or (and (stringp org-export-xhtml-xml-declaration)
  1134. org-export-xhtml-xml-declaration)
  1135. (cdr (assoc (plist-get opt-plist :html-extension)
  1136. org-export-xhtml-xml-declaration))
  1137. (cdr (assoc "html" org-export-xhtml-xml-declaration))
  1138. "")
  1139. charset)
  1140. language language
  1141. (plist-get opt-plist :title)
  1142. charset
  1143. (plist-get opt-plist :effective-date)
  1144. (plist-get opt-plist :author)
  1145. (plist-get opt-plist :description)
  1146. (plist-get opt-plist :keywords)
  1147. style
  1148. mathjax))))
  1149. (defun org-xhtml-end-document-content ()
  1150. (insert "\n</html>\n"))
  1151. (defun org-xhtml-begin-outline (level1 snumber title tags
  1152. target extra-targets extra-class)
  1153. (let* ((class (format "outline-%d" level1))
  1154. (class (if extra-class (concat class " " extra-class) class))
  1155. (id (format "outline-container-%s"
  1156. (org-lparse-suffix-from-snumber snumber)))
  1157. (extra (concat (when id (format " id=\"%s\"" id))
  1158. (when class (format " class=\"%s\"" class)))))
  1159. (org-lparse-insert-tag "<div%s>" extra)
  1160. (insert
  1161. (org-lparse-format 'HEADING
  1162. (org-lparse-format
  1163. 'HEADLINE title extra-targets tags snumber level1)
  1164. level1 target))))
  1165. (defun org-xhtml-end-outline ()
  1166. (org-lparse-insert-tag "</div>"))
  1167. (defun org-xhtml-begin-outline-text (level1 snumber extra-class)
  1168. (let* ((class (format "outline-text-%d" level1))
  1169. (class (if extra-class (concat class " " extra-class) class))
  1170. (id (format "text-%s" (org-lparse-suffix-from-snumber snumber)))
  1171. (extra (concat (when id (format " id=\"%s\"" id))
  1172. (when class (format " class=\"%s\"" class)))))
  1173. (org-lparse-insert-tag "<div%s>" extra)))
  1174. (defun org-xhtml-end-outline-text ()
  1175. (org-lparse-insert-tag "</div>"))
  1176. (defun org-xhtml-begin-paragraph (&optional style)
  1177. (let* ((class (cdr (assoc style '((footnote . "footnote")
  1178. (verse . nil)))))
  1179. (extra (if class (format " class=\"%s\"" class) "")))
  1180. (org-lparse-insert-tag "<p%s>" extra)))
  1181. (defun org-xhtml-end-paragraph ()
  1182. (insert "</p>"))
  1183. (defun org-xhtml-format-environment (style beg-end)
  1184. (assert (memq style '(blockquote center verse fixedwidth quote native)) t)
  1185. (case style
  1186. (blockquote
  1187. (case beg-end
  1188. (BEGIN
  1189. (org-lparse-end-paragraph)
  1190. (insert "<blockquote>\n")
  1191. (org-lparse-begin-paragraph))
  1192. (END
  1193. (org-lparse-end-paragraph)
  1194. (insert "\n</blockquote>\n")
  1195. (org-lparse-begin-paragraph))))
  1196. (verse
  1197. (case beg-end
  1198. (BEGIN
  1199. (org-lparse-end-paragraph)
  1200. (insert "\n<p class=\"verse\">\n")
  1201. (setq org-lparse-par-open t))
  1202. (END
  1203. (insert "</p>\n")
  1204. (setq org-lparse-par-open nil)
  1205. (org-lparse-begin-paragraph))))
  1206. (center
  1207. (case beg-end
  1208. (BEGIN
  1209. (org-lparse-end-paragraph)
  1210. (insert "\n<div style=\"text-align: center\">")
  1211. (org-lparse-begin-paragraph))
  1212. (END
  1213. (org-lparse-end-paragraph)
  1214. (insert "\n</div>")
  1215. (org-lparse-begin-paragraph))))
  1216. (fixedwidth
  1217. (case beg-end
  1218. (BEGIN
  1219. (org-lparse-end-paragraph)
  1220. (insert "<pre class=\"example\">\n"))
  1221. (END
  1222. (insert "</pre>\n")
  1223. (org-lparse-begin-paragraph))))
  1224. (quote
  1225. (case beg-end
  1226. (BEGIN
  1227. (org-lparse-end-paragraph)
  1228. (insert "<pre>"))
  1229. (END
  1230. (insert "</pre>\n")
  1231. (org-lparse-begin-paragraph))))
  1232. (native
  1233. (case beg-end
  1234. (BEGIN (org-lparse-end-paragraph))
  1235. (END (org-lparse-begin-paragraph))))
  1236. (t (error "Unknown environment %s" style))))
  1237. (defun org-xhtml-begin-environment (style)
  1238. (org-xhtml-format-environment style 'BEGIN))
  1239. (defun org-xhtml-end-environment (style)
  1240. (org-xhtml-format-environment style 'END))
  1241. (defun org-xhtml-begin-list (ltype)
  1242. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  1243. ltype))
  1244. (case ltype
  1245. (ordered (let* ((arg1 nil)
  1246. (extra (if arg1 (format " start=\"%d\"" arg1) "")))
  1247. (org-lparse-insert-tag "<ol%s>" extra)))
  1248. (unordered (org-lparse-insert-tag "<ul>"))
  1249. (description (org-lparse-insert-tag "<dl>"))
  1250. (t (error "Unknown list type: %s" ltype))))
  1251. (defun org-xhtml-end-list (ltype)
  1252. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  1253. ltype))
  1254. (org-lparse-insert-tag
  1255. (case ltype
  1256. (ordered "</ol>")
  1257. (unordered "</ul>")
  1258. (description "</dl>")
  1259. (t (error "Unknown list type: %s" ltype)))))
  1260. (defun org-xhtml-begin-list-item (ltype &optional arg headline)
  1261. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  1262. ltype))
  1263. (case ltype
  1264. (ordered
  1265. (assert (not headline) t)
  1266. (let* ((counter arg)
  1267. (extra (if counter (format " value=\"%s\"" counter) "")))
  1268. (org-lparse-insert-tag "<li%s>" extra)))
  1269. (unordered
  1270. (let* ((id arg)
  1271. (extra (if id (format " id=\"%s\"" id) "")))
  1272. (org-lparse-insert-tag "<li%s>" extra)
  1273. (when headline
  1274. (insert headline (org-lparse-format 'LINE-BREAK) "\n"))))
  1275. (description
  1276. (assert (not headline) t)
  1277. (let* ((desc-tag (or arg "(no term)")))
  1278. (insert
  1279. (org-xhtml-format-tags '("<dt>" . "</dt>") desc-tag))
  1280. (org-lparse-insert-tag "<dd>")))
  1281. (t (error "Unknown list type"))))
  1282. (defun org-xhtml-end-list-item (ltype)
  1283. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  1284. ltype))
  1285. (case ltype
  1286. (ordered (org-lparse-insert-tag "</li>"))
  1287. (unordered (org-lparse-insert-tag "</li>"))
  1288. (description (org-lparse-insert-tag "</dd>"))
  1289. (t (error "Unknown list type"))))
  1290. ;; Following variables are let bound when table emission is in
  1291. ;; progress. See org-lparse.el.
  1292. ;; FIXME: the org-lparse defvar belongs to org-lparse.el
  1293. (defvar org-lparse-table-begin-marker)
  1294. (defvar org-lparse-table-ncols)
  1295. (defvar org-lparse-table-rowgrp-open)
  1296. (defvar org-lparse-table-rownum)
  1297. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  1298. (defvar org-lparse-table-is-styled)
  1299. (defvar org-lparse-table-rowgrp-info)
  1300. (defvar org-lparse-table-colalign-vector)
  1301. (defvar org-lparse-table-num-numeric-items-per-column)
  1302. (defun org-xhtml-begin-table-rowgroup (&optional is-header-row)
  1303. (when org-lparse-table-rowgrp-open
  1304. (org-lparse-end 'TABLE-ROWGROUP))
  1305. (org-lparse-insert-tag (if is-header-row "<thead>" "<tbody>"))
  1306. (setq org-lparse-table-rowgrp-open t)
  1307. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  1308. (defun org-xhtml-end-table-rowgroup ()
  1309. (when org-lparse-table-rowgrp-open
  1310. (setq org-lparse-table-rowgrp-open nil)
  1311. (org-lparse-insert-tag
  1312. (if org-lparse-table-cur-rowgrp-is-hdr "</thead>" "</tbody>"))))
  1313. (defun org-xhtml-begin-table (caption label attributes)
  1314. (let ((html-table-tag
  1315. (org-export-splice-attributes html-table-tag attributes)))
  1316. (when label
  1317. (setq html-table-tag
  1318. (org-export-splice-attributes
  1319. html-table-tag
  1320. (format "id=\"%s\"" (org-solidify-link-text label)))))
  1321. (org-lparse-insert-tag html-table-tag))
  1322. ;; Since the output of HTML table formatter can also be used in
  1323. ;; DocBook document, we want to always include the caption to make
  1324. ;; DocBook XML file valid.
  1325. (insert (format "<caption>%s</caption>" (or caption "")) "\n"))
  1326. (defun org-xhtml-end-table ()
  1327. (when org-lparse-table-is-styled
  1328. (goto-char org-lparse-table-begin-marker)
  1329. (setq org-lparse-table-begin-marker nil)
  1330. (let ((c -1) gr colgropen)
  1331. (insert
  1332. (mapconcat
  1333. (lambda (x)
  1334. (incf c)
  1335. (setq gr (pop org-table-colgroup-info))
  1336. (concat
  1337. (if (memq gr '(:start :startend))
  1338. (prog1
  1339. (if colgropen
  1340. "</colgroup>\n<colgroup>"
  1341. "<colgroup>")
  1342. (setq colgropen t))
  1343. "")
  1344. (let* ((align (aref org-lparse-table-colalign-vector c))
  1345. (alignspec (if org-xhtml-format-table-no-css
  1346. " align=\"%s\"" " class=\"%s\""))
  1347. (extra (format alignspec align)))
  1348. (format "<col%s />" extra))
  1349. (if (memq gr '(:end :startend))
  1350. (progn (setq colgropen nil) "</colgroup>")
  1351. "")))
  1352. org-lparse-table-num-numeric-items-per-column ""))
  1353. (if colgropen (insert "</colgroup>")))
  1354. ;; fill style attributes for table cells
  1355. (while (re-search-forward "@@class\\([0-9]+\\)@@" nil t)
  1356. (let ((c (string-to-number (match-string 1))))
  1357. (replace-match
  1358. (if org-export-xhtml-table-align-individual-fields
  1359. (format (if org-xhtml-format-table-no-css " align=\"%s\""
  1360. " class=\"%s\"")
  1361. (or (aref org-lparse-table-colalign-vector c) "left")) "")
  1362. t t)))
  1363. (goto-char (point-max)))
  1364. (org-lparse-insert-tag "</table>\n"))
  1365. (defun org-xhtml-format-table-row (row)
  1366. (org-xhtml-format-tags
  1367. (cons (eval (car org-export-table-row-tags))
  1368. (eval (cdr org-export-table-row-tags))) row))
  1369. (defun org-xhtml-format-table-cell (text r c)
  1370. (let ((cell-style-cookie (or (and org-lparse-table-is-styled
  1371. (format "@@class%03d@@" c)) "")))
  1372. (cond
  1373. (org-lparse-table-cur-rowgrp-is-hdr
  1374. (org-xhtml-format-tags
  1375. org-export-table-header-tags text "col" cell-style-cookie))
  1376. ((and (= c 0) org-export-xhtml-table-use-header-tags-for-first-column)
  1377. (org-xhtml-format-tags
  1378. org-export-table-header-tags text "row" cell-style-cookie))
  1379. (t
  1380. (org-xhtml-format-tags
  1381. org-export-table-data-tags text cell-style-cookie)))))
  1382. (defun org-xhtml-begin-footnote-definition (n)
  1383. (org-lparse-begin-paragraph 'footnote)
  1384. (insert
  1385. (format
  1386. (format org-export-xhtml-footnote-format
  1387. "<a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a>")
  1388. n n n)))
  1389. (defun org-xhtml-end-footnote-definition (n)
  1390. (org-lparse-end-paragraph))
  1391. (defun org-xhtml-format-spaces (n)
  1392. (let ((space (or (and org-lparse-encode-pending "\\nbsp") "&nbsp;")) out)
  1393. (while (> n 0)
  1394. (setq out (concat out space))
  1395. (setq n (1- n))) out))
  1396. (defun org-xhtml-format-tabs (&optional n)
  1397. (ignore))
  1398. (defun org-xhtml-format-line-break ()
  1399. (org-xhtml-format-tags "<br/>" ""))
  1400. (defun org-xhtml-format-horizontal-line ()
  1401. (concat "\n" "<hr/>" "\n"))
  1402. (defun org-xhtml-format-line (line)
  1403. (case org-lparse-dyn-current-environment
  1404. ((quote fixedwidth) (concat (org-xml-encode-plain-text line) "\n"))
  1405. (t (concat line "\n"))))
  1406. (defun org-xhtml-format-comment (fmt &rest args)
  1407. (let ((comment (apply 'format fmt args)))
  1408. (format "\n<!-- %s -->\n" comment)))
  1409. (defun org-xhtml-format-fontify (text style &optional id)
  1410. (let (class extra how)
  1411. (cond
  1412. ((eq style 'underline)
  1413. (setq extra " style=\"text-decoration:underline;\"" ))
  1414. ((setq how (cdr (assoc style
  1415. '((bold . ("<b>" . "</b>"))
  1416. (emphasis . ("<i>" . "</i>"))
  1417. (code . ("<code>" . "</code>"))
  1418. (verbatim . ("<code>" . "</code>"))
  1419. (strike . ("<del>" . "</del>"))
  1420. (subscript . ("<sub>" . "</sub>"))
  1421. (superscript . ("<sup>" . "</sup>")))))))
  1422. ((listp style)
  1423. (setq class (mapconcat 'identity style " ")))
  1424. ((stringp style)
  1425. (setq class style))
  1426. (t (error "Unknown style %S" style)))
  1427. (setq extra (concat (when class (format " class=\"%s\"" class))
  1428. (when id (format " id=\"%s\"" id))
  1429. extra))
  1430. (org-xhtml-format-tags
  1431. (or how '("<span%s>" . "</span>")) text extra)))
  1432. (defun org-xhtml-format-link (text href &optional extra)
  1433. (let ((extra (concat (format " href=\"%s\"" href)
  1434. (and extra (concat " " extra)))))
  1435. (org-xhtml-format-tags '("<a%s>" . "</a>") text extra)))
  1436. (defun org-xhtml-format-heading (text level &optional id)
  1437. (let* ((extra (concat (when id (format " id=\"%s\"" id)))))
  1438. (concat (format "<h%d%s>" level extra) text (format "</h%d>" level))))
  1439. (defun org-xhtml-format-headline (title extra-targets tags
  1440. &optional snumber level)
  1441. (concat
  1442. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1443. (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1444. title
  1445. (and tags (concat (org-lparse-format 'SPACES 3)
  1446. (org-lparse-format 'ORG-TAGS tags)))))
  1447. (defun org-xhtml-format-anchor (text name &optional class)
  1448. (let* ((id name)
  1449. (extra (concat
  1450. (when name (format " name=\"%s\"" name))
  1451. (when id (format " id=\"%s\"" id))
  1452. (when class (format " class=\"%s\"" class)))))
  1453. (org-xhtml-format-tags '("<a%s>" . "</a>") text extra)))
  1454. (defun org-xhtml-format-footnote-reference (n def refcnt)
  1455. (let ((extra (if (= refcnt 1) "" (format ".%d" refcnt))))
  1456. (format org-export-xhtml-footnote-format
  1457. (format
  1458. "<a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a>"
  1459. n extra n n))))
  1460. (defun org-xhtml-format-footnotes-section (section-name definitions)
  1461. (if (not definitions) ""
  1462. (format org-export-xhtml-footnotes-section section-name definitions)))
  1463. (defun org-xhtml-format-org-entity (wd)
  1464. (org-entity-get-representation wd 'html))
  1465. (defun org-xhtml-format-tags (tag text &rest args)
  1466. (let ((prefix (when org-lparse-encode-pending "@"))
  1467. (suffix (when org-lparse-encode-pending "@")))
  1468. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1469. (defun org-xhtml-get (what &optional opt-plist)
  1470. (case what
  1471. (BACKEND 'html)
  1472. (INIT-METHOD nil)
  1473. (SAVE-METHOD nil)
  1474. (CLEANUP-METHOD nil)
  1475. ;; (OTHER-BACKENDS
  1476. ;; ;; There is a provision to register a per-backend converter and
  1477. ;; ;; output formats. Refer `org-lparse-get-converter' and
  1478. ;; ;; `org-lparse-get-other-backends'.
  1479. ;; ;; The default behaviour is to use `org-lparse-convert-process'
  1480. ;; ;; and `org-lparse-convert-capabilities'.
  1481. ;; )
  1482. ;; (CONVERT-METHOD
  1483. ;; ;; See note above
  1484. ;; )
  1485. (EXPORT-DIR (org-export-directory :html opt-plist))
  1486. (FILE-NAME-EXTENSION (plist-get opt-plist :html-extension))
  1487. (EXPORT-BUFFER-NAME "*Org HTML Export*")
  1488. (ENTITY-CONTROL org-xhtml-entity-control-callbacks-alist)
  1489. (ENTITY-FORMAT org-xhtml-entity-format-callbacks-alist)
  1490. (TOPLEVEL-HLEVEL org-export-xhtml-toplevel-hlevel)
  1491. (SPECIAL-STRING-REGEXPS org-export-xhtml-special-string-regexps)
  1492. (CODING-SYSTEM-FOR-WRITE org-export-xhtml-coding-system)
  1493. (CODING-SYSTEM-FOR-SAVE org-export-xhtml-coding-system)
  1494. (INLINE-IMAGES org-export-xhtml-inline-images)
  1495. (INLINE-IMAGE-EXTENSIONS org-export-xhtml-inline-image-extensions)
  1496. (PLAIN-TEXT-MAP org-export-xhtml-protect-char-alist)
  1497. (TABLE-FIRST-COLUMN-AS-LABELS
  1498. org-export-xhtml-table-use-header-tags-for-first-column)
  1499. (TODO-KWD-CLASS-PREFIX org-export-xhtml-todo-kwd-class-prefix)
  1500. (TAG-CLASS-PREFIX org-export-xhtml-tag-class-prefix)
  1501. (FOOTNOTE-SEPARATOR org-export-xhtml-footnote-separator)
  1502. (t (error "Unknown property: %s" what))))
  1503. (defun org-xhtml-get-coding-system-for-write ()
  1504. (or org-export-xhtml-coding-system
  1505. (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
  1506. (defun org-xhtml-get-coding-system-for-save ()
  1507. (or org-export-xhtml-coding-system
  1508. (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
  1509. (defun org-xhtml-insert-toc (toc)
  1510. ;; locate where toc needs to be inserted
  1511. (goto-char (point-min))
  1512. (cond
  1513. ((or (re-search-forward "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1514. (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t))
  1515. (goto-char (match-beginning 0))
  1516. (replace-match "")
  1517. (insert toc))
  1518. (org-lparse-dyn-first-heading-pos
  1519. (goto-char org-lparse-dyn-first-heading-pos)
  1520. (when (looking-at "\\s-*</p>")
  1521. (goto-char (match-end 0))
  1522. (insert "\n"))
  1523. (insert toc))
  1524. (t (ignore))))
  1525. (defun org-xhtml-insert-preamble (opt-plist)
  1526. (when (plist-get opt-plist :html-preamble)
  1527. (let ((html-pre (plist-get opt-plist :html-preamble))
  1528. (title (plist-get opt-plist :title))
  1529. (date (plist-get opt-plist :effective-date))
  1530. (author (plist-get opt-plist :author))
  1531. (lang-words (plist-get opt-plist :lang-words))
  1532. (email (plist-get opt-plist :email)))
  1533. (cond ((stringp html-pre)
  1534. (insert
  1535. (format-spec html-pre `((?t . ,title) (?a . ,author)
  1536. (?d . ,date) (?e . ,email)))))
  1537. ((functionp html-pre)
  1538. (funcall html-pre opt-plist))
  1539. (t
  1540. (insert
  1541. (format-spec
  1542. (or (cadr (assoc (nth 0 lang-words)
  1543. org-export-xhtml-preamble-format))
  1544. (cadr (assoc "en" org-export-xhtml-preamble-format)))
  1545. `((?t . ,title) (?a . ,author)
  1546. (?d . ,date) (?e . ,email)))))))))
  1547. (defun org-xhtml-insert-postamble (opt-plist)
  1548. (when org-lparse-footnote-definitions
  1549. (insert
  1550. (org-lparse-format
  1551. 'FOOTNOTES-SECTION (nth 4 (plist-get opt-plist :lang-words))
  1552. (mapconcat (lambda (x) (cdr x))
  1553. (nreverse org-lparse-footnote-definitions) "\n"))))
  1554. (let ((bib (org-export-xhtml-get-bibliography)))
  1555. (when bib
  1556. (insert "\n" bib "\n")))
  1557. ;; export html postamble
  1558. (unless body-only
  1559. (let* ((html-post (plist-get opt-plist :html-postamble))
  1560. (date (plist-get opt-plist :effective-date))
  1561. (author (plist-get opt-plist :author))
  1562. (email (plist-get opt-plist :email))
  1563. (lang-words (plist-get opt-plist :lang-words))
  1564. (html-validation-link (or org-export-xhtml-validation-link ""))
  1565. (email
  1566. (mapconcat (lambda(e)
  1567. (format "<a href=\"mailto:%s\">%s</a>" e e))
  1568. (split-string email ",+ *")
  1569. ", "))
  1570. (creator-info
  1571. (concat "Org version " org-version " with Emacs version "
  1572. (number-to-string emacs-major-version))))
  1573. (when (plist-get opt-plist :html-postamble)
  1574. (cond ((stringp html-post)
  1575. (insert "<div id=\"postamble\">\n")
  1576. (insert (format-spec html-post
  1577. `((?a . ,author) (?e . ,email)
  1578. (?d . ,date) (?c . ,creator-info)
  1579. (?v . ,html-validation-link))))
  1580. (insert "</div>"))
  1581. ((functionp html-post)
  1582. (funcall html-post opt-plist))
  1583. ((eq html-post 'auto)
  1584. ;; fall back on default postamble
  1585. (insert "<div id=\"postamble\">\n")
  1586. (when (plist-get opt-plist :time-stamp-file)
  1587. (insert "<p class=\"date\">" (nth 2 lang-words) ": " date "</p>\n"))
  1588. (when (and (plist-get opt-plist :author-info) author)
  1589. (insert "<p class=\"author\">" (nth 1 lang-words) ": " author "</p>\n"))
  1590. (when (and (plist-get opt-plist :email-info) email)
  1591. (insert "<p class=\"email\">" email "</p>\n"))
  1592. (when (plist-get opt-plist :creator-info)
  1593. (insert "<p class=\"creator\">"
  1594. (concat "Org version " org-version " with Emacs version "
  1595. (number-to-string emacs-major-version) "</p>\n")))
  1596. (insert html-validation-link "\n</div>"))
  1597. (t
  1598. (insert "<div id=\"postamble\">\n")
  1599. (insert (format-spec
  1600. (or (cadr (assoc (nth 0 lang-words)
  1601. org-export-xhtml-postamble-format))
  1602. (cadr (assoc "en" org-export-xhtml-postamble-format)))
  1603. `((?a . ,author) (?e . ,email)
  1604. (?d . ,date) (?c . ,creator-info)
  1605. (?v . ,html-validation-link))))
  1606. (insert "</div>"))))))
  1607. (if org-export-xhtml-with-timestamp
  1608. (insert org-export-xhtml-html-helper-timestamp)))
  1609. ;; There are references to org-html-expand, org-html-protect and
  1610. ;; org-html-do-expand outside of org-html.el. For now provide a
  1611. ;; migration path. Ultimately these functions have to be removed.
  1612. ;; (defun org-html-expand (string)
  1613. ;; "A simple wrapper around `org-xml-encode-org-text-skip-links'."
  1614. ;; (org-xml-encode-org-text-skip-links string))
  1615. ;; (defun org-html-protect (s)
  1616. ;; "A simple wrapper around `org-xml-encode-plain-text'."
  1617. ;; (org-xml-encode-plain-text s))
  1618. ;; (defun org-html-do-expand (s)
  1619. ;; "A simple wrapper around `org-xml-encode-org-text'."
  1620. ;; (org-xml-encode-org-text s))
  1621. ;; (defun org-export-xhtml-format-href (s)
  1622. ;; "A simple wrapper around `org-xml-format-href'."
  1623. ;; (org-xml-format-href s))
  1624. ;; (defun org-export-xhtml-format-desc (s)
  1625. ;; "A simple wrapper around `org-xml-format-desc'."
  1626. ;; (org-xml-format-desc s))
  1627. (provide 'org-xhtml)
  1628. ;;; org-xhtml.el ends here