Skip to content

Commit

Permalink
Add gems
Browse files Browse the repository at this point in the history
  • Loading branch information
quincylvania committed Jan 17, 2024
1 parent 911ffae commit a9ae483
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.3
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

# these gems aren't published so we need to tell ruby where they're located
gem "absolute-urls", path: "_plugins/absolute-urls"
gem "post-aliases", path: "_plugins/post-aliases"
gem "unwrap-img", path: "_plugins/unwrap-img"

gemspec
4 changes: 4 additions & 0 deletions _plugins/absolute-urls/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec
16 changes: 16 additions & 0 deletions _plugins/absolute-urls/absolute-urls.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

Gem::Specification.new do |spec|
spec.name = "absolute-urls"
spec.version = "0.1.0"
spec.authors = ["Quincy Morgan"]
spec.email = ["[email protected]"]

spec.summary = "Force absolute URLs in Jekyll Kramdown `href` and `src` properties."
spec.homepage = "https://github.com/osmus/dogwood"
spec.license = "MIT"

spec.files = Dir["lib/**/*"]

spec.add_runtime_dependency "jekyll", "~> 4.3"
end
25 changes: 25 additions & 0 deletions _plugins/absolute-urls/lib/absolute-urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This plugin ensures all `src` and `href` properties created by markdown parsing
# are absolute links. This solves issues that occur when the baseurl is a subdirectory.

require 'kramdown/utils/html'

SITE_BASEURL = (Jekyll.sites()[0].config['url'] || '') + (Jekyll.sites()[0].config['baseurl'] || '');

module AbsoluteUrls
def html_attributes(attr)

return super unless attr['src'] || attr['href']

if attr['src'] && attr['src'].start_with?("/")
attr['src'] = SITE_BASEURL + attr['src']
end

if attr['href'] && attr['href'].start_with?("/")
attr['href'] = SITE_BASEURL + attr['href']
end

super(attr)
end
end

Kramdown::Utils::Html.prepend AbsoluteUrls
4 changes: 4 additions & 0 deletions _plugins/post-aliases/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec
38 changes: 38 additions & 0 deletions _plugins/post-aliases/lib/post-aliases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This plugin generates redirect pages for all posts, pointing from the /:year/:month/:title
# format to the actual URL, whatever it may be. This makes the site backwards-compatible
# for links to pre-2023-redesign blog posts. It could have other uses as well, like URL-shortening.
#
# Example:
# /2023/02/tasking-manager-redesign
# --> automatically redirects to -->
# /news/2023/02/tasking-manager-redesign

module RedirectPosts
class RedirectPostsGenerator < Jekyll::Generator
safe true

def generate(site)
site.posts.docs.each do |post|
site.pages << PostRedirectPage.new(site, post)
end
end
end
end

class PostRedirectPage < Jekyll::Page
def initialize(site, post)
@site = site # the current site instance.
@base = site.source # path to the source directory.

@basename = post.basename # filename without the extension.
@ext = post.data["ext"] # the extension.
@name = post.basename + post.data["ext"] # basically @basename + @ext.

@data = {}

data["layout"] = "redirect"
data["redirect"] = post.url
data["permalink"] = post.date.strftime('/%Y/%m/') + post.data["slug"] + "/"

end
end
16 changes: 16 additions & 0 deletions _plugins/post-aliases/post-aliases.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

Gem::Specification.new do |spec|
spec.name = "post-aliases"
spec.version = "0.1.0"
spec.authors = ["Quincy Morgan"]
spec.email = ["[email protected]"]

spec.summary = "Create redirect pages for /:year/:month/:title to canonical posts."
spec.homepage = "https://github.com/osmus/dogwood"
spec.license = "MIT"

spec.files = Dir["lib/**/*"]

spec.add_runtime_dependency "jekyll", "~> 4.3"
end
4 changes: 4 additions & 0 deletions _plugins/unwrap-img/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec
31 changes: 31 additions & 0 deletions _plugins/unwrap-img/lib/unwrap-img.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This plugin unwrap <img> elements wrapped in <p> elements, and adds an 'img-container'
# class to <a> elements that wrap <img> elements. This allows us to properly style images
# separately from <p>, e.g. to give them different widths.

require 'kramdown/converter/html'

BLANK_RE = /\A[[:space:]]*\z/

module StandaloneImages
def isImageElement(ele)
return ele.type == :img || (ele.type == :html_element && ele.value == "img")
end
def convert_p(el, indent)
return super unless el.children.any? {|child| isImageElement(child) } || el.children.all? { |child| child.type == :a }
# remove empty text elements that might be sandwiched between images
els = el.children.select { |child| child.type != :text || !BLANK_RE.match?(child.value) }
els = els.map { |child|
if child.children.size == 1 && isImageElement(child.children.first)
if child.attr['class'].nil?
child.attr['class'] = 'img-container'
else
child.attr['class'] = child.attr['class'] + ' img-container'
end
end
convert(child, indent)
}
return els.join('')
end
end

Kramdown::Converter::Html.prepend StandaloneImages
16 changes: 16 additions & 0 deletions _plugins/unwrap-img/unwrap-img.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

Gem::Specification.new do |spec|
spec.name = "unwrap-img"
spec.version = "0.1.0"
spec.authors = ["Quincy Morgan"]
spec.email = ["[email protected]"]

spec.summary = "Unwrap Kramdown <img> elements from <p> elements."
spec.homepage = "https://github.com/osmus/dogwood"
spec.license = "MIT"

spec.files = Dir["lib/**/*"]

spec.add_runtime_dependency "jekyll", "~> 4.3"
end
23 changes: 23 additions & 0 deletions dogwood.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

Gem::Specification.new do |spec|
spec.name = "osmus-dogwood"
spec.version = "0.1.0"
spec.authors = ["Quincy Morgan"]
spec.email = ["[email protected]"]

spec.summary = "The Jekyll theme powering openstreetmap.us"
spec.homepage = "https://github.com/osmus/dogwood"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").select { |f| f.match(%r!^(assets|_data|_plugins|_layouts|_includes|_sass|LICENSE|README|_config\.yml)!i) }

spec.add_runtime_dependency "jekyll", "~> 4.3"

spec.add_runtime_dependency "jekyll-archives", "~> 2.2.1"
spec.add_runtime_dependency 'jekyll-include-cache', "~> 0.2.1"

spec.add_runtime_dependency "absolute-urls"
spec.add_runtime_dependency "post-aliases"
spec.add_runtime_dependency "unwrap-img"
end

0 comments on commit a9ae483

Please sign in to comment.