In which we come back, update Jekyll, and start again.
Yes, I
got a job and abandoned the blog for quite a
while. And now it looks similar but a little different.
Jekyll 4
Eager to join the modern era, I grabbed Jekyll 4.2.2, and a nice theme called
“Basically Basic”; but then the tweaking
began. Looking at the existing blog posts, it was clear that the code blocks and images would need
some work. Because the blog was originally built on pre-3.0
Octopress, I went there to get the original plugins for
code blocks and images.
Unfortunately
Jekyll 4 no longer supports Pygments
for syntax highlighting, so I had to convert the code block plugin to use
Rouge in order to format it in the same way. Rouge is
mostly compatible with Pygments-based post-processing, but the line numbering doesn’t quite match
up when you want something like arbitrary beginning line numbers (which I parse out of the link
URL and handle myself, as you can see below).
# Conversion of https://github.com/imathis/octopress/blob/master/plugins/pygments_code.rb for use# with Rouge 3.x.require'fileutils'require'digest/md5'# ROUGE_CACHE_DIR = File.expand_path('../../.rouge-cache', __FILE__)# FileUtils.mkdir_p(ROUGE_CACHE_DIR)module HighlightCode
defself.highlight(str, lang, starting_line =1)
lang ='ruby'if lang =='ru'
lang ='objc'if lang =='m'
lang ='perl'if lang =='pl'
lang ='yaml'if lang =='yml'
str = rouge(str, lang)
tableize_code(str, lang, starting_line)
enddefself.rouge(code, lang)
require"rouge"
formatter =::Rouge::Formatters::HTML.new
formatter =::Rouge::Formatters::HTMLLinewise.new(formatter, {class: "highlight"})
lexer =::Rouge::Lexer.find_fancy(lang, code) ||Rouge::Lexers::PlainTextif defined?(ROUGE_CACHE_DIR)
path =File.join(ROUGE_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
ifFile.exist?(path)
highlighted_code =File.read(path)
elsebegin
highlighted_code = formatter.format(lexer.lex(code))
endFile.open(path, 'w') {|f| f.print(highlighted_code) }
endelse
highlighted_code = formatter.format(lexer.lex(code))
end
highlighted_code
enddefself.tableize_code (str, lang ='', starting_line =1)
table ='<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
code =''
str.lines.each_with_index do|line,index|# puts "code line #{index} is #{line}"# now get rid of those div tags
line = line.delete_prefix("</div>")
line = line.delete_prefix("<div class=\"highlight\">")
line = line.tr("\r", '') if lang =='json'# getting extra carriage returns from JSON for some reason.# the last line is likely blank nowif index == str.lines.length -1&& line ==""# puts " ...skipping that one."nextend
table +="<span class='line-number'>#{index+starting_line}</span>\n"
code +="<span class='line'>#{line}</span>"end
table +="</pre></td><td class='code'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"endend
There’s a lot of “printf debugging” in there (commented out at the moment); it took a lot of trial
and error to get the formatting I expected. The caching proved to have no impact at all in this
environment, so I disabled it so it wouldn’t be a concern. It took
a bit of CSS work
as well, and the code blocks look a bit better to me now:
Not perfect, but better.
The image tag code and CSS from Octopress worked well, without much trouble.
But then…
I started considering how I would handle the categories, and add some less-minimal navigation.
Given the time that had elapsed, I also looked into what newer (but not too new) static site
generators existed. And then I saw
Hugo-Octopress.