org-html.el 87 KB

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