Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/jekyll-boxify.rb
1677 views
1
# frozen_string_literal: true
2
3
require 'jekyll'
4
require './_plugins/gtn'
5
6
module Jekyll
7
module Generators
8
# The GTN Box generation process
9
class Boxify < Jekyll::Generator
10
def initialize(config) # :nodoc:
11
super
12
@config = config['boxify'] ||= {}
13
end
14
15
def generate(site) # :nodoc:
16
Jekyll.logger.info '[GTN/Boxify]'
17
site.pages.each { |page| boxify page, site }
18
site.posts.docs.each { |post| boxify post, site }
19
end
20
21
##
22
# This function adds boxes to the page content.
23
# Params:
24
# +page+:: The page to add boxes to
25
# +site+:: The +Jekyll::Site+ object
26
def boxify(page, _site)
27
return if page.content.nil?
28
29
lang = page['lang'] || 'en'
30
31
# Interim solution, fancier box titles
32
# rubocop:disable Layout/LineLength
33
page.content = page.content.gsub(%r{<(?<boxclass>#{Gtn::Boxify.box_classes})-title( ?(?<noprefix>noprefix))>(?<title>.*?)</\s*\k<boxclass>-title\s*>}) do
34
# rubocop:enable Layout/LineLength
35
m = ::Regexp.last_match
36
box_type = m[:boxclass]
37
title = m[:title]
38
noprefix = m[:noprefix]
39
if page.data['citation_target'] == 'jupyter'
40
title = Gtn::Boxify.safe_title(title)
41
title = Gtn::Boxify.format_box_title(title, box_type, lang, noprefix: noprefix)
42
icon = Gtn::Boxify.get_icon(box_type, emoji: true)
43
box = "<div class=\"box-title\" aria-description=\"#{box_type} box: " \
44
"#{title}\" style=\"font-size: 150%\">#{icon} #{title}</div>"
45
box.gsub!(/\\&quot/, '&quot')
46
box.gsub!(/([^\\])"/, '\1\\"')
47
else
48
_, box = Gtn::Boxify.generate_title(box_type, title, lang, page.path, noprefix: noprefix)
49
end
50
51
box
52
end
53
54
# Long term solution, proper new boxes
55
# BUT: does not work with <details></details> that are actual HTML elements, so we'll need to rename those.
56
# page.content = page.content.gsub(/<(#{Gtn::Boxify.box_classes})>/) {
57
# box_type = $1
58
# box = Gtn::Boxify.generate_box(box_type, nil, lang, page.path)
59
# box
60
# }
61
62
# page.content = page.content.gsub(/<(#{Gtn::Boxify.box_classes}) title="([^"]*)">/) {
63
# box_type = $1
64
# title = $2
65
# box = Gtn::Boxify.generate_box(box_type, title, lang, page.path)
66
# box
67
# }
68
69
# page.content = page.content.gsub(/<\/\s*(#{Gtn::Boxify::box_classes})\s*>/) {
70
# box_type = $1
71
# "\n</div></div><!--#{box_type}-->"
72
# }
73
end
74
end
75
end
76
end
77
78