mastodon warning! may contain trace amounts of Lisp

tests

Table of Contents

Nullam tristique diam non turpis. Sed bibendum. Cras placerat accumsan nulla. Fusce sagittis, libero non molestie mollis, magna orci ultrices dolor, at vulputate neque nulla lacinia eros.

1. file variables

2. directory variables

  • ((org-mode . ((output-path . "/out/"))))

3. exporting

3.1. files &c

  • This exports the file and returns the filename.
(org-html-export-to-html nil nil nil nil nil)
  • This grabs that filename and moves it (“renames”) to output-path + filename.
(let ((out-file (org-html-export-to-html nil nil nil nil nil)))
  (rename-file out-file (concat output-path out-file) t))
  • What if more than one file is generated?
.#+BEGIN_SRC ditaa :file gen/hello-world-ditaa.png
+--------------+
|              |
| Hello World! |
|              |
+--------------+
.#+END_SRC

3.2. css

The variable that decides how to export the syntax highlighting in emacs is org-html-htmlize-output-type. Setting it to css let me use my file instead of having a fixed inline styling.

4. index

The function that actually parses an org-mode buffer is (org-element-parse-buffer). If you want to check the output outside the minibuffer, using (with-current-buffer "colophon.org" (org-element-parse-buffer)) will do. It spits lots of (redundant?) information, though.

isn’t hard, that is, after finding the relevant Stack Exchange answer.

Mining what we might want isn’t hard: ~(with-current-buffer "colophon.org" (org-element-property :value (car (org-global-props "DATE"))))~. With this we can get whatever information we put in the file header, so if we save the last update time, we can use it to build a time-based index of this weblog. Let’s get this more straightforward:

(defun weblog-get-property (property)
  "I send thee the org-mode buffer property you wish to know."
  (org-element-property :value (car (org-global-props property))))

Saving information to a file seems advisable. I didn’t find anything straightforward on the manuals, but then this on Stack Exchange:

(defun print-to-file (filename data)
  (with-temp-file filename
    (erase-buffer) ;; otherwise it will append instead of rewrite
    (prin1 data (current-buffer))))

(defun print-to-file-raw (filename data)
  (with-temp-file filename
    (erase-buffer) ;; otherwise it will append instead of rewrite
    (princ data (current-buffer))))

(defun read-from-file (filename)
  (with-temp-buffer
    (insert-file-contents filename)
    (cl-assert (eq (point) (point-min)))
    (read (current-buffer))))

Now we can save stuff inside an alist. A list of entries with filename, title, creation date, last update date, and tags. All this will require more work to use, but having them is a good first step.

(defun weblog-save ()

  ;; in case it’s the first time we use this file
  (unless (file-exists-p "metadata.el")
    (print-to-file "metadata.el" nil))

  (let ((out-file (org-html-export-to-html nil nil nil nil nil))
        (title (weblog-get-property "TITLE"))
        (creation-date (weblog-get-property "DATE"))
        (updating-date (time-convert nil 'integer))
        (categories (weblog-get-property "CATEGORIES"))
        ;; this means we need to update files sequentially,
        ;; otherwise two updates might delete each other
        (metadata-file (read-from-file "metadata.el"))
        (metadata-here nil))

    ;; this send the created file to the right place
    ;; output-path is defined in the directory variables file
    (rename-file out-file (concat output-path out-file) t)

    (setq metadata-here (list :title title
                              :creation-date creation-date
                              :updating-date updating-date
                              :categories categories))

    ;; we delete the older entry for out-file
    (setq metadata-file (assoc-delete-all out-file metadata-file))

    (push (cons out-file metadata-here) metadata-file)

    ;; this saves the metadata for indexing
    (print-to-file "metadata.el" nil)
    (print-to-file "metadata.el" metadata-file)))

· © Edgard Bikelis (eſb) created using Emacs 29.0.50 (Org mode 9.5.3) ·
· created: 2019-10-13 last modified: 2020-05-23 ·