Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require 'cl-lib)
- (require 'ox-html)
- ;;; User-Configurable Variables
- (defgroup org-export-static-html nil
- "Options specific for Static HTML Export."
- :tag "Org Static HTML"
- :group 'org-export
- :version "27.0.50")
- (defcustom org-static-html-resource-path "~/.emacs.d/data/org"
- "The directory containing offline HTML resources.
- This directory must contain every HTML resource which shall be included
- in the output-file.
- The Exporter fails if a file is missing."
- :group 'org-export-static-html
- :type 'string)
- ;;; Internal Variables
- (defconst org-static-html-themes
- '(("readthedocs" . (("css" . ("htmlize.css"
- "readtheorg.css"))
- ("js" . ("readtheorg.js"
- "jquery.js"
- "bootstrap.js"))))))
- ;;; Internal Functions
- (defun org-static-html-inline-css (css)
- "Puts Tags around the contents of CSS."
- (concat "<style type=\"text/css\">\n"
- "<!--/*--><![CDATA[/*><!--*/\n"
- (with-temp-buffer
- (insert-file-contents css)
- (buffer-string))
- "/*]]>*/-->\n"
- "</style>\n"))
- (defun org-static-html-inline-js (js)
- "Puts Tags around the contents of JS."
- (concat "<script type=\"text/javascript\">\n"
- "<!--/*--><![CDATA[/*><!--*/\n"
- (with-temp-buffer
- (insert-file-contents js)
- (buffer-string))
- "/*]]>*/-->\n"
- "</script>\n"))
- ;;; Define Back-End
- (org-export-define-derived-backend 'static-html 'html
- :menu-entry
- '(?h "Export to HTML"
- ((?s "As Single HTML file" org-static-html-export-to-html)))
- :options-alist
- '((:static-html-theme "THEME" "readthedocs" t))
- :translate-alist '((template . org-static-html-template)))
- (defun org-static-html--build-head (info)
- "Return information for the <head>..</head> of the HTML output.
- INFO is a plist used as a communication channel."
- (progn
- (debug)
- (org-element-normalize-string
- (concat
- (org-element-normalize-string (plist-get info :html-head))
- (org-element-normalize-string (plist-get info :html-head-extra))
- (org-element-normalize-string
- (mapconcat (lambda (css)
- (org-static-html-inline-css
- (concat org-static-html-resource-path "/css/" css)))
- (cdr (assoc "css"
- (assoc
- (plist-get info :static-html-theme)
- org-static-html-themes))) "\n"))))))
- (defun org-static-html--build-js (info)
- "Return all used Javascript as a single string.
- INFO is a plist used as a communication channel."
- (org-element-normalize-string
- (mapconcat (lambda (js)
- (org-static-html-inline-css
- (concat org-static-html-resource-path "/js/" js)))
- (cdr (assoc "js"
- (assoc (plist-get info :static-html-theme)
- org-static-html-themes))) "\n")))
- (defun org-static-html-template (contents info)
- "Return complete document string after HTML conversion.
- CONTENTS is the transcoded contents string. INFO is a plist
- holding export options."
- (concat
- (when (and (not (org-html-html5-p info)) (org-html-xhtml-p info))
- (let* ((xml-declaration (plist-get info :html-xml-declaration))
- (decl (or (and (stringp xml-declaration) xml-declaration)
- (cdr (assoc (plist-get info :html-extension)
- xml-declaration))
- (cdr (assoc "html" xml-declaration))
- "")))
- (when (not (or (not decl) (string= "" decl)))
- (format "%s\n"
- (format decl
- (or (and org-html-coding-system
- (fboundp 'coding-system-get)
- (coding-system-get org-html-coding-system
- 'mime-charset))
- "iso-8859-1"))))))
- (org-html-doctype info)
- "\n"
- (concat "<html"
- (cond ((org-html-xhtml-p info)
- (format
- " xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\""
- (plist-get info :language) (plist-get info :language)))
- ((org-html-html5-p info)
- (format " lang=\"%s\"" (plist-get info :language))))
- ">\n")
- "<head>\n"
- (org-html--build-meta-info info)
- (org-static-html--build-head info)
- (org-html--build-mathjax-config info)
- "</head>\n"
- "<body>\n"
- (let ((link-up (org-trim (plist-get info :html-link-up)))
- (link-home (org-trim (plist-get info :html-link-home))))
- (unless (and (string= link-up "") (string= link-home ""))
- (format (plist-get info :html-home/up-format)
- (or link-up link-home)
- (or link-home link-up))))
- ;; Preamble.
- (org-html--build-pre/postamble 'preamble info)
- ;; Document contents.
- (let ((div (assq 'content (plist-get info :html-divs))))
- (format "<%s id=\"%s\">\n" (nth 1 div) (nth 2 div)))
- ;; Document title.
- (when (plist-get info :with-title)
- (let ((title (and (plist-get info :with-title)
- (plist-get info :title)))
- (subtitle (plist-get info :subtitle))
- (html5-fancy (org-html--html5-fancy-p info)))
- (when title
- (format
- (if html5-fancy
- "<header>\n<h1 class=\"title\">%s</h1>\n%s</header>"
- "<h1 class=\"title\">%s%s</h1>\n")
- (org-export-data title info)
- (if subtitle
- (format
- (if html5-fancy
- "<p class=\"subtitle\">%s</p>\n"
- (concat "\n" (org-html-close-tag "br" nil info) "\n"
- "<span class=\"subtitle\">%s</span>\n"))
- (org-export-data subtitle info))
- "")))))
- contents
- (format "</%s>\n" (nth 1 (assq 'content (plist-get info :html-divs))))
- ;; Postamble.
- (org-html--build-pre/postamble 'postamble info)
- ;; Possibly use the Klipse library live code blocks.
- (when (plist-get info :html-klipsify-src)
- (concat "<script>" (plist-get info :html-klipse-selection-script)
- "</script><script src=\""
- org-html-klipse-js
- "\"></script><link rel=\"stylesheet\" type=\"text/css\" href=\""
- org-html-klipse-css "\"/>"))
- (org-static-html--build-js info)
- ;; Closing document.
- "</body>\n</html>"))
- (defun org-static-html-export-to-html
- (&optional async subtreep visible-only body-only ext-plist)
- "Export current buffer to a single HTML file.
- If narrowing is active in the current buffer, only export its
- narrowed part.
- If a region is active, export that region.
- A non-nil optional argument ASYNC means the process should happen
- asynchronously. The resulting file should be accessible through
- the `org-export-stack' interface.
- When optional argument SUBTREEP is non-nil, export the sub-tree
- at point, extracting information from the headline properties
- first.
- When optional argument VISIBLE-ONLY is non-nil, don't export
- contents of hidden elements.
- When optional argument BODY-ONLY is non-nil, only write code
- between \"<body>\" and \"</body>\" tags.
- EXT-PLIST, when provided, is a property list with external
- parameters overriding
- Return output file's name."
- (interactive)
- (let* ((extension (concat "." (or (plist-get ext-plist :html-extension)
- org-html-extension
- "html")))
- (file (org-export-output-file-name extension subtreep))
- (org-export-coding-system org-html-coding-system))
- (org-export-to-file 'static-html file
- async subtreep visible-only body-only ext-plist)))
- (provide 'ox-static-html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement