her.esy.fun/project.el

448 lines
17 KiB
EmacsLisp
Raw Normal View History

2019-08-23 20:03:53 +00:00
;; sign it with
;; gpg --local-user yann@esposito.host --output project.el.sig --detach-sign project.el
2020-04-25 10:11:07 +00:00
(defvar websiteorigin "https://her.esy.fun")
2019-10-20 19:56:27 +00:00
(defvar root-dir (projectile-project-root))
(defvar base-dir (concat root-dir "src"))
(defvar publish-dir (concat root-dir "_site"))
2020-02-22 17:11:49 +00:00
(defvar draft-publish-dir (concat root-dir "_full"))
2019-08-31 12:21:03 +00:00
(defvar assets-dir (concat base-dir "/"))
(defvar publish-assets-dir (concat publish-dir "/"))
2020-02-22 20:24:27 +00:00
(defvar draft-publish-assets-dir (concat draft-publish-dir "/"))
2019-08-31 12:21:03 +00:00
(defvar posts-dir (concat base-dir "/posts"))
(defvar rss-title "Subscribe to articles")
(defvar css-path "/css/y.css")
2019-08-31 12:21:03 +00:00
(defvar author-name "Yann Esposito")
(defvar author-email "yann@esposito.host")
2019-08-20 13:35:54 +00:00
(require 'org)
(require 'ox-publish)
(require 'ox-html)
(require 'org-element)
2019-08-31 22:04:23 +00:00
;; (setq org-link-file-path-type 'relative)
2019-08-20 13:35:54 +00:00
(setq org-publish-timestamp-directory
(concat (projectile-project-root) "_cache/"))
(defvar org-blog-head
(concat
2019-12-05 13:55:51 +00:00
"<link rel=\"stylesheet\" href=\"" css-path "\"/>"
"<link rel=\"alternate\" type=\"application/rss+xml\" href=\"/rss.xml\" />"
"<link rel=\"icon\" href=\"/favicon.ico\">"))
2019-08-20 13:35:54 +00:00
(defun menu (lst)
"Blog menu"
(concat
2019-11-19 03:56:12 +00:00
"<nav>"
2019-08-20 13:35:54 +00:00
(mapconcat 'identity
(append
'("<a href=\"/index.html\">Home</a>"
2019-09-01 06:53:11 +00:00
"<a href=\"/archive.html\">Posts</a>"
2019-08-23 20:03:53 +00:00
"<a href=\"/slides.html\">Slides</a>"
2020-02-21 10:27:51 +00:00
"<a href=\"/about-me.html\">About</a>
<span class=\"details\">
(<a href=\"https://gitea.esy.fun/yogsototh\">code</a>
<a href=\"https://espial.esy.fun/u:yogsototh\">bookmarks</a>
<a href=\"https://espial.esy.fun/u:yogsototh/notes\">notes</a>)</span>")
2019-08-20 13:35:54 +00:00
lst)
" | ")
2019-11-19 03:56:12 +00:00
"</nav>"))
2019-08-20 13:35:54 +00:00
(defun get-from-info (info k)
(let ((i (car (plist-get info k))))
(when (and i (stringp i))
i)))
2020-05-01 08:27:39 +00:00
(defun logo ()
(concat
"<div id=\"logo\">"
2020-05-02 13:30:40 +00:00
"<a href=\"/\">"
2020-05-01 08:27:39 +00:00
"<svg width=\"5em\" viewBox=\"0 0 64 64\">"
"<circle cx=\"32\" cy=\"32\" r=\"30\" stroke=\"var(--b2)\" stroke-width=\"2\" fill=\"var(--b03)\"/>"
"<circle cx=\"32\" cy=\"32\" r=\"12\" stroke=\"var(--r)\" stroke-width=\"2\" fill=\"var(--o)\"/>"
"<circle cx=\"32\" cy=\"32\" r=\"6\" stroke-width=\"0\" fill=\"var(--y)\"/>"
"<ellipse cx=\"32\" cy=\"14\" rx=\"14\" ry=\"8\" stroke-width=\"0\" fill=\"var(--b3)\"/>"
"</svg>"
2020-05-02 13:30:40 +00:00
"</a>"
2020-05-01 08:27:39 +00:00
"</div>"))
(defun relative-link (output-file)
"Given an output-file generate a link relative to the URL origin."
(replace-regexp-in-string ".*/_site" "" output-file))
2020-05-01 17:26:07 +00:00
(defun gen-permalink (output-file)
"Given the output-file generate a permalink"
(format "%s%s"
websiteorigin
(relative-link output-file)))
2020-05-01 17:26:07 +00:00
(defun gen-org-src (permalink)
"Given a permalink generate the path to the asssociated .org source file"
(replace-regexp-in-string "\.html$" ".org" permalink))
2019-08-20 13:35:54 +00:00
(defun org-blog-preamble (info)
"Pre-amble for whole blog."
(concat
2020-05-01 08:27:39 +00:00
(logo)
2019-08-20 13:35:54 +00:00
"<div class=\"content\">"
2019-11-10 21:15:00 +00:00
"<h1>"
(format "%s" (car (plist-get info :title)))
"</h1>"
2019-08-20 13:35:54 +00:00
(when-let ((subtitle (car (plist-get info :subtitle))))
(format "<h2>%s</h2>" subtitle))
2020-04-25 10:11:07 +00:00
(when-let ((date (plist-get info :date)))
(concat
"<div class=\"author\">"
"Yann Esposito"
"<br/>"
2020-04-25 10:11:07 +00:00
(format "<span class=\"article-date\">%s</span>"
(format-time-string "%Y-%m-%d"
(org-timestamp-to-time
(car date))))
"<br/>on "
2020-04-25 10:11:07 +00:00
(format " <a href=\"%s\">Yann Esposito's blog</a>" websiteorigin)
2020-05-01 17:26:07 +00:00
(let ((permalink (gen-permalink (plist-get info :output-file))))
2020-04-25 10:41:37 +00:00
(concat
" - "
2020-05-01 17:26:07 +00:00
(let ((orgfile (gen-org-src permalink)))
2020-04-25 10:41:37 +00:00
(format " <a href=\"%s\">source</a>" orgfile))
" - "
(format " <a class=\"permalink\" href=\"%s\">§permalink</a>" permalink)))
2020-04-25 10:11:07 +00:00
"</div>"))
2020-05-02 11:37:25 +00:00
(when-let ((description (plist-get info :description)))
(format "<div class=\"abstract\">%s</div>" description))
2019-08-20 13:35:54 +00:00
"</div>"))
(defun rand-obfs (c)
(let ((r (% (random) 20)))
(cond ;; ((eq 0 r) (format "%c" c))
((<= 0 r 10) (format "&#%d;" c))
(t (format "&#x%X;" c)))))
(defun obfuscate-html (txt)
(apply 'concat
(mapcar 'rand-obfs txt)))
2020-05-01 13:25:59 +00:00
(defun keywords-to-html (keywords)
(let ((keywords (split-string keywords ",\s*")))
(format " <span class=\"keywords\">%s</span>"
(mapconcat (lambda (k)
2020-05-01 17:26:07 +00:00
(format "<code>#%s</code>" k))
2020-05-01 13:25:59 +00:00
(cl-sort keywords 'string-lessp :key 'downcase)
" "))))
2019-08-20 13:35:54 +00:00
(defun org-blog-postamble (info)
"Post-amble for whole blog."
(concat
"<div class=\"content\">"
;; TODO install a comment system
2020-04-25 10:11:07 +00:00
;; (let ((url (format "%s%s" websiteorigin (replace-regexp-in-string base-dir "" (plist-get info :input-file)))))
2019-08-20 13:35:54 +00:00
;; (format "<a href=\"https://comments.esy.fun/slug/%s\">comment</a>"
;; (url-hexify-string url)))
"<footer>"
2020-05-01 13:25:59 +00:00
(let* ((author (when-let ((author (get-from-info info :author)))
(if-let ((email (plist-get info :email)))
(let* ((obfs-email (obfuscate-html email))
(obfs-author (obfuscate-html author))
(obfs-title (obfuscate-html (get-from-info info :title)))
(full-email (format "%s &lt;%s&gt;" obfs-author obfs-email)))
(format "<span class=\"author\"><a href=\"%s%s%s%s\">%s</a></span>"
(obfuscate-html "mailto:")
full-email
(obfuscate-html "?subject=yblog: ")
obfs-title
full-email))
(format "<span class=\"author\">%s</span>" author))))
(date (when-let ((date (get-from-info info :date)))
(format "<div class=\"date\">Created: %s (%s)</div>" date (y-date date))))
(keywords (when-let ((keywords (plist-get info :keywords)))
(keywords-to-html keywords)))
(rss
(concat
"<div class=\"rss\">"
"<a rel=\"alternate\""
" type=\"application/rss+xml\""
" href=\"/rss.xml\">"
"RSS"
"</a>"
" (<a href=\"https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fher.esy.fun%2Frss.xml\">"
2020-05-01 17:26:07 +00:00
"validate</a>)"
2020-05-01 13:25:59 +00:00
"</div>"))
(generated-date
(format "<div class=\"date\">%s</div>"
(format-time-string "%Y-%m-%d %H:%M:%S")))
(size
2020-05-03 19:50:56 +00:00
"<div class=\"web-file-size\">XXK (html XXK, css XXK, img XXK)</div>")
(gzsize
"<div class=\"gzweb-file-size\">XXK (html XXK, css XXK, img XXK)</div>")
2020-05-01 13:25:59 +00:00
(generated-with
(format (concat "<div class=\"creator\">"
"<a href=\"https://www.gnu.org/software/emacs/\" target=\"_blank\" rel=\"noopener noreferrer\">Emacs %s</a>, "
"<a href=\"http://spacemacs.org\" target=\"_blank\" rel=\"noopener noreferrer\">Spacemacs %s</a>, "
"<a href=\"http://orgmode.org\" target=\"_blank\" rel=\"noopener noreferrer\">Org Mode %s</a>"
"</div>")
2020-05-01 17:26:07 +00:00
emacs-version spacemacs-version org-version))
(website-code
"<a href=\"https://gitea.esy.fun/yogsototh/her.esy.fun\" target=\"_blank\" rel=\"noopener noreferrer\">Website source code</a>")
(org-src (gen-org-src (relative-link (plist-get info :output-file))))
2020-05-01 17:26:07 +00:00
(org-src-link (format "<a href=\"%s\">%s</a>" org-src org-src)))
2020-05-01 13:25:59 +00:00
(concat
"<i>Any comment? Click on my email below and I'll add it.</i>"
2020-05-01 13:25:59 +00:00
"<table>"
(mapconcat (lambda (entry)
(when (cdr entry)
(format "<tr><td>%s</td><td>%s</td></tr>"
(car entry) (cdr entry))))
2020-05-01 17:26:07 +00:00
`(("author" . ,author)
("date" . ,date)
("tags" . ,keywords)
("rss" . ,rss)
("size" . ,size)
2020-05-03 19:50:56 +00:00
("gz" . ,gzsize)
2020-05-09 12:40:59 +00:00
("generated" . ,generated-date)
("gen-with" . ,generated-with)
("src" . ,website-code)
2020-05-01 17:26:07 +00:00
("org-file" . ,org-src-link))
2020-05-01 13:25:59 +00:00
" ")
"</table>"))
2019-08-20 13:35:54 +00:00
"</footer>"
(menu '("<a href=\"#preamble\">↑ Top ↑</a>"))
"</div>"))
2019-08-31 22:04:23 +00:00
(defun date-format-entry (entry _style project)
"Return string for each ENTRY in PROJECT."
2019-09-01 06:53:11 +00:00
(when (string-match "posts/.*" entry)
(let* ((file (org-publish--expand-file-name entry project))
(title (org-publish-find-title entry project))
2020-05-01 13:25:59 +00:00
(artdate (format-time-string "%Y-%m-%d" (org-publish-find-date entry project)))
2020-05-02 13:30:40 +00:00
(description (org-publish-find-property entry :description project 'html)))
2019-09-16 09:42:49 +00:00
(concat
2020-05-01 13:25:59 +00:00
(format " @@html:<span class=\"metas\">%s</span>@@: " artdate)
2019-09-16 09:42:49 +00:00
(format " *[[file:%s][%s]]*" file title)
2020-05-02 13:30:40 +00:00
(format " @@html:<div class=\"description\">%s</div>@@" description)
2020-05-01 13:25:59 +00:00
(format " @@html:<div class=\"metas\">@@ ")
" @@html:</div>@@\n"))))
2019-08-20 13:35:54 +00:00
2020-05-02 13:30:40 +00:00
(defun org-blog-sitemap-fn-descr (_descr title list)
2019-08-20 13:35:54 +00:00
"Return sitemap using TITLE and LIST returned by `org-blog-sitemap-format-entry'."
(concat "#+TITLE: " title "\n"
"#+AUTHOR: " author-name "\n"
"#+EMAIL: " author-email "\n"
2020-04-18 08:48:44 +00:00
(concat "@@html:" (menu '()) "@@")
2019-11-27 18:06:42 +00:00
"\n\n"
2019-09-01 06:53:11 +00:00
(mapconcat (lambda (li) (format "%s" (car li)))
2019-08-20 13:35:54 +00:00
(seq-filter #'car (cdr list))
2019-08-31 12:21:03 +00:00
"\n")))
2019-08-20 13:35:54 +00:00
2019-09-16 09:42:49 +00:00
(defun org-blog-prepare (project-plist)
"With help from `https://github.com/howardabrams/dot-files'.
Touch `archive.org' to rebuilt it.
Argument `PROJECT-PLIST' contains information about the current project."
(let* ((base-directory (plist-get project-plist :base-directory))
(buffer (find-file-noselect (expand-file-name "archive.org" base-directory) t)))
(with-current-buffer buffer
(set-buffer-modified-p t)
(save-buffer 0))
(kill-buffer buffer)))
2019-08-20 13:35:54 +00:00
(defun org-blog-publish-to-html (plist filename pub-dir)
"Same as `org-html-publish-to-html' but modifies html before finishing."
2019-11-22 03:23:04 +00:00
(let* ((file-path (org-html-publish-to-html plist filename pub-dir))
2020-05-02 11:37:25 +00:00
(mk-path (format "./%s.html"
(replace-regexp-in-string ".*/\\([^/]*\\)\\.org$"
"\\1"
filename))))
2019-08-20 13:35:54 +00:00
(with-current-buffer (find-file-noselect file-path)
(goto-char (point-min))
(search-forward "<body>")
2020-05-11 20:51:19 +00:00
(insert (mapconcat 'identity
`("<input name=\"t\" type=\"radio\" id=\"l\">"
"<input name=\"t\" type=\"radio\" id=\"d\">"
"<div id=\"labels\">"
"<div class=\"content\">"
"<label for=\"l\">light</label>"
"|"
"<label for=\"d\">dark</label>"
"</div>"
"</div>"
"<div class=\"main\">")
"\n"))
2019-09-01 21:27:15 +00:00
(goto-char (point-max))
(search-backward "</body>")
(insert "\n</div>\n")
2019-08-20 13:35:54 +00:00
(save-buffer)
(kill-buffer))
file-path))
2020-05-03 12:15:35 +00:00
(defun compress-image (filename dst-file)
"Compress images using imagemagick"
(shell-command
(format
(concat
"~/.nix-profile/bin/convert"
" %s" ;; source
" -sampling-factor 4:2:0"
" -strip"
2020-05-25 20:28:06 +00:00
" -resize 300x300\\>"
2020-05-03 12:32:00 +00:00
" -interlace Plane"
2020-05-03 12:15:35 +00:00
" -quality 85"
2020-05-24 23:30:48 +00:00
" -define filter:blur=0.75"
" -filter Gaussian"
2020-05-09 12:40:59 +00:00
" -ordered-dither o4x4,4"
2020-05-03 12:32:00 +00:00
" %s" ;; dest
2020-05-03 12:15:35 +00:00
)
filename
dst-file)))
(defun compress-css (root-dir filename dst-file)
"Compress CSS usin compresscss.sh script"
(shell-command
(format "%s/engine/compresscss.sh %s %s" root-dir filename dst-file)))
2019-08-20 13:35:54 +00:00
(defun org-blog-publish-attachment (plist filename pub-dir)
"Publish a file with no transformation of any kind.
FILENAME is the filename of the Org file to be published. PLIST
is the property list for the given project. PUB-DIR is the
publishing directory.
Take care of minimizing the pictures using imagemagick.
Return output file name."
(unless (file-directory-p pub-dir)
(make-directory pub-dir t))
(or (equal (expand-file-name (file-name-directory filename))
(file-name-as-directory (expand-file-name pub-dir)))
(let ((dst-file (expand-file-name (file-name-nondirectory filename) pub-dir)))
2020-05-24 23:30:48 +00:00
(cond ((string-match-p ".*\\.\\(png\\|jpg\\|jpeg\\|gif\\)$" filename)
2020-05-03 12:15:35 +00:00
(compress-image filename dst-file))
2019-10-20 19:56:27 +00:00
((string-match-p ".*\\.css$" filename)
2020-05-03 12:15:35 +00:00
(compress-css root-dir filename dst-file))
2019-10-20 19:56:27 +00:00
(t (copy-file filename dst-file t))))))
2019-08-20 13:35:54 +00:00
2019-08-31 12:21:03 +00:00
(defalias 'org-blog-posts-sitemap-fn
2020-05-02 13:30:40 +00:00
(apply-partially 'org-blog-sitemap-fn-descr ""))
2019-08-31 12:21:03 +00:00
2019-11-04 08:03:38 +00:00
(setq org-html-htmlize-output-type 'css)
(setq org-html-htmlize-font-prefix "org-")
2019-08-20 13:35:54 +00:00
(setq org-publish-project-alist
`(("orgfiles"
:base-directory ,base-dir
2019-09-30 13:10:39 +00:00
:exclude ".*drafts/.*"
2019-08-20 13:35:54 +00:00
:base-extension "org"
:publishing-directory ,publish-dir
:recursive t
2019-09-16 09:42:49 +00:00
:preparation-function org-blog-prepare
2019-08-20 13:35:54 +00:00
:publishing-function org-blog-publish-to-html
:with-toc nil
:with-title nil
:with-date t
:section-numbers nil
:html-doctype "html5"
:html-html5-fancy t
:html-head-include-default-style nil
:html-head-include-scripts nil
:htmlized-source t
:html-head-extra ,org-blog-head
:html-preamble org-blog-preamble
:html-postamble org-blog-postamble
2019-08-31 13:45:32 +00:00
:auto-sitemap t
2019-09-01 06:53:11 +00:00
:sitemap-filename "archive.org"
2019-08-31 13:45:32 +00:00
:sitemap-title "Articles"
:sitemap-style list
2019-08-31 22:04:23 +00:00
:sitemap-sort-files anti-chronologically
:sitemap-format-entry date-format-entry
:sitemap-function org-blog-posts-sitemap-fn)
2019-08-31 13:45:32 +00:00
2020-02-22 20:52:52 +00:00
("css"
:base-directory ,assets-dir
:base-extension "css"
:publishing-directory ,publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
2020-02-22 17:11:49 +00:00
2020-02-22 20:52:52 +00:00
("img"
2019-08-20 13:35:54 +00:00
:base-directory ,assets-dir
2020-02-22 20:52:52 +00:00
:base-extension "(jpg|png|gif|jpeg)"
2019-08-20 13:35:54 +00:00
:publishing-directory ,publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
2020-02-22 20:52:52 +00:00
("files"
:base-directory ,assets-dir
:base-extension ".*"
:exclude ".*\.(org|css|jpg|png|gif|jpeg)$"
:publishing-directory ,publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
2020-02-22 17:11:49 +00:00
("draft-org-files"
:base-directory ,base-dir
:base-extension "org"
:publishing-directory ,draft-publish-dir
:recursive t
:preparation-function org-blog-prepare
:publishing-function org-blog-publish-to-html
:with-toc nil
:with-title nil
:with-date t
:section-numbers nil
:html-doctype "html5"
:html-html5-fancy t
:html-head-include-default-style nil
:html-head-include-scripts nil
:htmlized-source t
:html-head-extra ,org-blog-head
:html-preamble org-blog-preamble
2020-02-22 20:52:52 +00:00
:html-postamble org-blog-postamble)
("draft-css"
:base-directory ,assets-dir
:base-extension "css"
:publishing-directory ,draft-publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
("draft-img"
:base-directory ,assets-dir
:base-extension "(jpg|png|gif|jpeg)"
:publishing-directory ,draft-publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
("draft-files"
2020-02-22 17:11:49 +00:00
:base-directory ,assets-dir
:base-extension ".*"
2020-02-22 20:52:52 +00:00
:include ".*\.(org|css|jpg|png|gif|jpeg)$"
2020-02-22 17:11:49 +00:00
:publishing-directory ,draft-publish-assets-dir
:publishing-function org-blog-publish-attachment
:recursive t)
2020-02-22 20:52:52 +00:00
("assets" :components ("css" "img" "files"))
2020-02-22 17:11:49 +00:00
("blog" :components ("orgfiles" "assets"))
2020-02-22 20:52:52 +00:00
("draft-assets" :components ("draft-css" "draft-img" "draft-files"))
2020-02-22 17:11:49 +00:00
("draft" :components ("draft-org-files" "draft-assets"))))
2019-08-20 13:35:54 +00:00
;; add target=_blank and rel="noopener noreferrer" to all links by default
(defun my-org-export-add-target-blank-to-http-links (text backend info)
"Add target=\"_blank\" to external links."
(when (and
(org-export-derived-backend-p backend 'html)
(string-match "href=\"http[^\"]+" text)
(not (string-match "target=\"" text))
2020-04-25 10:11:07 +00:00
(not (string-match (concat "href=\"" websiteorigin "[^\"]*") text)))
2019-08-20 13:35:54 +00:00
(string-match "<a " text)
(replace-match "<a target=\"_blank\" rel=\"noopener noreferrer\" " nil nil text)))
(add-to-list 'org-export-filter-link-functions
'my-org-export-add-target-blank-to-http-links)
2020-02-29 00:06:00 +00:00
(defun my-add-link-to-tangled-files (backend)
"Add a link just before source code block with tangled files.
BACKEND is the export backend. Used as symbol."
(while ;; (re-search-forward )
2020-02-29 15:46:07 +00:00
(re-search-forward "^\\( *\\)#\\+begin_src .*:tangle \\([^\s\n]*\\)" nil t)
(replace-match "\\1#+CAPTION: [[./\\2][=\\2=]]\n\\&")))
2020-02-29 00:06:00 +00:00
2020-02-29 15:46:07 +00:00
(setq org-export-before-processing-hook nil)
(add-hook 'org-export-before-processing-hook
'my-add-link-to-tangled-files)
2020-02-09 11:57:46 +00:00
2019-08-20 13:35:54 +00:00
(provide 'her-esy-fun-publish)