Shhhhhh! This post be for RSS subscribers only. Look here for more information (and to join in on the fun).
--
'tis been a little while since I needlessly tweaked me blog, just to add a feature not a soul asked for, but I got bit by the "why the hell not?" bug and decided to cross-publish all of me posts in plaintext format in addition to HTML.
While the end-result be pretty straightforward (just click on the txt link at the bottom of this post for an example), getting there took a wee bit of discovery on me part 'cause I be not particularly familiar with Jekyll Generators—I say a "little bit" 'cause, if I be speaking plain, GitHub Copilot did most of the hard work for me, so all I had to do was expand on some scaffolding.
Generating a Generator
So, as mentioned, the first thing I needed to do was create a Jekyll Generator to turn me Markdown posts into plain text ones (.md => .txt).
For the uninitiated, a generator be a plugin that allows ye to generate additional content during the build process, typically against information that has already been inventoried or processed. Generating alternative formats (like me plaintext generator) be a common use-case, howbeit modifying existing content be also not unheard of.
To set sail, though, we need to create two files: the generator plugin itself, and a template file. Lets start with the template (which we will place in _layouts/txt.html):
---
layout: null
---
{{ content }}
All of our plaintext files will be built off the above template, which (if ye be new to Jekyll templates) be exactly what it looks like: a page that prints the provided content with no additional layouts applied.
Next, we need to create the generator, which will be placed in _plugins/jekyll-txt-generator/jekyll-txt-generator.rb. For the purposes of this tutorial, we be going to start with as basic a functionality as possible: converting our Markdown post files into plain text files with no additional processing:
module Jekyll
class TxtGenerator < Generator
safe true
priority :low
def generate(site)
site.posts.docs.each do |post|
site.pages << TxtPost.new(site, post)
end
end
end
class TxtPost < Page
def initialize(site, post)
@site = site
@base = site.source
@dir = File.dirname(post.url)
@name = File.basename(post.url, File.extname(post.url)) + '.txt'
content = txtify(post)
self.process(@name)
self.read_yaml(File.join(@base, '_layouts'), 'txt.html')
self.content = content
self.data['layout'] = nil
end
def txtify(post)
content << post.content
content
end
end
end
At its core, what's happening above be that we be iterating through every post in the _posts directory (the default directory for posts), and then passing its information into the TxtPost::Page class, which takes that data, modifies it as necessary (which be to say, does naught) using the txtify() method, applies it to our txt.html template created above, and then writes it out to a new .txt file alongside the other generated .html file.
Getting MUDdy with It
While the end-results were acceptable, I quickly realized that I could do a lot more to make things readable, namely including a nicely-formatted post Title and Date at the top of each generated text file.
To do this, I wanted to modify the txtify() method to prefix the post.content with the aforementioned post title and date, as well as a nice line to separate it from the rest of the post content:
def txtify(post)
content = ""
content << 'Title: ' + post.data['title'] + "\n"
content << 'Date: ' + post.date.strftime('%B %d, %Y') + "\n"
content << "\n=-=-=-=\n\n"
content << post.content
content
end
For the unaware, I be a huge fan of Multi-User Dungeons (MUDs). The early ancestors of MMORPGs, MUDs be the reason I got into software development in the first place, so the horizontal line I chose to separate the post data from the content be a nod to the mighty first MUD I coded for called Ainoki.
For whatever reason, the original developer of Ainoki truly loved to use alternating equals signs and dashes as separators, and I've liked that aesthetic e'er since.
Once the generator runs, we now have text files that look something like this:
Title: A Polyglot Hello World Has Appeared! Date: December 30, 2023 =-=-=-= You ever have an interesting problem drop in your lap that you just can't let go of?
Pretty simple, but looks nice and gets the job done.
Not-Quite Markdown
Thank the stars, I write all of me posts in Markdown, which be already a pretty plaintext-friendly format, so I didn't have to go too crazy and have to convert HTML to plaintext (which would be a little more than irritating).
But there be a few elements in Markdown that be not the most reader-friendly in their native form that I wanted to address:
- Images
- Links
Markdown links and images both follow a similar format: [I am a link](http://link.awesome) and . Not the worst thing in the world, but also not the most parseable.
So, at a bare minimum, I knew that I wanted to preserve most of the Markdowniness of me posts, with the exception of more readable images and links.
Ahoy, Footnotes
A while back, I hosted some content in a self-hosted Gopherhole (Gopher being one of the early competitors of the World Wide Web that be still available in some capacities today).
While managing a Gopherhole ended up being a bit too much overhead for me (maybe I'll revisit it someday if I can simplify me delivery... likely as not using another Jekyll plugin!), one common pattern employed by most of the phlogs (Gopher Blogs... get it?) I read was a footnote system for page links.
This made a shipload of sense to copy, since ye can't actually create hyperlinks in plaintext anyway, which got me to here:
def txtify(post)
content = ""
content << 'Title: ' + post.data['title'] + "\n"
content << 'Date: ' + post.date.strftime('%B %d, %Y') + "\n"
content << "\n=-=-=-=\n\n"
content << post.content
footnotes = []
# Convert Markdown links to footnotes
content.gsub!(/\[([^\]]+)\]\(([^\)]+)\)/) do |match|
footnotes << "#{$2}"
"#{$1}[#{footnotes.size}]"
end
# Trim trailing newlines at the end of the file
content.gsub!(/\n+\z/, '')
# Append footnotes
content << "\n\n=-=-=-=\n\n"
footnotes.each_with_index do |link, index|
# add config.url to relative links
if link.start_with?('/')
link = @site.config['url'] + link
end
content << "[#{index+1}]: #{link}\n"
end
content
end
To break it down, I did the following:
- Identified all Markdown-formatted links using everyone's favorite tool to use: regular expressions (
/\[([^\]]+)\]\(([^\)]+)\)/). - Iterated through each link and saved the link in a
footnotesarray, and rewrote the Markdown links in a pretty simple format (Link Title[1]). - Wrote out each link at the bottom of the page in a footnotes section, separated with me handy-dandy MUD-style
=-=-=-=separator.
I also made sure to convert me internal links to fully-qualified links, so the reader could simply copy-paste the entire link instead of having to type in me domain name.
When all be said and sailed this resulted in a nice footer section that looks like the following:
Are you in the same boat? If so, I'd love to hear how you are approaching the problem! =-=-=-= [1]: https://handbrake.fr/ [2]: https://www.everand.com/ [3]: https://www.amazon.com/Browse-Kindle-Unlimited-Books/ [4]: https://www.overdrive.com/apps/libby [5]: https://www.reddit.com/r/opendirectories/ [6]: https://calibre-ebook.com/ [7]: https://www.moondownload.com/
\*swoon\*
And Finally, Images
The last thing I needed to do was deal with me images.
To speak plain, by the time I got here I was starting to get burned out. I mean, truth be told, how many different ways can ye render a link to an image? As the tides reveal, not many if ye care about yer mental health.
What I ultimately landed on was an exceedingly simple Image: prefix afore each image link.
I don't put images inline in me posts (in fact I be not even sure if Markdown supports that), so knowing that the images be going to always be on their own line made it easy enough to cut a few thought corners here.
All I truly had to do was write another regular expression to identify the image format, throw away the alt tag—I know, not at all accessible of me, but the vast majority of me images don't actually add aught to the posts beyond aesthetics—and then add the Image: prefix afore the link:
def txtify(post)
content = ""
content << 'Title: ' + post.data['title'] + "\n"
content << 'Date: ' + post.date.strftime('%B %d, %Y') + "\n"
content << "\n=-=-=-=\n\n"
content << post.content
footnotes = []
# Convert Markdown links to footnotes
content.gsub!(/\[([^\]]+)\]\(([^\)]+)\)/) do |match|
footnotes << "#{$2}"
"#{$1}[#{footnotes.size}]"
end
# Convert Markdown images to references
content.gsub!(/\!\[([^\]]*)\]\(([^\)]+)\)/) do |match|
link = $2
# add config.url to relative images
if link.start_with?('/')
link = @site.config['url'] + link
end
"Image: #{link}"
end
# Trim trailing newlines at the end of the file
content.gsub!(/\n+\z/, '')
# Append footnotes
content << "\n\n=-=-=-=\n\n"
footnotes.each_with_index do |link, index|
# add config.url to relative links
if link.start_with?('/')
link = @site.config['url'] + link
end
content << "[#{index+1}]: #{link}\n"
end
content
end
Simple, no?
I won't bother breaking the code down this time, 'cause 'tis just a sub-routine of the link parser, and I be sure ye get the idea by now. But what it does output be something like the following:
Image: http://flower.codes/assets/img/posts/windowsill-books.jpg My wife and I have been talking about moving a lot lately. We've hit a wall with our current environment, and have been longing for something just a bit simpler for quite some time now.
'tis not perfect, but I don't care enough to try and fathom what perfect means, so good enough be sure as the tides good enough here.
What's Next?
This was a fun exercise for me.
I've been wanting to cross-publish me posts in different formats for quite some time, but for reasons not entirely clear to me, put it all off.
But I be glad I did it, and 'tis got me thinking about me next experiment with this Jekyll-based nightmare I call a blog; namely, properly cross-posting to a Gopherhole or some other format without adding any unnecessary overhead.
Stay tuned, me shipmates.
--
This be post 014 of #100DaysToOffload