ezup.dev

Source Code of Dash Eclipse's Personal Site (ezup.dev)
git clone git://ezup.dev/ezup.dev.git
Log | Files | Refs | README | LICENSE

publish.el (9287B)


      1;;; publish.el
      2
      3(require 'package)
      4(package-initialize)
      5;(unless package-archive-contents
      6  ;(add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/") t)
      7  ;(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
      8  ;(package-refresh-contents))
      9;(dolist (pkg '(org-plus-contrib htmlize))
     10  ;(unless (package-installed-p pkg)
     11    ;(package-install pkg)))
     12
     13(add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/") t)
     14(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
     15(package-refresh-contents)
     16(package-install 'htmlize)
     17(package-install 'org-plus-contrib)
     18(package-install 'ox-reveal)
     19
     20(require 'org)
     21(require 'ox-publish)
     22(require 'ox-reveal)
     23
     24
     25(defvar ezup-url "https://ezup.dev/blog/")
     26(defvar ezup-title "Dash Eclipse's Blog | ezup.dev")
     27
     28(setq org-html-postamble nil)
     29      ;;org-export-with-author nil)
     30      ;;org-export-with-toc nil)
     31
     32(setq org-html-divs '((preamble "header" "top")
     33                      (content "main" "content"))
     34      org-html-container-element "section"
     35      org-html-metadata-timestamp-format "%Y-%m-%d"
     36      org-html-checkbox-type 'html
     37      org-html-html5-fancy t
     38      org-html-validation-link t
     39      org-html-doctype "html5"
     40      org-html-htmlize-output-type 'css
     41      org-src-fontify-natively t)
     42
     43(defvar ezup-html-head
     44  "<link rel='icon' type='image/x-icon' href='/favicon.svg'/>
     45<meta name='viewport' content='width=device-width, initial-scale=1'>
     46<link rel='stylesheet' href='/styles/topnav.css' type='text/css'/>
     47<link rel='stylesheet' href='/styles/site.css' type='text/css'/>
     48<link rel='stylesheet' href='/styles/syntax-coloring.css' type='text/css'/>
     49<link rel='alternate' type='application/rss+xml' title='RSS' href='/blog/rss.xml'>")
     50
     51(defun ezup/prepostamble-format (type)
     52  "Return the content for the preamble/postamble of TYPE."
     53  `(("en" ,(with-temp-buffer
     54             (insert-file-contents (expand-file-name (format "%s.html" type) "layouts"))
     55             (buffer-string)))))
     56
     57;; org-html-publish-to-html
     58;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/lisp/ox-html.el#L3868-L3880
     59(defun ezup/org-html-publish-to-html (plist filename pub-dir)
     60  "Wrapper function to publish an file to html.
     61
     62PLIST contains the properties, FILENAME the source file and
     63  PUB-DIR the output directory."
     64  (let ((project (cons 'ezup plist)))
     65    (plist-put plist :subtitle
     66               (format "Published on %s by %s"
     67                       (format-time-string "%Y-%m-%d" (org-publish-find-date filename project))
     68                       (plist-get plist :author)))
     69    (org-html-publish-to-html plist filename pub-dir)))
     70
     71
     72;; org-publish-sitemap-default
     73;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/lisp/ox-publish.el#L911-L917
     74(defun ezup/org-publish-sitemap (title list)
     75  "Generate sitemap as a string, having TITLE.
     76LIST is an internal representation for the files to include, as
     77returned by `org-list-to-lisp'."
     78  ;;(let ((filtered-list (cl-remove-if (lambda (x)
     79                                       ;;(and (sequencep x) (null (car x))))
     80                                     ;;list)))
     81    (concat "#+TITLE: " title "\n"
     82            ;"#+OPTIONS: title:nil\n"
     83            "#+META_TYPE: website\n"
     84            "#+DESCRIPTION: Dash Eclipse's Personal Blog\n"
     85            ;;"\n#+ATTR_HTML: :class sitemap\n"
     86            ; TODO use org-list-to-subtree instead
     87            (org-list-to-org list)))
     88
     89
     90;; org-publish-sitemap-default-entry
     91;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/lisp/ox-publish.el#L898-L909
     92(defun ezup/org-publish-sitemap-entry (entry style project)
     93  "Default format for site map ENTRY, as a string.
     94ENTRY is a file name.  STYLE is the style of the sitemap.
     95PROJECT is the current project."
     96  (cond ((not (directory-name-p entry))
     97         (format "[[file:%s][%s]]
     98                 #+HTML: <p class='pubdate'>by %s on %s.</p>"
     99                 entry
    100                 (org-publish-find-title entry project)
    101                 (car (org-publish-find-property entry :author project))
    102                 (format-time-string "%b %d, %Y"
    103                   (org-publish-find-date entry project))))
    104        ((eq style 'tree)
    105         ;; Return only last subdir.
    106         (file-name-nondirectory (directory-file-name entry)))
    107        (t entry)))
    108
    109;; org-publish-sitemap-default
    110;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/lisp/ox-publish.el#L911-L917
    111(defun ezup/org-publish-sitemap-rss (title list)
    112  "Default site map, as a string.
    113TITLE is the the title of the site map.  LIST is an internal
    114representation for the files to include, as returned by
    115`org-list-to-lisp'.  PROJECT is the current project."
    116  (concat "#+TITLE: " title "\n\n"
    117          (org-list-to-subtree list 1 '(:icount "" :istart ""))))
    118
    119;; org-rss-publish-to-rss
    120;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/contrib/lisp/ox-rss.el#L204-L223
    121(defun ezup/org-rss-publish-to-rss (plist filename pub-dir)
    122  "Publish rss.org to RSS."
    123  (if (equal "rss.org" (file-name-nondirectory filename))
    124    (org-rss-publish-to-rss plist filename pub-dir)))
    125
    126;; org-publish-sitemap-default-entry
    127;; https://code.orgmode.org/bzg/org-mode/src/release_9.3.7/lisp/ox-publish.el#L898-L909
    128(defun ezup/format-rss-feed-entry (entry style project)
    129  "Format ENTRY for the RSS feed.
    130ENTRY is a file name.  STYLE is either 'list' or 'tree'.
    131PROJECT is the current project."
    132  (cond ((not (directory-name-p entry))
    133         (let* ((file (org-publish--expand-file-name entry project))
    134                (title (org-publish-find-title entry project))
    135                (date (format-time-string "%Y-%m-%d" (org-publish-find-date entry project)))
    136                (link (concat (file-name-sans-extension entry) ".html")))
    137           (with-temp-buffer
    138             (insert (format "* %s\n" title))
    139             (org-set-property "RSS_PERMALINK" link)
    140             (org-set-property "PUBDATE" date)
    141	     ;;(insert-file-contents file)
    142             (buffer-string))))
    143        ((eq style 'tree)
    144         ;; Return only last subdir.
    145         (file-name-nondirectory (directory-file-name entry)))
    146        (t entry)))
    147
    148;; https://orgmode.org/manual/Complex-example.html#Complex-example
    149;; https://orgmode.org/manual/Site-map.html
    150(setq org-publish-project-alist
    151      `(("posts"
    152         :base-directory "posts"
    153         :recursive t
    154         ;;:section-numbers nil
    155         ;;:with-toc nil
    156         :base-extension "org"
    157         :exclude "rss.org\\|index.org"
    158         ;:exclude ,(regexp-opt '("rss.org" "index.org"))
    159         :publishing-function ezup/org-html-publish-to-html
    160         :publishing-directory ".web/blog"
    161         :auto-sitemap t
    162         :sitemap-filename "index.org"
    163         :sitemap-title "Blog Index"
    164         :sitemap-style list
    165         :sitemap-sort-files anti-chronologically
    166         :sitemap-function ezup/org-publish-sitemap
    167         :sitemap-format-entry ezup/org-publish-sitemap-entry
    168         :author "Dash Eclipse"
    169         :html-head-include-scripts nil
    170         :html-head-include-default-style nil
    171         :html-head ,ezup-html-head
    172         :html-preamble-format ,(ezup/prepostamble-format "preamble"))
    173        ("rss"
    174         :base-directory "posts"
    175         :base-extension "org"
    176         :recursive nil
    177         ;:exclude ,(regexp-opt '("rss.org" "index.org"))
    178         :exclude "rss.org\\|index.org"
    179         :publishing-directory ".web/blog"
    180         :org-rss-use-entry-url-as-guid t
    181         ;;:publishing-function org-rss-publish-to-rss
    182         :publishing-function ezup/org-rss-publish-to-rss
    183         :rss-extension "xml"
    184         :html-link-home ,ezup-url
    185         :html-link-use-abs-url t
    186         :html-link-org-files-as-html t
    187         :auto-sitemap t
    188         ;;:exclude ".*"
    189         ;;:include ("index.org")
    190         :sitemap-filename "rss.org"
    191         :sitemap-title ,ezup-title
    192         :sitemap-style list
    193         :sitemap-sort-files anti-chronologically
    194         :sitemap-function ezup/org-publish-sitemap-rss
    195         :sitemap-format-entry ezup/format-rss-feed-entry
    196         :with-author t
    197         :author "Dash Eclipse"
    198         :email "dash@ezup.dev")
    199        ("images"
    200         :base-directory "images"
    201         :base-extension "svg\\|jpg\\|png"
    202         :publishing-directory ".web/images"
    203         :publishing-function org-publish-attachment)
    204        ("favicon"
    205         :base-directory "."
    206         :base-extension "svg\\|ico"
    207         :publishing-directory ".web"
    208         :publishing-function org-publish-attachment)
    209        ("css"
    210         :base-directory "styles"
    211         :base-extension "css"
    212         :publishing-directory ".web/styles"
    213         :publishing-function org-publish-attachment)
    214        ("fonts"
    215         :base-directory "fonts"
    216         :base-extension "woff2"
    217         :publishing-directory ".web/fonts"
    218         :publishing-function org-publish-attachment)
    219        ("html"
    220         :base-directory "."
    221         :base-extension "html"
    222         :publishing-directory ".web"
    223         :publishing-function org-publish-attachment)
    224	("keys"
    225	 :base-directory "keys"
    226	 :base-extension "asc"
    227	 :publishing-directory ".web/keys"
    228	 :publishing-function org-publish-attachment)
    229        ("website" :components ("posts" "rss" "images" "css" "fonts" "html" "keys"))))
    230
    231(provide 'publish)
    232