0%

Blogging with Octopress and Jekyll

Nowadays, most blogs are powered by Wordpress. I am a Wordpress users too and I have to admit it is really a great for blogs.
As others CMS, Wordpress requires a database and PHP in order to process the dynamic pages server-side.
Jekyll is a static site generator. With it I can generate all my blog pages in on my computer and then publish the entire website on a static hosting server.

Update - Actually I used Octopress for few days. Then I moved to a very similar static site generator: Hexo. Hexo is easier to use and quite compatible with the Jekyll blog format. Additionally, Hexo runs with nodejs :-)
Have a look at the source code with instructions.

Prepare the Jekyll environment

The first drawback is that you need to set-up a local dev environment in order to be able to generate a static website on your machine. For Jekyll/Octopress you need several tool: git, ruby with bundler, and a javascript runtime.

git

I bet you already know what git is.

ruby

Ruby is an open source programming language. In order to install it locally, the are convenient ways such as rbenv or rvm.
Make sure to install at least ruby 1.9.3 in order to be compatible with Octopress 3.0 and check with:

1
> ruby --version
2
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin16]

A javascript runtime

My favourite javascript runtime is nodejs. A convenient way to install it is nvm. After the installation you can check it with:

1
> node --version
2
v6.11.2

Bundler

Ruby libraries are, almost always, distributed in form of ruby-gems (in short gems).
Bundler provides a consistent environment for ruby projects by tracking and installing the exact gems and versions that are needed. All the gems dependencies are declared in the Gemfile and jekyll websites usually have a Gemfile for the dependencies.

Bundler is a gem itself and to install it (globally in your ruby environment) just type:

1
gem install bundler

Note: if you are using an old version of rbenv, then you’ll need to run rbenv rehash in order to be able to start using bundler.

Start a new website

For my first experience with Jekyll I am going with Octopress (a blogging framework based on Jekyll). It is basically Jekyll with a new graphic theme and additional plugins.
To start clone the Octopress source code git clone git://github.com/imathis/octopress.git octopress and then:

1
> cd octopress/
2
[git:master]> bundle install

With bundle install we are downloading all the gems needed to generate the website locally.
A very special gem is rake, a Make-like task-runner implemented in ruby. It is used in Octopress to simplify some development task that are usually performed manually when using a plain Jekyll installation.
For example, I used bundle exec rake setup_github_pages to publish my local website to github-pages. This task created a source branch for the local Octopress installation and setup a master branch in the _deploy subfolder.

pushed the local octopress installation to the source branch of my github repository.

Setup an Octopress theme

In order to use the default theme, just type:

1
[git:source *]> bundle exec rake install
2
## Copying classic theme into ./source and ./sass
3
mkdir -p source
4
cp -r .themes/classic/source/. source
5
mkdir -p sass
6
cp -r .themes/classic/sass/. sass
7
mkdir -p source/_posts
8
mkdir -p public

The classic theme has been copied in the source and sass directories.
Also the public folder has been created: it will host my generated pages.

Generate the static website

Using rake, just issue:

1
[git:source *]> bundle exec rake generate
2
## Generating Site with Jekyll
3
directory source/stylesheets
4
    write source/stylesheets/screen.css
5
Configuration file: /opt/home/github/fsferrara/octopress/_config.yml
6
            Source: source
7
       Destination: public
8
      Generating...
9
                    done.
10
 Auto-regeneration: disabled. Use --watch to enable.

Generate the static website

Always using rake, just issue bundle exec rake deploy.

Commit the website source

This is the Jekyll feature I like most: I can version the source code of the website :-)
For the first commit:

1
[git:source *]> git add .
2
[git:source +]> git commit -m "my first octopress commit"
3
[git:source]> git push origin source

Since I am using github as hosting solution, I have a source branch containing the source code of the blog, and a master branch (linked to the _deploy subfolder) containing the generated website.

Restoring the local website on a new computer

Or at least, restoring the local website on the same computer with a new os installation.
The steps are:

1
> git clone https://github.com/fsferrara/fsferrara.github.io.git
2
> cd fsferrara.github.io
3
[git:master]> git checkout source
4
[git:source]> git remote add octopress git://github.com/imathis/octopress.git
5
[git:source]> git clone https://github.com/fsferrara/fsferrara.github.io.git _deploy
6
[git:source]> bundle install
7
[git:source]> bundle exec rake generate
8
[git:source]> bundle exec rake deploy

The procedure is quite the same, except the fact I had to clone the master branch in the _deploy subfolder because I am using github-pages.

My first post with Octopress

My first post is exactly this one you are now reading. As per Jekyll convention it is named in a format like YYYY-MM-DD-post-title.markdown and it is placed in the source/_posts directory.
Anyway, the new_post rake task simplify the operation of adding a new post. I could have run this:

1
[git:source]> bundler exec rake new_post["Blogging with Octopress and Jekyll"]
2
mkdir -p source/_posts
3
Creating new post: source/_posts/2018-01-05-blogging-with-octopress-and-jekyll.markdown

to have the new post file in the source/_posts folder. Additionally, the post file is created with the standard Front Matter:

1
[git:source]> cat source/_posts/2018-01-05-blogging-with-octopress-and-jekyll.markdown
2
---
3
layout: post
4
title: "Blogging with Octopress and Jekyll"
5
date: 2018-01-05 23:01:08 +0100
6
comments: true
7
categories:
8
---

Front matter is where Jekyll starts to get really cool. Any file that contains a YAML front matter block will be processed by Jekyll as a special file. This block adds meta-data to the file sush as permalink, categories, tags, and so on.

The about page

Similar to the post creation, the rake task new_page helps to create new pages.
For the usual about page: bundle exec rake new_page["about"]. Here are more information about Octopress pages.

The about page usually has a link in the main menu. To add such a link, modify the file source/_includes/custom/navigation.html to add a new entry.

Preview the changes locally before publishing

Jekyll is able to generate and serve the static website locally. Octopress offers the rake task bundle exec rake preview that starts a web server locally listening at http://localhost:4000/.
This task will also watch for local changes and apply them immediately to the local preview.