Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. (require 'cl-lib)
  2. (require 'ox-html)
  3.  
  4. ;;; User-Configurable Variables
  5. (defgroup org-export-static-html nil
  6. "Options specific for Static HTML Export."
  7. :tag "Org Static HTML"
  8. :group 'org-export
  9. :version "27.0.50")
  10.  
  11. (defcustom org-static-html-resource-path "~/.emacs.d/data/org"
  12. "The directory containing offline HTML resources.
  13.  
  14. This directory must contain every HTML resource which shall be included
  15. in the output-file.
  16.  
  17. The Exporter fails if a file is missing."
  18. :group 'org-export-static-html
  19. :type 'string)
  20.  
  21. ;;; Internal Variables
  22. (defconst org-static-html-themes
  23. '(("readthedocs" . (("css" . ("htmlize.css"
  24. "readtheorg.css"))
  25. ("js" . ("readtheorg.js"
  26. "jquery.js"
  27. "bootstrap.js"))))))
  28.  
  29. ;;; Internal Functions
  30. (defun org-static-html-inline-css (css)
  31. "Puts Tags around the contents of CSS."
  32. (concat "<style type=\"text/css\">\n"
  33. "<!--/*--><![CDATA[/*><!--*/\n"
  34. (with-temp-buffer
  35. (insert-file-contents css)
  36. (buffer-string))
  37. "/*]]>*/-->\n"
  38. "</style>\n"))
  39.  
  40. (defun org-static-html-inline-js (js)
  41. "Puts Tags around the contents of JS."
  42. (concat "<script type=\"text/javascript\">\n"
  43. "<!--/*--><![CDATA[/*><!--*/\n"
  44. (with-temp-buffer
  45. (insert-file-contents js)
  46. (buffer-string))
  47. "/*]]>*/-->\n"
  48. "</script>\n"))
  49.  
  50. ;;; Define Back-End
  51. (org-export-define-derived-backend 'static-html 'html
  52. :menu-entry
  53. '(?h "Export to HTML"
  54. ((?s "As Single HTML file" org-static-html-export-to-html)))
  55. :options-alist
  56. '((:static-html-theme "THEME" "readthedocs" t))
  57. :translate-alist '((template . org-static-html-template)))
  58.  
  59. (defun org-static-html--build-head (info)
  60. "Return information for the <head>..</head> of the HTML output.
  61. INFO is a plist used as a communication channel."
  62. (progn
  63. (debug)
  64.  
  65. (org-element-normalize-string
  66. (concat
  67. (org-element-normalize-string (plist-get info :html-head))
  68. (org-element-normalize-string (plist-get info :html-head-extra))
  69. (org-element-normalize-string
  70. (mapconcat (lambda (css)
  71. (org-static-html-inline-css
  72. (concat org-static-html-resource-path "/css/" css)))
  73. (cdr (assoc "css"
  74. (assoc
  75. (plist-get info :static-html-theme)
  76. org-static-html-themes))) "\n"))))))
  77.  
  78. (defun org-static-html--build-js (info)
  79. "Return all used Javascript as a single string.
  80. INFO is a plist used as a communication channel."
  81. (org-element-normalize-string
  82. (mapconcat (lambda (js)
  83. (org-static-html-inline-css
  84. (concat org-static-html-resource-path "/js/" js)))
  85. (cdr (assoc "js"
  86. (assoc (plist-get info :static-html-theme)
  87. org-static-html-themes))) "\n")))
  88.  
  89. (defun org-static-html-template (contents info)
  90. "Return complete document string after HTML conversion.
  91. CONTENTS is the transcoded contents string. INFO is a plist
  92. holding export options."
  93. (concat
  94. (when (and (not (org-html-html5-p info)) (org-html-xhtml-p info))
  95. (let* ((xml-declaration (plist-get info :html-xml-declaration))
  96. (decl (or (and (stringp xml-declaration) xml-declaration)
  97. (cdr (assoc (plist-get info :html-extension)
  98. xml-declaration))
  99. (cdr (assoc "html" xml-declaration))
  100. "")))
  101. (when (not (or (not decl) (string= "" decl)))
  102. (format "%s\n"
  103. (format decl
  104. (or (and org-html-coding-system
  105. (fboundp 'coding-system-get)
  106. (coding-system-get org-html-coding-system
  107. 'mime-charset))
  108. "iso-8859-1"))))))
  109. (org-html-doctype info)
  110. "\n"
  111. (concat "<html"
  112. (cond ((org-html-xhtml-p info)
  113. (format
  114. " xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\""
  115. (plist-get info :language) (plist-get info :language)))
  116. ((org-html-html5-p info)
  117. (format " lang=\"%s\"" (plist-get info :language))))
  118. ">\n")
  119. "<head>\n"
  120. (org-html--build-meta-info info)
  121. (org-static-html--build-head info)
  122. (org-html--build-mathjax-config info)
  123. "</head>\n"
  124. "<body>\n"
  125. (let ((link-up (org-trim (plist-get info :html-link-up)))
  126. (link-home (org-trim (plist-get info :html-link-home))))
  127. (unless (and (string= link-up "") (string= link-home ""))
  128. (format (plist-get info :html-home/up-format)
  129. (or link-up link-home)
  130. (or link-home link-up))))
  131. ;; Preamble.
  132. (org-html--build-pre/postamble 'preamble info)
  133. ;; Document contents.
  134. (let ((div (assq 'content (plist-get info :html-divs))))
  135. (format "<%s id=\"%s\">\n" (nth 1 div) (nth 2 div)))
  136. ;; Document title.
  137. (when (plist-get info :with-title)
  138. (let ((title (and (plist-get info :with-title)
  139. (plist-get info :title)))
  140. (subtitle (plist-get info :subtitle))
  141. (html5-fancy (org-html--html5-fancy-p info)))
  142. (when title
  143. (format
  144. (if html5-fancy
  145. "<header>\n<h1 class=\"title\">%s</h1>\n%s</header>"
  146. "<h1 class=\"title\">%s%s</h1>\n")
  147. (org-export-data title info)
  148. (if subtitle
  149. (format
  150. (if html5-fancy
  151. "<p class=\"subtitle\">%s</p>\n"
  152. (concat "\n" (org-html-close-tag "br" nil info) "\n"
  153. "<span class=\"subtitle\">%s</span>\n"))
  154. (org-export-data subtitle info))
  155. "")))))
  156. contents
  157. (format "</%s>\n" (nth 1 (assq 'content (plist-get info :html-divs))))
  158. ;; Postamble.
  159. (org-html--build-pre/postamble 'postamble info)
  160. ;; Possibly use the Klipse library live code blocks.
  161. (when (plist-get info :html-klipsify-src)
  162. (concat "<script>" (plist-get info :html-klipse-selection-script)
  163. "</script><script src=\""
  164. org-html-klipse-js
  165. "\"></script><link rel=\"stylesheet\" type=\"text/css\" href=\""
  166. org-html-klipse-css "\"/>"))
  167. (org-static-html--build-js info)
  168. ;; Closing document.
  169. "</body>\n</html>"))
  170.  
  171. (defun org-static-html-export-to-html
  172. (&optional async subtreep visible-only body-only ext-plist)
  173. "Export current buffer to a single HTML file.
  174. If narrowing is active in the current buffer, only export its
  175. narrowed part.
  176. If a region is active, export that region.
  177. A non-nil optional argument ASYNC means the process should happen
  178. asynchronously. The resulting file should be accessible through
  179. the `org-export-stack' interface.
  180. When optional argument SUBTREEP is non-nil, export the sub-tree
  181. at point, extracting information from the headline properties
  182. first.
  183. When optional argument VISIBLE-ONLY is non-nil, don't export
  184. contents of hidden elements.
  185. When optional argument BODY-ONLY is non-nil, only write code
  186. between \"<body>\" and \"</body>\" tags.
  187. EXT-PLIST, when provided, is a property list with external
  188. parameters overriding
  189. Return output file's name."
  190. (interactive)
  191. (let* ((extension (concat "." (or (plist-get ext-plist :html-extension)
  192. org-html-extension
  193. "html")))
  194. (file (org-export-output-file-name extension subtreep))
  195. (org-export-coding-system org-html-coding-system))
  196. (org-export-to-file 'static-html file
  197. async subtreep visible-only body-only ext-plist)))
  198.  
  199. (provide 'ox-static-html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement