org-html.el 74 KB

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