publish.el (9379B)
1 ;;; package --- summary Website generator. 2 3 ;;; Copyright (C) Ryan Jeffrey 2021 4 5 ;;; Author: Ryan Jeffrey <ryan@ryanmj.xyz> 6 ;;; Created: 2021-02-12 7 ;;; Keywords: website org 8 ;;; Version: 0.1 9 ;;; Package-Requires: ((emacs "27.1")) 10 ;;; URL: https://gitlab.com/Mallock/site 11 12 ;;; License: 13 14 ;; This file is part of Ryan's Homepage. 15 ;; 16 ;; Ryan's Homepage is free software: you can redistribute it and/or modify 17 ;; it under the terms of the GNU General Public License as published by 18 ;; the Free Software Foundation, either version 3 of the License, or 19 ;; (at your option) any later version. 20 ;; 21 ;; Ryan's Homepage is distributed in the hope that it will be useful, 22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 ;; GNU General Public License for more details. 25 ;; 26 ;; You should have received a copy of the GNU General Public License 27 ;; along with Ryan's Homepage. If not, see <http://www.gnu.org/licenses/>. 28 29 30 ;;; Commentary: 31 32 ;; This script uses org-mode to export my site. 33 34 ;;; Code: 35 36 37 (require 'ox-publish) 38 (require 'ox-html) 39 40 (setq site-dir (concat (getenv "HOME") "/src/site")) 41 (setq export-site "/ssh:root@ryanmj.xyz:/var/www/underground/") 42 43 ;; Get SHA checksum for org-mode js file. 44 (defvar js-sum1 (shell-command-to-string 45 "sha384sum -b scripts/main.js | awk '{ print $1 }' | xxd -r -p | base64 | tr -d '\n'") 46 "SHA checksum for `org-mode' js file.") 47 48 (defun create-preamble (plist) 49 "Insert preamble, PLIST is list of options." 50 (let* ((file-name (file-name-nondirectory (plist-get plist :output-file)))) 51 (cond 52 ;;((string= file-name "index.html") 53 (t 54 (with-temp-buffer 55 (insert-file-contents "views/preamble-i.html") (buffer-string)))))) 56 ;;(t (insert-file-contents "views/preamble-e.html"))))) 57 58 (defun create-postamble (plist) 59 "Insert postamble, PLIST is list of options." 60 (let* ((file-name (file-name-nondirectory (plist-get plist :output-file)))) 61 (cond 62 ;;((string= file-name "index.html") 63 (t 64 (with-temp-buffer 65 (insert-file-contents "views/postamble-i.html") (buffer-string)))))) 66 ;;(t (insert-file-contents "views/postamble-e.html"))))) 67 68 ;; Replace __PROMPT__ with the actual prompt 69 (add-hook 'org-export-before-parsing-hook #'(lambda (backend) 70 (goto-char (point-min)) 71 (while (search-forward "__PROMPT__" (point-max) t) 72 (kill-backward-chars (length "__PROMPT__")) 73 (insert "@@html:<span class=\"prompt1\">ryan</span><span class=\"prompt2\">@</span><span class=\"prompt3\">themainframe</span><span class=\"prompt4\"></span>@@")))) 74 75 76 (defun index-sitemap-entry (entry _style project) 77 "Create ls-like output on file ENTRY with style _STYLE and from project PROJECT." 78 (if (string= entry "index.org") 79 "" 80 (concat 81 "@@html:<p>" 82 (cond ; Get the prefix if the ls -l output. 83 ((file-symlink-p entry) "lrwxrwxrwx 1") 84 ((file-directory-p entry) "drwxr-xr-x 2") 85 (t "-rw-r--r-- 1")) 86 " ryan ryan " 87 (format "%4s " 88 (shell-command-to-string 89 (concat "find " entry " -name '*.org' -exec cat {} + | wc -c | numfmt --to=si | tr -d '\n'"))) 90 (shell-command-to-string (concat "ls -dl '--time-style=+%b %m %Y' " 91 entry 92 " | awk '{printf \"%s %2d %s \", $6, $7, $8} '" )) 93 "@@" 94 (format "[[file:%s]]" entry) 95 "@@html:</p>@@"))) 96 97 98 99 (defun create-index-blogmap (title list) 100 "Create the sitemap for the posts/ directory. 101 Return sitemap using TITLE and LIST returned by `create-blogmap-entry'." 102 (concat "#+TITLE: " title "\n\n" 103 (mapconcat (lambda (li) 104 (format "%s" (car li))) 105 (seq-filter #'car (cdr list)) 106 "\n"))) 107 108 ;; Replace <!--LS HERE--> with ls output. 109 (add-hook 'org-export-before-parsing-hook #'(lambda (backend) 110 "Create fake ls listing." 111 (goto-char (point-min)) 112 (while (search-forward "<!--LS HERE-->" (point-max) t) 113 (kill-whole-line) 114 (insert (concat 115 "@@html:<p>total " 116 (shell-command-to-string 117 "find . -name '*.org' -exec cat {} + | wc -c | numfmt --to=si | tr -d '\n'") 118 " Words</p>@@\n"))))) 119 120 ;; Replace __SHASUM__ with the sum of the file. 121 (add-hook 'org-export-before-parsing-hook #'(lambda (backend) 122 "Create fake ls listing." 123 (goto-char (point-min)) 124 (while (search-forward "__SHASUM__" (point-max) t) 125 (kill-backward-chars (length "__SHASUM__")) 126 (insert-before-markers js-sum1)))) 127 128 129 (defun create-blogmap-entry (entry _style project) 130 "Create an entry for the blogmap. 131 One string for each ENTRY in PROJECT." 132 (if (string= entry "index.org") 133 "" 134 (format "@@html:<p>-rw-r--r-- 1 ryan ryan @@ %4s [[file:%s][%s]] @@html:</p>@@" 135 (shell-command-to-string (format "wc -c < %s | numfmt --to=si | tr -d '\n'" (org-publish--expand-file-name entry project))) 136 ;(format-time-string "%h %d, %Y" 137 ; (org-publish-find-date entry project)) 138 entry 139 (org-publish-find-title entry project)))) 140 141 (defun create-blogmap (title list) 142 "Create the sitemap for the posts/ directory. 143 Return sitemap using TITLE and LIST returned by `create-blogmap-entry'." 144 (concat "#+TITLE: " title "\n\n" 145 "\n#+begin_archive\n" 146 (mapconcat (lambda (li) 147 (format "%s" (car li))) 148 (seq-filter #'car (cdr list)) 149 "") 150 "\n#+end_archive\n")) 151 152 (defun force-main-publish () 153 "Force evaluation of main project." 154 (org-publish "main" t) 155 (org-publish "posts" t) 156 (org-publish-all)) 157 158 ;; Sets up exporting defaults for org mode. 159 ;; "posts" are blog posts. 160 ;; "main" is for index files like index.html, blog.html, etc. 161 (setq org-publish-project-alist 162 '(("main" 163 :base-directory "" 164 :base-extension "org" 165 :publishing-directory "public" 166 :publishing-function org-html-publish-to-html 167 :html-preamble create-preamble 168 :html-postamble create-postamble 169 :auto-sitemap t 170 :sitemap-filename "sitemap.org" 171 :sitemap-title nil 172 :sitemap-style list 173 :sitemap-sort-files anti-chronologically 174 :sitemap-format-entry index-sitemap-entry 175 :sitemap-function create-index-blogmap 176 :sitemap-sort-folders first 177 ;;:html-link-up "/" 178 ;;:html-link-home "/" 179 :recursive nil) 180 ("posts" 181 :base-directory "posts" 182 :base-extension "org" 183 :publishing-directory "public/posts" 184 :recursive t 185 :publishing-function org-html-publish-to-html 186 187 :html-preamble create-preamble 188 :html-postamble create-postamble 189 190 ;; Sitemap. 191 192 :auto-sitemap t 193 :sitemap-filename "sitemap.org" 194 :sitemap-title "Blog Map" 195 :sitemap-style list 196 :sitemap-sort-files anti-chronologically 197 :sitemap-format-entry create-blogmap-entry 198 :sitemap-function create-blogmap 199 ) 200 ("misc" 201 :base-directory "misc" 202 :base-extension "org" 203 :publishing-directory "public/misc" 204 :recursive t 205 :publishing-function org-html-publish-to-html 206 :html-preamble create-preamble 207 :html-postamble create-postamble 208 :auto-sitemap nil) 209 ("css" 210 :base-directory "css/" 211 :base-extension "css" 212 :publishing-directory "public/css" 213 :publishing-function org-publish-attachment 214 :recursive nil) 215 ("res" 216 :base-directory "res/" 217 :publishing-directory "public/res" 218 :base-extension "png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|otf\\|ttf\\|ico" 219 :recursive t 220 :publishing-function org-publish-attachment) 221 ("files" 222 :base-directory "files/" 223 :publishing-directory "public/files" 224 :base-extension "html\\|txt\\|org" 225 :recursive t 226 :publishing-function org-publish-attachment 227 :htmlized-source nil 228 :html-preamble nil 229 :html-postamble nil) 230 ("scripts" 231 :base-directory "scripts/" 232 :publishing-directory "public/scripts" 233 :base-extension "js" 234 :recursive t 235 :publishing-function org-publish-attachment) 236 ("all" :components ("posts" "css" "main" "res" "files" "scripts")))) 237 238 (provide 'publish) 239 ;;; publish.el ends here