#+TITLE: Using org to Blog with Jekyll #+AUTHOR: Ian Barton. #+EMAIL: ian@manor-farm.org #+LANGUAGE: en #+OPTIONS: H:3 num:nil toc:t \n:nil ::t |:t ^:nil -:t f:t *:t tex:t d:(HIDE) tags:not-in-toc #+STARTUP: hidestars #+HTML_LINK_UP: index.html #+HTML_LINK_HOME: https://orgmode.org/worg/ # This file is released by its authors and contributors under the GNU # Free Documentation license v1.3 or later, code examples are released # under the GNU General Public License v3 or later. * Introduction [[http://wiki.github.com/mojombo/jekyll][Jekyll]] is a static web site generator written in Ruby. It can transform various text markups, using a templating language, into static html. The resulting site can be served by almost any web server without requiring additional components such as php. Jekyll is the tool used to produce Github's pages. This article discusses how to produce both a static site and a blog using Jekyll and org. Rather than writing a markup processor for org files, I have relied on org's html export features to generate files that can be processed by Jekyll. Org already has an excellent html export engine. However, it lacks built in support for blogging. Using Jekyll also gives more control over the final appearance of your site. Publishing your site with org and Jekyll involves three steps: 1) write your page content using org. 2) use org to export your pages to html in the Jekyll project directory. 3) run Jekyll to convert your html pages exported from org into your final site. By default Jekyll produces its output in the =_site= directory of Jekyll's working directory. This is a self contained version of your site, which can be deployed to your web server. The files in =_site= are completely self contained, so all you need to do is to copy them to your web server. Methods include using ftp, rsync or a git post commit hook. You can configure where Jekyll puts its published files in =_config.yml=. Essentially, I am using org to produce everything between the =
= tags on the page and Jekyll to produce the rest. Note that you can easily embed html content in your org pages using the =+BEGIN_EXPORT html= tag. * Install Jekyll Installation is described at the [[http://github.com/mojombo/jekyll][Jekyll]] web site. * Project Directory Structure Jekyll expects a certain directory structure. In the example below my Jekyll project is in a directory called =jekyll=. Blog posts are in =_posts= and the layout templates in =_layouts=. The =_includes= directory is for files containing code you want to include in other pages e.g. a header or sidebar. The file =_config.yml= is a YAML file that contains Jekyll's configuration for the site. In addition to the =_posts= directory you can create other directories to hold different non blog parts of your site. #+BEGIN_EXAMPLE '|myproject '| |org '| |_posts '| |-- 2009-11-26-my-first-post.org '| |index.org '| |jekyll '| -- _config.yml '| -- _layouts '| |-- default.html '| `-- post.html '| -- _posts '| |-- 2009-11-26-my-first-post.html '| '| -- |_site '| -- |_includes ` -- index.html #+END_EXAMPLE You should setup the directory structure of your org files to mirror that of the Jekyll project. Then when you export your org files as html the files will end up in the correct place in your Jekyll project. I usually place the directory containing my org files in the directory about the Jekyll project directory to make sure that Jekyll doesn't consider .org files to be part of its project. * Configuring org html Export The fundamentals of publishing html are described in the [[https://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.html][HTML publishing tutorial]] on worg. I am assuming that you have a basic working org publishing setup. By default org produces complete web pages. However, as I am using Jekyll I am only really interested in the section of the page between the == tags, as Jekyll produces the rest. Most things in org are configurable and it's possible to tell org to export only the bits of the page between the == tags. Here is the relevant section of my =.emacs= file: #+BEGIN_SRC emacs-lisp (setq org-publish-project-alist '( ("org-ianbarton" ;; Path to your org files. :base-directory "~/devel/ianbarton/org/" :base-extension "org" ;; Path to your Jekyll project. :publishing-directory "~/devel/ianbarton/jekyll/" :recursive t :publishing-function org-publish-org-to-html :headline-levels 4 :html-extension "html" :body-only t ;; Only export section between ) ("org-static-ian" :base-directory "~/devel/ianbarton/org/" :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php" :publishing-directory "~/devel/ianbarton/" :recursive t :publishing-function org-publish-attachment) ("ian" :components ("org-ianbarton" "org-static-ian")) )) #+END_SRC To export my site I just run =C-c e X ian=. You need to set the destination of your exported files to your Jekyll project directory. Assuming you have set up your org directory structure to mirror that of your Jekyll project everything should end up in the correct place. * Creating an org File to be Published with Jekyll When you run Jekyll it processes the source files for your site and any files with YAML Front Matter are subject to special processing. The Front Matter is used to tell Jekyll how to format your page. Bear in mind that Jekyll doesn't process your =.org= files, but the =.html= files produced by exporting. So when writing an org file it should be formatted in such a way that when exported it produces html suitable for processing by Jekyll. YAML Front Matter must be the first thing in the file, with no blank lines above the Front Matter Section. A typical Front Matter Section would look like: #+begin_example --- layout: default title: My Page Title. --- #+end_example So you should ensure that any Front Matter directives come first in your org file. Note that the three hyphens =---= are part of the markup and are required. The layout tag tells Jekyll which layout from its =_layouts= directory should be used to format your page. You can include any other keys in the Front Matter section (e.g. =title:=), which you can use in your page. See the Jekyll [[http://wiki.github.com/mojombo/jekyll/yaml-front-matter][wiki]] for more details on Front Matter. Below is a short extract from one of my org files showing my setup: #+BEGIN_EXAMPLE ,#+STARTUP: showall indent ,#+STARTUP: hidestars ,#+BEGIN_EXPORT html --- layout: default title: Benighted on the Ben. excerpt: An unplanned bivouac on Ben Nevis. --- ,#+END_EXPORT It was early January when six of us travelled up to .... #+END_EXAMPLE The Front Matter section is wrapped in =#+BEGIN_EXPORT html= so it is exported literally to the final html file. You may need to upgrade your org version as older versions produced two blank lines before the Front Matter section when exported. You can define your own Front Matter keys and use them within your generated page. In the above example I use the "excerpt" key to display "teasers" for a blog post. Note that the current git version of org removes the first =---= if the directory containing the file start with an underscore. The workaround is to start your file with =---= in both the first two lines. Carsten has also provided two hooks that are run after exporting is complete, which can also be used to tidy up the output: #+BEGIN_SRC emacs-lisp org-export-html-final-hook (always) org-publish-after-export-hook (when going through org-publish) #+END_SRC Once you have exported your org project to html it's simply a matter of running jekyll to produce the final output. By default Jekyll puts its output in the =_site= directory of your project, but you can customize this in your =_config.yml= file. * Blogging with Jekyll and Org Jekyll has built-in support for blogging. Anything you place in the =_posts= directory of your Jekyll project is considered as a blog post. However, the file names of your posts must adhere to the following format: #+BEGIN_EXAMPLE yyyy-mm-dd-post_name.html #+END_EXAMPLE To write a post just create a new file with the correct filename in your =org/_posts= directory. You may find that Yasnippet is useful for inserting Front Matter and other directives in your org file. When you have finished just run =C-c e X= project_name to export your org project as html and then run jekyll to generate your site. You can use Jekyll's [[http://wiki.github.com/mojombo/jekyll/template-data][template]] markup to decide how your blog posts are displayed. On the Jekyll [[http://wiki.github.com/mojombo/jekyll/sites][sites]] page there are many sites with source listed, so you can study how other people use the markup to create their blog. You can also view my site http://www.ian-barton.com and see a snapshot of the source at http://github.com/geekinthesticks/ianbarton. You can assign categories to your posts either by placing posts inside folders like: #+BEGIN_EXAMPLE _posts/org/jekyll/howto.html #+END_EXAMPLE This would assign your post to the /org/ and /jekyll/ categories. or by using YAML markup in your org file: #+BEGIN_EXAMPLE categories: - org - linux #+END_EXAMPLE ** Showing Blog Posts on the Front Page Most blogs show the latest posts on their front page. The example below shows the title and an excerpt for the five latest posts: #+BEGIN_EXAMPLE htmlA photo of me