org-html.el 71 KB

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