org-html.el 69 KB

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