;; sign it with ;; gpg --local-user yann@esposito.host --output project.el.sig --detach-sign project.el (defvar domainname "https://her.esy.fun") (defvar root-dir (projectile-project-root)) (defvar base-dir (concat root-dir "src")) (defvar publish-dir (concat root-dir "_site")) (defvar assets-dir (concat base-dir "/")) (defvar publish-assets-dir (concat publish-dir "/")) (defvar posts-dir (concat base-dir "/posts")) (defvar rss-title "Subscribe to articles") (defvar posts-descr "Articles") (defvar css-path "/css/mk.css") (defvar author-name "Yann Esposito") (defvar author-email "yann@esposito.host") (require 'org) (require 'ox-publish) (require 'ox-html) (require 'org-element) ;; (setq org-link-file-path-type 'relative) (setq org-publish-timestamp-directory (concat (projectile-project-root) "_cache/")) (defvar org-blog-head (concat "" "" "")) (defun menu (lst) "Blog menu" (concat "")) (defun get-from-info (info k) (let ((i (car (plist-get info k)))) (when (and i (stringp i)) i))) (defun org-blog-preamble (info) "Pre-amble for whole blog." (concat "
" (when-let ((date (plist-get info :date))) (format "%s" (format-time-string "%Y-%m-%d" (org-timestamp-to-time (car date))))) "

" (format "%s" (car (plist-get info :title))) "

" (when-let ((subtitle (car (plist-get info :subtitle)))) (format "

%s

" subtitle)) "
")) (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))) (defun org-blog-postamble (info) "Post-amble for whole blog." (concat "
" ;; TODO install a comment system ;; (let ((url (format "%s%s" domainname (replace-regexp-in-string base-dir "" (plist-get info :input-file))))) ;; (format "comment" ;; (url-hexify-string url))) "" (menu '("↑ Top ↑")) "
")) (defun y/org-get-keywords () (org-element-map (org-element-parse-buffer 'element) 'keyword (lambda (keyword) (cons (org-element-property :key keyword) (org-element-property :value keyword))))) (defun y/org-get-meta (keyword) (cdr (assoc keyword (y/org-get-keywords)))) (defun y/get-meta (file meta-name) "Return the value of the meta of an org-mode file. (y/get-meta file \"DESCRIPTION\") " (org-babel-with-temp-filebuffer file (y/org-get-meta meta-name))) (defun date-format-entry (entry _style project) "Return string for each ENTRY in PROJECT." (when (string-match "posts/.*" entry) (let* ((file (org-publish--expand-file-name entry project)) (title (org-publish-find-title entry project)) (date (format-time-string "%Y-%m-%d" (org-publish-find-date entry project))) (keywords (split-string (y/get-meta file "KEYWORDS") ",\s*")) (description (y/get-meta file "DESCRIPTION"))) (concat "- " (format " [%s]" date) (format " *[[file:%s][%s]]*" file title) (format " @@html:
@@%s@@html:
@@" (mapconcat (lambda (k) (format "@@html:@@#%s@@html:@@" k)) (cl-sort keywords 'string-lessp :key 'downcase) " ")) (format "@@html:
@@%s@@html:
@@" description))))) (defun org-blog-sitemap-fn-descr (descr title list) "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" "#+DESCRIPTION: " descr "\n" "" "@@html:@@" "\n\n" (mapconcat (lambda (li) (format "%s" (car li))) (seq-filter #'car (cdr list)) "\n"))) (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))) (defun org-blog-publish-to-html (plist filename pub-dir) "Same as `org-html-publish-to-html' but modifies html before finishing." (let* ((file-path (org-html-publish-to-html plist filename pub-dir)) (mk-path (format "./%s.html" (replace-regexp-in-string ".*/\\([^/]*\\)\\.org$" "\\1" filename))) (min-path (format "./%s-min.html" (replace-regexp-in-string ".*/\\([^/]*\\)\\.org$" "\\1" filename))) (sci-path (format "./%s-sci.html" (replace-regexp-in-string ".*/\\([^/]*\\)\\.org$" "\\1" filename))) (modern-path (format "./%s-modern.html" (replace-regexp-in-string ".*/\\([^/]*\\)\\.org$" "\\1" filename)))) (with-current-buffer (find-file-noselect file-path) (goto-char (point-min)) (search-forward "") (insert (mapconcat 'identity `("" "" "
" "
" "her.esy.fun - " "" " / " "" " - " ,(format "mk" mk-path) ,(format "min" min-path) ,(format "sci" sci-path) ,(format "modern" modern-path) "
" "
" "
") "\n")) (goto-char (point-max)) (search-backward "") (insert "\n
\n") (save-buffer) (kill-buffer)) file-path)) (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))) (cond ((string-match-p ".*\\.\\(png\\|jpg\\|gif\\)$" filename) (shell-command (format "~/.nix-profile/bin/convert %s -resize 400x400\\> -colorspace Gray -ordered-dither o8x8,8 %s" filename dst-file))) ((string-match-p ".*\\.css$" filename) (shell-command (format "%s/compresscss.sh %s %s" root-dir filename dst-file))) (t (copy-file filename dst-file t)))))) (defalias 'org-blog-posts-sitemap-fn (apply-partially 'org-blog-sitemap-fn-descr posts-descr)) (defun org-blog-optimize (_) (let ((default-directory root-dir)) (message "executing ./pre-deploy.sh") (shell-command "./pre-deploy.sh"))) (setq org-html-htmlize-output-type 'css) (setq org-html-htmlize-font-prefix "org-") (setq org-publish-project-alist `(("orgfiles" :base-directory ,base-dir :exclude ".*drafts/.*" :base-extension "org" :publishing-directory ,publish-dir :recursive t :preparation-function org-blog-prepare :publishing-function org-blog-publish-to-html :completion-function org-blog-optimize :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 :auto-sitemap t :sitemap-filename "archive.org" :sitemap-title "Articles" :sitemap-style list :sitemap-sort-files anti-chronologically :sitemap-format-entry date-format-entry :sitemap-function org-blog-posts-sitemap-fn) ("assets" :base-directory ,assets-dir :base-extension ".*" :exclude ".*\.org$" :publishing-directory ,publish-assets-dir :publishing-function org-blog-publish-attachment :recursive t) ("blog" :components ("assets" "orgfiles")))) ;; 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)) (not (string-match (concat "href=\"" domainname "[^\"]*") text))) (string-match "