:PROPERTIES:
:ID: 73d663b6-1aea-4d82-a0f6-b88b302e49cb
:END:
#+TITLE: Hello, Org
#+DATE: <2023-06-20 Tue 11:45>
#+filetags: :technology:home:
* TLDR
- Create a new folder
- Put [[https://raw.githubusercontent.com/mattkae/matthewkosarek-xyz/master/index.css][index.css]], [[https://raw.githubusercontent.com/mattkae/matthewkosarek-xyz/master/publish.el][publish.el]], and [[https://github.com/mattkae/matthewkosarek-xyz/blob/master/publish.sh][publish.sh]] in the folder
- Create a folder called ~_posts~ (this is where blog posts are written)
- Create an org file in ~_posts~, ideally with a ~#+DATE~ and ~#+TITLE~ attribute
- Create a folder called ~posts~ (this is where blog posts are published to)
- Put [[https://raw.githubusercontent.com/mattkae/matthewkosarek-xyz/master/posts/post.js][post.js]] and [[https://github.com/mattkae/matthewkosarek-xyz/blob/master/posts/post.css][post.css]] inside of the ~posts~ directory
- Run ~./publish.sh~ to generate the blog post files
- Run ~./python -m http.server 8080~ from the root folder
- Navigate to ~localhost:8080/posts/sitemap.html~ to see your posts
* Introduction
I've recently fallen in love with ~org-mode~, specifically when I use it with [[https://www.orgroam.com/][org-roam]]. I find the whole workflow of creating, tagging, and - later on - searching for information on my computer to be very elegant. On top of that, now that I have the time, I want to begin writing blog posts to better work out my thoughts. With both of these things in mind, I am again turning to the universal tool for human prospering: ~org-mode~. This time, I want to see how it can help me turn a simple org file into a blog post on my website. My requirements are:
1. Org files must get published to HTML files in a particular format with a preset stylesheet
2. Code blocks must include code highlighting
3. Images must be supported
4. Posts must be timestamped with the creation date next to the title
5. A high-level "directory" page should be generated containing a list of the posts ordered chronologically with the newest at the top
6. Posts should have tags that can be used for filtering and search.
And that's pretty much it for now. Without further ado, let's jump into getting this up and running.
(Note: I will be heavily inspired by [[https://systemcrafters.net/publishing-websites-with-org-mode/building-the-site/#creating-the-build-script][this post from System Crafters]]. I highly recommend that you read his post first before you follow my post, as he provides more details about the ~org-publish-project-alist~ command than I am willing to go into in this post.)
* Basic HTML File
As a pilot, we are going to use this org file that I am currently writing (~hello.org~) as our guinea pig. The goal is to have this org file be our very first blog post.
Emacs ships with org export goodies out of the box via the ~ox-publish.el~ package (which you can find [[https://github.com/emacs-mirror/emacs/blob/master/lisp/org/ox-publish.el][here]]). In our case, we will want to use this package to write a script that exports all the ~./_posts/*.org~ files and outputs them to a corresponding ~./posts/*.html~. Leaning heavily on the System Crafters information, we can create a file called ~publish.el~ and write the following inside of it:
#+BEGIN_SRC emacs-lisp
(require 'ox-publish)
(setq org-publish-project-alist
(list
(list "matthewkosarek.xyz"
:recursive t
:base-directory "./_posts"
:publishing-directory "./posts"
:publishing-function: 'org-html-publish-to-html)))
(org-publish-all t)
(message "Build Complete")
#+END_SRC
Next, in the same way that System Crafters made a shell script to execute this lisp, snippet, we can create a file called ~publish.sh~ and write the following inside of it:
#+BEGIN_SRC sh
#!/bin/sh
emacs -Q --script publish.el
#+END_SRC
We then do a ~chmod +x publish.sh~ to make it an executable and run it with ~./publish.sh~. If everything went according to plan, we should see a new file at ~posts/hello.html~.
* Disabling features that we don't want
The next thing will be to remove some of the generated items that I didn't ask for, namely the table of contents, author, section numbers, creation time stamp, and the validation link.
#+BEGIN_SRC emacs-lisp
(require 'ox-publish)
(setq org-publish-project-alist
(list
(list "matthewkosarek.xyz"
:recursive t
:base-directory "./_posts"
:publishing-directory "./posts"
:publishing-function: 'org-html-publish-to-html
:with-toc nil ; Disable table of contents
:with-author nil ; Disable author
:section-numbers nil ; Disable section numbers
:time-stamp-file nil ; Disable timestamp
:with-date nil))) ; Disable date
(setq org-html-validation-link nil) ; Disable the validation link at the bottom
(org-publish-all t)
(message "Build Complete")
#+END_SRC
* Styling & Code Highlighting
Next thing on our list is custom styling. This can be achieved by first installing the ~htmlize~ package from ~melpa~ / ~elpa~. The EmacsWiki describes this as "a package for exporting the contents of an Emacs buffer to HTML while respecting display properties such as colors, fonts, underlining, invisibility, etc" ([[https://www.emacswiki.org/emacs/Htmlize][reference]]). If used "out-of-the-box", the buffer will be exported to HTML with all of the styles inlined (e.g. if you underline something in your org file, you will generate a ~...~). However, we are more interested in styling everything by ourselves: we don't want ~htmlize~ making assumptions about what underlining means to us! Luckily, ~htmlize~ gives us the option to export with class names instead of inline styles so that we can specify each style for ourselves.
#+BEGIN_SRC emacs-lisp
(require 'ox-publish)
;; First, we need to setup our publish.el file to hook up to melpa/elpa so that we can ensure
;; htmlize is installed before we begin publishing.
(require 'package)
(setq package-user-dir (expand-file-name "./.packages"))
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("elpa" . "https://elpa.gnu.org/packages/")))
;; Initialize the package system
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Install dependencies
(package-install 'htmlize)
(setq org-publish-project-alist
(list
(list "matthewkosarek.xyz"
:recursive t
:base-directory "./_posts"
:publishing-directory "./posts"
:publishing-function: 'org-html-publish-to-html
:with-toc nil
:with-author nil
:section-numbers nil
:time-stamp-file nil)))
(setq org-html-htmlize-output-type 'css) ;; Output classnames in the HTML instead of inline CSS
(setq org-html-htmlize-font-prefix "org-") ;; Prefix all class names with "org-"
(setq org-html-validation-link nil
org-html-head-include-scripts nil ;; Removes any scripts that were included by default
org-html-head-include-default-style nil) ;; Removes any styles that were included by default
(org-publish-all t)
(message "Build Complete")
#+END_SRC
If you run ~publish.sh~ and open the HTML page now, you will see that _zero_ styling has been applied to the page. However, if you inspect an element in your browser that you /suspect/ should have styling (like our underlined element from before), you will see that it has a class name instead of inline styles.
Now that our generated elements have class names, we can define the style for each relevant class name. In my case, I want to include both the ~index.css~ file that my entire website defines (you can find that [[https://matthewkosarek.xyz/index.css][here]]) so that there are some standard styles across the site. These standard styles include the font that should be used, the spacing around the ~body~ tag, the link styles, and other generic goodies. On top of that, we will want a custom stylesheet specifically for "post" files. In my case, I have defined the following in ~posts/post.css~:
#+BEGIN_SRC css
pre {
background-color: #FEFEFE;
border: 1px solid #D5D5D5;
border-radius: 2px;
padding: 1rem;
}
code {
font-family: "Consolas" sans-serif;
color: #D0372D;
}
.underline {
text-decoration: underline;
}
/* Taken from: https://emacs.stackexchange.com/questions/7629/the-syntax-highlight-and-indentation-of-source-code-block-in-exported-html-file */
pre span.org-builtin {color:#006FE0;font-weight:bold;}
pre span.org-string {color:#008000;}
pre span.org-keyword {color:#0000FF;}
pre span.org-variable-name {color:#BA36A5;}
pre span.org-function-name {color:#006699;}
pre span.org-type {color:#6434A3;}
pre span.org-preprocessor {color:#808080;font-weight:bold;}
pre span.org-constant {color:#D0372D;}
pre span.org-comment-delimiter {color:#8D8D84;}
pre span.org-comment {color:#8D8D84;font-style:italic}
1pre span.org-outshine-level-1 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-2 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-3 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-4 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-5 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-6 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-7 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-8 {color:#8D8D84;font-style:italic}
pre span.org-outshine-level-9 {color:#8D8D84;font-style:italic}
pre span.org-rainbow-delimiters-depth-1 {color:#707183;}
pre span.org-rainbow-delimiters-depth-2 {color:#7388d6;}
pre span.org-rainbow-delimiters-depth-3 {color:#909183;}
pre span.org-rainbow-delimiters-depth-4 {color:#709870;}
pre span.org-rainbow-delimiters-depth-5 {color:#907373;}
pre span.org-rainbow-delimiters-depth-6 {color:#6276ba;}
pre span.org-rainbow-delimiters-depth-7 {color:#858580;}
pre span.org-rainbow-delimiters-depth-8 {color:#80a880;}
pre span.org-rainbow-delimiters-depth-9 {color:#887070;}
pre span.org-sh-quoted-exec {color:#FF1493;}
pre span.org-css-selector {color:#0000FF;}
pre span.org-css-property {color:#00AA00;}
#+END_SRC
That CSS file should get you going with some decent code highlighting and styles, but I don't pretend that it is complete.
Finally, we need to tell org mode to include our two CSS files when the page is loaded. To do this, we can use the HTML ~~ entity. We will set the ~org-html-head~ variable to insert two link entities at the top of the page.
#+BEGIN_SRC emacs-lisp
(require 'ox-publish)
(require 'package)
(setq package-user-dir (expand-file-name "./.packages"))
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("elpa" . "https://elpa.gnu.org/packages/")))
;; Initialize the package system
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Install dependencies
(package-install 'htmlize)
(setq org-publish-project-alist
(list
(list "matthewkosarek.xyz"
:recursive t
:base-directory "./_posts"
:publishing-directory "./posts"
:publishing-function: 'org-html-publish-to-html
:with-toc nil
:with-author nil
:section-numbers nil
:time-stamp-file nil)))
(setq org-html-htmlize-output-type 'css)
(setq org-html-htmlize-font-prefix "org-")
(setq org-html-validation-link nil
org-html-head-include-scripts nil
org-html-head-include-default-style nil
org-html-head "
") ;; Include index.css and posts/post.css when the page loads
;; Note that I also set the "favicon" too, but this is optional
(org-publish-all t)
(message "Build Complete")
#+END_SRC
If we run the publish again, we can see that we have full styling on our code snippets and everything else on our website.
* Images
Our first two criteria have been met! Next on the list is solving images. As an example, let's use this [[/_posts/assets/squirrel.jpg][squirrel image]] that I found online with an open source license. The ideal situation would be:
1. The squirrel image lives closely to this org document (~hello.org~)
2. We can reference the image file in our org file, and see it in our HTML page as an image
Unfortunately, it doesn't look to be that easy. Let's examine the ideal situation. Let's say we provide a relative path to an image in our org file like so:
#+BEGIN_SRC txt
[[./assets/squirrel.jpg]]
#+END_SRC
If we click this link in our org buffer, the relative path will work right away. However, when we export the org file to HTML, the following tag will be generated:
#+BEGIN_SRC html
#+END_SRC
The browser cannot resolve this absolute path, which results in the alternate "squirrel.jpg" text being shown next to a broken image.
So what's the fix here? Well, we have two options, but I am going to go with the easiest. For more information, check out [[https://stackoverflow.com/questions/14684263/how-to-org-mode-image-absolute-path-of-export-html][this stackoverflow post]]. The route I chose puts the onus of making a proper link on the writer of the blog post. The fix simply modifies the ~src~ attribute of the generated HTML to have an absolute path to the image, while also allowing the org file to retain a link to the image that it understands.
#+BEGIN_SRC TXT
#+ATTR_HTML: :src /_posts/assets/squirrel.jpg
[[./assets/squirrel.jpg]]
#+END_SRC
That's all there is to it! There are simpler ways as well, but that should do it:
#+CAPTION: A Cute Squirrel
#+ATTR_HTML: :src /_posts/assets/squirrel.jpg :width 300
[[./assets/squirrel.jpg]]
* Creation Date
Let's add the creation date below the title next. To start, we will modify the publish command to remove the title (~:with-title nil~) and, in its place, show a preamble bit of HTML that contains a formatted ~div~ with the title and the "last modified" span.z
#+BEGIN_SRC emacs-lisp
(setq org-publish-project-alist
(list
(list "matthewkosarek.xyz"
:recursive t
:base-directory "./_posts"
:publishing-directory "./posts"
:publishing-function: 'org-html-publish-to-html
:with-toc nil
:with-author nil
:section-numbers nil
:time-stamp-file nil
:with-title nil
:html-preamble-format '(("en" "