org-html.el 92 KB

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