org-html.el 89 KB

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