Path: blob/main/_plugins/jekyll-boxify.rb
1677 views
# frozen_string_literal: true12require 'jekyll'3require './_plugins/gtn'45module Jekyll6module Generators7# The GTN Box generation process8class Boxify < Jekyll::Generator9def initialize(config) # :nodoc:10super11@config = config['boxify'] ||= {}12end1314def generate(site) # :nodoc:15Jekyll.logger.info '[GTN/Boxify]'16site.pages.each { |page| boxify page, site }17site.posts.docs.each { |post| boxify post, site }18end1920##21# This function adds boxes to the page content.22# Params:23# +page+:: The page to add boxes to24# +site+:: The +Jekyll::Site+ object25def boxify(page, _site)26return if page.content.nil?2728lang = page['lang'] || 'en'2930# Interim solution, fancier box titles31# rubocop:disable Layout/LineLength32page.content = page.content.gsub(%r{<(?<boxclass>#{Gtn::Boxify.box_classes})-title( ?(?<noprefix>noprefix))>(?<title>.*?)</\s*\k<boxclass>-title\s*>}) do33# rubocop:enable Layout/LineLength34m = ::Regexp.last_match35box_type = m[:boxclass]36title = m[:title]37noprefix = m[:noprefix]38if page.data['citation_target'] == 'jupyter'39title = Gtn::Boxify.safe_title(title)40title = Gtn::Boxify.format_box_title(title, box_type, lang, noprefix: noprefix)41icon = Gtn::Boxify.get_icon(box_type, emoji: true)42box = "<div class=\"box-title\" aria-description=\"#{box_type} box: " \43"#{title}\" style=\"font-size: 150%\">#{icon} #{title}</div>"44box.gsub!(/\\"/, '"')45box.gsub!(/([^\\])"/, '\1\\"')46else47_, box = Gtn::Boxify.generate_title(box_type, title, lang, page.path, noprefix: noprefix)48end4950box51end5253# Long term solution, proper new boxes54# BUT: does not work with <details></details> that are actual HTML elements, so we'll need to rename those.55# page.content = page.content.gsub(/<(#{Gtn::Boxify.box_classes})>/) {56# box_type = $157# box = Gtn::Boxify.generate_box(box_type, nil, lang, page.path)58# box59# }6061# page.content = page.content.gsub(/<(#{Gtn::Boxify.box_classes}) title="([^"]*)">/) {62# box_type = $163# title = $264# box = Gtn::Boxify.generate_box(box_type, title, lang, page.path)65# box66# }6768# page.content = page.content.gsub(/<\/\s*(#{Gtn::Boxify::box_classes})\s*>/) {69# box_type = $170# "\n</div></div><!--#{box_type}-->"71# }72end73end74end75end767778