jekyll-hyperlinkify-glossary 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41e9782180bc4ffd77b76f183662c4e31699de31caa970d8df05f0ea35d12ac1
4
+ data.tar.gz: cc88cf2676d7d04ca84f91087e634978fcef9ad9bff420aa28568c84f89b744b
5
+ SHA512:
6
+ metadata.gz: 2bd4a877e1c070f42cb71d35e90cc83795fd1b8850684d9e3380c90efaa8a2c2232dd6302f744060ec639b3cfc39d16c6aaf79d2f11cfcd0d6b54fc4fc69d925
7
+ data.tar.gz: ff77b9cc5540c1a73ff237473e6c6657d2e2377a30bcaa8868cc796529f401141da18149cb82b5f39543751df79ced0a199ca015bf4ced04db157640c61c4344
@@ -0,0 +1,15 @@
1
+ # Enrich all documents and pages with an array of all glossary entries and their synonyms
2
+ Jekyll::Hooks.register :site, :post_read do |site|
3
+ puts 'Enriching documents with glossary collection'
4
+ glossary_entries = []
5
+ site.collections["glossary"].docs.each do |item|
6
+ next_glossary_entry = [[item.url, item.data['title']],item.data['synonyms']].compact.reduce([], :|)
7
+ glossary_entries.push next_glossary_entry.map(&:downcase )
8
+ end
9
+ site.documents.each{ |document|
10
+ document.data['glossary_entries'] = glossary_entries
11
+ }
12
+ site.pages.each{ |document|
13
+ document.data['glossary_entries'] = glossary_entries
14
+ }
15
+ end
@@ -0,0 +1,65 @@
1
+ require 'jekyll'
2
+ require 'hooks/enrich_documents_with_glossary'
3
+
4
+ # Get all glossary entries from the page an replace all terms with appropriate hyperlinks
5
+ module Jekyll
6
+ class HyperlinkGlossaryEntries
7
+ BODY_START_TAG = "<body"
8
+ OPENING_BODY_TAG_REGEX = %r!<body(.*)>\s*!
9
+ class << self
10
+ # Replace all oocurances of glossary entries with a hyperlink.
11
+ def hyperlinkify(document)
12
+ @glossary_entries = document.data["glossary_entries"]
13
+ if @glossary_entries.nil?
14
+ puts "This document has no glossary entries: " + document.data["title"].to_s
15
+ return
16
+ end
17
+ @test_word = "icf"
18
+ @test_array = ["icf","inklusion"]
19
+ @test_url = "/glossary/icf.html"
20
+ document.output = if document.output.include? BODY_START_TAG
21
+ process_document(document)
22
+ else
23
+ process_html_body(document.output)
24
+ end
25
+
26
+ end
27
+
28
+ # Determine if the content should be processed.
29
+ def processable?(document)
30
+ (document.is_a?(Jekyll::Page) || document.write?) &&
31
+ (document.output_ext == ".html" || document.permalink&.end_with?("/")) &&
32
+ (document.data["jekyll-hyperlink-glossary"] != false)
33
+ end
34
+
35
+ private
36
+
37
+ # Process html content which has an body opening tag
38
+ def process_document(document)
39
+ head, opener, tail = document.output.partition(OPENING_BODY_TAG_REGEX)
40
+ body_content, *rest = tail.partition("</body>")
41
+
42
+ processed_markup = process_html_body(body_content, document.data["title"])
43
+
44
+ document.output = String.new(head) << opener << processed_markup << rest.join
45
+ end
46
+
47
+ # Process every word of content and replace glossary entries
48
+ def process_html_body(html, title)
49
+ @glossary_entries.each do |glossary_entry|
50
+ if glossary_entry[1].to_s != title.to_s.downcase
51
+ exclude_regex="\\b(?!<(h\\d|a).*>)(?!.*<\/(h\\d|a)>)(?!.html)"
52
+ glossary_entry_regex = Regexp.new("\\b"+glossary_entry.drop(1).join(exclude_regex+"|")+exclude_regex, true)
53
+ first_half_of_hyperlink = "<a href=\"#{ glossary_entry[0] }\">"
54
+ html = html.gsub(glossary_entry_regex, first_half_of_hyperlink+ '\0</a>')
55
+ end
56
+ end
57
+ html
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ Jekyll::Hooks.register %i[documents pages], :post_render do |document|
64
+ Jekyll::HyperlinkGlossaryEntries.hyperlinkify(document) if Jekyll::HyperlinkGlossaryEntries.processable?(document)
65
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-hyperlinkify-glossary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Marc Schmidt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.7'
27
+ description: A Jekyll gem to replace glossary entries in html with hyperlinks
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/hooks/enrich_documents_with_glossary.rb
34
+ - lib/jekyll-hyperlinkify-glossary.rb
35
+ homepage: https://github.com/MarcSchmidt/jekyll-hyperlinkify-glossary
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.19
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: A Jekyll gem to replace glossary entries in html with hyperlinks
58
+ test_files: []