org-html.el 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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)
  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. (get-text-property 0 'org-protected (car lines))))
  776. (insert (pop lines) "\n"))
  777. (and par (insert "<p>\n")))
  778. (throw 'nextline nil))
  779. ;; Horizontal line
  780. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  781. (if org-par-open
  782. (insert "\n</p>\n<hr/>\n<p>\n")
  783. (insert "\n<hr/>\n"))
  784. (throw 'nextline nil))
  785. ;; Blockquotes, verse, and center
  786. (when (equal "ORG-BLOCKQUOTE-START" line)
  787. (org-close-par-maybe)
  788. (insert "<blockquote>\n")
  789. (org-open-par)
  790. (throw 'nextline nil))
  791. (when (equal "ORG-BLOCKQUOTE-END" line)
  792. (org-close-par-maybe)
  793. (insert "\n</blockquote>\n")
  794. (org-open-par)
  795. (throw 'nextline nil))
  796. (when (equal "ORG-VERSE-START" line)
  797. (org-close-par-maybe)
  798. (insert "\n<p class=\"verse\">\n")
  799. (setq inverse t)
  800. (throw 'nextline nil))
  801. (when (equal "ORG-VERSE-END" line)
  802. (insert "</p>\n")
  803. (org-open-par)
  804. (setq inverse nil)
  805. (throw 'nextline nil))
  806. (when (equal "ORG-CENTER-START" line)
  807. (org-close-par-maybe)
  808. (insert "\n<div style=\"text-align: center\">")
  809. (org-open-par)
  810. (throw 'nextline nil))
  811. (when (equal "ORG-CENTER-END" line)
  812. (org-close-par-maybe)
  813. (insert "\n</div>")
  814. (org-open-par)
  815. (throw 'nextline nil))
  816. (run-hooks 'org-export-html-after-blockquotes-hook)
  817. (when inverse
  818. (let ((i (org-get-string-indentation line)))
  819. (if (> i 0)
  820. (setq line (concat (mapconcat 'identity
  821. (make-list (* 2 i) "\\nbsp") "")
  822. " " (org-trim line))))
  823. (unless (string-match "\\\\\\\\[ \t]*$" line)
  824. (setq line (concat line "\\\\")))))
  825. ;; make targets to anchors
  826. (while (string-match
  827. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  828. (cond
  829. ((match-end 2)
  830. (setq line (replace-match
  831. (format
  832. "@<a name=\"%s\" id=\"%s\">@</a>"
  833. (org-solidify-link-text (match-string 1 line))
  834. (org-solidify-link-text (match-string 1 line)))
  835. t t line)))
  836. ((and org-export-with-toc (equal (string-to-char line) ?*))
  837. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  838. (setq line (replace-match
  839. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  840. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  841. t t line)))
  842. (t
  843. (setq line (replace-match
  844. (concat "@<a name=\""
  845. (org-solidify-link-text (match-string 1 line))
  846. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  847. t t line)))))
  848. (setq line (org-html-handle-time-stamps line))
  849. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  850. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  851. ;; Also handle sub_superscripts and checkboxes
  852. (or (string-match org-table-hline-regexp line)
  853. (setq line (org-html-expand line)))
  854. ;; Format the links
  855. (setq start 0)
  856. (while (string-match org-bracket-link-analytic-regexp++ line start)
  857. (setq start (match-beginning 0))
  858. (setq path (save-match-data (org-link-unescape
  859. (match-string 3 line))))
  860. (setq type (cond
  861. ((match-end 2) (match-string 2 line))
  862. ((save-match-data
  863. (or (file-name-absolute-p path)
  864. (string-match "^\\.\\.?/" path)))
  865. "file")
  866. (t "internal")))
  867. (setq path (org-extract-attributes (org-link-unescape path)))
  868. (setq attr (get-text-property 0 'org-attributes path))
  869. (setq desc1 (if (match-end 5) (match-string 5 line))
  870. desc2 (if (match-end 2) (concat type ":" path) path)
  871. descp (and desc1 (not (equal desc1 desc2)))
  872. desc (or desc1 desc2))
  873. ;; Make an image out of the description if that is so wanted
  874. (when (and descp (org-file-image-p
  875. desc org-export-html-inline-image-extensions))
  876. (save-match-data
  877. (if (string-match "^file:" desc)
  878. (setq desc (substring desc (match-end 0)))))
  879. (setq desc (org-add-props
  880. (concat "<img src=\"" desc "\"/>")
  881. '(org-protected t))))
  882. ;; FIXME: do we need to unescape here somewhere?
  883. (cond
  884. ((equal type "internal")
  885. (setq rpl
  886. (concat
  887. "<a href=\""
  888. (if (= (string-to-char path) ?#) "" "#")
  889. (org-solidify-link-text
  890. (save-match-data (org-link-unescape path)) nil)
  891. "\"" attr ">"
  892. (org-export-html-format-desc desc)
  893. "</a>")))
  894. ((and (equal type "id")
  895. (setq id-file (org-id-find-id-file path)))
  896. ;; This is an id: link to another file (if it was the same file,
  897. ;; it would have become an internal link...)
  898. (setq id-file (file-relative-name
  899. id-file (file-name-directory org-current-export-file)))
  900. (setq id-file (concat (file-name-sans-extension id-file)
  901. "." html-extension))
  902. (setq rpl (concat "<a href=\"" id-file "#"
  903. (if (org-uuidgen-p path) "ID-")
  904. path "\""
  905. attr ">"
  906. (org-export-html-format-desc desc)
  907. "</a>")))
  908. ((member type '("http" "https"))
  909. ;; standard URL, just check if we need to inline an image
  910. (if (and (or (eq t org-export-html-inline-images)
  911. (and org-export-html-inline-images (not descp)))
  912. (org-file-image-p
  913. path org-export-html-inline-image-extensions))
  914. (setq rpl (org-export-html-format-image
  915. (concat type ":" path) org-par-open))
  916. (setq link (concat type ":" path))
  917. (setq rpl (concat "<a href=\""
  918. (org-export-html-format-href link)
  919. "\"" attr ">"
  920. (org-export-html-format-desc desc)
  921. "</a>"))))
  922. ((member type '("ftp" "mailto" "news"))
  923. ;; standard URL
  924. (setq link (concat type ":" path))
  925. (setq rpl (concat "<a href=\""
  926. (org-export-html-format-href link)
  927. "\"" attr ">"
  928. (org-export-html-format-desc desc)
  929. "</a>")))
  930. ((string= type "coderef")
  931. (setq rpl (format "<a href=\"#coderef-%s\" class=\"coderef\" onmouseover=\"CodeHighlightOn(this, 'coderef-%s');\" onmouseout=\"CodeHighlightOff(this, 'coderef-%s');\">%s</a>"
  932. path path path
  933. (format (org-export-get-coderef-format path (and descp desc))
  934. (cdr (assoc path org-export-code-refs))))))
  935. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  936. ;; The link protocol has a function for format the link
  937. (setq rpl
  938. (save-match-data
  939. (funcall fnc (org-link-unescape path) desc1 'html))))
  940. ((string= type "file")
  941. ;; FILE link
  942. (let* ((filename path)
  943. (abs-p (file-name-absolute-p filename))
  944. thefile file-is-image-p search)
  945. (save-match-data
  946. (if (string-match "::\\(.*\\)" filename)
  947. (setq search (match-string 1 filename)
  948. filename (replace-match "" t nil filename)))
  949. (setq valid
  950. (if (functionp link-validate)
  951. (funcall link-validate filename current-dir)
  952. t))
  953. (setq file-is-image-p
  954. (org-file-image-p
  955. filename org-export-html-inline-image-extensions))
  956. (setq thefile (if abs-p (expand-file-name filename) filename))
  957. (when (and org-export-html-link-org-files-as-html
  958. (string-match "\\.org$" thefile))
  959. (setq thefile (concat (substring thefile 0
  960. (match-beginning 0))
  961. "." html-extension))
  962. (if (and search
  963. ;; make sure this is can be used as target search
  964. (not (string-match "^[0-9]*$" search))
  965. (not (string-match "^\\*" search))
  966. (not (string-match "^/.*/$" search)))
  967. (setq thefile (concat thefile "#"
  968. (org-solidify-link-text
  969. (org-link-unescape search)))))
  970. (when (string-match "^file:" desc)
  971. (setq desc (replace-match "" t t desc))
  972. (if (string-match "\\.org$" desc)
  973. (setq desc (replace-match "" t t desc))))))
  974. (setq rpl (if (and file-is-image-p
  975. (or (eq t org-export-html-inline-images)
  976. (and org-export-html-inline-images
  977. (not descp))))
  978. (progn
  979. (message "image %s %s" thefile org-par-open)
  980. (org-export-html-format-image thefile org-par-open))
  981. (concat "<a href=\"" thefile "\"" attr ">"
  982. (org-export-html-format-desc desc)
  983. "</a>")))
  984. (if (not valid) (setq rpl desc))))
  985. (t
  986. ;; just publish the path, as default
  987. (setq rpl (concat "<i>&lt;" type ":"
  988. (save-match-data (org-link-unescape path))
  989. "&gt;</i>"))))
  990. (setq line (replace-match rpl t t line)
  991. start (+ start (length rpl))))
  992. ;; TODO items
  993. (if (and (string-match org-todo-line-regexp line)
  994. (match-beginning 2))
  995. (setq line
  996. (concat (substring line 0 (match-beginning 2))
  997. "<span class=\""
  998. (if (member (match-string 2 line)
  999. org-done-keywords)
  1000. "done" "todo")
  1001. " " (match-string 2 line)
  1002. "\"> " (org-export-html-get-todo-kwd-class-name
  1003. (match-string 2 line))
  1004. "</span>" (substring line (match-end 2)))))
  1005. ;; Does this contain a reference to a footnote?
  1006. (when org-export-with-footnotes
  1007. (setq start 0)
  1008. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  1009. (if (get-text-property (match-beginning 2) 'org-protected line)
  1010. (setq start (match-end 2))
  1011. (let ((n (match-string 2 line)) extra a)
  1012. (if (setq a (assoc n footref-seen))
  1013. (progn
  1014. (setcdr a (1+ (cdr a)))
  1015. (setq extra (format ".%d" (cdr a))))
  1016. (setq extra "")
  1017. (push (cons n 1) footref-seen))
  1018. (setq line
  1019. (replace-match
  1020. (format
  1021. "%s<sup><a class=\"footref\" name=\"fnr.%s%s\" href=\"#fn.%s\">%s</a></sup>"
  1022. (match-string 1 line) n extra n n)
  1023. t t line))))))
  1024. (cond
  1025. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1026. ;; This is a headline
  1027. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1028. level-offset))
  1029. txt (match-string 2 line))
  1030. (if (string-match quote-re0 txt)
  1031. (setq txt (replace-match "" t t txt)))
  1032. (if (<= level (max umax umax-toc))
  1033. (setq head-count (+ head-count 1)))
  1034. (when in-local-list
  1035. ;; Close any local lists before inserting a new header line
  1036. (while local-list-type
  1037. (org-close-li (car local-list-type))
  1038. (insert (format "</%sl>\n" (car local-list-type)))
  1039. (pop local-list-type))
  1040. (setq local-list-indent nil
  1041. in-local-list nil))
  1042. (setq first-heading-pos (or first-heading-pos (point)))
  1043. (org-html-level-start level txt umax
  1044. (and org-export-with-toc (<= level umax))
  1045. head-count)
  1046. ;; QUOTES
  1047. (when (string-match quote-re line)
  1048. (org-close-par-maybe)
  1049. (insert "<pre>")
  1050. (setq inquote t)))
  1051. ((string-match "^[ \t]*- __+[ \t]*$" line)
  1052. ;; Explicit list closure
  1053. (when local-list-type
  1054. (let ((ind (org-get-indentation line)))
  1055. (while (and local-list-indent
  1056. (<= ind (car local-list-indent)))
  1057. (org-close-li (car local-list-type))
  1058. (insert (format "</%sl>\n" (car local-list-type)))
  1059. (pop local-list-type)
  1060. (pop local-list-indent))
  1061. (or local-list-indent (setq in-local-list nil))))
  1062. (throw 'nextline nil))
  1063. ((and org-export-with-tables
  1064. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1065. (when (not table-open)
  1066. ;; New table starts
  1067. (setq table-open t table-buffer nil table-orig-buffer nil))
  1068. ;; Accumulate lines
  1069. (setq table-buffer (cons line table-buffer)
  1070. table-orig-buffer (cons origline table-orig-buffer))
  1071. (when (or (not lines)
  1072. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1073. (car lines))))
  1074. (setq table-open nil
  1075. table-buffer (nreverse table-buffer)
  1076. table-orig-buffer (nreverse table-orig-buffer))
  1077. (org-close-par-maybe)
  1078. (insert (org-format-table-html table-buffer table-orig-buffer))))
  1079. (t
  1080. ;; Normal lines
  1081. (when (string-match
  1082. (cond
  1083. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1084. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1085. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  1086. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  1087. line)
  1088. (setq ind (or (get-text-property 0 'original-indentation line)
  1089. (org-get-string-indentation line))
  1090. item-type (if (match-beginning 4) "o" "u")
  1091. starter (if (match-beginning 2)
  1092. (substring (match-string 2 line) 0 -1))
  1093. line (substring line (match-beginning 5))
  1094. item-tag nil)
  1095. (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
  1096. (setq item-type "d"
  1097. item-tag (match-string 1 line)
  1098. line (substring line (match-end 0))))
  1099. (when (and (not (equal item-type "d"))
  1100. (not (string-match "[^ \t]" line)))
  1101. ;; empty line. Pretend indentation is large.
  1102. (setq ind (if org-empty-line-terminates-plain-lists
  1103. 0
  1104. (1+ (or (car local-list-indent) 1)))))
  1105. (setq didclose nil)
  1106. (while (and in-local-list
  1107. (or (and (= ind (car local-list-indent))
  1108. (not starter))
  1109. (< ind (car local-list-indent))))
  1110. (setq didclose t)
  1111. (org-close-li (car local-list-type))
  1112. (insert (format "</%sl>\n" (car local-list-type)))
  1113. (pop local-list-type) (pop local-list-indent)
  1114. (setq in-local-list local-list-indent))
  1115. (cond
  1116. ((and starter
  1117. (or (not in-local-list)
  1118. (> ind (car local-list-indent))))
  1119. ;; Start new (level of) list
  1120. (org-close-par-maybe)
  1121. (insert (cond
  1122. ((equal item-type "u") "<ul>\n<li>\n")
  1123. ((equal item-type "o") "<ol>\n<li>\n")
  1124. ((equal item-type "d")
  1125. (format "<dl>\n<dt>%s</dt><dd>\n" item-tag))))
  1126. (push item-type local-list-type)
  1127. (push ind local-list-indent)
  1128. (setq in-local-list t))
  1129. (starter
  1130. ;; continue current list
  1131. (org-close-li (car local-list-type))
  1132. (insert (cond
  1133. ((equal (car local-list-type) "d")
  1134. (format "<dt>%s</dt><dd>\n" (or item-tag "???")))
  1135. (t "<li>\n"))))
  1136. (didclose
  1137. ;; we did close a list, normal text follows: need <p>
  1138. (org-open-par)))
  1139. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  1140. (setq line
  1141. (replace-match
  1142. (if (equal (match-string 1 line) "X")
  1143. "<b>[X]</b>"
  1144. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  1145. t t line))))
  1146. ;; Empty lines start a new paragraph. If hand-formatted lists
  1147. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1148. ;; also start a new paragraph.
  1149. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  1150. ;; Is this the start of a footnote?
  1151. (when org-export-with-footnotes
  1152. (when (and (boundp 'footnote-section-tag-regexp)
  1153. (string-match (concat "^" footnote-section-tag-regexp)
  1154. line))
  1155. ;; ignore this line
  1156. (throw 'nextline nil))
  1157. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1158. (org-close-par-maybe)
  1159. (let ((n (match-string 1 line)))
  1160. (setq org-par-open t
  1161. line (replace-match
  1162. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  1163. ;; Check if the line break needs to be conserved
  1164. (cond
  1165. ((string-match "\\\\\\\\[ \t]*$" line)
  1166. (setq line (replace-match "<br/>" t t line)))
  1167. (org-export-preserve-breaks
  1168. (setq line (concat line "<br/>"))))
  1169. ;; Check if a paragraph should be started
  1170. (let ((start 0))
  1171. (while (and org-par-open
  1172. (string-match "\\\\par\\>" line start))
  1173. ;; Leave a space in the </p> so that the footnote matcher
  1174. ;; does not see this.
  1175. (if (not (get-text-property (match-beginning 0)
  1176. 'org-protected line))
  1177. (setq line (replace-match "</p ><p >" t t line)))
  1178. (setq start (match-end 0))))
  1179. (insert line "\n")))))
  1180. ;; Properly close all local lists and other lists
  1181. (when inquote
  1182. (insert "</pre>\n")
  1183. (org-open-par))
  1184. (when in-local-list
  1185. ;; Close any local lists before inserting a new header line
  1186. (while local-list-type
  1187. (org-close-li (car local-list-type))
  1188. (insert (format "</%sl>\n" (car local-list-type)))
  1189. (pop local-list-type))
  1190. (setq local-list-indent nil
  1191. in-local-list nil))
  1192. (org-html-level-start 1 nil umax
  1193. (and org-export-with-toc (<= level umax))
  1194. head-count)
  1195. ;; the </div> to close the last text-... div.
  1196. (when (and (> umax 0) first-heading-pos) (insert "</div>\n"))
  1197. (save-excursion
  1198. (goto-char (point-min))
  1199. (while (re-search-forward "<p class=\"footnote\">[^\000]*?\\(</p>\\|\\'\\)" nil t)
  1200. (push (match-string 0) footnotes)
  1201. (replace-match "" t t)))
  1202. (when footnotes
  1203. (insert (format org-export-html-footnotes-section
  1204. (or (nth 4 lang-words) "Footnotes")
  1205. (mapconcat 'identity (nreverse footnotes) "\n"))
  1206. "\n"))
  1207. (let ((bib (org-export-html-get-bibliography)))
  1208. (when bib
  1209. (insert "\n" bib "\n")))
  1210. (unless body-only
  1211. (when (plist-get opt-plist :auto-postamble)
  1212. (insert "<div id=\"postamble\">\n")
  1213. (when (and org-export-author-info author)
  1214. (insert "<p class=\"author\"> "
  1215. (nth 1 lang-words) ": " author "\n")
  1216. (when email
  1217. (if (listp (split-string email ",+ *"))
  1218. (mapc (lambda(e)
  1219. (insert "<a href=\"mailto:" e "\">&lt;"
  1220. e "&gt;</a>\n"))
  1221. (split-string email ",+ *"))
  1222. (insert "<a href=\"mailto:" email "\">&lt;"
  1223. email "&gt;</a>\n")))
  1224. (insert "</p>\n"))
  1225. (when (and date org-export-time-stamp-file)
  1226. (insert "<p class=\"date\"> "
  1227. (nth 2 lang-words) ": "
  1228. date "</p>\n"))
  1229. (when org-export-creator-info
  1230. (insert (format "<p class=\"creator\">HTML generated by org-mode %s in emacs %s</p>\n"
  1231. org-version emacs-major-version)))
  1232. (when org-export-html-validation-link
  1233. (insert org-export-html-validation-link "\n"))
  1234. (insert "</div>"))
  1235. (if org-export-html-with-timestamp
  1236. (insert org-export-html-html-helper-timestamp))
  1237. (insert (or (plist-get opt-plist :postamble) ""))
  1238. (insert "\n</div>\n</body>\n</html>\n"))
  1239. (unless (plist-get opt-plist :buffer-will-be-killed)
  1240. (normal-mode)
  1241. (if (eq major-mode default-major-mode) (html-mode)))
  1242. ;; insert the table of contents
  1243. (goto-char (point-min))
  1244. (when thetoc
  1245. (if (or (re-search-forward
  1246. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  1247. (re-search-forward
  1248. "\\[TABLE-OF-CONTENTS\\]" nil t))
  1249. (progn
  1250. (goto-char (match-beginning 0))
  1251. (replace-match ""))
  1252. (goto-char first-heading-pos)
  1253. (when (looking-at "\\s-*</p>")
  1254. (goto-char (match-end 0))
  1255. (insert "\n")))
  1256. (insert "<div id=\"table-of-contents\">\n")
  1257. (mapc 'insert thetoc)
  1258. (insert "</div>\n"))
  1259. ;; remove empty paragraphs and lists
  1260. (goto-char (point-min))
  1261. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  1262. (replace-match ""))
  1263. (goto-char (point-min))
  1264. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  1265. (replace-match ""))
  1266. (goto-char (point-min))
  1267. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  1268. (replace-match ""))
  1269. ;; Convert whitespace place holders
  1270. (goto-char (point-min))
  1271. (let (beg end n)
  1272. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  1273. (setq n (get-text-property beg 'org-whitespace)
  1274. end (next-single-property-change beg 'org-whitespace))
  1275. (goto-char beg)
  1276. (delete-region beg end)
  1277. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  1278. (make-string n ?x)))))
  1279. (or to-buffer (save-buffer))
  1280. (goto-char (point-min))
  1281. (or (org-export-push-to-kill-ring "HTML")
  1282. (message "Exporting... done"))
  1283. (if (eq to-buffer 'string)
  1284. (prog1 (buffer-substring (point-min) (point-max))
  1285. (kill-buffer (current-buffer)))
  1286. (current-buffer)))))
  1287. (defun org-export-html-format-href (s)
  1288. "Make sure the S is valid as a href reference in an XHTML document."
  1289. (save-match-data
  1290. (let ((start 0))
  1291. (while (string-match "&" s start)
  1292. (setq start (+ (match-beginning 0) 3)
  1293. s (replace-match "&amp;" t t s)))))
  1294. s)
  1295. (defun org-export-html-format-desc (s)
  1296. "Make sure the S is valid as a description in a link."
  1297. (if (and s (not (get-text-property 1 'org-protected s)))
  1298. (save-match-data
  1299. (org-html-do-expand s))
  1300. s))
  1301. (defun org-export-html-format-image (src par-open)
  1302. "Create image tag with source and attributes."
  1303. (save-match-data
  1304. (if (string-match "^ltxpng/" src)
  1305. (format "<img src=\"%s\"/>" src)
  1306. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1307. (attr (org-find-text-property-in-string 'org-attributes src))
  1308. (label (org-find-text-property-in-string 'org-label src)))
  1309. (format "%s<div %sclass=\"figure\">
  1310. <p><img src=\"%s\"%s /></p>%s
  1311. </div>%s"
  1312. (if org-par-open "</p>\n" "")
  1313. (if label (format "id=\"%s\" " label) "")
  1314. src
  1315. (if (string-match "\\<alt=" (or attr ""))
  1316. (concat " " attr )
  1317. (concat " " attr " alt=\"" src "\""))
  1318. (if caption (concat "\n<p>" caption "</p>") "")
  1319. (if org-par-open "\n<p>" ""))))))
  1320. (defun org-export-html-get-bibliography ()
  1321. "Find bibliography, cut it out and return it."
  1322. (catch 'exit
  1323. (let (beg end (cnt 1) bib)
  1324. (save-excursion
  1325. (goto-char (point-min))
  1326. (when (re-search-forward "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  1327. (setq beg (match-beginning 0))
  1328. (while (re-search-forward "</?div\\>" nil t)
  1329. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  1330. (when (= cnt 0)
  1331. (and (looking-at ">") (forward-char 1))
  1332. (setq bib (buffer-substring beg (point)))
  1333. (delete-region beg (point))
  1334. (throw 'exit bib))))
  1335. nil))))
  1336. (defvar org-table-number-regexp) ; defined in org-table.el
  1337. (defun org-format-table-html (lines olines)
  1338. "Find out which HTML converter to use and return the HTML code."
  1339. (if (stringp lines)
  1340. (setq lines (org-split-string lines "\n")))
  1341. (if (string-match "^[ \t]*|" (car lines))
  1342. ;; A normal org table
  1343. (org-format-org-table-html lines)
  1344. ;; Table made by table.el - test for spanning
  1345. (let* ((hlines (delq nil (mapcar
  1346. (lambda (x)
  1347. (if (string-match "^[ \t]*\\+-" x) x
  1348. nil))
  1349. lines)))
  1350. (first (car hlines))
  1351. (ll (and (string-match "\\S-+" first)
  1352. (match-string 0 first)))
  1353. (re (concat "^[ \t]*" (regexp-quote ll)))
  1354. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  1355. hlines))))
  1356. (if (and (not spanning)
  1357. (not org-export-prefer-native-exporter-for-tables))
  1358. ;; We can use my own converter with HTML conversions
  1359. (org-format-table-table-html lines)
  1360. ;; Need to use the code generator in table.el, with the original text.
  1361. (org-format-table-table-html-using-table-generate-source olines)))))
  1362. (defvar org-table-number-fraction) ; defined in org-table.el
  1363. (defun org-format-org-table-html (lines &optional splice)
  1364. "Format a table into HTML."
  1365. (require 'org-table)
  1366. ;; Get rid of hlines at beginning and end
  1367. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1368. (setq lines (nreverse lines))
  1369. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1370. (setq lines (nreverse lines))
  1371. (when org-export-table-remove-special-lines
  1372. ;; Check if the table has a marking column. If yes remove the
  1373. ;; column and the special lines
  1374. (setq lines (org-table-clean-before-export lines)))
  1375. (let* ((caption (or (get-text-property 0 'org-caption (car lines))
  1376. (get-text-property (or (next-single-property-change
  1377. 0 'org-caption (car lines))
  1378. 0)
  1379. 'org-caption (car lines))))
  1380. (attributes (or (get-text-property 0 'org-attributes (car lines))
  1381. (get-text-property (or (next-single-property-change
  1382. 0 'org-attributes (car lines))
  1383. 0)
  1384. 'org-attributes (car lines))))
  1385. (html-table-tag (org-export-splice-attributes
  1386. html-table-tag attributes))
  1387. (head (and org-export-highlight-first-table-line
  1388. (delq nil (mapcar
  1389. (lambda (x) (string-match "^[ \t]*|-" x))
  1390. (cdr lines)))))
  1391. (nlines 0) fnum i
  1392. tbopen line fields html gr colgropen)
  1393. (if splice (setq head nil))
  1394. (unless splice (push (if head "<thead>" "<tbody>") html))
  1395. (setq tbopen t)
  1396. (while (setq line (pop lines))
  1397. (catch 'next-line
  1398. (if (string-match "^[ \t]*|-" line)
  1399. (progn
  1400. (unless splice
  1401. (push (if head "</thead>" "</tbody>") html)
  1402. (if lines (push "<tbody>" html) (setq tbopen nil)))
  1403. (setq head nil) ;; head ends here, first time around
  1404. ;; ignore this line
  1405. (throw 'next-line t)))
  1406. ;; Break the line into fields
  1407. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1408. (unless fnum (setq fnum (make-vector (length fields) 0)))
  1409. (setq nlines (1+ nlines) i -1)
  1410. (push (concat "<tr>"
  1411. (mapconcat
  1412. (lambda (x)
  1413. (setq i (1+ i))
  1414. (if (and (< i nlines)
  1415. (string-match org-table-number-regexp x))
  1416. (incf (aref fnum i)))
  1417. (cond
  1418. (head
  1419. (concat
  1420. (format (car org-export-table-header-tags) "col")
  1421. x
  1422. (cdr org-export-table-header-tags)))
  1423. ((and (= i 0) org-export-html-table-use-header-tags-for-first-column)
  1424. (concat
  1425. (format (car org-export-table-header-tags) "row")
  1426. x
  1427. (cdr org-export-table-header-tags)))
  1428. (t
  1429. (concat (car org-export-table-data-tags) x
  1430. (cdr org-export-table-data-tags)))))
  1431. fields "")
  1432. "</tr>")
  1433. html)))
  1434. (unless splice (if tbopen (push "</tbody>" html)))
  1435. (unless splice (push "</table>\n" html))
  1436. (setq html (nreverse html))
  1437. (unless splice
  1438. ;; Put in col tags with the alignment (unfortunately often ignored...)
  1439. (push (concat
  1440. "<colgroup>"
  1441. (mapconcat
  1442. (lambda (x)
  1443. (setq gr (pop org-table-colgroup-info))
  1444. (format "<col align=\"%s\" />"
  1445. (if (> (/ (float x) nlines) org-table-number-fraction)
  1446. "right" "left")))
  1447. fnum "")
  1448. "</colgroup>")
  1449. html)
  1450. ;; Since the output of HTML table formatter can also be used in
  1451. ;; DocBook document, we want to always include the caption to make
  1452. ;; DocBook XML file valid.
  1453. (push (format "<caption>%s</caption>" (or caption "")) html)
  1454. (push html-table-tag html))
  1455. (concat (mapconcat 'identity html "\n") "\n")))
  1456. (defun org-export-splice-attributes (tag attributes)
  1457. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  1458. (if (not attributes)
  1459. tag
  1460. (let (oldatt newatt)
  1461. (setq oldatt (org-extract-attributes-from-string tag)
  1462. tag (pop oldatt)
  1463. newatt (cdr (org-extract-attributes-from-string attributes)))
  1464. (while newatt
  1465. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  1466. (if (string-match ">" tag)
  1467. (setq tag
  1468. (replace-match (concat (org-attributes-to-string oldatt) ">")
  1469. t t tag)))
  1470. tag)))
  1471. (defun org-format-table-table-html (lines)
  1472. "Format a table generated by table.el into HTML.
  1473. This conversion does *not* use `table-generate-source' from table.el.
  1474. This has the advantage that Org-mode's HTML conversions can be used.
  1475. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1476. (let (line field-buffer
  1477. (head org-export-highlight-first-table-line)
  1478. fields html empty i)
  1479. (setq html (concat html-table-tag "\n"))
  1480. (while (setq line (pop lines))
  1481. (setq empty "&nbsp;")
  1482. (catch 'next-line
  1483. (if (string-match "^[ \t]*\\+-" line)
  1484. (progn
  1485. (if field-buffer
  1486. (progn
  1487. (setq
  1488. html
  1489. (concat
  1490. html
  1491. "<tr>"
  1492. (mapconcat
  1493. (lambda (x)
  1494. (if (equal x "") (setq x empty))
  1495. (if head
  1496. (concat
  1497. (format (car org-export-table-header-tags) "col")
  1498. x
  1499. (cdr org-export-table-header-tags))
  1500. (concat (car org-export-table-data-tags) x
  1501. (cdr org-export-table-data-tags))))
  1502. field-buffer "\n")
  1503. "</tr>\n"))
  1504. (setq head nil)
  1505. (setq field-buffer nil)))
  1506. ;; Ignore this line
  1507. (throw 'next-line t)))
  1508. ;; Break the line into fields and store the fields
  1509. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1510. (if field-buffer
  1511. (setq field-buffer (mapcar
  1512. (lambda (x)
  1513. (concat x "<br/>" (pop fields)))
  1514. field-buffer))
  1515. (setq field-buffer fields))))
  1516. (setq html (concat html "</table>\n"))
  1517. html))
  1518. (defun org-format-table-table-html-using-table-generate-source (lines)
  1519. "Format a table into html, using `table-generate-source' from table.el.
  1520. This has the advantage that cell- or row-spanning is allowed.
  1521. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  1522. (require 'table)
  1523. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1524. (erase-buffer)
  1525. (insert (mapconcat 'identity lines "\n"))
  1526. (goto-char (point-min))
  1527. (if (not (re-search-forward "|[^+]" nil t))
  1528. (error "Error processing table"))
  1529. (table-recognize-table)
  1530. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1531. (table-generate-source 'html " org-tmp2 ")
  1532. (set-buffer " org-tmp2 ")
  1533. (buffer-substring (point-min) (point-max))))
  1534. (defun org-export-splice-style (style extra)
  1535. "Splice EXTRA into STYLE, just before \"</style>\"."
  1536. (if (and (stringp extra)
  1537. (string-match "\\S-" extra)
  1538. (string-match "</style>" style))
  1539. (concat (substring style 0 (match-beginning 0))
  1540. "\n" extra "\n"
  1541. (substring style (match-beginning 0)))
  1542. style))
  1543. (defun org-html-handle-time-stamps (s)
  1544. "Format time stamps in string S, or remove them."
  1545. (catch 'exit
  1546. (let (r b)
  1547. (while (string-match org-maybe-keyword-time-regexp s)
  1548. (or b (setq b (substring s 0 (match-beginning 0))))
  1549. (setq r (concat
  1550. r (substring s 0 (match-beginning 0))
  1551. " @<span class=\"timestamp-wrapper\">"
  1552. (if (match-end 1)
  1553. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  1554. (match-string 1 s)))
  1555. (format " @<span class=\"timestamp\">%s@</span>"
  1556. (substring
  1557. (org-translate-time (match-string 3 s)) 1 -1))
  1558. "@</span>")
  1559. s (substring s (match-end 0))))
  1560. ;; Line break if line started and ended with time stamp stuff
  1561. (if (not r)
  1562. s
  1563. (setq r (concat r s))
  1564. (unless (string-match "\\S-" (concat b s))
  1565. (setq r (concat r "@<br/>")))
  1566. r))))
  1567. (defvar htmlize-buffer-places) ; from htmlize.el
  1568. (defun org-export-htmlize-region-for-paste (beg end)
  1569. "Convert the region to HTML, using htmlize.el.
  1570. This is much like `htmlize-region-for-paste', only that it uses
  1571. the settings define in the org-... variables."
  1572. (let* ((htmlize-output-type org-export-htmlize-output-type)
  1573. (htmlize-css-name-prefix org-export-htmlize-css-font-prefix)
  1574. (htmlbuf (htmlize-region beg end)))
  1575. (unwind-protect
  1576. (with-current-buffer htmlbuf
  1577. (buffer-substring (plist-get htmlize-buffer-places 'content-start)
  1578. (plist-get htmlize-buffer-places 'content-end)))
  1579. (kill-buffer htmlbuf))))
  1580. ;;;###autoload
  1581. (defun org-export-htmlize-generate-css ()
  1582. "Create the CSS for all font definitions in the current Emacs session.
  1583. Use this to create face definitions in your CSS style file that can then
  1584. be used by code snippets transformed by htmlize.
  1585. This command just produces a buffer that contains class definitions for all
  1586. faces used in the current Emacs session. You can copy and paste the ones you
  1587. need into your CSS file.
  1588. If you then set `org-export-htmlize-output-type' to `css', calls to
  1589. the function `org-export-htmlize-region-for-paste' will produce code
  1590. that uses these same face definitions."
  1591. (interactive)
  1592. (require 'htmlize)
  1593. (and (get-buffer "*html*") (kill-buffer "*html*"))
  1594. (with-temp-buffer
  1595. (let ((fl (face-list))
  1596. (htmlize-css-name-prefix "org-")
  1597. (htmlize-output-type 'css)
  1598. f i)
  1599. (while (setq f (pop fl)
  1600. i (and f (face-attribute f :inherit)))
  1601. (when (and (symbolp f) (or (not i) (not (listp i))))
  1602. (insert (org-add-props (copy-sequence "1") nil 'face f))))
  1603. (htmlize-region (point-min) (point-max))))
  1604. (switch-to-buffer "*html*")
  1605. (goto-char (point-min))
  1606. (if (re-search-forward "<style" nil t)
  1607. (delete-region (point-min) (match-beginning 0)))
  1608. (if (re-search-forward "</style>" nil t)
  1609. (delete-region (1+ (match-end 0)) (point-max)))
  1610. (beginning-of-line 1)
  1611. (if (looking-at " +") (replace-match ""))
  1612. (goto-char (point-min)))
  1613. (defun org-html-protect (s)
  1614. ;; convert & to &amp;, < to &lt; and > to &gt;
  1615. (let ((start 0))
  1616. (while (string-match "&" s start)
  1617. (setq s (replace-match "&amp;" t t s)
  1618. start (1+ (match-beginning 0))))
  1619. (while (string-match "<" s)
  1620. (setq s (replace-match "&lt;" t t s)))
  1621. (while (string-match ">" s)
  1622. (setq s (replace-match "&gt;" t t s)))
  1623. ; (while (string-match "\"" s)
  1624. ; (setq s (replace-match "&quot;" t t s)))
  1625. )
  1626. s)
  1627. (defun org-html-expand (string)
  1628. "Prepare STRING for HTML export. Applies all active conversions.
  1629. If there are links in the string, don't modify these."
  1630. (let* ((re (concat org-bracket-link-regexp "\\|"
  1631. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  1632. m s l res)
  1633. (while (setq m (string-match re string))
  1634. (setq s (substring string 0 m)
  1635. l (match-string 0 string)
  1636. string (substring string (match-end 0)))
  1637. (push (org-html-do-expand s) res)
  1638. (push l res))
  1639. (push (org-html-do-expand string) res)
  1640. (apply 'concat (nreverse res))))
  1641. (defun org-html-do-expand (s)
  1642. "Apply all active conversions to translate special ASCII to HTML."
  1643. (setq s (org-html-protect s))
  1644. (if org-export-html-expand
  1645. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1646. (setq s (replace-match "<\\1>" t nil s))))
  1647. (if org-export-with-emphasize
  1648. (setq s (org-export-html-convert-emphasize s)))
  1649. (if org-export-with-special-strings
  1650. (setq s (org-export-html-convert-special-strings s)))
  1651. (if org-export-with-sub-superscripts
  1652. (setq s (org-export-html-convert-sub-super s)))
  1653. (if org-export-with-TeX-macros
  1654. (let ((start 0) wd ass)
  1655. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1656. s start))
  1657. (if (get-text-property (match-beginning 0) 'org-protected s)
  1658. (setq start (match-end 0))
  1659. (setq wd (match-string 1 s))
  1660. (if (setq ass (assoc wd org-html-entities))
  1661. (setq s (replace-match (or (cdr ass)
  1662. (concat "&" (car ass) ";"))
  1663. t t s))
  1664. (setq start (+ start (length wd))))))))
  1665. s)
  1666. (defconst org-export-html-special-string-regexps
  1667. '(("\\\\-" . "&shy;")
  1668. ("---\\([^-]\\)" . "&mdash;\\1")
  1669. ("--\\([^-]\\)" . "&ndash;\\1")
  1670. ("\\.\\.\\." . "&hellip;"))
  1671. "Regular expressions for special string conversion.")
  1672. (defun org-export-html-convert-special-strings (string)
  1673. "Convert special characters in STRING to HTML."
  1674. (let ((all org-export-html-special-string-regexps)
  1675. e a re rpl start)
  1676. (while (setq a (pop all))
  1677. (setq re (car a) rpl (cdr a) start 0)
  1678. (while (string-match re string start)
  1679. (if (get-text-property (match-beginning 0) 'org-protected string)
  1680. (setq start (match-end 0))
  1681. (setq string (replace-match rpl t nil string)))))
  1682. string))
  1683. (defun org-export-html-convert-sub-super (string)
  1684. "Convert sub- and superscripts in STRING to HTML."
  1685. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1686. (while (string-match org-match-substring-regexp string s)
  1687. (cond
  1688. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1689. ((get-text-property (match-beginning 2) 'org-protected string)
  1690. (setq s (match-end 2)))
  1691. (t
  1692. (setq s (match-end 1)
  1693. key (if (string= (match-string 2 string) "_") "sub" "sup")
  1694. c (or (match-string 8 string)
  1695. (match-string 6 string)
  1696. (match-string 5 string))
  1697. string (replace-match
  1698. (concat (match-string 1 string)
  1699. "<" key ">" c "</" key ">")
  1700. t t string)))))
  1701. (while (string-match "\\\\\\([_^]\\)" string)
  1702. (setq string (replace-match (match-string 1 string) t t string)))
  1703. string))
  1704. (defun org-export-html-convert-emphasize (string)
  1705. "Apply emphasis."
  1706. (let ((s 0) rpl)
  1707. (while (string-match org-emph-re string s)
  1708. (if (not (equal
  1709. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1710. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1711. (setq s (match-beginning 0)
  1712. rpl
  1713. (concat
  1714. (match-string 1 string)
  1715. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  1716. (match-string 4 string)
  1717. (nth 3 (assoc (match-string 3 string)
  1718. org-emphasis-alist))
  1719. (match-string 5 string))
  1720. string (replace-match rpl t t string)
  1721. s (+ s (- (length rpl) 2)))
  1722. (setq s (1+ s))))
  1723. string))
  1724. (defun org-open-par ()
  1725. "Insert <p>, but first close previous paragraph if any."
  1726. (org-close-par-maybe)
  1727. (insert "\n<p>")
  1728. (setq org-par-open t))
  1729. (defun org-close-par-maybe ()
  1730. "Close paragraph if there is one open."
  1731. (when org-par-open
  1732. (insert "</p>")
  1733. (setq org-par-open nil)))
  1734. (defun org-close-li (&optional type)
  1735. "Close <li> if necessary."
  1736. (org-close-par-maybe)
  1737. (insert (if (equal type "d") "</dd>\n" "</li>\n")))
  1738. (defvar in-local-list)
  1739. (defvar local-list-indent)
  1740. (defun org-export-html-close-lists-maybe (line)
  1741. (let ((ind (get-text-property 0 'original-indentation line))
  1742. didclose)
  1743. (when ind
  1744. (while (and in-local-list
  1745. (<= ind (car local-list-indent)))
  1746. (setq didclose t)
  1747. (org-close-li (car local-list-type))
  1748. (insert (format "</%sl>\n" (car local-list-type)))
  1749. (pop local-list-type) (pop local-list-indent)
  1750. (setq in-local-list local-list-indent))
  1751. (and didclose (org-open-par)))))
  1752. (defvar body-only) ; dynamically scoped into this.
  1753. (defun org-html-level-start (level title umax with-toc head-count)
  1754. "Insert a new level in HTML export.
  1755. When TITLE is nil, just close all open levels."
  1756. (org-close-par-maybe)
  1757. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1758. (extra-targets (assoc target org-export-target-aliases))
  1759. (preferred (cdr (assoc target org-export-preferred-target-alist)))
  1760. (remove (or preferred target))
  1761. (l org-level-max)
  1762. snumber href suffix)
  1763. (setq extra-targets (remove remove extra-targets))
  1764. (setq extra-targets
  1765. (mapconcat (lambda (x)
  1766. (if (org-uuidgen-p x) (setq x (concat "ID-" x)))
  1767. (format "<a name=\"%s\" id=\"%s\"></a>"
  1768. x x))
  1769. extra-targets
  1770. ""))
  1771. (while (>= l level)
  1772. (if (aref org-levels-open (1- l))
  1773. (progn
  1774. (org-html-level-close l umax)
  1775. (aset org-levels-open (1- l) nil)))
  1776. (setq l (1- l)))
  1777. (when title
  1778. ;; If title is nil, this means this function is called to close
  1779. ;; all levels, so the rest is done only if title is given
  1780. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1781. (setq title (replace-match
  1782. (if org-export-with-tags
  1783. (save-match-data
  1784. (concat
  1785. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  1786. (mapconcat
  1787. (lambda (x)
  1788. (format "<span class=\"%s\">%s</span>"
  1789. (org-export-html-get-tag-class-name x)
  1790. x))
  1791. (org-split-string (match-string 1 title) ":")
  1792. "&nbsp;")
  1793. "</span>"))
  1794. "")
  1795. t t title)))
  1796. (if (> level umax)
  1797. (progn
  1798. (if (aref org-levels-open (1- level))
  1799. (progn
  1800. (org-close-li)
  1801. (if target
  1802. (insert (format "<li id=\"%s\">" target) extra-targets title "<br/>\n")
  1803. (insert "<li>" title "<br/>\n")))
  1804. (aset org-levels-open (1- level) t)
  1805. (org-close-par-maybe)
  1806. (if target
  1807. (insert (format "<ul>\n<li id=\"%s\">" target)
  1808. extra-targets title "<br/>\n")
  1809. (insert "<ul>\n<li>" title "<br/>\n"))))
  1810. (aset org-levels-open (1- level) t)
  1811. (setq snumber (org-section-number level))
  1812. (setq level (+ level org-export-html-toplevel-hlevel -1))
  1813. (if (and org-export-with-section-numbers (not body-only))
  1814. (setq title (concat
  1815. (format "<span class=\"section-number-%d\">%s</span>"
  1816. level snumber)
  1817. " " title)))
  1818. (unless (= head-count 1) (insert "\n</div>\n"))
  1819. (setq href (cdr (assoc (concat "sec-" snumber) org-export-preferred-target-alist)))
  1820. (setq suffix (or href snumber))
  1821. (setq href (or href (concat "sec-" snumber)))
  1822. (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"
  1823. suffix level level href
  1824. extra-targets
  1825. title level level suffix))
  1826. (org-open-par)))))
  1827. (defun org-export-html-get-tag-class-name (tag)
  1828. "Turn tag into a valid class name.
  1829. Replaces invalid characters with \"_\" and then prepends a prefix."
  1830. (save-match-data
  1831. (while (string-match "[^a-zA-Z0-9_]" tag)
  1832. (setq tag (replace-match "_" t t tag))))
  1833. (concat org-export-html-tag-class-prefix tag))
  1834. (defun org-export-html-get-todo-kwd-class-name (kwd)
  1835. "Turn todo keyword into a valid class name.
  1836. Replaces invalid characters with \"_\" and then prepends a prefix."
  1837. (save-match-data
  1838. (while (string-match "[^a-zA-Z0-9_]" kwd)
  1839. (setq kwd (replace-match "_" t t kwd))))
  1840. (concat org-export-html-todo-kwd-class-prefix kwd))
  1841. (defun org-html-level-close (level max-outline-level)
  1842. "Terminate one level in HTML export."
  1843. (if (<= level max-outline-level)
  1844. (insert "</div>\n")
  1845. (org-close-li)
  1846. (insert "</ul>\n")))
  1847. (provide 'org-html)
  1848. ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1
  1849. ;;; org-html.el ends here