Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
galaxyproject
GitHub Repository: galaxyproject/training-material
Path: blob/main/_plugins/abbr.rb
1677 views
1
# frozen_string_literal: true
2
3
require 'jekyll'
4
5
module Jekyll
6
module Generators
7
##
8
# This class modifies the page contents to replace {abbr} with the associated
9
# abbreviation in a proper <abbr> tag. It's done as a generator because it's
10
# easier to operate once per page and be able to easily see if we've generated
11
# the abbreviation before.
12
#
13
# This could in theory operate as a simple tag, but I'm not sure how to keep
14
# track of "# of times seen per page" there.
15
class Abbreviate < Jekyll::Generator
16
safe true
17
18
def initialize(config) # :nodoc:
19
super
20
@config = config['abbreviate'] ||= {}
21
end
22
23
def generate(site) # :nodoc:
24
site.pages
25
.reject { |page| skip_layout?(page.data['layout']) }
26
.each { |page| abbreviate page }
27
site.posts.docs
28
.reject { |post| skip_layout?(post.data['layout']) }
29
.each { |post| abbreviate post }
30
end
31
32
private
33
34
def abbreviate(page) # :nodoc:
35
return unless page.data.key?('abbreviations')
36
37
seen = {}
38
page.data['abbreviations'].each do |abbr, definition|
39
page.content = page.content.gsub(/\{(#{abbr})\}/) do
40
if seen.key?(abbr)
41
firstdef = false
42
else
43
firstdef = true
44
seen[abbr] = true
45
end
46
47
if firstdef
48
"#{definition} (#{abbr})"
49
else
50
"<abbr title=\"#{definition}\">#{abbr}</abbr>"
51
end
52
end
53
end
54
end
55
56
def skip_layout?(layout)
57
to_skip = @config['skip_layouts'] || []
58
59
true if to_skip.empty?
60
61
to_skip.include?(layout)
62
end
63
end
64
end
65
end
66
67